sync 6fdf6301e2bb
Browse filesFiles changed (5)
39 unmodified lines4041424344454647484950513 unmodified lines55565752585960616263586465666739 unmodified lines| `T` | `float16` || `U` | `float32` |
## Implementation variants
One implementation is selected per call from the device capabilities, the request shapes and the dtypes; these notes say what each one covers.
- `elementwise` — Elementwise rotation. Streams `vec4` words when each head's `(seq, dim)` plane holds a multiple of four elements, so no word straddles two heads; scalar elements otherwise.
## Device requirements
Every implementation variant requires `shader-f16`; the package has no variant-level fallback without that capability.3 unmodified lines- [`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 cases- [`bench.json`](build/webgpu/bench.json) — benchmark cases- [`gemma-rotary-embedding.wgsl.jinja`](build/webgpu/gemma-rotary-embedding.wgsl.jinja)
## Use with `@huggingface/kernels`
```shnpm install --save-exact @huggingface/kernels@0.0.1-preview.2npm install --save-exact @huggingface/kernels@0.0.1-preview.3```
Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.112345678910118 unmodified lines20212216171819202123242526enable f16;{% macro flat_index_2d(workgroupSize, name="i", bound="params.count", guardInline=false) %}{% set wgTerm = workgroupSize ~ "u" if workgroupSize is number else workgroupSize %} // 2D-folded flat index: gid.y carries the high bits past the dispatch's // per-axis workgroup fold width. let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }}; if ({{ name }} >= {{ bound }}) { return; }{% endmacro %}{{ env.wgsl.resourceDeclarations }}
// output1 = q * cos(emb) + q_rot * sin(emb); output2 applies the same8 unmodified lines
@compute @workgroup_size(WG, 1, 1)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. 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; }{{ flat_index_2d("WG", "index") }} // (batch, head, seq, dim) -> (batch, seq, dim): divide out the head-major stride and // keep the remainder, which is exactly the (seq, dim) offset shared by every head. let emb_index = (index / params.headSeqDim) * params.seqDim + index % params.seqDim;27 unmodified lines28293031323331323336373839404142434445464748495051525354555657585960616263646566676869707172737475763435363738394041422 unmodified lines454647878889909192934849505152535455565797989958596061626364104105656667686927 unmodified lines "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"], "id": "elementwise", "requires": { "features": ["shader-f16"] }, "derive": { "vec4": true, "scalar": "dtypes.T", "vector": "\"vec4<f16>\"", "workgroupSize": "tunables.workgroupSize" }, "passes": [ { "id": "main", "name": "GemmaRotaryEmbedding.Vec4", "shader": "gemma-rotary-embedding.wgsl.jinja", "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 } } ] }, { "id": "scalar", "priority": 0, "requires": { "features": ["shader-f16"] }, "derive": { "vec4": false, "vec4": "vec4Ok", "lanes": "4 if vec4Ok else 1", "scalar": "dtypes.T", "vector": "\"vec4<f16>\"", "element": "vector if vec4Ok else scalar", "embElement": "\"vec4<f32>\" if vec4Ok else \"f32\"", "workgroupSize": "tunables.workgroupSize" }, "passes": [2 unmodified lines "name": "GemmaRotaryEmbedding", "shader": "gemma-rotary-embedding.wgsl.jinja", "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" }, { "arg": "embT", "name": "emb", "elementType": "$embElement" }, { "arg": "qT", "name": "q", "elementType": "$element" }, { "arg": "qRotT", "name": "q_rot", "elementType": "$element" }, { "arg": "kT", "name": "k", "elementType": "$element" }, { "arg": "kRotT", "name": "k_rot", "elementType": "$element" }, { "arg": "output1T", "name": "output1", "elementType": "$element" }, { "arg": "output2T", "name": "output2", "elementType": "$element" }, { "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)" } { "name": "count", "type": "u32", "value": "numel(shapes.qT) / lanes" }, { "name": "seqDim", "type": "u32", "value": "(seqLen * headDim) / lanes" }, { "name": "headSeqDim", "type": "u32", "value": "(numHeads * seqLen * headDim) / lanes" } ] } ], "dispatch": { "x": "min(ceilDiv((max(1, numel(shapes.qT))), (workgroupSize)), 65535)", "y": "ceilDiv(ceilDiv((max(1, numel(shapes.qT))), (workgroupSize)), 65535)", "x": "min(ceilDiv((numel(shapes.qT) / 4 if vec4Ok else max(1, numel(shapes.qT))), (workgroupSize)), 65535)", "y": "ceilDiv(ceilDiv((numel(shapes.qT) / 4 if vec4Ok else max(1, numel(shapes.qT))), (workgroupSize)), 65535)", "z": 1 } }12334561 unmodified line891011121311121314151617181920161718{ "name": "com.microsoft.GemmaRotaryEmbedding", "id": "_com_microsoft_gemmarotaryembedding_webgpu_e5a6aca", "id": "_com_microsoft_gemmarotaryembedding_webgpu_bb6bdf4", "version": 1, "license": "Apache-2.0", "backend": { "type": "webgpu" },1 unmodified line "algorithm": "sha256", "files": { "bench.json": "E1026CMMv9Pppl2+JpGoj3dWCSmk8tZUEwBYo3UCiD4=", "gemma-rotary-embedding.wgsl.jinja": "t4wKCJ/E3156iIqQTq1d2XGqMBA83kI0EAADYck7NI0=", "manifest.json": "41x/WJ1/AyRVHsYgM3nIqECf6Z8zix9MBzVgLIaLILE=", "test.json": "WwfG+ffVK8tAHKvZCOkixu97BOEIQjEdR4NSOSLA9dc=" "gemma-rotary-embedding.wgsl.jinja": "J7OaLWdNRVSQJN7334F9gTgV5OrdWUywfjXx5rPnHr4=", "manifest.json": "QJJdYbfJe3E3SRzXSXLz3IuLFfsthXglsTWLtCQWh4U=", "test.json": "0qR3IsgdZC/lwA1WbH1QDFYTijEjsyo3t++UOJnt6VY=" } }, "provenance": { "kernel": { "sha": "91d990483a174128daf7673f3f37a7c890493ae1", "dirty": false } }, "webgpu": { "manifestSpec": "2.0", "variants": { "vec4": ["gemma-rotary-embedding.wgsl.jinja"], "scalar": ["gemma-rotary-embedding.wgsl.jinja"] } } "provenance": { "kernel": { "sha": "6fdf6301e2bbcc2f03bf1eaf493b7ad55ef33afc", "dirty": false } }, "webgpu": { "manifestSpec": "2.1", "variants": { "elementwise": ["gemma-rotary-embedding.wgsl.jinja"] } }}200 unmodified lines201202203204204205206207200 unmodified lines { "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." "notes": "Head dim 6 is not a multiple of four, but seq_len x head_dim = 4x6 = 24 is; checks four-element grouping that straddles head-dim boundaries within one (batch, seq) block rather than aligning to them." }, "inputs": { "embT": {README.md+8-2
build/webgpu/gemma-rotary-embedding.wgsl.jinja+9-7
build/webgpu/manifest.json+17-56
build/webgpu/metadata.json+6-9
build/webgpu/test.json+1-1