Debian Codegraph @

tensilelite/Tensile/TensileLogic/Run.py

raw symbols SCIP
1 ################################################################################
2 #
3 # Copyright (C) 2025 Advanced Micro Devices, Inc. All rights reserved.
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
22 #
23 ################################################################################
24
25
26 import functools
27
28 from pathlib import Path
29 from typing import List, Dict, NamedTuple
30
31 from Tensile.Common import ParallelMap2, print1, IsaVersion, IsaInfo, setVerbosity
32 from Tensile.Common.Architectures import SUPPORTED_ISA
33 from Tensile.Common.Capabilities import makeIsaInfoMap
34 from Tensile.Common.GlobalParameters import assignGlobalParameters
35 from Tensile.LibraryIO import readYAML
36 from Tensile.Toolchain.Validators import validateToolchain
37
38 from .ParseArguments import parseArguments
39 from .ValidMatrixInstruction import _validateMatrixInstruction
40 from .ValidWorkGroup import _validateWorkGroup
41 from .HandleCustomKernel import handleCustomKernel, hasCustomKernel
42
43
44 class Check(NamedTuple):
45 OnlyCustomKernels: bool
46 All: bool
47
48
49 def _runChecks(
50 logicPath: Path, isaInfoMap: Dict[IsaVersion, IsaInfo], check: Check, files: List[Path]
51 ):
52 """
53 Run checks on the given logic files.
54
55 Args:
56 logicPath: Path to a directory containing logic files or to an individual logic file.
57 isaInfoMap: Map of IsaVersion to IsaInfo.
58 check: Object containing flags for checking.
59 files: List of logic files to check.
60
61 Returns:
62 Tuple of (keep, total) where keep is the number of unrejected solutions and
63 total is the total number of solutions parsed.
64 """
65 keep, total = 0, 0
66 for file in files:
67 if "Experimental" in file.parts:
68 return keep, total
69
70 solutions = []
71 data = readYAML(file)
72 problemType = data[4]
73 if check.OnlyCustomKernels and hasCustomKernel(file):
74 print1(f">> {file.relative_to(logicPath)}")
75 solutions = data[5] # Solutions are the 5th index
76 elif check.All:
77 print1(f">> {file.relative_to(logicPath)}")
78 solutions = data[5] # Solutions are the 5th index
79
80 for s in solutions:
81 s, isCustom = handleCustomKernel(s, isaInfoMap)
82 if check.OnlyCustomKernels and not isCustom:
83 continue
84
85 s["ProblemType"] = problemType
86 if all(
87 [
88 _validateMatrixInstruction(s, isaInfoMap, file.relative_to(logicPath)),
89 _validateWorkGroup(s, file.relative_to(logicPath)),
90 ]
91 ):
92 keep += 1
93 total += 1
94
95 return keep, total
96
97
98 def _setup():
99 args = parseArguments()
100
101 setVerbosity(args.Verbose)
102 jobs = int(args.Jobs)
103 cxxCompiler = validateToolchain(args.CxxCompiler)
104 logicPath = Path(args.LogicPath)
105
106 if not any([args.CheckAll, args.CheckOnlyCustomKernels]):
107 print1("No checks specified. Exiting.")
108 exit(0)
109 check = Check(
110 OnlyCustomKernels=args.CheckOnlyCustomKernels,
111 All=args.CheckAll,
112 )
113
114 if logicPath.is_file() and logicPath.suffix == ".yaml":
115 files = [logicPath]
116 else:
117 pattern = "**/*.yaml"
118 files = list(logicPath.glob(pattern))
119 if len(files) == 0:
120 print1(f"No files found in {logicPath}")
121 exit(1)
122 print1(f"Found {len(files)} files")
123
124 isaInfoMap = makeIsaInfoMap(SUPPORTED_ISA, str(cxxCompiler))
125 assignGlobalParameters({"PrintSolutionRejectionReason": True}, isaInfoMap)
126
127 return jobs, isaInfoMap, logicPath, files, check
128
129
130 def main():
131 jobs, isaInfoMap, logicPath, files, check = _setup()
132
133 batchSize = len(files) // min(len(files), jobs)
134 batches = (files[i : i + batchSize] for i in range(0, len(files), batchSize))
135
136 fn = functools.partial(_runChecks, logicPath, isaInfoMap, check)
137 keep, total = 0, 0
138
139 results = ParallelMap2(fn, batches, multiArg=False, procs=jobs, return_as="list")
140
141 for _keep, _total in results:
142 keep += _keep
143 total += _total
144
145 rejects = total - keep
146 print(f"Total {total} solutions")
147 print(f"Keep {keep} solutions")
148 print(f"Reject {rejects} solutions")
149
150 if rejects > 0:
151 exit(1)