sync 91d990483a17
Browse filesFiles changed (6)
17 unmodified lines181920212223242521222324252627282930312930313233343 unmodified lines38394041414243441 unmodified line4647484950495051525354555657585917 unmodified lines
## 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 || Name | Logical dtype | Rank | Shape | Description | Presence || --- | --- | --- | --- | --- | --- || `X` | `T` | — | — | Unscaled input with shape `(..., C)`. Any rank of at least 1 is accepted; only the trailing channel axis is distinguished. | required || `Y` | `T` | — | — | Input scaled by the gate, with the same shape as `X`. | required || `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 || Name | Logical dtype | Rank | Shape | Description | Presence || --- | --- | --- | --- | --- | --- || `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
3 unmodified lines
## Files
- [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, provenance)- [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, per-variant templates, 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 cases1 unmodified line
## 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.```shnpm install --save-exact @huggingface/kernels@0.0.1-preview.2```
Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.
The `version: 1` option selects the published kernel contract; it is independent of any operator opset, contrib `since_version`, or model version.It follows the `v1` branch as fixes land. To pin exact artifact bytes, pass a 40-character commit `revision` instead of `version`.
Replace each `*Data` placeholder with a typed array containing the corresponding input data.
12234{ "op": "com.microsoft.GatedAdd", "tunableSpace": { "WORKGROUP_SIZE": [64, 128, 256] }, "cases": [ {12334567567891011101112131415161718191314211523161718192829202122313223242534352627283738293031324133344344353637384739404941424352444546474849505960615152534 unmodified lines58596072736162636465667867686970{% macro flat_tail_open() %}@compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {fn main(@builtin(global_invocation_id) gid: vec3<u32>) { // 2D-folded flat index: gid.y carries the high bits when the element count exceeds the // maxComputeWorkgroupsPerDimension limit (the dispatch caps x and spills the rest into y). let invocation = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u;{% if source.itemsPerInvocation is defined %} // dispatch's per-axis workgroup fold width (the dispatch caps x and spills the rest into y). let invocation = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ tunables.WORKGROUP_SIZE }}u; // Tail-safe scalar x4 keeps vector-like dispatch density without requiring // the logical tensor length (or its storage binding) to be vec4 aligned.{% set itemsPerInvocation = itemsPerInvocation if itemsPerInvocation is defined else 4 %} let begin = invocation * {{ source.itemsPerInvocation }}u; let end = min(begin + {{ source.itemsPerInvocation }}u, params.count); let begin = invocation * {{ itemsPerInvocation }}u; let end = min(begin + {{ itemsPerInvocation }}u, params.count); for (var i = begin; i < end; i = i + 1u) {{%- else %} let i = invocation; if (i >= params.count) { return; }{%- endif %}{% endmacro %}{%- endmacro %}{% macro flat_tail_close() %}{% if source.itemsPerInvocation is defined %} }{% endif %}{% endmacro %}
{% 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). // 2D-folded flat index: gid.y carries the high bits past the dispatch's // per-axis workgroup fold width (outputs > 16.7M elements).{% elif note == "limit" %} // 2D-folded flat index: gid.y carries the high bits past the // maxComputeWorkgroupsPerDimension limit. // 2D-folded flat index: gid.y carries the high bits past the dispatch's // per-axis workgroup fold width.{% 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. // The flat dispatch is folded across x/y at a fixed per-axis workgroup // width; 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). // 2D-folded flat vec4 index: gid.y carries the high bits past the dispatch's // per-axis workgroup fold width (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. // dispatch's per-axis workgroup fold width.{% elif note == "dispatch" %} // 2D-folded flat index: gid.y carries the high bits past the // maxComputeWorkgroupsPerDimension dispatch limit. // 2D-folded flat index: gid.y carries the high bits past the dispatch's // per-axis workgroup fold width.{% endif %}{% if bound == "" %} let {{ name }} = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u; let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ tunables.WORKGROUP_SIZE }}u;{%- elif guardInline %} let {{ name }} = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u; let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ tunables.WORKGROUP_SIZE }}u; if ({{ name }} >= {{ bound }}) { return; }{%- else %} let {{ name }} = gid.x + gid.y * nwg.x * {{ tunables.WORKGROUP_SIZE }}u; let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ 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)4 unmodified lines// 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.// contraction. This preserves the staged precision of separate multiplication// and addition across the storage-type boundary.const HIDDEN: u32 = {{ hidden }}u;
{% if vec4 %}@compute @workgroup_size({{ tunables.WORKGROUP_SIZE }})fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {fn main(@builtin(global_invocation_id) gid: vec3<u32>) {{{ flat_index_2d() }} // 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.1 unmodified line23456789101112131415161718192021222324252627567293031323334358910113912131415411617181920464721222324252627285455565758596061626364656667686970717273747576777879808182838485868788899029303132333492353637383940414243444599100464748495051106525310810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014154555657585914360616263646566671 unmodified line "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`." } ], "inputs": { "X": { "dtype": "T" }, "Y": { "dtype": "T" }, "gate": { "dtype": "T" } }, "outputs": { "output": { "dtype": "T", "rank": "ranks.X", "shape": "shapes.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" } }, "tunables": { "WORKGROUP_SIZE": { "default": 256 } }, "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" "vec4Rows": "channels % 4 == 0 and numel(shapes.X) % 4 == 0", "scalar": "dtypes.T", "hidden": "channels if channels > 0 else 1" }, "constants": { "scalar": "dtypes.T", "usesF16": "dtypes.T == \"f16\"", "hidden": "channels if channels > 0 else 1" }, "when": ["gateContract"], "variants": [ { "id": "vec4", "priority": 30, "when": ["gateContract", "vec4Rows", "numel(shapes.X) > 0"], "constants": { "vec4": true, "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"" }, "when": ["vec4Rows", "numel(shapes.X) > 0"], "derive": { "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" }] } } { "arg": "X", "name": "x", "elementType": "$vectorScalar" }, { "arg": "Y", "name": "y", "elementType": "$vectorScalar" }, "gate", { "arg": "output", "elementType": "$vectorScalar" }, { "name": "params", "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.X) / 4" }] } ], "dispatch": { "threads": "numel(shapes.X) / 4", "workgroupSize": "tunables.WORKGROUP_SIZE" } "dispatch": { "x": "min(ceilDiv((numel(shapes.X) / 4), (tunables.WORKGROUP_SIZE)), 65535)", "y": "ceilDiv(ceilDiv((numel(shapes.X) / 4), (tunables.WORKGROUP_SIZE)), 65535)", "z": 1 } } ] }, { "id": "scalar", "priority": 0, "when": ["gateContract"], "constants": { "vec4": false }, "derive": { "vec4": false }, "passes": [ { "id": "main", "name": "GatedAdd.scalar", "shader": "gated-add.wgsl.jinja", "source": { "inputs": { "itemsPerInvocation": 4 } }, "derive": { "itemsPerInvocation": 4 }, "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)" }] } } { "arg": "X", "name": "x", "elementType": "$scalar" }, { "arg": "Y", "name": "y", "elementType": "$scalar" }, "gate", "output", { "name": "params", "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.X)" }] } ], "dispatch": { "threads": "ceilDiv(numel(shapes.X), 4)", "workgroupSize": "tunables.WORKGROUP_SIZE" } "dispatch": { "x": "min(ceilDiv((ceilDiv(numel(shapes.X), 4)), (tunables.WORKGROUP_SIZE)), 65535)", "y": "ceilDiv(ceilDiv((ceilDiv(numel(shapes.X), 4)), (tunables.WORKGROUP_SIZE)), 65535)", "z": 1 } } ] }1233456789101112131011121314151617161718192021{ "name": "com.microsoft.GatedAdd", "id": "_com_microsoft_gatedadd_webgpu_bd62a87", "id": "_com_microsoft_gatedadd_webgpu_913bb83", "version": 1, "license": "Apache-2.0", "backend": { "type": "webgpu" }, "digest": { "algorithm": "sha256", "files": { "bench.json": "Yo5szY3Ccb8NOxk0YLKXGApxHMFCENocZt9z2/GaJew=", "gated-add.wgsl.jinja": "1jbbfo5jhq8VJQHFSPM20gaFGAman4LWBgrssi9fQ5E=", "manifest.json": "t9s9nEQJMkL8JEPMCsIRRZhZlohyMFsAkyIW7GIwTRw=", "test.json": "5Gsy9SVd5u5t8bVgKOLTcNMfO4wqI7rFSjHIhPpLEgk=" "bench.json": "f/TmVS5V2Nom2d9yiZ8HU0gLA+HtwVePXaGO4EmsmoY=", "gated-add.wgsl.jinja": "6xWRekBrMasrrGGXBN6cHj1NeycAppVTPGz1QPRDnUQ=", "manifest.json": "a3y5tgF4lud3fMIhIIyomyBnUdsTqCeCaWuGJOSdW1U=", "test.json": "CgQChPZDm5zqD3shwXcy5E57YHqJlw8wSwSyTQeEvN4=" } }, "provenance": { "kernel": { "sha": "c928d21e6cc1310861cba3bafb75f5f679ecf5f3", "dirty": false } }, "webgpu": { "manifestSpec": "1.0", "specialized": true, "opPath": "ops/com.microsoft.GatedAdd" } "provenance": { "kernel": { "sha": "91d990483a174128daf7673f3f37a7c890493ae1", "dirty": false } }, "webgpu": { "manifestSpec": "2.0", "variants": { "vec4": ["gated-add.wgsl.jinja"], "scalar": ["gated-add.wgsl.jinja"] } }}1223491 unmodified lines96979810099100101102{ "op": "com.microsoft.GatedAdd", "cases": [ { "name": "rank3_rows_f32",91 unmodified lines { "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." "notes": "Expected values follow `output = X + round_to_T(Y * gate)` directly, and every intermediate is exactly representable in float32." }, "inputs": { "X": {README.md+15-11
build/webgpu/bench.json-1
build/webgpu/gated-add.wgsl.jinja+24-35
build/webgpu/manifest.json+31-110
build/webgpu/metadata.json+10-7
build/webgpu/test.json+1-2