Debian Codegraph @

tensilelite/Tensile/Components/Signature.py

raw symbols SCIP
1 ################################################################################
2 #
3 # Copyright (C) 2022-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 rocisa.code import SignatureBase
26 from rocisa.enum import SignatureValueKind as SVK
27 from ..Component import Signature
28 from ..Common import DataDirection
29 from ..Activation import ActivationType
30
31 from dataclasses import dataclass, field
32
33 @dataclass
34 class UserArgumentsInfo:
35 # Common args
36 commonArgsNum: int = 0
37 commonArgsSize: int = 0
38 # variable related fixed parameters
39 alphaMaxSize: int = 16
40 alphaMaxRegisterSize: int = field(init=False)
41 betaMaxSize: int = 16
42 betaMaxRegisterSize: int = field(init=False)
43 scaleASize: int = 0
44 scaleBSize: int = 0
45 scaleCSize: int = 0
46 scaleDSize: int = 0
47 actMaxSize: int = 4
48 actMaxRegisterSize: int = field(init=False)
49 # gemm related
50 gemmArgumentSize: int = 0
51 # Epilogue related
52 scaleAlphaVecSize: int = 0
53 biasSize: int = 0
54 eSize: int = 0
55 activationSize: int = 0
56 factorDimSize: int = 0
57 # Total argument size
58 totalSize: int = 0
59
60 def __post_init__(self):
61 self.alphaMaxRegisterSize = self.alphaMaxSize // 4
62 self.betaMaxRegisterSize = self.betaMaxSize // 4
63 self.actMaxRegisterSize = self.actMaxSize // 4
64
65 def getSrcValueType(kernel, isTypeA):
66 # special cases for F8 datatypes
67 if kernel["ProblemType"]["DataType"].isAnyFloat8():
68 srcValueType = "FP8"
69 elif kernel["ProblemType"]["DataType"].isAnyBFloat8():
70 srcValueType = "BF8"
71 elif kernel["ProblemType"]["DataType"].isAnyFloat8BFloat8():
72 srcValueType = "FP8" if isTypeA else "BF8"
73 elif kernel["ProblemType"]["DataType"].isAnyBFloat8Float8():
74 srcValueType = "BF8" if isTypeA else "FP8"
75 else:
76 if isTypeA:
77 srcValueType = kernel["ProblemType"]["DataTypeA"].toNameAbbrev().upper()
78 else:
79 srcValueType = kernel["ProblemType"]["DataTypeB"].toNameAbbrev().upper()
80
81 srcValueType = srcValueType.lower()
82 return srcValueType
83
84 def getDstValueType(kernel):
85 # special cases for F8 datatypes
86 if kernel["ProblemType"]["DataType"].isAnyFloat8():
87 dstValueType = "FP8"
88 elif kernel["ProblemType"]["DataType"].isAnyBFloat8():
89 dstValueType = "BF8"
90 else:
91 dstValueType = kernel["ProblemType"]["DataType"].toNameAbbrev().upper()
92
93 dstValueType = dstValueType.lower()
94 return dstValueType
95
96 # Creates kernel header, compatible with code object version 4 and up. V2 and V3 no longer supported.
97 class SignatureDefault(Signature):
98
99 def __call__(self, writer) -> SignatureBase:
100 kernel = writer.states.kernel
101
102 userArgumentsInfo = UserArgumentsInfo()
103
104 # kern arg size
105 kernArgReg = 0
106 kernArgReg += 3*writer.states.rpga
107 kernArgReg += max(1,int(writer.states.bpeAB/4)) # alpha
108 if kernel["ProblemType"]["UseBeta"]:
109 kernArgReg += max(1,int(writer.states.bpeCexternal/4)) # beta
110 kernArgReg += kernel["ProblemType"]["NumIndicesC"] # strides
111 kernArgReg += kernel["ProblemType"]["NumIndicesC"] # strides
112 kernArgReg += len(kernel["ProblemType"]["IndexAssignmentsA"]) # strides
113 kernArgReg += len(kernel["ProblemType"]["IndexAssignmentsB"]) # strides
114 if not kernel["ProblemType"]["UseInitialStridesAB"]:
115 kernArgReg -= 2 # strides
116 if not kernel["ProblemType"]["UseInitialStridesCD"]:
117 kernArgReg -= 2 # strides
118 kernArgReg += kernel["ProblemType"]["NumIndicesSummation"]
119 kernArgReg += kernel["ProblemType"]["NumIndicesC"]
120 if writer.debugConfig.debugKernel:
121 kernArgReg += writer.states.rpga # debug buffer
122 # kernArgBytes = kernArgReg * 4 # bytes/reg
123
124 group_segment_size = kernel["LdsNumBytes"]
125
126 sgprWgZ = 1 if kernel["ProblemType"]["NumIndicesC"] > 2 else 0
127 signature = SignatureBase(kernelName=writer.states.kernelName,
128 kernArgsVersion=kernel["InternalSupportParams"]["KernArgsVersion"],
129 codeObjectVersion=kernel["CodeObjectVersion"],
130 groupSegmentSize=group_segment_size,
131 sgprWorkGroup=(1, 1, sgprWgZ),
132 vgprWorkItem=0,
133 flatWorkGroupSize=(kernel["NumThreads"]),
134 preloadKernArgs=bool(kernel["PreloadKernArgs"]))
135
136 # General Argument info
137 signature.addArg( "Gemm info", SVK.SIG_VALUE, "u32")
138 signature.addArg("kernel info0", SVK.SIG_VALUE, "u32")
139 signature.addArg("kernel info1", SVK.SIG_VALUE, "u32")
140 signature.addArg("numWG", SVK.SIG_VALUE, "u32")
141 # When modify the size, please also update TENSILE_COMMON_KERNEL_ARGS_SIZE in ContractionSolution.hpp
142 userArgumentsInfo.commonArgsNum += 4
143 userArgumentsInfo.commonArgsSize = userArgumentsInfo.commonArgsNum * writer.states.bpr
144
145 srcValueTypeA = getSrcValueType(kernel, True)
146 srcValueTypeB = getSrcValueType(kernel, False)
147 dstValueType = kernel["ProblemType"]["DestDataType"].toNameAbbrev()
148 cptValueType = kernel["ProblemType"]["ComputeDataType"].toNameAbbrev()
149 biasValueType = "void"
150 actValueType = kernel["ProblemType"]["ActivationComputeDataType"].toNameAbbrev()
151
152 for i in range(0, writer.states.numSgprSizesFree):
153 signature.addArg( "SizesFree%u"%i, SVK.SIG_VALUE, "u32")
154 userArgumentsInfo.gemmArgumentSize += 4
155
156 for i in range(0, writer.states.numSgprSizesSum):
157 signature.addArg( "SizesSum%u"%i, SVK.SIG_VALUE, "u32")
158 userArgumentsInfo.gemmArgumentSize += 4
159
160 if writer.debugConfig.debugKernel:
161 signature.addArg("AddressDbg", SVK.SIG_GLOBALBUFFER, "struct", "generic")
162 signature.addArg( "D", SVK.SIG_GLOBALBUFFER, dstValueType, "generic")
163 signature.addArg( "C", SVK.SIG_GLOBALBUFFER, dstValueType, "generic")
164 signature.addArg( "A", SVK.SIG_GLOBALBUFFER, srcValueTypeA, "generic")
165 signature.addArg( "B", SVK.SIG_GLOBALBUFFER, srcValueTypeB, "generic")
166 userArgumentsInfo.gemmArgumentSize += (8 + 8 + 8 + 8) # A, B, C, D buffer
167
168 if kernel["ProblemType"]["Sparse"]:
169 signature.addArg("MetaData", SVK.SIG_GLOBALBUFFER, "void" , "generic")
170
171 if kernel["StreamK"] > 0 and kernel["StreamKAtomic"] == 0:
172 signature.addArg("AddressWS", SVK.SIG_GLOBALBUFFER, cptValueType, "generic")
173 signature.addArg("AddressFlags", SVK.SIG_GLOBALBUFFER, dstValueType, "generic")
174
175 for i in range(0, writer.states.d.numSgprStrides):
176 signature.addArg( "strideD%u"%i, SVK.SIG_VALUE, "u32")
177 userArgumentsInfo.gemmArgumentSize += 4
178
179 for i in range(0, writer.states.c.numSgprStrides):
180 signature.addArg( "strideC%u"%i, SVK.SIG_VALUE, "u32")
181 userArgumentsInfo.gemmArgumentSize += 4
182
183 for i in range(0, writer.states.a.numSgprStrides):
184 signature.addArg( "strideA%u"%i, SVK.SIG_VALUE, "u32")
185 userArgumentsInfo.gemmArgumentSize += 4
186
187 for i in range(0, writer.states.b.numSgprStrides):
188 signature.addArg( "strideB%u"%i, SVK.SIG_VALUE, "u32")
189 userArgumentsInfo.gemmArgumentSize += 4
190
191 if kernel["ProblemType"]["Sparse"]:
192 for i in range(0, writer.states.m.numSgprStrides):
193 signature.addArg( "strideMetadata%u"%i, SVK.SIG_VALUE, "u32")
194
195 for idxChar in kernel["PackedC0IdxChars"][:-1]:
196 signature.addArg("MagicNumberSize%s"%idxChar, SVK.SIG_VALUE, "u32")
197 signature.addArg( "MagicShiftSize%s"%idxChar, SVK.SIG_VALUE, "u32")
198
199 # Note: We use packed f16 if alpha and beta are f16
200 pack_cptValueType = 'pkf16' if kernel["ProblemType"]["ComputeDataType"].isHalf() else cptValueType
201 signature.addArg( "alpha", SVK.SIG_VALUE, pack_cptValueType)
202 if kernel["ProblemType"]["UseBeta"]:
203 signature.addArg("beta", SVK.SIG_VALUE, pack_cptValueType)
204 # These are fixed sizes
205 userArgumentsInfo.gemmArgumentSize += userArgumentsInfo.alphaMaxSize
206 userArgumentsInfo.gemmArgumentSize += userArgumentsInfo.betaMaxSize
207
208 if kernel["StreamK"]:
209 # StreamK args
210 signature.addArg("ItersPerTile", SVK.SIG_VALUE, "u32")
211 signature.addArg("MagicNumberItersPerTile", SVK.SIG_VALUE, "u32")
212 signature.addArg("MagicShiftItersPerTile", SVK.SIG_VALUE, "u32")
213 signature.addArg("TotalIters", SVK.SIG_VALUE, "u32")
214 signature.addArg("SKItersPerWG", SVK.SIG_VALUE, "u32")
215 userArgumentsInfo.gemmArgumentSize += 20
216 if kernel["StreamK"] >= 2: # Two-tile SK
217 signature.addArg("skGrid", SVK.SIG_VALUE, "u32")
218 signature.addArg("skTiles", SVK.SIG_VALUE, "u32")
219 userArgumentsInfo.gemmArgumentSize += 8
220
221 if kernel["ProblemType"]["UseScaleAB"]:
222 signature.addArg("AddressScaleA", SVK.SIG_GLOBALBUFFER, cptValueType, "generic")
223 signature.addArg("AddressScaleB", SVK.SIG_GLOBALBUFFER, cptValueType, "generic")
224 userArgumentsInfo.scaleASize += 8
225 userArgumentsInfo.scaleBSize += 8
226 if kernel["ProblemType"]["UseScaleCD"]:
227 signature.addArg("AddressScaleC", SVK.SIG_GLOBALBUFFER, cptValueType, "generic")
228 signature.addArg("AddressScaleD", SVK.SIG_GLOBALBUFFER, cptValueType, "generic")
229 userArgumentsInfo.scaleCSize += 8
230 userArgumentsInfo.scaleDSize += 8
231
232 if kernel["ProblemType"]["UseScaleAlphaVec"]:
233 signature.addArg("AddressScaleAlphaVec", SVK.SIG_GLOBALBUFFER, cptValueType, "generic")
234 if kernel["ProblemType"]["UseScaleAlphaVec"] == 3:
235 userArgumentsInfo.factorDimSize =4
236
237 userArgumentsInfo.scaleAlphaVecSize += 8
238
239 if writer.states.useBias != DataDirection.NONE:
240 signature.addArg("bias", SVK.SIG_GLOBALBUFFER, biasValueType, "generic") # Note: We append the data in ws_d
241 if writer.states.needBiasType:
242 signature.addArg("biasType", SVK.SIG_VALUE, "u32")
243 signature.addArg("StrideBias", SVK.SIG_VALUE, "u32")
244 if kernel["ProblemType"]["UseBias"] == 3:
245 userArgumentsInfo.factorDimSize = 4
246 userArgumentsInfo.biasSize += (8 + 4 + 4)
247
248 if userArgumentsInfo.factorDimSize == 4:
249 signature.addArg("factorDim", SVK.SIG_VALUE, "u32")
250
251 if kernel["ProblemType"]["UseE"]:
252 signature.addArg( "E", SVK.SIG_GLOBALBUFFER, cptValueType, "generic")
253 for i in range(0, writer.states.e.numSgprStrides):
254 signature.addArg("StrideE%u"%i, SVK.SIG_VALUE, "u32")
255 userArgumentsInfo.eSize += 8
256 for i in range(0, writer.states.e.numSgprStrides):
257 userArgumentsInfo.eSize += 4
258
259 if ((kernel["ProblemType"]["ActivationType"] != 'none') and kernel["ActivationFused"]):
260 if kernel["ProblemType"]["ActivationComputeDataType"].isHalf():
261 actValueType = 'pkf16'
262 for name in kernel["ProblemType"]["ActivationType"].getAdditionalArgStringList():
263 signature.addArg( name, SVK.SIG_VALUE, actValueType)
264 if kernel["ProblemType"]["ActivationType"] in ['all', 'hipblaslt_all'] :
265 signature.addArg( "activationType", SVK.SIG_VALUE, "u32")
266
267 # TODO- combine one workspace
268 if (kernel["ProblemType"]["OutputAmaxD"]):
269 signature.addArg( "AddrAmaxOut", SVK.SIG_GLOBALBUFFER, cptValueType, "generic")
270 signature.addArg( "AmaxWS", SVK.SIG_GLOBALBUFFER, cptValueType, "generic")
271 signature.addArg( "AmaxSync", SVK.SIG_GLOBALBUFFER, "u32", "generic")
272
273 if (kernel["GlobalSplitUAlgorithm"] == 'MultipleBufferSingleKernel'):
274 signature.addArg( "dstD", SVK.SIG_GLOBALBUFFER, dstValueType, "generic")
275 signature.addArg( "Synchronizer", SVK.SIG_GLOBALBUFFER, cptValueType, "generic")
276 signature.addArg( "GSUSync", SVK.SIG_VALUE, "u32")
277
278 activationType = ActivationType("all")
279 for name in activationType.getAdditionalArgStringList():
280 userArgumentsInfo.activationSize += userArgumentsInfo.actMaxSize
281 userArgumentsInfo.activationSize += 4 # Type size
282
283 # Calculate total size
284 userArgumentsInfo.totalSize = userArgumentsInfo.gemmArgumentSize + \
285 userArgumentsInfo.scaleASize + \
286 userArgumentsInfo.scaleBSize + \
287 userArgumentsInfo.scaleCSize + \
288 userArgumentsInfo.scaleDSize + \
289 userArgumentsInfo.scaleAlphaVecSize + \
290 userArgumentsInfo.biasSize + \
291 userArgumentsInfo.factorDimSize + \
292 userArgumentsInfo.eSize + \
293 userArgumentsInfo.activationSize
294
295 writer.states.userArgsInfo = userArgumentsInfo
296
297 self.addOptConfigComment(signature,
298 tt=[kernel["ThreadTile0"], kernel["ThreadTile1"]],
299 sg=[kernel["SubGroup0"], kernel["SubGroup1"]],
300 vwA=kernel["VectorWidthA"],
301 vwB=kernel["VectorWidthB"],
302 glvwA=kernel["GlobalReadVectorWidthA"],
303 glvwB=kernel["GlobalReadVectorWidthB"],
304 d2lA=kernel["DirectToLdsA"],
305 d2lB=kernel["DirectToLdsB"],
306 useSgprForGRO=kernel["_UseSgprForGRO"])
307
308 return signature
309
310 def addOptConfigComment(self, signature: SignatureBase, tt, sg, vwA, vwB, glvwA, glvwB, d2lA, d2lB, useSgprForGRO):
311 signature.addDescriptionTopic("Optimizations and Config:")
312 signature.addDescriptionBlock("ThreadTile= %u x %u" % (tt[0], tt[1]) )
313 signature.addDescriptionBlock("SubGroup= %u x %u" % (sg[0], sg[1]) )
314 signature.addDescriptionBlock("VectorWidthA=%u" % vwA )
315 signature.addDescriptionBlock("VectorWidthB=%u" % vwB )
316 signature.addDescriptionBlock("GlobalReadVectorWidthA=%u, GlobalReadVectorWidthB=%u" % (glvwA, glvwB) )
317 signature.addDescriptionBlock("DirectToLdsA=%s" % d2lA )
318 signature.addDescriptionBlock("DirectToLdsB=%s" % d2lB )
319 signature.addDescriptionBlock("UseSgprForGRO=%s" % useSgprForGRO )