File size: 8,373 Bytes
3aadfb1 | 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 | {
"domain": "com.microsoft",
"name": "FastGelu",
"sinceVersion": 1,
"description": "Applies the GELU (Gaussian Error Linear Unit) activation using a tanh approximation: `Y = 0.5 * X * (1 + tanh(0.797885 * X + 0.035677 * X^3))`. An optional `bias` is added to `X` before the activation is computed. This WebGPU package implements float16 and float32; the schema-allowed double and bfloat16 types are not supported.",
"inputs": [
{ "role": "X", "dtype": "T", "description": "Values transformed by FastGelu after adding the optional `bias`." },
{
"role": "bias",
"dtype": "T",
"rank": 1,
"optional": true,
"description": "Optional 1-D bias added to `X` along the last dimension before the GELU activation."
}
],
"outputs": [
{
"role": "Y",
"dtype": "T",
"rank": "ranks.X",
"shape": "shapes.X",
"description": "Output tensor after applying the GELU activation; same shape as `X`."
}
],
"typeConstraints": { "T": ["float32", "float16"] },
"args": {
"X": { "kind": "tensor", "semantic": "X", "role": "input" },
"bias": { "kind": "tensor", "semantic": "bias", "role": "input", "required": false },
"Y": { "kind": "tensor", "semantic": "Y", "role": "output" }
},
"tunables": { "WORKGROUP_SIZE": 256 },
"derive": {
"deviceWorkgroupCap": "min(device.limits.maxComputeInvocationsPerWorkgroup, device.limits.maxComputeWorkgroupSizeX)",
"storageBufferLimit": "min(device.limits.maxStorageBufferBindingSize, device.limits.maxBufferSize)",
"workgroupSize": "min(tunables.WORKGROUP_SIZE, deviceWorkgroupCap)",
"buffersFit": "numel(shapes.X) * dtypeBytes(dtypes.T) <= storageBufferLimit and numel(shapes.Y) * dtypeBytes(dtypes.T) <= storageBufferLimit and (not present.bias or numel(shapes.bias) * dtypeBytes(dtypes.T) <= storageBufferLimit)",
"dispatchFits": "numel(shapes.X) <= device.limits.maxComputeWorkgroupsPerDimension * device.limits.maxComputeWorkgroupsPerDimension * workgroupSize * 4",
"baseOk": "workgroupSize > 0 and ranks.X >= 1 and numel(shapes.X) == numel(shapes.Y) and f16Ok(dtypes.T) and buffersFit and dispatchFits",
"biasOk": "present.bias and ranks.bias == 1 and dim(shapes.bias, 0) == dim(shapes.X, ranks.X - 1)",
"noBiasOk": "not present.bias",
"vec4Ok": "numel(shapes.X) > 0 and numel(shapes.X) % 4 == 0 and dim(shapes.X, ranks.X - 1) % 4 == 0"
},
"constants": { "scalar": "dtypes.T", "usesF16": "dtypes.T == \"f16\"", "approximate": "\"tanh\"" },
"bindingSets": {
"vec4Bias": [
{
"name": "x",
"arg": "X",
"semantic": "X",
"buffer": { "type": "read-only-storage" },
"elementType": "$vectorScalar"
},
{
"name": "bias",
"arg": "bias",
"semantic": "bias",
"buffer": { "type": "read-only-storage" },
"elementType": "$scalar",
"length": "$hidden"
},
{ "name": "y", "arg": "Y", "semantic": "Y", "buffer": { "type": "storage" }, "elementType": "$vectorScalar" },
{
"name": "params",
"semantic": "kernel.params",
"buffer": { "type": "uniform" },
"struct": { "name": "Params", "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.X) / 4" }] }
}
],
"vec4": [
{
"name": "x",
"arg": "X",
"semantic": "X",
"buffer": { "type": "read-only-storage" },
"elementType": "$vectorScalar"
},
{ "name": "y", "arg": "Y", "semantic": "Y", "buffer": { "type": "storage" }, "elementType": "$vectorScalar" },
{
"name": "params",
"semantic": "kernel.params",
"buffer": { "type": "uniform" },
"struct": { "name": "Params", "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.X) / 4" }] }
}
],
"scalarBias": [
{ "name": "x", "arg": "X", "semantic": "X", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" },
{
"name": "bias",
"arg": "bias",
"semantic": "bias",
"buffer": { "type": "read-only-storage" },
"elementType": "$scalar"
},
{ "name": "y", "arg": "Y", "semantic": "Y", "buffer": { "type": "storage" }, "elementType": "$scalar" },
{
"name": "params",
"semantic": "kernel.params",
"buffer": { "type": "uniform" },
"struct": { "name": "Params", "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.X)" }] }
}
],
"scalar": [
{ "name": "x", "arg": "X", "semantic": "X", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" },
{ "name": "y", "arg": "Y", "semantic": "Y", "buffer": { "type": "storage" }, "elementType": "$scalar" },
{
"name": "params",
"semantic": "kernel.params",
"buffer": { "type": "uniform" },
"struct": { "name": "Params", "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.X)" }] }
}
]
},
"variants": [
{
"id": "vec4_bias",
"priority": 30,
"when": ["baseOk", "biasOk", "vec4Ok"],
"constants": {
"vec4": true,
"vec4Tail": false,
"hasBias": true,
"vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"",
"hidden": "dim(shapes.X, ranks.X - 1) if dim(shapes.X, ranks.X - 1) > 0 else 1"
},
"passes": [
{
"id": "main",
"name": "FastGelu.vec4Bias",
"shader": "elementwise-bias-gelu.wgsl.jinja",
"bindings": "vec4Bias",
"dispatch": { "threads": "numel(shapes.X) / 4", "workgroupSize": "workgroupSize" }
}
]
},
{
"id": "vec4_no_bias",
"priority": 25,
"when": ["baseOk", "noBiasOk", "vec4Ok"],
"constants": { "vec4": true, "vec4Tail": false, "hasBias": false, "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"" },
"passes": [
{
"id": "main",
"name": "FastGelu.vec4",
"shader": "elementwise-bias-gelu.wgsl.jinja",
"bindings": "vec4",
"dispatch": { "threads": "numel(shapes.X) / 4", "workgroupSize": "workgroupSize" }
}
]
},
{
"id": "vec4_tail_bias",
"priority": 20,
"when": ["baseOk", "biasOk", "numel(shapes.X) > 0"],
"constants": {
"vec4": false,
"vec4Tail": true,
"hasBias": true,
"hidden": "dim(shapes.X, ranks.X - 1) if dim(shapes.X, ranks.X - 1) > 0 else 1"
},
"passes": [
{
"id": "main",
"name": "FastGelu.vec4TailBias",
"shader": "elementwise-bias-gelu.wgsl.jinja",
"bindings": "scalarBias",
"dispatch": { "threads": "ceilDiv(numel(shapes.X), 4)", "workgroupSize": "workgroupSize" }
}
]
},
{
"id": "vec4_tail_no_bias",
"priority": 15,
"when": ["baseOk", "noBiasOk", "numel(shapes.X) > 0"],
"constants": { "vec4": false, "vec4Tail": true, "hasBias": false },
"passes": [
{
"id": "main",
"name": "FastGelu.vec4Tail",
"shader": "elementwise-bias-gelu.wgsl.jinja",
"bindings": "scalar",
"dispatch": { "threads": "ceilDiv(numel(shapes.X), 4)", "workgroupSize": "workgroupSize" }
}
]
},
{
"id": "scalar_bias",
"priority": 10,
"when": ["baseOk", "biasOk", "true"],
"constants": {
"vec4": false,
"vec4Tail": false,
"hasBias": true,
"hidden": "dim(shapes.X, ranks.X - 1) if dim(shapes.X, ranks.X - 1) > 0 else 1"
},
"passes": [
{
"id": "main",
"name": "FastGelu.scalarBias",
"shader": "elementwise-bias-gelu.wgsl.jinja",
"bindings": "scalarBias",
"dispatch": { "threads": "numel(shapes.X)", "workgroupSize": "workgroupSize" }
}
]
},
{
"id": "scalar_no_bias",
"priority": 0,
"when": ["baseOk", "noBiasOk", "true"],
"constants": { "vec4": false, "vec4Tail": false, "hasBias": false },
"passes": [
{
"id": "main",
"name": "FastGelu.scalar",
"shader": "elementwise-bias-gelu.wgsl.jinja",
"bindings": "scalar",
"dispatch": { "threads": "numel(shapes.X)", "workgroupSize": "workgroupSize" }
}
]
}
]
}
|