sync 91d990483a17
Browse filesFiles changed (6)
17 unmodified lines181920212223242526272829303132333 unmodified lines373839404142431 unmodified line4546474849505152535417 unmodified lines## Inputs| Name | Bind key | Logical dtype | Rank | Shape | Description | Presence || --- | --- | --- | --- | --- | --- | --- || `A` | `A` | `T` | — | — | The main input tensor of any shape. | required || `B` | `B` | `T` | `1` | — | 1-D bias tensor whose length equals the last dimension of `A`. | required |## Outputs| Name | Bind key | Logical dtype | Rank | Shape | Description | Presence || --- | --- | --- | --- | --- | --- | --- || `C` | `C` | `T` | same as `A` | same as `A` | Output tensor after applying GELU to `A + B`; same shape as `A`. | required |## Type constraints3 unmodified lines## 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 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.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.17 unmodified lines181920212223242526272829303132333 unmodified lines373839404142431 unmodified line454647484950515253545556575817 unmodified lines## Inputs| Name | Logical dtype | Rank | Shape | Description | Presence || --- | --- | --- | --- | --- | --- || `A` | `T` | — | — | The main input tensor of any shape. | required || `B` | `T` | `1` | — | 1-D bias tensor whose length equals the last dimension of `A`. | required |## Outputs| Name | Logical dtype | Rank | Shape | Description | Presence || --- | --- | --- | --- | --- | --- || `C` | `T` | same as `A` | same as `A` | Output tensor after applying GELU to `A + B`; same shape as `A`. | required |## Type constraints3 unmodified lines## Files- [`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````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.
12345{"op": "com.microsoft.BiasGelu","tunableSpace": { "WORKGROUP_SIZE": [64, 128, 256] },"cases": [{1234{"tunableSpace": { "WORKGROUP_SIZE": [64, 128, 256] },"cases": [{
12345678910111213141516171819202122232425262 unmodified lines2930313233343536373839404142434445464748495051525354555657582 unmodified lines616263646566676869707172737475767778798081828384858687888990{% if usesF16 %}enable f16;{% endif %}{{ env.wgsl.resourceDeclarations }}{% set wg = workgroupSize if workgroupSize is defined else tunables.WORKGROUP_SIZE %}// Bias plus GELU, with a specialization-selected tanh or erf approximation.// The optional bias is a rank-1 vector broadcast over the innermost (hidden)// axis: bias index = element_index % HIDDEN. The `vec4` path requires// HIDDEN % 4 == 0 and numel % 4 == 0 so a vec4 group never crosses the hidden// axis (the bias slice is then contiguous). `vec4Tail` keeps scalar bindings but// evaluates four guarded lanes per invocation, so odd hidden sizes retain the// same parallel efficiency without crossing row/bias boundaries. Gelu math and overflow guards match// the vectorized unary implementation: tanh saturates to +/-1 by |x|~9, and the// erf path uses the same rational approximation as Gelu.{% if approximate == "erf" %}fn erf_approx(x: f32) -> f32 {let ax = abs(x);// The polynomial has a small nonzero floor near zero. Use erf(x) ~=// 2/sqrt(pi)*x below 2^-20 to preserve erf(0) == 0, odd symmetry, and the// correctly rounded f32 result. The exactly representable threshold keeps// scalar and vector branching identical. NaN falls through to the polynomial// and propagates.if (ax < 9.5367431640625e-7) {return 1.1283791670955126 * x;}2 unmodified lineslet y = 1.0 - (((((1.061405429 * t - 1.453152027) * t) + 1.421413741) * t - 0.284496736) * t + 0.254829592) * t * exp(-(ax * ax));return sign * y;}{% else %}fn tanh_safe(x: f32) -> f32 {if (x > 10.0) { return 1.0; }if (x < -10.0) { return -1.0; }return tanh(x);}{% endif %}fn gelu_value(v: f32) -> f32 {{% if approximate == "erf" %}return 0.5 * v * (1.0 + erf_approx(v * 0.7071067811865476));{% else %}return 0.5 * v * (1.0 + tanh_safe(0.7978845608028654 * (v + 0.044715 * v * v * v)));{% endif %}}{% if hasBias %}const HIDDEN: u32 = {{ hidden }}u;{% endif %}@compute @workgroup_size({{ wg }})fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {// 2D-folded flat index: gid.y carries the high bits past the// maxComputeWorkgroupsPerDimension dispatch limit (outputs > 16.7M elements).let i = gid.x + gid.y * nwg.x * {{ wg }}u;if (i >= params.count) {return;}2 unmodified lines{% for lane in range(4) %}if (base + {{ lane }}u < params.count) {let xv{{ lane }} = f32(x[base + {{ lane }}u]);{% if hasBias %}let v{{ lane }} = xv{{ lane }} + f32(bias[(base + {{ lane }}u) % HIDDEN]);{% else %}let v{{ lane }} = xv{{ lane }};{% endif %}y[base + {{ lane }}u] = {{ scalar }}(gelu_value(v{{ lane }}));}{% endfor %}{% elif vec4 %}let xv = vec4<f32>(x[i]);{% if hasBias %}let bcol = (i * 4u) % HIDDEN;let v = xv + vec4<f32>(f32(bias[bcol]), f32(bias[bcol + 1u]), f32(bias[bcol + 2u]), f32(bias[bcol + 3u]));{% else %}let v = xv;{% endif %}y[i] = vec4<{{ scalar }}>(vec4<f32>(gelu_value(v.x), gelu_value(v.y), gelu_value(v.z), gelu_value(v.w)));{% else %}let xv = f32(x[i]);{% if hasBias %}let v = xv + f32(bias[i % HIDDEN]);{% else %}let v = xv;{% endif %}y[i] = {{ scalar }}(gelu_value(v));{% endif %}}12345678910111213141516172 unmodified lines20212223242526272829303132333435362 unmodified lines394041424344454647484950515253545556{{ env.wgsl.resourceDeclarations }}{% set wg = workgroupSize if workgroupSize is defined else tunables.WORKGROUP_SIZE %}// Computes GELU, optionally after adding a rank-1 bias broadcast over the// innermost axis. A present bias uses element_index % hidden_extent. The `vec4`// path requires the innermost extent and element count to be divisible by four;// `vec4Tail` instead uses scalar bindings with four guarded lanes.// GELU uses the rational erf approximation defined below.fn erf_approx(x: f32) -> f32 {let ax = abs(x);// The polynomial has a small nonzero floor near zero. Use erf(x) ~=// 2/sqrt(pi)*x below 2^-20 to preserve erf(0) == 0, odd symmetry, and the// small-input linear behavior. The threshold is exactly representable in// f32. NaN falls through to the polynomial and propagates.if (ax < 9.5367431640625e-7) {return 1.1283791670955126 * x;}2 unmodified lineslet y = 1.0 - (((((1.061405429 * t - 1.453152027) * t) + 1.421413741) * t - 0.284496736) * t + 0.254829592) * t * exp(-(ax * ax));return sign * y;}fn gelu_value(v: f32) -> f32 {return 0.5 * v * (1.0 + erf_approx(v * 0.7071067811865476));}const HIDDEN: u32 = {{ hidden | default(0) }}u;@compute @workgroup_size({{ wg }})fn main(@builtin(global_invocation_id) gid: vec3<u32>) {// 2D-folded flat index: gid.y carries the high bits past the// per-axis dispatch fold width (outputs > 16.7M elements).let i = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wg }}u;if (i >= params.count) {return;}2 unmodified lines{% for lane in range(4) %}if (base + {{ lane }}u < params.count) {let xv{{ lane }} = f32(x[base + {{ lane }}u]);let v{{ lane }} = xv{{ lane }} + f32(bias[(base + {{ lane }}u) % HIDDEN]);y[base + {{ lane }}u] = {{ scalar }}(gelu_value(v{{ lane }}));}{% endfor %}{% elif vec4 %}let xv = vec4<f32>(x[i]);let bcol = (i * 4u) % HIDDEN;let v = xv + vec4<f32>(f32(bias[bcol]), f32(bias[bcol + 1u]), f32(bias[bcol + 2u]), f32(bias[bcol + 3u]));y[i] = vec4<{{ scalar }}>(vec4<f32>(gelu_value(v.x), gelu_value(v.y), gelu_value(v.z), gelu_value(v.w)));{% else %}let xv = f32(x[i]);let v = xv + f32(bias[i % HIDDEN]);y[i] = {{ scalar }}(gelu_value(v));{% endif %}}
1 unmodified line234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465667 unmodified lines74757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211225 unmodified lines1281291301311321331341351361371381391401411421431441455 unmodified lines1511521531541551561571581 unmodified line"domain": "com.microsoft","name": "BiasGelu","sinceVersion": 1,"description": "Applies GELU to `A + B`, where the 1-D bias `B` is broadcast along the last dimension of `A`. This implementation supports float16 and float32; the schema's double and bfloat16 types are not implemented.","inputs": [{ "role": "A", "dtype": "T", "description": "The main input tensor of any shape." },{"role": "B","dtype": "T","rank": 1,"description": "1-D bias tensor whose length equals the last dimension of `A`."}],"outputs": [{"role": "C","dtype": "T","rank": "ranks.A","shape": "shapes.A","description": "Output tensor after applying GELU to `A + B`; same shape as `A`."}],"typeConstraints": { "T": ["float32", "float16"] },"args": {"A": { "kind": "tensor", "semantic": "A", "role": "input" },"B": { "kind": "tensor", "semantic": "B", "role": "input" },"C": { "kind": "tensor", "semantic": "C", "role": "output" }},"tunables": { "WORKGROUP_SIZE": 64 },"derive": {"deviceWorkgroupCap": "min(device.limits.maxComputeInvocationsPerWorkgroup, device.limits.maxComputeWorkgroupSizeX)","workgroupOk": "tunables.WORKGROUP_SIZE > 0 and tunables.WORKGROUP_SIZE <= deviceWorkgroupCap","baseOk": "workgroupOk and ranks.A >= 1 and ranks.C == ranks.A and sameShape(shapes.A, shapes.C) and f16Ok(dtypes.T) and ranks.B == 1 and dim(shapes.B, 0) == dim(shapes.A, ranks.A - 1)","vec4Ok": "numel(shapes.A) > 0 and numel(shapes.A) % 4 == 0 and dim(shapes.A, ranks.A - 1) % 4 == 0"},"bindingSets": {"scalarTail": [{ "name": "x", "arg": "A", "semantic": "A", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" },{"name": "bias","arg": "B","semantic": "B","buffer": { "type": "read-only-storage" },"elementType": "$scalar"},{ "name": "y", "arg": "C", "semantic": "C", "buffer": { "type": "storage" }, "elementType": "$scalar" },{"name": "params","semantic": "kernel.params","buffer": { "type": "uniform" },"struct": { "name": "Params", "fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.A)" }] }}]},"variants": [{"id": "vec4","priority": 20,"when": ["baseOk", "vec4Ok"],"constants": {"scalar": "dtypes.T","usesF16": "dtypes.T == \"f16\"","approximate": "\"erf\"","vec4": true,"vec4Tail": false,7 unmodified lines"name": "BiasGelu.vec4","shader": "elementwise-bias-gelu.wgsl.jinja","bindings": [{"name": "x","arg": "A","semantic": "A","buffer": { "type": "read-only-storage" },"elementType": "$vectorScalar"},{"name": "bias","arg": "B","semantic": "B","buffer": { "type": "read-only-storage" },"elementType": "$scalar","length": "$hidden"},{"name": "y","arg": "C","semantic": "C","buffer": { "type": "storage" },"elementType": "$vectorScalar"},{"name": "params","semantic": "kernel.params","buffer": { "type": "uniform" },"struct": {"name": "Params","fields": [{ "name": "count", "type": "u32", "value": "numel(shapes.A) / 4" }]}}],"dispatch": { "threads": "numel(shapes.A) / 4", "workgroupSize": "tunables.WORKGROUP_SIZE" }}]},{"id": "vec4_tail","priority": 10,"when": ["baseOk", "numel(shapes.A) > 0"],"constants": {"scalar": "dtypes.T","usesF16": "dtypes.T == \"f16\"","approximate": "\"erf\"","vec4": false,"vec4Tail": true,5 unmodified lines"id": "main","name": "BiasGelu.vec4Tail","shader": "elementwise-bias-gelu.wgsl.jinja","bindings": "scalarTail","dispatch": { "threads": "ceilDiv(numel(shapes.A), 4)", "workgroupSize": "tunables.WORKGROUP_SIZE" }}]},{"id": "scalar","priority": 0,"when": ["baseOk", "true"],"constants": {"scalar": "dtypes.T","usesF16": "dtypes.T == \"f16\"","approximate": "\"erf\"","vec4": false,"vec4Tail": false,5 unmodified lines"id": "main","name": "BiasGelu.scalar","shader": "elementwise-bias-gelu.wgsl.jinja","bindings": "scalarTail","dispatch": { "threads": "numel(shapes.A)", "workgroupSize": "tunables.WORKGROUP_SIZE" }}]}1 unmodified line23456789101112131415161718192021222324257 unmodified lines333435363738394041424344454647484950515253545556575 unmodified lines63646566676869707172737475767778798081828384858687885 unmodified lines9495969798991001011021031041051061071081091101 unmodified line"domain": "com.microsoft","name": "BiasGelu","sinceVersion": 1,"inputs": { "A": { "dtype": "T" }, "B": { "dtype": "T", "rank": 1 } },"outputs": { "C": { "dtype": "T", "rank": "ranks.A", "shape": "shapes.A" } },"typeConstraints": { "T": ["float32", "float16"] },"tunables": { "WORKGROUP_SIZE": { "default": 64 } },"derive": {"deviceWorkgroupCap": "min(device.limits.maxComputeInvocationsPerWorkgroup, device.limits.maxComputeWorkgroupSizeX)","workgroupOk": "tunables.WORKGROUP_SIZE > 0 and tunables.WORKGROUP_SIZE <= deviceWorkgroupCap","baseOk": "workgroupOk and ranks.A >= 1 and ranks.C == ranks.A and sameShape(shapes.A, shapes.C) and f16Ok(dtypes.T) and ranks.B == 1 and dim(shapes.B, 0) == dim(shapes.A, ranks.A - 1)","vec4Ok": "numel(shapes.A) > 0 and numel(shapes.A) % 4 == 0 and dim(shapes.A, ranks.A - 1) % 4 == 0"},"when": ["baseOk"],"variants": [{"id": "vec4","priority": 20,"when": ["vec4Ok"],"derive": {"scalar": "dtypes.T","approximate": "\"erf\"","vec4": true,"vec4Tail": false,7 unmodified lines"name": "BiasGelu.vec4","shader": "elementwise-bias-gelu.wgsl.jinja","bindings": [{ "arg": "A", "name": "x", "elementType": "$vectorScalar" },{ "arg": "B", "name": "bias", "elementType": "$scalar", "length": "$hidden" },{ "arg": "C", "name": "y", "elementType": "$vectorScalar" },{ "name": "params", "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.A) / 4" }] }],"dispatch": {"x": "min(ceilDiv((numel(shapes.A) / 4), (tunables.WORKGROUP_SIZE)), 65535)","y": "ceilDiv(ceilDiv((numel(shapes.A) / 4), (tunables.WORKGROUP_SIZE)), 65535)","z": 1}}]},{"id": "vec4_tail","priority": 10,"when": ["numel(shapes.A) > 0"],"derive": {"scalar": "dtypes.T","approximate": "\"erf\"","vec4": false,"vec4Tail": true,5 unmodified lines"id": "main","name": "BiasGelu.vec4Tail","shader": "elementwise-bias-gelu.wgsl.jinja","bindings": [{ "arg": "A", "name": "x", "elementType": "$scalar" },{ "arg": "B", "name": "bias", "elementType": "$scalar" },{ "arg": "C", "name": "y", "elementType": "$scalar" },{ "name": "params", "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.A)" }] }],"dispatch": {"x": "min(ceilDiv((ceilDiv(numel(shapes.A), 4)), (tunables.WORKGROUP_SIZE)), 65535)","y": "ceilDiv(ceilDiv((ceilDiv(numel(shapes.A), 4)), (tunables.WORKGROUP_SIZE)), 65535)","z": 1}}]},{"id": "scalar","priority": 0,"when": ["true"],"derive": {"scalar": "dtypes.T","approximate": "\"erf\"","vec4": false,"vec4Tail": false,5 unmodified lines"id": "main","name": "BiasGelu.scalar","shader": "elementwise-bias-gelu.wgsl.jinja","bindings": [{ "arg": "A", "name": "x", "elementType": "$scalar" },{ "arg": "B", "name": "bias", "elementType": "$scalar" },{ "arg": "C", "name": "y", "elementType": "$scalar" },{ "name": "params", "struct": [{ "name": "count", "type": "u32", "value": "numel(shapes.A)" }] }],"dispatch": {"x": "min(ceilDiv((numel(shapes.A)), (tunables.WORKGROUP_SIZE)), 65535)","y": "ceilDiv(ceilDiv((numel(shapes.A)), (tunables.WORKGROUP_SIZE)), 65535)","z": 1}}]}
123456789101112131415161718{"name": "com.microsoft.BiasGelu","id": "_com_microsoft_biasgelu_webgpu_0db9e22","version": 1,"license": "Apache-2.0","backend": { "type": "webgpu" },"digest": {"algorithm": "sha256","files": {"bench.json": "UiPLr8ZUfUCDcEfplbEhxBCd01I/EdY03qb4n/Qtzyc=","elementwise-bias-gelu.wgsl.jinja": "Eg8N2jJCMce+IsYNcCzuxvs8CSv7yTQXdorvv+c0L58=","manifest.json": "YkUZuF88k5utKaSJd858ft/Msp3otF/iijavqWmOK3E=","test.json": "2XoIgkpiRq0YEMEp23vi/cmZk8pZFcSt7x7vVzsBRyk="}},"provenance": { "kernel": { "sha": "2e7068faf55e7f43df740015f6d1ee49391a41c5", "dirty": false } },"webgpu": { "manifestSpec": "1.0", "specialized": true, "opPath": "ops/com.microsoft.BiasGelu" }}12345678910111213141516171819202122232425{"name": "com.microsoft.BiasGelu","id": "_com_microsoft_biasgelu_webgpu_36033e5","version": 1,"license": "Apache-2.0","backend": { "type": "webgpu" },"digest": {"algorithm": "sha256","files": {"bench.json": "UU7d/EXIwpUa3ajXE03pfLtNOL6zmaBTfs+dNjHpHO0=","elementwise-bias-gelu.wgsl.jinja": "DYoecWVFPppr2OxLpHEZ+fwpgvhdG5b9dd9g+iJCdag=","manifest.json": "hdBcYemmctslZc/98IFMZSmOLqdy3VVeclZjzZO/kJk=","test.json": "YTbMVtlxCU0qheLWicG9H2oZVJeKSTx77UCb8d2PVC8="}},"provenance": { "kernel": { "sha": "91d990483a174128daf7673f3f37a7c890493ae1", "dirty": false } },"webgpu": {"manifestSpec": "2.0","variants": {"vec4": ["elementwise-bias-gelu.wgsl.jinja"],"vec4_tail": ["elementwise-bias-gelu.wgsl.jinja"],"scalar": ["elementwise-bias-gelu.wgsl.jinja"]}}}
12345183 unmodified lines189190191192193194195{"op": "com.microsoft.BiasGelu","cases": [{"name": "dispatch_cliff_vec4_f32",183 unmodified lines"provenance": {"source": "onnxruntime/test/contrib_ops/element_wise_ops_test.cc","test": "BiasGeluTest.Float","notes": "Scalar-path companion for BiasGelu subnormal linear-region behavior with zero bias."},"inputs": {"A": { "dtype": "float32", "shape": [1, 3], "data": { "kind": "values", "values": [1e-40, -1e-40, 1e-38] } },1234183 unmodified lines188189190191192193194{"cases": [{"name": "dispatch_cliff_vec4_f32",183 unmodified lines"provenance": {"source": "onnxruntime/test/contrib_ops/element_wise_ops_test.cc","test": "BiasGeluTest.Float","notes": "Zero bias isolates subnormal linear-region behavior on the scalar path."},"inputs": {"A": { "dtype": "float32", "shape": [1, 3], "data": { "kind": "values", "values": [1e-40, -1e-40, 1e-38] } },
README.md+14-10
build/webgpu/bench.json-1
build/webgpu/elementwise-bias-gelu.wgsl.jinja+11-45
build/webgpu/manifest.json+41-89
build/webgpu/metadata.json+14-7
build/webgpu/test.json+1-2