| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
from rocisa.code import Module |
| 26 |
from rocisa.container import DSModifiers, vgpr, sgpr, SDWAModifiers, VOP3PModifiers, ContinuousRegister |
| 27 |
from rocisa.enum import SelectBit |
| 28 |
from rocisa.instruction import SMovB32, SWaitCnt, VOrB32, VPermB32, VLShiftLeftOrB32, \ |
| 29 |
VMovB32, VMovB64,VLShiftRightB32, VCvtPkFP8toF32, VCvtF32toF16, VCvtFP8toF32,VCvtScaleFP8toF16,VCvtScalePkFP8toF16, \ |
| 30 |
VCvtPkF32toBF16, VCvtBF16toFP32, PVCvtBF16toFP32, VDot2CF32BF16, SNop, VSubF32, VSwapB32 |
| 31 |
|
| 32 |
from ..Component import LocalRead |
| 33 |
|
| 34 |
from math import ceil |
| 35 |
|
| 36 |
class LocalReadVALU(LocalRead): |
| 37 |
kernel = {"EnableMatrixInstruction": False} |
| 38 |
|
| 39 |
""" |
| 40 |
Local Read: Do It A/B |
| 41 |
iui = Inner Unroll Idx |
| 42 |
epsi = expand pointer swap index. Only used for PAP |
| 43 |
""" |
| 44 |
def __call__(self, writer, kernel, bufferIdx, iui, epsi, tP): |
| 45 |
tc = tP["tensorChar"] |
| 46 |
|
| 47 |
if tc == "A": |
| 48 |
writer.states.localReadDoCntA += 1 |
| 49 |
elif tc == "Metadata": |
| 50 |
writer.states.localReadDoCntMetadata += 1 |
| 51 |
else: |
| 52 |
writer.states.localReadDoCntB += 1 |
| 53 |
|
| 54 |
tile01 = tP["tile01Idx"] |
| 55 |
imod = Module("LocalReadDo%s_I%s"%(tc,iui)) |
| 56 |
pack = Module("pack%s_I%s"%(tc,iui)) |
| 57 |
instruction = tP["localReadInstruction"] |
| 58 |
numOffsets = instruction.numOffsets |
| 59 |
blockWidth = instruction.blockWidth |
| 60 |
offsetMultiplier = 1 |
| 61 |
valuIdx = 0 |
| 62 |
|
| 63 |
if kernel["UseDotInstruction"]: |
| 64 |
numVectorsPerTile = kernel["ThreadTile%u"%tile01] |
| 65 |
numReadsPerVector = (writer.states.lrvwUnrollA * tP["bpe"]) // (blockWidth*4) |
| 66 |
LdsPad = kernel["LdsPad%s"%tc] if kernel["LdsBlockSizePerPad%s"%tc] == 0 else 0 |
| 67 |
tileStride = kernel["_DepthU%s"%tc] + LdsPad if kernel["UnrollMajorLDS%s" % tP["tensorChar"]] else 1 |
| 68 |
else: |
| 69 |
numVectorsPerTile = (kernel["ThreadTile%u"%tile01]//kernel["VectorWidthA"]) |
| 70 |
numReadsPerVector = (kernel["VectorWidthA"] * tP["bpe"]) // (blockWidth*4) |
| 71 |
|
| 72 |
for vIdx in range(0, numVectorsPerTile): |
| 73 |
for rIdx in range(0, int(numReadsPerVector)): |
| 74 |
localReadCode = imod.add(Module("LocalRead%s Valu%u"%(tc,valuIdx))) |
| 75 |
paramList = [] |
| 76 |
destVgpr = vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valuIdx), blockWidth) |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
for oIdx in range(0, numOffsets): |
| 82 |
|
| 83 |
if kernel["UseDotInstruction"]: |
| 84 |
paramList.append(((rIdx*blockWidth + kernel["SubGroup%u"%tile01] * (vIdx*numOffsets+oIdx) * tileStride \ |
| 85 |
+ tP["localReadOffset"]) * tP["bpe"] + tP["localReadSwapByteOffset"]) // offsetMultiplier) |
| 86 |
else: |
| 87 |
paramList.append(((rIdx*blockWidth + kernel["SubGroup%u"%tile01] * (vIdx*numOffsets+oIdx)*kernel["VectorWidthA"] \ |
| 88 |
+ tP["localReadOffset"]) * tP["bpe"] + tP["localReadSwapByteOffset"]) // offsetMultiplier) |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
num = paramList[0] //65536 |
| 97 |
paramList[0] = paramList[0] - num * 65536 |
| 98 |
srcVgpr=vgpr("LocalReadAddr%s+%d"%(tc,num)) |
| 99 |
|
| 100 |
if numOffsets == 1: |
| 101 |
ds = DSModifiers(na=1, offset=paramList[0]) |
| 102 |
if numOffsets == 2: |
| 103 |
ds = DSModifiers(na=2, offset0=paramList[0], offset1=paramList[1]) |
| 104 |
LocalReadX = instruction.getInst() |
| 105 |
localReadCode.add(LocalReadX(dst=destVgpr, src=srcVgpr, ds=ds)) |
| 106 |
valuIdx += blockWidth |
| 107 |
|
| 108 |
|
| 109 |
with writer.allocTmpSgpr(1) as tmpSgprInfo: |
| 110 |
tmpSgpr = tmpSgprInfo.idx |
| 111 |
if writer.db["CheckValue1%s" % tc]: |
| 112 |
dbgVgpr = destVgpr |
| 113 |
dbgVgprList = destVgpr.split("v[") |
| 114 |
if len(dbgVgprList) == 1: |
| 115 |
dbgVgpr = dbgVgprList[0] |
| 116 |
else: |
| 117 |
|
| 118 |
|
| 119 |
dbgVgprList = (dbgVgprList[1].split("]")[0]).split(':') |
| 120 |
dbgVgpr = "v[%s]"%dbgVgprList[0] |
| 121 |
|
| 122 |
|
| 123 |
localReadCode.add(SWaitCnt(dscnt=0, comment="CheckValue1 wait for lds read")) |
| 124 |
if writer.asmCaps["SeparateVscnt"]: |
| 125 |
|
| 126 |
localReadCode.add(SWaitCnt(vlcnt=0, vscnt=0)) |
| 127 |
|
| 128 |
if kernel["ProblemType"]["DataType"].isHalf(): |
| 129 |
localReadCode.add(SMovB32(dst=sgpr(tmpSgpr), src=hex(0x3c003c00), comment="CheckValue1: FP16")) |
| 130 |
localReadCode.add(writer.assert_eq( dbgVgpr, sgpr(tmpSgpr))) |
| 131 |
|
| 132 |
elif kernel["ProblemType"]["DataType"].isBFloat16(): |
| 133 |
localReadCode.add(SMovB32(dst=sgpr(tmpSgpr), src=hex(0x3f803f80), comment="CheckValue1: BF16")) |
| 134 |
localReadCode.add(writer.assert_eq( dbgVgpr, sgpr(tmpSgpr))) |
| 135 |
|
| 136 |
|
| 137 |
if kernel["ProblemType"]["DataType"].isInt8(): |
| 138 |
localReadCode.add(SMovB32(dst=sgpr(tmpSgpr), src=hex(0x01010101), comment="CheckValue1: INT8")) |
| 139 |
localReadCode.add(writer.assert_eq( dbgVgpr, sgpr(tmpSgpr))) |
| 140 |
|
| 141 |
|
| 142 |
elif kernel["ProblemType"]["DataType"].isInt8x4(): |
| 143 |
localReadCode.add(writer.assert_eq( dbgVgpr, 1)) |
| 144 |
|
| 145 |
elif kernel["ProblemType"]["DataType"].isSingle(): |
| 146 |
localReadCode.add(writer.assert_eq( dbgVgpr, 1.0) ) |
| 147 |
|
| 148 |
return imod, pack |
| 149 |
|
| 150 |
class LocalReadMFMA(LocalRead): |
| 151 |
kernel = {"EnableMatrixInstruction": True} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
def cal_offset_srcAddr(self, maxLDSConstOffset, tc, offset): |
| 156 |
num = offset // maxLDSConstOffset |
| 157 |
offset_val = offset - num * maxLDSConstOffset |
| 158 |
srcAddr = vgpr("LocalReadAddr%s+%u" %(tc, num)) |
| 159 |
return offset_val, srcAddr |
| 160 |
|
| 161 |
|
| 162 |
""" |
| 163 |
Local Read: Do It A/B |
| 164 |
iui = Inner Unroll Idx |
| 165 |
epsi = expand pointer swap index. Only used for PAP |
| 166 |
""" |
| 167 |
def __call__(self, writer, kernel, bufferIdx, iui, epsi, tP): |
| 168 |
imod = Module("LocalReadDo%s_I%s" % (tP["tensorChar"],iui)) |
| 169 |
|
| 170 |
tc = tP["tensorChar"] |
| 171 |
if tc == "A": |
| 172 |
writer.states.localReadDoCntA += 1 |
| 173 |
elif tc == "Metadata": |
| 174 |
writer.states.localReadDoCntMetadata += 1 |
| 175 |
else: |
| 176 |
writer.states.localReadDoCntB += 1 |
| 177 |
tile01 = tP["tile01Idx"] |
| 178 |
instruction = tP["localReadInstruction"] |
| 179 |
bpr = 4 |
| 180 |
|
| 181 |
numOffsets = instruction.numOffsets |
| 182 |
blockWidth = instruction.blockWidth |
| 183 |
unrollBlockWidth = instruction.blockWidth if kernel["UnrollMajorLDS%s"%tc] else tP["bpeDS"]/4 |
| 184 |
tileBlockWidth = tP["bpeDS"]/4 if kernel["UnrollMajorLDS%s"%tc] else instruction.blockWidth |
| 185 |
|
| 186 |
vectorWidth = kernel["VectorWidth%s"%tc] |
| 187 |
|
| 188 |
MIWaveGroupShape = [ kernel["MatrixInstM"] * kernel["MatrixInstBM"] * kernel["MIWaveGroup"][0] * kernel["VectorWidthA"], \ |
| 189 |
kernel["MatrixInstN"] * kernel["MatrixInstBN"] * kernel["MIWaveGroup"][1] * kernel["VectorWidthB"]] |
| 190 |
|
| 191 |
LdsPad = kernel["LdsPad%s"%tc] if kernel["LdsBlockSizePerPad%s"%tc] == 0 else 0 |
| 192 |
tileStride = 1 |
| 193 |
UnrollStride = kernel["MacroTile%s" % tP["tensorChar"]] + LdsPad |
| 194 |
if kernel["UnrollMajorLDS%s" % tP["tensorChar"]]: |
| 195 |
tileStride = kernel["_DepthU%s"%tc] + LdsPad |
| 196 |
UnrollStride = 1 |
| 197 |
|
| 198 |
numVectorsPerTile = kernel["MIWaveTile"][tile01] // vectorWidth |
| 199 |
numReadsPerVector = (vectorWidth * tP["bpeDS"]) // int(tileBlockWidth * bpr) |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
enableLDSTr = tP["enableLDSTr"] |
| 204 |
numReadsPerUnroll = ceil(tP["bpeDS"] * kernel["MIInputPerThread%s"%tc] / int(unrollBlockWidth * bpr)) |
| 205 |
numVgpr = int(ceil(blockWidth)) |
| 206 |
if tc == 'A': |
| 207 |
lrvwTile = writer.states.lrvwTileA |
| 208 |
elif tc == 'B': |
| 209 |
lrvwTile = writer.states.lrvwTileB |
| 210 |
elif tc == "Metadata": |
| 211 |
lrvwTile = writer.states.lrvwTileMetadata |
| 212 |
else: |
| 213 |
lrvwTile = 1 |
| 214 |
numElementPerRead = 1 if kernel["ConvertAfterDS"] and not kernel["UseF32XEmulation"] else (int(blockWidth * bpr) // tP['bpe'] // lrvwTile) |
| 215 |
inputPerThread = kernel["LocalReadVectorWidth"] if not writer.states.inTailLoop else kernel["MIInputPerThread%s"%tc] |
| 216 |
|
| 217 |
|
| 218 |
if writer.states.archCaps["HasEccHalf"] or not writer.states.asmCaps["HasWMMA_V1"]: |
| 219 |
needPack = tP["bpeDS"] < 4 and not kernel["UnrollMajorLDS%s"%tc] and not tP["isM"] |
| 220 |
|
| 221 |
needPackMetadata = tP["isM"] and ((kernel["MIInputPerThread%s"%tc] * tP["bpeDS"] / (blockWidth * 4) > 1) or (kernel["ProblemType"]["DataType"].numBytes() == 1 and writer.states.lrvwTileMetadata > 1)) |
| 222 |
needPack |= needPackMetadata |
| 223 |
else: |
| 224 |
needPack = blockWidth == 0.25 |
| 225 |
needPack |= (kernel["ConvertAfterDS"] and (tP["bpe"] != tP["bpeDS"])) |
| 226 |
needPack |= kernel["UseF32XEmulation"] |
| 227 |
pack = Module("pack%s_I%s"%(tc,iui)) |
| 228 |
|
| 229 |
|
| 230 |
numSplitMetadata = max(ceil((blockWidth * 4) // tP["bpeDS"]) - 1, 0) if tP["isM"] else 0 |
| 231 |
|
| 232 |
|
| 233 |
blocksPerTGroupSMFMA = 1 |
| 234 |
elementsPerBlockSMFMA = 1 |
| 235 |
blockOffsetSMFMA = 1 |
| 236 |
if kernel["ProblemType"]["Sparse"] != 0: |
| 237 |
if kernel["MIInputPerThread"] * kernel["ProblemType"]["DataTypeB"].numBytes() > 16: |
| 238 |
isSparseTrack = (kernel["ProblemType"]["Sparse"] == 1 and tP["isA"]) or (kernel["ProblemType"]["Sparse"] == 2 and tP["isB"]) or tP["isM"] |
| 239 |
|
| 240 |
|
| 241 |
blocksPerTGroupSMFMA = 1 if isSparseTrack else 2 |
| 242 |
if blocksPerTGroupSMFMA > 1: |
| 243 |
threadGroups = kernel["MatrixInstK"] // kernel["MIInputPerThread"] |
| 244 |
elementsPerBlockSMFMA = kernel["MIInputPerThread"] // blocksPerTGroupSMFMA |
| 245 |
blockStride = elementsPerBlockSMFMA * threadGroups |
| 246 |
blockOffsetSMFMA = blockStride - elementsPerBlockSMFMA |
| 247 |
|
| 248 |
maxLDSConstOffset = writer.states.regCaps["maxLDSConstOffset"] |
| 249 |
valufIdx = 0 |
| 250 |
if enableLDSTr: |
| 251 |
numberMTilesPerWave = kernel["MIWaveTile"][tile01] |
| 252 |
highBits = 0 |
| 253 |
for tIdx in range(0, numberMTilesPerWave): |
| 254 |
offset_val = (tP["localReadOffset"]+MIWaveGroupShape[tile01]*tIdx) * tP["bpeDS"] + tP["localReadSwapByteOffset"] |
| 255 |
if (kernel["LdsBlockSizePerPad%s"%tc] != 0) and (kernel["LdsPad%s"%tc] != 0): |
| 256 |
offset_val = offset_val + (offset_val // kernel["LdsBlockSizePerPad%s"%tc]) * kernel["LdsPad%s"%tc] * tP["bpeDS"] |
| 257 |
offset, srcAddr = self.cal_offset_srcAddr(maxLDSConstOffset, tc, offset_val) |
| 258 |
ds = DSModifiers(na=1, offset=offset) |
| 259 |
LocalReadX = instruction.getInst(highBits) |
| 260 |
destVgpr = vgpr("Valu%s_X%u_I%u+%u+0"%(tc,bufferIdx,iui, 4*tIdx), 2) |
| 261 |
comment = "LDS Transpose" |
| 262 |
valuiIdx = int(valufIdx) |
| 263 |
localReadCode = imod.add(Module("LocalRead%s Valu%u"%(tc,valuiIdx))) |
| 264 |
localReadCode.add(LocalReadX(dst=destVgpr, src=srcAddr, ds=ds, comment=comment)) |
| 265 |
destVgpr = vgpr("Valu%s_X%u_I%u+%u+2"%(tc,bufferIdx,iui,4*tIdx), 2) |
| 266 |
offset_val += UnrollStride*inputPerThread; |
| 267 |
offset, srcAddr = self.cal_offset_srcAddr(maxLDSConstOffset, tc, offset_val) |
| 268 |
ds = DSModifiers(na=1, offset=offset) |
| 269 |
localReadCode.add(LocalReadX(dst=destVgpr, src=srcAddr, ds=ds, comment=comment)) |
| 270 |
else: |
| 271 |
for vIdx in range(0, numVectorsPerTile): |
| 272 |
for eIdx in range(0, numReadsPerVector): |
| 273 |
valuiIdx = int(valufIdx) |
| 274 |
baseValuiIdx = valuiIdx |
| 275 |
localReadCode = imod.add(Module("LocalRead%s Valu%u"%(tc,valuiIdx))) |
| 276 |
if needPack or numSplitMetadata: |
| 277 |
packCode = pack.add(Module("packCode")) |
| 278 |
|
| 279 |
tmpvgprHI = [] |
| 280 |
for rIdx in range(0, numReadsPerUnroll): |
| 281 |
valuiIdx = int(valufIdx) |
| 282 |
baseLRVgpr = vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valuiIdx), numVgpr) |
| 283 |
destVgpr = baseLRVgpr |
| 284 |
highBitsForHalf = (blockWidth == 0.5) and ((rIdx % 2) == 1) |
| 285 |
isHigh16Bits = (blockWidth == 0.25) and ( ((rIdx % 4) //2) == 1) |
| 286 |
|
| 287 |
if needPack or numSplitMetadata: |
| 288 |
if kernel["UseF32XEmulation"]: |
| 289 |
if valuiIdx % 4 == 0: |
| 290 |
tmpvgprIDx = (valuiIdx % 8) // 4 |
| 291 |
tmpvgprHI.append(writer.vgprPool.checkOutAligned(2, 2)) |
| 292 |
numTmpForCVTSubTF32 = 1 |
| 293 |
tmpvgpr =[] |
| 294 |
for i in range(numTmpForCVTSubTF32): |
| 295 |
tmpvgpr.append(writer.vgprPool.checkOut(1)) |
| 296 |
|
| 297 |
v0 = vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valuiIdx)) |
| 298 |
v1 = vgpr("Valu%s_X%u_I%u+%u+1"%(tc, bufferIdx, iui, valuiIdx)) |
| 299 |
v2 = vgpr("Valu%s_X%u_I%u+%u+2"%(tc, bufferIdx, iui, valuiIdx)) |
| 300 |
v3 = vgpr("Valu%s_X%u_I%u+%u+3"%(tc, bufferIdx, iui, valuiIdx)) |
| 301 |
|
| 302 |
packCode.add(VCvtPkF32toBF16(dst=vgpr(tmpvgprHI[tmpvgprIDx]), src0=v0, src1=v1)) |
| 303 |
|
| 304 |
if kernel["UseDot2F32XEmulation"]: |
| 305 |
packCode.add(VDot2CF32BF16(dst=v0, src0=hex(0x8000bf80), src1=vgpr(tmpvgprHI[tmpvgprIDx]))) |
| 306 |
packCode.add(VDot2CF32BF16(dst=v1, src0=hex(0xbf800000), src1=vgpr(tmpvgprHI[tmpvgprIDx]))) |
| 307 |
else: |
| 308 |
packCode.add(PVCvtBF16toFP32(dst=vgpr(tmpvgpr[0]), src=vgpr(tmpvgprHI[tmpvgprIDx]))) |
| 309 |
packCode.add(VSubF32(dst=v0, src0=v0, src1=vgpr(tmpvgpr[0]))) |
| 310 |
packCode.add(VCvtBF16toFP32(dst=vgpr(tmpvgpr[0]), src=vgpr(tmpvgprHI[tmpvgprIDx]), vgprMask=None, vi=1)) |
| 311 |
packCode.add(VSubF32(dst=v1, src0=v1, src1=vgpr(tmpvgpr[0]))) |
| 312 |
|
| 313 |
packCode.add(VCvtPkF32toBF16(dst=vgpr(tmpvgprHI[tmpvgprIDx]+1), src0=v2, src1=v3)) |
| 314 |
|
| 315 |
if kernel["UseDot2F32XEmulation"]: |
| 316 |
packCode.add(VDot2CF32BF16(dst=v2, src0=hex(0x8000bf80), src1=vgpr(tmpvgprHI[tmpvgprIDx]+1))) |
| 317 |
else: |
| 318 |
packCode.add(PVCvtBF16toFP32(dst=vgpr(tmpvgpr[0]), src=vgpr(tmpvgprHI[tmpvgprIDx]+1))) |
| 319 |
packCode.add(VSubF32(dst=v2, src0=v2, src1=vgpr(tmpvgpr[0]))) |
| 320 |
|
| 321 |
|
| 322 |
packCode.add(VCvtBF16toFP32(dst=vgpr(tmpvgpr[0]), src=vgpr(tmpvgprHI[tmpvgprIDx]+1), vgprMask=None, vi=1)) |
| 323 |
packCode.add(VSubF32(dst=v3, src0=v3, src1=vgpr(tmpvgpr[0]))) |
| 324 |
|
| 325 |
if kernel["UseDot2F32XEmulation"]: |
| 326 |
packCode.add(VMovB32(dst=vgpr(tmpvgpr[0]), src=0)) |
| 327 |
packCode.add(VMovB32(dst=vgpr(tmpvgpr[0]), src=0)) |
| 328 |
|
| 329 |
for i in range(numTmpForCVTSubTF32): |
| 330 |
writer.vgprPool.checkIn(tmpvgpr[i]) |
| 331 |
|
| 332 |
if rIdx == numReadsPerUnroll - 1: |
| 333 |
if not (kernel["MatrixInstM"] == 16 and kernel["MatrixInstK"] == 16): |
| 334 |
v0 = vgpr("Valu%s_X%u_I%u+%u+0"%(tc, bufferIdx, iui, baseValuiIdx)) |
| 335 |
v1 = vgpr("Valu%s_X%u_I%u+%u+1"%(tc, bufferIdx, iui, baseValuiIdx)) |
| 336 |
v2 = vgpr("Valu%s_X%u_I%u+%u+2"%(tc, bufferIdx, iui, baseValuiIdx)) |
| 337 |
v3 = vgpr("Valu%s_X%u_I%u+%u+3"%(tc, bufferIdx, iui, baseValuiIdx)) |
| 338 |
v4 = vgpr("Valu%s_X%u_I%u+%u+4"%(tc, bufferIdx, iui, baseValuiIdx)) |
| 339 |
v5 = vgpr("Valu%s_X%u_I%u+%u+5"%(tc, bufferIdx, iui, baseValuiIdx)) |
| 340 |
v6 = vgpr("Valu%s_X%u_I%u+%u+6"%(tc, bufferIdx, iui, baseValuiIdx)) |
| 341 |
v7 = vgpr("Valu%s_X%u_I%u+%u+7"%(tc, bufferIdx, iui, baseValuiIdx)) |
| 342 |
packCode.add(VCvtPkF32toBF16(dst=v7, src0=v6, src1=v7)) |
| 343 |
packCode.add(VCvtPkF32toBF16(dst=v6, src0=v4, src1=v5)) |
| 344 |
packCode.add(VCvtPkF32toBF16(dst=v5, src0=v2, src1=v3)) |
| 345 |
packCode.add(VCvtPkF32toBF16(dst=v4, src0=v0, src1=v1)) |
| 346 |
tmpvgprHI064 = vgpr(tmpvgprHI[0], 2) |
| 347 |
tmpvgprHI164 = vgpr(tmpvgprHI[1], 2) |
| 348 |
valuvgprHI064 = vgpr("Valu%s_X%u_I%u+%u+0"%(tc, bufferIdx, iui, baseValuiIdx), 2) |
| 349 |
valuvgprHI164 = vgpr("Valu%s_X%u_I%u+%u+2"%(tc, bufferIdx, iui, baseValuiIdx), 2) |
| 350 |
packCode.add(VMovB64(dst=valuvgprHI064, src=tmpvgprHI064)) |
| 351 |
packCode.add(VMovB64(dst=valuvgprHI164, src=tmpvgprHI164)) |
| 352 |
for i in range(len(tmpvgprHI)): |
| 353 |
writer.vgprPool.checkIn(tmpvgprHI[i]) |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
if kernel["ConvertAfterDS"] and (tP["bpe"] != tP["bpeDS"]): |
| 367 |
if tP["bpe"] == 2 and tP["bpeDS"] == 4: |
| 368 |
assert 0 |
| 369 |
else: |
| 370 |
highBitsForHalf = False |
| 371 |
isHigh16Bits = False |
| 372 |
|
| 373 |
if kernel["UnrollMajorLDS%s"%tc]: |
| 374 |
cvtTimes = (blockWidth * writer.states.bpr // tP["bpeDS"]) // kernel["MIInputPerThread%s"%tc] |
| 375 |
for i in range(0, cvtTimes): |
| 376 |
offset = cvtTimes - i - 1 |
| 377 |
if writer.states.asmCaps["Hascvtf16_fp8"]: |
| 378 |
packCode.add(VCvtScalePkFP8toF16(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valuiIdx+1+offset*2)),\ |
| 379 |
src=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valuiIdx+offset)), scale=0x3f800000,\ |
| 380 |
vop3=VOP3PModifiers(op_sel=[1,0,0,0]), comment="convert fp8 to f16")) |
| 381 |
packCode.add(VCvtScalePkFP8toF16(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valuiIdx+0+offset*2)),\ |
| 382 |
src=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valuiIdx+offset)), scale=0x3f800000,\ |
| 383 |
vop3=VOP3PModifiers(op_sel=[0,0,0,0]), comment="convert fp8 to f16")) |
| 384 |
else: |
| 385 |
packCode.add(VCvtPkFP8toF32(dst=vgpr("CvtTemp", 2), src=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valuiIdx+offset)),\ |
| 386 |
sdwa=SDWAModifiers(src0_sel=SelectBit.WORD_1), comment="convert to F32")) |
| 387 |
packCode.add(VCvtF32toF16(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valuiIdx+1+offset*2)), src=vgpr("CvtTemp+0"),\ |
| 388 |
sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_0), comment="Convert to FP16")) |
| 389 |
packCode.add(VCvtF32toF16(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valuiIdx+1+offset*2)), src=vgpr("CvtTemp+1"),\ |
| 390 |
sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_1), comment="Convert to FP16")) |
| 391 |
packCode.add(VCvtPkFP8toF32(dst=vgpr("CvtTemp", 2), src=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valuiIdx+offset)),\ |
| 392 |
sdwa=SDWAModifiers(src0_sel=SelectBit.WORD_0), comment="convert to F32")) |
| 393 |
packCode.add(VCvtF32toF16(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valuiIdx+0+offset*2)), src=vgpr("CvtTemp+0"),\ |
| 394 |
sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_0), comment="Convert to FP16")) |
| 395 |
packCode.add(VCvtF32toF16(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valuiIdx+0+offset*2)), src=vgpr("CvtTemp+1"),\ |
| 396 |
sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_1), comment="Convert to FP16")) |
| 397 |
|
| 398 |
elif (writer.states.lrvwTileA == 1 and tc == 'A') or (writer.states.lrvwTileB == 1 and tc == 'B'): |
| 399 |
sdwa = SDWAModifiers(dst_sel=SelectBit.WORD_0) if (rIdx % 2 == 0) else SDWAModifiers(dst_sel=SelectBit.WORD_1) |
| 400 |
destVgpr = vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, int(valufIdx*2)), numVgpr) |
| 401 |
CvtDstVgpr = vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, int(valufIdx*2)), numVgpr) |
| 402 |
if needPack and rIdx != 0: |
| 403 |
destVgpr = vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, rIdx%4, valuiIdx), numVgpr) |
| 404 |
if writer.states.asmCaps["Hascvtf16_fp8"]: |
| 405 |
sel = [0,0,0,0] if (rIdx % 2 == 0) else [0,0,1,0] |
| 406 |
packCode.add(VCvtScaleFP8toF16(dst=CvtDstVgpr, src=destVgpr, scale=0x3f800000, vop3=VOP3PModifiers(op_sel=sel), comment="convert fp8 to f16")) |
| 407 |
else: |
| 408 |
packCode.add(VCvtFP8toF32(dst=destVgpr, src=destVgpr, sdwa=SDWAModifiers(src0_sel=SelectBit.BYTE_0))) |
| 409 |
packCode.add(VCvtF32toF16(dst=CvtDstVgpr, src=destVgpr, sdwa=sdwa, comment="Convert to FP16")) |
| 410 |
|
| 411 |
elif (writer.states.lrvwTileA == 2 and tc == 'A') or (writer.states.lrvwTileB == 2 and tc == 'B'): |
| 412 |
if needPack or numSplitMetadata: |
| 413 |
destVgpr = vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, rIdx%(kernel["MIInputPerThread%s"%tc]), vIdx*numVgpr), numVgpr) |
| 414 |
for i in range(0, numVgpr): |
| 415 |
cvtDstVgpr = vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, rIdx%(kernel["MIInputPerThread%s"%tc]), vIdx*numVgpr), numVgpr) |
| 416 |
if writer.states.asmCaps["Hascvtf16_fp8"]: |
| 417 |
packCode.add(VCvtScalePkFP8toF16(dst=destVgpr, src=destVgpr,scale=0x3f800000,vop3=VOP3PModifiers(op_sel=[0,0,0,0]),comment="convert F8 to F16")) |
| 418 |
else: |
| 419 |
packCode.add(VCvtPkFP8toF32(dst=vgpr("CvtTemp", 2), src=destVgpr, sdwa=SDWAModifiers(src0_sel=SelectBit.WORD_0), comment="convert to F32")) |
| 420 |
packCode.add(VCvtF32toF16(dst=destVgpr, src=vgpr("CvtTemp+0"), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_0), comment="Convert to FP16")) |
| 421 |
packCode.add(VCvtF32toF16(dst=destVgpr, src=vgpr("CvtTemp+1"), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_1), comment="Convert to FP16")) |
| 422 |
|
| 423 |
if rIdx == numReadsPerUnroll-1: |
| 424 |
for i in range(0, numVgpr): |
| 425 |
vgprIdx = (vIdx * numVgpr + i) * tP["bpe"] * kernel["MIInputPerThread%s"%tc] // writer.states.bpr * min(writer.states.bpr // tP["bpe"], vectorWidth) |
| 426 |
vgprOffset = 0 |
| 427 |
for vectorIdx in range(0, 2): |
| 428 |
for elementIdx in range(0, tP["bpe"]*kernel["MIInputPerThread%s"%tc]//writer.states.bpr): |
| 429 |
packCode.add(VPermB32(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, vgprIdx+vgprOffset)), \ |
| 430 |
src0=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, elementIdx*2+1, i+vIdx*numVgpr)), \ |
| 431 |
src1=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, elementIdx*2, i+vIdx*numVgpr)), \ |
| 432 |
src2=sgpr("PackKForV%u"%vectorIdx), \ |
| 433 |
comment="select K=%u%u for vector=%u"%(elementIdx*2, elementIdx*2+1, vectorIdx))) |
| 434 |
vgprOffset += 1 |
| 435 |
|
| 436 |
elif (writer.states.lrvwTileA == 4 and tc == 'A') or (writer.states.lrvwTileB == 4 and tc == 'B'): |
| 437 |
if needPack or numSplitMetadata: |
| 438 |
destVgpr = vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, rIdx%(kernel["MIInputPerThread%s"%tc]), 2 * vIdx * numVgpr), numVgpr) |
| 439 |
cvtDestVgpr = vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, rIdx%(kernel["MIInputPerThread%s"%tc]), 2 * vIdx * numVgpr + 1), numVgpr) |
| 440 |
packCode.add(VLShiftRightB32(dst=cvtDestVgpr, shiftHex=16, src=destVgpr, comment="shift 2 element to vgpr+1")) |
| 441 |
if writer.states.asmCaps["Hascvtf16_fp8"]: |
| 442 |
packCode.add(VCvtScalePkFP8toF16(dst=destVgpr, src=destVgpr,scale=0x3f800000, comment="convert F8 to F16")) |
| 443 |
packCode.add(VCvtScalePkFP8toF16(dst=cvtDestVgpr, src=cvtDestVgpr,scale=0x3f800000, comment="convert F8 to F16")) |
| 444 |
else: |
| 445 |
packCode.add(VCvtPkFP8toF32(dst=vgpr("CvtTemp", 2), src=destVgpr, sdwa=SDWAModifiers(src0_sel=SelectBit.WORD_0), comment="convert to F32")) |
| 446 |
packCode.add(VCvtF32toF16(dst=destVgpr, src=vgpr("CvtTemp+0"), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_0), comment="Convert to FP16")) |
| 447 |
packCode.add(VCvtF32toF16(dst=destVgpr, src=vgpr("CvtTemp+1"), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_1), comment="Convert to FP16")) |
| 448 |
packCode.add(VCvtPkFP8toF32(dst=vgpr("CvtTemp", 2), src=cvtDestVgpr, sdwa=SDWAModifiers(src0_sel=SelectBit.WORD_0), comment="convert to F32")) |
| 449 |
packCode.add(VCvtF32toF16(dst=cvtDestVgpr, src=vgpr("CvtTemp+0"), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_0), comment="Convert to FP16")) |
| 450 |
packCode.add(VCvtF32toF16(dst=cvtDestVgpr, src=vgpr("CvtTemp+1"), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_1), comment="Convert to FP16")) |
| 451 |
|
| 452 |
if rIdx == numReadsPerUnroll-1: |
| 453 |
for i in range(0, numVgpr*2): |
| 454 |
vgprIdx = (2 * vIdx * numVgpr + i) * tP["bpe"] * kernel["MIInputPerThread%s"%tc] // writer.states.bpr * min(writer.states.bpr // tP["bpe"], vectorWidth) |
| 455 |
vgprOffset = 0 |
| 456 |
for vectorIdx in range(0, 2): |
| 457 |
for elementIdx in range(0, tP["bpe"]*kernel["MIInputPerThread%s"%tc]//writer.states.bpr): |
| 458 |
packCode.add(VPermB32(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, vgprIdx+vgprOffset)), \ |
| 459 |
src0=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, elementIdx*2+1, i+2*vIdx*numVgpr)), \ |
| 460 |
src1=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, elementIdx*2, i+2*vIdx*numVgpr)), \ |
| 461 |
src2=sgpr("PackKForV%u"%vectorIdx), \ |
| 462 |
comment="select K=%u%u for vector=%u"%(elementIdx*2, elementIdx*2+1, vectorIdx))) |
| 463 |
vgprOffset += 1 |
| 464 |
|
| 465 |
elif (writer.states.lrvwTileA == 8 and tc == 'A') or (writer.states.lrvwTileB == 8 and tc == 'B'): |
| 466 |
if needPack or numSplitMetadata: |
| 467 |
destVgpr = vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, rIdx%(kernel["MIInputPerThread%s"%tc]), 2*vIdx*numVgpr), numVgpr) |
| 468 |
cvtDestVgpr0 = vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, rIdx%(kernel["MIInputPerThread%s"%tc]), 2*vIdx*numVgpr+0), 1) |
| 469 |
cvtDestVgpr1 = vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, rIdx%(kernel["MIInputPerThread%s"%tc]), 2*vIdx*numVgpr+1), 1) |
| 470 |
cvtDestVgpr2 = vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, rIdx%(kernel["MIInputPerThread%s"%tc]), 2*vIdx*numVgpr+2), 1) |
| 471 |
cvtDestVgpr3 = vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, rIdx%(kernel["MIInputPerThread%s"%tc]), 2*vIdx*numVgpr+3), 1) |
| 472 |
packCode.add(VLShiftRightB32(dst=cvtDestVgpr3, shiftHex=16, src=cvtDestVgpr1, comment="shift 2 element to vgpr+3")) |
| 473 |
|
| 474 |
packCode.add(VMovB32(dst=cvtDestVgpr2, src=cvtDestVgpr1)) |
| 475 |
packCode.add(VLShiftRightB32(dst=cvtDestVgpr1, shiftHex=16, src=cvtDestVgpr0, comment="shift 2 element to vgpr+1")) |
| 476 |
if writer.states.asmCaps["Hascvtf16_fp8"]: |
| 477 |
packCode.add(VCvtScalePkFP8toF16(dst=cvtDestVgpr0, src=cvtDestVgpr0,scale=0x3f800000, comment="convert F8 to F16")) |
| 478 |
packCode.add(VCvtScalePkFP8toF16(dst=cvtDestVgpr1, src=cvtDestVgpr1,scale=0x3f800000, comment="convert F8 to F16")) |
| 479 |
packCode.add(VCvtScalePkFP8toF16(dst=cvtDestVgpr2, src=cvtDestVgpr2,scale=0x3f800000, comment="convert F8 to F16")) |
| 480 |
packCode.add(VCvtScalePkFP8toF16(dst=cvtDestVgpr3, src=cvtDestVgpr3,scale=0x3f800000, comment="convert F8 to F16")) |
| 481 |
else: |
| 482 |
packCode.add(VCvtPkFP8toF32(dst=vgpr("CvtTemp", 2), src=cvtDestVgpr0, sdwa=SDWAModifiers(src0_sel=SelectBit.WORD_0), comment="convert to F32")) |
| 483 |
packCode.add(VCvtF32toF16(dst=cvtDestVgpr0, src=vgpr("CvtTemp+0"), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_0), comment="Convert to FP16")) |
| 484 |
packCode.add(VCvtF32toF16(dst=cvtDestVgpr0, src=vgpr("CvtTemp+1"), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_1), comment="Convert to FP16")) |
| 485 |
packCode.add(VCvtPkFP8toF32(dst=vgpr("CvtTemp", 2), src=cvtDestVgpr1, sdwa=SDWAModifiers(src0_sel=SelectBit.WORD_0), comment="convert to F32")) |
| 486 |
packCode.add(VCvtF32toF16(dst=cvtDestVgpr1, src=vgpr("CvtTemp+0"), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_0), comment="Convert to FP16")) |
| 487 |
packCode.add(VCvtF32toF16(dst=cvtDestVgpr1, src=vgpr("CvtTemp+1"), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_1), comment="Convert to FP16")) |
| 488 |
packCode.add(VCvtPkFP8toF32(dst=vgpr("CvtTemp", 2), src=cvtDestVgpr2, sdwa=SDWAModifiers(src0_sel=SelectBit.WORD_0), comment="convert to F32")) |
| 489 |
packCode.add(VCvtF32toF16(dst=cvtDestVgpr2, src=vgpr("CvtTemp+0"), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_0), comment="Convert to FP16")) |
| 490 |
packCode.add(VCvtF32toF16(dst=cvtDestVgpr2, src=vgpr("CvtTemp+1"), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_1), comment="Convert to FP16")) |
| 491 |
packCode.add(VCvtPkFP8toF32(dst=vgpr("CvtTemp", 2), src=cvtDestVgpr3, sdwa=SDWAModifiers(src0_sel=SelectBit.WORD_0), comment="convert to F32")) |
| 492 |
packCode.add(VCvtF32toF16(dst=cvtDestVgpr3, src=vgpr("CvtTemp+0"), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_0), comment="Convert to FP16")) |
| 493 |
packCode.add(VCvtF32toF16(dst=cvtDestVgpr3, src=vgpr("CvtTemp+1"), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_1), comment="Convert to FP16")) |
| 494 |
|
| 495 |
if rIdx == numReadsPerUnroll-1: |
| 496 |
for i in range(0, numVgpr*2): |
| 497 |
vgprIdx = (2 * vIdx * numVgpr + i) * tP["bpe"] * kernel["MIInputPerThread%s"%tc] // writer.states.bpr * min(writer.states.bpr // tP["bpe"], vectorWidth) |
| 498 |
vgprOffset = 0 |
| 499 |
for vectorIdx in range(0, 2): |
| 500 |
for elementIdx in range(0, tP["bpe"]*kernel["MIInputPerThread%s"%tc]//writer.states.bpr): |
| 501 |
packCode.add(VPermB32(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, vgprIdx+vgprOffset)), \ |
| 502 |
src0=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, elementIdx*2+1, i+2*vIdx*numVgpr)), \ |
| 503 |
src1=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, elementIdx*2, i+2*vIdx*numVgpr)), \ |
| 504 |
src2=sgpr("PackKForV%u"%vectorIdx), \ |
| 505 |
comment="select K=%u%u for vector=%u"%(elementIdx*2, elementIdx*2+1, vectorIdx))) |
| 506 |
vgprOffset += 1 |
| 507 |
else: |
| 508 |
pass |
| 509 |
elif lrvwTile > 1: |
| 510 |
highBitsForHalf = 0 |
| 511 |
isHigh8Bits = 0 |
| 512 |
isHigh16Bits = 0 |
| 513 |
numElementPerReg = writer.states.bpr//tP["bpe"] |
| 514 |
|
| 515 |
needPackK16 = False |
| 516 |
needPackK8Lw = False |
| 517 |
if kernel["ProblemType"]["DataType"].isHalf() or kernel["ProblemType"]["DataType"].isBFloat16(): |
| 518 |
if writer.states.lrvwTileA > 1 or writer.states.lrvwTileB > 1: |
| 519 |
needPackK16 = True |
| 520 |
if writer.states.lrvwTileMetadata > 1: |
| 521 |
needPackK8Lw = True |
| 522 |
|
| 523 |
tPackM = "M" if needPackK16 and needPackK8Lw else "" |
| 524 |
|
| 525 |
if needPack or numSplitMetadata: |
| 526 |
destVgpr = vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, rIdx%(kernel["MIInputPerThread%s"%tc]), vIdx*numVgpr), numVgpr) |
| 527 |
if rIdx == numReadsPerUnroll-1: |
| 528 |
for i in range(0, numVgpr): |
| 529 |
|
| 530 |
vgprIdx = (vIdx*numVgpr+i)*tP["bpeDS"]*kernel["MIInputPerThread%s"%tc]//writer.states.bpr*min(writer.states.bpr//tP["bpeDS"],vectorWidth) |
| 531 |
if numSplitMetadata: |
| 532 |
vgprIdx = (vIdx*numVgpr+i)*ceil(tP["bpeDS"]*kernel["MIInputPerThread%s"%tc] / writer.states.bpr)*min(writer.states.bpr//tP["bpeDS"],vectorWidth) |
| 533 |
if kernel["MIInputPerThread%s"%tc] == 4: |
| 534 |
vgprOffset = 0 |
| 535 |
for elementIdx in range(0, numSplitMetadata+1): |
| 536 |
if elementIdx >= writer.states.bpr: |
| 537 |
break |
| 538 |
|
| 539 |
packCode.add(VPermB32(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, vgprIdx+elementIdx)), src0=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, 1, i+vIdx*numVgpr)), src1=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, 0, i+vIdx*numVgpr)), src2=sgpr("PackKFor%sV%u"%(tPackM, vgprOffset)), \ |
| 540 |
comment="1 select K=%u%u for vector=%u"%(0, 1, vgprOffset))) |
| 541 |
packCode.add(VPermB32(dst=vgpr("PackTemp"), src0=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, 3, i+vIdx*numVgpr)), src1=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, 2, i+vIdx*numVgpr)), src2=sgpr("PackKFor%sV%u"%(tPackM, vgprOffset)), \ |
| 542 |
comment="1 select K=%u%u for vector=%u"%(2, 3, vgprOffset))) |
| 543 |
packCode.add(VLShiftLeftOrB32(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, vgprIdx+elementIdx)), src0=vgpr("PackTemp"), shiftHex=16, src1=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, vgprIdx+elementIdx)), comment="pack two half Vgpr to one Vgpr")) |
| 544 |
vgprOffset += 1 |
| 545 |
elif kernel["MIInputPerThread%s"%tc] == 2: |
| 546 |
vgprOffset = 0 |
| 547 |
for elementIdx in range(0, numSplitMetadata+1): |
| 548 |
if elementIdx >= writer.states.bpr: |
| 549 |
break |
| 550 |
|
| 551 |
packCode.add(VPermB32(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, vgprIdx+elementIdx)), src0=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, 1, i+vIdx*numVgpr)), src1=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, 0, i+vIdx*numVgpr)), src2=sgpr("PackKFor%sV%u"%(tPackM, vgprOffset)), \ |
| 552 |
comment="select K=%u%u for vector=%u"%(0, 1, vgprOffset))) |
| 553 |
vgprOffset += 1 |
| 554 |
elif kernel["MIInputPerThread%s"%tc] == 1: |
| 555 |
destVgpr_ = vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, rIdx%(kernel["MIInputPerThread%s"%tc]), vIdx*numVgpr + i)) |
| 556 |
bitShift = 0 |
| 557 |
for elementIdx in range(0, numSplitMetadata+1): |
| 558 |
|
| 559 |
if elementIdx >= writer.states.bpr: |
| 560 |
break |
| 561 |
comment_ = "another VGPR storing lshr %d-bit value %d %d" %(bitShift, vgprIdx, elementIdx) if bitShift != 0 else "" |
| 562 |
packCode.add(VMovB32(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, vgprIdx+elementIdx)), src=destVgpr_, comment=comment_)) |
| 563 |
if bitShift != 0: |
| 564 |
packCode.add(VLShiftRightB32(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, vgprIdx+elementIdx)), shiftHex=hex(bitShift), src=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, vgprIdx+elementIdx)), comment="ValuMetadata Vpgr >> %d" % bitShift)) |
| 565 |
bitShift += 8 |
| 566 |
else: |
| 567 |
assert False |
| 568 |
elif tP["isM"]: |
| 569 |
vgprOffset = 0 |
| 570 |
for elementIdx in range(0, kernel["MIInputPerThread%s"%tc]): |
| 571 |
packCode.add(VPermB32(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, vgprIdx+elementIdx+vIdx*2)), src0=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, vgprOffset*2 + 1 , i+vIdx*numVgpr)), src1=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, vgprOffset*2, i+vIdx*numVgpr)), src2=sgpr("PackKForV%u"%elementIdx), \ |
| 572 |
comment="select K=%u%u for vector=%u"%(vgprOffset*2+1, vgprOffset*2, elementIdx))) |
| 573 |
vgprOffset += (1 if elementIdx % 2 == 1 else 0) |
| 574 |
elif kernel["ProblemType"]["DataType"].isHalf() or kernel["MFMA_BF16_1K"] or kernel["ProblemType"]["DataType"].isBFloat16(): |
| 575 |
vgprOffset = 0 |
| 576 |
for vectorIdx in range(0, numElementPerReg): |
| 577 |
for elementIdx in range(0, tP["bpe"]*kernel["MIInputPerThread%s"%tc]//writer.states.bpr): |
| 578 |
packCode.add(VPermB32(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, vgprIdx+vgprOffset)), src0=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, elementIdx*numElementPerReg+1, i+vIdx*numVgpr)), src1=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, elementIdx*numElementPerReg, i+vIdx*numVgpr)), src2=sgpr("PackKForV%u"%vectorIdx), \ |
| 579 |
comment="select K=%u%u for vector=%u"%(elementIdx*numElementPerReg, elementIdx*numElementPerReg+1, vectorIdx))) |
| 580 |
vgprOffset += 1 |
| 581 |
elif kernel["ProblemType"]["DataType"].isInt8() or kernel["ProblemType"]["DataType"].is8bitFloat(): |
| 582 |
vgprOffset = 0 |
| 583 |
|
| 584 |
for vectorIdx in range(0, numElementPerReg): |
| 585 |
if vectorWidth <= 2 and vectorIdx > 1: |
| 586 |
break |
| 587 |
for elementIdx in range(0, tP["bpe"]*kernel["MIInputPerThread%s"%tc]//writer.states.bpr): |
| 588 |
packCode.add(VPermB32(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, vgprIdx+vgprOffset)), src0=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, elementIdx*numElementPerReg+1, i+vIdx*numVgpr)), src1=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, elementIdx*numElementPerReg, i+vIdx*numVgpr)), src2=sgpr("PackKForV%u"%vectorIdx), \ |
| 589 |
comment="select K=%u%u for vector=%u"%(elementIdx*4, elementIdx*4+1, vectorIdx))) |
| 590 |
packCode.add(VPermB32(dst=vgpr("PackTemp"), src0=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, elementIdx*numElementPerReg+3, i+vIdx*numVgpr)), src1=vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, elementIdx*numElementPerReg+2, i+vIdx*numVgpr)), src2=sgpr("PackKForV%u"%vectorIdx), \ |
| 591 |
comment="select K=%u%u for vector=%u"%(elementIdx*4+2, elementIdx*4+3, vectorIdx))) |
| 592 |
packCode.add(VLShiftLeftOrB32(dst=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, vgprIdx + vgprOffset)), src0=vgpr("PackTemp"), shiftHex=16, src1=vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, vgprIdx + vgprOffset)), comment="pack two half Vgpr to one Vgpr")) |
| 593 |
vgprOffset += 1 |
| 594 |
|
| 595 |
else: |
| 596 |
isHigh8Bits = (blockWidth == 0.25) and ( ((rIdx % 4) % 2) == 1) |
| 597 |
|
| 598 |
if tP["isM"]: |
| 599 |
isHigh8Bits = (blockWidth == 0.25) and ( (rIdx % 2) == 1) |
| 600 |
isHigh16Bits = (blockWidth == 0.25) and ( (rIdx % 4) == 3) if kernel["MIInputPerThread%s"%tc] == 4 else False |
| 601 |
if isHigh8Bits: |
| 602 |
dstVgpr = vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valufIdx/2), numVgpr) |
| 603 |
lowVgpr = vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valufIdx - 1), numVgpr) |
| 604 |
highVgpr = vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valufIdx), numVgpr) |
| 605 |
packCode.add(VLShiftLeftOrB32(dst=dstVgpr, src0=highVgpr, shiftHex=8, src1=lowVgpr, comment="pack two int8 Vgpr to one half Vgpr")) |
| 606 |
if isHigh16Bits: |
| 607 |
|
| 608 |
dstVgpr = vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valufIdx/4), numVgpr) |
| 609 |
lowVgpr = vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valufIdx/2 - 1), numVgpr) |
| 610 |
highVgpr = vgpr("Valu%s_X%u_I%u+%u"%(tc, bufferIdx, iui, valufIdx/2), numVgpr) |
| 611 |
packCode.add(VLShiftLeftOrB32(dst=dstVgpr, src0=highVgpr, shiftHex=hex(0x10), src1=lowVgpr, comment="pack two int8x2 Vgpr to one Vgpr")) |
| 612 |
|
| 613 |
isHigh16Bits = False |
| 614 |
isHigh8Bits = False |
| 615 |
elif writer.states.archCaps["HasEccHalf"] or not writer.states.asmCaps["HasWMMA_V1"]: |
| 616 |
if highBitsForHalf: |
| 617 |
highVgpr = vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, rIdx%2, valuiIdx), numVgpr) |
| 618 |
if writer.states.archCaps["DSLow16NotPreserve"]: |
| 619 |
packCode.add(VLShiftLeftOrB32(dst=baseLRVgpr, src0=highVgpr, shiftHex=hex(0x10), src1=baseLRVgpr, comment="pack two half Vgpr to one Vgpr")) |
| 620 |
else: |
| 621 |
packCode.add(VOrB32(dst=baseLRVgpr, src0=baseLRVgpr, src1=highVgpr, comment="pack two half Vgpr to one Vgpr")) |
| 622 |
destVgpr = highVgpr |
| 623 |
|
| 624 |
if rIdx != 0: |
| 625 |
if isHigh8Bits or isHigh16Bits: |
| 626 |
highVgpr = vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, rIdx%4, valuiIdx), numVgpr) |
| 627 |
destVgpr = highVgpr |
| 628 |
if isHigh8Bits: |
| 629 |
lowVgpr = vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, (rIdx%4)-1, valuiIdx), numVgpr) if isHigh16Bits else baseLRVgpr |
| 630 |
packCode.add(VLShiftLeftOrB32(dst=lowVgpr, src0=highVgpr, shiftHex=8, src1=lowVgpr, comment="pack two int8 Vgpr to one half Vgpr")) |
| 631 |
if isHigh16Bits: |
| 632 |
if writer.states.archCaps["DSLow16NotPreserve"]: |
| 633 |
packCode.add(VLShiftLeftOrB32(dst=baseLRVgpr, src0=lowVgpr, shiftHex=hex(0x10), src1=baseLRVgpr, comment="pack two half Vgpr to one Vgpr")) |
| 634 |
else: |
| 635 |
packCode.add(VOrB32(dst=baseLRVgpr, src0=baseLRVgpr, src1=lowVgpr, comment="pack two half Vgpr to one Vgpr")) |
| 636 |
else: |
| 637 |
|
| 638 |
if rIdx != 0: |
| 639 |
if isHigh8Bits: |
| 640 |
highVgpr = vgpr("Valu%s_X%u_I%u_D%u+%u"%(tc, bufferIdx, iui, rIdx%2, valuiIdx), numVgpr) |
| 641 |
destVgpr = highVgpr |
| 642 |
if isHigh8Bits and isHigh16Bits: |
| 643 |
packCode.add(VLShiftLeftOrB32(dst=baseLRVgpr, src0=highVgpr, shiftHex=hex(0x8), src1=baseLRVgpr, comment="pack two int8x2 Vgpr to one Vgpr")) |
| 644 |
|
| 645 |
if kernel["ConvertAfterDS"] and kernel["UnrollMajorLDS%s"%tc]: |
| 646 |
valufIdx += blockWidth * (tP["bpe"] // tP["bpeDS"]) if (not tP["isM"]) else 1 |
| 647 |
else: |
| 648 |
valufIdx += blockWidth if (not tP["isM"]) else 1 |
| 649 |
|
| 650 |
|
| 651 |
paramList = [] |
| 652 |
|
| 653 |
for oIdx in range(0, numOffsets): |
| 654 |
offset_val = (eIdx + (vIdx * numOffsets+oIdx) * MIWaveGroupShape[tile01]) * tileStride |
| 655 |
|
| 656 |
if kernel["ProblemType"]["Sparse"] != 0: |
| 657 |
if blocksPerTGroupSMFMA > 1: |
| 658 |
blockId = (rIdx * numElementPerRead) // elementsPerBlockSMFMA |
| 659 |
if kernel["UnrollMajorLDS%s"%(tc)]: |
| 660 |
offset_val = offset_val + (blockOffsetSMFMA * blockId) |
| 661 |
else: |
| 662 |
offset_val = offset_val + (blockOffsetSMFMA * blockId) * UnrollStride |
| 663 |
offset_val = (rIdx * numElementPerRead * UnrollStride + offset_val + tP["localReadOffset"]) * tP["bpeDS"] |
| 664 |
elif kernel["ProblemType"]["DataType"].is8bitFloat() and kernel["MatrixInstK"] > 32: |
| 665 |
incOffset = 0 |
| 666 |
midIdx = numReadsPerUnroll // 2 |
| 667 |
if rIdx >= midIdx: |
| 668 |
if kernel["UnrollMajorLDS%s" % tP["tensorChar"]] == False: |
| 669 |
|
| 670 |
if kernel["MatrixInstM"] == 32: |
| 671 |
incOffset = midIdx * numElementPerRead * UnrollStride |
| 672 |
elif kernel["MatrixInstM"] == 16: |
| 673 |
incOffset = 3 * midIdx * numElementPerRead * UnrollStride |
| 674 |
else: |
| 675 |
if kernel["MatrixInstM"] == 32: |
| 676 |
incOffset = 16 |
| 677 |
elif kernel["MatrixInstM"] == 16: |
| 678 |
incOffset = 48 |
| 679 |
incOffset = rIdx * numElementPerRead * UnrollStride + incOffset |
| 680 |
offset_val = (incOffset + offset_val + tP["localReadOffset"]) * tP["bpeDS"] |
| 681 |
elif kernel["UseF32XEmulation"]: |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
incOffset = 0 |
| 687 |
midIdx = numReadsPerUnroll // 2 |
| 688 |
if rIdx >= midIdx: |
| 689 |
if kernel["UnrollMajorLDS%s" % tP["tensorChar"]] == False: |
| 690 |
if kernel["MatrixInstM"] == 32: |
| 691 |
incOffset = midIdx * numElementPerRead * UnrollStride |
| 692 |
elif kernel["MatrixInstM"] == 16: |
| 693 |
incOffset = 3 * midIdx * numElementPerRead * UnrollStride |
| 694 |
else: |
| 695 |
if kernel["MatrixInstM"] == 32 and kernel["MatrixInstK"] == 16: |
| 696 |
incOffset = 4 |
| 697 |
elif kernel["MatrixInstM"] == 16 and kernel["MatrixInstK"] == 32: |
| 698 |
incOffset = 12 |
| 699 |
incOffset = rIdx * numElementPerRead * UnrollStride + incOffset |
| 700 |
offset_val = (incOffset + offset_val + tP["localReadOffset"]) * tP["bpeDS"] |
| 701 |
else: |
| 702 |
offset_val = (rIdx * numElementPerRead * UnrollStride + offset_val + tP["localReadOffset"]) * tP["bpeDS"] |
| 703 |
|
| 704 |
if (kernel["LdsBlockSizePerPad%s"%tc] != 0) and (kernel["LdsPad%s"%tc] != 0): |
| 705 |
offset_val = offset_val + (offset_val // kernel["LdsBlockSizePerPad%s"%tc]) * kernel["LdsPad%s"%tc] * tP["bpeDS"] |
| 706 |
offset_val = offset_val + tP["localReadSwapByteOffset"] |
| 707 |
if (kernel["DirectToLds%s" % tc] and \ |
| 708 |
kernel["GlobalReadVectorWidth%c"%tc] * tP["bpeDS"] > 4): |
| 709 |
|
| 710 |
dummy, offset_val = writer.lraOffsetConversionForDTLandNLC(kernel, tP, offset_val) |
| 711 |
|
| 712 |
paramList.append(int(offset_val)) |
| 713 |
|
| 714 |
comment = "L -> Reg lro=%d swapByteOffset=%u ti=%u vIdx=%u eIdx=%u rIdx=%u oIdx=%u buffer=%u iui=%u" \ |
| 715 |
% (tP["localReadOffset"], tP["localReadSwapByteOffset"], MIWaveGroupShape[tile01], vIdx, eIdx, rIdx, oIdx, bufferIdx, iui) |
| 716 |
|
| 717 |
highBits = 0 if writer.states.archCaps["DSLow16NotPreserve"] else highBitsForHalf or isHigh16Bits |
| 718 |
|
| 719 |
|
| 720 |
if(paramList[0] >=131072): |
| 721 |
paramList[0] = paramList[0] -131072 |
| 722 |
srcAddr=vgpr("LocalReadAddr%s+2"%tc) |
| 723 |
elif (paramList[0] >=65536): |
| 724 |
paramList[0] = paramList[0] -65536 |
| 725 |
srcAddr=vgpr("LocalReadAddr%s+1"%tc) |
| 726 |
else: |
| 727 |
srcAddr=vgpr("LocalReadAddr%s"%tc) |
| 728 |
|
| 729 |
if numOffsets == 1: |
| 730 |
ds = DSModifiers(na=1, offset=paramList[0]) |
| 731 |
else: |
| 732 |
ds = DSModifiers(na=2, offset0=paramList[0], offset1=paramList[1]) |
| 733 |
LocalReadX = instruction.getInst(highBits) |
| 734 |
localReadCode.add(LocalReadX(dst=destVgpr, src=srcAddr, ds=ds, comment=comment)) |
| 735 |
|
| 736 |
with writer.allocTmpSgpr(1) as tmpSgprInfo: |
| 737 |
tmpSgpr = tmpSgprInfo.idx |
| 738 |
if writer.db["CheckValue1%s"%tc] and not writer.inTailLoop: |
| 739 |
|
| 740 |
dbgVgpr = destVgpr |
| 741 |
dbgVgprList = destVgpr.split("v[") |
| 742 |
if len(dbgVgprList) == 1: |
| 743 |
dbgVgpr = dbgVgprList[0] |
| 744 |
else: |
| 745 |
|
| 746 |
|
| 747 |
dbgVgprList = (dbgVgprList[1].split("]")[0]).split(':') |
| 748 |
dbgVgpr = "v[%s]"%dbgVgprList[0] |
| 749 |
localReadCode.add(SWaitCnt(dscnt=0, vscnt=0, comment="CheckValue1 wait for LDS read")) |
| 750 |
|
| 751 |
if kernel["ProblemType"]["DataType"].isHalf(): |
| 752 |
hexValue = hex(0x3c003c00) |
| 753 |
if needPack: |
| 754 |
hexValue = hex(0x3c000000) if highBitsForHalf else hex(0x00003c00) |
| 755 |
localReadCode.add(SMovB32(dst=sgpr(tmpSgpr), src=hexValue, comment="CheckValue1: FP16")) |
| 756 |
localReadCode.add(writer.assert_eq( dbgVgpr, sgpr(tmpSgpr))) |
| 757 |
|
| 758 |
elif kernel["ProblemType"]["DataType"].isBFloat16(): |
| 759 |
hexValue = hex(0x3f803f80) |
| 760 |
if needPack: |
| 761 |
hexValue = hex(0x3f800000) if highBitsForHalf else hex(0x00003f80) |
| 762 |
localReadCode.add(SMovB32(dst=sgpr(tmpSgpr), src=hexValue, comment="CheckValue1: BF16")) |
| 763 |
localReadCode.add(writer.assert_eq( dbgVgpr, sgpr(tmpSgpr))) |
| 764 |
|
| 765 |
if kernel["ProblemType"]["DataType"].isInt8(): |
| 766 |
if needPack: |
| 767 |
hexValue = hex(0x00010000) if isHigh16Bits else hex(0x00000001) |
| 768 |
localReadCode.add(SMovB32(dst=sgpr(tmpSgpr), src=hexValue, comment="CheckValue1: INT8")) |
| 769 |
localReadCode.add(writer.assert_eq( dbgVgpr, sgpr(tmpSgpr))) |
| 770 |
|
| 771 |
|
| 772 |
elif kernel["ProblemType"]["DataType"].isInt8x4(): |
| 773 |
localReadCode.add(SMovB32(dst=sgpr(tmpSgpr), src=hex(0x01010101), comment="CheckValue1: INT8x4")) |
| 774 |
localReadCode.add(writer.assert_eq( dbgVgpr, sgpr(tmpSgpr))) |
| 775 |
|
| 776 |
elif kernel["ProblemType"]["DataType"].isSingle(): |
| 777 |
localReadCode.add(writer.assert_eq( dbgVgpr, 1.0) ) |
| 778 |
|
| 779 |
if (tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc]: |
| 780 |
imod = Module("LocalReadDo%s_I%s (Empty)" % (tP["tensorChar"],iui)) |
| 781 |
|
| 782 |
|
| 783 |
if (tP["isA"] or tP["isB"]) and kernel["enableGLTr%s"%tc]: |
| 784 |
pack = Module("Pack%s_I%s (Empty)" % (tP["tensorChar"],iui)) |
| 785 |
|
| 786 |
return imod, pack |