Debian Codegraph @

tensilelite/Tensile/KernelHelperNaming.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 from copy import deepcopy
26 from enum import IntEnum
27
28 from Tensile.Common.GlobalParameters import internalParameters
29 from Tensile.KernelWriterBetaOnly import KernelWriterBetaOnly
30 from Tensile.KernelWriterConversion import KernelWriterConversion
31 from Tensile.KernelWriterActivationEnumHeader import KernelWriterActivationEnumHeader
32 from Tensile.KernelWriterActivationFunction import KernelWriterActivationFunction
33 from Tensile.KernelWriterReduction import KernelWriterReduction
34
35
36 class KernelHelperEnum(IntEnum):
37 BetaOnly = 0
38 Conversion = 1
39 ActivationEnumHeader = 2
40 ActivationFunction = 3
41 Reduction = 4
42 All = 5
43
44
45 def conversionKernelNames(solution):
46 # need to check that the fields mutated in the the init function aren't used in the naming function
47 # e.g. usebias got set to zero if gradient which changes behavior of name function.
48 conversionKernelNames = []
49 loadVectorWidth = [1, 2] if solution["ProblemType"]["DataType"].isDouble() else [1, 2, 4]
50 gsuList = [internalParameters["GlobalSplitUPGR"]]
51 if solution["GlobalSplitUAlgorithm"] == "SingleBuffer":
52 gsuList = [1]
53 elif solution["GlobalSplitUAlgorithm"] == "MultipleBufferSingleKernel":
54 return
55 for vw in loadVectorWidth:
56 # for _ in gsuList: I don't think this does anything
57 if solution["ProblemType"]["UseBias"]:
58 typeList = solution["ProblemType"]["BiasDataTypeList"]
59 if solution["ProblemType"]["Gradient"]:
60 # # If gradient + bias D, generates a normal GSU kernel for bias D = nullptr case
61 conversionKernelNames.append(KernelWriterConversion.kernelName(solution, vw))
62 for btype in typeList:
63 conversionKernelNames.append(KernelWriterConversion.kernelName(solution, vw, btype))
64 else:
65 conversionKernelNames.append(KernelWriterConversion.kernelName(solution, vw))
66 return conversionKernelNames if conversionKernelNames else []
67
68
69 def activationEnumHeaderNames(solution):
70 activationEnumHeaderNames = []
71 if solution["ProblemType"]["ActivationType"] in ['all', 'hipblaslt_all']:
72 activationEnumHeaderNames.append(KernelWriterActivationEnumHeader.kernelName(solution))
73 return activationEnumHeaderNames
74
75
76 def activationFunctionNames(solution):
77 activationFunctionNames = []
78 if solution["ProblemType"]["ActivationType"] in ['all', 'hipblaslt_all']:
79 activationFunctionNames.append(KernelWriterActivationFunction.kernelName(solution))
80 return activationFunctionNames
81
82
83 def reductionKernelNames(solution):
84 reductionKernelNames = []
85 if solution["ProblemType"]["Gradient"] and solution["ProblemType"]["UseBias"]:
86 for btype in solution["ProblemType"]["BiasDataTypeList"]:
87 reductionKernelNames.append(KernelWriterReduction.kernelName(solution, btype))
88 return reductionKernelNames
89
90
91 def betaOnlyKernelNames(solution):
92 betaOnlyKernelNames = []
93 if (solution["GlobalSplitU"] > 1 or solution["GlobalSplitU"] == -1) or (solution["StreamK"] > 0 and solution["StreamKAtomic"] == 1):
94 if solution["ProblemType"]["UseBias"]:
95 for btype in solution["ProblemType"]["BiasDataTypeList"]:
96 betaOnlyKernelNames.append(KernelWriterBetaOnly.kernelName(solution, btype))
97 else:
98 betaOnlyKernelNames.append(KernelWriterBetaOnly.kernelName(solution))
99 return betaOnlyKernelNames
100
101
102 def kernelObjectNameCallables():
103 return [(KernelHelperEnum.Conversion, conversionKernelNames),
104 (KernelHelperEnum.ActivationEnumHeader, activationEnumHeaderNames),
105 (KernelHelperEnum.ActivationFunction, activationFunctionNames),
106 (KernelHelperEnum.Reduction, reductionKernelNames),
107 (KernelHelperEnum.BetaOnly, betaOnlyKernelNames)]
108
109
110 def initHelperKernelObjects(solution, kernelHelperType, cxxCompiler, isaInfoMap):
111 if kernelHelperType not in KernelHelperEnum:
112 raise Exception("Failed to find kernel helper type.")
113 result = []
114 if kernelHelperType == KernelHelperEnum.BetaOnly or kernelHelperType == KernelHelperEnum.All:
115 result.extend(initBetaOnlyKernelObjects(solution))
116 if kernelHelperType == KernelHelperEnum.Conversion or kernelHelperType == KernelHelperEnum.All:
117 result.extend(initConversionKernelObjects(solution, isaInfoMap))
118 if kernelHelperType == KernelHelperEnum.ActivationEnumHeader or kernelHelperType == KernelHelperEnum.All:
119 result.extend(initActivationEnumHeaderObjects(solution))
120 if kernelHelperType == KernelHelperEnum.ActivationFunction or kernelHelperType == KernelHelperEnum.All:
121 result.extend(initActivationFunctionObjects(solution, cxxCompiler, isaInfoMap))
122 if kernelHelperType == KernelHelperEnum.Reduction or kernelHelperType == KernelHelperEnum.All:
123 result.extend(initReductionKernelObjects(solution))
124 sortByEnum = lambda x: ("Enum" in x.getKernelName(), result.index(x))
125 return sorted(result, key=sortByEnum, reverse=True) # Ensure that we write Enum kernel helpers are first in list
126
127
128 def initBetaOnlyKernelObjects(solution):
129 betaOnlyKernelObjects = []
130 if (solution["GlobalSplitU"] > 1 or solution["GlobalSplitU"] == -1) or (solution["StreamK"] > 0 and solution["StreamKAtomic"] == 1):
131 if solution["ProblemType"]["UseBias"]:
132 for btype in solution["ProblemType"]["BiasDataTypeList"]:
133 state = {}
134 state["ProblemType"] = deepcopy(solution["ProblemType"])
135 state["ProblemType"]["GroupedGemm"] = False
136 state["ProblemType"]["BiasDataTypeList"] = []
137 state["ProblemType"]["BiasDataType"] = deepcopy(btype)
138 state["KernelLanguage"] = "Source"
139 state["_GlobalAccumulation"] = solution["_GlobalAccumulation"]
140 betaOnlyKernelObjects.append(KernelWriterBetaOnly(state))
141 else:
142 state = {}
143 state["ProblemType"] = deepcopy(solution["ProblemType"])
144 state["ProblemType"]["GroupedGemm"] = False
145 state["KernelLanguage"] = "Source"
146 state["_GlobalAccumulation"] = solution["_GlobalAccumulation"]
147 betaOnlyKernelObjects.append(KernelWriterBetaOnly(state))
148 return betaOnlyKernelObjects
149
150
151 def initConversionKernelObjects(solution, isaInfoMap):
152 conversionKernelObjects = []
153 loadVectorWidth = [1, 2] if solution["ProblemType"]["DataType"].isDouble() else [1, 2, 4]
154 genPGRPostKernels = True
155 gsuList = [internalParameters["GlobalSplitUPGR"]]
156 if solution["GlobalSplitUAlgorithm"] == "SingleBuffer":
157 genPGRPostKernels = False
158 gsuList = [1]
159 elif solution["GlobalSplitUAlgorithm"] == "MultipleBufferSingleKernel":
160 return conversionKernelObjects
161 for vw in loadVectorWidth:
162 for globalSplitU in gsuList:
163 unrollOnly = False if globalSplitU == internalParameters["GlobalSplitUPGR"] else True
164 if solution["ProblemType"]["UseBias"]:
165 typeList = solution["ProblemType"]["BiasDataTypeList"]
166 if solution["ProblemType"]["Gradient"]:
167 # If gradient + bias D, generates a normal GSU kernel for bias D = nullptr case
168 state = {}
169 state["ProblemType"] = deepcopy(solution["ProblemType"])
170 state["ProblemType"]["GroupedGemm"] = False
171 state["ProblemType"]["UseBias"] = 0
172 state["GenPGRPostKernels"] = genPGRPostKernels
173 state["KernelLanguage"] = "Source"
174 state["GlobalSplitU"] = globalSplitU
175 state["UnrollOnly"] = unrollOnly
176 state["_GlobalAccumulation"] = solution["_GlobalAccumulation"]
177 state["ActivationFused"] = solution["ActivationFused"]
178 conversionKernelObjects.append(KernelWriterConversion(state, vw, isaInfoMap))
179 for btype in typeList:
180 state = {}
181 state["ProblemType"] = deepcopy(solution["ProblemType"])
182 state["ProblemType"]["GroupedGemm"] = False
183 state["ProblemType"]["BiasDataTypeList"] = []
184 state["ProblemType"]["BiasDataType"] = deepcopy(btype)
185 state["GenPGRPostKernels"] = genPGRPostKernels
186 state["KernelLanguage"] = "Source"
187 state["GlobalSplitU"] = globalSplitU
188 state["UnrollOnly"] = unrollOnly
189 state["_GlobalAccumulation"] = solution["_GlobalAccumulation"]
190 state["ActivationFused"] = solution["ActivationFused"]
191 conversionKernelObjects.append(KernelWriterConversion(state, vw, isaInfoMap))
192 else:
193 state = {}
194 state["ProblemType"] = deepcopy(solution["ProblemType"])
195 state["ProblemType"]["GroupedGemm"] = False
196 state["GenPGRPostKernels"] = genPGRPostKernels
197 state["KernelLanguage"] = "Source"
198 state["GlobalSplitU"] = globalSplitU
199 state["UnrollOnly"] = unrollOnly
200 state["_GlobalAccumulation"] = solution["_GlobalAccumulation"]
201 state["ActivationFused"] = solution["ActivationFused"]
202 conversionKernelObjects.append(KernelWriterConversion(state, vw, isaInfoMap))
203 return conversionKernelObjects
204
205
206 def initActivationEnumHeaderObjects(solution):
207 activationEnumHeaderObjects = []
208 if solution["ProblemType"]["ActivationType"] in ['all', 'hipblaslt_all']:
209 state = {}
210 state["ProblemType"] = deepcopy(solution["ProblemType"])
211 state["ProblemType"]["GroupedGemm"] = False
212 state["KernelLanguage"] = "Source"
213 activationEnumHeaderObjects.append(KernelWriterActivationEnumHeader(state))
214 return activationEnumHeaderObjects
215
216
217 def initActivationFunctionObjects(solution, cxxCompiler, isaInfoMap):
218 activationFunctionObjects = []
219 if solution["ProblemType"]["ActivationType"] in ['all', 'hipblaslt_all']:
220 state = {}
221 state["ProblemType"] = deepcopy(solution["ProblemType"])
222 state["ProblemType"]["GroupedGemm"] = False
223 state["KernelLanguage"] = "Source"
224 state["Kernel"] = {"WavefrontSize": solution["WavefrontSize"], "ISA": tuple(solution["ISA"])}
225 activationFunctionObjects.append(KernelWriterActivationFunction(state, cxxCompiler, list(isaInfoMap.keys())))
226 return activationFunctionObjects
227
228
229 def initReductionKernelObjects(solution):
230 reductionKernelObjects = []
231 if solution["ProblemType"]["Gradient"] and solution["ProblemType"]["UseBias"]:
232 for btype in solution["ProblemType"]["BiasDataTypeList"]:
233 state = {}
234 state["ProblemType"] = deepcopy(solution["ProblemType"])
235 state["ProblemType"]["GroupedGemm"] = False
236 state["ProblemType"]["BiasDataTypeList"] = []
237 state["ProblemType"]["BiasDataType"] = deepcopy(btype)
238 reductionKernelObjects.append(KernelWriterReduction(state))
239 return reductionKernelObjects