| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
from rocisa.code import Module, Label |
| 24 |
from rocisa.container import vgpr, sgpr, SMEMModifiers, replaceHolder, EXEC,\ |
| 25 |
VOP3PModifiers, ContinuousRegister |
| 26 |
from rocisa.instruction import SAddCU32, SAddI32, SAddU32, SAndB32, SBarrier, \ |
| 27 |
SBranch, SCBranchSCC0, SCBranchSCC1, SCMovB32, SCSelectB32, SCmpEQU32, SCmpEQU64, \ |
| 28 |
SCmpGtU32, SCmpLeU32, SCmpLtU32, SLShiftLeftB32, SLShiftLeftB64, SLShiftRightB32, SLoadB32, \ |
| 29 |
SMinU32, SMovB32, SMovB64, SMulI32, SNop, SSleep, SStoreB32, SSubU32, \ |
| 30 |
SWaitCnt, VAddF32, VAddF64, VAddPKF16, VAddU32, VLShiftRightB32, VMovB32, \ |
| 31 |
VReadfirstlaneB32, VCvtBF16toFP32 |
| 32 |
from rocisa.functions import scalarStaticDivideAndRemainder, sMagicDiv2, \ |
| 33 |
vectorStaticMultiply, BranchIfNotZero, scalarUInt32DivideAndRemainder |
| 34 |
|
| 35 |
|
| 36 |
from ..Common import print2, ceilDivide, log2 |
| 37 |
from ..Component import Component |
| 38 |
from ..AsmStoreState import StoreState, VectorDataTypes |
| 39 |
from ..AsmAddressCalculation import AddrCalculation |
| 40 |
import abc |
| 41 |
|
| 42 |
from copy import deepcopy |
| 43 |
|
| 44 |
class XCCMapping(Component): |
| 45 |
""" |
| 46 |
XCC mapping code. |
| 47 |
""" |
| 48 |
|
| 49 |
class XCCMappingOff(XCCMapping): |
| 50 |
kernel = {"StreamKXCCMapping": 0} |
| 51 |
|
| 52 |
def __call__(self, writer, kernel): |
| 53 |
module = Module("XCCMapping Off") |
| 54 |
return module |
| 55 |
|
| 56 |
class XCCMappingOn(XCCMapping): |
| 57 |
|
| 58 |
@classmethod |
| 59 |
def matches(cls, writer, debug=False): |
| 60 |
return writer.states.kernel["StreamKXCCMapping"] > 0 |
| 61 |
|
| 62 |
def __call__(self, writer, kernel): |
| 63 |
module = Module("XCCMapping On") |
| 64 |
|
| 65 |
with writer.allocTmpSgpr(4) as tmpSgprRes: |
| 66 |
sXCC = tmpSgprRes.idx |
| 67 |
sGridC = tmpSgprRes.idx + 1 |
| 68 |
sGridF = tmpSgprRes.idx + 2 |
| 69 |
sGridM = tmpSgprRes.idx + 3 |
| 70 |
sTmp = None |
| 71 |
sTmpRes = None |
| 72 |
sqTmp = writer.sgprPool.checkOut(1, "sqTmp", preventOverflow=False) |
| 73 |
divisor = kernel["StreamKXCCMapping"] |
| 74 |
if ((divisor & (divisor - 1)) != 0): |
| 75 |
sTmp = writer.sgprPool.checkOutAligned(2, 2, "sTmp", preventOverflow=False) |
| 76 |
sTmpRes = ContinuousRegister(idx=sTmp, size=2) |
| 77 |
|
| 78 |
|
| 79 |
module.add(SAddU32(dst=sgpr(sGridC), src0=sgpr("skGrid"), src1=hex(kernel["StreamKXCCMapping"] - 1), comment="ceil(grid/xccm)")) |
| 80 |
module.add(scalarStaticDivideAndRemainder(qReg=sGridC, rReg=-1, dReg=sGridC, divisor=kernel["StreamKXCCMapping"], tmpSgprRes=sTmpRes, doRemainder=0)) |
| 81 |
|
| 82 |
|
| 83 |
module.add(scalarStaticDivideAndRemainder(qReg=sGridF, rReg=sGridM, dReg="skGrid", divisor=kernel["StreamKXCCMapping"], tmpSgprRes=sTmpRes)) |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
module.add(scalarStaticDivideAndRemainder(qReg=sqTmp, rReg=sXCC, dReg="WorkGroup0", divisor=kernel["StreamKXCCMapping"], tmpSgprRes=sTmpRes, doRemainder=2)) |
| 88 |
|
| 89 |
module.add(SCmpLtU32(src0=sgpr(sXCC), src1=sgpr(sGridM), comment="XCCM < Remainder")) |
| 90 |
module.add(SCSelectB32(dst=sgpr(sGridC), src0=sgpr(sGridC), src1=sgpr(sGridF), comment="Select multiplier")) |
| 91 |
module.add(SCSelectB32(dst=sgpr(sGridM), src0=0, src1=sgpr(sGridM), comment="Select remainder")) |
| 92 |
|
| 93 |
module.add(scalarStaticDivideAndRemainder(qReg="WorkGroup0", rReg=-1, dReg="WorkGroup0", divisor=kernel["StreamKXCCMapping"], tmpSgprRes=sTmpRes, doRemainder=0)) |
| 94 |
module.add(SMulI32(dst=sgpr(sXCC), src0=sgpr(sXCC), src1=sgpr(sGridC), comment="XCC group id")) |
| 95 |
module.add(SAddU32(dst=sgpr("WorkGroup0"), src0=sgpr("WorkGroup0"), src1=sgpr(sXCC), comment="Add XCC group offset")) |
| 96 |
module.add(SAddU32(dst=sgpr("WorkGroup0"), src0=sgpr("WorkGroup0"), src1=sgpr(sGridM), comment="Add remainder offset")) |
| 97 |
|
| 98 |
writer.sgprPool.checkIn(sqTmp) |
| 99 |
if sTmp is not None: |
| 100 |
writer.sgprPool.checkIn(sTmp) |
| 101 |
|
| 102 |
return module |
| 103 |
|
| 104 |
|
| 105 |
class StreamK(Component): |
| 106 |
""" |
| 107 |
StreamK code. |
| 108 |
""" |
| 109 |
def __call__(self): |
| 110 |
assert(0) |
| 111 |
|
| 112 |
@abc.abstractmethod |
| 113 |
def preLoop(self, writer, kernel): |
| 114 |
pass |
| 115 |
|
| 116 |
@abc.abstractmethod |
| 117 |
def graWorkGroup(self, writer, kernel, tPA, tPB): |
| 118 |
pass |
| 119 |
|
| 120 |
def skTileIndex(self, writer, kernel, sTmp, tPA, tPB): |
| 121 |
module = Module("StreamK skTileIndex") |
| 122 |
|
| 123 |
|
| 124 |
if kernel["PrefetchGlobalRead"]: |
| 125 |
module.add(writer.localReadResetOffsets(kernel, tPA)) |
| 126 |
module.add(writer.localReadResetOffsets(kernel, tPB)) |
| 127 |
|
| 128 |
module.addComment0("StreamK calculate tile idx and map to WG") |
| 129 |
|
| 130 |
|
| 131 |
module.add(sMagicDiv2(sgpr(sTmp), sgpr(sTmp+1), sgpr("StreamKIter"), sgpr("MagicNumberItersPerTile"), sgpr("MagicShiftItersPerTile"), sgpr(sTmp+2))) |
| 132 |
|
| 133 |
module.add(SMulI32(dst=sgpr(sTmp+1), src0=sgpr(sTmp), src1=sgpr("ItersPerTile"), comment="Tile start iteration")) |
| 134 |
|
| 135 |
module.add(SAddU32(dst=sgpr(sTmp+2), src0=sgpr(sTmp+1), src1=sgpr("ItersPerTile"), comment="Tile end iteration")) |
| 136 |
|
| 137 |
module.add(SSubU32(dst=sgpr("StreamKLocalStart"), src0=sgpr("StreamKIter"), src1=sgpr(sTmp+1), comment="Local iteration start")) |
| 138 |
|
| 139 |
module.add(SMinU32(dst=sgpr("StreamKLocalEnd"), src0=sgpr("StreamKIterEnd"), src1=sgpr(sTmp+2), comment="1. (Local) iteration end (SK tile)")) |
| 140 |
module.add(SSubU32(dst=sgpr("StreamKLocalEnd"), src0=sgpr("StreamKLocalEnd"), src1=sgpr(sTmp+1), comment="2. Local iteration end (SK tile)")) |
| 141 |
|
| 142 |
return module |
| 143 |
|
| 144 |
def skIndexToWG(self, writer, kernel, sTmp): |
| 145 |
|
| 146 |
module = Module("StreamK skIndexToWG") |
| 147 |
|
| 148 |
|
| 149 |
module.addComment0("Map StreamK tile index to wg0/1/2") |
| 150 |
module.add(SMulI32(dst=sgpr(sTmp+1), src0=sgpr("NumWorkGroups0"), src1=sgpr("NumWorkGroups1"), comment="Total tiles")) |
| 151 |
tmpVgpr = writer.vgprPool.checkOut(2, "div") |
| 152 |
tmpVgprRes = ContinuousRegister(idx=tmpVgpr, size=2) |
| 153 |
module.add(scalarUInt32DivideAndRemainder(qReg="WorkGroup2", dReg=sTmp, divReg=sTmp+1, rReg=sTmp+2, tmpVgprRes=tmpVgprRes, wavewidth=kernel["WavefrontSize"], doRemainder=True, comment="TileID // nWG0*nWG1")) |
| 154 |
module.add(scalarUInt32DivideAndRemainder(qReg="WorkGroup1", dReg=sTmp+2, divReg="NumWorkGroups0", rReg="WorkGroup0", tmpVgprRes=tmpVgprRes, wavewidth=kernel["WavefrontSize"], doRemainder=True, comment="TileID // nWG0")) |
| 155 |
tmpVgprRes = None |
| 156 |
writer.vgprPool.checkIn(tmpVgpr) |
| 157 |
module.addSpaceLine() |
| 158 |
|
| 159 |
return module |
| 160 |
|
| 161 |
def skExtraIters(self, writer, kernel, sSkExtraIters, sTmp): |
| 162 |
|
| 163 |
|
| 164 |
module = Module("StreamK skExtraIters") |
| 165 |
module.add(SMulI32(dst=sgpr(sSkExtraIters), src0=sgpr("skTiles"), src1=sgpr("ItersPerTile"))) |
| 166 |
module.add(SMulI32(dst=sgpr(sTmp), src0=sgpr("SKItersPerWG"), src1=sgpr("skGrid"))) |
| 167 |
module.add(SSubU32(dst=sgpr(sSkExtraIters), src0=sgpr(sSkExtraIters), src1=sgpr(sTmp), comment="skTiles * ItersPerTile - SKItersPerWG * skGrid")) |
| 168 |
|
| 169 |
return module |
| 170 |
|
| 171 |
@abc.abstractmethod |
| 172 |
def computeLoadSrd(self, writer, kernel, tc, sTmp): |
| 173 |
pass |
| 174 |
|
| 175 |
def computeLoadSrdCommon(self, writer, kernel, tc, sTmp): |
| 176 |
module = Module("StreamK Common computeLoadSrd") |
| 177 |
|
| 178 |
tileStart = sTmp + 2 |
| 179 |
|
| 180 |
module.add(SMulI32(dst=sgpr(sTmp), src0=sgpr("StreamKLocalStart"), src1="DepthU", comment="StreamK tile start offset")) |
| 181 |
strideL = writer.strideRef(tc, kernel["ProblemType"]["IndicesSummation"][0]) |
| 182 |
module.add(writer.s_mul_u64_u32(sgpr(sTmp), sgpr(sTmp+1), sgpr(sTmp), strideL, comment="StreamK tile start offset")) |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
module.add(SAddU32(dst=sgpr(tileStart+0), src0=sgpr(tileStart+0), src1=sgpr(sTmp+0), comment="accum GsuOffset term to tilestart")) |
| 187 |
module.add(SAddCU32(dst=sgpr(tileStart+1), src0=sgpr(tileStart+1), src1=sgpr(sTmp+1), comment="accum GsuOffset term to tilestart")) |
| 188 |
|
| 189 |
return module |
| 190 |
|
| 191 |
@abc.abstractmethod |
| 192 |
def computeStoreSrdStart(self, writer, kernel): |
| 193 |
pass |
| 194 |
|
| 195 |
def computeStoreSrdStartCommon(self, writer, kernel): |
| 196 |
module = Module("StreamK Common computeStoreSrdStart") |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
skSplitSrd = Label("SK_SplitSrd", "") |
| 201 |
module.add(SCmpEQU64(src0=sgpr("AddressFlags", 2), src1=hex(0), comment="Check for synchronizer")) |
| 202 |
module.add(SCBranchSCC0(labelName=skSplitSrd.getLabelName(), comment="Skip this block if using single-kernel stream-k fixup")) |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
indices = list(range(0, kernel["ProblemType"]["NumIndicesC"])) |
| 208 |
numDim = len(indices) |
| 209 |
|
| 210 |
module.add(SCmpEQU32(src0=sgpr("skTiles"), src1=1, comment="split == 1 ?")) |
| 211 |
module.add(SCBranchSCC1(labelName=skSplitSrd.getLabelName(), comment="branch if split == 1")) |
| 212 |
|
| 213 |
with writer.allocTmpSgpr(4, alignment=1) as tmpSgprInfo: |
| 214 |
if tmpSgprInfo.idx % 2 == 0: |
| 215 |
tmpSgprX2 = tmpSgprInfo.idx+0 |
| 216 |
tmpSgpr0 = tmpSgprInfo.idx+0 |
| 217 |
tmpSgpr1 = tmpSgprInfo.idx+1 |
| 218 |
tmpSgpr2 = tmpSgprInfo.idx+2 |
| 219 |
tmpSgpr3 = tmpSgprInfo.idx+3 |
| 220 |
else: |
| 221 |
tmpSgprX2 = tmpSgprInfo.idx+1 |
| 222 |
tmpSgpr0 = tmpSgprInfo.idx+1 |
| 223 |
tmpSgpr1 = tmpSgprInfo.idx+2 |
| 224 |
tmpSgpr2 = tmpSgprInfo.idx+0 |
| 225 |
tmpSgpr3 = tmpSgprInfo.idx+3 |
| 226 |
module.addComment("Split Output Buffer offset: Free0 + (Free1-1)*StrideC1J + (Free2-1)*StrideCK * SplitIdx * bpe%s") |
| 227 |
|
| 228 |
module.addModuleAsFlatItems(writer.s_mul_u64_u32(sgpr(tmpSgpr0), sgpr(tmpSgpr1), sgpr("SizesFree+0"), sgpr("SkPartialIdx"), comment="Free0")) |
| 229 |
for i in range(1, numDim): |
| 230 |
module.add(SSubU32(dst=sgpr(tmpSgpr2), src0=sgpr("SizesFree+%u"%i), src1=1, comment="Free%u" % i)) |
| 231 |
module.add(SMulI32(dst=sgpr(tmpSgpr2), src0=sgpr(tmpSgpr2), src1=sgpr("SkPartialIdx"), comment="Free%u" % i)) |
| 232 |
module.addModuleAsFlatItems(writer.s_mul_u64_u32(sgpr(tmpSgpr2), sgpr(tmpSgpr3), sgpr(tmpSgpr2), sgpr("StrideC%s"%writer.states.indexChars[i]), comment="Free%u" % i)) |
| 233 |
module.add(SAddU32(dst=sgpr(tmpSgpr0), src0=sgpr(tmpSgpr0), src1=sgpr(tmpSgpr2), comment="Free%u" % i)) |
| 234 |
module.add(SAddCU32(dst=sgpr(tmpSgpr1), src0=sgpr(tmpSgpr1), src1=sgpr(tmpSgpr3), comment="Free%u" % i)) |
| 235 |
module.add(SLShiftLeftB64(dst=sgpr(tmpSgprX2,2), src=sgpr(tmpSgprX2,2), shiftHex=log2(writer.states.bpeCinternal), comment="scale by bpe")) |
| 236 |
module.add(SAddU32(dst=sgpr("SrdD+0"), src0=sgpr("SrdD+0"), src1=sgpr(tmpSgprX2), comment="add lo GSU offset to SRD")) |
| 237 |
module.add(SAddCU32(dst=sgpr("SrdD+1"), src0=sgpr("SrdD+1"), src1=sgpr(tmpSgpr1), comment="add hi GSU offset to SRD")) |
| 238 |
|
| 239 |
module.add(skSplitSrd) |
| 240 |
|
| 241 |
return module |
| 242 |
|
| 243 |
@abc.abstractmethod |
| 244 |
def graAddresses(self, writer, kernel, tP, vTmp): |
| 245 |
pass |
| 246 |
|
| 247 |
def graAddressesCommon(self, writer, kernel, tP, vTmp): |
| 248 |
module = Module("StreamK Common graAddresses") |
| 249 |
|
| 250 |
tc = tP["tensorChar"] |
| 251 |
|
| 252 |
tmpOffset = writer.sgprPool.checkOut(2, "skStartOffset", preventOverflow=False) |
| 253 |
module.add(SMulI32(dst=sgpr(tmpOffset), src0=sgpr("StreamKLocalStart"), src1="DepthU*%d" % (tP["bpe"]), comment="StreamK tile start offset")) |
| 254 |
strideL = writer.strideRef(tc, kernel["ProblemType"]["IndicesSummation"][0]) |
| 255 |
module.add(writer.s_mul_u64_u32(sgpr(tmpOffset), sgpr(tmpOffset+1), sgpr(tmpOffset), strideL, "StreamK tile start offset")) |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
module.add(SAddU32(dst=sgpr(tmpOffset+0), src0=sgpr(tmpOffset+0), src1=sgpr("Address%s+0" % tc), comment="accum skOffset term to tilestart")) |
| 260 |
module.add(SAddCU32(dst=sgpr(tmpOffset+1), src0=sgpr(tmpOffset+1), src1=sgpr("Address%s+1" % tc), comment="accum skOffset term to tilestart")) |
| 261 |
module.add(VMovB32(dst=vgpr(vTmp+0), src=sgpr(tmpOffset+0))) |
| 262 |
module.add(VMovB32(dst=vgpr(vTmp+1), src=sgpr(tmpOffset+1))) |
| 263 |
writer.sgprPool.checkIn(tmpOffset) |
| 264 |
|
| 265 |
return module |
| 266 |
|
| 267 |
@abc.abstractmethod |
| 268 |
def declareStaggerParms(self, writer, kernel): |
| 269 |
pass |
| 270 |
|
| 271 |
def declareStaggerParmsCommon(self, writer, kernel): |
| 272 |
module = Module("StreamK Common declareStaggerParms") |
| 273 |
|
| 274 |
|
| 275 |
module.add(SCmpGtU32(src0=sgpr("StreamKLocalStart"), src1=0, comment="does wg start tile?")) |
| 276 |
module.add(SCMovB32(dst=sgpr("StaggerUIter"), src=0, comment="set stagger=0 for partial tiles")) |
| 277 |
module.add(SCmpLtU32(src0=sgpr("StreamKLocalEnd"), src1=sgpr("ItersPerTile"), comment="does wg finish tile?")) |
| 278 |
module.add(SCMovB32(dst=sgpr("StaggerUIter"), src=0, comment="set stagger=0 for partial tiles")) |
| 279 |
|
| 280 |
return module |
| 281 |
|
| 282 |
@abc.abstractmethod |
| 283 |
def tailLoopNumIter(self, writer, kernel, loopCounter): |
| 284 |
pass |
| 285 |
|
| 286 |
def tailLoopNumIterCommon(self, writer, kernel, loopCounter): |
| 287 |
module = Module("StreamK Common tailLoopNumIter") |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
module.add(SCmpLtU32(src0=sgpr("StreamKLocalEnd"), src1=sgpr("ItersPerTile"), comment="Check if WG processes final iteration of tile")) |
| 292 |
module.add(SCMovB32(dst=loopCounter, src=0, comment="This WG not completing tile")) |
| 293 |
|
| 294 |
return module |
| 295 |
|
| 296 |
@abc.abstractmethod |
| 297 |
def calculateLoopNumIter(self, writer, kernel, loopCounterName, loopIdx, tmpSgprInfo): |
| 298 |
pass |
| 299 |
|
| 300 |
def calculateLoopNumIterCommon(self, writer, kernel, loopCounterName, loopIdx, tmpSgprInfo): |
| 301 |
module = Module("StreamK Common calculateLoopNumIter") |
| 302 |
|
| 303 |
|
| 304 |
module.add(SSubU32(dst=sgpr(loopCounterName), src0=sgpr("StreamKLocalEnd"), src1=sgpr("StreamKLocalStart"), comment="StreamK loop counter = localEnd - localStart")) |
| 305 |
|
| 306 |
alphaLabel2 = Label("SKAlphaCheck2", "") |
| 307 |
module.add(BranchIfNotZero("Alpha", kernel["ProblemType"]["ComputeDataType"].toEnum(), alphaLabel2)) |
| 308 |
module.add(SMovB32(dst=sgpr(loopCounterName), src=0, comment="Skip iterations")) |
| 309 |
module.add(alphaLabel2) |
| 310 |
|
| 311 |
|
| 312 |
if not kernel["NoTailLoop"]: |
| 313 |
tmpSgpr = tmpSgprInfo.idx |
| 314 |
unrollIdx = writer.states.unrollIdx |
| 315 |
loopChar = writer.states.indexChars[kernel["ProblemType"]["IndicesSummation"][unrollIdx]] |
| 316 |
|
| 317 |
assert kernel["DepthU"] % 2 == 0 |
| 318 |
if ((kernel["DepthU"] & (kernel["DepthU"] - 1)) == 0): |
| 319 |
module.add(scalarStaticDivideAndRemainder(qReg=tmpSgpr, rReg=tmpSgpr+1, dReg=("SizesSum+%u" % unrollIdx), divisor=kernel["DepthU"], tmpSgprRes=None, doRemainder=2)) |
| 320 |
else: |
| 321 |
with writer.allocTmpSgpr(4) as tmpSgpr1: |
| 322 |
module.add(scalarStaticDivideAndRemainder(qReg=tmpSgpr, rReg=tmpSgpr+1, dReg=("SizesSum+%u" % unrollIdx), divisor=kernel["DepthU"], tmpSgprRes=tmpSgpr1, doRemainder=2)) |
| 323 |
module.add(SCmpEQU32(src0=sgpr(tmpSgpr+1), src1=0, comment="numIter%s == 0"%loopChar )) |
| 324 |
module.add(SCSelectB32(dst=sgpr(tmpSgpr), src0=0, src1=1, comment="check if size uses tail loop")) |
| 325 |
module.add(SCmpEQU32(src0=sgpr("StreamKLocalEnd"), src1=sgpr("ItersPerTile"), comment="Check if WG processes final iteration of tile")) |
| 326 |
module.add(SCSelectB32(dst=sgpr(tmpSgpr), src0=sgpr(tmpSgpr), src1=0, comment="this WG runs tail loop")) |
| 327 |
module.add(SSubU32(dst=sgpr(loopCounterName), src0=sgpr(loopCounterName), src1=sgpr(tmpSgpr), comment="Adjust loop counter for tail loop")) |
| 328 |
|
| 329 |
return module |
| 330 |
|
| 331 |
@abc.abstractmethod |
| 332 |
def storeBranches(self, writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct): |
| 333 |
pass |
| 334 |
|
| 335 |
def storeBranchesCommon(self, writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct): |
| 336 |
module = Module("StreamK Common storeBranches") |
| 337 |
|
| 338 |
|
| 339 |
if kernel["StreamKAtomic"]: |
| 340 |
return module |
| 341 |
|
| 342 |
skFixupLabel = Label(label=writer.labels.getNameInc("SK_Fixup"), comment="") |
| 343 |
skStoreLabel = Label(label=writer.labels.getNameInc("SK_Store"), comment="") |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
tmpSgpr = writer.sgprPool.checkOut(4, "globalWriteElements", preventOverflow=False) |
| 351 |
|
| 352 |
|
| 353 |
module.add(SCmpEQU32(src0=sgpr("StreamKLocalStart"), src1=0, comment="does wg start tile?")) |
| 354 |
module.add(writer.longBranchScc0(skPartialsLabel, posNeg=1)) |
| 355 |
|
| 356 |
|
| 357 |
if kernel["DebugStreamK"] & 1 == 0: |
| 358 |
|
| 359 |
|
| 360 |
module.add(SCmpEQU32(src0=sgpr("StreamKLocalEnd"), src1=sgpr("ItersPerTile"), comment="does wg finish tile?")) |
| 361 |
module.add(SCBranchSCC1(labelName=skStoreLabel.getLabelName(), comment="Branch if started and finished tile, go to regular store code")) |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
sCtaIdx = writer.sgprPool.checkOut(1, "CtaIdx", preventOverflow=False) |
| 366 |
module.add(SAddU32(dst=sgpr(sCtaIdx), src0=sgpr("StreamKIdx"), src1=1, comment="input partial tile index")) |
| 367 |
|
| 368 |
sFixupEnd = writer.sgprPool.checkOut(1, "FixupEnd", preventOverflow=False) |
| 369 |
module.add(sMagicDiv2(sgpr(tmpSgpr), sgpr(tmpSgpr+1), sgpr("StreamKIterEnd"), sgpr("MagicNumberItersPerTile"), sgpr("MagicShiftItersPerTile"), sgpr(tmpSgpr+2))) |
| 370 |
module.add(SMulI32(dst=sgpr(tmpSgpr), src0=sgpr(tmpSgpr), src1=sgpr("ItersPerTile"), comment="start iteration of partial tile")) |
| 371 |
module.add(SSubU32(dst=sgpr(sFixupEnd), src0=sgpr("StreamKIterEnd"), src1=sgpr(tmpSgpr), comment="calc iterations completed by this WG")) |
| 372 |
|
| 373 |
module.add(skFixupLabel) |
| 374 |
|
| 375 |
|
| 376 |
module.add(SLShiftLeftB32(dst=sgpr(tmpSgpr), src=sgpr(sCtaIdx), shiftHex=log2(4), comment="flag offset based on CTA index")) |
| 377 |
module.add(SLoadB32(dst=sgpr(tmpSgpr+2), base=sgpr("AddressFlags", 2), soffset=sgpr(tmpSgpr), smem=SMEMModifiers(glc=True), comment="get flag")) |
| 378 |
|
| 379 |
module.add(SWaitCnt(kmcnt=0, comment="wait for flag load")) |
| 380 |
if kernel["DebugStreamK"] & 2 == 0: |
| 381 |
module.add(SCmpEQU32(src0=sgpr(tmpSgpr+2), src1=1, comment="check if ready")) |
| 382 |
module.add(SCBranchSCC0(labelName=skFixupLabel.getLabelName(), comment="if flag not set, wait and check again")) |
| 383 |
|
| 384 |
|
| 385 |
module.add(SBarrier(comment="wait for all workgroups before resetting flag")) |
| 386 |
skipFlagReset = Label(label=writer.labels.getNameInc("SK_SkipFlagReset"), comment="") |
| 387 |
module.add(VReadfirstlaneB32(dst=sgpr(tmpSgpr+2), src=vgpr("Serial"), comment="Wave 0 updates flags")) |
| 388 |
module.add(SCmpEQU32(src0=sgpr(tmpSgpr+2), src1=0, comment="Check for wave 0")) |
| 389 |
module.add(SCBranchSCC0(labelName=skipFlagReset.getLabelName(), comment="Skip flag reset")) |
| 390 |
|
| 391 |
module.add(SStoreB32(src=sgpr(tmpSgpr+2), base=sgpr("AddressFlags", 2), soffset=sgpr(tmpSgpr), smem=SMEMModifiers(glc=True), comment="reset flag")) |
| 392 |
module.add(skipFlagReset) |
| 393 |
writer.sgprPool.checkIn(tmpSgpr) |
| 394 |
|
| 395 |
fixupEdge = [False] |
| 396 |
module.add(self.fixupStep(writer, kernel, vectorWidths, elements, fixupEdge, tmpVgpr, cvtVgprStruct, sCtaIdx)) |
| 397 |
|
| 398 |
if kernel["StreamK"] >= 2: |
| 399 |
sSkExtraIters = writer.sgprPool.checkOut(1, "extraIters", preventOverflow=False) |
| 400 |
sIterCount = writer.sgprPool.checkOut(1, "iterCount", preventOverflow=False) |
| 401 |
module.add(self.skExtraIters(writer, kernel, sSkExtraIters, sIterCount)) |
| 402 |
module.add(SAddU32(dst=sgpr(sIterCount), src0=sgpr("SKItersPerWG"), src1=1, comment="Add extra iter")) |
| 403 |
module.add(SCmpLtU32(src0=sgpr(sCtaIdx), src1=sgpr(sSkExtraIters), comment="Check if next WG had an extra iteration")) |
| 404 |
module.add(SCSelectB32(dst=sgpr(sIterCount), src0=sgpr(sIterCount), src1=sgpr("SKItersPerWG"), comment="Select correct number of iterations for next WG")) |
| 405 |
module.add(SAddU32(dst=sgpr(sFixupEnd), src0=sgpr(sFixupEnd), src1=sgpr(sIterCount), comment="next partial tile iteration")) |
| 406 |
writer.sgprPool.checkIn(sSkExtraIters) |
| 407 |
writer.sgprPool.checkIn(sIterCount) |
| 408 |
module.add(SAddU32(dst=sgpr(sCtaIdx), src0=sgpr(sCtaIdx), src1=1, comment="next partial tile index")) |
| 409 |
if kernel["StreamK"] == 1: |
| 410 |
module.add(SAddU32(dst=sgpr(sFixupEnd), src0=sgpr(sFixupEnd), src1=sgpr("SKItersPerWG"), comment="next partial tile iteration")) |
| 411 |
module.add(SCmpLtU32(src0=sgpr(sFixupEnd), src1=sgpr("ItersPerTile"), comment="done loading partial tiles?")) |
| 412 |
module.add(SCBranchSCC1(labelName=skFixupLabel.getLabelName(), comment="Branch to continue fixup loop")) |
| 413 |
|
| 414 |
writer.sgprPool.checkIn(sFixupEnd) |
| 415 |
writer.sgprPool.checkIn(sCtaIdx) |
| 416 |
|
| 417 |
module.add(skStoreLabel) |
| 418 |
|
| 419 |
return module |
| 420 |
|
| 421 |
@abc.abstractmethod |
| 422 |
def writePartials(self, writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct, endLabel): |
| 423 |
pass |
| 424 |
|
| 425 |
def writePartialsCommon(self, writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct, endLabel): |
| 426 |
module = Module("StreamK Common writePartials") |
| 427 |
|
| 428 |
|
| 429 |
if kernel["StreamKAtomic"]: |
| 430 |
return module |
| 431 |
|
| 432 |
module.add(skPartialsLabel) |
| 433 |
if kernel["DebugStreamK"] & 2 != 0: |
| 434 |
return module |
| 435 |
|
| 436 |
|
| 437 |
edges = [False] |
| 438 |
|
| 439 |
partialsLabels = {} |
| 440 |
for edge in edges: |
| 441 |
partialsLabels[edge] = Label(writer.labels.getNameInc("GW_Partials_E%u" % ( 1 if edge else 0)), comment="") |
| 442 |
|
| 443 |
if False in edges and True in edges: |
| 444 |
with self.allocTmpSgpr(4) as tmpSgprInfo: |
| 445 |
module.add(writer.checkIsEdge(kernel, tmpSgprInfo, partialsLabels[True], partialsLabels[True])) |
| 446 |
|
| 447 |
for edge in edges: |
| 448 |
module.add(partialsLabels[edge]) |
| 449 |
module.add(self.computeWorkspaceSrd(writer, kernel, sgpr("StreamKIdx"))) |
| 450 |
module.add(self.partialsWriteProcedure(writer, kernel, vectorWidths, elements, False, False, edge, tmpVgpr, cvtVgprStruct, endLabel)) |
| 451 |
|
| 452 |
return module |
| 453 |
|
| 454 |
def computeWorkspaceSrd(self, writer, kernel, sCtaIdx, tmpSgpr = None): |
| 455 |
module = Module("StreamK Common computeWorkspaceSrd") |
| 456 |
|
| 457 |
|
| 458 |
module.add(SMovB64(dst=sgpr("SrdWS+0", 2), src=sgpr("AddressWS+0", 2), comment="init SRD base address")) |
| 459 |
module.add(SMovB32(dst=sgpr("SrdWS+2"), src="BufferOOB", comment="")) |
| 460 |
module.add(SMovB32(dst=sgpr("SrdWS+3"), src="Srd127_96", comment="Set bits 127_96 in post-loop SRD")) |
| 461 |
|
| 462 |
tmpLocal = None |
| 463 |
if tmpSgpr == None: |
| 464 |
tmpLocal = writer.sgprPool.checkOut(1, "SKMappingTemp", preventOverflow=False) |
| 465 |
tmpSgpr = tmpLocal |
| 466 |
|
| 467 |
assert kernel["BufferStore"] |
| 468 |
module.addSpaceLine() |
| 469 |
module.add(SMulI32(dst=sgpr(tmpSgpr), src0=hex(kernel["MacroTile0"]*kernel["MacroTile1"]*writer.states.bpeCinternal), src1=sCtaIdx, comment="Offset to correct partials tile")) |
| 470 |
module.add(SAddU32(dst=sgpr("SrdWS+0"), src0=sgpr("SrdWS+0"), src1=sgpr(tmpSgpr), comment="add lo to SRD")) |
| 471 |
module.add(SAddCU32(dst=sgpr("SrdWS+1"), src0=sgpr("SrdWS+1"), src1=0, comment="add hi to SRD")) |
| 472 |
|
| 473 |
if tmpLocal is not None: |
| 474 |
writer.sgprPool.checkIn(tmpLocal) |
| 475 |
|
| 476 |
return module |
| 477 |
|
| 478 |
def partialsWriteProcedure(self, writer, kernel, vectorWidths, elements, alpha, beta, edge, tmpVgpr, cvtVgprStruct, endLabel): |
| 479 |
module = Module("StreamK Common partialsWriteProcedure") |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
edgeI = edge |
| 498 |
|
| 499 |
gwvw = vectorWidths[edgeI] |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
vectorDataTypes = VectorDataTypes() |
| 507 |
ss = StoreState(writer, kernel, gwvw, edge, beta, False, elements[edgeI], vectorDataTypes, dim=0, isWorkspace=True) |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
maxVgprs, _ = writer.getMaxRegsForOccupancy(kernel["NumThreads"], writer.vgprPool.size(), writer.sgprPool.size(), \ |
| 512 |
writer.getLdsSize(kernel), writer.agprPool.size(), writer.states.doubleVgpr) |
| 513 |
if writer.states.serializedStore: |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
writer.vgprPool.growPool(writer.vgprPool.size()-writer.vgprPool.available(), maxVgprs, 1, \ |
| 521 |
"grow-pool up to next occupancy for GlobalWrite") |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
numVgprAvailable = writer.vgprPool.availableBlock(ss.numVgprsPerElement, ss.align) |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
minElements = 1 |
| 542 |
if kernel["ProblemType"]["DataType"].isHalf() or kernel["ProblemType"]["DataType"].isBFloat16(): |
| 543 |
minElements = 2 |
| 544 |
elif kernel["ProblemType"]["DataType"].is8bitFloat(): |
| 545 |
|
| 546 |
minElements = 4 |
| 547 |
minNeeded = minElements * ss.numVgprsPerElement |
| 548 |
|
| 549 |
shrinkDb = 0 |
| 550 |
if shrinkDb: |
| 551 |
print("numVgprAvailable=", numVgprAvailable, "minElements=", minElements, "minNeeded=", minNeeded) |
| 552 |
|
| 553 |
if numVgprAvailable < minNeeded: |
| 554 |
gwvwOrig = gwvw |
| 555 |
currentOccupancy = writer.getOccupancy(kernel["NumThreads"], writer.getLdsSize(kernel), \ |
| 556 |
writer.vgprPool.size(), writer.sgprPool.size(), writer.agprPool.size(), writer.states.doubleVgpr) |
| 557 |
futureOccupancy = writer.getOccupancy(kernel["NumThreads"], writer.getLdsSize(kernel), \ |
| 558 |
writer.vgprPool.size() - numVgprAvailable + minNeeded, writer.sgprPool.size(), writer.agprPool.size(), writer.states.doubleVgpr) |
| 559 |
|
| 560 |
if shrinkDb: |
| 561 |
print("currentOccupancy=%u futureOccupancy=%u VGPRs=%u numVgprAvail=%u vgprPerElem=%u" \ |
| 562 |
% (currentOccupancy, futureOccupancy, writer.vgprPool.size(), \ |
| 563 |
numVgprAvailable, minElements*ss.numVgprsPerElement)) |
| 564 |
if futureOccupancy > currentOccupancy: |
| 565 |
if shrinkDb: |
| 566 |
print("warning: %s growing VGPR for GlobalWrite batching - this may bloat VGPR usage" % \ |
| 567 |
(writer.states.kernelName)) |
| 568 |
print(" numVgprAvailable=", numVgprAvailable, \ |
| 569 |
"numVgprsPerElement=", ss.numVgprsPerElement, \ |
| 570 |
"beta=", beta, "gwvw=", gwvw) |
| 571 |
elif gwvw != gwvwOrig: |
| 572 |
ss.gwvw = gwvw |
| 573 |
if shrinkDb: |
| 574 |
print2("info: %s shrank gwvw from %u to %u but kept occupancy same=%u." \ |
| 575 |
% (writer.states.kernelName, gwvwOrig, gwvw, currentOccupancy)) |
| 576 |
|
| 577 |
if numVgprAvailable < minElements*ss.numVgprsPerElement: |
| 578 |
print2("info: growing pool += %d * %d for GlobalWrite\n" \ |
| 579 |
% (minElements,ss.numVgprsPerElement)) |
| 580 |
print2(writer.vgprPool.state()) |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
writer.vgprPool.growPool(0, minElements, ss.numVgprsPerElement, \ |
| 587 |
"grow-pool for GlobalWrite") |
| 588 |
numVgprAvailable = writer.vgprPool.available() |
| 589 |
print2(writer.vgprPool.state()) |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
atomicW = min(gwvw, writer.getVectorAtomicWidth(kernel)) |
| 594 |
|
| 595 |
|
| 596 |
if ss.numVgprsPerElement: |
| 597 |
numElementsPerBatch = numVgprAvailable // ss.numVgprsPerElement |
| 598 |
else: |
| 599 |
numElementsPerBatch = len(elements[edgeI]) |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
numElementsPerBatch = numElementsPerBatch if not kernel["NumElementsPerBatchStore"] else min(kernel["NumElementsPerBatchStore"],numElementsPerBatch) |
| 604 |
|
| 605 |
if shrinkDb: |
| 606 |
print("NumElementsPerBatch=", numElementsPerBatch, "LimitedBySgprs=", ss.cfg.numElementsPerBatchLimitedBySgprs, \ |
| 607 |
"WARNING" if ss.cfg.numElementsPerBatchLimitedBySgprs < numElementsPerBatch else "okay") |
| 608 |
if ss.cfg.numElementsPerBatchLimitedBySgprs < numElementsPerBatch: |
| 609 |
numElementsPerBatch = ss.cfg.numElementsPerBatchLimitedBySgprs |
| 610 |
|
| 611 |
|
| 612 |
if (kernel["ProblemType"]["DataType"].isHalf() or kernel["ProblemType"]["DataType"].isBFloat16()): |
| 613 |
|
| 614 |
if numElementsPerBatch > 1: |
| 615 |
numElementsPerBatch = int(numElementsPerBatch/2)*2 |
| 616 |
elif not kernel["EnableMatrixInstruction"]: |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
if shrinkDb: |
| 625 |
print("WARNING: half requires at least two elements per batch") |
| 626 |
self.overflowedResources = 3 |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
assert numElementsPerBatch > 0, "numElementsPerBatch=0 for %s"%writer.states.kernelName |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
numBatches = max(1, ceilDivide(len(elements[edgeI]),numElementsPerBatch)) |
| 643 |
|
| 644 |
numSgprs = ss.cfg.numTempSgprPerBatch + ss.cfg.numMaskSgprPerBatch + ss.cfg.numMaskSgprPerElement * numElementsPerBatch |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
if writer.db["PrintStoreRegisterDb"]: |
| 649 |
print("edgeI", edgeI, "NumBatches", numBatches, "NumElementsPerBatch", numElementsPerBatch, "numVgprsPerElement", ss.numVgprsPerElement, "len(elements[edgeI])", len(elements[edgeI])) |
| 650 |
print("numSgprs=", numSgprs, "sgprPool.size()=", writer.sgprPool.size(), "numTempSgprPerBatch=", ss.cfg.numTempSgprPerBatch, |
| 651 |
"numMaskSgprPerBatch=", ss.cfg.numMaskSgprPerBatch, "numMaskSgprPerElement=", ss.cfg.numMaskSgprPerElement) |
| 652 |
print(writer.sgprPool.state()) |
| 653 |
module.addComment1("edge=%d, allocate %u sgpr. perBatchTmpS=%u perBatchMaskS=%u perElementMaskS=%u elementsPerBatch=%u" % |
| 654 |
(edgeI, numSgprs, ss.cfg.numTempSgprPerBatch, ss.cfg.numMaskSgprPerBatch, ss.cfg.numMaskSgprPerElement, numElementsPerBatch)) |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
with writer.allocTmpSgpr(numSgprs, 2) as tmpSgprRes: |
| 659 |
tmpSgpr = tmpSgprRes.idx |
| 660 |
elementSgprs = tmpSgpr + ss.cfg.numTempSgprPerBatch |
| 661 |
|
| 662 |
codeAccVgprRead = deepcopy(writer.codes.accVgprRead) if writer.states.serializedStore else None |
| 663 |
|
| 664 |
useCodeMulAlpha = kernel["MIArchVgpr"] and alpha and not (kernel["GlobalSplitU"] > 1 or kernel["GlobalSplitU"] == -1) |
| 665 |
if useCodeMulAlpha: |
| 666 |
codeAccVgprRead = None |
| 667 |
|
| 668 |
for batchIdx in range(0, numBatches): |
| 669 |
elementStartIdx = batchIdx * numElementsPerBatch |
| 670 |
elementStopIdx = min(elementStartIdx + numElementsPerBatch, len(elements[edgeI])) |
| 671 |
elementsThisBatch = elements[edgeI][elementStartIdx:elementStopIdx] |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
module.add(self.partialsWriteBatch(writer, kernel, ss, batchIdx, alpha, beta, edge, gwvw, atomicW, \ |
| 677 |
elementsThisBatch, writer.vgprs.addrD, writer.vgprs.addrC, \ |
| 678 |
tmpVgpr, cvtVgprStruct, \ |
| 679 |
elementSgprs, tmpSgpr, codeAccVgprRead)) |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
module.add(SWaitCnt(vscnt=0, comment="wait for data store")) |
| 686 |
module.add(SBarrier(comment="store all data before setting flag")) |
| 687 |
module.add(SLShiftLeftB32(dst=sgpr(tmpSgpr), src=sgpr("StreamKIdx"), shiftHex=log2(4), comment="flag offset based on CTA index")) |
| 688 |
with writer.allocTmpSgpr(1) as flagSgprRes: |
| 689 |
flagSgpr = flagSgprRes.idx |
| 690 |
skipFlagSet = Label(label=writer.labels.getNameInc("SK_SkipFlagSet"), comment="") |
| 691 |
module.add(VReadfirstlaneB32(dst=sgpr(flagSgpr), src=vgpr("Serial"), comment="Wave 0 updates flags")) |
| 692 |
module.add(SCmpEQU32(src0=sgpr(flagSgpr), src1=0, comment="Check for wave 0")) |
| 693 |
module.add(SCBranchSCC0(labelName=skipFlagSet.getLabelName(), comment="Skip flag set")) |
| 694 |
module.add(SMovB32(dst=sgpr(flagSgpr), src=1, comment="flag data")) |
| 695 |
module.add(SStoreB32(src=sgpr(flagSgpr), base=sgpr("AddressFlags", 2), soffset=sgpr(tmpSgpr), smem=SMEMModifiers(glc=True), comment="set flag")) |
| 696 |
module.add(skipFlagSet) |
| 697 |
module.add(SWaitCnt(kmcnt=0, comment="wait for flag")) |
| 698 |
|
| 699 |
module.add(SBranch(labelName=endLabel.getLabelName(), comment="jump to end")) |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
return module |
| 705 |
|
| 706 |
def partialsWriteBatch(self, writer, kernel, ss, batchIdx, applyAlpha, beta, edge, gwvw, atomicW, \ |
| 707 |
batchElements, addrD, addrC, \ |
| 708 |
tmpVgpr, cvtVgprStruct, batchElementSgprs, tmpSgpr, codeAccVgprRead): |
| 709 |
module = Module("StreamK Common partialsWriteBatch") |
| 710 |
|
| 711 |
module.addComment0("optSingleColVgpr=%u optSharedColVgpr=%u optSGPRUsage=%s optSrdIncForRow=%u" % \ |
| 712 |
(ss.optSingleColVgpr, ss.optSharedColVgpr, ss.optSGPRUsage, ss.optSrdIncForRow)) |
| 713 |
|
| 714 |
if kernel["StoreSyncOpt"]: |
| 715 |
module.add(SSleep(kernel["StoreSyncOpt"] - 1, "optimization: sync and wait")) |
| 716 |
module.add(SBarrier()) |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
commentStr = "Partials Write%s%s%s Batch #%u (d1,d0,vc1,vc0) =\n " \ |
| 721 |
% (" Alpha" if applyAlpha else "", " Beta" if beta else "", " Edge" if edge else "", batchIdx) |
| 722 |
for elementIdx in range(0, len(batchElements)): |
| 723 |
element = batchElements[elementIdx] |
| 724 |
commentStr += "(%u,%u,%u,%u:vw%u)" % (element[0], element[1], element[2], element[3], gwvw) |
| 725 |
if elementIdx < len(batchElements)-1: |
| 726 |
commentStr += "; " |
| 727 |
module.addComment2(commentStr) |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
ss.setupStoreElementsForBatch(kernel, gwvw, batchElements, batchElementSgprs, isOptNLL=False, factorDim=0, isWorkspace=True) |
| 733 |
|
| 734 |
storesIssued = 0 |
| 735 |
tmpS01 = tmpSgpr |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
module.addComment1("calc coords, apply mask, and issue loads (if necessary)") |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
if writer.db["ConservativeWaitCnt"] & 0x10: |
| 745 |
module.add(SBarrier("debug")) |
| 746 |
module.add(SWaitCnt(vlcnt=0, vscnt=0, comment="ConservativeWaitCnt")) |
| 747 |
module.add(SBarrier("debug")) |
| 748 |
if not edge and writer.db["ForceEdgeStores"]>=2: |
| 749 |
module.add(writer.getBomb()) |
| 750 |
if edge and writer.db["AssertNoEdge"]: |
| 751 |
module.add(writer.getBomb()) |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
if codeAccVgprRead is not None and kernel["LocalSplitU"] == 1: |
| 763 |
regsPerScalar = writer.states.bpeCinternal // writer.states.bpr |
| 764 |
|
| 765 |
for elementIdx in range(0, len(batchElements)): |
| 766 |
|
| 767 |
for vi in range(0, gwvw): |
| 768 |
|
| 769 |
for rIdx in range(0, regsPerScalar): |
| 770 |
module.add(replaceHolder(codeAccVgprRead.popFirstItem(), ss.elementSumIdx[elementIdx]*regsPerScalar + regsPerScalar*vi + rIdx)) |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
if not kernel["MIArchVgpr"]: |
| 777 |
module.add(SNop(1, "2 wait states required before reading vgpr")) |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
for elementIdx in range(len(batchElements)): |
| 785 |
for vi in range(gwvw): |
| 786 |
sumIdxV = ss.elementSumIdx[elementIdx] + vi |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
if kernel["ProblemType"]["ComputeDataType"].isInt32() or kernel["ProblemType"]["ComputeDataType"].isSingle(): |
| 792 |
if writer.db["ForceExpectedValue"]: |
| 793 |
module.add(VMovB32(dst=vgpr("ValuC+%u"%sumIdxV), src=writer.db["ValueCExpectedValue"], comment="force expected value")) |
| 794 |
|
| 795 |
if writer.db["ForceVSerial"]: |
| 796 |
module.add(VMovB32(dst=vgpr("ValuC+%u"%sumIdxV), src=vgpr("Serial"), comment="force expected value to serial")) |
| 797 |
|
| 798 |
if writer.db["CheckValueC"]: |
| 799 |
module.add(SMovB32(dst=sgpr(tmpS01), src=writer.db["ValueCExpectedValue"], comment="Move expected value")) |
| 800 |
module.add(writer.getCmpAssert(writer.asmAssert.eq, vgpr("ValuC+%u"%sumIdxV), sgpr(tmpS01))) |
| 801 |
|
| 802 |
module.addComment1("apply mask, calc new C and issue writes") |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
if kernel["ProblemType"]["DestDataType"].isBFloat16() and kernel["ProblemType"]["HighPrecisionAccumulate"]: |
| 813 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprBf16Mask), "0xffff0000", comment="mask for pack two bfloat16 element to 32bit" )) |
| 814 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprFp32Nan), "0x7fff0000", comment="fp32 Nan" )) |
| 815 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprBf16Inc), "0x7fff", comment="rounding bias for bfloat16" )) |
| 816 |
elif kernel["ProblemType"]["DestDataType"].isFloat8_fnuz() and kernel["ProblemType"]["HighPrecisionAccumulate"]: |
| 817 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprFp8NanInf), "0x207", comment="Nan and +/- inf" )) |
| 818 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprFp8Max), "0x43700000", comment="Fp8 Max value 240 as float32" )) |
| 819 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprFp8Min), "0xc3700000", comment="Fp8 Min value -240 as float32" )) |
| 820 |
elif kernel["ProblemType"]["DestDataType"].isFloat8() and kernel["ProblemType"]["HighPrecisionAccumulate"]: |
| 821 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprFp8NanInf), "0x207", comment="Nan and +/- inf" )) |
| 822 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprFp8Max), "0x43E00000", comment="Fp8 Max value 448 as float32" )) |
| 823 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprFp8Min), "0xc3E00000", comment="Fp8 Min value -448 as float32" )) |
| 824 |
elif kernel["ProblemType"]["DestDataType"].isAnyBFloat8() and kernel["ProblemType"]["HighPrecisionAccumulate"]: |
| 825 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprBF8NanInf), "0x207", comment="Nan and +/- inf" )) |
| 826 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprBF8Max), "0x47600000", comment="BF8 Max value 57344 as float32" )) |
| 827 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprBF8Min), "0xc7600000", comment="BF8 Min value -57344 as float32" )) |
| 828 |
|
| 829 |
if kernel["EnableMatrixInstruction"]: |
| 830 |
WaveNum = kernel["MIWaveGroup"][0] * kernel["MIWaveGroup"][1] * kernel["WorkGroup"][2] |
| 831 |
else: |
| 832 |
WaveNum = kernel["NumThreads"] // kernel["WavefrontSize"] |
| 833 |
|
| 834 |
storeCode = Module("Partials GroupLoadStore") |
| 835 |
for elementIdx in range(len(batchElements)): |
| 836 |
element = batchElements[elementIdx] |
| 837 |
addrCalc: AddrCalculation = ss.elementAddr[elementIdx] |
| 838 |
addr = addrCalc.addrDVgpr |
| 839 |
sumIdx = ss.elementSumIdx[elementIdx] |
| 840 |
|
| 841 |
storeWidth = kernel["StoreVectorWidth"] |
| 842 |
|
| 843 |
if batchIdx == 0 and elementIdx == 0: |
| 844 |
tmpSgprRes = ContinuousRegister(idx=tmpS01, size=1) |
| 845 |
module.add(vectorStaticMultiply(vgpr(addr), vgpr("Serial"), storeWidth * writer.states.bpeCinternal, tmpSgprRes)) |
| 846 |
|
| 847 |
module.add(SMovB32(dst=sgpr(tmpS01), src=0, comment="Init sgpr offset")) |
| 848 |
else: |
| 849 |
increment = (kernel["WavefrontSize"] * WaveNum) * storeWidth * writer.states.bpeCinternal |
| 850 |
|
| 851 |
module.add(SAddU32(dst=sgpr(tmpS01), src0=sgpr(tmpS01), src1=increment, comment="Inc sgpr offset")) |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
tmpStoreCode = writer.addStore(kernel, ss, 'WS', addrCalc, sumIdx, tmpS01, edge, wsOffset=sgpr(tmpS01)) |
| 863 |
if kernel["GroupLoadStore"]: |
| 864 |
storeCode.add(tmpStoreCode) |
| 865 |
else: |
| 866 |
module.add(tmpStoreCode) |
| 867 |
storesIssued += 1 |
| 868 |
|
| 869 |
module.add(storeCode) |
| 870 |
|
| 871 |
|
| 872 |
lastData = -1 |
| 873 |
for elementIdx in range(0, len(batchElements)): |
| 874 |
if not ss.sharedColDVgprs: |
| 875 |
addrCalc: AddrCalculation = ss.elementAddres[elementIdx] |
| 876 |
addrDVgpr = addrCalc.addrDVgpr |
| 877 |
addrCVgpr = addrCalc.addrCVgpr |
| 878 |
writer.vgprPool.checkIn(addrDVgpr) |
| 879 |
if addrCVgpr != addrDVgpr: |
| 880 |
writer.vgprPool.checkIn(addrCVgpr) |
| 881 |
|
| 882 |
data = ss.elementData[elementIdx] |
| 883 |
if data != 0: |
| 884 |
if data != lastData: |
| 885 |
writer.vgprPool.checkIn(data) |
| 886 |
lastData = data |
| 887 |
|
| 888 |
ss.firstBatch = False |
| 889 |
ss.checkInTempVgprC() |
| 890 |
|
| 891 |
if writer.states.serializedStore: |
| 892 |
module.add(SNop(0, "1 wait state required when next inst writes vgprs held by previous dwordx4 store inst")) |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
return module |
| 902 |
|
| 903 |
def fixupStep(self, writer, kernel, vectorWidths, elements, edges, tmpVgpr, cvtVgprStruct, sCtaIdx): |
| 904 |
module = Module("StreamK Common fixupStep") |
| 905 |
|
| 906 |
fixupLabels = {} |
| 907 |
for edge in edges: |
| 908 |
fixupLabels[edge] = Label(writer.labels.getNameInc("Fixup_E%u" % ( 1 if edge else 0)), comment="") |
| 909 |
|
| 910 |
|
| 911 |
if False in edges and True in edges: |
| 912 |
module.add(writer.checkIsEdge(kernel, tmpSgprInfo, fixupLabels[True], fixupLabels[True])) |
| 913 |
|
| 914 |
|
| 915 |
for edge in edges: |
| 916 |
|
| 917 |
module.add(fixupLabels[edge]) |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
edgeI = edge |
| 934 |
|
| 935 |
gwvw = vectorWidths[edgeI] |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
vectorDataTypes = VectorDataTypes() |
| 942 |
ss = StoreState(writer, kernel, gwvw, edge, True, False, elements[edgeI], vectorDataTypes, dim=0, isWorkspace=True) |
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
maxVgprs, _ = writer.getMaxRegsForOccupancy(kernel["NumThreads"], writer.vgprPool.size(), writer.sgprPool.size(), \ |
| 964 |
writer.getLdsSize(kernel), writer.agprPool.size(), writer.states.doubleVgpr) |
| 965 |
if writer.states.serializedStore: |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
writer.vgprPool.growPool(writer.vgprPool.size()-writer.vgprPool.available(), maxVgprs, 1, \ |
| 973 |
"grow-pool up to next occupancy for GlobalWrite") |
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
numVgprAvailable = writer.vgprPool.availableBlock(ss.numVgprsPerElement, ss.align) |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
minElements = 1 |
| 994 |
if kernel["ProblemType"]["DataType"].isHalf() or kernel["ProblemType"]["DataType"].isBFloat16(): |
| 995 |
minElements = 2 |
| 996 |
elif kernel["ProblemType"]["DataType"].is8bitFloat(): |
| 997 |
minElements = 4 |
| 998 |
minNeeded = minElements * ss.numVgprsPerElement |
| 999 |
|
| 1000 |
shrinkDb = 0 |
| 1001 |
if shrinkDb: |
| 1002 |
print("numVgprAvailable=", numVgprAvailable, "minElements=", minElements, "minNeeded=", minNeeded) |
| 1003 |
|
| 1004 |
if numVgprAvailable < minNeeded: |
| 1005 |
gwvwOrig = gwvw |
| 1006 |
currentOccupancy = writer.getOccupancy(kernel["NumThreads"], writer.getLdsSize(kernel), \ |
| 1007 |
writer.vgprPool.size(), writer.sgprPool.size(), writer.agprPool.size(), writer.states.doubleVgpr) |
| 1008 |
futureOccupancy = writer.getOccupancy(kernel["NumThreads"], writer.getLdsSize(kernel), \ |
| 1009 |
writer.vgprPool.size() - numVgprAvailable + minNeeded, writer.sgprPool.size(), writer.agprPool.size(), writer.states.doubleVgpr) |
| 1010 |
|
| 1011 |
if shrinkDb: |
| 1012 |
print("currentOccupancy=%u futureOccupancy=%u VGPRs=%u numVgprAvail=%u vgprPerElem=%u" \ |
| 1013 |
% (currentOccupancy, futureOccupancy, writer.vgprPool.size(), \ |
| 1014 |
numVgprAvailable, minElements*ss.numVgprsPerElement)) |
| 1015 |
if futureOccupancy > currentOccupancy: |
| 1016 |
if shrinkDb: |
| 1017 |
print("warning: %s growing VGPR for GlobalWrite batching - this may bloat VGPR usage" % \ |
| 1018 |
(writer.states.kernelName)) |
| 1019 |
print(" numVgprAvailable=", numVgprAvailable, \ |
| 1020 |
"numVgprsPerElement=", ss.numVgprsPerElement, \ |
| 1021 |
"gwvw=", gwvw) |
| 1022 |
elif gwvw != gwvwOrig: |
| 1023 |
ss.gwvw = gwvw |
| 1024 |
if shrinkDb: |
| 1025 |
print2("info: %s shrank gwvw from %u to %u but kept occupancy same=%u." \ |
| 1026 |
% (writer.states.kernelName, gwvwOrig, gwvw, currentOccupancy)) |
| 1027 |
|
| 1028 |
if numVgprAvailable < minElements*ss.numVgprsPerElement: |
| 1029 |
print2("info: growing pool += %d * %d for GlobalWrite\n" \ |
| 1030 |
% (minElements,ss.numVgprsPerElement)) |
| 1031 |
print2(writer.vgprPool.state()) |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
writer.vgprPool.growPool(0, minElements, ss.numVgprsPerElement, \ |
| 1038 |
"grow-pool for GlobalWrite") |
| 1039 |
numVgprAvailable = writer.vgprPool.available() |
| 1040 |
print2(writer.vgprPool.state()) |
| 1041 |
|
| 1042 |
|
| 1043 |
if ss.numVgprsPerElement: |
| 1044 |
numElementsPerBatch = numVgprAvailable // ss.numVgprsPerElement |
| 1045 |
else: |
| 1046 |
numElementsPerBatch = len(elements[edgeI]) |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
numElementsPerBatch = numElementsPerBatch if not kernel["NumElementsPerBatchStore"] else min(kernel["NumElementsPerBatchStore"],numElementsPerBatch) |
| 1051 |
|
| 1052 |
if shrinkDb: |
| 1053 |
print("NumElementsPerBatch=", numElementsPerBatch, "LimitedBySgprs=", ss.cfg.numElementsPerBatchLimitedBySgprs, \ |
| 1054 |
"WARNING" if ss.cfg.numElementsPerBatchLimitedBySgprs < numElementsPerBatch else "okay") |
| 1055 |
if ss.cfg.numElementsPerBatchLimitedBySgprs < numElementsPerBatch: |
| 1056 |
numElementsPerBatch = ss.cfg.numElementsPerBatchLimitedBySgprs |
| 1057 |
|
| 1058 |
|
| 1059 |
if (kernel["ProblemType"]["DataType"].isHalf() or kernel["ProblemType"]["DataType"].isBFloat16()): |
| 1060 |
|
| 1061 |
if numElementsPerBatch > 1: |
| 1062 |
numElementsPerBatch = int(numElementsPerBatch/2)*2 |
| 1063 |
elif not kernel["EnableMatrixInstruction"]: |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
if shrinkDb: |
| 1072 |
print("WARNING: half requires at least two elements per batch") |
| 1073 |
self.overflowedResources = 3 |
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
assert numElementsPerBatch > 0, "numElementsPerBatch=0 for %s"%writer.states.kernelName |
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
numBatches = max(1, ceilDivide(len(elements[edgeI]),numElementsPerBatch)) |
| 1087 |
|
| 1088 |
numSgprs = ss.cfg.numTempSgprPerBatch + ss.cfg.numMaskSgprPerBatch + ss.cfg.numMaskSgprPerElement * numElementsPerBatch |
| 1089 |
|
| 1090 |
if writer.db["PrintStoreRegisterDb"]: |
| 1091 |
print("edgeI", edgeI, "NumBatches", numBatches, "NumElementsPerBatch", numElementsPerBatch, "numVgprsPerElement", ss.numVgprsPerElement, "len(elements[edgeI])", len(elements[edgeI])) |
| 1092 |
print ("numSgprs=", numSgprs, "sgprPool.size()=", writer.sgprPool.size(), "numTempSgprPerBatch=", ss.cfg.numTempSgprPerBatch, |
| 1093 |
"numMaskSgprPerBatch=", ss.cfg.numMaskSgprPerBatch, "numMaskSgprPerElement=", ss.cfg.numMaskSgprPerElement) |
| 1094 |
print(writer.sgprPool.state()) |
| 1095 |
module.addComment1("edge=%d, allocate %u sgpr. perBatchTmpS=%u perBatchMaskS=%u perElementMaskS=%u elementsPerBatch=%u" % |
| 1096 |
(edgeI, numSgprs, ss.cfg.numTempSgprPerBatch, ss.cfg.numMaskSgprPerBatch, ss.cfg.numMaskSgprPerElement, numElementsPerBatch)) |
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
with writer.allocTmpSgpr(numSgprs, 2) as tmpSgprRes: |
| 1102 |
tmpSgpr = tmpSgprRes.idx |
| 1103 |
elementSgprs = tmpSgpr + ss.cfg.numTempSgprPerBatch |
| 1104 |
|
| 1105 |
codeAccVgprRead = deepcopy(writer.codes.accVgprRead) if writer.states.serializedStore else None |
| 1106 |
|
| 1107 |
codeAccVgprWrite = deepcopy(writer.codes.accVgprWrite) if writer.states.serializedStore else None |
| 1108 |
|
| 1109 |
module.add(self.computeWorkspaceSrd(writer, kernel, sgpr(sCtaIdx), tmpSgpr)) |
| 1110 |
|
| 1111 |
for batchIdx in range(0, numBatches): |
| 1112 |
elementStartIdx = batchIdx * numElementsPerBatch |
| 1113 |
elementStopIdx = min(elementStartIdx + numElementsPerBatch, len(elements[edgeI])) |
| 1114 |
elementsThisBatch = elements[edgeI][elementStartIdx:elementStopIdx] |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
module.add(self.fixupBatch(writer, kernel, ss, batchIdx, edge, gwvw, \ |
| 1120 |
elementsThisBatch, writer.vgprs.addrD, writer.vgprs.addrC, \ |
| 1121 |
tmpVgpr, cvtVgprStruct, \ |
| 1122 |
elementSgprs, tmpSgpr, codeAccVgprRead, codeAccVgprWrite)) |
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
return module |
| 1133 |
|
| 1134 |
def fixupBatch(self, writer, kernel, ss, batchIdx, edge, gwvw, \ |
| 1135 |
batchElements, addrD, addrC, \ |
| 1136 |
tmpVgpr, cvtVgprStruct, batchElementSgprs, tmpSgpr, codeAccVgprRead, codeAccVgprWrite): |
| 1137 |
module = Module("StreamK Common fixupBatch") |
| 1138 |
|
| 1139 |
module.addComment0("optSingleColVgpr=%u optSharedColVgpr=%u optSGPRUsage=%s optSrdIncForRow=%u" % \ |
| 1140 |
(ss.optSingleColVgpr, ss.optSharedColVgpr, ss.optSGPRUsage, ss.optSrdIncForRow)) |
| 1141 |
|
| 1142 |
if kernel["StoreSyncOpt"]: |
| 1143 |
module.add(SSleep(kernel["StoreSyncOpt"] - 1, "optimization: sync and wait")) |
| 1144 |
module.add(SBarrier()) |
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
commentStr = "Fixup%s Batch #%u (d1,d0,vc1,vc0) =\n " \ |
| 1149 |
% (" Edge" if edge else "", batchIdx) |
| 1150 |
for elementIdx in range(0, len(batchElements)): |
| 1151 |
element = batchElements[elementIdx] |
| 1152 |
commentStr += "(%u,%u,%u,%u:vw%u)" % (element[0], element[1], element[2], element[3], gwvw) |
| 1153 |
if elementIdx < len(batchElements)-1: |
| 1154 |
commentStr += "; " |
| 1155 |
module.addComment2(commentStr) |
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
ss.setupStoreElementsForBatch(kernel, gwvw, batchElements, batchElementSgprs, isOptNLL=False, factorDim=0, isWorkspace=True) |
| 1163 |
|
| 1164 |
loadsIssued = 0 |
| 1165 |
storesIssued = 0 |
| 1166 |
tmpS01 = tmpSgpr |
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
module.addComment1("calc coords, apply mask, and issue loads (if necessary)") |
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
if writer.db["ConservativeWaitCnt"] & 0x10: |
| 1187 |
module.add(SBarrier("debug")) |
| 1188 |
module.add(SWaitCnt(vlcnt=0, vscnt=0, comment="ConservativeWaitCnt")) |
| 1189 |
module.add(SBarrier("debug")) |
| 1190 |
if not edge and writer.db["ForceEdgeStores"]>=2: |
| 1191 |
module.add(writer.getBomb()) |
| 1192 |
if edge and writer.db["AssertNoEdge"]: |
| 1193 |
module.add(writer.getBomb()) |
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
if kernel["EnableMatrixInstruction"]: |
| 1203 |
WaveNum = kernel["MIWaveGroup"][0] * kernel["MIWaveGroup"][1] * kernel["WorkGroup"][2] |
| 1204 |
else: |
| 1205 |
WaveNum = kernel["NumThreads"] // kernel["WavefrontSize"] |
| 1206 |
|
| 1207 |
for elementIdx in range(0, len(batchElements)): |
| 1208 |
element = batchElements[elementIdx] |
| 1209 |
addrCVgpr = ss.elementAddr[elementIdx].addrCVgpr |
| 1210 |
|
| 1211 |
addrCalc = ss.elementAddr[elementIdx] |
| 1212 |
data = ss.elementData[elementIdx] |
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
vc0 = element[3] |
| 1219 |
|
| 1220 |
storeWidth = kernel["StoreVectorWidth"] |
| 1221 |
|
| 1222 |
if batchIdx == 0 and elementIdx == 0: |
| 1223 |
tmpS01Res = ContinuousRegister(idx=tmpS01, size=1) |
| 1224 |
module.add(vectorStaticMultiply(vgpr(addrCVgpr), vgpr("Serial"), storeWidth * writer.states.bpeCinternal, tmpS01Res)) |
| 1225 |
|
| 1226 |
module.add(SMovB32(dst=sgpr(tmpS01), src=0, comment="Init sgpr offset")) |
| 1227 |
else: |
| 1228 |
increment = (kernel["WavefrontSize"] * WaveNum) * storeWidth * writer.states.bpeCinternal |
| 1229 |
|
| 1230 |
module.add(SAddU32(dst=sgpr(tmpS01), src0=sgpr(tmpS01), src1=increment, comment="Inc sgpr offset")) |
| 1231 |
|
| 1232 |
module.add(writer.readInput(kernel, ss, 'WS', kernel["ProblemType"]["ComputeDataType"], addrCalc, vc0, data, gwvw, addrCVgpr, sgpr(tmpS01))) |
| 1233 |
loadsIssued += 1 |
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
if codeAccVgprRead is not None and kernel["LocalSplitU"] == 1: |
| 1240 |
regsPerScalar = writer.states.bpeCinternal // writer.states.bpr |
| 1241 |
|
| 1242 |
for elementIdx in range(0, len(batchElements)): |
| 1243 |
|
| 1244 |
for vi in range(0, gwvw): |
| 1245 |
|
| 1246 |
for rIdx in range(0, regsPerScalar): |
| 1247 |
module.add(replaceHolder(codeAccVgprRead.popFirstItem(), ss.elementSumIdx[elementIdx]*regsPerScalar + regsPerScalar*vi + rIdx - writer.states.c.startVgprValu)) |
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
if not kernel["MIArchVgpr"]: |
| 1255 |
module.add(SNop(1, "2 wait states required before reading vgpr")) |
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
interleaveStoreVmcnt = writer.states.interleaveStoreVmcnt and not edge |
| 1262 |
for elementIdx in range(0, len(batchElements)): |
| 1263 |
for vi in range(0, gwvw): |
| 1264 |
sumIdxV = ss.elementSumIdx[elementIdx] + vi |
| 1265 |
|
| 1266 |
if kernel["ProblemType"]["ComputeDataType"].isInt32() or kernel["ProblemType"]["ComputeDataType"].isSingle(): |
| 1267 |
if writer.db["ForceExpectedValue"]: |
| 1268 |
module.add(VMovB32(dst=vgpr("ValuC+%u"%sumIdxV), src=writer.db["ValueCExpectedValue"], comment="force expected value")) |
| 1269 |
if writer.db["ForceVSerial"]: |
| 1270 |
module.add(VMovB32(dst=vgpr("ValuC+%u"%sumIdxV), src=vgpr("Serial"), comment="force expected value to serial")) |
| 1271 |
if writer.db["CheckValueC"]: |
| 1272 |
module.add(SMovB32(dst=sgpr(tmpS01), src=writer.db["ValueCExpectedValue"], comment="Move expected value")) |
| 1273 |
module.add(writer.getCmpAssert(writer.asmAssert.eq, vgpr("ValuC+%u"%sumIdxV), sgpr(tmpS01))) |
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
if not interleaveStoreVmcnt: |
| 1278 |
module.add(SWaitCnt(vlcnt=0, vscnt=0, comment="wait C")) |
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
module.addComment1("apply mask, calc new C and issue writes") |
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
if kernel["ProblemType"]["DestDataType"].isBFloat16() and kernel["ProblemType"]["HighPrecisionAccumulate"]: |
| 1297 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprBf16Mask), "0xffff0000", comment="mask for pack two bfloat16 element to 32bit" )) |
| 1298 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprFp32Nan), "0x7fff0000", comment="fp32 Nan" )) |
| 1299 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprBf16Inc), "0x7fff", comment="rounding bias for bfloat16" )) |
| 1300 |
elif kernel["ProblemType"]["DestDataType"].isFloat8() and kernel["ProblemType"]["HighPrecisionAccumulate"]: |
| 1301 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprFp8NanInf), "0x207", comment="Nan and +/- inf" )) |
| 1302 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprFp8Max), "0x43E00000", comment="OCP Fp8 Max value 448 as float32" )) |
| 1303 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprFp8Min), "0xc3E00000", comment="OCP Fp8 Min value -448 as float32" )) |
| 1304 |
elif kernel["ProblemType"]["DestDataType"].isFloat8_fnuz() and kernel["ProblemType"]["HighPrecisionAccumulate"]: |
| 1305 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprFp8NanInf), "0x207", comment="Nan and +/- inf" )) |
| 1306 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprFp8Max), "0x43700000", comment="Fp8 Max value 240 as float32" )) |
| 1307 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprFp8Min), "0xc3700000", comment="Fp8 Min value -240 as float32" )) |
| 1308 |
elif kernel["ProblemType"]["DestDataType"].isAnyBFloat8() and kernel["ProblemType"]["HighPrecisionAccumulate"]: |
| 1309 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprBF8NanInf), "0x207", comment="Nan and +/- inf" )) |
| 1310 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprBF8Max), "0x47600000", comment="BF8 Max value 57344 as float32" )) |
| 1311 |
module.add(VMovB32(vgpr(cvtVgprStruct.vgprBF8Min), "0xc7600000", comment="BF8 Min value -57344 as float32" )) |
| 1312 |
|
| 1313 |
for elementIdx in range(0, len(batchElements)): |
| 1314 |
element = batchElements[elementIdx] |
| 1315 |
addr = ss.elementAddr[elementIdx].addrDVgpr |
| 1316 |
mask = ss.elementMask[elementIdx] |
| 1317 |
addrCalc = ss.elementAddr[elementIdx] |
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
vc0 = element[3] |
| 1322 |
sumIdx = ss.elementSumIdx[elementIdx] |
| 1323 |
|
| 1324 |
|
| 1325 |
if edge and not kernel["BufferStore"]: |
| 1326 |
module.add(writer.getEdgeMovInstType()(EXEC(), sgpr(mask, writer.states.laneSGPRC), "sgprs -> exec")) |
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
if interleaveStoreVmcnt: |
| 1335 |
vlcnt = loadsIssued - elementIdx - 1 |
| 1336 |
|
| 1337 |
if writer.states.asmCaps["SeparateVscnt"] or writer.states.asmCaps["SeparateVMcnt"]: |
| 1338 |
vscnt = -1 |
| 1339 |
vmComment = "{} = {} - {} - 1".format(vlcnt, loadsIssued, elementIdx) |
| 1340 |
else: |
| 1341 |
waitStoreCnt = storesIssued if not kernel["GroupLoadStore"] else 0 |
| 1342 |
vscnt = waitStoreCnt |
| 1343 |
vmComment = "{} = {} - {} + {} - 1".format(vlcnt, loadsIssued, elementIdx, waitStoreCnt) |
| 1344 |
|
| 1345 |
|
| 1346 |
module.addSpaceLine() |
| 1347 |
|
| 1348 |
module.add(SWaitCnt(vlcnt=vlcnt, vscnt=vscnt, comment="wait C (interleaved) {}".format(vmComment))) |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
for vi in range(0, gwvw): |
| 1356 |
dataV = ss.elementData[elementIdx] + int(vi*ss.cfg.numVgprsPerDataPerVI) |
| 1357 |
sumIdxV = ss.elementSumIdx[elementIdx] + vi |
| 1358 |
if kernel["ProblemType"]["ComputeDataType"].isHalf(): |
| 1359 |
if not kernel["ProblemType"]["HighPrecisionAccumulate"]: |
| 1360 |
if writer.states.asmCaps["HasWMMA"] and kernel["EnableMatrixInstructionStore"]: |
| 1361 |
dataV = ss.elementData[elementIdx] + int(vi / 2 * ss.cfg.numVgprsPerDataPerVI) |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
if (vi % 2) != 0: |
| 1367 |
module.add(VLShiftRightB32(dst=vgpr(dataV), shiftHex=16, src=vgpr(dataV), \ |
| 1368 |
comment="shift 16bit to get next half of packed ValueC")) |
| 1369 |
|
| 1370 |
module.add(VAddPKF16(dst=vgpr("ValuC+%u"%(sumIdxV)), src0=vgpr(dataV), src1=vgpr("ValuC+%u"%(sumIdxV)), \ |
| 1371 |
comment="sum*alpha + C*beta")) |
| 1372 |
elif sumIdxV%2==0 or (not ss.cfg.halfDataRegPerVI and gwvw==1): |
| 1373 |
newSumIdxV = sumIdxV // 2 - writer.states.c.startVgprValu |
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
module.add(VAddPKF16(dst=vgpr("ValuC+%u"%(newSumIdxV)), src0=vgpr(dataV), src1=vgpr("ValuC+%u"%(newSumIdxV)), \ |
| 1379 |
comment="sum*alpha + C*beta")) |
| 1380 |
else: |
| 1381 |
pass |
| 1382 |
else: |
| 1383 |
newSumIdxV = sumIdxV - writer.states.c.startVgprValu |
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
dataCExternal = ss.elementData[elementIdx] + vi//2 |
| 1389 |
hi16 = (vi + gwvw*vc0) % 2 |
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
module.add(writer.states.mixinst(dst=vgpr("ValuC+%u"%newSumIdxV), src0=1, \ |
| 1397 |
src1=vgpr(dataCExternal), src2=vgpr("ValuC+%u"%newSumIdxV), \ |
| 1398 |
vop3=VOP3PModifiers(op_sel=[0,hi16,0], op_sel_hi=[0,1,0]), |
| 1399 |
comment="//C*=beta")) |
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
elif kernel["ProblemType"]["ComputeDataType"].isBFloat16(): |
| 1406 |
if kernel["ProblemType"]["HighPrecisionAccumulate"]: |
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
dataCExternal = ss.elementData[elementIdx] + vi//2 |
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
module.add(VCvtBF16toFP32(dst=vgpr(tmpVgpr), src=vgpr(dataCExternal), vgprMask=vgpr(cvtVgprStruct.vgprBf16Mask), vi=(vi))) |
| 1417 |
newSumIdxV = sumIdxV - writer.states.c.startVgprValu |
| 1418 |
module.add(VAddF32(dst=vgpr("ValuC+%u"%sumIdxV), src0=vgpr("ValuC+%u"%sumIdxV), src1=vgpr(tmpVgpr), comment="accum partials")) |
| 1419 |
|
| 1420 |
elif kernel["ProblemType"]["ComputeDataType"].isSingle(): |
| 1421 |
if kernel["ProblemType"]["DataType"].isInt8(): |
| 1422 |
newSumIdxV = sumIdxV - writer.states.c.startVgprValu |
| 1423 |
module.add(VAddU32(dst=vgpr("ValuC+%u"%newSumIdxV), src0=vgpr(dataV+0), src1=vgpr("ValuC+%u"%newSumIdxV), comment="accum partials")) |
| 1424 |
else: |
| 1425 |
newSumIdxV = sumIdxV - writer.states.c.startVgprValu |
| 1426 |
module.add(VAddF32(dst=vgpr("ValuC+%u"%newSumIdxV), src0=vgpr("ValuC+%u"%newSumIdxV), src1=vgpr(dataV+0), comment="accum partials")) |
| 1427 |
|
| 1428 |
elif kernel["ProblemType"]["ComputeDataType"].isInt32(): |
| 1429 |
newSumIdxV = sumIdxV - writer.states.c.startVgprValu |
| 1430 |
|
| 1431 |
|
| 1432 |
module.add(VAddU32(dst=vgpr("ValuC+%u"%newSumIdxV), src0=vgpr(dataV+0), src1=vgpr("ValuC+%u"%newSumIdxV), comment="accum partials")) |
| 1433 |
|
| 1434 |
elif kernel["ProblemType"]["ComputeDataType"].isDouble(): |
| 1435 |
newSumIdxV = sumIdxV * 2 - writer.states.c.startVgprValu |
| 1436 |
|
| 1437 |
module.add(VAddF64(dst=vgpr("ValuC+%u"%(newSumIdxV),2), src0=vgpr("ValuC+%u"%(newSumIdxV),2), src1=vgpr(dataV+0,2), comment="accum partials")) |
| 1438 |
|
| 1439 |
|
| 1440 |
elif kernel["ProblemType"]["ComputeDataType"].isSingleComplex(): |
| 1441 |
newSumIdxV = sumIdxV * 2 - writer.states.c.startVgprValu |
| 1442 |
module.add(VAddF32(dst=vgpr("ValuC+%u"%(newSumIdxV)), src0=vgpr("ValuC+%u"%(newSumIdxV)), src1=vgpr(dataV+0), comment="accum partials real")) |
| 1443 |
module.add(VAddF32(dst=vgpr("ValuC+%u"%(newSumIdxV+1)), src0=vgpr("ValuC+%u"%(newSumIdxV+1)), src1=vgpr(dataV+1), comment="accum partials imag")) |
| 1444 |
|
| 1445 |
|
| 1446 |
elif kernel["ProblemType"]["ComputeDataType"].isDoubleComplex(): |
| 1447 |
newSumIdxV = sumIdxV * 4 - writer.states.c.startVgprValu |
| 1448 |
module.add(VAddF64(dst=vgpr("ValuC+%u"%(newSumIdxV+0),2), src0=vgpr("ValuC+%u"%(newSumIdxV+0),2), src1=vgpr(dataV+0,2), comment="accum partials real")) |
| 1449 |
module.add(VAddF64(dst=vgpr("ValuC+%u"%(newSumIdxV+2),2), src0=vgpr("ValuC+%u"%(newSumIdxV+2),2), src1=vgpr(dataV+2,2), comment="accum partials imag")) |
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
if codeAccVgprWrite is not None and kernel["LocalSplitU"] == 1: |
| 1456 |
regsPerScalar = writer.states.bpeCinternal // writer.states.bpr |
| 1457 |
|
| 1458 |
for elementIdx in range(0, len(batchElements)): |
| 1459 |
|
| 1460 |
for vi in range(0, gwvw): |
| 1461 |
|
| 1462 |
for rIdx in range(0, regsPerScalar): |
| 1463 |
module.add(replaceHolder(codeAccVgprWrite.popFirstItem(), ss.elementSumIdx[elementIdx]*regsPerScalar + regsPerScalar*vi + rIdx - writer.states.c.startVgprValu)) |
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
if not kernel["MIArchVgpr"]: |
| 1471 |
module.add(SNop(1, "2 wait states required before reading vgpr")) |
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
if edge and (not kernel["BufferStore"]): |
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
module.add(self.getEdgeMovInstType()(EXEC(), -1, "full mask -> exec")) |
| 1526 |
|
| 1527 |
if writer.db["ConservativeWaitCnt"] & 0x40: |
| 1528 |
module.add(SBarrier("debug")) |
| 1529 |
module.add(SWaitCnt(vlcnt=0, vscnt=0, comment="ConservativeWaitCnt")) |
| 1530 |
module.add(SBarrier("debug")) |
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
lastData = -1 |
| 1537 |
for elementIdx in range(0, len(batchElements)): |
| 1538 |
if not ss.sharedColDVgprs: |
| 1539 |
addrCalc: AddrCalculation = ss.elementAddres[elementIdx] |
| 1540 |
addrDVgpr = addrCalc.addrDVgpr |
| 1541 |
addrCVgpr = addrCalc.addrCVgpr |
| 1542 |
writer.vgprPool.checkIn(addrDVgpr) |
| 1543 |
if addrCVgpr != addrDVgpr: |
| 1544 |
writer.vgprPool.checkIn(addrCVgpr) |
| 1545 |
|
| 1546 |
data = ss.elementData[elementIdx] |
| 1547 |
if data != 0: |
| 1548 |
if data != lastData: |
| 1549 |
writer.vgprPool.checkIn(data) |
| 1550 |
lastData = data |
| 1551 |
|
| 1552 |
ss.firstBatch = False |
| 1553 |
ss.checkInTempVgprC() |
| 1554 |
|
| 1555 |
if writer.states.serializedStore: |
| 1556 |
module.add(SNop(0, "1 wait state required when next inst writes vgprs held by previous dwordx4 store inst")) |
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
return module |
| 1565 |
|
| 1566 |
class StreamKOff(StreamK): |
| 1567 |
kernel = {"StreamK": 0} |
| 1568 |
|
| 1569 |
def preLoop(self, writer, kernel): |
| 1570 |
module = Module("StreamK Off openLoop") |
| 1571 |
return module |
| 1572 |
|
| 1573 |
def graWorkGroup(self, writer, kernel, tPA, tPB): |
| 1574 |
module = Module("StreamK Off graWorkGroup") |
| 1575 |
|
| 1576 |
if writer.states.archCaps["WorkGroupIdFromTTM"]: |
| 1577 |
module.add(SMovB32(dst=sgpr("WorkGroup0"), src="ttmp9", comment="workaround")) |
| 1578 |
module.add(SAndB32(dst=sgpr("WorkGroup1"), src0=hex(0xFFFF), src1="ttmp7", comment="workaround")) |
| 1579 |
module.add(SLShiftRightB32(dst=sgpr("WorkGroup2"), shiftHex=hex(0x10), src="ttmp7")) |
| 1580 |
|
| 1581 |
return module |
| 1582 |
|
| 1583 |
def computeLoadSrd(self, writer, kernel, tc, sTmp): |
| 1584 |
module = Module("StreamK Off computeLoadSrd") |
| 1585 |
return module |
| 1586 |
|
| 1587 |
def computeStoreSrdStart(self, writer, kernel): |
| 1588 |
module = Module("StreamK Off computeStoreSrdStart") |
| 1589 |
return module |
| 1590 |
|
| 1591 |
def graAddresses(self, writer, kernel, tP, vTmp): |
| 1592 |
module = Module("StreamK Off graAddresses") |
| 1593 |
|
| 1594 |
tc = tP["tensorChar"] |
| 1595 |
module.add(VMovB32(dst=vgpr(vTmp+0), src=sgpr("Address%s+0" % tc))) |
| 1596 |
module.add(VMovB32(dst=vgpr(vTmp+1), src=sgpr("Address%s+1" % tc))) |
| 1597 |
|
| 1598 |
return module |
| 1599 |
|
| 1600 |
def declareStaggerParms(self, writer, kernel): |
| 1601 |
module = Module("StreamK Off declareStaggerParms") |
| 1602 |
return module |
| 1603 |
|
| 1604 |
def tailLoopNumIter(self, writer, kernel, loopCounter): |
| 1605 |
module = Module("StreamK Off tailLoopNumIter") |
| 1606 |
return module |
| 1607 |
|
| 1608 |
def calculateLoopNumIter(self, writer, kernel, loopCounterName, loopIdx, tmpSgprInfo): |
| 1609 |
module = Module("StreamK Off calculateLoopNumIter") |
| 1610 |
|
| 1611 |
quotient = loopCounterName |
| 1612 |
dividend = "SizesSum+%u" % loopIdx |
| 1613 |
divisor = kernel["DepthU"] |
| 1614 |
|
| 1615 |
if kernel["NoTailLoop"] and kernel["AssertSummationElementMultiple"] % kernel["DepthU"] != 0: |
| 1616 |
|
| 1617 |
module.add(SAddI32(dst=sgpr(quotient), src0=(divisor - 1), src1=sgpr(dividend), comment="round up SizeSum / DepthU" )) |
| 1618 |
module.add(scalarStaticDivideAndRemainder(qReg=quotient, rReg=-1, dReg=quotient, divisor=divisor, tmpSgprRes=tmpSgprInfo, doRemainder=False)) |
| 1619 |
else: |
| 1620 |
module.add(scalarStaticDivideAndRemainder(qReg=quotient, rReg=-1, dReg=dividend, divisor=divisor, tmpSgprRes=tmpSgprInfo, doRemainder=False)) |
| 1621 |
|
| 1622 |
return module |
| 1623 |
|
| 1624 |
def storeBranches(self, writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct): |
| 1625 |
module = Module("StreamK Off storeBranches") |
| 1626 |
return module |
| 1627 |
|
| 1628 |
def writePartials(self, writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct, endLabel): |
| 1629 |
module = Module("StreamK Off writePartials") |
| 1630 |
return module |
| 1631 |
|
| 1632 |
class StreamKBasic(StreamK): |
| 1633 |
kernel = {"StreamK": 1} |
| 1634 |
|
| 1635 |
def preLoop(self, writer, kernel): |
| 1636 |
module = Module("StreamK Basic openLoop") |
| 1637 |
|
| 1638 |
xccMapping = Component.XCCMapping.find(writer) |
| 1639 |
module.add(xccMapping(writer, kernel)) |
| 1640 |
|
| 1641 |
module.add(SMovB32(dst=sgpr("StreamKIdx"), src=sgpr("WorkGroup0"), comment="Save original StreamK index")) |
| 1642 |
|
| 1643 |
module.add(SMulI32(dst=sgpr("StreamKIter"), src0=sgpr("StreamKIdx"), src1=sgpr("SKItersPerWG"), comment="StreamK starting iteration")) |
| 1644 |
module.add(SAddU32(dst=sgpr("StreamKIterEnd"), src0=sgpr("StreamKIter"), src1=sgpr("SKItersPerWG"), comment="StreamK ending iteration")) |
| 1645 |
module.add(SMinU32(dst=sgpr("StreamKIterEnd"), src0=sgpr("StreamKIterEnd"), src1=sgpr("TotalIters"), comment="Cap ending iter at total iters")) |
| 1646 |
module.add(SCmpLtU32(src0=sgpr("StreamKIter"), src1=sgpr("StreamKIterEnd"), comment="Make sure there's work to do")) |
| 1647 |
module.add(writer.longBranchScc0(Label("KernelEnd", ""), posNeg=1)) |
| 1648 |
module.add(writer.undefineSgpr("TotalIters")) |
| 1649 |
|
| 1650 |
return module |
| 1651 |
|
| 1652 |
def graWorkGroup(self, writer, kernel, tPA, tPB): |
| 1653 |
module = Module("StreamK Basic graWorkGroup") |
| 1654 |
|
| 1655 |
|
| 1656 |
sTmp = writer.sgprPool.checkOutAligned(4, 2, "SKMappingTemp", preventOverflow=False) |
| 1657 |
|
| 1658 |
module.add(self.skTileIndex(writer, kernel, sTmp, tPA, tPB)) |
| 1659 |
|
| 1660 |
|
| 1661 |
module.add(SMovB32(dst=sgpr("StreamKIter"), src=sgpr(sTmp+2), comment="Increment StreamK Iteration")) |
| 1662 |
|
| 1663 |
module.add(self.skIndexToWG(writer, kernel, sTmp)) |
| 1664 |
|
| 1665 |
writer.sgprPool.checkIn(sTmp) |
| 1666 |
|
| 1667 |
return module |
| 1668 |
|
| 1669 |
def computeLoadSrd(self, writer, kernel, tc, sTmp): |
| 1670 |
module = Module("StreamK Basic computeLoadSrd") |
| 1671 |
module.add(self.computeLoadSrdCommon(writer, kernel, tc, sTmp)) |
| 1672 |
return module |
| 1673 |
|
| 1674 |
def computeStoreSrdStart(self, writer, kernel): |
| 1675 |
module = Module("StreamK Basic computeStoreSrdStart") |
| 1676 |
|
| 1677 |
|
| 1678 |
return module |
| 1679 |
|
| 1680 |
def graAddresses(self, writer, kernel, tP, vTmp): |
| 1681 |
module = Module("StreamK Basic graAddresses") |
| 1682 |
module.add(self.graAddressesCommon(writer, kernel, tP, vTmp)) |
| 1683 |
return module |
| 1684 |
|
| 1685 |
def declareStaggerParms(self, writer, kernel): |
| 1686 |
module = Module("StreamK Basic declareStaggerParms") |
| 1687 |
module.add(self.declareStaggerParmsCommon(writer, kernel)) |
| 1688 |
return module |
| 1689 |
|
| 1690 |
def tailLoopNumIter(self, writer, kernel, loopCounter): |
| 1691 |
module = Module("StreamK Basic tailLoopNumIter") |
| 1692 |
module.add(self.tailLoopNumIterCommon(writer, kernel, loopCounter)) |
| 1693 |
return module |
| 1694 |
|
| 1695 |
def calculateLoopNumIter(self, writer, kernel, loopCounterName, loopIdx, tmpSgprInfo): |
| 1696 |
module = Module("StreamK Basic calculateLoopNumIter") |
| 1697 |
module.add(self.calculateLoopNumIterCommon(writer, kernel, loopCounterName, loopIdx, tmpSgprInfo)) |
| 1698 |
return module |
| 1699 |
|
| 1700 |
def storeBranches(self, writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct): |
| 1701 |
module = Module("StreamK Basic storeBranches") |
| 1702 |
module.add(self.storeBranchesCommon(writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct)) |
| 1703 |
return module |
| 1704 |
|
| 1705 |
def writePartials(self, writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct, endLabel): |
| 1706 |
module = Module("StreamK Basic writePartials") |
| 1707 |
module.add(self.writePartialsCommon(writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct, endLabel)) |
| 1708 |
return module |
| 1709 |
|
| 1710 |
class StreamKTwoTileOriginal(StreamK): |
| 1711 |
kernel = {"StreamK": 2} |
| 1712 |
|
| 1713 |
def preLoop(self, writer, kernel): |
| 1714 |
module = Module("StreamK TwoTileOriginal openLoop") |
| 1715 |
|
| 1716 |
xccMapping = Component.XCCMapping.find(writer) |
| 1717 |
module.add(xccMapping(writer, kernel)) |
| 1718 |
|
| 1719 |
module.add(SMovB32(dst=sgpr("StreamKIdx"), src=sgpr("WorkGroup0"), comment="Save original StreamK index")) |
| 1720 |
|
| 1721 |
|
| 1722 |
sSkExtraIters = writer.sgprPool.checkOut(1, "extraIters", preventOverflow=False) |
| 1723 |
sIter = writer.sgprPool.checkOut(2, "SKIter", preventOverflow=False) |
| 1724 |
module.add(self.skExtraIters(writer, kernel, sSkExtraIters, sIter)) |
| 1725 |
module.add(SMulI32(dst=sgpr("StreamKIter"), src0=sgpr("StreamKIdx"), src1=sgpr("SKItersPerWG"), comment="StreamK starting iteration (case: after extra iters)")) |
| 1726 |
module.add(SAddU32(dst=sgpr("StreamKIter"), src0=sgpr("StreamKIter"), src1=sgpr(sSkExtraIters), comment="Add extra iters")) |
| 1727 |
module.add(SAddU32(dst=sgpr("StreamKIterEnd"), src0=sgpr("StreamKIter"), src1=sgpr("SKItersPerWG"), comment="StreamK ending iteration (case: after extra iters)")) |
| 1728 |
|
| 1729 |
|
| 1730 |
module.add(SAddU32(dst=sgpr(sIter+1), src0=sgpr("SKItersPerWG"), src1=1, comment="Spread out extra iterations")) |
| 1731 |
module.add(SMulI32(dst=sgpr(sIter), src0=sgpr("StreamKIdx"), src1=sgpr(sIter+1), comment="StreamK starting iteration (case: before extra iters)")) |
| 1732 |
module.add(SAddU32(dst=sgpr(sIter+1), src0=sgpr(sIter), src1=sgpr(sIter+1), comment="StreamK ending iteration (case: before extra iters)")) |
| 1733 |
|
| 1734 |
module.add(SCmpLtU32(src0=sgpr("StreamKIdx"), src1=sgpr(sSkExtraIters), comment="Check if lane gets an extra iteration")) |
| 1735 |
module.add(SCSelectB32(dst=sgpr("StreamKIter"), src0=sgpr(sIter), src1=sgpr("StreamKIter"), comment="Set start iter")) |
| 1736 |
module.add(SCSelectB32(dst=sgpr("StreamKIterEnd"), src0=sgpr(sIter+1), src1=sgpr("StreamKIterEnd"), comment="Set end iter")) |
| 1737 |
writer.sgprPool.checkIn(sSkExtraIters) |
| 1738 |
writer.sgprPool.checkIn(sIter) |
| 1739 |
|
| 1740 |
|
| 1741 |
sTmp = writer.sgprPool.checkOut(1, "TotalSKIters", preventOverflow=False) |
| 1742 |
module.add(SMulI32(dst=sgpr(sTmp), src0=sgpr("skTiles"), src1=sgpr("ItersPerTile"), comment="Total SK iters")) |
| 1743 |
module.add(SMinU32(dst=sgpr("StreamKIterEnd"), src0=sgpr("StreamKIterEnd"), src1=sgpr(sTmp), comment="Cap ending iter at total SK iters")) |
| 1744 |
writer.sgprPool.checkIn(sTmp) |
| 1745 |
|
| 1746 |
module.add(SCmpLtU32(src0=sgpr("StreamKIter"), src1=sgpr("TotalIters"), comment="Make sure there's work to do")) |
| 1747 |
module.add(writer.longBranchScc0(Label("KernelEnd", ""), posNeg=1)) |
| 1748 |
|
| 1749 |
return module |
| 1750 |
|
| 1751 |
def graWorkGroup(self, writer, kernel, tPA, tPB): |
| 1752 |
module = Module("StreamK TwoTileOriginal graWorkGroup") |
| 1753 |
|
| 1754 |
|
| 1755 |
sTmp = writer.sgprPool.checkOutAligned(4, 2, "SKMappingTemp", preventOverflow=False) |
| 1756 |
|
| 1757 |
module.add(self.skTileIndex(writer, kernel, sTmp, tPA, tPB)) |
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
module.add(SSubU32(dst=sgpr(sTmp+3), src0=sgpr(sTmp+2), src1=sgpr(sTmp+1), comment="Local iteration end (DP tile)")) |
| 1762 |
|
| 1763 |
module.add(SCmpLtU32(src0=sgpr("StreamKIter"), src1=sgpr("StreamKIterEnd"), comment="Check if in SK or DP section")) |
| 1764 |
module.add(SCSelectB32(dst=sgpr("StreamKLocalEnd"), src0=sgpr("StreamKLocalEnd"), src1=sgpr(sTmp+3), comment="Apply SK or DP end iteration")) |
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
module.add(SMulI32(dst=sgpr(sTmp+3), src0=sgpr("skTiles"), src1=sgpr("ItersPerTile"), comment="Offset to first DP tile")) |
| 1770 |
module.add(SMulI32(dst=sgpr(sTmp+1), src0=sgpr("StreamKIdx"), src1=sgpr("ItersPerTile"), comment="WG tile offset")) |
| 1771 |
module.add(SAddU32(dst=sgpr(sTmp+3), src0=sgpr(sTmp+3), src1=sgpr(sTmp+1), comment="DP start offset + WG offset")) |
| 1772 |
|
| 1773 |
module.add(SMulI32(dst=sgpr(sTmp+1), src0=sgpr("skGrid"), src1=sgpr("ItersPerTile"), comment="DP iterations shift")) |
| 1774 |
module.add(SAddU32(dst=sgpr(sTmp+1), src0=sgpr(sTmp+1), src1=sgpr("StreamKIter"), comment="Add DP shift")) |
| 1775 |
|
| 1776 |
module.add(SCmpLtU32(src0=sgpr("StreamKIter"), src1=sgpr("StreamKIterEnd"), comment="Check if in SK or DP section")) |
| 1777 |
module.add(SCSelectB32(dst=sgpr(sTmp+3), src0=sgpr(sTmp+3), src1=sgpr(sTmp+1), comment="Select first DP tile, or add DP shift")) |
| 1778 |
|
| 1779 |
module.add(SCmpLtU32(src0=sgpr(sTmp+2), src1=sgpr("StreamKIterEnd"), comment="Check if there are more SK tiles")) |
| 1780 |
module.add(SCSelectB32(dst=sgpr("StreamKIter"), src0=sgpr(sTmp+2), src1=sgpr(sTmp+3), comment="Select next SK or DP tile")) |
| 1781 |
|
| 1782 |
module.add(self.skIndexToWG(writer, kernel, sTmp)) |
| 1783 |
|
| 1784 |
writer.sgprPool.checkIn(sTmp) |
| 1785 |
|
| 1786 |
return module |
| 1787 |
|
| 1788 |
def computeLoadSrd(self, writer, kernel, tc, sTmp): |
| 1789 |
module = Module("StreamK TwoTileOriginal computeLoadSrd") |
| 1790 |
module.add(self.computeLoadSrdCommon(writer, kernel, tc, sTmp)) |
| 1791 |
return module |
| 1792 |
|
| 1793 |
def computeStoreSrdStart(self, writer, kernel): |
| 1794 |
module = Module("StreamK TwoTileOriginal computeStoreSrdStart") |
| 1795 |
module.add(self.computeStoreSrdStartCommon(writer, kernel)) |
| 1796 |
return module |
| 1797 |
|
| 1798 |
def graAddresses(self, writer, kernel, tP, vTmp): |
| 1799 |
module = Module("StreamK TwoTileOriginal graAddresses") |
| 1800 |
module.add(self.graAddressesCommon(writer, kernel, tP, vTmp)) |
| 1801 |
return module |
| 1802 |
|
| 1803 |
def declareStaggerParms(self, writer, kernel): |
| 1804 |
module = Module("StreamK TwoTileOriginal declareStaggerParms") |
| 1805 |
module.add(self.declareStaggerParmsCommon(writer, kernel)) |
| 1806 |
return module |
| 1807 |
|
| 1808 |
def tailLoopNumIter(self, writer, kernel, loopCounter): |
| 1809 |
module = Module("StreamK TwoTileOriginal tailLoopNumIter") |
| 1810 |
module.add(self.tailLoopNumIterCommon(writer, kernel, loopCounter)) |
| 1811 |
return module |
| 1812 |
|
| 1813 |
def calculateLoopNumIter(self, writer, kernel, loopCounterName, loopIdx, tmpSgprInfo): |
| 1814 |
module = Module("StreamK TwoTileOriginal calculateLoopNumIter") |
| 1815 |
module.add(self.calculateLoopNumIterCommon(writer, kernel, loopCounterName, loopIdx, tmpSgprInfo)) |
| 1816 |
return module |
| 1817 |
|
| 1818 |
def storeBranches(self, writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct): |
| 1819 |
module = Module("StreamK TwoTileOriginal storeBranches") |
| 1820 |
module.add(self.storeBranchesCommon(writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct)) |
| 1821 |
return module |
| 1822 |
|
| 1823 |
def writePartials(self, writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct, endLabel): |
| 1824 |
module = Module("StreamK TwoTileOriginal writePartials") |
| 1825 |
module.add(self.writePartialsCommon(writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct, endLabel)) |
| 1826 |
return module |
| 1827 |
|
| 1828 |
|
| 1829 |
class StreamKTwoTileDPFirst(StreamK): |
| 1830 |
kernel = {"StreamK": 3} |
| 1831 |
|
| 1832 |
def preLoop(self, writer, kernel): |
| 1833 |
module = Module("StreamK TwoTileDPFirst openLoop") |
| 1834 |
|
| 1835 |
xccMapping = Component.XCCMapping.find(writer) |
| 1836 |
module.add(xccMapping(writer, kernel)) |
| 1837 |
|
| 1838 |
module.add(SMovB32(dst=sgpr("StreamKIdx"), src=sgpr("WorkGroup0"), comment="Save original StreamK index")) |
| 1839 |
|
| 1840 |
|
| 1841 |
skInitDone = Label("SK_InitDone", "") |
| 1842 |
|
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
skSplitInit = Label("SK_SplitInit", "") |
| 1847 |
module.add(SCmpEQU64(src0=sgpr("AddressFlags", 2), src1=hex(0), comment="Check for synchronizer")) |
| 1848 |
module.add(SCBranchSCC0(labelName=skSplitInit.getLabelName(), comment="Jump to single kernel init")) |
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
stmpTileIdx = writer.sgprPool.checkOut(1, "TileIdx", preventOverflow=False) |
| 1857 |
stmpPartialIdx = writer.sgprPool.checkOut(1, "PartialIdx", preventOverflow=False) |
| 1858 |
tmpVgpr = writer.vgprPool.checkOut(2, "div") |
| 1859 |
tmpVgprRes = ContinuousRegister(idx=tmpVgpr, size=2) |
| 1860 |
module.add(scalarUInt32DivideAndRemainder(qReg=stmpTileIdx, dReg="StreamKIdx", divReg="SkSplit", rReg=stmpPartialIdx, tmpVgprRes=tmpVgprRes, wavewidth=kernel["WavefrontSize"], doRemainder=True, comment="TileIdx = SKIdx // WGsPerTile, PartialIdx = SKIdx % WGsPerTile")) |
| 1861 |
tmpVgprRes = None |
| 1862 |
writer.vgprPool.checkIn(tmpVgpr) |
| 1863 |
|
| 1864 |
|
| 1865 |
skHasExtraLabel = Label("SK_HasExtra", "") |
| 1866 |
skDoneExtraLabel = Label("SK_DoneExtra", "") |
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
sSkExtraIters = writer.sgprPool.checkOut(1, "extraIters", preventOverflow=False) |
| 1871 |
module.add(SMulI32(dst=sgpr(sSkExtraIters), src0=sgpr("SkSplit"), src1=sgpr("SKItersPerWG"))) |
| 1872 |
module.add(SSubU32(dst=sgpr(sSkExtraIters), src0=sgpr("ItersPerTile"), src1=sgpr(sSkExtraIters), comment="extraIters = itersPerTile - SkSplit * skItersPerWG")) |
| 1873 |
|
| 1874 |
module.add(SMulI32(dst=sgpr("StreamKIter"), src0=sgpr(stmpPartialIdx), src1=sgpr("SKItersPerWG"), comment="StreamK starting iteration (case: after extra iters)")) |
| 1875 |
module.add(SCmpLtU32(src0=sgpr(stmpPartialIdx), src1=sgpr(sSkExtraIters), comment="Check if WG gets an extra iteration")) |
| 1876 |
module.add(SCBranchSCC1(labelName=skHasExtraLabel.getLabelName(), comment="Has extra iter")) |
| 1877 |
|
| 1878 |
module.add(SAddU32(dst=sgpr("StreamKIter"), src0=sgpr("StreamKIter"), src1=sgpr(sSkExtraIters), comment="This WG does not have an extra iteration")) |
| 1879 |
module.add(SAddU32(dst=sgpr("StreamKIterEnd"), src0=sgpr("StreamKIter"), src1=sgpr("SKItersPerWG"), comment="StreamK ending iteration (case: after extra iters)")) |
| 1880 |
module.add(SBranch(labelName=skDoneExtraLabel.getLabelName(), comment="Done init for parallel reduction")) |
| 1881 |
|
| 1882 |
module.add(skHasExtraLabel) |
| 1883 |
module.add(SAddU32(dst=sgpr("StreamKIter"), src0=sgpr("StreamKIter"), src1=sgpr(stmpPartialIdx), comment="This WG has an extra iteration")) |
| 1884 |
module.add(SAddU32(dst=sgpr("StreamKIterEnd"), src0=sgpr("StreamKIter"), src1=sgpr("SKItersPerWG"), comment="StreamK ending iteration (case: after extra iters)")) |
| 1885 |
module.add(SAddU32(dst=sgpr("StreamKIterEnd"), src0=sgpr("StreamKIterEnd"), src1=1, comment="StreamK ending iteration (case: after extra iters)")) |
| 1886 |
module.add(skDoneExtraLabel) |
| 1887 |
|
| 1888 |
module.add(SMulI32(dst=sgpr(stmpTileIdx), src0=sgpr(stmpTileIdx), src1=sgpr("ItersPerTile"), comment="Tile offset = tilesIdx * itersPerTile")) |
| 1889 |
module.add(SAddU32(dst=sgpr("StreamKIter"), src0=sgpr("StreamKIter"), src1=sgpr(stmpTileIdx), comment="Offset to correct tile")) |
| 1890 |
module.add(SAddU32(dst=sgpr("StreamKIterEnd"), src0=sgpr("StreamKIterEnd"), src1=sgpr(stmpTileIdx), comment="Offset to correct tile")) |
| 1891 |
|
| 1892 |
module.add(SMovB32(dst=sgpr("SkPartialIdx"), src=sgpr(stmpPartialIdx), comment="Save partial idx for SrdD calculation")) |
| 1893 |
|
| 1894 |
module.add(SBranch(labelName=skInitDone.getLabelName(), comment="Done init for parallel reduction")) |
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
|
| 1916 |
module.add(skSplitInit) |
| 1917 |
writer.sgprPool.checkIn(sSkExtraIters) |
| 1918 |
writer.sgprPool.checkIn(stmpPartialIdx) |
| 1919 |
writer.sgprPool.checkIn(stmpTileIdx) |
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
module.add(SMulI32(dst=sgpr("StreamKIter"), src0=sgpr("StreamKIdx"), src1=sgpr("ItersPerTile"), comment="DP starting iteration (case: DP work to do)")) |
| 1925 |
module.add(SMovB32(dst=sgpr("StreamKIterEnd"), src=sgpr("TotalIters"), comment="DP ending iteration (case: only DP work to do)")) |
| 1926 |
sTmp = writer.sgprPool.checkOut(1, "TotalSKIters", preventOverflow=False) |
| 1927 |
module.add(SMulI32(dst=sgpr(sTmp), src0=sgpr("skTiles"), src1=sgpr("ItersPerTile"), comment="Total SK iters")) |
| 1928 |
module.add(SCmpLtU32(src0=sgpr(sTmp), src1=sgpr("TotalIters"), comment="Check if there are DP tiles to do")) |
| 1929 |
module.add(SCBranchSCC1(labelName=skInitDone.getLabelName(), comment="Done init")) |
| 1930 |
writer.sgprPool.checkIn(sTmp) |
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
sSkExtraIters = writer.sgprPool.checkOut(1, "extraIters", preventOverflow=False) |
| 1935 |
sIter = writer.sgprPool.checkOut(2, "SKIter", preventOverflow=False) |
| 1936 |
module.add(self.skExtraIters(writer, kernel, sSkExtraIters, sIter)) |
| 1937 |
module.add(SMulI32(dst=sgpr("StreamKIter"), src0=sgpr("StreamKIdx"), src1=sgpr("SKItersPerWG"), comment="StreamK starting iteration (case: after extra iters)")) |
| 1938 |
module.add(SAddU32(dst=sgpr("StreamKIter"), src0=sgpr("StreamKIter"), src1=sgpr(sSkExtraIters), comment="Add extra iters")) |
| 1939 |
module.add(SAddU32(dst=sgpr("StreamKIterEnd"), src0=sgpr("StreamKIter"), src1=sgpr("SKItersPerWG"), comment="StreamK ending iteration (case: after extra iters)")) |
| 1940 |
|
| 1941 |
|
| 1942 |
module.add(SAddU32(dst=sgpr(sIter+1), src0=sgpr("SKItersPerWG"), src1=1, comment="Spread out extra iterations")) |
| 1943 |
module.add(SMulI32(dst=sgpr(sIter), src0=sgpr("StreamKIdx"), src1=sgpr(sIter+1), comment="StreamK starting iteration (case: before extra iters)")) |
| 1944 |
module.add(SAddU32(dst=sgpr(sIter+1), src0=sgpr(sIter), src1=sgpr(sIter+1), comment="StreamK ending iteration (case: before extra iters)")) |
| 1945 |
|
| 1946 |
module.add(SCmpLtU32(src0=sgpr("StreamKIdx"), src1=sgpr(sSkExtraIters), comment="Check if lane gets an extra iteration")) |
| 1947 |
module.add(SCSelectB32(dst=sgpr("StreamKIter"), src0=sgpr(sIter), src1=sgpr("StreamKIter"), comment="Set start iter")) |
| 1948 |
module.add(SCSelectB32(dst=sgpr("StreamKIterEnd"), src0=sgpr(sIter+1), src1=sgpr("StreamKIterEnd"), comment="Set end iter")) |
| 1949 |
writer.sgprPool.checkIn(sSkExtraIters) |
| 1950 |
writer.sgprPool.checkIn(sIter) |
| 1951 |
|
| 1952 |
|
| 1953 |
sTmp = writer.sgprPool.checkOut(1, "TotalSKIters", preventOverflow=False) |
| 1954 |
module.add(SMulI32(dst=sgpr(sTmp), src0=sgpr("skTiles"), src1=sgpr("ItersPerTile"), comment="Total SK iters")) |
| 1955 |
module.add(SMinU32(dst=sgpr("StreamKIterEnd"), src0=sgpr("StreamKIterEnd"), src1=sgpr(sTmp), comment="Cap ending iter at total SK iters")) |
| 1956 |
writer.sgprPool.checkIn(sTmp) |
| 1957 |
|
| 1958 |
module.add(skInitDone) |
| 1959 |
|
| 1960 |
module.add(SCmpLtU32(src0=sgpr("StreamKIter"), src1=sgpr("TotalIters"), comment="Make sure there's work to do")) |
| 1961 |
module.add(writer.longBranchScc0(Label("KernelEnd", ""), posNeg=1)) |
| 1962 |
|
| 1963 |
return module |
| 1964 |
|
| 1965 |
def graWorkGroup(self, writer, kernel, tPA, tPB): |
| 1966 |
module = Module("StreamK TwoTileDPFirst graWorkGroup") |
| 1967 |
|
| 1968 |
|
| 1969 |
sTmp = writer.sgprPool.checkOutAligned(4, 2, "SKMappingTemp", preventOverflow=False) |
| 1970 |
|
| 1971 |
module.add(self.skTileIndex(writer, kernel, sTmp, tPA, tPB)) |
| 1972 |
|
| 1973 |
skUpdateDone = Label("SK_UpdateDone", "") |
| 1974 |
|
| 1975 |
|
| 1976 |
skSplitUpdate = Label("SK_SplitUpdate", "") |
| 1977 |
module.add(SCmpEQU64(src0=sgpr("AddressFlags", 2), src1=hex(0), comment="Check for synchronizer")) |
| 1978 |
module.add(SCBranchSCC0(labelName=skSplitUpdate.getLabelName(), comment="Jump to single kernel update")) |
| 1979 |
|
| 1980 |
module.add(SMovB32(dst=sgpr(sTmp+1), src=sgpr("StreamKIterEnd"), comment="Parallel reduction, work contained to single partial tile")) |
| 1981 |
|
| 1982 |
module.add(SBranch(labelName=skUpdateDone.getLabelName(), comment="Done update for parallel reduction")) |
| 1983 |
module.add(skSplitUpdate) |
| 1984 |
|
| 1985 |
|
| 1986 |
module.add(SMulI32(dst=sgpr(sTmp+3), src0=sgpr("skTiles"), src1=sgpr("ItersPerTile"), comment="Total SK iters")) |
| 1987 |
module.add(SSubU32(dst=sgpr(sTmp+3), src0=sgpr("TotalIters"), src1=sgpr(sTmp+3), comment="Offset to first SK tile")) |
| 1988 |
|
| 1989 |
module.add(SMulI32(dst=sgpr(sTmp+1), src0=sgpr("skGrid"), src1=sgpr("ItersPerTile"), comment="DP iterations shift")) |
| 1990 |
module.add(SAddU32(dst=sgpr(sTmp+1), src0=sgpr(sTmp+1), src1=sgpr("StreamKIter"), comment="Add DP shift")) |
| 1991 |
|
| 1992 |
module.add(SCmpLtU32(src0=sgpr(sTmp+1), src1=sgpr(sTmp+3), comment="Check if still in DP section")) |
| 1993 |
module.add(SCBranchSCC1(labelName=skUpdateDone.getLabelName(), comment="Done update")) |
| 1994 |
|
| 1995 |
module.add(SMovB32(dst=sgpr(sTmp+1), src=sgpr(sTmp+2), comment="SK iterations shift")) |
| 1996 |
module.add(SCmpLeU32(src0=sgpr(sTmp+3), src1=sgpr("StreamKIter"), comment="Check if continuing in SK section")) |
| 1997 |
module.add(SCBranchSCC1(labelName=skUpdateDone.getLabelName(), comment="Done update")) |
| 1998 |
|
| 1999 |
|
| 2000 |
sSkExtraIters = writer.sgprPool.checkOut(1, "extraIters", preventOverflow=False) |
| 2001 |
sIter = writer.sgprPool.checkOut(2, "SKIter", preventOverflow=False) |
| 2002 |
module.add(self.skExtraIters(writer, kernel, sSkExtraIters, sIter)) |
| 2003 |
module.add(SMulI32(dst=sgpr("StreamKIter"), src0=sgpr("StreamKIdx"), src1=sgpr("SKItersPerWG"), comment="StreamK starting iteration (case: after extra iters)")) |
| 2004 |
module.add(SAddU32(dst=sgpr("StreamKIter"), src0=sgpr("StreamKIter"), src1=sgpr(sSkExtraIters), comment="Add extra iters")) |
| 2005 |
module.add(SAddU32(dst=sgpr("StreamKIterEnd"), src0=sgpr("StreamKIter"), src1=sgpr("SKItersPerWG"), comment="StreamK ending iteration (case: after extra iters)")) |
| 2006 |
|
| 2007 |
|
| 2008 |
module.add(SAddU32(dst=sgpr(sIter+1), src0=sgpr("SKItersPerWG"), src1=1, comment="Spread out extra iterations")) |
| 2009 |
module.add(SMulI32(dst=sgpr(sIter), src0=sgpr("StreamKIdx"), src1=sgpr(sIter+1), comment="StreamK starting iteration (case: before extra iters)")) |
| 2010 |
module.add(SAddU32(dst=sgpr(sIter+1), src0=sgpr(sIter), src1=sgpr(sIter+1), comment="StreamK ending iteration (case: before extra iters)")) |
| 2011 |
|
| 2012 |
module.add(SCmpLtU32(src0=sgpr("StreamKIdx"), src1=sgpr(sSkExtraIters), comment="Check if lane gets an extra iteration")) |
| 2013 |
writer.sgprPool.checkIn(sSkExtraIters) |
| 2014 |
module.add(SCSelectB32(dst=sgpr("StreamKIter"), src0=sgpr(sIter), src1=sgpr("StreamKIter"), comment="Set start iter")) |
| 2015 |
module.add(SCSelectB32(dst=sgpr("StreamKIterEnd"), src0=sgpr(sIter+1), src1=sgpr("StreamKIterEnd"), comment="Set end iter")) |
| 2016 |
writer.sgprPool.checkIn(sIter) |
| 2017 |
module.add(SAddU32(dst=sgpr(sTmp+1), src0=sgpr("StreamKIter"), src1=sgpr(sTmp+3), comment="Offset to start of SK section")) |
| 2018 |
module.add(SAddU32(dst=sgpr("StreamKIterEnd"), src0=sgpr("StreamKIterEnd"), src1=sgpr(sTmp+3), comment="Offset to start of SK section")) |
| 2019 |
|
| 2020 |
|
| 2021 |
module.add(SMinU32(dst=sgpr("StreamKIterEnd"), src0=sgpr("StreamKIterEnd"), src1=sgpr("TotalIters"), comment="Cap ending iter at total SK iters")) |
| 2022 |
|
| 2023 |
|
| 2024 |
module.add(SCmpLtU32(src0=sgpr("StreamKIter"), src1=sgpr("TotalIters"), comment="Make sure there's work to do")) |
| 2025 |
module.add(writer.longBranchScc0(Label("KernelEnd", ""), posNeg=1)) |
| 2026 |
|
| 2027 |
|
| 2028 |
|
| 2029 |
module.add(skUpdateDone) |
| 2030 |
module.add(SMovB32(dst=sgpr("StreamKIter"), src=sgpr(sTmp+1), comment="Store current iteration")) |
| 2031 |
|
| 2032 |
|
| 2033 |
module.add(self.skIndexToWG(writer, kernel, sTmp)) |
| 2034 |
|
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
|
| 2039 |
alphaLabel = Label("SKAlphaCheck", "") |
| 2040 |
module.add(BranchIfNotZero("Alpha", kernel["ProblemType"]["ComputeDataType"].toEnum(), alphaLabel)) |
| 2041 |
|
| 2042 |
module.add(SCmpEQU32(src0=sgpr("StreamKLocalStart"), src1=0, comment="does wg start tile?")) |
| 2043 |
skCloseLoopLabel = Label("SK_CloseLoop", "") |
| 2044 |
module.add(writer.longBranchScc0(skCloseLoopLabel, posNeg=1)) |
| 2045 |
module.add(SMovB32(dst=sgpr("StreamKLocalEnd"), src=sgpr("ItersPerTile"), comment="Skip iterations")) |
| 2046 |
module.add(alphaLabel) |
| 2047 |
|
| 2048 |
writer.sgprPool.checkIn(sTmp) |
| 2049 |
|
| 2050 |
return module |
| 2051 |
|
| 2052 |
def computeLoadSrd(self, writer, kernel, tc, sTmp): |
| 2053 |
module = Module("StreamK TwoTileDPFirst computeLoadSrd") |
| 2054 |
module.add(self.computeLoadSrdCommon(writer, kernel, tc, sTmp)) |
| 2055 |
return module |
| 2056 |
|
| 2057 |
def computeStoreSrdStart(self, writer, kernel): |
| 2058 |
module = Module("StreamK TwoTileDPFirst computeStoreSrdStart") |
| 2059 |
module.add(self.computeStoreSrdStartCommon(writer, kernel)) |
| 2060 |
return module |
| 2061 |
|
| 2062 |
def graAddresses(self, writer, kernel, tP, vTmp): |
| 2063 |
module = Module("StreamK TwoTileDPFirst graAddresses") |
| 2064 |
module.add(self.graAddressesCommon(writer, kernel, tP, vTmp)) |
| 2065 |
return module |
| 2066 |
|
| 2067 |
def declareStaggerParms(self, writer, kernel): |
| 2068 |
module = Module("StreamK TwoTileDPFirst declareStaggerParms") |
| 2069 |
module.add(self.declareStaggerParmsCommon(writer, kernel)) |
| 2070 |
return module |
| 2071 |
|
| 2072 |
def tailLoopNumIter(self, writer, kernel, loopCounter): |
| 2073 |
module = Module("StreamK TwoTileDPFirst tailLoopNumIter") |
| 2074 |
module.add(self.tailLoopNumIterCommon(writer, kernel, loopCounter)) |
| 2075 |
return module |
| 2076 |
|
| 2077 |
def calculateLoopNumIter(self, writer, kernel, loopCounterName, loopIdx, tmpSgprInfo): |
| 2078 |
module = Module("StreamK TwoTileDPFirst calculateLoopNumIter") |
| 2079 |
module.add(self.calculateLoopNumIterCommon(writer, kernel, loopCounterName, loopIdx, tmpSgprInfo)) |
| 2080 |
return module |
| 2081 |
|
| 2082 |
def storeBranches(self, writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct): |
| 2083 |
module = Module("StreamK TwoTileDPFirst storeBranches") |
| 2084 |
module.add(self.storeBranchesCommon(writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct)) |
| 2085 |
return module |
| 2086 |
|
| 2087 |
def writePartials(self, writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct, endLabel): |
| 2088 |
module = Module("StreamK TwoTileDPFirst writePartials") |
| 2089 |
module.add(self.writePartialsCommon(writer, kernel, skPartialsLabel, vectorWidths, elements, tmpVgpr, cvtVgprStruct, endLabel)) |
| 2090 |
return module |