| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
import ctypes |
| 24 |
import math |
| 25 |
import struct |
| 26 |
from collections import OrderedDict |
| 27 |
from copy import deepcopy |
| 28 |
from enum import Enum, IntFlag |
| 29 |
from typing import List, Union |
| 30 |
|
| 31 |
from rocisa import rocIsa |
| 32 |
from rocisa.code import Module, TextBlock |
| 33 |
from rocisa.container import VCC, EXEC, vgpr, sgpr, HolderContainer, RegisterContainer, Holder |
| 34 |
from rocisa.enum import InstType |
| 35 |
from rocisa.instruction import Instruction, SMovB32, SNop, SSetMask, VAddF16, VAddF32, \ |
| 36 |
VAddPKF16, VAndB32, VCmpGEF16, VCmpGEF32, VCmpGEF64, VCmpGEI32, VCmpGTF16, VCmpGTF32, \ |
| 37 |
VCmpGTF64, VCmpGTI32, VCmpXClassF32, VCmpXLtF32, VCndMaskB32, VExpF16, VExpF32, VFmaF16, \ |
| 38 |
VFmaF32, VFmaPKF16, VMaxF32, VMaxF64, VMaxI32, VMaxPKF16, VMed3I32, VMinF16, VMinF32, \ |
| 39 |
VMinF64, VMinI32, VMovB32, VMulF16, VMulF32, VMulF64, VMulLOU32, VMulPKF16, VRcpF16, \ |
| 40 |
VRcpF32, VSubF32, VSubI32 |
| 41 |
|
| 42 |
from Tensile.Common.DataType import DataType |
| 43 |
from Tensile.Common.Utilities import printExit, printWarning |
| 44 |
|
| 45 |
from dataclasses import dataclass |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
class ActivationAvailable: |
| 100 |
def __init__(self, canHalf=False, canSingle=False, canDouble=False, canBFloat16=False, canInt8=False, canInt16=False, canInt32=False): |
| 101 |
self.half = canHalf |
| 102 |
self.single = canSingle |
| 103 |
self.double = canDouble |
| 104 |
self.bfloat16 = canBFloat16 |
| 105 |
self.int8 = canInt8 |
| 106 |
self.int16 = canInt16 |
| 107 |
self.int32 = canInt32 |
| 108 |
|
| 109 |
class ActivationTypeRegister: |
| 110 |
def __init__(self, name, isGradient, extraArgs, canHalf=False, canSingle=False, canDouble=False, canBFloat16=False, canInt8=False, canInt16=False, canInt32=False): |
| 111 |
self.name = name |
| 112 |
self.isGradient = isGradient |
| 113 |
self.extraArgs = extraArgs |
| 114 |
self.can = ActivationAvailable(canHalf, canSingle, canDouble, canBFloat16, canInt8, canInt16, canInt32) |
| 115 |
def typeAvailable(self, dataType): |
| 116 |
if dataType.isHalf() and self.can.half: |
| 117 |
return True |
| 118 |
elif dataType.isSingle() and self.can.single: |
| 119 |
return True |
| 120 |
elif dataType.isDouble() and self.can.double: |
| 121 |
return True |
| 122 |
elif dataType.isBFloat16() and self.can.bfloat16: |
| 123 |
return True |
| 124 |
elif dataType.isInt8() and self.can.int8: |
| 125 |
return True |
| 126 |
elif dataType.isInt32() and self.can.int32: |
| 127 |
return True |
| 128 |
return False |
| 129 |
|
| 130 |
class ActivationType: |
| 131 |
class Export(Enum): |
| 132 |
NORMAL = 0 |
| 133 |
GRADONLY = 1 |
| 134 |
BOTH = 2 |
| 135 |
|
| 136 |
|
| 137 |
class SupportedBy(IntFlag): |
| 138 |
HIPBLASLT = 0b01 |
| 139 |
TENSILE = 0b10 |
| 140 |
ALL = 0b11 |
| 141 |
|
| 142 |
stringList = ['alpha', 'beta', 'gamma', 'delta' ] |
| 143 |
|
| 144 |
|
| 145 |
lookupVeri = OrderedDict([('exp', ActivationTypeRegister('exp', 0, True, True, False, False, False, False, False)) ]) |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
lookup = OrderedDict([('none', { \ |
| 150 |
'instance': ActivationTypeRegister('none', False, 0, True, True, True, True, True, True, True), \ |
| 151 |
'supported_by': SupportedBy.TENSILE | SupportedBy.HIPBLASLT}), \ |
| 152 |
('abs', { \ |
| 153 |
'instance': ActivationTypeRegister('abs', False, 0, True, True, True, True, False, False, True), \ |
| 154 |
'supported_by': SupportedBy.TENSILE}), \ |
| 155 |
('clippedrelu', { \ |
| 156 |
'instance': ActivationTypeRegister('clippedrelu', False, 2, True, True, True, False, False, False, True), \ |
| 157 |
'supported_by': SupportedBy.TENSILE}), \ |
| 158 |
('gelu', { \ |
| 159 |
'instance': ActivationTypeRegister('gelu', False, 0, True, True, False, False, False, False, False), \ |
| 160 |
'supported_by': SupportedBy.TENSILE | SupportedBy.HIPBLASLT}), \ |
| 161 |
('leakyrelu', { \ |
| 162 |
'instance': ActivationTypeRegister('leakyrelu', False, 1, True, True, True, False, False, False, True), \ |
| 163 |
'supported_by': SupportedBy.TENSILE}), \ |
| 164 |
('relu', { \ |
| 165 |
'instance': ActivationTypeRegister('relu', False, 0, True, True, True, False, False, False, True), \ |
| 166 |
'supported_by': SupportedBy.TENSILE | SupportedBy.HIPBLASLT}), \ |
| 167 |
('sigmoid', { \ |
| 168 |
'instance': ActivationTypeRegister('sigmoid', False, 0, True, True, False, False, False, False, False), \ |
| 169 |
'supported_by': SupportedBy.TENSILE}), \ |
| 170 |
('tanh', { \ |
| 171 |
'instance': ActivationTypeRegister('tanh', False, 2, True, True, False, False, False, False, False), \ |
| 172 |
'supported_by': SupportedBy.TENSILE}), \ |
| 173 |
('dgelu', { \ |
| 174 |
'instance': ActivationTypeRegister('dgelu', True, 0, False, True, False, False, False, False, False), \ |
| 175 |
'supported_by': SupportedBy.TENSILE | SupportedBy.HIPBLASLT}), \ |
| 176 |
('geluscaling', { \ |
| 177 |
'instance': ActivationTypeRegister('geluscaling', False, 1, True, True, False, False, False, False, False), \ |
| 178 |
'supported_by': SupportedBy.TENSILE}), \ |
| 179 |
('silu', { \ |
| 180 |
'instance': ActivationTypeRegister('silu', False, 0, True, True, False, False, False, False, False), \ |
| 181 |
'supported_by': SupportedBy.TENSILE | SupportedBy.HIPBLASLT}), \ |
| 182 |
('swish', { \ |
| 183 |
'instance': ActivationTypeRegister('swish', False, 1, True, True, False, False, False, False, False), \ |
| 184 |
'supported_by': SupportedBy.TENSILE}), \ |
| 185 |
('clamp', { \ |
| 186 |
'instance': ActivationTypeRegister('clamp', False, 2, True, True, True, False, False, False, True), \ |
| 187 |
'supported_by': SupportedBy.TENSILE | SupportedBy.HIPBLASLT}), \ |
| 188 |
('hipblaslt_all', { \ |
| 189 |
'instance': ActivationTypeRegister('hipblaslt_all', False, 0), \ |
| 190 |
'supported_by': SupportedBy.HIPBLASLT}), \ |
| 191 |
('all', { \ |
| 192 |
'instance': ActivationTypeRegister('all', False, 0), \ |
| 193 |
'supported_by': SupportedBy.TENSILE | SupportedBy.HIPBLASLT}) ]) |
| 194 |
def __init__(self, value): |
| 195 |
if isinstance(value, str): |
| 196 |
strValue = value.lower() |
| 197 |
if strValue in self.lookup: |
| 198 |
self.value = strValue |
| 199 |
elif strValue in self.lookupVeri: |
| 200 |
self.value = strValue |
| 201 |
else: |
| 202 |
raise RuntimeError("Unrecognized activation type %s"%value) |
| 203 |
elif isinstance(value, ActivationType): |
| 204 |
self.value = value.value |
| 205 |
else: |
| 206 |
raise RuntimeError("Unrecognized input type %s, should be string or ActivationType"%str(value)) |
| 207 |
|
| 208 |
def passActivation(self, isGradient, exportType: Export) -> bool: |
| 209 |
if exportType == ActivationType.Export.NORMAL: |
| 210 |
return isGradient |
| 211 |
elif exportType == ActivationType.Export.GRADONLY: |
| 212 |
return not isGradient |
| 213 |
elif exportType == ActivationType.Export.BOTH: |
| 214 |
return False |
| 215 |
|
| 216 |
def getAdditionalArgNum(self, exportType: Export=Export.NORMAL): |
| 217 |
if self.value in ['all', 'hipblaslt_all']: |
| 218 |
maxArgNum = 0 |
| 219 |
for _, value in self.lookup.items(): |
| 220 |
activationInst = value['instance'] |
| 221 |
if self.passActivation(activationInst.isGradient, exportType): |
| 222 |
continue |
| 223 |
maxArgNum = max(maxArgNum, activationInst.extraArgs) |
| 224 |
return maxArgNum |
| 225 |
elif self.value in self.lookup: |
| 226 |
return self.lookup[self.value]['instance'].extraArgs |
| 227 |
return 0 |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
def fitSupported(self, config: SupportedBy, components: SupportedBy): |
| 232 |
return (config & components) |
| 233 |
def getAdditionalArgStringList(self, addPrefix=True): |
| 234 |
list = [] |
| 235 |
for i in range(0, self.getAdditionalArgNum()): |
| 236 |
if addPrefix: |
| 237 |
list.append("activation" + self.stringList[i].capitalize()) |
| 238 |
else: |
| 239 |
list.append(self.stringList[i]) |
| 240 |
return list |
| 241 |
@classmethod |
| 242 |
def getEnumIndex(cls, enumStr): |
| 243 |
return list(cls.lookup.keys()).index(enumStr) |
| 244 |
@classmethod |
| 245 |
def getEnumStrList(cls, dataType, configSupported, includeNone = True, exportType: Export=Export.NORMAL): |
| 246 |
enumList = [] |
| 247 |
for key, value in cls.lookup.items(): |
| 248 |
activationInst = value['instance'] |
| 249 |
components = value['supported_by'] |
| 250 |
if cls.passActivation(cls, activationInst.isGradient, exportType): |
| 251 |
continue |
| 252 |
if (((key != 'none') or includeNone) and (key not in ['all', 'hipblaslt_all'])): |
| 253 |
if (activationInst.typeAvailable(dataType)) and (cls.fitSupported(cls, configSupported, components)) : |
| 254 |
enumList.append(key) |
| 255 |
if not enumList: |
| 256 |
printWarning("No available activation for this data type %s.\n"%str(dataType)) |
| 257 |
return enumList |
| 258 |
def state(self): return self.value.capitalize() |
| 259 |
def __repr__(self): |
| 260 |
return self.__str__() |
| 261 |
def __str__(self): |
| 262 |
return self.value.capitalize() |
| 263 |
def __eq__(self, other): |
| 264 |
if isinstance(other, str): |
| 265 |
return self.value == other.lower() |
| 266 |
elif isinstance(other, ActivationType): |
| 267 |
return self.value == other.value |
| 268 |
else: |
| 269 |
raise RuntimeError("Unrecognized type in rhs, should be string or ActivationType") |
| 270 |
def __lt__(self, other): |
| 271 |
if isinstance(other, str): |
| 272 |
return self.value < other.lower() |
| 273 |
elif isinstance(other, ActivationType): |
| 274 |
return self.value < other.value |
| 275 |
else: |
| 276 |
raise RuntimeError("Unrecognized type in rhs, should be string or ActivationType") |
| 277 |
|
| 278 |
def toEnum(self): |
| 279 |
return self.value.capitalize() |
| 280 |
|
| 281 |
@dataclass |
| 282 |
class actCacheInfo: |
| 283 |
usePK: bool |
| 284 |
saturateI8: bool |
| 285 |
enableGuard: bool |
| 286 |
isAlt: bool |
| 287 |
prefix: str |
| 288 |
vgprIdxList: List[List[RegisterContainer]] |
| 289 |
module: Module |
| 290 |
vgprCounter: int |
| 291 |
sgprCounter: int |
| 292 |
|
| 293 |
def isSame(self, usePK, saturateI8, enableGuard, isAlt, prefix): |
| 294 |
return (self.usePK == usePK) and (self.saturateI8 == saturateI8) and \ |
| 295 |
(self.enableGuard == enableGuard) and (self.prefix == prefix) and \ |
| 296 |
(self.isAlt == isAlt) |
| 297 |
|
| 298 |
ActivationMagicNumbers = {"FloatGeluK0": 0x3f4c422a, \ |
| 299 |
"FloatGeluK1": 0x3d372713, \ |
| 300 |
"Float16GeluK1": 0x29b9, \ |
| 301 |
|
| 302 |
"FloatDGeluK0": 0x3d5b33b3, \ |
| 303 |
|
| 304 |
"FloatDGeluK1": 0x3ecc4220, \ |
| 305 |
|
| 306 |
"FloatDGeluK2": 0x3d12220c, \ |
| 307 |
|
| 308 |
"FloatDGeluK3": 0x3f4c4231} |
| 309 |
|
| 310 |
|
| 311 |
class floatUnion(ctypes.Union): |
| 312 |
_fields_ = [('u', ctypes.c_uint), ('f', ctypes.c_float)] |
| 313 |
|
| 314 |
class ActivationModule: |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
def __init__(self) -> None: |
| 324 |
|
| 325 |
self.vgprCounter = 0 |
| 326 |
self.sgprCounter = 0 |
| 327 |
|
| 328 |
self.usePK = True |
| 329 |
self.saturateI8 = False |
| 330 |
self.vgprPrefixFormat = "" |
| 331 |
|
| 332 |
|
| 333 |
self.needCombine = False |
| 334 |
|
| 335 |
self.cacheDict = {} |
| 336 |
self.useCache = False |
| 337 |
self.labelCounter = 0 |
| 338 |
|
| 339 |
self.enableGuard = False |
| 340 |
self.isAlt = False |
| 341 |
|
| 342 |
def __reduce__(self): |
| 343 |
return (ActivationModule, ()) |
| 344 |
|
| 345 |
|
| 346 |
def getModule(self, cDataType, activationType, vgprIn, vgprOut): |
| 347 |
if self.useCache: |
| 348 |
module = self.getCache(cDataType, activationType, vgprIn, vgprOut) |
| 349 |
if module: |
| 350 |
return module |
| 351 |
module = "" |
| 352 |
self.needCombine = False |
| 353 |
self.resetGprCounter() |
| 354 |
if (activationType == 'abs'): |
| 355 |
module = self.getAbsModule(cDataType, vgprIn, vgprOut) |
| 356 |
elif (activationType == 'clippedrelu'): |
| 357 |
module = self.getClippedReluModule(cDataType, vgprIn, vgprOut, "activationAlpha", "activationBeta") |
| 358 |
elif (activationType == 'exp'): |
| 359 |
module = self.getExpModule(cDataType, vgprIn, vgprOut) |
| 360 |
elif (activationType == 'gelu'): |
| 361 |
module = self.getGeluModule(cDataType, vgprIn, vgprOut) |
| 362 |
elif (activationType == 'geluscaling'): |
| 363 |
module = self.getGeluModule(cDataType, vgprIn, vgprOut, "activationAlpha") |
| 364 |
elif (activationType == 'leakyrelu'): |
| 365 |
module = self.getLeakyReluModule(cDataType, vgprIn, vgprOut, "activationAlpha") |
| 366 |
elif (activationType == 'relu'): |
| 367 |
module = self.getReluModule(cDataType, vgprIn, vgprOut) |
| 368 |
elif (activationType == 'sigmoid'): |
| 369 |
module = self.getSigmoidModule(cDataType, vgprIn, vgprOut) |
| 370 |
elif (activationType == 'tanh'): |
| 371 |
module = self.getTanhModule(cDataType, vgprIn, vgprOut, "activationAlpha", "activationBeta") |
| 372 |
elif (activationType == 'dgelu'): |
| 373 |
module = self.getDGeluModule(cDataType, vgprIn, vgprOut) |
| 374 |
elif (activationType == 'silu'): |
| 375 |
module = self.getSiluModule(cDataType, vgprIn, vgprOut) |
| 376 |
elif (activationType == 'swish'): |
| 377 |
module = self.getSwishModule(cDataType, vgprIn, vgprOut, "activationAlpha") |
| 378 |
elif (activationType == 'clamp'): |
| 379 |
module = self.getClampModule(cDataType, vgprIn, vgprOut, "activationAlpha", "activationBeta") |
| 380 |
elif (activationType == 'none'): |
| 381 |
return Module("No activation") |
| 382 |
else: |
| 383 |
return Module("%s not implemented"%activationType) |
| 384 |
|
| 385 |
module = self.postProcess(cDataType, module) |
| 386 |
|
| 387 |
if self.useCache and (vgprIn != vgprOut): |
| 388 |
self.createCache(cDataType, activationType, vgprIn, vgprOut, module) |
| 389 |
return module |
| 390 |
|
| 391 |
def getAllGprUsage(self, cDataType, actType, exportType: ActivationType.Export=ActivationType.Export.NORMAL) -> dict: |
| 392 |
usage = {} |
| 393 |
if actType == 'all': |
| 394 |
enumList = ActivationType.getEnumStrList(cDataType, configSupported=ActivationType.SupportedBy.ALL, exportType=exportType) |
| 395 |
elif actType == 'hipblaslt_all': |
| 396 |
enumList = ActivationType.getEnumStrList(cDataType, configSupported=ActivationType.SupportedBy.HIPBLASLT, exportType=exportType) |
| 397 |
else: |
| 398 |
enumList = [str(actType)] |
| 399 |
for enumStr in enumList: |
| 400 |
_ = self.getModule(cDataType, enumStr, 0, 1) |
| 401 |
usage[enumStr] = {"vgpr": self.vgprCounter, "sgpr": self.sgprCounter} |
| 402 |
return usage |
| 403 |
|
| 404 |
def postProcess(self, cDataType, module): |
| 405 |
if self.needCombine: |
| 406 |
CombineInstructions(module) |
| 407 |
module = ConvertCoeffToHex(module, cDataType, self.usePK) |
| 408 |
return module |
| 409 |
|
| 410 |
def assignGpr(self, module, vgprIdx, sgprIdx): |
| 411 |
patternPrefix = ["v", "s"] |
| 412 |
gprIdx = [vgprIdx, sgprIdx] |
| 413 |
for idx, pf in enumerate(patternPrefix): |
| 414 |
module = HolderToGpr(module, gprIdx[idx], pf) |
| 415 |
return module |
| 416 |
|
| 417 |
def setUsePK(self, usePK): |
| 418 |
self.usePK = usePK |
| 419 |
|
| 420 |
def setSaturationForInt8(self, sat): |
| 421 |
self.saturateI8 = sat |
| 422 |
|
| 423 |
def setVgprPrefixFormat(self, formatting): |
| 424 |
self.vgprPrefixFormat = formatting |
| 425 |
|
| 426 |
def setUseCache(self, cache: bool): |
| 427 |
self.useCache = cache |
| 428 |
|
| 429 |
def setGuard(self, guard: bool): |
| 430 |
self.enableGuard = guard |
| 431 |
|
| 432 |
def setAlt(self, alt: bool): |
| 433 |
self.isAlt = alt |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
def resetGprCounter(self): |
| 444 |
self.vgprCounter = 0 |
| 445 |
self.sgprCounter = 0 |
| 446 |
|
| 447 |
def getVgpr(self, num): |
| 448 |
value = self.vgprCounter |
| 449 |
self.vgprCounter += num |
| 450 |
return value |
| 451 |
|
| 452 |
def getSgpr(self, num): |
| 453 |
value = self.sgprCounter |
| 454 |
self.sgprCounter += num |
| 455 |
return value |
| 456 |
|
| 457 |
def vgprPrefix(self, *args): |
| 458 |
if isinstance(args[0], int) and self.vgprPrefixFormat: |
| 459 |
vgprStr = self.vgprPrefixFormat%args[0] |
| 460 |
else: |
| 461 |
vgprStr = args[0] |
| 462 |
|
| 463 |
if len(args) == 1: |
| 464 |
return vgpr(vgprStr) |
| 465 |
else: |
| 466 |
args = args[1] |
| 467 |
return vgpr(vgprStr, args) |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
def getAbsModule(self, cDataType, vgprIn, vgprOut): |
| 478 |
module = Module("Abs") |
| 479 |
if cDataType.isHalf() or cDataType.isBFloat16(): |
| 480 |
absMagic = "0x7fff7fff" if self.usePK else "0x7fff" |
| 481 |
module.add(VAndB32(dst=self.vgprPrefix(vgprOut), src0=absMagic, src1=self.vgprPrefix(vgprIn), comment="Remove sign bit")) |
| 482 |
elif cDataType.isSingle(): |
| 483 |
module.add(VAndB32(dst=self.vgprPrefix(vgprOut), src0="0x7fffffff", src1=self.vgprPrefix(vgprIn), comment="Remove sign bit")) |
| 484 |
elif cDataType.isDouble(): |
| 485 |
module.add(VAndB32(dst=self.vgprPrefix(vgprOut+1), src0="0x7fffffff", src1=self.vgprPrefix(vgprIn+1), comment="Remove sign bit")) |
| 486 |
elif cDataType.isInt32(): |
| 487 |
vgprTemp = self.getVgpr(1) |
| 488 |
module.add(VSubI32(dst=vgpr(Holder(idx=vgprTemp)), src0=0, src1=self.vgprPrefix(vgprIn), comment="x2 = -x")) |
| 489 |
if self.saturateI8: |
| 490 |
vgprTemp2 = self.getVgpr(1) |
| 491 |
module.add(VMovB32(dst=vgpr(Holder(idx=vgprTemp2)), src=hex(127), comment="value = 127")) |
| 492 |
module.add(VMed3I32(dst=self.vgprPrefix(vgprOut), src0=self.vgprPrefix(vgprIn), src1=vgpr(Holder(idx=vgprTemp)), src2=vgpr(Holder(idx=vgprTemp2)), comment="y = min(127, max(x, x2))")) |
| 493 |
else: |
| 494 |
module.add(VMaxI32(dst=self.vgprPrefix(vgprOut), src0=vgpr(Holder(idx=vgprTemp)), src1=self.vgprPrefix(vgprIn), comment="y = max(x, x2)")) |
| 495 |
else: |
| 496 |
raise RuntimeError("Unsupported data type %s."%cDataType.toDevice("HIP")) |
| 497 |
return module |
| 498 |
|
| 499 |
def getClippedReluModule(self, cDataType, vgprIn, vgprOut, activationAlpha, activationBeta): |
| 500 |
module = Module("ClippedRelu") |
| 501 |
if cDataType.isHalf(): |
| 502 |
vgprTemp = self.getVgpr(1) |
| 503 |
for i in range(0, 2): |
| 504 |
select_bit = SelectBit.WORD_0 if i == 0 else SelectBit.WORD_1 |
| 505 |
module.add(VCmpGTF16(dst=VCC(), src0=self.vgprPrefix(vgprIn), src1=sgpr(activationAlpha), \ |
| 506 |
sdwa=SDWAModifiers(src0_sel=select_bit, src1_sel=SelectBit.WORD_0), comment="x > alpha?")) |
| 507 |
module.add(VMinF16(dst=self.vgprPrefix(vgprOut), src0=sgpr(activationBeta), src1=self.vgprPrefix(vgprIn), \ |
| 508 |
sdwa=SDWAModifiers(dst_sel=select_bit, dst_unused=UnusedBit.UNUSED_PRESERVE, \ |
| 509 |
src0_sel=select_bit, src1_sel=select_bit), \ |
| 510 |
comment="min(x, beta)")) |
| 511 |
module.add(VMinF16(dst=vgpr(Holder(idx=vgprTemp)), src0=sgpr(activationBeta), src1=0.0, \ |
| 512 |
sdwa=SDWAModifiers(dst_sel=select_bit, dst_unused=UnusedBit.UNUSED_PRESERVE, \ |
| 513 |
src0_sel=select_bit, src1_sel=select_bit), \ |
| 514 |
comment="min(0, beta)")) |
| 515 |
module.add(VCndMaskB32(dst=self.vgprPrefix(vgprOut), src0=vgpr(Holder(idx=vgprTemp)), src1=self.vgprPrefix(vgprOut), \ |
| 516 |
sdwa=SDWAModifiers(dst_sel=select_bit, dst_unused=UnusedBit.UNUSED_PRESERVE, \ |
| 517 |
src0_sel=select_bit, src1_sel=select_bit), \ |
| 518 |
comment="set x to min(0, beta) if <= alpha")) |
| 519 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 520 |
elif cDataType.isSingle(): |
| 521 |
module.add(VCmpGTF32(dst=VCC(), src0=self.vgprPrefix(vgprIn), src1=sgpr(activationAlpha), comment="x > alpha ?")) |
| 522 |
module.add(VMinF32(dst=self.vgprPrefix(vgprIn), src0=sgpr(activationBeta), src1=self.vgprPrefix(vgprIn), comment="min(x, beta)")) |
| 523 |
vgprTemp = self.getVgpr(1) |
| 524 |
module.add(VMinF32(dst=vgpr(Holder(idx=vgprTemp)), src0=sgpr(activationBeta), src1=0.0, comment="min(0, beta)")) |
| 525 |
module.add(VCndMaskB32(dst=self.vgprPrefix(vgprIn), src0=vgpr(Holder(idx=vgprTemp)), src1=self.vgprPrefix(vgprIn), comment="set x to min(0, beta) if <= alpha")) |
| 526 |
elif cDataType.isDouble(): |
| 527 |
module.add(VCmpGTF64(dst=VCC(), src0=self.vgprPrefix(vgprIn, 2), src1=sgpr(activationAlpha, 2), comment="x > alpha ?")) |
| 528 |
module.add(VMinF64(dst=self.vgprPrefix(vgprIn, 2), src0=sgpr(activationBeta, 2), src1=self.vgprPrefix(vgprIn, 2), comment="min(x, beta)")) |
| 529 |
vgprTemp = self.getVgpr(2) |
| 530 |
module.add(VMinF64(dst=vgpr(Holder(idx=vgprTemp), 2), src0=sgpr(activationBeta, 2), src1=0.0, comment="min(0, beta)")) |
| 531 |
module.add(VCndMaskB32(dst=self.vgprPrefix(vgprIn), src0=vgpr(Holder(idx=vgprTemp)), src1=self.vgprPrefix(vgprIn), comment="set x to min(0, beta) if <= alpha")) |
| 532 |
module.add(VCndMaskB32(dst=self.vgprPrefix(vgprIn+1), src0=vgpr(Holder(idx=vgprTemp+1)), src1=self.vgprPrefix(vgprIn+1), comment="set x to min(0, beta) if <= alpha")) |
| 533 |
elif cDataType.isInt32(): |
| 534 |
module.add(VCmpGTI32(dst=VCC(), src0=self.vgprPrefix(vgprIn), src1=sgpr(activationAlpha), comment="x > alpha ?")) |
| 535 |
module.add(VMinI32(dst=self.vgprPrefix(vgprIn), src0=sgpr(activationBeta), src1=self.vgprPrefix(vgprIn), comment="min(x, beta)")) |
| 536 |
vgprTemp = self.getVgpr(1) |
| 537 |
module.add(VMinI32(dst=vgpr(Holder(idx=vgprTemp)), src0=sgpr(activationBeta), src1=0, comment="min(0, beta)")) |
| 538 |
module.add(VCndMaskB32(dst=self.vgprPrefix(vgprIn), src0=vgpr(Holder(idx=vgprTemp)), src1=self.vgprPrefix(vgprIn), comment="set x to min(0, beta) if <= alpha")) |
| 539 |
return module |
| 540 |
|
| 541 |
def getExpModule(self, cDataType, vgprIn, vgprOut): |
| 542 |
ti = rocIsa.getInstance() |
| 543 |
module = Module("Exp") |
| 544 |
if cDataType.isHalf(): |
| 545 |
sgprMagic = self.getSgpr(1) |
| 546 |
module.add(SMovB32(dst=sgpr(Holder(idx=sgprMagic)), src=math.log(math.e,2), comment="exp magic")) |
| 547 |
if self.usePK: |
| 548 |
module.add(VMulPKF16(dst=self.vgprPrefix(vgprOut), src0=sgpr(Holder(idx=sgprMagic)), src1=self.vgprPrefix(vgprIn), comment="exp step 1")) |
| 549 |
for i in range(0, 2): |
| 550 |
select_bit = SelectBit.WORD_0 if i == 0 else SelectBit.WORD_1 |
| 551 |
module.add(VExpF16(dst=self.vgprPrefix(vgprOut), src=self.vgprPrefix(vgprOut), \ |
| 552 |
sdwa=SDWAModifiers(dst_sel=select_bit, dst_unused=UnusedBit.UNUSED_PRESERVE, \ |
| 553 |
src0_sel=select_bit), \ |
| 554 |
comment="exp step 2")) |
| 555 |
if ti.getArchCaps()["TransOpWait"]: |
| 556 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 557 |
else: |
| 558 |
module.add(VMulF16(dst=self.vgprPrefix(vgprOut), src0=sgpr(Holder(idx=sgprMagic)), src1=self.vgprPrefix(vgprIn), comment="exp step 1")) |
| 559 |
module.add(VExpF16(dst=self.vgprPrefix(vgprOut), src=self.vgprPrefix(vgprOut), comment="exp step 2")) |
| 560 |
if ti.getArchCaps()["TransOpWait"]: |
| 561 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 562 |
elif cDataType.isSingle(): |
| 563 |
module.add(VMulF32(dst=self.vgprPrefix(vgprOut), src0=math.log(math.e,2), src1=self.vgprPrefix(vgprIn), comment="exp step 1")) |
| 564 |
module.add(VExpF32(dst=self.vgprPrefix(vgprOut), src=self.vgprPrefix(vgprOut), comment="exp step 2" )) |
| 565 |
if ti.getArchCaps()["TransOpWait"]: |
| 566 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 567 |
else: |
| 568 |
raise RuntimeError("Unsupported data type %s."%cDataType.toDevice("HIP")) |
| 569 |
return module |
| 570 |
|
| 571 |
def getGeluModule(self, cDataType, vgprIn, vgprOut, activationAlpha=None): |
| 572 |
self.needCombine = True |
| 573 |
module = Module("Gelu") |
| 574 |
|
| 575 |
if cDataType.isHalf(): |
| 576 |
flt16GeluK1Str = HexToStr(cDataType, self.usePK, ActivationMagicNumbers["Float16GeluK1"]) |
| 577 |
sgprMagicK1 = self.getSgpr(1) |
| 578 |
sgprPKLiteral = self.getSgpr(1) |
| 579 |
module.add(SMovB32(dst=sgpr(Holder(idx=sgprMagicK1)), src=flt16GeluK1Str, comment="Float16GeluK1" )) |
| 580 |
module.add(SMovB32(dst=sgpr(Holder(idx=sgprPKLiteral)), src=coef.f)) |
| 581 |
vgprTemp = self.getVgpr(1) |
| 582 |
if self.usePK: |
| 583 |
module.add(VMulPKF16(dst=vgpr(Holder(idx=vgprTemp)), src0=self.vgprPrefix(vgprIn), src1=self.vgprPrefix(vgprIn), comment="x * x" )) |
| 584 |
module.add(VFmaPKF16(dst=vgpr(Holder(idx=vgprTemp)), src0=vgpr(Holder(idx=vgprTemp)), src1=sgpr(Holder(idx=sgprMagicK1)), src2=1.0, \ |
| 585 |
vop3=VOP3PModifiers(op_sel_hi=[1,1,0,1]), comment="x^2 * k1 + 1")) |
| 586 |
module.add(VMulPKF16(dst=vgpr(Holder(idx=vgprTemp)), src0=self.vgprPrefix(vgprIn), src1=vgpr(Holder(idx=vgprTemp)), comment="x * (x^2 * k1 + 1)")) |
| 587 |
coef = floatUnion(u=ActivationMagicNumbers["FloatGeluK0"]) |
| 588 |
module.add(VMulPKF16(dst=vgpr(Holder(idx=vgprTemp)), src0=sgpr(Holder(idx=sgprPKLiteral)), src1=vgpr(Holder(idx=vgprTemp)), comment="k0 * x * (x^2 * k1 + 1)")) |
| 589 |
module.add(self.getTanhModule(cDataType, Holder(idx=vgprTemp), Holder(idx=vgprTemp), "", "")) |
| 590 |
module.add(VAddPKF16(dst=vgpr(Holder(idx=vgprTemp)), src0=1.0, src1=vgpr(Holder(idx=vgprTemp)), \ |
| 591 |
vop3=VOP3PModifiers(op_sel_hi=[0,1,1]), comment="1 + tanh(...)" )) |
| 592 |
module.add(VMulPKF16(dst=vgpr(Holder(idx=vgprTemp)), src0=self.vgprPrefix(vgprIn), src1=vgpr(Holder(idx=vgprTemp)), comment="x * (1 + tanh(...))")) |
| 593 |
|
| 594 |
if activationAlpha == None: |
| 595 |
module.add(VMulPKF16(dst=self.vgprPrefix(vgprOut), src0=0.5, src1=vgpr(Holder(idx=vgprTemp)), \ |
| 596 |
vop3=VOP3PModifiers(op_sel_hi=[0,1,1]), comment="0.5 * x * (1 + tanh(...))")) |
| 597 |
else: |
| 598 |
module.add(VMulPKF16(dst=vgpr(Holder(idx=vgprTemp)), src0=0.5, src1=vgpr(Holder(idx=vgprTemp)), \ |
| 599 |
vop3=VOP3PModifiers(op_sel_hi=[0,1,1]), comment="0.5 * x * (1 + tanh(...))")) |
| 600 |
module.add(VMulPKF16(dst=self.vgprPrefix(vgprOut), src0=sgpr(activationAlpha), src1=vgpr(Holder(idx=vgprTemp)), \ |
| 601 |
vop3=VOP3PModifiers(op_sel_hi=[0,1,1]), comment="0.5 * x * (1 + tanh(...)) * scale")) |
| 602 |
|
| 603 |
else: |
| 604 |
module.add(VMulF16(dst=vgpr(Holder(idx=vgprTemp)), src0=self.vgprPrefix(vgprIn), src1=self.vgprPrefix(vgprIn), comment="x * x" )) |
| 605 |
module.add(VFmaF16(dst=vgpr(Holder(idx=vgprTemp)), src0=vgpr(Holder(idx=vgprTemp)), src1=sgpr(Holder(idx=sgprMagicK1)), src2=1.0, comment="x^2 * k1 + 1")) |
| 606 |
module.add(VMulF16(dst=vgpr(Holder(idx=vgprTemp)), src0=self.vgprPrefix(vgprIn), src1=vgpr(Holder(idx=vgprTemp)), comment="x * (x^2 * k1 + 1)")) |
| 607 |
coef = floatUnion(u=ActivationMagicNumbers["FloatGeluK0"]) |
| 608 |
module.add(VMulF16(dst=vgpr(Holder(idx=vgprTemp)), src0=sgpr(Holder(idx=sgprPKLiteral)), src1=vgpr(Holder(idx=vgprTemp)), comment="k0 * x * (x^2 * k1 + 1)")) |
| 609 |
module.add(self.getTanhModule(cDataType, Holder(idx=vgprTemp), Holder(idx=vgprTemp), "", "")) |
| 610 |
module.add(VAddF16(dst=vgpr(Holder(idx=vgprTemp)), src0=1.0, src1=vgpr(Holder(idx=vgprTemp)), comment="1 + tanh(...)" )) |
| 611 |
module.add(VMulF16(dst=vgpr(Holder(idx=vgprTemp)), src0=self.vgprPrefix(vgprIn), src1=vgpr(Holder(idx=vgprTemp)), comment="x * (1 + tanh(...))")) |
| 612 |
if activationAlpha == None: |
| 613 |
module.add(VMulF16(dst=self.vgprPrefix(vgprOut), src0=0.5, src1=vgpr(Holder(idx=vgprTemp)), comment="0.5 * x * (1 + tanh(...))")) |
| 614 |
else: |
| 615 |
module.add(VMulF16(dst=vgpr(Holder(idx=vgprTemp)), src0=0.5, src1=vgpr(Holder(idx=vgprTemp)), comment="0.5 * x * (1 + tanh(...))")) |
| 616 |
module.add(VMulF16(dst=self.vgprPrefix(vgprOut), src0=sgpr(activationAlpha), src1=vgpr(Holder(idx=vgprTemp)), comment="0.5 * x * (1 + tanh(...)) * scale")) |
| 617 |
elif cDataType.isSingle(): |
| 618 |
vgprTemp = self.getVgpr(1) |
| 619 |
flt16GeluK1Str = HexToStr(cDataType, self.usePK, ActivationMagicNumbers["FloatGeluK1"]) |
| 620 |
module.add(VMulF32(dst=vgpr(Holder(idx=vgprTemp)), src0=flt16GeluK1Str, src1=self.vgprPrefix(vgprIn), comment="k1 * x")) |
| 621 |
module.add(VFmaF32(dst=vgpr(Holder(idx=vgprTemp)), src0=self.vgprPrefix(vgprIn), src1=vgpr(Holder(idx=vgprTemp)), src2=1.0, comment="1 + (k1 * x * x)")) |
| 622 |
module.add(VMulF32(dst=vgpr(Holder(idx=vgprTemp)), src0=self.vgprPrefix(vgprIn), src1=vgpr(Holder(idx=vgprTemp)), comment="x * (1 + k1 * x * x)")) |
| 623 |
coef = floatUnion(u=ActivationMagicNumbers["FloatGeluK0"]) |
| 624 |
module.add(VMulF32(dst=vgpr(Holder(idx=vgprTemp)), src0=coef.f, src1=vgpr(Holder(idx=vgprTemp)), comment="k0 * x * (x^2 * k1 + 1)")) |
| 625 |
module.add(self.getTanhModule(cDataType, Holder(idx=vgprTemp), Holder(idx=vgprTemp), "", "")) |
| 626 |
module.add(VAddF32(dst=vgpr(Holder(idx=vgprTemp)), src0=1.0, src1=vgpr(Holder(idx=vgprTemp)), comment="1 + tanh(...)" )) |
| 627 |
module.add(VMulF32(dst=vgpr(Holder(idx=vgprTemp)), src0=self.vgprPrefix(vgprIn), src1=vgpr(Holder(idx=vgprTemp)), comment="x * (1 + tanh(...))")) |
| 628 |
if activationAlpha == None: |
| 629 |
module.add(VMulF32(dst=self.vgprPrefix(vgprOut), src0=0.5, src1=vgpr(Holder(idx=vgprTemp)), comment="0.5 * x * (1 + tanh(...))")) |
| 630 |
else: |
| 631 |
module.add(VMulF32(dst=vgpr(Holder(idx=vgprTemp)), src0=0.5, src1=vgpr(Holder(idx=vgprTemp)), comment="0.5 * x * (1 + tanh(...))")) |
| 632 |
module.add(VMulF32(dst=self.vgprPrefix(vgprOut), src0=sgpr(activationAlpha), src1=vgpr(Holder(idx=vgprTemp)), comment="0.5 * x * (1 + tanh(...)) * scale")) |
| 633 |
else: |
| 634 |
raise RuntimeError("Unsupported data type %s."%cDataType.toDevice("HIP")) |
| 635 |
return module |
| 636 |
|
| 637 |
def getLeakyReluModule(self, cDataType, vgprIn, vgprOut, activationAlpha): |
| 638 |
module = Module("LeakyRelu") |
| 639 |
if cDataType.isHalf(): |
| 640 |
vgprTemp = self.getVgpr(1) |
| 641 |
module.add(VMulPKF16(dst=vgpr(Holder(idx=vgprTemp)), src0=sgpr(activationAlpha), src1=self.vgprPrefix(vgprIn), comment="tmp = x * alpha")) |
| 642 |
for i in range(0, 2): |
| 643 |
select_bit = SelectBit.WORD_0 if i == 0 else SelectBit.WORD_1 |
| 644 |
module.add(VCmpGEF16(dst=VCC(), src0=self.vgprPrefix(vgprIn), src1=0.0, \ |
| 645 |
sdwa=SDWAModifiers(src0_sel=select_bit, src1_sel=SelectBit.WORD_0), \ |
| 646 |
comment="x > 0 ?")) |
| 647 |
module.add(VCndMaskB32(dst=self.vgprPrefix(vgprOut), src0=vgpr(Holder(idx=vgprTemp)), src1=self.vgprPrefix(vgprIn), \ |
| 648 |
sdwa=SDWAModifiers(dst_sel=select_bit, dst_unused=UnusedBit.UNUSED_PRESERVE, \ |
| 649 |
src0_sel=select_bit, src1_sel=select_bit), \ |
| 650 |
comment="set x to tmp if < 0")) |
| 651 |
elif cDataType.isSingle(): |
| 652 |
vgprTemp = self.getVgpr(1) |
| 653 |
module.add(VMulF32(dst=vgpr(Holder(idx=vgprTemp)), src0=sgpr(activationAlpha), src1=self.vgprPrefix(vgprIn), comment="tmp = x * alpha")) |
| 654 |
module.add(VCmpGEF32(dst=VCC(), src0=self.vgprPrefix(vgprIn), src1=0.0, comment="x >= 0 ?")) |
| 655 |
module.add(VCndMaskB32(dst=self.vgprPrefix(vgprOut), src0=vgpr(Holder(idx=vgprTemp)), src1=self.vgprPrefix(vgprIn), comment="set x to tmp if < 0")) |
| 656 |
elif cDataType.isDouble(): |
| 657 |
vgprTemp = self.getVgpr(2) |
| 658 |
module.add(VMulF64(dst=vgpr(Holder(idx=vgprTemp), 2), src0=sgpr(activationAlpha, 2), src1=self.vgprPrefix(vgprIn, 2), comment="tmp = x * alpha")) |
| 659 |
module.add(VCmpGEF64(dst=VCC(), src0=self.vgprPrefix(vgprIn, 2), src1=0.0, comment="x >= 0 ?")) |
| 660 |
module.add(VCndMaskB32(dst=self.vgprPrefix(vgprOut), src0=vgpr(Holder(idx=vgprTemp)), src1=self.vgprPrefix(vgprIn), comment="set x to tmp if < 0")) |
| 661 |
module.add(VCndMaskB32(dst=self.vgprPrefix(vgprOut+1), src0=vgpr(Holder(idx=vgprTemp+1)), src1=self.vgprPrefix(vgprIn+1), comment="set x to tmp if < 0")) |
| 662 |
elif cDataType.isInt32(): |
| 663 |
vgprTemp = self.getVgpr(1) |
| 664 |
module.add(VMulLOU32(dst=vgpr(Holder(idx=vgprTemp)), src0=sgpr(activationAlpha), src1=self.vgprPrefix(vgprIn), comment="tmp = x * alpha")) |
| 665 |
module.add(VCmpGEI32(dst=VCC(), src0=self.vgprPrefix(vgprIn), src1=0, comment="x >= 0 ?")) |
| 666 |
module.add(VCndMaskB32(dst=self.vgprPrefix(vgprOut), src0=vgpr(Holder(idx=vgprTemp)), src1=self.vgprPrefix(vgprIn), comment="set x to tmp if < 0")) |
| 667 |
else: |
| 668 |
raise RuntimeError("Unsupported data type %s."%cDataType.toDevice("HIP")) |
| 669 |
return module |
| 670 |
|
| 671 |
def getReluModule(self, cDataType, vgprIn, vgprOut): |
| 672 |
module = Module("LeakyRelu") |
| 673 |
if cDataType.isHalf(): |
| 674 |
module.add(VMaxPKF16(dst=self.vgprPrefix(vgprOut), src0=self.vgprPrefix(vgprIn), src1=0, comment="x = max(0, x)" )) |
| 675 |
elif cDataType.isSingle(): |
| 676 |
module.add(VMaxF32(dst=self.vgprPrefix(vgprOut), src0=self.vgprPrefix(vgprIn), src1=0, comment="x = max(0, x)" )) |
| 677 |
elif cDataType.isDouble(): |
| 678 |
module.add(VMaxF64(dst=self.vgprPrefix(vgprOut, 2), src0=self.vgprPrefix(vgprIn, 2), src1=0, comment="x = max(0, x)" )) |
| 679 |
elif cDataType.isInt32(): |
| 680 |
if self.saturateI8: |
| 681 |
vgprTemp = self.getVgpr(1) |
| 682 |
module.add(VMovB32(dst=vgpr(Holder(idx=vgprTemp)), src=hex(127), comment="value = 127")) |
| 683 |
module.add(VMed3I32(dst=self.vgprPrefix(vgprOut), src0=self.vgprPrefix(vgprIn), src1=0, src2=vgpr(Holder(idx=vgprTemp)), comment="x = min(127, max(0, x))" )) |
| 684 |
else: |
| 685 |
module.add(VMaxI32(dst=self.vgprPrefix(vgprOut), src0=self.vgprPrefix(vgprIn), src1=0, comment="x = max(0, x)" )) |
| 686 |
else: |
| 687 |
raise RuntimeError("Unsupported data type %s."%cDataType.toDevice("HIP")) |
| 688 |
return module |
| 689 |
|
| 690 |
def getSigmoidModule(self, cDataType, vgprIn, vgprOut): |
| 691 |
ti = rocIsa.getInstance() |
| 692 |
self.needCombine = True |
| 693 |
module = Module("Sigmoid") |
| 694 |
if cDataType.isHalf(): |
| 695 |
if self.usePK: |
| 696 |
module.add(VMulPKF16(dst=self.vgprPrefix(vgprOut), src0=-1.0, src1=self.vgprPrefix(vgprIn), comment=" x = -x")) |
| 697 |
module.add(self.getExpModule(cDataType, vgprOut, vgprOut)) |
| 698 |
module.add(VAddPKF16(dst=self.vgprPrefix(vgprOut), src0=1.0, src1=self.vgprPrefix(vgprOut), \ |
| 699 |
vop3=VOP3PModifiers(op_sel_hi=[0,1,1]), comment="1 + exp(-x)")) |
| 700 |
for i in range(0, 2): |
| 701 |
select_bit = SelectBit.WORD_0 if i == 0 else SelectBit.WORD_1 |
| 702 |
module.add(VRcpF16(dst=self.vgprPrefix(vgprOut), src=self.vgprPrefix(vgprOut), \ |
| 703 |
sdwa=SDWAModifiers(dst_sel=select_bit, dst_unused=UnusedBit.UNUSED_PRESERVE, src0_sel=select_bit), \ |
| 704 |
comment="1 / (1 + exp(-x))")) |
| 705 |
if ti.getArchCaps()["TransOpWait"]: |
| 706 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 707 |
else: |
| 708 |
module.add(VMulF16(dst=self.vgprPrefix(vgprOut), src0=-1.0, src1=self.vgprPrefix(vgprIn), comment=" x = -x")) |
| 709 |
module.add(self.getExpModule(cDataType, vgprOut, vgprOut)) |
| 710 |
module.add(VAddF16(dst=self.vgprPrefix(vgprOut), src0=1.0, src1=self.vgprPrefix(vgprOut), comment="1 + exp(-x)")) |
| 711 |
module.add(VRcpF16(dst=self.vgprPrefix(vgprOut), src=self.vgprPrefix(vgprOut), comment="1 / (1 + exp(-x))")) |
| 712 |
if ti.getArchCaps()["TransOpWait"]: |
| 713 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 714 |
elif cDataType.isSingle(): |
| 715 |
module.add(VMulF32(dst=self.vgprPrefix(vgprOut), src0=-1.0, src1=self.vgprPrefix(vgprIn), comment=" x = -x")) |
| 716 |
module.add(self.getExpModule(cDataType, vgprOut, vgprOut)) |
| 717 |
module.add(VAddF32(dst=self.vgprPrefix(vgprOut), src0=1.0, src1=self.vgprPrefix(vgprOut), comment="1 + exp(-x)" )) |
| 718 |
module.add(VRcpF32(dst=self.vgprPrefix(vgprOut), src=self.vgprPrefix(vgprOut), comment="1 / (1 + exp(-x))" )) |
| 719 |
if ti.getArchCaps()["TransOpWait"]: |
| 720 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 721 |
else: |
| 722 |
raise RuntimeError("Unsupported data type %s."%cDataType.toDevice("HIP")) |
| 723 |
return module |
| 724 |
|
| 725 |
def getTanhModule(self, cDataType, vgprIn, vgprOut, activationAlpha, activationBeta): |
| 726 |
ti = rocIsa.getInstance() |
| 727 |
self.needCombine = True |
| 728 |
module = Module("Tanh") |
| 729 |
if cDataType.isHalf(): |
| 730 |
|
| 731 |
if self.usePK: |
| 732 |
if activationAlpha: |
| 733 |
module.add(VMulPKF16(dst=self.vgprPrefix(vgprOut), src0=sgpr(activationAlpha), src1=self.vgprPrefix(vgprIn), comment="x * alpha")) |
| 734 |
module.add(VMulPKF16(dst=self.vgprPrefix(vgprOut), src0=2, src1=self.vgprPrefix(vgprOut), comment=" x = 2 * x")) |
| 735 |
else: |
| 736 |
module.add(VMulPKF16(dst=self.vgprPrefix(vgprOut), src0=2, src1=self.vgprPrefix(vgprIn), comment=" x = 2 * x")) |
| 737 |
module.add(self.getExpModule(cDataType, vgprOut, vgprOut)) |
| 738 |
module.add(VAddPKF16(dst=self.vgprPrefix(vgprOut), src0=1.0, src1=self.vgprPrefix(vgprOut), \ |
| 739 |
vop3=VOP3PModifiers(op_sel_hi=[0,1,1]), comment="e^2x + 1")) |
| 740 |
for i in range(0, 2): |
| 741 |
select_bit = SelectBit.WORD_0 if i == 0 else SelectBit.WORD_1 |
| 742 |
vgprCtrl = "dst_sel:WORD_%d dst_unused:UNUSED_PRESERVE src0_sel:WORD_%d"%(i, i) |
| 743 |
module.add(VRcpF16(dst=self.vgprPrefix(vgprOut), src=self.vgprPrefix(vgprOut), \ |
| 744 |
sdwa=SDWAModifiers(dst_sel=select_bit, dst_unused=UnusedBit.UNUSED_PRESERVE, \ |
| 745 |
src0_sel=select_bit), \ |
| 746 |
comment="1 / (1 + exp(-x))")) |
| 747 |
if ti.getArchCaps()["TransOpWait"]: |
| 748 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 749 |
module.add(VFmaPKF16(dst=self.vgprPrefix(vgprOut), src0=-2.0, src1=self.vgprPrefix(vgprOut), src2=1.0, \ |
| 750 |
vop3=VOP3PModifiers(op_sel_hi=[0,1,0,1]), comment="tanh(x) = (1 / (e^2x + 1)) * (-2) + 1")) |
| 751 |
if activationBeta: |
| 752 |
module.add(VMulPKF16(dst=self.vgprPrefix(vgprOut), src0=sgpr(activationBeta), src1=self.vgprPrefix(vgprOut), comment="beta * tanh(x)")) |
| 753 |
else: |
| 754 |
if activationAlpha: |
| 755 |
module.add(VMulF16(dst=self.vgprPrefix(vgprOut), src0=sgpr(activationAlpha), src1=self.vgprPrefix(vgprIn), comment="x * alpha")) |
| 756 |
module.add(VMulF16(dst=self.vgprPrefix(vgprOut), src0=2, src1=self.vgprPrefix(vgprOut), comment=" x = 2 * x")) |
| 757 |
else: |
| 758 |
module.add(VMulF16(dst=self.vgprPrefix(vgprOut), src0=2, src1=self.vgprPrefix(vgprIn), comment=" x = 2 * x")) |
| 759 |
module.add(self.getExpModule(cDataType, vgprOut, vgprOut)) |
| 760 |
module.add(VAddF16(dst=self.vgprPrefix(vgprOut), src0=1.0, src1=self.vgprPrefix(vgprOut), comment="e^2x + 1")) |
| 761 |
module.add(VRcpF16(dst=self.vgprPrefix(vgprOut), src=self.vgprPrefix(vgprOut), comment="1 / (1 + exp(-x))")) |
| 762 |
if ti.getArchCaps()["TransOpWait"]: |
| 763 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 764 |
module.add(VFmaF16(dst=self.vgprPrefix(vgprOut), src0=-2.0, src1=self.vgprPrefix(vgprOut), src2=1.0, comment="tanh(x) = (1 / (e^2x + 1)) * (-2) + 1")) |
| 765 |
if activationBeta: |
| 766 |
module.add(VMulF16(dst=self.vgprPrefix(vgprOut), src0=sgpr(activationBeta), src1=self.vgprPrefix(vgprOut), comment="beta * tanh(x)")) |
| 767 |
elif cDataType.isSingle(): |
| 768 |
if activationAlpha: |
| 769 |
module.add(VMulF32(dst=self.vgprPrefix(vgprOut), src0=sgpr(activationAlpha), src1=self.vgprPrefix(vgprIn), comment="x * alpha")) |
| 770 |
module.add(VMulF32(dst=self.vgprPrefix(vgprOut), src0=2, src1=self.vgprPrefix(vgprOut), comment=" x = 2 * x")) |
| 771 |
else: |
| 772 |
module.add(VMulF32(dst=self.vgprPrefix(vgprOut), src0=2, src1=self.vgprPrefix(vgprIn), comment=" x = 2 * x")) |
| 773 |
module.add(self.getExpModule(cDataType, vgprOut, vgprOut)) |
| 774 |
module.add(VAddF32(dst=self.vgprPrefix(vgprOut), src0=1.0, src1=self.vgprPrefix(vgprOut), comment="e^2x + 1")) |
| 775 |
module.add(VRcpF32(dst=self.vgprPrefix(vgprOut), src=self.vgprPrefix(vgprOut), comment="1 / (e^2x + 1)")) |
| 776 |
if ti.getArchCaps()["TransOpWait"]: |
| 777 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 778 |
module.add(VFmaF32(dst=self.vgprPrefix(vgprOut), src0=-2.0, src1=self.vgprPrefix(vgprOut), src2=1.0, comment="(-2) * (1 / (e^2x + 1)) + 1")) |
| 779 |
if activationBeta: |
| 780 |
module.add(VMulF32(dst=self.vgprPrefix(vgprOut), src0=sgpr(activationBeta), src1=self.vgprPrefix(vgprOut), comment="beta * tanh(x)")) |
| 781 |
else: |
| 782 |
raise RuntimeError("Unsupported data type %s."%cDataType.toDevice("HIP")) |
| 783 |
return module |
| 784 |
|
| 785 |
def getDGeluModule(self, cDataType, vgprIn, vgprOut): |
| 786 |
ti = rocIsa.getInstance() |
| 787 |
self.needCombine = True |
| 788 |
module = Module("Gradient Gelu") |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
if cDataType.isSingle(): |
| 794 |
vgprTemp1 = self.getVgpr(1) |
| 795 |
vgprTemp2 = self.getVgpr(1) |
| 796 |
vgprTemp3 = self.getVgpr(1) |
| 797 |
sgprTemp = self.getSgpr(1) |
| 798 |
module.add(VMulF32(dst=vgpr(Holder(idx=vgprTemp1)), src0=self.vgprPrefix(vgprIn), src1=self.vgprPrefix(vgprIn), comment="tmp1 = pow(x * 2)")) |
| 799 |
module.add(VMulF32(dst=vgpr(Holder(idx=vgprTemp1)), src0=vgpr(Holder(idx=vgprTemp1)), src1=self.vgprPrefix(vgprIn), comment="tmp1 = pow(x * 3)")) |
| 800 |
coef = floatUnion(u=ActivationMagicNumbers["FloatDGeluK1"]) |
| 801 |
module.add(VMulF32(dst=vgpr(Holder(idx=vgprTemp2)), src0=hex(coef.u), src1=self.vgprPrefix(vgprIn), comment="tmp2 = 0.398942 * x")) |
| 802 |
coef = floatUnion(u=ActivationMagicNumbers["FloatDGeluK0"]) |
| 803 |
module.add(SMovB32(dst=sgpr(Holder(idx=sgprTemp)), src=hex(coef.u), comment="move magic number to sgpr")) |
| 804 |
module.add(VFmaF32(dst=vgpr(Holder(idx=vgprTemp2)), src0=sgpr(Holder(idx=sgprTemp)), src1=vgpr(Holder(idx=vgprTemp1)), src2=vgpr(Holder(idx=vgprTemp2)), comment="tmp2 = 0.0535161 * x^3 + tmp2")) |
| 805 |
coef = floatUnion(u=ActivationMagicNumbers["FloatDGeluK3"]) |
| 806 |
module.add(VMulF32(dst=vgpr(Holder(idx=vgprTemp3)), src0=hex(coef.u), src1=self.vgprPrefix(vgprIn), comment="tmp3 = 0.797885 * x")) |
| 807 |
coef = floatUnion(u=ActivationMagicNumbers["FloatDGeluK2"]) |
| 808 |
module.add(SMovB32(dst=sgpr(Holder(idx=sgprTemp)), src=hex(coef.u), comment="move magic number to sgpr")) |
| 809 |
module.add(VFmaF32(dst=vgpr(Holder(idx=vgprTemp1)), src0=sgpr(Holder(idx=sgprTemp)), src1=vgpr(Holder(idx=vgprTemp1)), src2=vgpr(Holder(idx=vgprTemp3)), comment="tmp1 = 0.035677 * x^3 + tmp3")) |
| 810 |
module.add(self.getExpModule(cDataType, Holder(idx=vgprTemp1), Holder(idx=vgprTemp3))) |
| 811 |
if self.isAlt: |
| 812 |
module.add(VMulF32(dst=vgpr(Holder(idx=vgprTemp1)), src0=-1.0, src1=vgpr(Holder(idx=vgprTemp1)), comment="tmp1 = -tmp1")) |
| 813 |
module.add(self.getExpModule(cDataType, Holder(idx=vgprTemp1), Holder(idx=vgprTemp1))) |
| 814 |
module.add(VAddF32(dst=self.vgprPrefix(vgprOut), src0=vgpr(Holder(idx=vgprTemp3)), src1=vgpr(Holder(idx=vgprTemp1)), comment="out = e^xx + e^-xx")) |
| 815 |
module.add(VSubF32(dst=vgpr(Holder(idx=vgprTemp1)), src0=vgpr(Holder(idx=vgprTemp3)), src1=vgpr(Holder(idx=vgprTemp1)), comment="tmp1 = e^xx - e^-xx")) |
| 816 |
module.add(VRcpF32(dst=vgpr(Holder(idx=vgprTemp3)), src=self.vgprPrefix(vgprOut), comment="tmp3 = 1/out")) |
| 817 |
if ti.getArchCaps()["TransOpWait"]: |
| 818 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 819 |
module.add(VMulF32(dst=vgpr(Holder(idx=vgprTemp3)), src0=vgpr(Holder(idx=vgprTemp1)), src1=vgpr(Holder(idx=vgprTemp3)), comment="tmp3 = tmp1 * tmp3")) |
| 820 |
if self.enableGuard: |
| 821 |
module.add(SMovB32(dst=sgpr(Holder(idx=sgprTemp)), src=hex(0x200), comment="move magic number to sgpr")) |
| 822 |
module.add(VCmpXClassF32(dst=EXEC(), src0=self.vgprPrefix(vgprOut), src1=sgpr(Holder(idx=sgprTemp)), comment="True if tmp1 = inf")) |
| 823 |
module.add(VMovB32(dst=vgpr(Holder(idx=vgprTemp3)), src=hex(0x3f800000), comment="tmp3 = 1 if True")) |
| 824 |
module.add(VCmpXLtF32(dst=EXEC(), src0=vgpr(Holder(idx=vgprTemp2)), src1=0, comment="check if x < 0" )) |
| 825 |
module.add(VMovB32(dst=vgpr(Holder(idx=vgprTemp3)), src=hex(0xbf800000), comment="tmp3 = -1 if True")) |
| 826 |
module.add(SSetMask(dst=EXEC(), src=-1, comment="reset mask" )) |
| 827 |
module.add(VMulF32(dst=vgpr(Holder(idx=vgprTemp1)), src0=0.5, src1=vgpr(Holder(idx=vgprTemp3)), comment="tmp1 = 0.5 * tmp1")) |
| 828 |
module.add(VMulF32(dst=self.vgprPrefix(vgprOut), src0=self.vgprPrefix(vgprOut), src1=self.vgprPrefix(vgprOut), comment="out = out * out")) |
| 829 |
module.add(VRcpF32(dst=self.vgprPrefix(vgprOut), src=self.vgprPrefix(vgprOut), comment="out = 1/out")) |
| 830 |
if ti.getArchCaps()["TransOpWait"]: |
| 831 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 832 |
else: |
| 833 |
module.add(self.getTanhModule(cDataType, Holder(idx=vgprTemp1), vgprOut, "", "")) |
| 834 |
module.add(VMulF32(dst=vgpr(Holder(idx=vgprTemp1)), src0=-1.0, src1=vgpr(Holder(idx=vgprTemp1)), comment="tmp1 = -tmp1")) |
| 835 |
module.add(self.getExpModule(cDataType, Holder(idx=vgprTemp1), Holder(idx=vgprTemp1))) |
| 836 |
module.add(VAddF32(dst=vgpr(Holder(idx=vgprTemp3)), src0=vgpr(Holder(idx=vgprTemp3)), src1=vgpr(Holder(idx=vgprTemp1)), comment="out = e^xx + e^-xx")) |
| 837 |
module.add(VMulF32(dst=vgpr(Holder(idx=vgprTemp1)), src0=0.5, src1=self.vgprPrefix(vgprOut), comment="tmp1 = 0.5 * tmp1")) |
| 838 |
module.add(VMulF32(dst=vgpr(Holder(idx=vgprTemp3)), src0=vgpr(Holder(idx=vgprTemp3)), src1=vgpr(Holder(idx=vgprTemp3)), comment="out = out * out")) |
| 839 |
module.add(VRcpF32(dst=self.vgprPrefix(vgprOut), src=vgpr(Holder(idx=vgprTemp3)), comment="out = 1/out")) |
| 840 |
if ti.getArchCaps()["TransOpWait"]: |
| 841 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 842 |
coef = floatUnion(f=4) |
| 843 |
module.add(VMulF32(dst=self.vgprPrefix(vgprOut), src0=hex(coef.u), src1=self.vgprPrefix(vgprOut), comment="out = 4 * out")) |
| 844 |
module.add(VFmaF32(dst=self.vgprPrefix(vgprOut), src0=self.vgprPrefix(vgprOut), src1=vgpr(Holder(idx=vgprTemp2)), src2=vgpr(Holder(idx=vgprTemp1)), comment="out = out * tmp2 + tmp1")) |
| 845 |
module.add(VAddF32(dst=self.vgprPrefix(vgprOut), src0=0.5, src1=self.vgprPrefix(vgprOut), comment="out = out + 0.5")) |
| 846 |
else: |
| 847 |
raise RuntimeError("Unsupported data type %s."%cDataType.toDevice("HIP")) |
| 848 |
return module |
| 849 |
|
| 850 |
def getSiluModule(self, cDataType, vgprIn, vgprOut): |
| 851 |
self.needCombine = True |
| 852 |
module = Module("Silu") |
| 853 |
vgprTemp = self.getVgpr(1) |
| 854 |
module.addModuleAsFlatItems(self.getSigmoidModule(cDataType, vgprIn, Holder(idx=vgprTemp))) |
| 855 |
if cDataType.isHalf(): |
| 856 |
if self.usePK: |
| 857 |
mulFunction = VMulPKF16 |
| 858 |
else: |
| 859 |
mulFunction = VMulF16 |
| 860 |
elif cDataType.isSingle(): |
| 861 |
mulFunction = VMulF32 |
| 862 |
else: |
| 863 |
raise RuntimeError("Unsupported data type %s."%cDataType.toDevice("HIP")) |
| 864 |
module.add(mulFunction(dst=self.vgprPrefix(vgprOut), src0=self.vgprPrefix(vgprIn), src1=self.vgprPrefix(Holder(idx=vgprTemp)), comment="x / (1 + exp(-x))")) |
| 865 |
return module |
| 866 |
|
| 867 |
def getSwishModule(self, cDataType, vgprIn, vgprOut, activationAlpha): |
| 868 |
self.needCombine = True |
| 869 |
module = Module("Swish") |
| 870 |
if cDataType.isHalf(): |
| 871 |
if self.usePK: |
| 872 |
mulFunction = VMulPKF16 |
| 873 |
else: |
| 874 |
mulFunction = VMulF16 |
| 875 |
elif cDataType.isSingle(): |
| 876 |
mulFunction = VMulF32 |
| 877 |
else: |
| 878 |
raise RuntimeError("Unsupported data type %s."%cDataType.toDevice("HIP")) |
| 879 |
vgprTempIn = self.getVgpr(1) |
| 880 |
vgprTempOut = self.getVgpr(1) |
| 881 |
module.add(mulFunction(dst=self.vgprPrefix(Holder(idx=vgprTempIn)), src0=self.vgprPrefix(vgprIn), src1=sgpr(activationAlpha), comment="x * beta")) |
| 882 |
module.addModuleAsFlatItems(self.getSigmoidModule(cDataType, Holder(idx=vgprTempIn), Holder(idx=vgprTempOut))) |
| 883 |
module.add(mulFunction(dst=self.vgprPrefix(vgprOut), src0=self.vgprPrefix(vgprIn), src1=self.vgprPrefix(Holder(idx=vgprTempOut)), comment="x / (1 + exp(-x * beta))")) |
| 884 |
return module |
| 885 |
|
| 886 |
def getClampModule(self, cDataType, vgprIn, vgprOut, activationAlpha, activationBeta): |
| 887 |
module = Module("Clamp") |
| 888 |
if cDataType.isDouble(): |
| 889 |
Vin, Vout = self.vgprPrefix(vgprIn, 2), self.vgprPrefix(vgprOut, 2) |
| 890 |
alpha, beta = sgpr(activationAlpha, 2), sgpr(activationBeta, 2) |
| 891 |
else: |
| 892 |
Vin, Vout = self.vgprPrefix(vgprIn), self.vgprPrefix(vgprOut) |
| 893 |
alpha, beta = sgpr(activationAlpha), sgpr(activationBeta) |
| 894 |
if cDataType.isHalf(): |
| 895 |
MIN, MAX = VMinF16, VMaxF16 |
| 896 |
elif cDataType.isSingle(): |
| 897 |
MIN, MAX = VMinF32, VMaxF32 |
| 898 |
elif cDataType.isDouble(): |
| 899 |
MIN, MAX = VMinF64, VMaxF64 |
| 900 |
elif cDataType.isInt32(): |
| 901 |
MIN, MAX = VMinI32, VMaxI32 |
| 902 |
|
| 903 |
if cDataType.isHalf(): |
| 904 |
for i in range(0, 2): |
| 905 |
select_bit = SelectBit.WORD_0 if i == 0 else SelectBit.WORD_1 |
| 906 |
sdwa = SDWAModifiers(dst_sel = select_bit, dst_unused = UnusedBit.UNUSED_PRESERVE, |
| 907 |
src0_sel = select_bit, src1_sel = select_bit) |
| 908 |
module.add(MIN(dst = Vout, src0 = beta, src1 = Vin, sdwa = sdwa, comment = "min(x, beta)")) |
| 909 |
module.add(MAX(dst = Vout, src0 = alpha, src1 = Vout, sdwa = sdwa, comment = "max(alpha, min(x, beta))")) |
| 910 |
else: |
| 911 |
module.add(MIN(dst = Vout, src0 = beta, src1 = Vin, comment = "min(x, beta)")) |
| 912 |
module.add(MAX(dst = Vout, src0 = alpha, src1 = Vout, comment = "max(alpha, min(x, beta))")) |
| 913 |
return module |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
def createCache(self, cDataType: DataType, activationType: str, vgprIn, vgprOut, module: Module): |
| 924 |
typeChar = cDataType.toChar() |
| 925 |
if activationType not in self.cacheDict: |
| 926 |
self.cacheDict[activationType] = {} |
| 927 |
actDict = self.cacheDict[activationType] |
| 928 |
copied = deepcopy(module) |
| 929 |
|
| 930 |
regName = self.vgprPrefixFormat.split("+")[0] if self.vgprPrefixFormat else "" |
| 931 |
vgprIdxList = createVgprIdxList(copied, [vgprIn, vgprOut], regName) |
| 932 |
actInfo = actCacheInfo(usePK=self.usePK, saturateI8=self.saturateI8, enableGuard=self.enableGuard, \ |
| 933 |
isAlt=self.isAlt, prefix=self.vgprPrefixFormat, vgprIdxList=vgprIdxList, module=copied, \ |
| 934 |
vgprCounter=self.vgprCounter, sgprCounter=self.sgprCounter) |
| 935 |
if typeChar in actDict: |
| 936 |
actDict[typeChar].append(actInfo) |
| 937 |
else: |
| 938 |
actDict[typeChar] = [actInfo] |
| 939 |
|
| 940 |
def getCache(self, cDataType: DataType, activationType: str, vgprIn, vgprOut) -> Union[Module, None]: |
| 941 |
if activationType in self.cacheDict: |
| 942 |
actDict = self.cacheDict[activationType] |
| 943 |
typeChar = cDataType.toChar() |
| 944 |
if typeChar in actDict: |
| 945 |
actInfoList = actDict[typeChar] |
| 946 |
for actInfo in actInfoList: |
| 947 |
if actInfo.isSame(usePK=self.usePK, saturateI8=self.saturateI8, isAlt=self.isAlt, \ |
| 948 |
enableGuard=self.enableGuard, prefix=self.vgprPrefixFormat): |
| 949 |
if self.vgprPrefixFormat: |
| 950 |
for vgpr in actInfo.vgprIdxList[0]: |
| 951 |
vgpr.regName.setOffset(0, vgprIn) |
| 952 |
for vgpr in actInfo.vgprIdxList[1]: |
| 953 |
vgpr.regName.setOffset(0, vgprOut) |
| 954 |
else: |
| 955 |
for vgpr in actInfo.vgprIdxList[0]: |
| 956 |
vgpr.regIdx = vgprIn |
| 957 |
for vgpr in actInfo.vgprIdxList[1]: |
| 958 |
vgpr.regIdx = vgprOut |
| 959 |
self.vgprCounter = actInfo.vgprCounter |
| 960 |
self.sgprCounter = actInfo.sgprCounter |
| 961 |
return deepcopy(actInfo.module) |
| 962 |
return None |
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
__FUSE_MAGIC_NAME__ = "dbiw@I$HONIhnjf4_fused" |
| 973 |
|
| 974 |
|
| 975 |
def CombineInstructions(module, fuseDebug = False): |
| 976 |
moduleAndIndex = dict() |
| 977 |
CombineInstructionsBetweenModules(module, moduleAndIndex, fuseDebug) |
| 978 |
|
| 979 |
module = RemoveEmptyBlocks(module) |
| 980 |
return module |
| 981 |
|
| 982 |
|
| 983 |
def CombineInstructionsBetweenModules(module, moduleAndIndex, fuseDebug): |
| 984 |
index = 0 |
| 985 |
while index < len(module.items()): |
| 986 |
item = module.items()[index] |
| 987 |
if isinstance(item, Module): |
| 988 |
CombineInstructionsBetweenModules(item, moduleAndIndex, fuseDebug) |
| 989 |
index = module.items().index(item) |
| 990 |
elif isinstance(item, SNop): |
| 991 |
pass |
| 992 |
elif isinstance(item, Instruction): |
| 993 |
newItem = item |
| 994 |
if moduleAndIndex: |
| 995 |
newItem = FuseInstruction(item, moduleAndIndex, fuseDebug) |
| 996 |
index = module.items().index(newItem) |
| 997 |
if isinstance(newItem.dst, RegisterContainer): |
| 998 |
|
| 999 |
moduleAndIndex[newItem.dst] = newItem |
| 1000 |
index += 1 |
| 1001 |
|
| 1002 |
def RemoveEmptyBlocks(module): |
| 1003 |
for idx, item in enumerate(module.items()): |
| 1004 |
if isinstance(item, Module): |
| 1005 |
newItem = RemoveEmptyBlocks(item) |
| 1006 |
module.setItem(idx, newItem) |
| 1007 |
if len(module.items()) == 1 and isinstance(module.items()[0], Module): |
| 1008 |
return module.items()[0] |
| 1009 |
return module |
| 1010 |
|
| 1011 |
def FuseInstruction(currentInst, moduleAndIndex, fuseDebug): |
| 1012 |
assert isinstance(currentInst, Instruction) |
| 1013 |
newInst = None |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
if type(currentInst) in {VAddF16, VAddPKF16, VAddF32}: |
| 1020 |
isPK = type(currentInst) is VAddPKF16 |
| 1021 |
outVgpr = currentInst.dst |
| 1022 |
addConst = "" |
| 1023 |
isSelfAddConst = False |
| 1024 |
for param in currentInst.srcs: |
| 1025 |
if param == outVgpr: |
| 1026 |
isSelfAddConst = True |
| 1027 |
if isinstance(param, (float, int)): |
| 1028 |
if param == 1: |
| 1029 |
addConst = param |
| 1030 |
|
| 1031 |
if isSelfAddConst and addConst: |
| 1032 |
oldInst = moduleAndIndex.get(outVgpr) |
| 1033 |
if isinstance(oldInst, Instruction): |
| 1034 |
if currentInst.instType == InstType.INST_F16: |
| 1035 |
func = VFmaPKF16 if isPK else VFmaF16 |
| 1036 |
elif currentInst.instType == InstType.INST_F32: |
| 1037 |
func = VFmaF32 |
| 1038 |
else: |
| 1039 |
assert("You should not reach here.") |
| 1040 |
if type(oldInst) is func and oldInst.srcs[2] == 1.0: |
| 1041 |
|
| 1042 |
|
| 1043 |
if not FindAssignAndUse(oldInst, currentInst, outVgpr, outVgpr): |
| 1044 |
newInst = type(oldInst)(oldInst.dst, *oldInst.srcs, oldInst.sdwa) |
| 1045 |
newInst.setSrc(2, addConst + newInst.srcs[2]) |
| 1046 |
newInst.comment += " ( + 1 (fused))" |
| 1047 |
replaceInst(currentInst, newInst, fuseDebug) |
| 1048 |
removeOldInst(oldInst, currentInst, newInst, fuseDebug) |
| 1049 |
|
| 1050 |
|
| 1051 |
elif type(currentInst) in {VMulF16, VMulPKF16, VMulF32, VMulF64}: |
| 1052 |
isPK = type(currentInst) is VMulPKF16 |
| 1053 |
outVgpr = currentInst.dst |
| 1054 |
mulConst = "" |
| 1055 |
newFuseInst = "" |
| 1056 |
isSelfMulConst = False |
| 1057 |
for param in currentInst.srcs: |
| 1058 |
if param == outVgpr: |
| 1059 |
isSelfMulConst = True |
| 1060 |
|
| 1061 |
if type(param) is RegisterContainer and param.regType == 's': |
| 1062 |
oldInst = moduleAndIndex.get(param) |
| 1063 |
if type(oldInst) is SMovB32: |
| 1064 |
oldparam = oldInst.srcs[0] |
| 1065 |
if oldInst.dst == param and isinstance(oldparam, (float, int)): |
| 1066 |
|
| 1067 |
if not FindUse(oldInst, currentInst, param): |
| 1068 |
mulConst = oldparam |
| 1069 |
newFuseInst = oldInst |
| 1070 |
if isinstance(param, (float, int)): |
| 1071 |
mulConst = param |
| 1072 |
|
| 1073 |
if isSelfMulConst and mulConst: |
| 1074 |
oldInst = moduleAndIndex.get(outVgpr) |
| 1075 |
if isinstance(oldInst, Instruction): |
| 1076 |
if currentInst.instType == InstType.INST_F16: |
| 1077 |
func = VMulPKF16 if isPK else VMulF16 |
| 1078 |
elif currentInst.instType == InstType.INST_F32: |
| 1079 |
func = VMulF32 |
| 1080 |
elif currentInst.instType == InstType.INST_F64: |
| 1081 |
func = VMulF64 |
| 1082 |
else: |
| 1083 |
assert("You should not reach here.") |
| 1084 |
|
| 1085 |
if type(oldInst) is func: |
| 1086 |
|
| 1087 |
|
| 1088 |
if not FindAssignAndUse(oldInst, currentInst, outVgpr, outVgpr): |
| 1089 |
for paramIdx, param in enumerate(oldInst.srcs): |
| 1090 |
if isinstance(param, (float, int)): |
| 1091 |
newInst = type(oldInst)(oldInst.dst, *oldInst.srcs, oldInst.sdwa) |
| 1092 |
newValue = param * mulConst |
| 1093 |
formatting = " (fused %f)" if isinstance(param, float) else " (fused %d)" |
| 1094 |
if newFuseInst: |
| 1095 |
newFuseInst.setSrc(0, newValue) |
| 1096 |
newInst.setSrc(paramIdx, newFuseInst.dst) |
| 1097 |
newFuseInst.comment += formatting%newValue |
| 1098 |
else: |
| 1099 |
newInst.setSrc(paramIdx, newValue) |
| 1100 |
newInst.comment += formatting%newValue |
| 1101 |
replaceInst(currentInst, newInst, fuseDebug) |
| 1102 |
removeOldInst(oldInst, currentInst, newInst, fuseDebug) |
| 1103 |
break |
| 1104 |
return newInst if newInst else currentInst |
| 1105 |
|
| 1106 |
|
| 1107 |
def FindUse(startInst, targetInst, varTarget): |
| 1108 |
_, isUse = FindUseIter(startInst, targetInst, varTarget) |
| 1109 |
return isUse |
| 1110 |
|
| 1111 |
|
| 1112 |
def FindUseIter(startItem, targetInst, varTarget): |
| 1113 |
module = startItem |
| 1114 |
idx = -1 |
| 1115 |
isEnd = False |
| 1116 |
isUse = False |
| 1117 |
if isinstance(startItem, Instruction): |
| 1118 |
module = startItem.parent |
| 1119 |
idx = module.items().index(startItem) |
| 1120 |
assert(isinstance(module, Module)) |
| 1121 |
if idx + 1 < len(module.items()[idx + 1:]): |
| 1122 |
for item in module.items()[idx + 1:]: |
| 1123 |
if item is targetInst: |
| 1124 |
pass |
| 1125 |
elif isinstance(item, SNop): |
| 1126 |
pass |
| 1127 |
elif isinstance(item, Instruction): |
| 1128 |
if item.srcs: |
| 1129 |
for param in item.srcs: |
| 1130 |
if param == varTarget: |
| 1131 |
isEnd = True |
| 1132 |
isUse = True |
| 1133 |
break |
| 1134 |
elif item.dst == varTarget: |
| 1135 |
isEnd = True |
| 1136 |
isUse = False |
| 1137 |
elif isinstance(item, Module): |
| 1138 |
isEnd, isUse = FindUseIter(item, targetInst, varTarget) |
| 1139 |
if isEnd: |
| 1140 |
return isEnd, isUse |
| 1141 |
return False, isUse |
| 1142 |
|
| 1143 |
|
| 1144 |
def FindAssignAndUse(startInst, endInst, assignVar, useVar): |
| 1145 |
_, isUse = FindAssignAndUseIter(startInst, endInst, assignVar, useVar) |
| 1146 |
return isUse |
| 1147 |
|
| 1148 |
|
| 1149 |
def FindAssignAndUseIter(startItem, endInst, assignVar, useVar): |
| 1150 |
module = startItem |
| 1151 |
idx = -1 |
| 1152 |
isEnd = False |
| 1153 |
isUse = False |
| 1154 |
if issubclass(type(startItem), Instruction): |
| 1155 |
module = startItem.parent |
| 1156 |
idx = module.items().index(startItem) |
| 1157 |
assert issubclass(type(module), Module) |
| 1158 |
moduleToIter = module.items()[idx + 1:] |
| 1159 |
if idx + 1 < len(moduleToIter): |
| 1160 |
for item in moduleToIter: |
| 1161 |
|
| 1162 |
itemType = type(item) |
| 1163 |
if item is endInst: |
| 1164 |
isEnd = True |
| 1165 |
elif itemType is SNop: |
| 1166 |
pass |
| 1167 |
elif issubclass(itemType, Instruction): |
| 1168 |
if item.dst == assignVar: |
| 1169 |
isEnd = True |
| 1170 |
isUse = True |
| 1171 |
|
| 1172 |
if item.srcs: |
| 1173 |
for param in item.srcs: |
| 1174 |
if param == useVar: |
| 1175 |
isEnd = True |
| 1176 |
isUse = True |
| 1177 |
break |
| 1178 |
elif issubclass(itemType, Module): |
| 1179 |
isEnd, isUse = FindAssignAndUseIter(item, endInst, assignVar, useVar) |
| 1180 |
if isEnd: |
| 1181 |
return isEnd, isUse |
| 1182 |
return isEnd, isUse |
| 1183 |
|
| 1184 |
def removeOldInst(removeInst, dstInst, fusedInst, debug): |
| 1185 |
module = removeInst.parent |
| 1186 |
targetIdx = -1 |
| 1187 |
for idx, item in enumerate(module.items()): |
| 1188 |
if item == removeInst: |
| 1189 |
if debug: |
| 1190 |
tb = TextBlock("\n/* Fused to block %s + %s -> %s */\n"%(str(removeInst), str(dstInst), str(fusedInst))) |
| 1191 |
tb.name = __FUSE_MAGIC_NAME__ |
| 1192 |
module.setItem(idx, tb) |
| 1193 |
else: |
| 1194 |
targetIdx = idx |
| 1195 |
break |
| 1196 |
|
| 1197 |
if targetIdx > -1: |
| 1198 |
module.removeItemByIndex(targetIdx) |
| 1199 |
|
| 1200 |
def replaceInst(srcInst, dstInst, debug): |
| 1201 |
module = srcInst.parent |
| 1202 |
targetIdx = -1 |
| 1203 |
for idx, item in enumerate(module.items()): |
| 1204 |
if item == srcInst: |
| 1205 |
if debug: |
| 1206 |
dstInst.comment += " (Block replaced %s)"%(str(srcInst)) |
| 1207 |
targetIdx = idx |
| 1208 |
break |
| 1209 |
if targetIdx > -1: |
| 1210 |
module.replaceItemByIndex(targetIdx, dstInst) |
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
def getMagic(cDataType, value, isPack=False): |
| 1221 |
if cDataType.isDouble(): |
| 1222 |
printExit("Currently magic does not support double.") |
| 1223 |
elif cDataType.isHalf(): |
| 1224 |
fu = value if isinstance(value, floatUnion) else floatUnion(f=value) |
| 1225 |
magicNum = struct.unpack('<H', struct.pack('<e', fu.f))[0] |
| 1226 |
if isPack: |
| 1227 |
magicNum = ctypes.c_uint(magicNum).value |
| 1228 |
magicNum = ((magicNum << 16) | magicNum) |
| 1229 |
elif cDataType.isSingle(): |
| 1230 |
fu = value if isinstance(value, floatUnion) else floatUnion(f=value) |
| 1231 |
magicNum = fu.u |
| 1232 |
return hex(magicNum) |
| 1233 |
|
| 1234 |
def getMagicStr(cDataType, value, isPack=False): |
| 1235 |
return str(getMagic(cDataType, value, isPack)) |
| 1236 |
|
| 1237 |
def HexToStr(cDataType, isPack, *args): |
| 1238 |
if len(args) == 1: |
| 1239 |
magicNum = args[0] |
| 1240 |
uint32 = ctypes.c_uint(magicNum).value |
| 1241 |
if isPack and cDataType.isHalf(): |
| 1242 |
uint32 = ((uint32 << 16) | uint32) |
| 1243 |
hexStr = str(hex(uint32)) |
| 1244 |
else: |
| 1245 |
raise RuntimeError("Currently does not support multiple args.") |
| 1246 |
return hexStr |
| 1247 |
|
| 1248 |
def ConvertCoeffToHex(module, cDataType, isPack): |
| 1249 |
if (module.name == "Exp"): |
| 1250 |
param = module.getItem(0).srcs[0] |
| 1251 |
module.getItem(0).setSrc(0, getMagic(cDataType, param, isPack)) |
| 1252 |
return module |
| 1253 |
for itemIdx, item in enumerate(module.items()): |
| 1254 |
if isinstance(item, Module): |
| 1255 |
newItem = ConvertCoeffToHex(item, cDataType, isPack) |
| 1256 |
module.setItem(itemIdx, newItem) |
| 1257 |
return module |
| 1258 |
|
| 1259 |
def HolderToGpr(module, idx, pf): |
| 1260 |
for itemIdx, item in enumerate(module.items()): |
| 1261 |
if isinstance(item, Module): |
| 1262 |
newItem = HolderToGpr(item, idx, pf) |
| 1263 |
module.setItem(itemIdx, newItem) |
| 1264 |
elif isinstance(item, SNop): |
| 1265 |
pass |
| 1266 |
elif isinstance(item, Instruction): |
| 1267 |
if isinstance(item.dst, HolderContainer) and item.dst.regType == pf: |
| 1268 |
item.dst.setRegNum(idx) |
| 1269 |
item.dst = item.dst.getCopiedRC() |
| 1270 |
if item.srcs: |
| 1271 |
for itemIdx, param in enumerate(item.srcs): |
| 1272 |
if isinstance(param, HolderContainer) and param.regType == pf: |
| 1273 |
param.setRegNum(idx) |
| 1274 |
item.setSrc(itemIdx, param.getCopiedRC()) |
| 1275 |
return module |
| 1276 |
|
| 1277 |
def addSpace(alignStr, str): |
| 1278 |
totalLength = len(alignStr) + len(str) |
| 1279 |
return '{message: >{width}}'.format(message=str, width=totalLength) |
| 1280 |
|
| 1281 |
class ActivationInline: |
| 1282 |
def __init__(self, dataType, enableGuard) -> None: |
| 1283 |
self.dataType = dataType |
| 1284 |
self.enableGuard = enableGuard |
| 1285 |
self.asmStr = "asm(" |
| 1286 |
|
| 1287 |
def replaceGpr(self, module): |
| 1288 |
for item in module.items(): |
| 1289 |
if isinstance(item, SNop): |
| 1290 |
pass |
| 1291 |
elif isinstance(item, Instruction): |
| 1292 |
if isinstance(item.dst, RegisterContainer): |
| 1293 |
if not item.dst.regName: |
| 1294 |
item.dst.setInlineAsm(True) |
| 1295 |
if item.srcs: |
| 1296 |
for param in item.srcs: |
| 1297 |
if isinstance(param, RegisterContainer): |
| 1298 |
if not param.regName: |
| 1299 |
param.setInlineAsm(True) |
| 1300 |
elif isinstance(item, Module): |
| 1301 |
self.replaceGpr(item) |
| 1302 |
|
| 1303 |
def getActivationAsmStr(self, activation, module, spaces): |
| 1304 |
module = activation.postProcess(self.dataType, module) |
| 1305 |
self.replaceGpr(module) |
| 1306 |
activation.assignGpr(module, 0, 0) |
| 1307 |
module.setInlineAsmPrintMode(True) |
| 1308 |
kStr = str(module) |
| 1309 |
newStr = "" |
| 1310 |
for instStr in kStr.split("\n"): |
| 1311 |
if instStr: |
| 1312 |
newStr += spaces + instStr + "\n" |
| 1313 |
return newStr |
| 1314 |
|
| 1315 |
|
| 1316 |
def generateInlineAssemblyBody(self, spaces, activationType): |
| 1317 |
ptrStr = self.dataType.toDevice("HIP") |
| 1318 |
activation = ActivationModule() |
| 1319 |
activation.setGuard(self.enableGuard) |
| 1320 |
activation.setUsePK(False) |
| 1321 |
activation.resetGprCounter() |
| 1322 |
kStr = "" |
| 1323 |
padSpacesStr = ' ' * spaces |
| 1324 |
asm = padSpacesStr + self.asmStr |
| 1325 |
if (activationType == 'abs'): |
| 1326 |
if self.dataType.isHalf() or self.dataType.isBFloat16(): |
| 1327 |
unionDataTypeStr = "_Float16" if self.dataType.isHalf() else "BFloat16" |
| 1328 |
unionName = "f16_union" if self.dataType.isHalf() else "bf16_union" |
| 1329 |
kStr += (padSpacesStr + "union {\n") |
| 1330 |
kStr += (padSpacesStr + " %s f;\n"%unionDataTypeStr) |
| 1331 |
kStr += (padSpacesStr + " short s;\n") |
| 1332 |
kStr += (padSpacesStr + "} %s;\n"%unionName) |
| 1333 |
kStr += (padSpacesStr + "%s.f = value;\n"%unionName) |
| 1334 |
kStr += (padSpacesStr + "%s.s = %s.s & 0x7fff;\n"%(unionName, unionName)) |
| 1335 |
kStr += (padSpacesStr + "value = %s.f;\n"%unionName) |
| 1336 |
elif (self.dataType.isSingle() or self.dataType.isDouble() or self.dataType.isInt32()): |
| 1337 |
kStr += (padSpacesStr + "value = abs(value);\n") |
| 1338 |
else: |
| 1339 |
raise RuntimeError("Unrecognized data type %s."%self.dataType) |
| 1340 |
elif (activationType == 'clippedrelu'): |
| 1341 |
if (self.dataType.isSingle() or self.dataType.isHalf() or self.dataType.isDouble()): |
| 1342 |
kStr += (padSpacesStr + "value = (value > alpha) ? min(value, beta) : min(0.0, beta);\n") |
| 1343 |
elif self.dataType.isInt32(): |
| 1344 |
kStr += (padSpacesStr + "value = (value > alpha) ? min(value, beta) : min(0, beta);\n") |
| 1345 |
elif (activationType == 'exp'): |
| 1346 |
kStr += (asm + " // Exp\n") |
| 1347 |
module = activation.getExpModule(self.dataType, 0, 0) |
| 1348 |
kStr += self.getActivationAsmStr(activation, module, (len(asm) * " ")) |
| 1349 |
kStr += addSpace(asm, ": \"+v\"(value) : \n") |
| 1350 |
kStr += self.getRequiredRegStr(asm, activation.vgprCounter, activation.sgprCounter) |
| 1351 |
elif (activationType == 'gelu'): |
| 1352 |
kStr += (asm + " // gelu\n") |
| 1353 |
module = activation.getGeluModule(self.dataType, 0, 0) |
| 1354 |
kStr += self.getActivationAsmStr(activation, module, (len(asm) * " ")) |
| 1355 |
kStr += addSpace(asm, ": \"+v\"(value) : \n") |
| 1356 |
kStr += self.getRequiredRegStr(asm, activation.vgprCounter, activation.sgprCounter) |
| 1357 |
elif (activationType == 'geluscaling'): |
| 1358 |
kStr += (asm + " // geluscaling\n") |
| 1359 |
module = activation.getGeluModule(self.dataType, 0, 0, 1) |
| 1360 |
kStr += self.getActivationAsmStr(activation, module, (len(asm) * " ")) |
| 1361 |
kStr += addSpace(asm, ": \"+v\"(value) : \"s\"(alpha)\n") |
| 1362 |
kStr += self.getRequiredRegStr(asm, activation.vgprCounter, activation.sgprCounter) |
| 1363 |
elif (activationType == 'leakyrelu'): |
| 1364 |
if (self.dataType.isSingle() or self.dataType.isHalf() or self.dataType.isDouble()): |
| 1365 |
kStr += (padSpacesStr + "value = (value >= 0.0) ? value : (value * alpha);\n") |
| 1366 |
elif self.dataType.isInt32(): |
| 1367 |
kStr += (padSpacesStr + "value = (value >= 0) ? value : (value * alpha);\n") |
| 1368 |
else: |
| 1369 |
raise RuntimeError("Unsupported data type %s."%ptrStr) |
| 1370 |
elif (activationType == 'relu'): |
| 1371 |
if (self.dataType.isSingle() or self.dataType.isHalf() or self.dataType.isDouble()): |
| 1372 |
kStr += (padSpacesStr + "value = max(0.0, value);\n") |
| 1373 |
elif self.dataType.isInt32(): |
| 1374 |
kStr += (padSpacesStr + "value = max(0, value);\n") |
| 1375 |
else: |
| 1376 |
raise RuntimeError("Unsupported data type %s."%ptrStr) |
| 1377 |
elif (activationType == 'sigmoid'): |
| 1378 |
kStr += (asm + " // Sigmoid\n") |
| 1379 |
module = activation.getSigmoidModule(self.dataType, 0, 0) |
| 1380 |
kStr += self.getActivationAsmStr(activation, module, (len(asm) * " ")) |
| 1381 |
kStr += addSpace(asm, ": \"+v\"(value) : \n") |
| 1382 |
kStr += self.getRequiredRegStr(asm, activation.vgprCounter, activation.sgprCounter) |
| 1383 |
elif (activationType == 'tanh'): |
| 1384 |
kStr += (asm + " // tanh\n") |
| 1385 |
module = activation.getTanhModule(self.dataType, 0, 0, 1, 2) |
| 1386 |
kStr += self.getActivationAsmStr(activation, module, (len(asm) * " ")) |
| 1387 |
kStr += addSpace(asm, ": \"+v\"(value) : \"s\"(alpha), \"s\"(beta)\n") |
| 1388 |
kStr += self.getRequiredRegStr(asm, activation.vgprCounter, activation.sgprCounter) |
| 1389 |
elif (activationType == 'dgelu'): |
| 1390 |
kStr += (asm + " // dgelu\n") |
| 1391 |
module = activation.getDGeluModule(self.dataType, 0, 0) |
| 1392 |
kStr += self.getActivationAsmStr(activation, module, (len(asm) * " ")) |
| 1393 |
kStr += addSpace(asm, ": \"+v\"(value) : \n") |
| 1394 |
needExec = True if self.enableGuard else False |
| 1395 |
kStr += self.getRequiredRegStr(asm, activation.vgprCounter, activation.sgprCounter, needExec=needExec) |
| 1396 |
elif (activationType == 'silu'): |
| 1397 |
kStr += (asm + " // Silu\n") |
| 1398 |
module = activation.getSiluModule(self.dataType, 0, 0) |
| 1399 |
kStr += self.getActivationAsmStr(activation, module, (len(asm) * " ")) |
| 1400 |
kStr += addSpace(asm, ": \"+v\"(value) : \n") |
| 1401 |
kStr += self.getRequiredRegStr(asm, activation.vgprCounter, activation.sgprCounter) |
| 1402 |
elif (activationType == 'swish'): |
| 1403 |
kStr += (asm + " // Swish\n") |
| 1404 |
module = activation.getSwishModule(self.dataType, 0, 0, 1) |
| 1405 |
kStr += self.getActivationAsmStr(activation, module, (len(asm) * " ")) |
| 1406 |
kStr += addSpace(asm, ": \"+v\"(value) : \"s\"(alpha)\n") |
| 1407 |
kStr += self.getRequiredRegStr(asm, activation.vgprCounter, activation.sgprCounter) |
| 1408 |
elif (activationType == 'clamp'): |
| 1409 |
kStr += (padSpacesStr + "value = max(alpha, min(value, beta));\n") |
| 1410 |
else: |
| 1411 |
if (activationType != 'none'): |
| 1412 |
raise RuntimeError("Unrecognized type %s."%activationType) |
| 1413 |
return kStr |
| 1414 |
|
| 1415 |
def getRequiredRegStr(self, spaceAlignStr, numOfVgpr, numOfSgpr, needExec=False): |
| 1416 |
requiredReg = [] |
| 1417 |
for i in range(0, numOfVgpr): |
| 1418 |
requiredReg.append("\"v%d\""%i) |
| 1419 |
for i in range(0, numOfSgpr): |
| 1420 |
requiredReg.append("\"s%d\""%i) |
| 1421 |
requiredStr = "" |
| 1422 |
if (len(requiredReg) > 0): |
| 1423 |
requiredStr = requiredReg[0] |
| 1424 |
for i in range(1, len(requiredReg)): |
| 1425 |
requiredStr += ", %s"%requiredReg[i] |
| 1426 |
if needExec: |
| 1427 |
if requiredStr: |
| 1428 |
requiredStr += ", \"exec\"" |
| 1429 |
else: |
| 1430 |
requiredStr += "\"exec\"" |
| 1431 |
kStr = "" |
| 1432 |
kStr += addSpace(spaceAlignStr,":%s);\n"%requiredStr) |
| 1433 |
return kStr |
| 1434 |
|
| 1435 |
def createVgprIdxList(module, vgprList: list, regName): |
| 1436 |
vlist = [[], []] |
| 1437 |
for item in module.items(): |
| 1438 |
if isinstance(item, Module): |
| 1439 |
tmplist = createVgprIdxList(item, vgprList, regName) |
| 1440 |
vlist[0].extend(tmplist[0]) |
| 1441 |
vlist[1].extend(tmplist[1]) |
| 1442 |
elif isinstance(item, Instruction): |
| 1443 |
for param in item.getParams(): |
| 1444 |
if isinstance(param, RegisterContainer): |
| 1445 |
for index, vgprIdx in enumerate(vgprList): |
| 1446 |
if param.regName and (param.regName.name == regName) and (param.regName.getOffset()[0] == vgprIdx): |
| 1447 |
vlist[index].append(param) |
| 1448 |
elif param.regIdx == vgprIdx: |
| 1449 |
vlist[index].append(param) |
| 1450 |
return vlist |