sync 91d990483a17
Browse filesFiles changed (6)
17 unmodified lines18192021212223242526272324252627282930313132333433343536378 unmodified lines46474849495051521 unmodified line5455565758575859606162636465666717 unmodified lines
## Inputs
| Name | Bind key | Logical dtype | WebGPU storage | Rank | Shape | Description | Presence || Name | Upstream name | Logical dtype | WebGPU storage | Rank | Shape | Description | Presence || --- | --- | --- | --- | --- | --- | --- | --- || `emb` | `embT` | `U` | `float32` | `3` | — | Rotary angles with shape `(batch_size, seq_len, dim)`, shared by every head. | required || `q` | `qT` | `T` | same as logical dtype | `4` | — | Query state with shape `(batch_size, num_heads, seq_len, dim)`. | required || `q_rot` | `qRotT` | `T` | same as logical dtype | `4` | — | Half-rotated query state, same shape as `q`. | required || `k` | `kT` | `T` | same as logical dtype | `4` | — | Key state, same shape as `q`. | required || `k_rot` | `kRotT` | `T` | same as logical dtype | `4` | — | Half-rotated key state, same shape as `q`. | required || `embT` | `emb` | `U` | `float32` | `3` | — | Rotary angles with shape `(batch_size, seq_len, dim)`, shared by every head. | required || `qT` | `q` | `T` | same as logical dtype | `4` | — | Query state with shape `(batch_size, num_heads, seq_len, dim)`. | required || `qRotT` | `q_rot` | `T` | same as logical dtype | `4` | — | Half-rotated query state, same shape as `q`. | required || `kT` | `k` | `T` | same as logical dtype | `4` | — | Key state, same shape as `q`. | required || `kRotT` | `k_rot` | `T` | same as logical dtype | `4` | — | Half-rotated key state, same shape as `q`. | required |
## Outputs
| Name | Bind key | Logical dtype | Rank | Shape | Description | Presence || Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence || --- | --- | --- | --- | --- | --- | --- || `output1` | `output1T` | `T` | same as `q` | same as `q` | Rotary-embedded query, same shape as `q`. | required || `output2` | `output2T` | `T` | same as `q` | same as `q` | Rotary-embedded key, same shape as `q`. | required || `output1T` | `output1` | `T` | same as `qT` | same as `qT` | Rotary-embedded query, same shape as `q`. | required || `output2T` | `output2` | `T` | same as `qT` | same as `qT` | Rotary-embedded key, same shape as `q`. | required |
## Type constraints
8 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.GemmaRotaryEmbedding", "tunableSpace": { "workgroupSize": [64, 128, 256] }, "cases": [ {11 unmodified lines121314151615161819171819202111 unmodified lines{% endif %}
@compute @workgroup_size(WG, 1, 1)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 past the // maxComputeWorkgroupsPerDimension limit. Reduces to gid.x when nwg.y == 1. let index = gid.x + gid.y * nwg.x * WG; // per-axis dispatch fold width. Reduces to gid.x when the dispatch does not fold. let index = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * WG; if (index >= params.count) { return; }1 unmodified line234567891011121314151617181920212223242526272829303132333435363738394041424344454647567891011121314151649171819202 unmodified lines232425585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164262728293031323334171172353637384 unmodified lines434445183184464748495051525354555657585960616263646566676869701 unmodified line727374192193757677784 unmodified lines83848520420586878889909192939495969798991001011021031041051061071081091101 unmodified line "domain": "com.microsoft", "name": "GemmaRotaryEmbedding", "sinceVersion": 1, "description": "Fuses the Gemma rotary-embedding tail: computes `sin` and `cos` from float32 `emb`, casts them to float16, then evaluates `q * cos + q_rot * sin` and the corresponding expression for `k`. `emb` has shape `(batch, seq, dim)` and is broadcast over the head axis of the `(batch, heads, seq, dim)` operands. Each product is rounded to float16 before the addition.", "inputs": [ { "role": "emb", "dtype": "U", "rank": 3, "description": "Rotary angles with shape `(batch_size, seq_len, dim)`, shared by every head." }, { "role": "q", "dtype": "T", "rank": 4, "description": "Query state with shape `(batch_size, num_heads, seq_len, dim)`." }, { "role": "q_rot", "dtype": "T", "rank": 4, "description": "Half-rotated query state, same shape as `q`." }, { "role": "k", "dtype": "T", "rank": 4, "description": "Key state, same shape as `q`." }, { "role": "k_rot", "dtype": "T", "rank": 4, "description": "Half-rotated key state, same shape as `q`." } ], "outputs": [ { "role": "output1", "dtype": "T", "rank": "ranks.qT", "shape": "shapes.qT", "description": "Rotary-embedded query, same shape as `q`." }, { "role": "output2", "dtype": "T", "rank": "ranks.qT", "shape": "shapes.qT", "description": "Rotary-embedded key, same shape as `q`." } ], "typeConstraints": { "T": ["float16"], "U": ["float32"] }, "args": { "embT": { "kind": "tensor", "semantic": "emb", "role": "input", "dtype": "float32" }, "qT": { "kind": "tensor", "semantic": "q", "role": "input" }, "qRotT": { "kind": "tensor", "semantic": "q_rot", "role": "input" }, "kT": { "kind": "tensor", "semantic": "k", "role": "input" }, "kRotT": { "kind": "tensor", "semantic": "k_rot", "role": "input" }, "output1T": { "kind": "tensor", "semantic": "output1", "role": "output" }, "output2T": { "kind": "tensor", "semantic": "output2", "role": "output" } "inputs": { "embT": { "onnx": "emb", "dtype": "U", "rank": 3, "storage": "float32" }, "qT": { "onnx": "q", "dtype": "T", "rank": 4 }, "qRotT": { "onnx": "q_rot", "dtype": "T", "rank": 4 }, "kT": { "onnx": "k", "dtype": "T", "rank": 4 }, "kRotT": { "onnx": "k_rot", "dtype": "T", "rank": 4 } }, "outputs": { "output1T": { "onnx": "output1", "dtype": "T", "rank": "ranks.qT", "shape": "shapes.qT" }, "output2T": { "onnx": "output2", "dtype": "T", "rank": "ranks.qT", "shape": "shapes.qT" } }, "typeConstraints": { "T": ["float16"], "U": ["float32"] }, "tunables": { "workgroupSize": 256 }, "tunables": { "workgroupSize": { "default": 256 } }, "derive": { "batchSize": "dim(shapes.qT, 0)", "numHeads": "dim(shapes.qT, 1)",2 unmodified lines "sameAsQ": "ranks.qRotT == 4 and ranks.kT == 4 and ranks.kRotT == 4 and sameShape(shapes.qRotT, shapes.qT) and sameShape(shapes.kT, shapes.qT) and sameShape(shapes.kRotT, shapes.qT)", "dtypesMatch": "tensorDtypes.qRotT == tensorDtypes.qT and tensorDtypes.kT == tensorDtypes.qT and tensorDtypes.kRotT == tensorDtypes.qT and tensorDtypes.output1T == tensorDtypes.qT and tensorDtypes.output2T == tensorDtypes.qT", "contract": "ranks.qT == 4 and ranks.embT == 3 and sameAsQ and dtypesMatch and tensorDtypes.qT == \"float16\" and f16Ok(dtypes.T) and tensorDtypes.embT == \"float32\" and dim(shapes.embT, 0) == batchSize and dim(shapes.embT, 1) == seqLen and dim(shapes.embT, 2) == headDim and sameShape(shapes.output1T, shapes.qT) and sameShape(shapes.output2T, shapes.qT)", "vec4Ok": "headDim % 4 == 0" }, "bindingSets": { "scalar": [ { "name": "emb", "arg": "embT", "semantic": "emb", "buffer": { "type": "read-only-storage" }, "elementType": "f32" }, { "name": "q", "arg": "qT", "semantic": "q", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" }, { "name": "q_rot", "arg": "qRotT", "semantic": "q_rot", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" }, { "name": "k", "arg": "kT", "semantic": "k", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" }, { "name": "k_rot", "arg": "kRotT", "semantic": "k_rot", "buffer": { "type": "read-only-storage" }, "elementType": "$scalar" }, { "name": "output1", "arg": "output1T", "semantic": "output1", "buffer": { "type": "storage" }, "elementType": "$scalar" }, { "name": "output2", "arg": "output2T", "semantic": "output2", "buffer": { "type": "storage" }, "elementType": "$scalar" }, { "name": "params", "semantic": "kernel.params", "buffer": { "type": "uniform" }, "struct": { "name": "Params", "fields": [ { "name": "count", "type": "u32", "value": "numel(shapes.qT)" }, { "name": "seqDim", "type": "u32", "value": "(seqLen * headDim)" }, { "name": "headSeqDim", "type": "u32", "value": "(numHeads * seqLen * headDim)" } ] } } ], "vec4": [ { "name": "emb", "arg": "embT", "semantic": "emb", "buffer": { "type": "read-only-storage" }, "elementType": "vec4<f32>" }, { "name": "q", "arg": "qT", "semantic": "q", "buffer": { "type": "read-only-storage" }, "elementType": "$vector" }, { "name": "q_rot", "arg": "qRotT", "semantic": "q_rot", "buffer": { "type": "read-only-storage" }, "elementType": "$vector" }, { "name": "k", "arg": "kT", "semantic": "k", "buffer": { "type": "read-only-storage" }, "elementType": "$vector" }, { "name": "k_rot", "arg": "kRotT", "semantic": "k_rot", "buffer": { "type": "read-only-storage" }, "elementType": "$vector" }, { "name": "output1", "arg": "output1T", "semantic": "output1", "buffer": { "type": "storage" }, "elementType": "$vector" }, { "name": "output2", "arg": "output2T", "semantic": "output2", "buffer": { "type": "storage" }, "elementType": "$vector" }, { "name": "params", "semantic": "kernel.params", "buffer": { "type": "uniform" }, "struct": { "name": "Params", "fields": [ { "name": "count", "type": "u32", "value": "numel(shapes.qT) / 4" }, { "name": "seqDim", "type": "u32", "value": "(seqLen * headDim) / 4" }, { "name": "headSeqDim", "type": "u32", "value": "(numHeads * seqLen * headDim) / 4" } ] } } ] "vec4Ok": "(seqLen * headDim) % 4 == 0" }, "when": ["contract", "tunables.workgroupSize >= 1", "floor(tunables.workgroupSize) == tunables.workgroupSize", "tunables.workgroupSize <= device.limits.maxComputeInvocationsPerWorkgroup", "tunables.workgroupSize <= device.limits.maxComputeWorkgroupSizeX"], "variants": [ { "id": "vec4", "priority": 10, "when": ["vec4Ok"], "requires": { "features": ["shader-f16"] }, "when": ["contract", "vec4Ok", "tunables.workgroupSize >= 1", "floor(tunables.workgroupSize) == tunables.workgroupSize", "tunables.workgroupSize <= device.limits.maxComputeInvocationsPerWorkgroup", "tunables.workgroupSize <= device.limits.maxComputeWorkgroupSizeX"], "constants": { "derive": { "vec4": true, "scalar": "dtypes.T", "vector": "\"vec4<f16>\"",4 unmodified lines "id": "main", "name": "GemmaRotaryEmbedding.Vec4", "shader": "gemma-rotary-embedding.wgsl.jinja", "bindings": "vec4", "dispatch": { "threads": "numel(shapes.qT) / 4", "workgroupSize": "constants.workgroupSize" } "bindings": [ { "arg": "embT", "name": "emb", "elementType": "vec4<f32>" }, { "arg": "qT", "name": "q", "elementType": "$vector" }, { "arg": "qRotT", "name": "q_rot", "elementType": "$vector" }, { "arg": "kT", "name": "k", "elementType": "$vector" }, { "arg": "kRotT", "name": "k_rot", "elementType": "$vector" }, { "arg": "output1T", "name": "output1", "elementType": "$vector" }, { "arg": "output2T", "name": "output2", "elementType": "$vector" }, { "name": "params", "struct": [ { "name": "count", "type": "u32", "value": "numel(shapes.qT) / 4" }, { "name": "seqDim", "type": "u32", "value": "(seqLen * headDim) / 4" }, { "name": "headSeqDim", "type": "u32", "value": "(numHeads * seqLen * headDim) / 4" } ] } ], "dispatch": { "x": "min(ceilDiv((numel(shapes.qT) / 4), (workgroupSize)), 65535)", "y": "ceilDiv(ceilDiv((numel(shapes.qT) / 4), (workgroupSize)), 65535)", "z": 1 } } ] },1 unmodified line "id": "scalar", "priority": 0, "requires": { "features": ["shader-f16"] }, "when": ["contract", "tunables.workgroupSize >= 1", "floor(tunables.workgroupSize) == tunables.workgroupSize", "tunables.workgroupSize <= device.limits.maxComputeInvocationsPerWorkgroup", "tunables.workgroupSize <= device.limits.maxComputeWorkgroupSizeX"], "constants": { "derive": { "vec4": false, "scalar": "dtypes.T", "vector": "\"vec4<f16>\"",4 unmodified lines "id": "main", "name": "GemmaRotaryEmbedding", "shader": "gemma-rotary-embedding.wgsl.jinja", "bindings": "scalar", "dispatch": { "threads": "max(1, numel(shapes.qT))", "workgroupSize": "constants.workgroupSize" } "bindings": [ { "arg": "embT", "name": "emb" }, { "arg": "qT", "name": "q", "elementType": "$scalar" }, { "arg": "qRotT", "name": "q_rot", "elementType": "$scalar" }, { "arg": "kT", "name": "k", "elementType": "$scalar" }, { "arg": "kRotT", "name": "k_rot", "elementType": "$scalar" }, { "arg": "output1T", "name": "output1", "elementType": "$scalar" }, { "arg": "output2T", "name": "output2", "elementType": "$scalar" }, { "name": "params", "struct": [ { "name": "count", "type": "u32", "value": "numel(shapes.qT)" }, { "name": "seqDim", "type": "u32", "value": "(seqLen * headDim)" }, { "name": "headSeqDim", "type": "u32", "value": "(numHeads * seqLen * headDim)" } ] } ], "dispatch": { "x": "min(ceilDiv((max(1, numel(shapes.qT))), (workgroupSize)), 65535)", "y": "ceilDiv(ceilDiv((max(1, numel(shapes.qT))), (workgroupSize)), 65535)", "z": 1 } } ] }1233456789101112131011121314151617161718192021{ "name": "com.microsoft.GemmaRotaryEmbedding", "id": "_com_microsoft_gemmarotaryembedding_webgpu_c64044f", "id": "_com_microsoft_gemmarotaryembedding_webgpu_e5a6aca", "version": 1, "license": "Apache-2.0", "backend": { "type": "webgpu" }, "digest": { "algorithm": "sha256", "files": { "bench.json": "/3JoilqqNUy3rMDPsbi0npsxk9oZgl6Db/2k+439H0c=", "gemma-rotary-embedding.wgsl.jinja": "w+t56Q+FUBau7k0eWwrulSaGWFEKYch4aCunpG3sbBs=", "manifest.json": "FK8aZrRWKnUBl9hUi7MrNAX7nE9/6pz89Wh/AL5kCDQ=", "test.json": "KnP1fOvNgEvi3e9lrItvga2K6sLswyE5trl1BhX72Js=" "bench.json": "E1026CMMv9Pppl2+JpGoj3dWCSmk8tZUEwBYo3UCiD4=", "gemma-rotary-embedding.wgsl.jinja": "t4wKCJ/E3156iIqQTq1d2XGqMBA83kI0EAADYck7NI0=", "manifest.json": "41x/WJ1/AyRVHsYgM3nIqECf6Z8zix9MBzVgLIaLILE=", "test.json": "WwfG+ffVK8tAHKvZCOkixu97BOEIQjEdR4NSOSLA9dc=" } }, "provenance": { "kernel": { "sha": "2e7068faf55e7f43df740015f6d1ee49391a41c5", "dirty": false } }, "webgpu": { "manifestSpec": "1.0", "specialized": true, "opPath": "ops/com.microsoft.GemmaRotaryEmbedding" } "provenance": { "kernel": { "sha": "91d990483a174128daf7673f3f37a7c890493ae1", "dirty": false } }, "webgpu": { "manifestSpec": "2.0", "variants": { "vec4": ["gemma-rotary-embedding.wgsl.jinja"], "scalar": ["gemma-rotary-embedding.wgsl.jinja"] } }}12234576789123 unmodified lines13313413513713613713813957 unmodified lines197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276{ "op": "com.microsoft.GemmaRotaryEmbedding", "cases": [ { "name": "f16_vec4", "provenance": { "notes": "float16 operands with a float32 emb, which is the type pair ONNX Runtime constrains this operator to. Sin and cos are computed at float32 and rounded once." "notes": "Float16 operands with float32 emb. Sin and cos are computed in float32 and rounded once." }, "inputs": { "embT": {123 unmodified lines { "name": "pinned_batch2_emb_broadcast", "provenance": { "notes": "Hand-computed by an independent model that Unsqueezes emb to (batch, 1, seq, dim) and broadcasts with real array shapes, never forming a flat index -- which is the step ONNX Runtime's own unit test gets wrong: it indexes emb by num_heads where the CUDA kernel uses seq_len, and its only case has batch_size 1, where the wrong term is multiplied by zero. heads (3) and seq_len (2) differ here and batch is 2, so the two disagree." "notes": "Expected values broadcast `emb` as `(batch, 1, sequence, dimension)`. Batch 2, three heads, and sequence length 2 exercise sequence-based embedding indexing independently of the head index." }, "inputs": { "embT": {57 unmodified lines "tolerance": 0.005 } } }, { "name": "f16_vec4_dim6_seq4_straddles_heads", "provenance": { "notes": "headDim 6 is not a multiple of four but seqLen * headDim = 24 is: the vec4 route admits it and its vec4s straddle head-dim boundaries within one (batch, seq) block." }, "inputs": { "embT": { "dtype": "float32", "shape": [1, 4, 6], "data": { "kind": "fillFloat32", "scale": 2.5, "sinStep": 0.25, "cosStep": 0.38 } }, "qT": { "dtype": "float16", "shape": [1, 2, 4, 6], "data": { "kind": "fillFloat32", "scale": 0.8, "sinStep": 0.19, "cosStep": 0.3 } }, "qRotT": { "dtype": "float16", "shape": [1, 2, 4, 6], "data": { "kind": "fillFloat32", "scale": 0.8, "sinStep": 0.45, "cosStep": 0.14 } }, "kT": { "dtype": "float16", "shape": [1, 2, 4, 6], "data": { "kind": "fillFloat32", "scale": 0.8, "sinStep": 0.37, "cosStep": 0.2 } }, "kRotT": { "dtype": "float16", "shape": [1, 2, 4, 6], "data": { "kind": "fillFloat32", "scale": 0.8, "sinStep": 0.49, "cosStep": 0.26 } } }, "outputs": { "output1T": { "dtype": "float16", "shape": [1, 2, 4, 6], "tolerance": 0.005 }, "output2T": { "dtype": "float16", "shape": [1, 2, 4, 6], "tolerance": 0.005 } } }, { "name": "f16_vec4_dim5_seq4_odd_headdim", "provenance": { "notes": "An odd headDim (5) with seqLen 4: seqLen * headDim = 20 keeps every vec4 of emb angles and of q/k inside one (batch, head) block." }, "inputs": { "embT": { "dtype": "float32", "shape": [2, 4, 5], "data": { "kind": "fillFloat32", "scale": 2.5, "sinStep": 0.26, "cosStep": 0.38 } }, "qT": { "dtype": "float16", "shape": [2, 2, 4, 5], "data": { "kind": "fillFloat32", "scale": 0.8, "sinStep": 0.19999999999999998, "cosStep": 0.3 } }, "qRotT": { "dtype": "float16", "shape": [2, 2, 4, 5], "data": { "kind": "fillFloat32", "scale": 0.8, "sinStep": 0.46, "cosStep": 0.14 } }, "kT": { "dtype": "float16", "shape": [2, 2, 4, 5], "data": { "kind": "fillFloat32", "scale": 0.8, "sinStep": 0.38, "cosStep": 0.2 } }, "kRotT": { "dtype": "float16", "shape": [2, 2, 4, 5], "data": { "kind": "fillFloat32", "scale": 0.8, "sinStep": 0.5, "cosStep": 0.26 } } }, "outputs": { "output1T": { "dtype": "float16", "shape": [2, 2, 4, 5], "tolerance": 0.005 }, "output2T": { "dtype": "float16", "shape": [2, 2, 4, 5], "tolerance": 0.005 } } } ]}README.md+16-12
build/webgpu/bench.json-1
build/webgpu/gemma-rotary-embedding.wgsl.jinja+3-4
build/webgpu/manifest.json+61-159
build/webgpu/metadata.json+10-7
build/webgpu/test.json+76-3