File size: 14,485 Bytes
993cc3b f841d79 993cc3b 2382e07 993cc3b f841d79 993cc3b f841d79 993cc3b f841d79 993cc3b f841d79 2382e07 993cc3b f841d79 2382e07 993cc3b f841d79 993cc3b 2382e07 993cc3b 2382e07 993cc3b f841d79 993cc3b 2382e07 993cc3b f841d79 993cc3b f841d79 993cc3b 2382e07 993cc3b 2382e07 993cc3b f841d79 993cc3b 2382e07 993cc3b f841d79 993cc3b f841d79 993cc3b 2382e07 993cc3b 2382e07 f841d79 2382e07 993cc3b 2382e07 f841d79 993cc3b f841d79 993cc3b 2382e07 993cc3b 2382e07 993cc3b 2382e07 f841d79 993cc3b f841d79 993cc3b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | {
"domain": "com.microsoft",
"name": "FusedGemm",
"sinceVersion": 1,
"inputs": {
"A": { "dtype": "T", "rank": 2 },
"B": { "dtype": "T", "rank": 2 },
"C": { "dtype": "T", "optional": true }
},
"outputs": { "Y": { "dtype": "T", "rank": 2, "shape": "[gemmM, gemmN]" } },
"attributes": {
"alpha": { "default": 1 },
"beta": { "default": 1 },
"transA": { "default": 0 },
"transB": { "default": 0 },
"activation": {},
"activation_alpha": {},
"activation_beta": {}
},
"attributeConstraints": { "activation": { "values": ["Relu", "LeakyRelu", "Sigmoid", "Tanh", "HardSigmoid"] } },
"typeConstraints": { "T": ["float32", "float16"] },
"derive": {
"deviceWorkgroupCap": "min(device.limits.maxComputeInvocationsPerWorkgroup, device.limits.maxComputeWorkgroupSizeX)",
"wave32Adapter": "has(device.adapterInfo, \"subgroupMinSize\") and has(device.adapterInfo, \"subgroupMaxSize\") and device.adapterInfo.subgroupMinSize == 32 and device.adapterInfo.subgroupMaxSize == 32",
"canPinSubgroupSize32": "device.features.has(\"subgroups\") and device.features.has(\"subgroup-size-control\") and has(device.adapterInfo, \"subgroupMinSize\") and has(device.adapterInfo, \"subgroupMaxSize\") and device.adapterInfo.subgroupMinSize <= 32 and device.adapterInfo.subgroupMaxSize >= 32",
"pinSubgroupSize32": "canPinSubgroupSize32 and not wave32Adapter",
"wave32Effective": "wave32Adapter or pinSubgroupSize32",
"variableSubgroup16To32": "device.features.has(\"subgroups\") and has(device.adapterInfo, \"subgroupMinSize\") and has(device.adapterInfo, \"subgroupMaxSize\") and device.adapterInfo.subgroupMinSize == 16 and device.adapterInfo.subgroupMaxSize == 32",
"transAFlag": "has(attrs, \"transA\") and attrs.transA != 0",
"transBFlag": "has(attrs, \"transB\") and attrs.transB != 0",
"gemmM": "dim(shapes.A, 1) if transAFlag else dim(shapes.A, 0)",
"gemmK": "dim(shapes.A, 0) if transAFlag else dim(shapes.A, 1)",
"gemmN": "dim(shapes.B, 0) if transBFlag else dim(shapes.B, 1)",
"gemmKB": "dim(shapes.B, 1) if transBFlag else dim(shapes.B, 0)",
"shapeOk": "ranks.A == 2 and ranks.B == 2 and ranks.Y == 2 and gemmK == gemmKB and dim(shapes.Y, 0) == gemmM and dim(shapes.Y, 1) == gemmN",
"dtypeOk": "(tensorDtypes.A == \"float32\" or tensorDtypes.A == \"float16\") and tensorDtypes.B == tensorDtypes.A and tensorDtypes.Y == tensorDtypes.A and f16Ok(dtypes.T)",
"rowBiasOk": "present.C and ((ranks.C == 1 and dim(shapes.C, 0) == gemmN) or (ranks.C == 2 and dim(shapes.C, 0) == 1 and dim(shapes.C, 1) == gemmN))",
"scalarBiasOk": "present.C and (ranks.C == 0 or (ranks.C == 1 and dim(shapes.C, 0) == 1) or (ranks.C == 2 and dim(shapes.C, 0) == 1 and dim(shapes.C, 1) == 1))",
"columnBiasOk": "present.C and ranks.C == 2 and dim(shapes.C, 0) == gemmM and dim(shapes.C, 1) == 1",
"matrixBiasOk": "present.C and ranks.C == 2 and dim(shapes.C, 0) == gemmM and dim(shapes.C, 1) == gemmN",
"cOk": "tensorDtypes.C == tensorDtypes.A and (rowBiasOk or scalarBiasOk or columnBiasOk or matrixBiasOk)",
"hasActivation": "has(attrs, \"activation\")",
"activationName": "attrs.activation if hasActivation else \"\"",
"activationOk": "not hasActivation or activationName == \"Relu\" or activationName == \"LeakyRelu\" or activationName == \"Sigmoid\" or activationName == \"Tanh\" or activationName == \"HardSigmoid\"",
"activationAlpha": "attrs.activation_alpha if has(attrs, \"activation_alpha\") else (0.2 if activationName == \"HardSigmoid\" else (0.01 if activationName == \"LeakyRelu\" else 0.0))",
"activationBeta": "attrs.activation_beta if has(attrs, \"activation_beta\") else (0.5 if activationName == \"HardSigmoid\" else 0.0)",
"baseContract": "shapeOk and dtypeOk and activationOk",
"noBiasContract": "baseContract and not present.C",
"biasContract": "baseContract and present.C and cOk",
"sgmatOperandBytes": "2 if tensorDtypes.A == \"float16\" and tensorDtypes.B == \"float16\" else 4",
"sgmatTileM": "32",
"sgmatTileN": "64",
"sgmatTileK": "32",
"sgmatDirectA": "tensorDtypes.A == \"float32\" and not transAFlag and gemmK > 0 and gemmM % sgmatTileM == 0",
"sgmatStorageBytes": "((0 if sgmatDirectA else sgmatTileM) + sgmatTileN) * sgmatTileK * sgmatOperandBytes + sgmatTileM * sgmatTileN * 4",
"sgmatResourcesFit": "128 <= deviceWorkgroupCap and sgmatStorageBytes <= device.limits.maxComputeWorkgroupStorageSize",
"sgmatScalingOk": "(attrs.alpha if has(attrs, \"alpha\") else 1) == 1 and (attrs.beta if has(attrs, \"beta\") else 1) == 1",
"sgmatLayoutOk": "gemmM > 0 and gemmK % sgmatTileK == 0 and gemmN % sgmatTileN == 0 and ((tensorDtypes.A == \"float16\" and device.features.has(\"shader-f16\") and (gemmM >= 2 or (not transAFlag and transBFlag))) or (tensorDtypes.A == \"float32\" and gemmM >= sgmatTileM)) and ceilDiv(gemmM, sgmatTileM) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) and ceilDiv(gemmN, sgmatTileN) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535)",
"sgmatContract": "wave32Effective and sgmatLayoutOk and sgmatScalingOk and sgmatResourcesFit",
"sgmatRowBiasContract": "sgmatContract and rowBiasOk and ranks.C == 1",
"usesF16": "tensorDtypes.A == \"float16\"",
"gemmActivation": "activationName",
"gemmActAlpha": "activationAlpha",
"gemmActBeta": "activationBeta",
"gemmEpilogue": "\"activation\" if hasActivation else \"none\"",
"gemvColumns": "4 if gemmN % 4 == 0 else 1",
"gemvWorkgroup": "min(pow(2, log2ceil(deviceWorkgroupCap + 1) - 1), pow2ceil(max(gemmK, 1)), 64)",
"portableMicro8x2Preferred": "(variableSubgroup16To32 or (wave32Effective and not device.features.has(\"shader-f16\"))) and 256 <= deviceWorkgroupCap and 32 <= device.limits.maxComputeWorkgroupSizeX and 8 <= device.limits.maxComputeWorkgroupSizeY",
"gemmMicroRows": "8 if portableMicro8x2Preferred else 4",
"gemmMicroCols": "2 if portableMicro8x2Preferred else 4",
"gemmStaticM": "gemmM",
"gemmStaticN": "gemmN",
"gemmStaticK": "gemmK"
},
"bindings": {
"x": { "arg": "A", "elementType": "$T" },
"w": { "arg": "B", "elementType": "$T" },
"bias": { "arg": "C", "elementType": "$T" },
"y": { "arg": "Y", "elementType": "$T" },
"params": { "struct": [{ "name": "M", "type": "u32", "value": "gemmM" }] },
"a": { "arg": "A", "elementType": "$T" },
"b": { "arg": "B", "elementType": "$T" }
},
"variants": [
{
"id": "gemv_plain",
"priority": 200,
"when": ["noBiasContract", "gemmM == 1 and gemmN > 0 and gemmK > 0 and transBFlag", "gemvWorkgroup * gemvColumns * 4 <= device.limits.maxComputeWorkgroupStorageSize"],
"derive": {
"hasBias": false,
"workgroupSize": "gemvWorkgroup",
"gemvFused": true,
"inFeatures": "gemmK",
"outFeatures": "gemmN",
"xScalar": "\"f16\" if usesF16 else \"f32\"",
"wScalar": "\"f16\" if usesF16 else \"f32\"",
"yScalar": "\"f16\" if usesF16 else \"f32\""
},
"passes": [
{
"id": "main",
"name": "FusedGemm.Gemv",
"shader": "gemv-contiguous.wgsl.jinja",
"bindings": [
"x",
"w",
"y",
{
"name": "params",
"struct": [{ "name": "alpha", "type": "f32", "value": "attrs.alpha if has(attrs, \"alpha\") else 1" }]
}
],
"dispatch": {
"x": "min(ceilDiv(gemmN, gemvColumns), DISPATCH_FOLD_WIDTH)",
"y": "1",
"z": "ceilDiv(ceilDiv(gemmN, gemvColumns), DISPATCH_FOLD_WIDTH)"
}
}
]
},
{
"id": "gemv_bias",
"priority": 200,
"when": ["biasContract", "gemmM == 1 and gemmN > 0 and gemmK > 0 and transBFlag", "gemvWorkgroup * gemvColumns * 4 <= device.limits.maxComputeWorkgroupStorageSize"],
"derive": {
"hasBias": true,
"workgroupSize": "gemvWorkgroup",
"gemvFused": true,
"inFeatures": "gemmK",
"outFeatures": "gemmN",
"xScalar": "\"f16\" if usesF16 else \"f32\"",
"wScalar": "\"f16\" if usesF16 else \"f32\"",
"yScalar": "\"f16\" if usesF16 else \"f32\""
},
"passes": [
{
"id": "main",
"name": "FusedGemm.Gemv",
"shader": "gemv-contiguous.wgsl.jinja",
"bindings": [
"x",
"w",
"bias",
"y",
{
"name": "params",
"struct": [
{ "name": "alpha", "type": "f32", "value": "attrs.alpha if has(attrs, \"alpha\") else 1" },
{ "name": "beta", "type": "f32", "value": "attrs.beta if has(attrs, \"beta\") else 1" }
]
}
],
"dispatch": {
"x": "min(ceilDiv(gemmN, gemvColumns), DISPATCH_FOLD_WIDTH)",
"y": "1",
"z": "ceilDiv(ceilDiv(gemmN, gemvColumns), DISPATCH_FOLD_WIDTH)"
}
}
]
},
{
"id": "sgmat_bias",
"priority": 100,
"when": ["sgmatRowBiasContract"],
"requires": {
"features": ["subgroups", "chromium-experimental-subgroup-matrix"],
"subgroupMatrixConfigs": [
{ "componentType": "f16", "M": 8, "N": 8, "K": 8 },
{ "componentType": "f32", "resultComponentType": "f32", "M": 8, "N": 8, "K": 8 }
]
},
"derive": {
"hasBias": true,
"aTransposedStorage": "transAFlag",
"bTransposedStorage": "not transBFlag",
"yScalar": "\"f16\" if tensorDtypes.Y == \"float16\" else \"f32\"",
"mmaScalar": "\"f16\" if tensorDtypes.A == \"float16\" and tensorDtypes.B == \"float16\" else \"f32\"",
"inFeatures": "gemmK",
"outFeatures": "gemmN",
"tileM": "sgmatTileM",
"tileN": "sgmatTileN",
"tileK": "sgmatTileK",
"directAStorage": "sgmatDirectA",
"compensatedMatrixChunkTiles": "2 if sgmatDirectA and gemmK >= 8 * sgmatTileK else 0"
},
"passes": [
{
"id": "main",
"name": "FusedGemm.SubgroupMatrixBias",
"shader": "gemm-subgroup-matrix.wgsl.jinja",
"bindings": ["x", "w", "bias", "y", "params"],
"dispatch": {
"x": "ceilDiv(gemmN, sgmatTileN)",
"y": "min(ceilDiv(gemmM, sgmatTileM), DISPATCH_FOLD_WIDTH)",
"z": "ceilDiv(ceilDiv(gemmM, sgmatTileM), DISPATCH_FOLD_WIDTH)"
}
}
]
},
{
"id": "sgmat",
"priority": 100,
"when": ["noBiasContract and sgmatContract"],
"requires": {
"features": ["subgroups", "chromium-experimental-subgroup-matrix"],
"subgroupMatrixConfigs": [
{ "componentType": "f16", "M": 8, "N": 8, "K": 8 },
{ "componentType": "f32", "resultComponentType": "f32", "M": 8, "N": 8, "K": 8 }
]
},
"derive": {
"hasBias": false,
"aTransposedStorage": "transAFlag",
"bTransposedStorage": "not transBFlag",
"yScalar": "\"f16\" if tensorDtypes.Y == \"float16\" else \"f32\"",
"mmaScalar": "\"f16\" if tensorDtypes.A == \"float16\" and tensorDtypes.B == \"float16\" else \"f32\"",
"inFeatures": "gemmK",
"outFeatures": "gemmN",
"tileM": "sgmatTileM",
"tileN": "sgmatTileN",
"tileK": "sgmatTileK",
"directAStorage": "sgmatDirectA",
"compensatedMatrixChunkTiles": "2 if sgmatDirectA and gemmK >= 8 * sgmatTileK else 0"
},
"passes": [
{
"id": "main",
"name": "FusedGemm.SubgroupMatrix",
"shader": "gemm-subgroup-matrix.wgsl.jinja",
"bindings": ["x", "w", "y", "params"],
"dispatch": {
"x": "ceilDiv(gemmN, sgmatTileN)",
"y": "min(ceilDiv(gemmM, sgmatTileM), DISPATCH_FOLD_WIDTH)",
"z": "ceilDiv(ceilDiv(gemmM, sgmatTileM), DISPATCH_FOLD_WIDTH)"
}
}
]
},
{
"id": "tiled_reg_bias",
"priority": 0,
"when": ["biasContract"],
"derive": {
"transA": "transAFlag",
"transB": "transBFlag",
"hasBias": true,
"rowBias": "rowBiasOk",
"scalarBias": "scalarBiasOk",
"columnBias": "columnBiasOk"
},
"passes": [
{
"id": "main",
"name": "FusedGemm.Bias",
"shader": "gemm-epilogue-tiled-reg.wgsl.jinja",
"bindings": [
"a",
"b",
"bias",
"y",
{
"name": "params",
"struct": [
{ "name": "M", "type": "u32", "value": "gemmM" },
{ "name": "N", "type": "u32", "value": "gemmN" },
{ "name": "K", "type": "u32", "value": "gemmK" },
{ "name": "alpha", "type": "f32", "value": "attrs.alpha if has(attrs, \"alpha\") else 1" },
{ "name": "beta", "type": "f32", "value": "attrs.beta if has(attrs, \"beta\") else 1" }
]
}
],
"dispatch": {
"x": "min(ceilDiv(gemmN, 64), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))",
"y": "min(ceilDiv(gemmM, 64), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))"
}
}
]
},
{
"id": "tiled_reg",
"priority": 0,
"when": ["noBiasContract"],
"derive": { "transA": "transAFlag", "transB": "transBFlag", "hasBias": false },
"passes": [
{
"id": "main",
"name": "FusedGemm",
"shader": "gemm-epilogue-tiled-reg.wgsl.jinja",
"bindings": [
"a",
"b",
"y",
{
"name": "params",
"struct": [
{ "name": "M", "type": "u32", "value": "gemmM" },
{ "name": "N", "type": "u32", "value": "gemmN" },
{ "name": "K", "type": "u32", "value": "gemmK" },
{ "name": "alpha", "type": "f32", "value": "attrs.alpha if has(attrs, \"alpha\") else 1" }
]
}
],
"dispatch": {
"x": "min(ceilDiv(gemmN, 64), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))",
"y": "min(ceilDiv(gemmM, 64), min(device.limits.maxComputeWorkgroupsPerDimension, 65535))"
}
}
]
}
]
}
|