File size: 8,176 Bytes
47389e2 2a3f031 47389e2 2a3f031 47389e2 2a3f031 74f557c 2a3f031 74f557c 2a3f031 74f557c 2a3f031 59216c2 2a3f031 59216c2 74f557c 59216c2 2a3f031 74f557c 2a3f031 59216c2 74f557c 59216c2 74f557c 59216c2 2a3f031 59216c2 2a3f031 74f557c 2a3f031 74f557c 2a3f031 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | ---
library_name: kernels
license: apache-2.0
tags:
- kernel
- webgpu
- wgsl
---
# com.microsoft.LinearAttention
`com.microsoft` · ONNX Runtime contrib operator · contrib since_version 1
## Description
Recurrent linear attention for packed `[B, T, H*D]` decode and prefill. It supports all four update rules, standard and inverse GQA, shared-key heads, and rollback states through `state_window`. Activations and state may independently use float16 or float32; bfloat16 is not implemented. `past_state` is optional for every update rule and defaults to zeros.
See the [ONNX Runtime `LinearAttention` contrib-operator spec](https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#com.microsoft.LinearAttention) for the reference semantics.
## Differences from the ONNX Runtime spec
- Attribute `chunk_size`: The schema defines `chunk_size` as a scheduling hint. Every value is accepted and none changes the result.
## Inputs
| Name | Logical dtype | Rank | Shape | Description | Presence |
| --- | --- | --- | --- | --- | --- |
| `query` | `T` | `3` | — | Query vectors with 3D packed shape `(B, T, H_q * d_k)`; heads are packed into the last dimension. | required |
| `key` | `T` | `3` | — | Key vectors with 3D packed shape `(B, T, H_k * d_k)`, where positive `H_k` divides `H_kv`; `H_k < H_kv` shares each key head across multiple KV-state heads. Keys should be L2-normalized for `delta`/`gated_delta` modes. | required |
| `value` | `T` | `3` | — | Value vectors with 3D packed shape `(B, T, H_kv * d_v)`. | required |
| `past_state` | `S` | derived | derived | Recurrent state from the previous step with shape `(B, H_kv, d_k, d_v)`, or `(W, B, H_kv, d_k, d_v)` when `state_window = W > 0`; defaults to zeros if absent. | optional |
| `decay` | `T` | `3` | — | Exponential decay gate in log-space with shape `(B, T, H_kv * d_k)` or `(B, T, H_kv)`; required for `gated` and `gated_delta` modes. | optional |
| `beta` | `T` | `3` | — | Update rate (sigmoid output) with shape `(B, T, H_kv)` or `(B, T, 1)`; required for `delta` and `gated_delta` modes. | optional |
## Outputs
| Name | Logical dtype | Rank | Shape | Description | Presence |
| --- | --- | --- | --- | --- | --- |
| `output` | `T` | `3` | derived | Attention output with 3D packed shape `(B, T, max(H_q, H_kv) * d_v)`. | required |
| `present_state` | `S` | derived | derived | Updated recurrent state with shape `(B, H_kv, d_k, d_v)`, or `(W, B, H_kv, d_k, d_v)` when `state_window = W > 0`. | required |
## Attributes
Attributes and default values (overridable per request):
| Attribute | Default | Description |
| --- | --- | --- |
| `chunk_size` | `64` | Accepted for schema compatibility; does not affect the result. |
| `kv_num_heads` | — | Number of key/value heads. |
| `q_num_heads` | — | Number of query heads. |
| `scale` | `0` | Scale applied to query-key products. Zero selects `1 / sqrt(d_k)`. |
| `state_window` | `0` | Number of recent recurrent states retained in `present_state`, in the supported range 0 to 8; zero returns only the current state. |
| `update_rule` | `"gated_delta"` | Recurrent update rule: `linear`, `gated`, `delta`, or `gated_delta`. |
## Type constraints
| Variable | Allowed dtypes |
| --- | --- |
| `T` | `float32`, `float16` |
| `S` | `float32`, `float16` |
## 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.
- `vec4_static` — Specializes recurrent indexing to tensor geometry while retaining the scalar scale uniform and device-bounded lane layout.
- `serial_small_dk` — Small key dimensions keep each state column private across the sequence. Linear updates specialize shape-known indexing and loop bounds, while gated-delta updates retain their uniform-driven recurrence.
## Device requirements
Some implementation variants require `subgroups`. Other variants run without it; which variant runs also depends on the request's shapes and dtypes.
## Files
- [`metadata.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/metadata.json) — kernel metadata (id, file digests, manifest spec, provenance)
- [`manifest.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/manifest.json) — the op contract (source of truth)
- [`test.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/test.json) — correctness cases
- [`bench.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/bench.json) — benchmark cases
- [`chunk-out.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/chunk-out.wgsl.jinja)
- [`chunk-prep.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/chunk-prep.wgsl.jinja)
- [`chunk-scan.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/chunk-scan.wgsl.jinja)
- [`chunk-ut.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/chunk-ut.wgsl.jinja)
- [`linear-attention.scalar.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/linear-attention.scalar.wgsl.jinja)
- [`linear-attention.serial.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/linear-attention.serial.wgsl.jinja)
- [`linear-attention.vec4.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/linear-attention.vec4.wgsl.jinja)
## Use with `@huggingface/kernels`
```sh
npm 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`.
```js
import { getKernel } from "@huggingface/kernels";
const kernel = await getKernel("webgpu-kernels/com.microsoft.LinearAttention", { version: 1 });
const queryData = Float32Array.of(
0.16359815, 0.16205266, 0.149098, 0.1309263, 0.1135055, 0.10155348, 0.09775273,
0.10232112, 0.11300086, 0.12546599, 0.13408697, 0.13293777, 0.11689379, 0.0826538,
0.029526938, -0.04014262, -0.12099168, -0.20511103, -0.28292736, -0.34438634,
-0.38028362, -0.3835658, -0.35042152, -0.28100657,
);
const keyData = Float32Array.of(
0.070980474, 0.023776323, -0.008363308, -0.008291626, 0.032685805, 0.11251383,
0.21865256, 0.33084837, 0.42576405, 0.48238245, 0.4869145, 0.4360576,
);
const valueData = Float32Array.of(
0.011000581, -0.053772997, -0.07845871, -0.044196326, 0.048658974, 0.18047206,
0.31837228, 0.42649853, 0.47720772, 0.45970497, 0.3833686, 0.2747198,
);
const past_stateData = Float32Array.of(
0.09853272, 0.07704112, 0.05560378, 0.044387434, 0.05080398, 0.07792509, 0.123736635,
0.18137693, 0.2403211, 0.28829855, 0.31359476, 0.307315, 0.26519224, 0.18860176,
0.084587306, -0.03511816,
);
const decayData = Float32Array.of(-0.09725125, -0.0639406, -0.08095616);
const betaData = Float32Array.of(0.68809795, 0.7210754, 0.68271667);
const { output, present_state } = await kernel({
query: { data: queryData, shape: [1, 3, 8] },
key: { data: keyData, shape: [1, 3, 4] },
value: { data: valueData, shape: [1, 3, 4] },
past_state: { data: past_stateData, shape: [1, 1, 4, 4] },
decay: { data: decayData, shape: [1, 3, 1] },
beta: { data: betaData, shape: [1, 3, 1] },
}, {
attrs: { q_num_heads: 2, kv_num_heads: 1 },
});
console.log({ output, present_state });
```
|