sync 4385228f788d
Browse filesFiles changed (5)
47 unmodified lines4849505151525354555657585955565758596061626364646566676869707172737472757677787980818283848586878889909192931 unmodified line959697989947 unmodified lines
## Device requirements
Every implementation variant requires `shader-f16`; the package has no variant-level fallback without that capability.Every implementation variant requires `shader-f16`, so the package does not run without it.
## 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 cases- [`gemma-rotary-embedding.wgsl.jinja`](build/webgpu/gemma-rotary-embedding.wgsl.jinja)- [`metadata.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.GemmaRotaryEmbedding/blob/v1/build/webgpu/metadata.json) — kernel metadata (id, file digests, manifest spec, provenance)- [`manifest.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.GemmaRotaryEmbedding/blob/v1/build/webgpu/manifest.json) — the op contract (source of truth)- [`test.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.GemmaRotaryEmbedding/blob/v1/build/webgpu/test.json) — correctness cases- [`bench.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.GemmaRotaryEmbedding/blob/v1/build/webgpu/bench.json) — benchmark cases- [`gemma-rotary-embedding.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.GemmaRotaryEmbedding/blob/v1/build/webgpu/gemma-rotary-embedding.wgsl.jinja)
## Use with `@huggingface/kernels`
```shnpm install --save-exact @huggingface/kernels@0.0.1-preview.3npm install --save-exact @huggingface/kernels@0.0.1-preview.4```
Paste the example into a browser app that resolves npm imports. Serve the app over HTTPS or localhost with WebGPU available.This example also needs a device that supports the kernel's required WebGPU features.
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.The `Uint16Array` inputs hold raw float16 bits and work on browsers without a `Float16Array` constructor.
```jsimport { getKernel } from "@huggingface/kernels";
const kernel = await getKernel("webgpu-kernels/com.microsoft.GemmaRotaryEmbedding", { version: 1 });
const embTData = Float32Array.of( 0.896741, -0.7510284, 2.9392502, 2.4113605, 2.7308347, -2.6883833, -0.7908616, -0.4171633,);const qTData = Uint16Array.of(22209, 55367, 54354, 22428, 55467, 23078, 21818, 54665);const qRotTData = Uint16Array.of(21832, 22300, 22448, 55684, 23047, 21605, 54528, 22649);const kTData = Uint16Array.of(55492, 21630, 54424, 21773, 55412, 55718, 54943, 22711);const kRotTData = Uint16Array.of(54430, 55336, 21701, 54758, 22367, 54902, 22904, 55082);
const { output1T, output2T } = await kernel({ embT: { data: embTData, shape: [1, 2, 4] }, qT: { data: qTData, shape: [1, 1, 2, 4] },1 unmodified line kT: { data: kTData, shape: [1, 1, 2, 4] }, kRotT: { data: kRotTData, shape: [1, 1, 2, 4] },});console.log({ output1T, output2T });```11234567889101112131415111213141516161718192021222324252618272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621632425262728293031323316416516616716816935361701711721731741751761771781791801811821831841851861871881891901911921931941953839404142196{% macro flat_index_2d(workgroupSize, name="i", bound="params.count", guardInline=false) %}{% macro flat_index_2d(workgroupSize, name="i", bound="params.count") %}{% 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 %}{% endmacro %}{{ env.wgsl.resourceDeclarations }}{% macro emit_precise_sincos_half_pi(width=1) %}{% set t = "vec4<f32>" if width == 4 else "f32" %}fn precise_sincos_half_pi{{ "4" if width == 4 else "" }}(x: {{ t }}) -> {{ "mat2x4<f32>" if width == 4 else "vec2<f32>" }} { let x2 = x * x;
// output1 = q * cos(emb) + q_rot * sin(emb); output2 applies the same// rotation to K. The operator is elementwise over (batch, heads, seq, dim),// while emb omits the head axis and is shared by every head.const WG: u32 = {{ workgroupSize }}u;{% if vec4 %}const ZERO: {{ vector }} = {{ vector }}(0.0); var sinPolynomial = {{ "vec4<f32>(" if width == 4 else "" }}1.6059043836821613e-10{{ ")" if width == 4 else "" }}; sinPolynomial = fma(sinPolynomial, x2, {{ "vec4<f32>(" if width == 4 else "" }}-2.505210838544172e-8{{ ")" if width == 4 else "" }}); sinPolynomial = fma(sinPolynomial, x2, {{ "vec4<f32>(" if width == 4 else "" }}2.7557319223985893e-6{{ ")" if width == 4 else "" }}); sinPolynomial = fma(sinPolynomial, x2, {{ "vec4<f32>(" if width == 4 else "" }}-1.984126984126984e-4{{ ")" if width == 4 else "" }}); sinPolynomial = fma(sinPolynomial, x2, {{ "vec4<f32>(" if width == 4 else "" }}8.333333333333333e-3{{ ")" if width == 4 else "" }}); sinPolynomial = fma(sinPolynomial, x2, {{ "vec4<f32>(" if width == 4 else "" }}-1.6666666666666666e-1{{ ")" if width == 4 else "" }});{% if preciseTrigResidualSine | default(false) %} // Add the small correction directly to x, avoiding rounding 1 + correction // before the final multiplication in cancellation-sensitive FFT roots. let sine = fma(x * x2, sinPolynomial, x);{% else %}const ZERO: {{ scalar }} = {{ scalar }}(0.0); let sine = x * fma(sinPolynomial, x2, {{ "vec4<f32>(" if width == 4 else "" }}1.0{{ ")" if width == 4 else "" }});{% endif %}
var cosPolynomial = {{ "vec4<f32>(" if width == 4 else "" }}2.08767569878681e-9{{ ")" if width == 4 else "" }}; cosPolynomial = fma(cosPolynomial, x2, {{ "vec4<f32>(" if width == 4 else "" }}-2.755731922398589e-7{{ ")" if width == 4 else "" }}); cosPolynomial = fma(cosPolynomial, x2, {{ "vec4<f32>(" if width == 4 else "" }}2.48015873015873e-5{{ ")" if width == 4 else "" }}); cosPolynomial = fma(cosPolynomial, x2, {{ "vec4<f32>(" if width == 4 else "" }}-1.388888888888889e-3{{ ")" if width == 4 else "" }}); cosPolynomial = fma(cosPolynomial, x2, {{ "vec4<f32>(" if width == 4 else "" }}4.1666666666666664e-2{{ ")" if width == 4 else "" }}); cosPolynomial = fma(cosPolynomial, x2, {{ "vec4<f32>(" if width == 4 else "" }}-5.0e-1{{ ")" if width == 4 else "" }}); let cosine = fma(cosPolynomial, x2, {{ "vec4<f32>(" if width == 4 else "" }}1.0{{ ")" if width == 4 else "" }});
return {{ "mat2x4<f32>" if width == 4 else "vec2<f32>" }}(cosine, sine);}{% endmacro %}{% set preciseTrigResidualSine = true %}{{ emit_precise_sincos_half_pi() }}
// Little-endian unsigned fixed point with 45 fractional bits. A product of// two 15-bit limbs and the carries on each diagonal fit in u32.alias Fraction = vec3<u32>;const LIMB_MASK = 32767u;const ONE = Fraction(0u, 0u, 32768u);
fn multiply_fraction(a: Fraction, b: Fraction) -> Fraction { let d0 = a[0] * b[0]; let d1 = a[0] * b[1] + a[1] * b[0] + (d0 >> 15u); let d2 = a[0] * b[2] + a[1] * b[1] + a[2] * b[0] + (d1 >> 15u); let d3 = a[1] * b[2] + a[2] * b[1] + (d2 >> 15u); let d4 = a[2] * b[2] + (d3 >> 15u); return Fraction(d3 & LIMB_MASK, d4 & LIMB_MASK, d4 >> 15u);}
fn subtract_fraction(a: Fraction, b: Fraction) -> Fraction { let low = a.x - b.x; let mid = a.y - b.y - (low >> 31u); return Fraction(low & LIMB_MASK, mid & LIMB_MASK, a.z - b.z - (mid >> 31u));}
fn fraction_to_f32(a: Fraction) -> f32 { // Fit the leading bits into one word and jam discarded bits into its low // bit so the u32-to-f32 conversion rounds only once. let high = (a.z << 15u) | a.y; let shift = min(15u, countLeadingZeros(high)); let drop = 15u - shift; let word = (high << shift) | (a.x >> drop); let sticky = (a.x & ((1u << drop) - 1u)) != 0u; return f32(word | u32(sticky)) * bitcast<f32>((97u - shift) << 23u);}
fn orient(c: f32, s: f32, quadrant: u32) -> vec2<f32> { switch (quadrant & 3u) { case 1u: { return vec2<f32>(-s, c); } case 2u: { return vec2<f32>(-c, -s); } case 3u: { return vec2<f32>(s, -c); } default: { return vec2<f32>(c, s); } }}
// floor((2/pi) * 2^225) in radix 2^15. Leading zero limbs extend the// multiplication window for small exponents.const TWO_OVER_PI = array<u32, 24>( 18263u, 31045u, 527u, 17465u, 13095u, 22693u, 24685u, 13533u, 9194u, 7519u, 20449u, 16722u, 18888u, 24795u, 20860u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u);
{% macro reciprocal_factorial(n) -%}{% set v = (35184372088832 / n + 0.5) | int -%}Fraction({{ v % 32768 }}u, {{ ((v / 32768) | int) % 32768 }}u, {{ (v / 1073741824) | int }}u){%- endmacro %}fn wide_coefficients(angle: f32) -> vec2<f32> { let bits = bitcast<u32>(abs(angle)); if (bits < 0x39000000u) { return vec2<f32>(1.0, angle); } if (bits >= 0x7f800000u) { return vec2<f32>(bitcast<f32>(bits | 0x00400000u)); }
// Payne-Hanek reduction. Slide the window by the input exponent so the // quadrant and fractional phase survive even at the largest f32 exponent. let mantissa = (bits & 0x7fffffu) | 0x800000u; let shift = 330u - (bits >> 23u); // 225 + 23 + 127 - 45 let first = shift / 15u - 2u; let offset = shift % 15u; var words: array<u32, 7>; var carry = 0u; for (var j = 0u; j < 7u; j++) { let i = first + j; let limb = (mantissa & LIMB_MASK) * TWO_OVER_PI[i] + (mantissa >> 15u) * TWO_OVER_PI[i - 1u] + carry; words[j] = limb & LIMB_MASK; carry = limb >> 15u; } var phase: vec4<u32>; for (var j = 0u; j < 4u; j++) { phase[j] = ((words[j + 2u] >> offset) | (words[j + 3u] << (15u - offset))) & LIMB_MASK; } let reflect = phase.z >= 16384u; let distance = select(phase.xyz, subtract_fraction(ONE, phase.xyz), reflect); let r = multiply_fraction(distance, Fraction(2138u, 27985u, 51471u)); // pi/2 let z = multiply_fraction(r, r);
// Evaluate sin(r)/r and cos(r) on [0, pi/4] before rounding to f32. var sine = {{ reciprocal_factorial(1307674368000) }}; var cosine = {{ reciprocal_factorial(87178291200) }};{% for pair in [[6227020800, 479001600], [39916800, 3628800], [362880, 40320], [5040, 720], [120, 24], [6, 2], [1, 1]] %} sine = subtract_fraction({{ reciprocal_factorial(pair[0]) }}, multiply_fraction(z, sine)); cosine = subtract_fraction({{ reciprocal_factorial(pair[1]) }}, multiply_fraction(z, cosine));{% endfor %} let sinAbs = fraction_to_f32(multiply_fraction(r, sine)); let pair = orient(fraction_to_f32(cosine), select(sinAbs, -sinAbs, reflect), phase.w + u32(reflect)); return vec2<f32>(pair.x, select(pair.y, -pair.y, angle < 0.0));}
fn near_half_midpoint(v: f32) -> bool { let bits = bitcast<u32>(abs(v)); // The low 13 f32 significand bits locate the midpoint of two normal // halves. Near zero, use the wide fold for the nonuniform f32-ulp guard. return bits < 0x38800000u || abs(i32(bits & 8191u) - 4096) <= 8;}
fn rotary_coefficients(angle: f32) -> vec2<f32> { if (!(abs(angle) <= 16384.0)) { return wide_coefficients(angle); } let quadrant = round(angle * 0.6366197723675814); // The first three pi/2 limbs times any admitted quadrant are exact f32 // products. The final limb preserves relative accuracy near a multiple. let leading = fma(-quadrant, 1.5703125, angle); let middle = fma(-quadrant, 0.000483989715576171875, leading); let trailing = fma(-quadrant, -1.62981450557708740234375E-7, middle); let r = fma(-quadrant, 6.077100506506192e-11, trailing); let pair = precise_sincos_half_pi(r); if (near_half_midpoint(pair.x) || near_half_midpoint(pair.y)) { return wide_coefficients(angle); } return orient(pair.x, pair.y, bitcast<u32>(i32(quadrant)));}
const WG: u32 = {{ workgroupSize }}u;@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;
// emb is float32 for every T. Compute sine and cosine at full width, then // round them to T before multiplication as required by the operator graph. let angle = emb[emb_index];{% if vec4 %} let sin_val = {{ vector }}(sin(angle)); let cos_val = {{ vector }}(cos(angle)); let plane = index % params.seqDim; let group = index / params.seqDim;{% if headTile > 1 %} let groups = (params.heads + {{ headTile - 1 }}u) / {{ headTile }}u; let batch = group / groups;{% else %} let sin_val = {{ scalar }}(sin(angle)); let cos_val = {{ scalar }}(cos(angle)); let batch = group / params.heads;{% endif %} let angle = emb[batch * params.seqDim + plane];{% for lane in range(lanes) %} let coeff{{ lane }} = rotary_coefficients(angle{% if vec4 %}[{{ lane }}]{% endif %});{% endfor %}{% for name, component in [["cosine", "x"], ["sine", "y"]] %} let {{ name }} = {{ element }}({% for lane in range(lanes) %}f16(coeff{{ lane }}.{{ component }}){{ ", " if not loop.last else "" }}{% endfor %});{% endfor %}{% if headTile > 1 %} let firstHead = (group % groups) * {{ headTile }}u; for (var h = firstHead; h < min(firstHead + {{ headTile }}u, params.heads); h++) { let dst = (batch * params.heads + h) * params.seqDim + plane;{% else %} let dst = index;{% endif %}{% macro apply_rotation() -%}// Each product is an f16 graph edge and rounds before the final add.{% for state in ["q", "k"] %}output{{ loop.index }}[dst] = fma({{ state }}[dst], cosine, {{ element }}(0.0)) + fma({{ state }}_rot[dst], sine, {{ element }}(0.0));{% endfor %}{%- endmacro %}{{ apply_rotation() | indent(4 if headTile > 1 else 2, true) }}{% if headTile > 1 %} }{% endif %}
// Round both products to T before addition. Explicit fma(..., 0) prevents a // backend from contracting the final add and retaining wider intermediates. output1[index] = fma(q[index], cos_val, ZERO) + fma(q_rot[index], sin_val, ZERO); output2[index] = fma(k[index], cos_val, ZERO) + fma(k_rot[index], sin_val, ZERO);}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172731{ "domain": "com.microsoft", "name": "GemmaRotaryEmbedding", "sinceVersion": 1, "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": { "default": 256 } }, "derive": { "batchSize": "dim(shapes.qT, 0)", "numHeads": "dim(shapes.qT, 1)", "seqLen": "dim(shapes.qT, 2)", "headDim": "dim(shapes.qT, 3)", "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": "(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": "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": [ { "id": "main", "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 } } ] } ]}{"domain":"com.microsoft","name":"GemmaRotaryEmbedding","sinceVersion":1,"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":{"default":256}},"derive":{"batchSize":"dim(shapes.qT, 0)","numHeads":"dim(shapes.qT, 1)","seqLen":"dim(shapes.qT, 2)","headDim":"dim(shapes.qT, 3)","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":"(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":"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","headTile":"max(1, min(numHeads, 16)) if seqLen * headDim / lanes >= workgroupSize else 1","headGroups":"ceilDiv(numHeads, headTile)"},"passes":[{"id":"main","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":"batchSize * headGroups * seqLen * headDim / lanes"},{"name":"seqDim","type":"u32","value":"(seqLen * headDim) / lanes"},{"name":"heads","type":"u32","value":"numHeads"}]}],"dispatch":{"x":"min(ceilDiv((max(1, batchSize * headGroups * seqLen * headDim / lanes)), (workgroupSize)), 65535)","y":"ceilDiv(ceilDiv((max(1, batchSize * headGroups * seqLen * headDim / lanes)), (workgroupSize)), 65535)","z":1}}]}]}1234567891011121314151617181{ "name": "com.microsoft.GemmaRotaryEmbedding", "id": "_com_microsoft_gemmarotaryembedding_webgpu_bb6bdf4", "version": 1, "license": "Apache-2.0", "backend": { "type": "webgpu" }, "digest": { "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"] } }}{"name":"com.microsoft.GemmaRotaryEmbedding","id":"_com_microsoft_gemmarotaryembedding_webgpu_3e1d30c","version":1,"license":"Apache-2.0","backend":{"type":"webgpu"},"digest":{"algorithm":"sha256","files":{"bench.json":"E1026CMMv9Pppl2+JpGoj3dWCSmk8tZUEwBYo3UCiD4=","gemma-rotary-embedding.wgsl.jinja":"Hs93RzZrphnZbjADNJYvHYBZiTjIgluQESgCFYFOVPc=","manifest.json":"vkkCHF52yrrlG/D3RcxenXoeRqJSsn4zh7l1k0K9bqI=","test.json":"IJc4AxdqBuIDelLLizkkK/ZEj8AG2DW7Uf6MaFYd5dg="}},"provenance":{"kernel":{"sha":"4385228f788ddaa1bd7aec6a80a20f3ddffd8011","dirty":false}},"webgpu":{"manifestSpec":"2.2"}}79 unmodified lines808182838384858641 unmodified lines128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198137 unmodified lines33633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345479 unmodified lines { "name": "pinned_f16_round_before_add", "provenance": { "notes": "Pins the round-to-T-before-add rule at float16. Every element is a near-cancellation: the two products are around 100 in magnitude and their sum is under 1, so rounding each product to float16 before the add moves the result by 150 to 900 ulps, while a backend that contracted the multiply into an fma and kept a float32 product would land somewhere else entirely. The angles were chosen so their float16 sin and cos survive several ulps of perturbation, so this does not test the GPU's transcendental." "notes": "Pins the round-to-T-before-add rule at float16. Every output1 element is a near-cancellation: the two products are around 100 in magnitude and their sum is under 1, so rounding each product to float16 before the add moves the result by 150 to 900 ulps, while a backend that contracted the multiply into an fma and kept a float32 product would land somewhere else entirely. The angles were chosen so their float16 sin and cos survive several ulps of perturbation, so this does not test the GPU's transcendental." }, "inputs": { "embT": {41 unmodified lines "data": { "kind": "values", "values": [-278.5, 26.625, -326.75, 338.25, 218.0, 55.8125, 221.125, 375.5] }, "tolerance": 0.005 } }, "skipGpu": { "category": "permanent", "reason": "WGSL lets each float16 conversion and product round to either neighbour and lets a product fuse into the add. Every output1 element here cancels two products near 100 to under 1, so those legal choices move a result by up to 0.5 while the rule under test moves it by at most 0.1." } }, { "name": "pinned_f16_typed_stages_same_sign", "provenance": { "notes": "The q, k and rotated inputs have magnitudes between 64 and 200, so each element's two products share a sign and do not cancel. Every result WGSL allows for these typed stages (a float16 neighbor at each conversion, product and store; a product fused into the add; sin and cos within WGSL's 2^-11 bound) lies within 2.93 * 2^-10 of the float16 typed-stage value. Relative tolerance 2^-8 accepts these results while swapped sin and cos or a wrong embedding index still fails." }, "inputs": { "embT": { "dtype": "float32", "shape": [1, 2, 4], "data": { "kind": "values", "values": [0.896741, -0.7510284, 2.9392502, 2.4113605, 2.7308347, -2.6883833, -0.7908616, -0.4171633] } }, "qT": { "dtype": "float16", "shape": [1, 1, 2, 4], "data": { "kind": "values", "values": [108.0625, -136.875, -69.125, 121.75, -149.375, 196.75, 83.625, -88.5625] } }, "qRotT": { "dtype": "float16", "shape": [1, 1, 2, 4], "data": { "kind": "values", "values": [84.5, 113.75, 123.0, -176.5, 192.875, 70.3125, -80.0, 143.125] } }, "kT": { "dtype": "float16", "shape": [1, 1, 2, 4], "data": { "kind": "values", "values": [-152.5, 71.875, -73.5, 80.8125, -142.5, -180.75, -105.9375, 150.875] } }, "kRotT": { "dtype": "float16", "shape": [1, 1, 2, 4], "data": { "kind": "values", "values": [-73.875, -133.0, 76.3125, -94.375, 117.9375, -103.375, 175.0, -114.625] } } }, "outputs": { "output1T": { "dtype": "float16", "shape": [1, 1, 2, 4], "data": { "kind": "values", "values": [133.5, -177.75, 92.375, -208.5, 214.0, -207.625, 115.6875, -139.0] }, "tolerance": 0, "relTolerance": 0.00390625 }, "output2T": { "dtype": "float16", "shape": [1, 1, 2, 4], "data": { "kind": "values", "values": [-152.875, 143.375, 87.3125, -123.125, 177.75, 207.75, -199.0, 184.375] }, "tolerance": 0, "relTolerance": 0.00390625 } } }, {137 unmodified lines "output1T": { "dtype": "float16", "shape": [2, 2, 4, 5], "tolerance": 0.005 }, "output2T": { "dtype": "float16", "shape": [2, 2, 4, 5], "tolerance": 0.005 } } }, { "name": "f16_midpoints_and_long_positions", "inputs": { "embT": { "dtype": "float32", "shape": [1, 1, 8], "data": { "kind": "values", "values": [0.24163803458213806, -2.613194704055786, 1.6262991428375244, 8193.0, -258176.0, 65536.0, 100000002004087730000.0, 3.3999999521443642e+38] } }, "qT": { "dtype": "float16", "shape": [1, 1, 1, 8], "data": { "kind": "values", "values": [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] } }, "qRotT": { "dtype": "float16", "shape": [1, 1, 1, 8], "data": { "kind": "values", "values": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] } }, "kT": { "dtype": "float16", "shape": [1, 1, 1, 8], "data": { "kind": "values", "values": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] } }, "kRotT": { "dtype": "float16", "shape": [1, 1, 1, 8], "data": { "kind": "values", "values": [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] } } }, "outputs": { "output1T": { "dtype": "float16", "shape": [1, 1, 1, 8], "data": { "kind": "values", "values": [0.97119140625, -0.86376953125, -0.05548095703125, 0.962890625, 0.99658203125, -0.7216796875, 0.75439453125, 0.9697265625] }, "exactRounding": true, "tolerance": 0, "relTolerance": 0 }, "output2T": { "dtype": "float16", "shape": [1, 1, 1, 8], "data": { "kind": "values", "values": [0.2392578125, -0.50439453125, 0.99853515625, -0.270263671875, 0.08416748046875, 0.69189453125, 0.65673828125, -0.244140625] }, "exactRounding": true, "tolerance": 0, "relTolerance": 0 } }, "provenance": { "notes": "Unit query and rotated key isolate cosine and sine at half midpoints and at widely separated finite positions." } }, { "name": "odd_head_plane_and_partial_head_tile", "inputs": { "embT": { "dtype": "float32", "shape": [2, 1, 3], "data": { "kind": "values", "values": [1.5707963705062866, -16384.25, 5.960464477539063e-8, 0.24163803458213806, 0.7, -5.6] } }, "qT": { "dtype": "float16", "shape": [2, 17, 1, 3], "data": { "kind": "fillFloat32", "scale": 1.0, "sinStep": 0.13, "cosStep": 0.46 } }, "qRotT": { "dtype": "float16", "shape": [2, 17, 1, 3], "data": { "kind": "fillFloat32", "scale": 1.0, "sinStep": 0.26, "cosStep": 0.6900000000000001 } }, "kT": { "dtype": "float16", "shape": [2, 17, 1, 3], "data": { "kind": "fillFloat32", "scale": 1.0, "sinStep": 0.39, "cosStep": 0.92 } }, "kRotT": { "dtype": "float16", "shape": [2, 17, 1, 3], "data": { "kind": "fillFloat32", "scale": 1.0, "sinStep": 0.52, "cosStep": 1.1500000000000001 } } }, "outputs": { "output1T": { "dtype": "float16", "shape": [2, 17, 1, 3], "exactRounding": true, "tolerance": 0, "relTolerance": 0 }, "output2T": { "dtype": "float16", "shape": [2, 17, 1, 3], "exactRounding": true, "tolerance": 0, "relTolerance": 0 } }, "tunables": { "workgroupSize": 1 }, "provenance": { "notes": "Two batches with distinct angles and seventeen heads exercise the scalar rotation and the final incomplete head tile." } } ]}README.md+22-8
build/webgpu/gemma-rotary-embedding.wgsl.jinja+179-26
build/webgpu/manifest.json+1-73
build/webgpu/metadata.json+1-18
build/webgpu/test.json+179-1