Debian Codegraph @

tensilelite/Tensile/Components/LraTileAssignment.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 Module
26 from rocisa.container import vgpr, ContinuousRegister
27 from rocisa.instruction import VAddU32
28 from rocisa.functions import vectorStaticRemainder, \
29 vectorStaticDivideAndRemainder, vectorStaticDivide, vectorStaticMultiply, \
30 vectorStaticMultiplyAdd
31
32 from ..Component import LraTileAssignment, LraTileProperties
33 from dataclasses import dataclass
34
35 @dataclass
36 class LraTilePropertiesMFMA(LraTileProperties):
37 dividendForKId: int
38 num1DBlocks: int
39 num1DWaves: int
40 dividedForBlkId: int
41 dividedForWaveId: int
42 vectorWidth: int
43 maxKId: int
44
45 class LraTileAssignmentVALU(LraTileAssignment):
46 kernel = {"EnableMatrixInstruction": False}
47
48 """
49 Local Read Addresses: Tile Assignment
50 """
51 def __call__(self, writer, kernel, tP):
52 module = Module("LraTileAssignmentVALU")
53
54 # allocate resources
55 qReg = writer.vgprPool.checkOut(1,"qReg") # quotient
56 rReg = writer.vgprPool.checkOut(1,"rReg") # remainder
57 # dot2: currently only support unroll major LDS
58 tc = tP["tensorChar"]
59 umlds = kernel["UnrollMajorLDS%s" % tc]
60 LdsPad = kernel["LdsPad%s" % tc] if kernel["LdsBlockSizePerPad%s" % tc] == 0 else 0
61 strideTile = kernel["_DepthU%s"%tc] + LdsPad if umlds else 1
62 tmpVgpr = writer.vgprPool.checkOutAligned(2,2,"tmpVgpr")
63 tmpVgprRes = ContinuousRegister(tmpVgpr, 2)
64
65 with writer.allocTmpSgpr(1) as tmpSgprInfo:
66 if tP["tileIdx"] == 0:
67 # kStr += "%slr%s = serial %% SG%s%s%s" \
68 # % (writer.commentPrefix, tP["tileChar"], tP["tileChar"], \
69 # writer.commentSuffix, writer.endLine)
70
71 # constant
72 dividendReg = "Serial" # local serial
73 divisor = kernel["SubGroup0"]
74 # dot2: waveSplitK
75 if kernel["UseDotInstruction"]:
76 if kernel["NumWaveSplitK"] > 1:
77 newSerial = writer.vgprPool.checkOut(1,"newSerial")
78 module.add(vectorStaticDivide(newSerial, dividendReg, kernel["NumWaveSplitK"], tmpVgprRes, \
79 "Divided by NumWaveSplitK(%u)" % kernel["NumWaveSplitK"]))
80 # generate instruction
81 module.add(vectorStaticDivideAndRemainder(qReg, rReg, newSerial, divisor, tmpVgprRes))
82 # tile offset
83 module.add(vectorStaticMultiply(vgpr(rReg), vgpr(rReg), strideTile, tmpSgprInfo, \
84 "1. M offset: mOffset = mIdx * mStride(%u)" % strideTile))
85 writer.vgprPool.checkIn(newSerial)
86 else:
87 module.add(vectorStaticDivideAndRemainder(qReg, rReg, dividendReg, divisor, tmpVgprRes))
88 # tile offset
89 module.add(vectorStaticMultiply(vgpr(rReg), vgpr(rReg), strideTile, tmpSgprInfo, \
90 "1. M offset: mOffset = mIdx * mStride(%u)" % strideTile))
91 else:
92 module.add(vectorStaticDivideAndRemainder(qReg, rReg, dividendReg, divisor, tmpVgprRes))
93
94 # release and return resource
95 tP["gpr"]["lro"] = rReg
96 writer.tmplro = qReg
97 else:
98 # kStr += "%slr%s = (serial / SG%s) %% SG%s%s%s" \
99 # % (writer.commentPrefix, tP["tileChar"], tP["tileChar"], \
100 # tP["tileChar"], writer.commentSuffix, writer.endLine)
101
102 # constant
103 divisor = kernel["SubGroup1"]
104 dividendReg = writer.tmplro
105 # generate instruction
106 module.add(vectorStaticDivideAndRemainder(qReg, rReg, dividendReg, divisor, tmpVgprRes))
107
108 if kernel["UseDotInstruction"]:
109 # tile offset
110 module.add(vectorStaticMultiply(vgpr(rReg), vgpr(rReg), strideTile, tmpSgprInfo, \
111 "1. N offset: nOffset = nIdx * nStride(%u)" % strideTile))
112
113 # release and return resource
114 tP["gpr"]["lro"] = rReg
115
116 writer.vgprPool.checkIn(writer.tmplro) # old
117 writer.vgprPool.checkIn(qReg)
118
119 writer.vgprPool.checkIn(tmpVgpr)
120
121 return module
122
123 class LraTileAssignmentMFMA(LraTileAssignment):
124 kernel = {"EnableMatrixInstruction": True}
125
126 """
127 Local Read Addresses: Tile Assignment A/B
128 """
129 def __call__(self, writer, kernel, tP):
130 module = Module("LraTileAssignmentMFMA")
131 module.addComment0("lr%s" % tP["tileChar"])
132 # alloc vgpr
133 tReg = writer.vgprPool.checkOut(1,"tReg") # remainder
134 kReg = writer.vgprPool.checkOut(1,"kReg") # remainder
135 tmpVgpr = writer.vgprPool.checkOutAligned(2,2,"tmpVgpr")
136 tmpVgprRes = ContinuousRegister(tmpVgpr, 2)
137
138 module.add(self.LraTileAssignmentCode(writer, kernel, tP, tReg, kReg, tmpVgprRes))
139
140 # release register
141 tP["gpr"]["lro"] = tReg
142 writer.vgprPool.checkIn(kReg)
143 writer.vgprPool.checkIn(tmpVgpr)
144
145 return module
146
147 def LraTileAssignmentCode(self, writer, kernel, tP, tReg, kReg, tmpVgprRes, dividendReg="Serial", isDTVAB=False):
148 module = Module("LraTileAssignmentCode")
149
150 # alloc vgpr
151 enableLDSTr = tP["enableLDSTr"]
152 dummy = writer.vgprPool.checkOut(1,"dummy")
153 if enableLDSTr:
154 sReg = writer.vgprPool.checkOut(1,"sReg") # remainder
155 mReg = writer.vgprPool.checkOut(1,"mReg") # remainder
156
157 isWmma_v1 = writer.states.asmCaps["HasWMMA_V1"]
158
159 # get constant parameter
160 tc = tP["tensorChar"]
161 tile01 = tP["tile01Idx"]
162 waveWidth = writer.states.kernel["WavefrontSize"]
163 inputPerThread = kernel["LocalReadVectorWidth"] if not writer.states.inTailLoop else kernel["MIInputPerThread%s"%tc]
164 if kernel["ProblemType"]["Sparse"]:
165 if (kernel["ProblemType"]["Sparse"] == 2 and tP["isB"]) or (kernel["ProblemType"]["Sparse"] == 1 and tP["isA"]):
166 inputPerThread = inputPerThread // 2
167 elif tP["isM"]:
168 inputPerThread = inputPerThread // 8
169 LdsPad = kernel["LdsPad%s" % tc] if kernel["LdsBlockSizePerPad%s" % tc] == 0 else 0
170
171 # parameter for get each type index
172 dividendForKId = kernel["MatrixInstM"] * kernel["MatrixInstB"]
173 num1DBlocks = kernel["MatrixInstBM"] if (tile01 == 0) else kernel["MatrixInstBN"]
174 num1DWaves = kernel["MIWaveGroup"][0] if (tile01 == 0) else kernel["MIWaveGroup"][1]
175 if kernel["SourceSwap"]:
176 dividedForBlkId = kernel["MatrixInstM"] if (tile01 == 0) else (kernel["MatrixInstM"] * kernel["MatrixInstBM"])
177 else:
178 dividedForBlkId = (kernel["MatrixInstN"] * kernel["MatrixInstBN"]) if (tile01 == 0) else kernel["MatrixInstN"]
179 dividedForWaveId = waveWidth if (tile01 == 0) else (waveWidth * kernel["MIWaveGroup"][0])
180 vectorWidth = kernel["VectorWidth%s"%tc]
181 if isDTVAB:
182 if tP["tlu"]:
183 # DTV + TLU case, glvw and vw are applied to the same direction. No need to apply both.
184 # non TLU case, glvw and vw are applied to the different direction. We need to apply vw here.
185 vectorWidth = 1
186 maxKId = waveWidth // ((kernel["MatrixInstM"] if (tile01 == 0) else kernel["MatrixInstN"]) * kernel["MatrixInstB"])
187 writer.states.lraTileProperties[tile01] = LraTilePropertiesMFMA(dividendForKId=dividendForKId, \
188 num1DBlocks=num1DBlocks, \
189 num1DWaves=num1DWaves, \
190 dividedForBlkId=dividedForBlkId, \
191 dividedForWaveId = dividedForWaveId, \
192 vectorWidth=vectorWidth, \
193 maxKId=maxKId)
194
195 # strider for each type of index
196 umlds = kernel["UnrollMajorLDS%s" % tc]
197 mt = kernel["MacroTile%u" % tile01]
198 if enableLDSTr:
199 strideTile = 4
200 else:
201 strideTile = kernel["_DepthU%s"%tc] + LdsPad if umlds else 1
202 if isDTVAB:
203 strideTile = 1 # DTV case. Actual stride will be applied later.
204
205 strideK = inputPerThread if umlds else (mt + LdsPad) * inputPerThread
206 if enableLDSTr:
207 strideK1 = mt+LdsPad
208
209 # FIXME SPARSE
210 if kernel["ProblemType"]["Sparse"] != 0:
211 if kernel["MIInputPerThread"] * kernel["ProblemType"]["DataType"].numBytes() > 16:
212 isSparseTrack = (kernel["ProblemType"]["Sparse"] == 2 and tP["isB"]) or (kernel["ProblemType"]["Sparse"] == 1 and tP["isA"]) or tP["isM"]
213 strideK = (inputPerThread if umlds else (mt + LdsPad) * inputPerThread) * (2 if isSparseTrack and kernel["MIInputPerThread%s"%tc] > inputPerThread else 1)
214 #special case for new F8 MFMA
215 elif kernel["ProblemType"]["DataType"].is8bitFloat() and kernel["MatrixInstK"] > 32:
216 if umlds:
217 strideK = 16
218 else:
219 strideK = (mt + LdsPad) * 16
220 elif kernel["UseF32XEmulation"] and not (kernel["MatrixInstM"] == 16 and kernel["MatrixInstK"] == 16):
221 if umlds:
222 strideK = 4
223 else:
224 strideK = (mt + LdsPad) * 4
225
226 strideBlock = kernel["MatrixInstM"] * strideTile
227 if enableLDSTr:
228 strideWave = kernel["MatrixInstM"] * vectorWidth
229 else:
230 strideWave = kernel["MatrixInstM"] * num1DBlocks * strideTile * vectorWidth
231
232 lsu = kernel["LocalSplitU"]
233
234 if isDTVAB:
235 strideTile = 1 # DTV case. Actual stride will be applied later.
236
237
238 with writer.allocTmpSgpr(1) as tmpSgprInfo:
239 # tile offset
240 module.add(vectorStaticRemainder(dummy, kReg, dividendReg, waveWidth, tmpVgprRes, tmpSgprInfo, \
241 "0. thread id in wave: wtid = tid %% wavelength(%u)" % waveWidth))
242 if enableLDSTr:
243 module.add(vectorStaticRemainder(dummy, tReg, kReg, 4, tmpVgprRes, tmpSgprInfo, \
244 "1. N offset: nIdx = wtid %% 4"))
245 module.add(vectorStaticRemainder(dummy, sReg, kReg, dividendForKId, tmpVgprRes, tmpSgprInfo, \
246 "1. N offset: nIdx = wtid %% MI_M(%d)"%dividendForKId))
247 module.add(vectorStaticDivide(sReg, sReg, 16, tmpVgprRes, \
248 "1. thread id in wave: k1Idx = mtid // 4"))
249 module.add(vectorStaticMultiply(vgpr(sReg), vgpr(sReg), 16, tmpSgprInfo, \
250 "1. K1 offset: lrK1Offset = k1Idx * mStride(%u)" % (strideK1)))
251
252 else:
253 module.add(vectorStaticRemainder(dummy, tReg, kReg, kernel["MatrixInstN"], tmpVgprRes, tmpSgprInfo, \
254 "1. N offset: nIdx = wtid %% MI_N(%u)" % kernel["MatrixInstN"]))
255 module.add(vectorStaticMultiply(vgpr(tReg), vgpr(tReg), strideTile, tmpSgprInfo, \
256 "1. N offset: nOffset = nIdx * nStride(%u)" % strideTile))
257 if enableLDSTr:
258 module.add(VAddU32(dst=vgpr(tReg), src0=vgpr(sReg), src1=vgpr(tReg), \
259 comment="1. offset in wave: lrOffset = bnOffset + lrKOffset"))
260 # block offset
261 if num1DBlocks > 1:
262 module.add(vectorStaticDivide(dummy, kReg, dividedForBlkId, tmpVgprRes, \
263 "2. block offset: bnIdx = wtid / dividedForBlkId(%u)" % dividedForBlkId))
264 module.add(vectorStaticRemainder(dummy, dummy, dummy, num1DBlocks, tmpVgprRes, tmpSgprInfo, \
265 "2. block offset: bnIdx = bnIdx %% num1DBlocks(%u)" % num1DBlocks))
266 module.add(vectorStaticMultiplyAdd(vgpr(tReg), vgpr(dummy), strideBlock, vgpr(tReg), tmpSgprInfo, \
267 "2. block offset: bnOffset = bnIdx * strideBlock(%u); 3. add N and block offset: bnOffset = block and N offset" % strideBlock))
268 else:
269 module.addComment0("Skip. 2. block offset: bnOffset = 0 when num1DBlocks = 1")
270
271 module.add(vectorStaticMultiply(vgpr(tReg), vgpr(tReg), vectorWidth, tmpSgprInfo, \
272 "4. apply VectorWidth: bnOffset = bnOffset * vw(%u)" % vectorWidth))
273
274 # unroll offset
275 #if isMfma and (dividendForKId != waveWidth):
276 if not isWmma_v1:
277 if (dividendForKId != waveWidth) and (not isDTVAB):
278 if enableLDSTr:
279 module.add(vectorStaticRemainder(dummy, mReg, kReg, 16, tmpVgprRes, tmpSgprInfo, \
280 "5.1 thread id in wave: mtid = wtid %% 16"))
281 module.add(vectorStaticDivide(mReg, mReg, 4, tmpVgprRes, \
282 "5.2 thread id in wave: k1Idx = mtid // 4"))
283 if (dividendForKId != waveWidth) or isDTVAB:
284 # DTVAB case, add this regardless of dividendForKId != waveWidth
285 module.add(vectorStaticDivide(kReg, kReg, dividendForKId, tmpVgprRes, \
286 "5. K offset: kIdx = wtid / (MIN(%u) * MIBB(%u))" % (kernel["MatrixInstN"], kernel["MatrixInstB"])))
287 if (dividendForKId != waveWidth) and (not isDTVAB):
288 if enableLDSTr:
289 module.add(vectorStaticMultiply(vgpr(kReg), vgpr(kReg), strideK, tmpSgprInfo, \
290 "5. K offset: lrKOffset = kIdx * mStride(%u)" % (strideK)))
291 module.add(vectorStaticMultiply(vgpr(mReg), vgpr(mReg), strideK1, tmpSgprInfo, \
292 "5.1 K1 offset: lrK1Offset = k1Idx * mStride(%u)" % (strideK1)))
293 module.add(VAddU32(dst=vgpr(kReg), src0=vgpr(mReg), src1=vgpr(kReg), \
294 comment="5.2 offset in wave: lrOffset = bnOffset + lrKOffset"))
295 module.add(VAddU32(dst=vgpr(tReg), src0=vgpr(kReg), src1=vgpr(tReg), \
296 comment="6. offset in wave: lrOffset = bnOffset + lrKOffset"))
297 else:
298 module.add(vectorStaticMultiplyAdd(vgpr(tReg), vgpr(kReg), strideK, vgpr(tReg), tmpSgprInfo, \
299 "5. K offset: lrKOffset = kIdx * mStride(%u); 6. offset in wave: lrOffset = bnOffset + lrKOffset" % (strideK)))
300
301 # wave offset
302 if num1DWaves > 1:
303 module.add(vectorStaticDivide(dummy, dividendReg, dividedForWaveId, tmpVgprRes, \
304 "7. wave offset in N dimen: wtid = tid / dividedForWaveId(%u)" % dividedForWaveId))
305 module.add(vectorStaticRemainder(dummy, dummy, dummy, num1DWaves, tmpVgprRes, tmpSgprInfo, \
306 "7. wave offset in M dimen: wtid0 = wtid / num1DWaves(%u)" % num1DWaves))
307 module.add(vectorStaticMultiplyAdd(vgpr(tReg), vgpr(dummy), strideWave, vgpr(tReg), tmpSgprInfo, \
308 "7. wave offset in M dimen: wOffset = wtid0 * W0Stride(%u); 7. final local read offset: flrOffset = lrOffset + WOffset" % strideWave))
309
310 # release register
311 writer.vgprPool.checkIn(dummy)
312 if enableLDSTr:
313 writer.vgprPool.checkIn(sReg)
314 writer.vgprPool.checkIn(mReg)
315
316 return module