sync 2e7068faf55e
Browse filesFiles changed (6)
- README.md+62
- build/webgpu/bench.jsonAdded+42
- build/webgpu/gated-add.wgsl.jinjaAdded+63
- build/webgpu/manifest.jsonAdded+147
- build/webgpu/metadata.jsonAdded+18
- build/webgpu/test.jsonAdded+185
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465---library_name: kernelslicense: apache-2.0tags:- kernel- webgpu- wgsl---# com.microsoft.GatedAdd
`com.microsoft` · ONNX Runtime contrib operator · contrib since_version 1
## Description
Adds `Y`, scaled by a per-row `gate`, to `X`: `output = X + round_to_T(Y * gate)`. `X` and `Y` have shape `(..., C)`; `gate` has the same rank with a trailing dimension of 1, so one value covers each row of `C` channels. Rounding the product to `T` before the addition preserves the semantics of a separate `Mul` followed by `Add`. Bfloat16 is not implemented.
See the [ONNX Runtime `GatedAdd` contrib-operator spec](https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#com.microsoft.GatedAdd) for the reference semantics.
## Inputs
| Name | Bind key | Logical dtype | Rank | Shape | Description | Presence || --- | --- | --- | --- | --- | --- | --- || `X` | `X` | `T` | — | — | Unscaled input with shape `(..., C)`. Any rank of at least 1 is accepted; only the trailing channel axis is distinguished. | required || `Y` | `Y` | `T` | — | — | Input scaled by the gate, with the same shape as `X`. | required || `gate` | `gate` | `T` | — | — | Per-row gate with shape `(..., 1)`: the same rank and leading dimensions as `X`, with a trailing dimension of 1 that broadcasts over the `C` channels. | required |
## Outputs
| Name | Bind key | Logical dtype | Rank | Shape | Description | Presence || --- | --- | --- | --- | --- | --- | --- || `output` | `output` | `T` | same as `X` | same as `X` | Gated sum `X + round_to_T(Y * gate)`, with the same shape as `X`. | required |
## Type constraints
| Variable | Allowed dtypes || --- | --- || `T` | `float32`, `float16` |
## Files
- [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, provenance)- [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth)- [`test.json`](build/webgpu/test.json) — correctness cases- [`bench.json`](build/webgpu/bench.json) — benchmark + tuning cases- [`gated-add.wgsl.jinja`](build/webgpu/gated-add.wgsl.jinja)
## Use with `@huggingface/kernels`
The loader derives every required output's shape and logical dtype from the manifest contract and this call.It then allocates the result tensors automatically.
The `version: 1` option selects the published kernel contract; it is independent of any operator opset, contrib `since_version`, or model version.
Replace each `*Data` placeholder with a typed array containing the corresponding input data.
```jsimport { getKernel } from "@huggingface/kernels";
const kernel = await getKernel("webgpu-kernels/com.microsoft.GatedAdd", { version: 1 });const { output } = await kernel({ X: { data: XData, shape: [2, 3] }, Y: { data: YData, shape: [2, 3] }, gate: { data: gateData, shape: [2, 1] },});```123456789101112131415161718192021222324252627282930313233343536373839404142{ "op": "com.microsoft.GatedAdd", "tunableSpace": { "WORKGROUP_SIZE": [64, 128, 256] }, "cases": [ { "name": "gatedadd-f32-t2048-h2048", "preset": "smoke", "vars": { "dtype": "float32" }, "inputs": { "X": { "shape": [2048, 2048], "dtype": "float32", "dist": "normal", "seed": 7101, "scale": 2 }, "Y": { "shape": [2048, 2048], "dtype": "float32", "dist": "normal", "seed": 7102, "scale": 2 }, "gate": { "shape": [2048, 1], "dtype": "float32", "dist": "normal", "seed": 7103, "scale": 1 } }, "outputs": { "output": { "shape": [2048, 2048], "dtype": "float32" } }, "bench": { "metrics": [{ "type": "bandwidth", "value": "2048 * 2048 * 3 * dtypeBytes(args.dtype)" }] } }, { "name": "gatedadd-f16-t2048-h2048", "preset": "model", "vars": { "dtype": "float16" }, "inputs": { "X": { "shape": [2048, 2048], "dtype": "float16", "dist": "normal", "seed": 7104, "scale": 2 }, "Y": { "shape": [2048, 2048], "dtype": "float16", "dist": "normal", "seed": 7105, "scale": 2 }, "gate": { "shape": [2048, 1], "dtype": "float16", "dist": "normal", "seed": 7106, "scale": 1 } }, "outputs": { "output": { "shape": [2048, 2048], "dtype": "float16" } }, "bench": { "metrics": [{ "type": "bandwidth", "value": "2048 * 2048 * 3 * dtypeBytes(args.dtype)" }] } }, { "name": "gatedadd-f32-channels-odd-h1023", "preset": "stress", "vars": { "dtype": "float32" }, "inputs": { "X": { "shape": [4096, 1023], "dtype": "float32", "dist": "normal", "seed": 7107, "scale": 2 }, "Y": { "shape": [4096, 1023], "dtype": "float32", "dist": "normal", "seed": 7108, "scale": 2 }, "gate": { "shape": [4096, 1], "dtype": "float32", "dist": "normal", "seed": 7109, "scale": 1 } }, "outputs": { "output": { "shape": [4096, 1023], "dtype": "float32" } }, "bench": { "metrics": [{ "type": "bandwidth", "value": "4096 * 1023 * 3 * dtypeBytes(args.dtype)" }] } } ]}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263{% macro flat_index_2d(name="i", bound="params.count", guardInline=false, note="dispatch-limit") %}{% if note == "dispatch-limit" %} // 2D-folded flat index: gid.y carries the high bits past the // maxComputeWorkgroupsPerDimension dispatch limit (outputs > 16.7M elements).{% elif note == "limit" %} // 2D-folded flat index: gid.y carries the high bits past the // maxComputeWorkgroupsPerDimension limit.{% elif note == "device-axis" %} // The flat dispatch is folded across x/y at the device's per-axis workgroup // limit; gid.y carries the high portion of the output index.{% elif note == "vec4-limit" %} // 2D-folded flat vec4 index: gid.y carries the high bits past the // maxComputeWorkgroupsPerDimension limit (the dispatch caps x and spills into y).{% elif note == "element-limit" %} // 2D-folded flat element index: gid.y carries the high bits past the // maxComputeWorkgroupsPerDimension limit.{% elif note == "dispatch" %} // 2D-folded flat index: gid.y carries the high bits past the // maxComputeWorkgroupsPerDimension dispatch limit.{% endif %}{% if bound == "" %} let {{ name }} = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u;{%- elif guardInline %} let {{ name }} = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u; if ({{ name }} >= {{ bound }}) { return; }{%- else %} let {{ name }} = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u; if ({{ name }} >= {{ bound }}) { return; }{%- endif %}{% endmacro %}
{% if usesF16 %}enable f16;{% endif %}{{ env.wgsl.resourceDeclarations }}
// com.microsoft.GatedAdd : output = X + round_to_T(Y * gate)// X, Y, output : the same shape (..., C).// gate : (..., 1) -- one value per row of C channels, so a row index// of element_index / HIDDEN selects it.// The product is rounded to T before the add, so the fusion agrees with a// separate Mul followed by Add. fma(y, gate, 0.0) is that single rounding// (adding zero cannot move the product) and, unlike a bare y * gate, it cannot// be contracted into the following add by a backend that permits floating-point// contraction -- contraction would keep an unrounded wider product and silently// make this op more accurate than the graph it replaces.const HIDDEN: u32 = {{ hidden }}u;
@compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {{{ flat_index_2d() }}{% if vec4 %} // A vec4 group is four consecutive channels of one row: HIDDEN % 4 == 0 stops // it from ever straddling two rows, so the whole group shares one gate value. let g = vec4<{{ scalar }}>(gate[i * 4u / HIDDEN]); output[i] = x[i] + fma(y[i], g, vec4<{{ scalar }}>(0.0));{% else %} let g = gate[i / HIDDEN]; output[i] = x[i] + fma(y[i], g, {{ scalar }}(0.0));{% endif %}}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147{ "domain": "com.microsoft", "name": "GatedAdd", "sinceVersion": 1, "description": "Adds `Y`, scaled by a per-row `gate`, to `X`: `output = X + round_to_T(Y * gate)`. `X` and `Y` have shape `(..., C)`; `gate` has the same rank with a trailing dimension of 1, so one value covers each row of `C` channels. Rounding the product to `T` before the addition preserves the semantics of a separate `Mul` followed by `Add`. Bfloat16 is not implemented.", "inputs": [ { "role": "X", "dtype": "T", "description": "Unscaled input with shape `(..., C)`. Any rank of at least 1 is accepted; only the trailing channel axis is distinguished." }, { "role": "Y", "dtype": "T", "description": "Input scaled by the gate, with the same shape as `X`." }, { "role": "gate", "dtype": "T", "description": "Per-row gate with shape `(..., 1)`: the same rank and leading dimensions as `X`, with a trailing dimension of 1 that broadcasts over the `C` channels." } ], "outputs": [ { "role": "output", "dtype": "T", "rank": "ranks.X", "shape": "shapes.X", "description": "Gated sum `X + round_to_T(Y * gate)`, with the same shape as `X`." } ], "typeConstraints": { "T": ["float32", "float16"] }, "tunables": { "WORKGROUP_SIZE": 256 }, "args": { "X": { "kind": "tensor", "semantic": "X", "role": "input" }, "Y": { "kind": "tensor", "semantic": "Y", "role": "input" }, "gate": { "kind": "tensor", "semantic": "gate", "role": "input" }, "output": { "kind": "tensor", "semantic": "output", "role": "output" } }, "derive": { "channels": "dim(shapes.X, ranks.X - 1)", "gateContract": "ranks.X >= 1 and channels > 0 and ranks.Y == ranks.X and ranks.gate == ranks.X and sameShape(shapes.Y, shapes.X) and sameShape(shapes.output, shapes.X) and dim(shapes.gate, ranks.gate - 1) == 1 and sameShape(prefix(shapes.gate, ranks.gate - 1), prefix(shapes.X, ranks.X - 1)) and f16Ok(dtypes.T)", "vec4Rows": "channels % 4 == 0 and numel(shapes.X) % 4 == 0" }, "constants": { "scalar": "dtypes.T", "usesF16": "dtypes.T == \"f16\"", "hidden": "channels if channels > 0 else 1" }, "variants": [ { "id": "vec4", "priority": 30, "when": ["gateContract", "vec4Rows", "numel(shapes.X) > 0"], "constants": { "vec4": true, "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"" }, "passes": [ { "id": "main", "name": "GatedAdd.vec4", "shader": "gated-add.wgsl.jinja", "bindings": [ { "name": "x", "arg": "X", "semantic": "X", "buffer": { "type": "read-only-storage" }, "elementType": "$vectorScalar" }, { "name": "y", "arg": "Y", "semantic": "Y", "buffer": { "type": "read-only-storage" }, "elementType": "$vectorScalar" }, { "name": "gate", "arg": "gate", "semantic": "gate", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" }, { "name": "output", "arg": "output", "semantic": "output", "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" }] } } ], "dispatch": { "threads": "numel(shapes.X) / 4", "workgroupSize": "tunables.WORKGROUP_SIZE" } } ] }, { "id": "scalar", "priority": 0, "when": ["gateContract"], "constants": { "vec4": false }, "passes": [ { "id": "main", "name": "GatedAdd.scalar", "shader": "gated-add.wgsl.jinja", "bindings": [ { "name": "x", "arg": "X", "semantic": "X", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" }, { "name": "y", "arg": "Y", "semantic": "Y", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" }, { "name": "gate", "arg": "gate", "semantic": "gate", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" }, { "name": "output", "arg": "output", "semantic": "output", "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)" }] } } ], "dispatch": { "threads": "numel(shapes.X)", "workgroupSize": "tunables.WORKGROUP_SIZE" } } ] } ]}123456789101112131415161718{ "name": "com.microsoft.GatedAdd", "id": "_com_microsoft_gatedadd_webgpu_c9e5b9d", "version": 1, "license": "Apache-2.0", "backend": { "type": "webgpu" }, "digest": { "algorithm": "sha256", "files": { "bench.json": "Yo5szY3Ccb8NOxk0YLKXGApxHMFCENocZt9z2/GaJew=", "gated-add.wgsl.jinja": "GiWIIFkb2GXB3/MDWr4Buyc/kKQrH5xr2tcee1RCOog=", "manifest.json": "eGSFQTnmYL4/Zqwfz8zkKFJgFT1Fn5IjP+slAJPCuTU=", "test.json": "5Gsy9SVd5u5t8bVgKOLTcNMfO4wqI7rFSjHIhPpLEgk=" } }, "provenance": { "kernel": { "sha": "2e7068faf55e7f43df740015f6d1ee49391a41c5", "dirty": false } }, "webgpu": { "manifestSpec": "1.0", "specialized": true, "opPath": "ops/com.microsoft.GatedAdd" }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185{ "op": "com.microsoft.GatedAdd", "cases": [ { "name": "rank3_rows_f32", "inputs": { "X": { "dtype": "float32", "shape": [2, 3, 8], "data": { "kind": "fillFloat32", "sinStep": 0.13, "cosStep": 0.29 } }, "Y": { "dtype": "float32", "shape": [2, 3, 8], "data": { "kind": "fillFloat32", "sinStep": 0.31, "cosStep": 0.07 } }, "gate": { "dtype": "float32", "shape": [2, 3, 1], "data": { "kind": "fillFloat32", "sinStep": 0.41, "cosStep": 0.17, "offset": 0.75 } } }, "outputs": { "output": { "dtype": "float32", "shape": [2, 3, 8], "tolerance": 0.000001, "relTolerance": 0.000001 } } }, { "name": "rank2_channels_odd_f32", "inputs": { "X": { "dtype": "float32", "shape": [5, 7], "data": { "kind": "fillFloat32", "sinStep": 0.19, "cosStep": 0.23 } }, "Y": { "dtype": "float32", "shape": [5, 7], "data": { "kind": "fillFloat32", "sinStep": 0.37, "cosStep": 0.11 } }, "gate": { "dtype": "float32", "shape": [5, 1], "data": { "kind": "fillFloat32", "sinStep": 0.53, "cosStep": 0.29, "offset": -1.25 } } }, "outputs": { "output": { "dtype": "float32", "shape": [5, 7], "tolerance": 0.000001, "relTolerance": 0.000001 } } }, { "name": "rank2_channels_not_vec4_aligned_f32", "inputs": { "X": { "dtype": "float32", "shape": [4, 6], "data": { "kind": "fillFloat32", "sinStep": 0.17, "cosStep": 0.43 } }, "Y": { "dtype": "float32", "shape": [4, 6], "data": { "kind": "fillFloat32", "sinStep": 0.61, "cosStep": 0.13 } }, "gate": { "dtype": "float32", "shape": [4, 1], "data": { "kind": "values", "values": [2.0, -3.0, 0.5, 7.0] } } }, "outputs": { "output": { "dtype": "float32", "shape": [4, 6], "tolerance": 0.000001, "relTolerance": 0.000001 } } }, { "name": "rank1_single_row_f32", "inputs": { "X": { "dtype": "float32", "shape": [16], "data": { "kind": "fillFloat32", "sinStep": 0.27, "cosStep": 0.09 } }, "Y": { "dtype": "float32", "shape": [16], "data": { "kind": "fillFloat32", "sinStep": 0.47, "cosStep": 0.21 } }, "gate": { "dtype": "float32", "shape": [1], "data": { "kind": "values", "values": [-1.75] } } }, "outputs": { "output": { "dtype": "float32", "shape": [16], "tolerance": 0.000001, "relTolerance": 0.000001 } } }, { "name": "rank4_rows_f32", "inputs": { "X": { "dtype": "float32", "shape": [2, 2, 3, 4], "data": { "kind": "fillFloat32", "sinStep": 0.11, "cosStep": 0.37 } }, "Y": { "dtype": "float32", "shape": [2, 2, 3, 4], "data": { "kind": "fillFloat32", "sinStep": 0.23, "cosStep": 0.59 } }, "gate": { "dtype": "float32", "shape": [2, 2, 3, 1], "data": { "kind": "fillFloat32", "sinStep": 0.71, "cosStep": 0.19, "offset": 1.5 } } }, "outputs": { "output": { "dtype": "float32", "shape": [2, 2, 3, 4], "tolerance": 0.000001, "relTolerance": 0.000001 } } }, { "name": "gate_broadcast_rows_pinned", "provenance": { "notes": "Hand-computed from the schema formula output = X + round_to_T(Y * gate); every value is exact in float32, so the expectation is independent of the reference." }, "inputs": { "X": { "dtype": "float32", "shape": [2, 3], "data": { "kind": "values", "values": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] } }, "Y": { "dtype": "float32", "shape": [2, 3], "data": { "kind": "values", "values": [0.5, -1.0, 2.0, 10.0, -0.25, 0.125] } }, "gate": { "dtype": "float32", "shape": [2, 1], "data": { "kind": "values", "values": [2.0, -0.5] } } }, "outputs": { "output": { "dtype": "float32", "shape": [2, 3], "data": { "kind": "values", "values": [2.0, 0.0, 7.0, -1.0, 5.125, 5.9375] } } } }, { "name": "f16_product_rounds_to_type_pinned", "provenance": { "notes": "Pins the round_to_T rule that separates this op from a wider-precision fused multiply-add. Row 0 uses gate = 1 + 2^-10, so Y = 1025 gives a real product of 1026.0009765625 that float16 rounds to 1026.0; X = -1026 then cancels it exactly to 0. A kernel that let the product stay unrounded -- by contracting the multiply into the add -- would return 2^-10 there instead. Every other value is exact in float16, so the whole expectation is hand-computable." }, "inputs": { "X": { "dtype": "float16", "shape": [2, 4], "data": { "kind": "values", "values": [-1026.0, -1000.0, 0.5, -8.0, 0.5, -1.0, 0.25, 3.0] } }, "Y": { "dtype": "float16", "shape": [2, 4], "data": { "kind": "values", "values": [1025.0, 1024.0, 512.0, 8.0, 1.0, 2.0, 3.0, 4.0] } }, "gate": { "dtype": "float16", "shape": [2, 1], "data": { "kind": "values", "values": [1.0009765625, 2.0] } } }, "outputs": { "output": { "dtype": "float16", "shape": [2, 4], "data": { "kind": "values", "values": [0.0, 25.0, 513.0, 0.0078125, 2.5, 3.0, 6.25, 11.0] }, "tolerance": 0.0005 } } }, { "name": "f16_rows", "inputs": { "X": { "dtype": "float16", "shape": [3, 16], "data": { "kind": "fillFloat32", "sinStep": 0.13, "cosStep": 0.29, "offset": 1.25 } }, "Y": { "dtype": "float16", "shape": [3, 16], "data": { "kind": "fillFloat32", "sinStep": 0.31, "cosStep": 0.07, "offset": -0.75 } }, "gate": { "dtype": "float16", "shape": [3, 1], "data": { "kind": "values", "values": [1.5, -0.5, 2.25] } } }, "outputs": { "output": { "dtype": "float16", "shape": [3, 16], "tolerance": 0.0005, "relTolerance": 0.002 } } }, { "name": "f16_channels_odd", "inputs": { "X": { "dtype": "float16", "shape": [4, 5], "data": { "kind": "fillFloat32", "sinStep": 0.43, "cosStep": 0.17, "offset": -1.5 } }, "Y": { "dtype": "float16", "shape": [4, 5], "data": { "kind": "fillFloat32", "sinStep": 0.19, "cosStep": 0.53, "offset": 0.875 } }, "gate": { "dtype": "float16", "shape": [4, 1], "data": { "kind": "values", "values": [0.75, -1.25, 3.0, 0.5] } } }, "outputs": { "output": { "dtype": "float16", "shape": [4, 5], "tolerance": 0.0005, "relTolerance": 0.002 } } } ]}README.md+62
build/webgpu/bench.jsonAdded+42
build/webgpu/gated-add.wgsl.jinjaAdded+63
build/webgpu/manifest.jsonAdded+147
build/webgpu/metadata.jsonAdded+18
build/webgpu/test.jsonAdded+185