sync 4385228f788d
Browse filesFiles changed (7)
59 unmodified lines606162636465666768636465666768697071727373747576777879808182818283848586878889909192939495969798991001011021031041052 unmodified lines10810911011111259 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 cases- [`mrotary-embedding-vector.wgsl.jinja`](build/webgpu/mrotary-embedding-vector.wgsl.jinja)- [`mrotary-embedding.wgsl.jinja`](build/webgpu/mrotary-embedding.wgsl.jinja)- [`metadata.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MRotaryEmbedding/blob/v1/build/webgpu/metadata.json) — kernel metadata (id, file digests, manifest spec, provenance)- [`manifest.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MRotaryEmbedding/blob/v1/build/webgpu/manifest.json) — the op contract (source of truth)- [`test.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MRotaryEmbedding/blob/v1/build/webgpu/test.json) — correctness cases- [`bench.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MRotaryEmbedding/blob/v1/build/webgpu/bench.json) — benchmark cases- [`mrotary-embedding-vector.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MRotaryEmbedding/blob/v1/build/webgpu/mrotary-embedding-vector.wgsl.jinja)- [`mrotary-embedding.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MRotaryEmbedding/blob/v1/build/webgpu/mrotary-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.
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.
```jsimport { getKernel } from "@huggingface/kernels";
const kernel = await getKernel("webgpu-kernels/com.microsoft.MRotaryEmbedding", { version: 1 });
const xData = Float32Array.of( -2, -1.9365079, -1.8730159, -1.8095238, -1.7460318, -1.6825397, -1.6190476, -1.5555556, -1.4920635, -1.4285715, -1.3650794, -1.3015873, -1.2380953, -1.1746032, -1.1111112, -1.0476191, -0.984127, -0.9206349, -0.85714287, -0.7936508, -0.73015875, -0.6666667, -0.6031746, -0.53968257, -0.47619048, -0.41269842, -0.34920636, -0.2857143, -0.22222222, -0.15873016, -0.0952381, -0.031746034, 0.031746034, 0.0952381, 0.15873016, 0.22222222, 0.2857143, 0.34920636, 0.41269842, 0.47619048, 0.53968257, 0.6031746, 0.6666667, 0.73015875, 0.7936508, 0.85714287, 0.9206349, 0.984127, 1.0476191, 1.1111112, 1.1746032, 1.2380953, 1.3015873, 1.3650794, 1.4285715, 1.4920635, 1.5555556, 1.6190476, 1.6825397, 1.7460318, 1.8095238, 1.8730159, 1.9365079, 2,);const positionIdsData = Int32Array.of(-1, 0, 1, 2, 0, -1, 2, 1, 2, 1, 0, -1);const cosData = Float32Array.of(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8);const sinData = Float32Array.of(-0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1);
const { y } = await kernel({ x: { data: xData, shape: [2, 2, 16] }, positionIds: { data: positionIdsData, shape: [3, 2, 2] },2 unmodified lines}, { attrs: { num_heads: 2, mrope_section: [2, 1, 1] },});console.log({ y });```111 unmodified lines11211311411511511611711827 unmodified lines146147148149149150151152111 unmodified lines "vars": { "elems": 1024, "rotPairs": 512 }, "provenance": { "model": "Qwen/Qwen3-VL-2B-Instruct", "notes": "Same layer's key projection: 8 kv heads, so half the query work and the smallest dispatch M-RoPE sees in decode." "notes": "Same layer's key projection: 8 kv heads (half the query's head count), one decoded token." }, "inputs": { "x": { "shape": [1, 8, 1, 128], "dtype": "float32", "dist": "normal", "seed": 1000, "scale": 0.2 },27 unmodified lines "vars": { "elems": 1048576, "rotPairs": 524288 }, "provenance": { "model": "Qwen/Qwen2.5-VL-3B-Instruct", "notes": "Qwen2.5-VL sectioned M-RoPE, mrope_section [16, 24, 24]. The sectioned branch reads three contiguous chunks where the interleaved branch strides by 3, so the two layouts have different cache access patterns at the same shape." "notes": "Qwen2.5-VL sectioned M-RoPE, mrope_section [16, 24, 24], over 16 query heads of head_dim 128 for a 512-token prefill." }, "inputs": { "x": { "shape": [1, 16, 512, 128], "dtype": "float32", "dist": "normal", "seed": 1000, "scale": 0.2 },1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341{ "domain": "com.microsoft", "name": "MRotaryEmbedding", "sinceVersion": 1, "inputs": { "x": { "onnx": "input", "dtype": "T" }, "positionIds": { "onnx": "position_ids", "dtype": "M", "rank": 3, "storage": "int32", "narrowing": "saturating" }, "cos": { "onnx": "cos_cache", "dtype": "T", "rank": 2 }, "sin": { "onnx": "sin_cache", "dtype": "T", "rank": 2 } }, "outputs": { "y": { "onnx": "output", "dtype": "T", "rank": "ranks.x", "shape": "shapes.x" } }, "attributes": { "interleaved": { "default": 0 }, "is_packed_batching": { "default": 0 }, "mrope_layout": { "default": 0 }, "num_heads": { "default": 0 }, "rotary_embedding_dim": { "default": 0 }, "scale": { "default": 1 }, "mrope_section": {} }, "attributeConstraints": { "interleaved": { "values": [0, 1] }, "is_packed_batching": { "values": [0] }, "mrope_layout": { "values": [0, 1] }, "mrope_section": { "required": true } }, "typeConstraints": { "T": ["float32", "float16"], "M": ["int64"] }, "tunables": { "WORKGROUP_SIZE": { "default": 256 } }, "derive": { "rank3HeadSize": "dim(shapes.x, 2) / attrs.num_heads if attrs.num_heads is defined and attrs.num_heads > 0 else 0", "headSize": "rank3HeadSize if ranks.x == 3 else dim(shapes.x, 3)", "effectiveRotaryDim": "attrs.rotary_embedding_dim if attrs.rotary_embedding_dim != 0 else headSize", "tasksPerHead": "ceilDiv(headSize, 2)", "pairCount": "(numel(shapes.x) / max(1, headSize)) * tasksPerHead", "pairDispatchOk": "ceilDiv(pairCount, tunables.WORKGROUP_SIZE) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) * min(device.limits.maxComputeWorkgroupsPerDimension, 65535)", "halfRotaryDim": "dim(shapes.cos, 1)", "sectionsDefined": "attrs.mrope_section is defined and (attrs.mrope_section | length) == 3", "sectionsValid": "sectionsDefined and attrs.mrope_section[0] >= 0 and attrs.mrope_section[1] >= 0 and attrs.mrope_section[2] >= 0 and attrs.mrope_section[0] + attrs.mrope_section[1] + attrs.mrope_section[2] == halfRotaryDim", "commonContract": "f16Ok(dtypes.T) and sameShape(shapes.x, shapes.y) and (attrs.interleaved == 0 or attrs.interleaved == 1) and (attrs.mrope_layout == 0 or attrs.mrope_layout == 1) and attrs.num_heads >= 0 and attrs.rotary_embedding_dim >= 0 and (attrs.rotary_embedding_dim == 0 or attrs.num_heads > 0) and sameShape(shapes.cos, shapes.sin) and ranks.cos == 2 and ranks.sin == 2 and sectionsValid and ranks.positionIds == 3 and dim(shapes.positionIds, 0) == 3 and dim(shapes.positionIds, 1) == dim(shapes.x, 0) and dim(shapes.positionIds, 2) == dim(shapes.x, 1 if ranks.x == 3 else 2)", "rank3Contract": "commonContract and ranks.x == 3 and ranks.y == 3 and attrs.num_heads is defined and attrs.num_heads >= 1 and dim(shapes.x, 2) % attrs.num_heads == 0 and (rank3HeadSize > 0 or numel(shapes.x) == 0) and effectiveRotaryDim % 2 == 0 and (attrs.rotary_embedding_dim == 0 or attrs.rotary_embedding_dim <= rank3HeadSize) and halfRotaryDim * 2 == effectiveRotaryDim", "rank4Contract": "commonContract and ranks.x == 4 and ranks.y == 4 and dim(shapes.x, 3) > 0 and effectiveRotaryDim % 2 == 0 and (attrs.rotary_embedding_dim == 0 or attrs.rotary_embedding_dim <= dim(shapes.x, 3)) and halfRotaryDim * 2 == effectiveRotaryDim" }, "when": ["pairDispatchOk", "rank3Contract or rank4Contract"], "bindings": { "x": { "elementType": "$T" }, "position_ids": { "arg": "positionIds", "elementType": "i32" }, "cos_cache": { "arg": "cos", "elementType": "$T" }, "sin_cache": { "arg": "sin", "elementType": "$T" }, "y": { "elementType": "$T" }, "params": { "struct": [ { "name": "pairCount", "type": "u32", "value": "pairCount" }, { "name": "batchSize", "type": "u32", "value": "dim(shapes.x, 0)" }, { "name": "sequenceLength", "type": "u32", "value": "dim(shapes.x, 1 if ranks.x == 3 else 2)" }, { "name": "numHeads", "type": "u32", "value": "attrs.num_heads if ranks.x == 3 else dim(shapes.x, 1)" }, { "name": "headSize", "type": "u32", "value": "headSize" }, { "name": "rotaryDim", "type": "u32", "value": "halfRotaryDim * 2" }, { "name": "halfRotaryDim", "type": "u32", "value": "halfRotaryDim" }, { "name": "section0", "type": "u32", "value": "attrs.mrope_section[0]" }, { "name": "section1", "type": "u32", "value": "attrs.mrope_section[1]" }, { "name": "section2", "type": "u32", "value": "attrs.mrope_section[2]" }, { "name": "scale", "type": "f32", "value": "attrs.scale" }, { "name": "maxSequenceLength", "type": "u32", "value": "dim(shapes.cos, 0)" } ] }, "x_vector": { "arg": "x", "name": "x", "elementType": "$vectorScalar" }, "y_vector": { "arg": "y", "name": "y", "elementType": "$vectorScalar" }, "params_vector": { "struct": [ { "name": "pairCount", "type": "u32", "value": "pairCount" }, { "name": "batchSize", "type": "u32", "value": "dim(shapes.x, 0)" }, { "name": "sequenceLength", "type": "u32", "value": "dim(shapes.x, 1 if ranks.x == 3 else 2)" }, { "name": "numHeads", "type": "u32", "value": "attrs.num_heads if ranks.x == 3 else dim(shapes.x, 1)" }, { "name": "headSize", "type": "u32", "value": "headSize" }, { "name": "halfRotaryDim", "type": "u32", "value": "halfRotaryDim" }, { "name": "section0", "type": "u32", "value": "attrs.mrope_section[0]" }, { "name": "section1", "type": "u32", "value": "attrs.mrope_section[1]" }, { "name": "section2", "type": "u32", "value": "attrs.mrope_section[2]" }, { "name": "scale", "type": "f32", "value": "attrs.scale" }, { "name": "maxSequenceLength", "type": "u32", "value": "dim(shapes.cos, 0)" } ], "name": "params" } }, "variants": [ { "id": "pairs4", "when": ["headSize % 8 == 0", "effectiveRotaryDim == headSize", "attrs.interleaved == 0"], "derive": { "mropeSectioned": "attrs.mrope_layout == 0", "pairWidth": "4", "vectorScalar": "\"vec4<\" ~ dtypes.T ~ \">\"", "scalar": "dtypes.T" }, "passes": [ { "id": "main", "name": "MRotaryEmbedding.Vector", "shader": "mrotary-embedding-vector.wgsl.jinja", "derive": { "rank": "ranks.x" }, "bindings": ["x_vector", "position_ids", "cos_cache", "sin_cache", "y_vector", "params_vector"], "dispatch": { "x": "min(ceilDiv((pairCount / 4), (tunables.WORKGROUP_SIZE)), 65535)", "y": "ceilDiv(ceilDiv((pairCount / 4), (tunables.WORKGROUP_SIZE)), 65535)", "z": 1 } } ], "priority": 10 }, { "id": "pairs", "derive": { "interleaved": "attrs.interleaved != 0", "mropeSectioned": "attrs.mrope_layout == 0", "scalar": "dtypes.T" }, "passes": [ { "id": "main", "name": "MRotaryEmbedding", "shader": "mrotary-embedding.wgsl.jinja", "derive": { "rank": "ranks.x" }, "bindings": ["x", "position_ids", "cos_cache", "sin_cache", "y", "params"], "dispatch": { "x": "min(ceilDiv((pairCount), (tunables.WORKGROUP_SIZE)), 65535)", "y": "ceilDiv(ceilDiv((pairCount), (tunables.WORKGROUP_SIZE)), 65535)", "z": 1 } } ] } ]}{"domain":"com.microsoft","name":"MRotaryEmbedding","sinceVersion":1,"inputs":{"x":{"onnx":"input","dtype":"T"},"positionIds":{"onnx":"position_ids","dtype":"M","rank":3,"storage":"int32","narrowing":"saturating"},"cos":{"onnx":"cos_cache","dtype":"T","rank":2},"sin":{"onnx":"sin_cache","dtype":"T","rank":2}},"outputs":{"y":{"onnx":"output","dtype":"T","rank":"ranks.x","shape":"shapes.x"}},"attributes":{"interleaved":{"default":0},"is_packed_batching":{"default":0},"mrope_layout":{"default":0},"num_heads":{"default":0},"rotary_embedding_dim":{"default":0},"scale":{"default":1},"mrope_section":{}},"attributeConstraints":{"interleaved":{"values":[0,1]},"is_packed_batching":{"values":[0]},"mrope_layout":{"values":[0,1]},"mrope_section":{"required":true}},"typeConstraints":{"T":["float32","float16"],"M":["int64"]},"tunables":{"WORKGROUP_SIZE":{"default":256}},"derive":{"rank3HeadSize":"dim(shapes.x, 2) / attrs.num_heads if attrs.num_heads is defined and attrs.num_heads > 0 else 0","headSize":"rank3HeadSize if ranks.x == 3 else dim(shapes.x, 3)","effectiveRotaryDim":"attrs.rotary_embedding_dim if attrs.rotary_embedding_dim != 0 else headSize","tasksPerHead":"ceilDiv(headSize, 2)","pairCount":"(numel(shapes.x) / max(1, headSize)) * tasksPerHead","pairDispatchOk":"ceilDiv(pairCount, tunables.WORKGROUP_SIZE) <= min(device.limits.maxComputeWorkgroupsPerDimension, 65535) * min(device.limits.maxComputeWorkgroupsPerDimension, 65535)","halfRotaryDim":"dim(shapes.cos, 1)","sectionsDefined":"attrs.mrope_section is defined and (attrs.mrope_section | length) == 3","sectionsValid":"sectionsDefined and attrs.mrope_section[0] >= 0 and attrs.mrope_section[1] >= 0 and attrs.mrope_section[2] >= 0 and attrs.mrope_section[0] + attrs.mrope_section[1] + attrs.mrope_section[2] == halfRotaryDim","commonContract":"f16Ok(dtypes.T) and sameShape(shapes.x, shapes.y) and (attrs.interleaved == 0 or attrs.interleaved == 1) and (attrs.mrope_layout == 0 or attrs.mrope_layout == 1) and attrs.num_heads >= 0 and attrs.rotary_embedding_dim >= 0 and (attrs.rotary_embedding_dim == 0 or attrs.num_heads > 0) and sameShape(shapes.cos, shapes.sin) and ranks.cos == 2 and ranks.sin == 2 and sectionsValid and ranks.positionIds == 3 and dim(shapes.positionIds, 0) == 3 and dim(shapes.positionIds, 1) == dim(shapes.x, 0) and dim(shapes.positionIds, 2) == dim(shapes.x, 1 if ranks.x == 3 else 2)","rank3Contract":"commonContract and ranks.x == 3 and ranks.y == 3 and attrs.num_heads is defined and attrs.num_heads >= 1 and dim(shapes.x, 2) % attrs.num_heads == 0 and (rank3HeadSize > 0 or numel(shapes.x) == 0) and effectiveRotaryDim % 2 == 0 and (attrs.rotary_embedding_dim == 0 or attrs.rotary_embedding_dim <= rank3HeadSize) and halfRotaryDim * 2 == effectiveRotaryDim","rank4Contract":"commonContract and ranks.x == 4 and ranks.y == 4 and dim(shapes.x, 3) > 0 and effectiveRotaryDim % 2 == 0 and (attrs.rotary_embedding_dim == 0 or attrs.rotary_embedding_dim <= dim(shapes.x, 3)) and halfRotaryDim * 2 == effectiveRotaryDim"},"when":["pairDispatchOk","rank3Contract or rank4Contract"],"bindings":{"x":{"elementType":"$T"},"position_ids":{"arg":"positionIds","elementType":"i32"},"cos_cache":{"arg":"cos","elementType":"$T"},"sin_cache":{"arg":"sin","elementType":"$T"},"y":{"elementType":"$T"},"params":{"struct":[{"name":"pairCount","type":"u32","value":"pairCount"},{"name":"batchSize","type":"u32","value":"dim(shapes.x, 0)"},{"name":"sequenceLength","type":"u32","value":"dim(shapes.x, 1 if ranks.x == 3 else 2)"},{"name":"numHeads","type":"u32","value":"attrs.num_heads if ranks.x == 3 else dim(shapes.x, 1)"},{"name":"headSize","type":"u32","value":"headSize"},{"name":"rotaryDim","type":"u32","value":"halfRotaryDim * 2"},{"name":"halfRotaryDim","type":"u32","value":"halfRotaryDim"},{"name":"section0","type":"u32","value":"attrs.mrope_section[0]"},{"name":"section1","type":"u32","value":"attrs.mrope_section[1]"},{"name":"section2","type":"u32","value":"attrs.mrope_section[2]"},{"name":"scale","type":"f32","value":"attrs.scale"},{"name":"maxSequenceLength","type":"u32","value":"dim(shapes.cos, 0)"}]},"x_vector":{"arg":"x","name":"x","elementType":"$vectorScalar"},"y_vector":{"arg":"y","name":"y","elementType":"$vectorScalar"},"params_vector":{"struct":[{"name":"pairCount","type":"u32","value":"pairCount"},{"name":"batchSize","type":"u32","value":"dim(shapes.x, 0)"},{"name":"sequenceLength","type":"u32","value":"dim(shapes.x, 1 if ranks.x == 3 else 2)"},{"name":"numHeads","type":"u32","value":"attrs.num_heads if ranks.x == 3 else dim(shapes.x, 1)"},{"name":"headSize","type":"u32","value":"headSize"},{"name":"halfRotaryDim","type":"u32","value":"halfRotaryDim"},{"name":"section0","type":"u32","value":"attrs.mrope_section[0]"},{"name":"section1","type":"u32","value":"attrs.mrope_section[1]"},{"name":"section2","type":"u32","value":"attrs.mrope_section[2]"},{"name":"scale","type":"f32","value":"attrs.scale"},{"name":"maxSequenceLength","type":"u32","value":"dim(shapes.cos, 0)"}],"name":"params"}},"variants":[{"id":"pairs4","when":["headSize % 8 == 0","effectiveRotaryDim == headSize","attrs.interleaved == 0"],"derive":{"mropeSectioned":"attrs.mrope_layout == 0","pairWidth":"4","vectorScalar":"\"vec4<\" ~ dtypes.T ~ \">\"","scalar":"dtypes.T"},"passes":[{"id":"main","name":"MRotaryEmbedding.Vector","shader":"mrotary-embedding-vector.wgsl.jinja","derive":{"rank":"ranks.x"},"bindings":["x_vector","position_ids","cos_cache","sin_cache","y_vector","params_vector"],"dispatch":{"x":"min(ceilDiv((pairCount / 4), (tunables.WORKGROUP_SIZE)), 65535)","y":"ceilDiv(ceilDiv((pairCount / 4), (tunables.WORKGROUP_SIZE)), 65535)","z":1}}],"priority":10},{"id":"pairs","derive":{"interleaved":"attrs.interleaved != 0","mropeSectioned":"attrs.mrope_layout == 0","scalar":"dtypes.T"},"passes":[{"id":"main","name":"MRotaryEmbedding","shader":"mrotary-embedding.wgsl.jinja","derive":{"rank":"ranks.x"},"bindings":["x","position_ids","cos_cache","sin_cache","y","params"],"dispatch":{"x":"min(ceilDiv((pairCount), (tunables.WORKGROUP_SIZE)), 65535)","y":"ceilDiv(ceilDiv((pairCount), (tunables.WORKGROUP_SIZE)), 65535)","z":1}}]}]}123456789101112131415161718192021221{ "name": "com.microsoft.MRotaryEmbedding", "id": "_com_microsoft_mrotaryembedding_webgpu_9f14b3e", "version": 1, "license": "Apache-2.0", "backend": { "type": "webgpu" }, "digest": { "algorithm": "sha256", "files": { "bench.json": "O2zNl+WongpRuCa+0uG2J/53uKtH0dOeHjs0V/SP5NQ=", "manifest.json": "m6PieJLI+fSwIhyAV5llQg4PcQyE9OtvRC0w/Z3jOy0=", "mrotary-embedding-vector.wgsl.jinja": "MZwbSQREHhZKFMGn1YC7CJATkbgtimGoPwdnzafMOn8=", "mrotary-embedding.wgsl.jinja": "bMGNQWBiANeYtK1oQO0dRJjn/JwkB8ZTu/Zw/uKW2M4=", "test.json": "TLcimbROUuwC6I8+3L/QcPyKX9brKS3g2eH68BRDfqI=" } }, "provenance": { "kernel": { "sha": "6fdf6301e2bbcc2f03bf1eaf493b7ad55ef33afc", "dirty": false } }, "webgpu": { "manifestSpec": "2.1", "variants": { "pairs4": ["mrotary-embedding-vector.wgsl.jinja"], "pairs": ["mrotary-embedding.wgsl.jinja"] } }}{"name":"com.microsoft.MRotaryEmbedding","id":"_com_microsoft_mrotaryembedding_webgpu_11d3b42","version":1,"license":"Apache-2.0","backend":{"type":"webgpu"},"digest":{"algorithm":"sha256","files":{"bench.json":"Fl1tCB9l2RJwgn5DkO/D6WvYuJ3c3NZIxzLFuSgxcjA=","manifest.json":"C9FEdinonRogSSd8PhC1eEVnGwHEsVx+HRrUkALDuX8=","mrotary-embedding-vector.wgsl.jinja":"ANe3C9wcatYdgHH9lL2/gRWNGPaLHWfk9Mnf28OAQgQ=","mrotary-embedding.wgsl.jinja":"brYdsOS/RdiB9eYAE3H/PwVbFDW1cVHgdcigIsj1l04=","test.json":"HrO6JM/91yOQNz9rnXt1QCB3bF6I75tdr1tpxb1DdzI="}},"provenance":{"kernel":{"sha":"4385228f788ddaa1bd7aec6a80a20f3ddffd8011","dirty":false}},"webgpu":{"manifestSpec":"2.2"}}22 unmodified lines232425262627282930313233303132333734353637383922 unmodified lines let valid{{ suffix }} = pos{{ suffix }} >= 0 && u32(pos{{ suffix }}) < params.maxSequenceLength; let cache{{ suffix }} = u32(pos{{ suffix }}) * params.halfRotaryDim + {{ pair }};{% endmacro %}{% 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.{% if bound == "" %} let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};{% elif guardInline %} let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }}; if ({{ name }} >= {{ bound }}) { return; }{% else %} let {{ name }} = gid.x + gid.y * {{ DISPATCH_FOLD_WIDTH }}u * {{ wgTerm }};{% if bound != "" %} if ({{ name }} >= {{ bound }}) { return; }{% endif %}{% endmacro %} }{% endif %}{% endmacro %}{{ env.wgsl.resourceDeclarations }}// A contiguous vector owns several split-half rotation pairs. Each component// gathers its own stream position, including vectors crossing section boundaries.22 unmodified lines232425262627282930313233333435363722 unmodified lines let valid{{ suffix }} = pos{{ suffix }} >= 0 && u32(pos{{ suffix }}) < params.maxSequenceLength; let cache{{ suffix }} = u32(pos{{ suffix }}) * params.halfRotaryDim + {{ pair }};{% endmacro %}{% 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 }}// M-RoPE: three position streams (temporal, height, width) index one cos/sin cache.// The half-rotary axis is partitioned among the streams, so each rotated pair picks34 unmodified lines353637383839404139 unmodified lines818283848485868737 unmodified lines12512612712812812913013118 unmodified lines15015115215315315415515623 unmodified lines18018118218318418518618718318418518680 unmodified lines26726826927427027127227330 unmodified lines30430530631130730830931030 unmodified lines34134234334834434534634730 unmodified lines37837938038538138238338430 unmodified lines41541641742241841942042130 unmodified lines45245345445945545645745830 unmodified lines48949049149649249349449530 unmodified lines52652752853352953053153230 unmodified lines56356456557056656756856930 unmodified lines60060160260760360460560630 unmodified lines63763863964464064164264330 unmodified lines67467567668167767867968013 unmodified lines694695696701702703704705706707708709710697698699700701114 unmodified lines81681781883181982082182219 unmodified lines84284384485784584684784819 unmodified lines86886987088387187287387423 unmodified lines89889990091390190290390423 unmodified lines92892993094393193293393419 unmodified lines95495595696995795895996019 unmodified lines98098198299598398498598623 unmodified lines1010101110121025101310141015101623 unmodified lines1040104110421055104310441045104619 unmodified lines1066106710681081106910701071107219 unmodified lines1092109310941107109510961097109823 unmodified lines1122112311241137112511261127112834 unmodified lines "provenance": { "source": "onnxruntime/test/contrib_ops/mrotary_embedding_op_test.cc", "test": "ContribOpMRotaryEmbeddingTest.SectionedRank3", "notes": "Pinned from ORT. Sectioned layout over three one-column sections, so each cache column is fed by a different position stream (T=[0,1], H=[1,2], W=[2,3]) and a stream mix-up changes every output." "notes": "Expected values from ORT. Sectioned layout over three one-column sections, so each cache column is fed by a different position stream (T=[0,1], H=[1,2], W=[2,3]) and a stream mix-up changes every output." }, "attrs": { "num_heads": 2,39 unmodified lines "provenance": { "source": "onnxruntime/test/contrib_ops/mrotary_embedding_op_test.cc", "test": "ContribOpMRotaryEmbeddingTest.InterleavedRank4", "notes": "Pinned from ORT. Interleaved mrope_layout together with interleaved rotation pairing and a non-unit scale, so the two independent 'interleaved' knobs and the scale are exercised at once." "notes": "Expected values from ORT. Interleaved mrope_layout together with interleaved rotation pairing and a non-unit scale, so the two independent 'interleaved' settings and the scale are exercised at once." }, "attrs": { "num_heads": 2,37 unmodified lines { "name": "rank3_layout1_interleaved0", "provenance": { "notes": "Interleaved mrope_layout with the split (non-interleaved) rotation pairing — the layout/pairing knobs are independent, and this is the combination neither pinned case covers." "notes": "Interleaved mrope_layout with the split (non-interleaved) rotation pairing — the layout and pairing settings are independent, and this is the combination neither ORT-derived case covers." }, "attrs": { "num_heads": 4,18 unmodified lines { "name": "rank4_layout0_partial_rotary_tail", "provenance": { "notes": "Sectioned layout with rotary_embedding_dim < head_size, so the lanes past the rotary window copy their tail values unchanged." "notes": "Sectioned layout with rotary_embedding_dim < head_size, so the four elements past the rotary window are copied unchanged." }, "attrs": { "num_heads": 2,23 unmodified lines "attrs": { "num_heads": 2, "mrope_section": [2, 1, 1] }, "inputs": { "x": { "dtype": "float32", "shape": [1, 4, 16], "data": { "kind": "linspace", "start": -2.0, "end": 2.0 } }, "positionIds": { "dtype": "int32", "shape": [3, 1, 4], "data": { "kind": "values", "values": [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3] } }, "positionIds": { "dtype": "int32", "shape": [3, 1, 4], "data": { "kind": "cycle", "values": [0, 1, 2, 3] } }, "cos": { "dtype": "float32", "shape": [8, 4], "data": { "kind": "linspace", "start": -1.0, "end": 1.0 } }, "sin": { "dtype": "float32", "shape": [8, 4], "data": { "kind": "linspace", "start": 1.0, "end": -1.0 } } },80 unmodified lines }, "outputs": { "y": { "dtype": "float32", "shape": [2, 5, 24], "tolerance": 0.00001, "relTolerance": 0.00001 } }, "provenance": { "notes": "Aligned vector pairs with a partial final workgroup, section-crossing cache columns, multiple batches, non-unit scale and both tensor ranks. Zero temporal section is covered at the smallest head width." "notes": "Rotary dimension 8 over 3 heads (the whole head rotates), batch 2 and 5 positions, float32 rank-3 [batch, tokens, hidden] input, sectioned layout, mrope_section [0, 3, 1] over 4 cache columns, scale 0.3. The temporal section is empty." } }, {30 unmodified lines }, "outputs": { "y": { "dtype": "float32", "shape": [2, 5, 48], "tolerance": 0.00001, "relTolerance": 0.00001 } }, "provenance": { "notes": "Aligned vector pairs with a partial final workgroup, section-crossing cache columns, multiple batches, non-unit scale and both tensor ranks. Zero temporal section is covered at the smallest head width." "notes": "Rotary dimension 16 over 3 heads (the whole head rotates), batch 2 and 5 positions, float32 rank-3 [batch, tokens, hidden] input, sectioned layout, mrope_section [1, 2, 5] over 8 cache columns, scale 0.3." } }, {30 unmodified lines }, "outputs": { "y": { "dtype": "float32", "shape": [2, 5, 72], "tolerance": 0.00001, "relTolerance": 0.00001 } }, "provenance": { "notes": "Aligned vector pairs with a partial final workgroup, section-crossing cache columns, multiple batches, non-unit scale and both tensor ranks. Zero temporal section is covered at the smallest head width." "notes": "Rotary dimension 24 over 3 heads (the whole head rotates), batch 2 and 5 positions, float32 rank-3 [batch, tokens, hidden] input, sectioned layout, mrope_section [1, 2, 9] over 12 cache columns, scale 0.3." } }, {30 unmodified lines }, "outputs": { "y": { "dtype": "float32", "shape": [2, 3, 5, 8], "tolerance": 0.00001, "relTolerance": 0.00001 } }, "provenance": { "notes": "Aligned vector pairs with a partial final workgroup, section-crossing cache columns, multiple batches, non-unit scale and both tensor ranks. Zero temporal section is covered at the smallest head width." "notes": "Rotary dimension 8 over 3 heads (the whole head rotates), batch 2 and 5 positions, float32 rank-4 [batch, heads, tokens, head] input, interleaved layout, mrope_section [0, 3, 1] over 4 cache columns, scale 0.3. The temporal section is empty." } }, {30 unmodified lines }, "outputs": { "y": { "dtype": "float32", "shape": [2, 3, 5, 16], "tolerance": 0.00001, "relTolerance": 0.00001 } }, "provenance": { "notes": "Aligned vector pairs with a partial final workgroup, section-crossing cache columns, multiple batches, non-unit scale and both tensor ranks. Zero temporal section is covered at the smallest head width." "notes": "Rotary dimension 16 over 3 heads (the whole head rotates), batch 2 and 5 positions, float32 rank-4 [batch, heads, tokens, head] input, interleaved layout, mrope_section [1, 2, 5] over 8 cache columns, scale 0.3." } }, {30 unmodified lines }, "outputs": { "y": { "dtype": "float32", "shape": [2, 3, 5, 24], "tolerance": 0.00001, "relTolerance": 0.00001 } }, "provenance": { "notes": "Aligned vector pairs with a partial final workgroup, section-crossing cache columns, multiple batches, non-unit scale and both tensor ranks. Zero temporal section is covered at the smallest head width." "notes": "Rotary dimension 24 over 3 heads (the whole head rotates), batch 2 and 5 positions, float32 rank-4 [batch, heads, tokens, head] input, interleaved layout, mrope_section [1, 2, 9] over 12 cache columns, scale 0.3." } }, {30 unmodified lines }, "outputs": { "y": { "dtype": "float16", "shape": [2, 5, 24], "tolerance": 0.01, "relTolerance": 0.01 } }, "provenance": { "notes": "Aligned vector pairs with a partial final workgroup, section-crossing cache columns, multiple batches, non-unit scale and both tensor ranks. Zero temporal section is covered at the smallest head width." "notes": "Rotary dimension 8 over 3 heads (the whole head rotates), batch 2 and 5 positions, float16 rank-3 [batch, tokens, hidden] input, sectioned layout, mrope_section [0, 3, 1] over 4 cache columns, scale 0.3. The temporal section is empty." } }, {30 unmodified lines }, "outputs": { "y": { "dtype": "float16", "shape": [2, 5, 48], "tolerance": 0.01, "relTolerance": 0.01 } }, "provenance": { "notes": "Aligned vector pairs with a partial final workgroup, section-crossing cache columns, multiple batches, non-unit scale and both tensor ranks. Zero temporal section is covered at the smallest head width." "notes": "Rotary dimension 16 over 3 heads (the whole head rotates), batch 2 and 5 positions, float16 rank-3 [batch, tokens, hidden] input, sectioned layout, mrope_section [1, 2, 5] over 8 cache columns, scale 0.3." } }, {30 unmodified lines }, "outputs": { "y": { "dtype": "float16", "shape": [2, 5, 72], "tolerance": 0.01, "relTolerance": 0.01 } }, "provenance": { "notes": "Aligned vector pairs with a partial final workgroup, section-crossing cache columns, multiple batches, non-unit scale and both tensor ranks. Zero temporal section is covered at the smallest head width." "notes": "Rotary dimension 24 over 3 heads (the whole head rotates), batch 2 and 5 positions, float16 rank-3 [batch, tokens, hidden] input, sectioned layout, mrope_section [1, 2, 9] over 12 cache columns, scale 0.3." } }, {30 unmodified lines }, "outputs": { "y": { "dtype": "float16", "shape": [2, 3, 5, 8], "tolerance": 0.01, "relTolerance": 0.01 } }, "provenance": { "notes": "Aligned vector pairs with a partial final workgroup, section-crossing cache columns, multiple batches, non-unit scale and both tensor ranks. Zero temporal section is covered at the smallest head width." "notes": "Rotary dimension 8 over 3 heads (the whole head rotates), batch 2 and 5 positions, float16 rank-4 [batch, heads, tokens, head] input, interleaved layout, mrope_section [0, 3, 1] over 4 cache columns, scale 0.3. The temporal section is empty." } }, {30 unmodified lines }, "outputs": { "y": { "dtype": "float16", "shape": [2, 3, 5, 16], "tolerance": 0.01, "relTolerance": 0.01 } }, "provenance": { "notes": "Aligned vector pairs with a partial final workgroup, section-crossing cache columns, multiple batches, non-unit scale and both tensor ranks. Zero temporal section is covered at the smallest head width." "notes": "Rotary dimension 16 over 3 heads (the whole head rotates), batch 2 and 5 positions, float16 rank-4 [batch, heads, tokens, head] input, interleaved layout, mrope_section [1, 2, 5] over 8 cache columns, scale 0.3." } }, {30 unmodified lines }, "outputs": { "y": { "dtype": "float16", "shape": [2, 3, 5, 24], "tolerance": 0.01, "relTolerance": 0.01 } }, "provenance": { "notes": "Aligned vector pairs with a partial final workgroup, section-crossing cache columns, multiple batches, non-unit scale and both tensor ranks. Zero temporal section is covered at the smallest head width." "notes": "Rotary dimension 24 over 3 heads (the whole head rotates), batch 2 and 5 positions, float16 rank-4 [batch, heads, tokens, head] input, interleaved layout, mrope_section [1, 2, 9] over 12 cache columns, scale 0.3." } }, {13 unmodified lines "data": { "kind": "values", "values": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0] } }, "positionIds": { "dtype": "int32", "shape": [3, 1, 1], "data": { "kind": "values", "values": [0, 0, 0] } }, "cos": { "dtype": "float32", "shape": [4, 3], "data": { "kind": "values", "values": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] } }, "sin": { "dtype": "float32", "shape": [4, 3], "data": { "kind": "values", "values": [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] } } "cos": { "dtype": "float32", "shape": [4, 3], "data": { "kind": "constant", "value": 0.0 } }, "sin": { "dtype": "float32", "shape": [4, 3], "data": { "kind": "constant", "value": 1.0 } } }, "outputs": { "y": { "dtype": "float32", "shape": [1, 1, 8], "tolerance": 0.000001, "relTolerance": 0.000001 } } },114 unmodified lines "provenance": { "source": "onnxruntime/test/contrib_ops/mrotary_embedding_op_test.cc", "test": "PositionIdsOOBPassthroughAllStreams", "notes": "Each invalid stream copies only its own rotation pair. Values outside int32 are saturated by the host projection." "notes": "The temporal position id -1 is negative, with the other two streams at 0. The invalid stream copies only its own rotation pair." }, "attrs": { "num_heads": 1, "rotary_embedding_dim": 6, "mrope_section": [1, 1, 1] }, "inputs": {19 unmodified lines "provenance": { "source": "onnxruntime/test/contrib_ops/mrotary_embedding_op_test.cc", "test": "PositionIdsOOBPassthroughAllStreams", "notes": "Each invalid stream copies only its own rotation pair. Values outside int32 are saturated by the host projection." "notes": "The temporal position id 2 equals the cache length of 2, with the other two streams at 0. The invalid stream copies only its own rotation pair." }, "attrs": { "num_heads": 1, "rotary_embedding_dim": 6, "mrope_section": [1, 1, 1] }, "inputs": {19 unmodified lines "provenance": { "source": "onnxruntime/test/contrib_ops/mrotary_embedding_op_test.cc", "test": "PositionIdsOOBPassthroughAllStreams", "notes": "Each invalid stream copies only its own rotation pair. Values outside int32 are saturated by the host projection." "notes": "The temporal position id 2147483647 is the int32 maximum, with the other two streams at 0. The invalid stream copies only its own rotation pair. The saturated values stand in for ids outside the int32 range." }, "attrs": { "num_heads": 1, "rotary_embedding_dim": 6, "mrope_section": [1, 1, 1] }, "inputs": {23 unmodified lines "provenance": { "source": "onnxruntime/test/contrib_ops/mrotary_embedding_op_test.cc", "test": "PositionIdsOOBPassthroughAllStreams", "notes": "Each invalid stream copies only its own rotation pair. Values outside int32 are saturated by the host projection." "notes": "The temporal position id -2147483648 is the int32 minimum, with the other two streams at 0. The invalid stream copies only its own rotation pair. The saturated values stand in for ids outside the int32 range." }, "attrs": { "num_heads": 1, "rotary_embedding_dim": 6, "mrope_section": [1, 1, 1] }, "inputs": {23 unmodified lines "provenance": { "source": "onnxruntime/test/contrib_ops/mrotary_embedding_op_test.cc", "test": "PositionIdsOOBPassthroughAllStreams", "notes": "Each invalid stream copies only its own rotation pair. Values outside int32 are saturated by the host projection." "notes": "The height position id -1 is negative, with the other two streams at 0. The invalid stream copies only its own rotation pair." }, "attrs": { "num_heads": 1, "rotary_embedding_dim": 6, "mrope_section": [1, 1, 1] }, "inputs": {19 unmodified lines "provenance": { "source": "onnxruntime/test/contrib_ops/mrotary_embedding_op_test.cc", "test": "PositionIdsOOBPassthroughAllStreams", "notes": "Each invalid stream copies only its own rotation pair. Values outside int32 are saturated by the host projection." "notes": "The height position id 2 equals the cache length of 2, with the other two streams at 0. The invalid stream copies only its own rotation pair." }, "attrs": { "num_heads": 1, "rotary_embedding_dim": 6, "mrope_section": [1, 1, 1] }, "inputs": {19 unmodified lines "provenance": { "source": "onnxruntime/test/contrib_ops/mrotary_embedding_op_test.cc", "test": "PositionIdsOOBPassthroughAllStreams", "notes": "Each invalid stream copies only its own rotation pair. Values outside int32 are saturated by the host projection." "notes": "The height position id 2147483647 is the int32 maximum, with the other two streams at 0. The invalid stream copies only its own rotation pair. The saturated values stand in for ids outside the int32 range." }, "attrs": { "num_heads": 1, "rotary_embedding_dim": 6, "mrope_section": [1, 1, 1] }, "inputs": {23 unmodified lines "provenance": { "source": "onnxruntime/test/contrib_ops/mrotary_embedding_op_test.cc", "test": "PositionIdsOOBPassthroughAllStreams", "notes": "Each invalid stream copies only its own rotation pair. Values outside int32 are saturated by the host projection." "notes": "The height position id -2147483648 is the int32 minimum, with the other two streams at 0. The invalid stream copies only its own rotation pair. The saturated values stand in for ids outside the int32 range." }, "attrs": { "num_heads": 1, "rotary_embedding_dim": 6, "mrope_section": [1, 1, 1] }, "inputs": {23 unmodified lines "provenance": { "source": "onnxruntime/test/contrib_ops/mrotary_embedding_op_test.cc", "test": "PositionIdsOOBPassthroughAllStreams", "notes": "Each invalid stream copies only its own rotation pair. Values outside int32 are saturated by the host projection." "notes": "The width position id -1 is negative, with the other two streams at 0. The invalid stream copies only its own rotation pair." }, "attrs": { "num_heads": 1, "rotary_embedding_dim": 6, "mrope_section": [1, 1, 1] }, "inputs": {19 unmodified lines "provenance": { "source": "onnxruntime/test/contrib_ops/mrotary_embedding_op_test.cc", "test": "PositionIdsOOBPassthroughAllStreams", "notes": "Each invalid stream copies only its own rotation pair. Values outside int32 are saturated by the host projection." "notes": "The width position id 2 equals the cache length of 2, with the other two streams at 0. The invalid stream copies only its own rotation pair." }, "attrs": { "num_heads": 1, "rotary_embedding_dim": 6, "mrope_section": [1, 1, 1] }, "inputs": {19 unmodified lines "provenance": { "source": "onnxruntime/test/contrib_ops/mrotary_embedding_op_test.cc", "test": "PositionIdsOOBPassthroughAllStreams", "notes": "Each invalid stream copies only its own rotation pair. Values outside int32 are saturated by the host projection." "notes": "The width position id 2147483647 is the int32 maximum, with the other two streams at 0. The invalid stream copies only its own rotation pair. The saturated values stand in for ids outside the int32 range." }, "attrs": { "num_heads": 1, "rotary_embedding_dim": 6, "mrope_section": [1, 1, 1] }, "inputs": {23 unmodified lines "provenance": { "source": "onnxruntime/test/contrib_ops/mrotary_embedding_op_test.cc", "test": "PositionIdsOOBPassthroughAllStreams", "notes": "Each invalid stream copies only its own rotation pair. Values outside int32 are saturated by the host projection." "notes": "The width position id -2147483648 is the int32 minimum, with the other two streams at 0. The invalid stream copies only its own rotation pair. The saturated values stand in for ids outside the int32 range." }, "attrs": { "num_heads": 1, "rotary_embedding_dim": 6, "mrope_section": [1, 1, 1] }, "inputs": {README.md+26-9
build/webgpu/bench.json+2-2
build/webgpu/manifest.json+1-134
build/webgpu/metadata.json+1-22
build/webgpu/mrotary-embedding-vector.wgsl.jinja+5-6
build/webgpu/mrotary-embedding.wgsl.jinja+3-2
build/webgpu/test.json+31-43