Debian Codegraph @

tensilelite/Tensile/SolutionStructs/Naming.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 from copy import deepcopy
25 from functools import lru_cache
26
27 from Tensile.Common.Constants import MAX_FILENAME_LENGTH
28 from Tensile.Common.RequiredParameters import getRequiredParametersMin, getRequiredParametersFull
29
30 from .Problem import ProblemType
31
32
33 def getKeyNoInternalArgs(state, splitGSU: bool):
34 state_copy = deepcopy(state)
35 state_copy["ProblemType"]["GroupedGemm"] = False
36 if splitGSU:
37 state_copy["GlobalSplitU"] = "M" if (state_copy["GlobalSplitU"] > 1 or state_copy["GlobalSplitU"] == -1) else state_copy["GlobalSplitU"]
38 elif state["GlobalSplitU"] != 0:
39 state_copy["GlobalSplitU"] = "M"
40 state_copy["WorkGroupMapping"] = "M"
41 state_copy["WorkGroupMappingXCC"] = "M"
42 state_copy["WorkGroupMappingXCCGroup"] = "M"
43 state_copy["StaggerU"] = "M"
44 state_copy["StaggerUStride"] = "M"
45 state_copy["StaggerUMapping"] = "M"
46 state_copy["GlobalSplitUCoalesced"] = "M"
47 state_copy["GlobalSplitUWorkGroupMappingRoundRobin"] = "M"
48 return state_copy
49
50
51 @lru_cache(maxsize=None)
52 def getParameterNameAbbreviation( name: str ):
53 return ''.join(c for c in name if c.isupper())
54
55
56 @ lru_cache(maxsize=None)
57 def getPrimitiveParameterValueAbbreviation(key, value):
58 if isinstance(value, str):
59 return getParameterNameAbbreviation(value)
60 elif isinstance(value, bool):
61 return "1" if value else "0"
62 elif isinstance(value, int):
63 if value >= 0:
64 return "%u" % value
65 else: # -1 -> n1
66 return "n%01u" % abs(value)
67 elif isinstance(value, ProblemType): # will need to deal with this
68 return str(value)
69 elif isinstance(value, float):
70 val1 = int(value)
71 val2 = int(round(value*100)) - int(value)*100
72 if val2 > 0:
73 s = "%dp%s" % (val1,str(val2).zfill(2))
74 else:
75 s = "%d" % (val1)
76 return s
77
78
79 def getParameterValueAbbreviation(key, value):
80 if key == "ISA":
81 return f"{value[0]}{value[1]}{value[2]:x}"
82 compositieTypes = (dict, list, tuple,)
83 if not isinstance(value, compositieTypes):
84 return getPrimitiveParameterValueAbbreviation(key, value)
85 elif isinstance(value, tuple):
86 return ''.join(str(v) for v in value)
87 elif isinstance(value, list):
88 return '_'.join(getParameterValueAbbreviation(key, v) for v in value)
89 elif isinstance(value, dict):
90 return "_".join(f"{pos:d}{k:d}" for pos,k in value.items())
91 else:
92 raise Exception(f"Parameter {key}={value} is new object type ({type(value)})")
93
94
95 def _getName(state, requiredParameters: frozenset, splitGSU: bool, ignoreInternalArgs):
96
97 if "CustomKernelName" in state and state["CustomKernelName"]:
98 return state["CustomKernelName"]
99
100 gsuBackup = state["GlobalSplitU"]
101 ggBackup = state["ProblemType"]["GroupedGemm"]
102
103 if ignoreInternalArgs:
104 state["ProblemType"]["GroupedGemm"] = False
105 if splitGSU:
106 state["GlobalSplitU"] = "M" if (state["GlobalSplitU"] > 1 or state["GlobalSplitU"] == -1) else state["GlobalSplitU"]
107
108
109 requiredParametersTemp = set(requiredParameters.union(["GlobalSplitU"]))
110
111 if ignoreInternalArgs:
112 if state["GlobalSplitU"] > 0 or state["GlobalSplitU"] == -1:
113 requiredParametersTemp.discard("GlobalSplitU")
114 else:
115 requiredParametersTemp = requiredParametersTemp.union(["WorkGroupMapping",
116 "WorkGroupMappingXCC",
117 "WorkGroupMappingXCCGroup",
118 "StaggerU",
119 "StaggerUStride",
120 "StaggerUMapping",
121 "GlobalSplitUCoalesced",
122 "GlobalSplitUWorkGroupMappingRoundRobin"])
123 components = [f'{str(ProblemType(state["ProblemType"],printIndexAssignmentInfo=False))}']
124
125 if "MacroTile0" in state \
126 and "MacroTile1" in state \
127 and "DepthU" in state:
128 components.append(f'{getParameterNameAbbreviation("MacroTile")}{state["MacroTile0"]}x{state["MacroTile1"]}x{state["DepthU"]}')
129
130 if "MatrixInstM" in state:
131 components.append(f'{getParameterNameAbbreviation("MatrixInstruction")}{state["MatrixInstM"]}x{state["MatrixInstN"]}x{state["MatrixInstB"]}')
132 requiredParametersTemp.add("MIWaveTile")
133 else:
134 requiredParametersTemp.add("ThreadTile")
135
136 if state["UseCustomMainLoopSchedule"]:
137 components.append('CMS')
138
139 components.append('SN')
140 for key in sorted(state.keys()):
141 if key[0] != '_' and key != "CustomKernelName" and key in requiredParametersTemp:
142 components.append(f'{getParameterNameAbbreviation(key)}{getParameterValueAbbreviation(key, state[key])}')
143
144 state["GlobalSplitU"] = gsuBackup
145 state["ProblemType"]["GroupedGemm"] = ggBackup
146
147 return '_'.join(components)
148
149
150 def shortenFileBase(splitGSU, kernel):
151 base = getKernelNameMin(kernel, splitGSU)
152 if len(base) <= MAX_FILENAME_LENGTH:
153 return base
154 import hashlib
155 import base64
156 pivot = MAX_FILENAME_LENGTH * 3 // 4
157 firstPart = base[:pivot]
158 secondPart = base[pivot:]
159 secondHash = hashlib.sha256(secondPart.encode()).digest()
160 secondPart = base64.b64encode(secondHash, b'_-').decode()
161 return firstPart + secondPart
162
163
164 def getKernelFileBase(splitGSU: bool, kernel):
165 if "CustomKernelName" in kernel and kernel["CustomKernelName"]:
166 fileBase = kernel["CustomKernelName"]
167 else:
168 fileBase = shortenFileBase(splitGSU, kernel)
169 return fileBase
170
171
172 def getKernelNameMin(kernel, splitGSU: bool):
173 return _getName(kernel, getRequiredParametersMin(), splitGSU, True)
174
175
176 def getSolutionNameMin(solution, splitGSU: bool):
177 return _getName(solution, getRequiredParametersMin(), splitGSU, False)
178
179
180 def getSolutionNameFull(state, splitGSU: bool):
181 return _getName(state, getRequiredParametersFull(), splitGSU, False)