sync 6fdf6301e2bb
Browse filesFiles changed (5)
39 unmodified lines4041424344453 unmodified lines4950515253545556575859606139 unmodified lines| `T` | `float16` || `U` | `float32` |## Device requirementsEvery 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- [`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.2```Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.39 unmodified lines4041424344454647484950513 unmodified lines5556575859606162636465666739 unmodified lines| `T` | `float16` || `U` | `float32` |## Implementation variantsOne 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 requirementsEvery 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 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.3```Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.
12348 unmodified lines131415161718192021222324enable f16;{{ 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;}// (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;12345678910118 unmodified lines20212223242526{% 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>) {{{ 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 lines2829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980812 unmodified lines8485868788899091929394959697989910010110210310410510610710827 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"],"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,"scalar": "dtypes.T","vector": "\"vec4<f16>\"","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" },{"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}}27 unmodified lines2829303132333435363738394041422 unmodified lines4546474849505152535455565758596061626364656667686927 unmodified lines"when": ["contract", "tunables.workgroupSize >= 1", "floor(tunables.workgroupSize) == tunables.workgroupSize", "tunables.workgroupSize <= device.limits.maxComputeInvocationsPerWorkgroup", "tunables.workgroupSize <= device.limits.maxComputeWorkgroupSizeX"],"variants": [{"id": "elementwise","requires": { "features": ["shader-f16"] },"derive": {"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", "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) / lanes" },{ "name": "seqDim", "type": "u32", "value": "(seqLen * headDim) / lanes" },{ "name": "headSeqDim", "type": "u32", "value": "(numHeads * seqLen * headDim) / lanes" }]}],"dispatch": {"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}}
1234561 unmodified line89101112131415161718192021{"name": "com.microsoft.GemmaRotaryEmbedding","id": "_com_microsoft_gemmarotaryembedding_webgpu_e5a6aca","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="}},"provenance": { "kernel": { "sha": "91d990483a174128daf7673f3f37a7c890493ae1", "dirty": false } },"webgpu": {"manifestSpec": "2.0","variants": { "vec4": ["gemma-rotary-embedding.wgsl.jinja"], "scalar": ["gemma-rotary-embedding.wgsl.jinja"] }}}1234561 unmodified line89101112131415161718{"name": "com.microsoft.GemmaRotaryEmbedding","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": "J7OaLWdNRVSQJN7334F9gTgV5OrdWUywfjXx5rPnHr4=","manifest.json": "QJJdYbfJe3E3SRzXSXLz3IuLFfsthXglsTWLtCQWh4U=","test.json": "0qR3IsgdZC/lwA1WbH1QDFYTijEjsyo3t++UOJnt6VY="}},"provenance": { "kernel": { "sha": "6fdf6301e2bbcc2f03bf1eaf493b7ad55ef33afc", "dirty": false } },"webgpu": { "manifestSpec": "2.1", "variants": { "elementwise": ["gemma-rotary-embedding.wgsl.jinja"] } }}
200 unmodified lines201202203204205206207200 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."},"inputs": {"embT": {200 unmodified lines201202203204205206207200 unmodified lines{"name": "f16_vec4_dim6_seq4_straddles_heads","provenance": {"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