sync 4385228f788d
Browse files- README.md +67 -37
- build/webgpu/bench.json +0 -0
- build/webgpu/chunk-out.wgsl.jinja +101 -2
- build/webgpu/chunk-prep.wgsl.jinja +43 -11
- build/webgpu/chunk-scan.wgsl.jinja +27 -1
- build/webgpu/chunk-ut.wgsl.jinja +34 -1
- build/webgpu/linear-attention.scalar.wgsl.jinja +97 -23
- build/webgpu/linear-attention.serial.wgsl.jinja +69 -13
- build/webgpu/linear-attention.vec4.wgsl.jinja +619 -108
- build/webgpu/manifest.json +0 -0
- build/webgpu/metadata.json +1 -56
- build/webgpu/test.json +0 -0
README.md
CHANGED
|
@@ -16,23 +16,27 @@ Recurrent linear attention for packed `[B, T, H*D]` decode and prefill. It suppo
|
|
| 16 |
|
| 17 |
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.
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
## Inputs
|
| 20 |
|
| 21 |
-
| Name |
|
| 22 |
-
| --- | --- | --- | --- | --- | --- |
|
| 23 |
-
| `
|
| 24 |
-
| `
|
| 25 |
-
| `
|
| 26 |
-
| `
|
| 27 |
-
| `
|
| 28 |
-
| `
|
| 29 |
|
| 30 |
## Outputs
|
| 31 |
|
| 32 |
-
| Name |
|
| 33 |
-
| --- | --- | --- | --- | --- | --- |
|
| 34 |
-
| `
|
| 35 |
-
| `
|
| 36 |
|
| 37 |
## Attributes
|
| 38 |
|
|
@@ -58,50 +62,76 @@ Attributes and default values (overridable per request):
|
|
| 58 |
|
| 59 |
One implementation is selected per call from the device capabilities, the request shapes and the dtypes; these notes say what each one covers.
|
| 60 |
|
| 61 |
-
- `
|
| 62 |
-
- `
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
|
| 66 |
## Files
|
| 67 |
|
| 68 |
-
- [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests,
|
| 69 |
-
- [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth)
|
| 70 |
-
- [`test.json`](build/webgpu/test.json) — correctness cases
|
| 71 |
-
- [`bench.json`](build/webgpu/bench.json) — benchmark cases
|
| 72 |
-
- [`chunk-out.wgsl.jinja`](build/webgpu/chunk-out.wgsl.jinja)
|
| 73 |
-
- [`chunk-prep.wgsl.jinja`](build/webgpu/chunk-prep.wgsl.jinja)
|
| 74 |
-
- [`chunk-scan.wgsl.jinja`](build/webgpu/chunk-scan.wgsl.jinja)
|
| 75 |
-
- [`chunk-ut.wgsl.jinja`](build/webgpu/chunk-ut.wgsl.jinja)
|
| 76 |
-
- [`linear-attention.scalar.wgsl.jinja`](build/webgpu/linear-attention.scalar.wgsl.jinja)
|
| 77 |
-
- [`linear-attention.serial.wgsl.jinja`](build/webgpu/linear-attention.serial.wgsl.jinja)
|
| 78 |
-
- [`linear-attention.vec4.wgsl.jinja`](build/webgpu/linear-attention.vec4.wgsl.jinja)
|
| 79 |
|
| 80 |
## Use with `@huggingface/kernels`
|
| 81 |
|
| 82 |
```sh
|
| 83 |
-
npm install --save-exact @huggingface/kernels@0.0.1-preview.
|
| 84 |
```
|
| 85 |
|
|
|
|
|
|
|
| 86 |
Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.
|
| 87 |
|
| 88 |
The `version: 1` option selects the published kernel contract; it is independent of any operator opset, contrib `since_version`, or model version.
|
| 89 |
It follows the `v1` branch as fixes land. To pin exact artifact bytes, pass a 40-character commit `revision` instead of `version`.
|
| 90 |
|
| 91 |
-
Replace each `*Data` placeholder with a typed array containing the corresponding input data.
|
| 92 |
-
|
| 93 |
```js
|
| 94 |
import { getKernel } from "@huggingface/kernels";
|
| 95 |
|
| 96 |
const kernel = await getKernel("webgpu-kernels/com.microsoft.LinearAttention", { version: 1 });
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
}, {
|
| 105 |
attrs: { q_num_heads: 2, kv_num_heads: 1 },
|
| 106 |
});
|
|
|
|
| 107 |
```
|
|
|
|
| 16 |
|
| 17 |
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.
|
| 18 |
|
| 19 |
+
## Differences from the ONNX Runtime spec
|
| 20 |
+
|
| 21 |
+
- Attribute `chunk_size`: The schema defines `chunk_size` as a scheduling hint. Every value is accepted and none changes the result.
|
| 22 |
+
|
| 23 |
## Inputs
|
| 24 |
|
| 25 |
+
| Name | Logical dtype | Rank | Shape | Description | Presence |
|
| 26 |
+
| --- | --- | --- | --- | --- | --- |
|
| 27 |
+
| `query` | `T` | `3` | — | Query vectors with 3D packed shape `(B, T, H_q * d_k)`; heads are packed into the last dimension. | required |
|
| 28 |
+
| `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 |
|
| 29 |
+
| `value` | `T` | `3` | — | Value vectors with 3D packed shape `(B, T, H_kv * d_v)`. | required |
|
| 30 |
+
| `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 |
|
| 31 |
+
| `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 |
|
| 32 |
+
| `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 |
|
| 33 |
|
| 34 |
## Outputs
|
| 35 |
|
| 36 |
+
| Name | Logical dtype | Rank | Shape | Description | Presence |
|
| 37 |
+
| --- | --- | --- | --- | --- | --- |
|
| 38 |
+
| `output` | `T` | `3` | derived | Attention output with 3D packed shape `(B, T, max(H_q, H_kv) * d_v)`. | required |
|
| 39 |
+
| `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 |
|
| 40 |
|
| 41 |
## Attributes
|
| 42 |
|
|
|
|
| 62 |
|
| 63 |
One implementation is selected per call from the device capabilities, the request shapes and the dtypes; these notes say what each one covers.
|
| 64 |
|
| 65 |
+
- `vec4_static` — Specializes recurrent indexing to tensor geometry while retaining the scalar scale uniform and device-bounded lane layout.
|
| 66 |
+
- `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.
|
| 67 |
+
|
| 68 |
+
## Device requirements
|
| 69 |
+
|
| 70 |
+
Some implementation variants require `subgroups`. Other variants run without it; which variant runs also depends on the request's shapes and dtypes.
|
| 71 |
|
| 72 |
## Files
|
| 73 |
|
| 74 |
+
- [`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)
|
| 75 |
+
- [`manifest.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/manifest.json) — the op contract (source of truth)
|
| 76 |
+
- [`test.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/test.json) — correctness cases
|
| 77 |
+
- [`bench.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/bench.json) — benchmark cases
|
| 78 |
+
- [`chunk-out.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/chunk-out.wgsl.jinja)
|
| 79 |
+
- [`chunk-prep.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/chunk-prep.wgsl.jinja)
|
| 80 |
+
- [`chunk-scan.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/chunk-scan.wgsl.jinja)
|
| 81 |
+
- [`chunk-ut.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/chunk-ut.wgsl.jinja)
|
| 82 |
+
- [`linear-attention.scalar.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/linear-attention.scalar.wgsl.jinja)
|
| 83 |
+
- [`linear-attention.serial.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/linear-attention.serial.wgsl.jinja)
|
| 84 |
+
- [`linear-attention.vec4.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.LinearAttention/blob/v1/build/webgpu/linear-attention.vec4.wgsl.jinja)
|
| 85 |
|
| 86 |
## Use with `@huggingface/kernels`
|
| 87 |
|
| 88 |
```sh
|
| 89 |
+
npm install --save-exact @huggingface/kernels@0.0.1-preview.4
|
| 90 |
```
|
| 91 |
|
| 92 |
+
Paste the example into a browser app that resolves npm imports. Serve the app over HTTPS or localhost with WebGPU available.
|
| 93 |
+
|
| 94 |
Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.
|
| 95 |
|
| 96 |
The `version: 1` option selects the published kernel contract; it is independent of any operator opset, contrib `since_version`, or model version.
|
| 97 |
It follows the `v1` branch as fixes land. To pin exact artifact bytes, pass a 40-character commit `revision` instead of `version`.
|
| 98 |
|
|
|
|
|
|
|
| 99 |
```js
|
| 100 |
import { getKernel } from "@huggingface/kernels";
|
| 101 |
|
| 102 |
const kernel = await getKernel("webgpu-kernels/com.microsoft.LinearAttention", { version: 1 });
|
| 103 |
+
|
| 104 |
+
const queryData = Float32Array.of(
|
| 105 |
+
0.16359815, 0.16205266, 0.149098, 0.1309263, 0.1135055, 0.10155348, 0.09775273,
|
| 106 |
+
0.10232112, 0.11300086, 0.12546599, 0.13408697, 0.13293777, 0.11689379, 0.0826538,
|
| 107 |
+
0.029526938, -0.04014262, -0.12099168, -0.20511103, -0.28292736, -0.34438634,
|
| 108 |
+
-0.38028362, -0.3835658, -0.35042152, -0.28100657,
|
| 109 |
+
);
|
| 110 |
+
const keyData = Float32Array.of(
|
| 111 |
+
0.070980474, 0.023776323, -0.008363308, -0.008291626, 0.032685805, 0.11251383,
|
| 112 |
+
0.21865256, 0.33084837, 0.42576405, 0.48238245, 0.4869145, 0.4360576,
|
| 113 |
+
);
|
| 114 |
+
const valueData = Float32Array.of(
|
| 115 |
+
0.011000581, -0.053772997, -0.07845871, -0.044196326, 0.048658974, 0.18047206,
|
| 116 |
+
0.31837228, 0.42649853, 0.47720772, 0.45970497, 0.3833686, 0.2747198,
|
| 117 |
+
);
|
| 118 |
+
const past_stateData = Float32Array.of(
|
| 119 |
+
0.09853272, 0.07704112, 0.05560378, 0.044387434, 0.05080398, 0.07792509, 0.123736635,
|
| 120 |
+
0.18137693, 0.2403211, 0.28829855, 0.31359476, 0.307315, 0.26519224, 0.18860176,
|
| 121 |
+
0.084587306, -0.03511816,
|
| 122 |
+
);
|
| 123 |
+
const decayData = Float32Array.of(-0.09725125, -0.0639406, -0.08095616);
|
| 124 |
+
const betaData = Float32Array.of(0.68809795, 0.7210754, 0.68271667);
|
| 125 |
+
|
| 126 |
+
const { output, present_state } = await kernel({
|
| 127 |
+
query: { data: queryData, shape: [1, 3, 8] },
|
| 128 |
+
key: { data: keyData, shape: [1, 3, 4] },
|
| 129 |
+
value: { data: valueData, shape: [1, 3, 4] },
|
| 130 |
+
past_state: { data: past_stateData, shape: [1, 1, 4, 4] },
|
| 131 |
+
decay: { data: decayData, shape: [1, 3, 1] },
|
| 132 |
+
beta: { data: betaData, shape: [1, 3, 1] },
|
| 133 |
}, {
|
| 134 |
attrs: { q_num_heads: 2, kv_num_heads: 1 },
|
| 135 |
});
|
| 136 |
+
console.log({ output, present_state });
|
| 137 |
```
|
build/webgpu/bench.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
build/webgpu/chunk-out.wgsl.jinja
CHANGED
|
@@ -2,6 +2,9 @@
|
|
| 2 |
{% macro write_scalar(expr, dtype) %}{% if dtype == "float16" %}f16({{ expr }}){% else %}{{ expr }}{% endif %}{% endmacro %}
|
| 3 |
{% macro decay_at(bt, h, i) %}{% if decayPerElement %}gexp[({{ bt }}) * params.decayPackedDim + ({{ h }}) * HEAD_DIM_K + ({{ i }})]{% else %}gexp[({{ bt }}) * params.decayPackedDim + ({{ h }})]{% endif %}{% endmacro %}
|
| 4 |
{% macro emit_chunk_operands(needKtil=false, needQuery=false) %}
|
|
|
|
|
|
|
|
|
|
| 5 |
// The output scale defaults to 1/sqrt(head_dim_k), matching the recurrent kernels.
|
| 6 |
fn out_scale() -> f32 {
|
| 7 |
return select(inverseSqrt(f32(HEAD_DIM_K)), params.scale, params.scale != 0.0);
|
|
@@ -9,6 +12,7 @@ fn out_scale() -> f32 {
|
|
| 9 |
fn k_hat(bt: u32, h: u32, i: u32) -> f32 {
|
| 10 |
let raw = {{ read_scalar("key", "bt * params.kPackedDim + (h / KV_PER_KEY_HEAD) * HEAD_DIM_K + i", queryDtype) }};
|
| 11 |
{% if usesDecay %}
|
|
|
|
| 12 |
return raw / {{ decay_at("bt", "h", "i") }};
|
| 13 |
{% else %}
|
| 14 |
return raw;
|
|
@@ -20,6 +24,7 @@ fn k_hat(bt: u32, h: u32, i: u32) -> f32 {
|
|
| 20 |
fn q_til(bt: u32, q_head: u32, {% if usesDecay %}h: u32, {% endif %}i: u32, scale: f32) -> f32 {
|
| 21 |
let raw = {{ read_scalar("query", "bt * params.qPackedDim + q_head * HEAD_DIM_K + i", queryDtype) }};
|
| 22 |
{% if usesDecay %}
|
|
|
|
| 23 |
return raw * {{ decay_at("bt", "h", "i") }} * scale;
|
| 24 |
{% else %}
|
| 25 |
return raw * scale;
|
|
@@ -58,6 +63,66 @@ var<workgroup> qblock: array<f32, TOKEN_ROWS * HEAD_DIM_K>;
|
|
| 58 |
var<workgroup> ktile: array<f32, CHUNK * TK>;
|
| 59 |
var<workgroup> pmrow: array<f32, TOKEN_ROWS * CHUNK>;
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
@compute @workgroup_size(WG, 1, 1)
|
| 62 |
fn main(
|
| 63 |
@builtin(workgroup_id) wg: vec3<u32>,
|
|
@@ -75,6 +140,10 @@ fn main(
|
|
| 75 |
}
|
| 76 |
let head = out_head / heads_per_group;
|
| 77 |
let q_head = (head * params.qNumHeads) / params.kvNumHeads + out_head % heads_per_group;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
let base_t = batch * params.seqLength + chunk * CHUNK;
|
| 79 |
let live = min(CHUNK, params.seqLength - chunk * CHUNK);
|
| 80 |
let scale = out_scale();
|
|
@@ -120,11 +189,28 @@ fn main(
|
|
| 120 |
for (var e = tid; e < TOKEN_ROWS * CHUNK; e = e + WG) {
|
| 121 |
let m = e / CHUNK;
|
| 122 |
let s = e % CHUNK;
|
| 123 |
-
if (s <= tb + m) {
|
|
|
|
|
|
|
|
|
|
| 124 |
var total = 0.0;
|
| 125 |
for (var u = 0u; u < TK; u = u + 1u) {
|
| 126 |
if (kb + u < HEAD_DIM_K) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
total = total + qblock[m * HEAD_DIM_K + kb + u] * ktile[s * TK + u];
|
|
|
|
|
|
|
|
|
|
| 128 |
}
|
| 129 |
}
|
| 130 |
pmrow[e] = pmrow[e] + total;
|
|
@@ -136,7 +222,16 @@ fn main(
|
|
| 136 |
if (i < HEAD_DIM_K) {
|
| 137 |
let sv = states[state_base + i * HEAD_DIM_V + tid];
|
| 138 |
for (var m = 0u; m < TOKEN_ROWS; m = m + 1u) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
acc[m] = acc[m] + qblock[m * HEAD_DIM_K + i] * sv;
|
|
|
|
|
|
|
|
|
|
| 140 |
}
|
| 141 |
}
|
| 142 |
}
|
|
@@ -161,8 +256,12 @@ fn main(
|
|
| 161 |
for (var m = 0u; m < TOKEN_ROWS; m = m + 1u) {
|
| 162 |
let t = tb + m;
|
| 163 |
if (t < live) {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
output[(base_t + t) * packed_out + out_head * HEAD_DIM_V + tid] =
|
| 165 |
-
{{ write_scalar("
|
| 166 |
}
|
| 167 |
}
|
| 168 |
}
|
|
|
|
| 2 |
{% macro write_scalar(expr, dtype) %}{% if dtype == "float16" %}f16({{ expr }}){% else %}{{ expr }}{% endif %}{% endmacro %}
|
| 3 |
{% macro decay_at(bt, h, i) %}{% if decayPerElement %}gexp[({{ bt }}) * params.decayPackedDim + ({{ h }}) * HEAD_DIM_K + ({{ i }})]{% else %}gexp[({{ bt }}) * params.decayPackedDim + ({{ h }})]{% endif %}{% endmacro %}
|
| 4 |
{% macro emit_chunk_operands(needKtil=false, needQuery=false) %}
|
| 5 |
+
{% if usesDecay %}
|
| 6 |
+
var<private> chunk_log_mode: bool;
|
| 7 |
+
{% endif %}
|
| 8 |
// The output scale defaults to 1/sqrt(head_dim_k), matching the recurrent kernels.
|
| 9 |
fn out_scale() -> f32 {
|
| 10 |
return select(inverseSqrt(f32(HEAD_DIM_K)), params.scale, params.scale != 0.0);
|
|
|
|
| 12 |
fn k_hat(bt: u32, h: u32, i: u32) -> f32 {
|
| 13 |
let raw = {{ read_scalar("key", "bt * params.kPackedDim + (h / KV_PER_KEY_HEAD) * HEAD_DIM_K + i", queryDtype) }};
|
| 14 |
{% if usesDecay %}
|
| 15 |
+
if (chunk_log_mode) { return raw; }
|
| 16 |
return raw / {{ decay_at("bt", "h", "i") }};
|
| 17 |
{% else %}
|
| 18 |
return raw;
|
|
|
|
| 24 |
fn q_til(bt: u32, q_head: u32, {% if usesDecay %}h: u32, {% endif %}i: u32, scale: f32) -> f32 {
|
| 25 |
let raw = {{ read_scalar("query", "bt * params.qPackedDim + q_head * HEAD_DIM_K + i", queryDtype) }};
|
| 26 |
{% if usesDecay %}
|
| 27 |
+
if (chunk_log_mode) { return raw * scale; }
|
| 28 |
return raw * {{ decay_at("bt", "h", "i") }} * scale;
|
| 29 |
{% else %}
|
| 30 |
return raw * scale;
|
|
|
|
| 63 |
var<workgroup> ktile: array<f32, CHUNK * TK>;
|
| 64 |
var<workgroup> pmrow: array<f32, TOKEN_ROWS * CHUNK>;
|
| 65 |
|
| 66 |
+
fn chunk_finite(v: f32) -> bool {
|
| 67 |
+
return (bitcast<u32>(v) & 0x7f800000u) != 0x7f800000u;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
// A staged Q.K dot can overflow before multiplication by a tiny correction.
|
| 71 |
+
// On a nonfinite output only, contract K with that correction into one physical
|
| 72 |
+
// state component at a time, then project Q. No private state array is needed.
|
| 73 |
+
// Keep the original result if visible operands or the alternate contraction are
|
| 74 |
+
// nonfinite, preserving genuine NaN/infinity behavior instead of hiding it.
|
| 75 |
+
fn contract_state_output(
|
| 76 |
+
base_t: u32, local_t: u32, head: u32, q_head: u32,
|
| 77 |
+
state_base: u32, value_col: u32,{% if usesBeta %} chunk_base: u32,{% endif %} original: f32,
|
| 78 |
+
) -> f32 {
|
| 79 |
+
let end_t = base_t + local_t;
|
| 80 |
+
var total = 0.0;
|
| 81 |
+
for (var i = 0u; i < HEAD_DIM_K; i = i + 1u) {
|
| 82 |
+
let qraw = {{ read_scalar("query", "end_t * params.qPackedDim + q_head * HEAD_DIM_K + i", queryDtype) }};
|
| 83 |
+
var component = states[state_base + i * HEAD_DIM_V + value_col];
|
| 84 |
+
if (!chunk_finite(qraw) || !chunk_finite(component)) { return original; }
|
| 85 |
+
{% if usesDecay %}
|
| 86 |
+
let end_prefix = {{ decay_at("end_t", "head", "i") }};
|
| 87 |
+
if (!chunk_finite(end_prefix)) { return original; }
|
| 88 |
+
var entry_decay = end_prefix;
|
| 89 |
+
if (chunk_log_mode) { entry_decay = exp(end_prefix); }
|
| 90 |
+
if (!chunk_finite(entry_decay)) { return original; }
|
| 91 |
+
component = component * entry_decay;
|
| 92 |
+
{% endif %}
|
| 93 |
+
for (var s = 0u; s <= local_t; s = s + 1u) {
|
| 94 |
+
let bt = base_t + s;
|
| 95 |
+
let kraw = {{ read_scalar("key", "bt * params.kPackedDim + (head / KV_PER_KEY_HEAD) * HEAD_DIM_K + i", queryDtype) }};
|
| 96 |
+
{% if usesBeta %}
|
| 97 |
+
let correction = deltas[(chunk_base + s) * HEAD_DIM_V + value_col];
|
| 98 |
+
{% else %}
|
| 99 |
+
let correction = {{ read_scalar("value", "bt * params.vPackedDim + head * HEAD_DIM_V + value_col", queryDtype) }};
|
| 100 |
+
{% endif %}
|
| 101 |
+
if (!chunk_finite(kraw) || !chunk_finite(correction)) { return original; }
|
| 102 |
+
var contribution = kraw * correction;
|
| 103 |
+
{% if usesDecay %}
|
| 104 |
+
let source_prefix = {{ decay_at("bt", "head", "i") }};
|
| 105 |
+
if (!chunk_finite(source_prefix)) { return original; }
|
| 106 |
+
var ratio: f32;
|
| 107 |
+
if (chunk_log_mode) {
|
| 108 |
+
ratio = exp(end_prefix - source_prefix);
|
| 109 |
+
} else {
|
| 110 |
+
ratio = end_prefix / source_prefix;
|
| 111 |
+
}
|
| 112 |
+
if (!chunk_finite(ratio)) { return original; }
|
| 113 |
+
contribution = contribution * ratio;
|
| 114 |
+
{% endif %}
|
| 115 |
+
if (!chunk_finite(contribution)) { return original; }
|
| 116 |
+
component = component + contribution;
|
| 117 |
+
}
|
| 118 |
+
if (!chunk_finite(component)) { return original; }
|
| 119 |
+
total = total + qraw * component;
|
| 120 |
+
}
|
| 121 |
+
let result = total * out_scale();
|
| 122 |
+
if (chunk_finite(result)) { return result; }
|
| 123 |
+
return original;
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
@compute @workgroup_size(WG, 1, 1)
|
| 127 |
fn main(
|
| 128 |
@builtin(workgroup_id) wg: vec3<u32>,
|
|
|
|
| 140 |
}
|
| 141 |
let head = out_head / heads_per_group;
|
| 142 |
let q_head = (head * params.qNumHeads) / params.kvNumHeads + out_head % heads_per_group;
|
| 143 |
+
{% if usesDecay %}
|
| 144 |
+
let flag_base = params.batchSize * params.seqLength * params.decayPackedDim;
|
| 145 |
+
chunk_log_mode = gexp[flag_base + (batch * params.kvNumHeads + head) * num_chunks + chunk] != 0.0;
|
| 146 |
+
{% endif %}
|
| 147 |
let base_t = batch * params.seqLength + chunk * CHUNK;
|
| 148 |
let live = min(CHUNK, params.seqLength - chunk * CHUNK);
|
| 149 |
let scale = out_scale();
|
|
|
|
| 189 |
for (var e = tid; e < TOKEN_ROWS * CHUNK; e = e + WG) {
|
| 190 |
let m = e / CHUNK;
|
| 191 |
let s = e % CHUNK;
|
| 192 |
+
if (s <= tb + m && tb + m < live) {
|
| 193 |
+
{% if usesDecay and not decayPerElement %}
|
| 194 |
+
let head_ratio = select(1.0, exp({{ decay_at("base_t + tb + m", "head", "0u") }} - {{ decay_at("base_t + s", "head", "0u") }}), chunk_log_mode);
|
| 195 |
+
{% endif %}
|
| 196 |
var total = 0.0;
|
| 197 |
for (var u = 0u; u < TK; u = u + 1u) {
|
| 198 |
if (kb + u < HEAD_DIM_K) {
|
| 199 |
+
{% if usesDecay %}
|
| 200 |
+
if (chunk_log_mode) {
|
| 201 |
+
{% if decayPerElement %}
|
| 202 |
+
let i = kb + u;
|
| 203 |
+
let ratio = exp({{ decay_at("base_t + tb + m", "head", "i") }} - {{ decay_at("base_t + s", "head", "i") }});
|
| 204 |
+
total = total + (qblock[m * HEAD_DIM_K + i] * ratio) * ktile[s * TK + u];
|
| 205 |
+
{% else %}
|
| 206 |
+
total = total + (qblock[m * HEAD_DIM_K + kb + u] * head_ratio) * ktile[s * TK + u];
|
| 207 |
+
{% endif %}
|
| 208 |
+
} else {
|
| 209 |
+
{% endif %}
|
| 210 |
total = total + qblock[m * HEAD_DIM_K + kb + u] * ktile[s * TK + u];
|
| 211 |
+
{% if usesDecay %}
|
| 212 |
+
}
|
| 213 |
+
{% endif %}
|
| 214 |
}
|
| 215 |
}
|
| 216 |
pmrow[e] = pmrow[e] + total;
|
|
|
|
| 222 |
if (i < HEAD_DIM_K) {
|
| 223 |
let sv = states[state_base + i * HEAD_DIM_V + tid];
|
| 224 |
for (var m = 0u; m < TOKEN_ROWS; m = m + 1u) {
|
| 225 |
+
{% if usesDecay %}
|
| 226 |
+
if (chunk_log_mode) {
|
| 227 |
+
let entry_decay = exp({{ decay_at("base_t + tb + m", "head", "i") }});
|
| 228 |
+
acc[m] = acc[m] + qblock[m * HEAD_DIM_K + i] * entry_decay * sv;
|
| 229 |
+
} else {
|
| 230 |
+
{% endif %}
|
| 231 |
acc[m] = acc[m] + qblock[m * HEAD_DIM_K + i] * sv;
|
| 232 |
+
{% if usesDecay %}
|
| 233 |
+
}
|
| 234 |
+
{% endif %}
|
| 235 |
}
|
| 236 |
}
|
| 237 |
}
|
|
|
|
| 256 |
for (var m = 0u; m < TOKEN_ROWS; m = m + 1u) {
|
| 257 |
let t = tb + m;
|
| 258 |
if (t < live) {
|
| 259 |
+
var result = acc[m];
|
| 260 |
+
if (!chunk_finite(result)) {
|
| 261 |
+
result = contract_state_output(base_t, t, head, q_head, state_base, tid,{% if usesBeta %} chunk_base,{% endif %} result);
|
| 262 |
+
}
|
| 263 |
output[(base_t + t) * packed_out + out_head * HEAD_DIM_V + tid] =
|
| 264 |
+
{{ write_scalar("result", queryDtype) }};
|
| 265 |
}
|
| 266 |
}
|
| 267 |
}
|
build/webgpu/chunk-prep.wgsl.jinja
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
{% macro read_scalar(name, index, dtype) %}{% if dtype == "float16" %}f32({{ name }}[{{ index }}]){% else %}{{ name }}[{{ index }}]{% endif %}{% endmacro %}
|
| 2 |
{{ env.wgsl.resourceDeclarations }}
|
| 3 |
|
| 4 |
-
//
|
| 5 |
-
//
|
| 6 |
-
//
|
| 7 |
-
// exp(prefix) rather than the prefix itself keeps the per-element exponential out
|
| 8 |
-
// of the O(chunk^2) inner loops, where it would cost one transcendental per
|
| 9 |
-
// multiply-add instead of one per element.
|
| 10 |
const CHUNK: u32 = {{ chunkSize }}u;
|
|
|
|
|
|
|
|
|
|
| 11 |
const WG: u32 = {{ workgroupSize }}u;
|
| 12 |
|
| 13 |
@compute @workgroup_size(WG, 1, 1)
|
|
@@ -20,7 +20,8 @@ fn main(
|
|
| 20 |
let flat = wg.x + wg.y * {{ DISPATCH_FOLD_WIDTH }}u;
|
| 21 |
let num_chunks = (params.seqLength + CHUNK - 1u) / CHUNK;
|
| 22 |
let chunk = flat % num_chunks;
|
| 23 |
-
let
|
|
|
|
| 24 |
if (batch >= params.batchSize) {
|
| 25 |
return;
|
| 26 |
}
|
|
@@ -28,14 +29,45 @@ fn main(
|
|
| 28 |
let t1 = min(t0 + CHUNK, params.seqLength);
|
| 29 |
let packed = params.decayPackedDim;
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
var prefix = 0.0;
|
| 35 |
for (var t = t0; t < t1; t = t + 1u) {
|
| 36 |
let idx = (batch * params.seqLength + t) * packed + col;
|
| 37 |
prefix = prefix + {{ read_scalar("decay", "idx", queryDtype) }};
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
}
|
| 41 |
}
|
|
|
|
| 1 |
{% macro read_scalar(name, index, dtype) %}{% if dtype == "float16" %}f32({{ name }}[{{ index }}]){% else %}{{ name }}[{{ index }}]{% endif %}{% endmacro %}
|
| 2 |
{{ env.wgsl.resourceDeclarations }}
|
| 3 |
|
| 4 |
+
// Chunk prefixes share one tensor with a small per-head mode trailer. Prefixes
|
| 5 |
+
// outside the safe reciprocal range stay logarithmic; ordinary prefixes become
|
| 6 |
+
// exponentials so the common path uses plain multiply/divide.
|
|
|
|
|
|
|
|
|
|
| 7 |
const CHUNK: u32 = {{ chunkSize }}u;
|
| 8 |
+
const HEAD_DIM_K: u32 = {{ headDimK }}u;
|
| 9 |
+
const KV_PER_KEY_HEAD: u32 = {{ kvPerKeyHead }}u;
|
| 10 |
+
var<workgroup> unsafe_prefix: atomic<u32>;
|
| 11 |
const WG: u32 = {{ workgroupSize }}u;
|
| 12 |
|
| 13 |
@compute @workgroup_size(WG, 1, 1)
|
|
|
|
| 20 |
let flat = wg.x + wg.y * {{ DISPATCH_FOLD_WIDTH }}u;
|
| 21 |
let num_chunks = (params.seqLength + CHUNK - 1u) / CHUNK;
|
| 22 |
let chunk = flat % num_chunks;
|
| 23 |
+
let head = (flat / num_chunks) % params.kvNumHeads;
|
| 24 |
+
let batch = flat / (num_chunks * params.kvNumHeads);
|
| 25 |
if (batch >= params.batchSize) {
|
| 26 |
return;
|
| 27 |
}
|
|
|
|
| 29 |
let t1 = min(t0 + CHUNK, params.seqLength);
|
| 30 |
let packed = params.decayPackedDim;
|
| 31 |
|
| 32 |
+
let columns = {{ "HEAD_DIM_K" if decayPerElement else "1u" }};
|
| 33 |
+
let col0 = head * columns;
|
| 34 |
+
if (lid.x == 0u) { atomicStore(&unsafe_prefix, 0u); }
|
| 35 |
+
workgroupBarrier();
|
| 36 |
+
for (var local_col = lid.x; local_col < columns; local_col = local_col + WG) {
|
| 37 |
+
let col = col0 + local_col;
|
| 38 |
var prefix = 0.0;
|
| 39 |
for (var t = t0; t < t1; t = t + 1u) {
|
| 40 |
let idx = (batch * params.seqLength + t) * packed + col;
|
| 41 |
prefix = prefix + {{ read_scalar("decay", "idx", queryDtype) }};
|
| 42 |
+
if (prefix < -64.0 || prefix > 64.0) { atomicOr(&unsafe_prefix, 1u); }
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
// A conservative finite key bound also keeps reciprocal-prefix staging finite.
|
| 46 |
+
for (var e = lid.x; e < (t1 - t0) * HEAD_DIM_K; e = e + WG) {
|
| 47 |
+
let t = t0 + e / HEAD_DIM_K;
|
| 48 |
+
let i = e % HEAD_DIM_K;
|
| 49 |
+
let raw = {{ read_scalar("key", "(batch * params.seqLength + t) * params.kPackedDim + (head / KV_PER_KEY_HEAD) * HEAD_DIM_K + i", queryDtype) }};
|
| 50 |
+
if (abs(raw) > 4294967296.0) { atomicOr(&unsafe_prefix, 1u); }
|
| 51 |
+
}
|
| 52 |
+
workgroupBarrier();
|
| 53 |
+
let log_mode = atomicLoad(&unsafe_prefix);
|
| 54 |
+
if (lid.x == 0u) {
|
| 55 |
+
let flag_base = params.batchSize * params.seqLength * packed;
|
| 56 |
+
gexp[flag_base + (batch * params.kvNumHeads + head) * num_chunks + chunk] = f32(log_mode);
|
| 57 |
+
}
|
| 58 |
+
// Recompute the same prefix order after classification, then publish each
|
| 59 |
+
// element once. Safe heads need no intermediate prefix write and reread.
|
| 60 |
+
for (var local_col = lid.x; local_col < columns; local_col = local_col + WG) {
|
| 61 |
+
let col = col0 + local_col;
|
| 62 |
+
var prefix = 0.0;
|
| 63 |
+
for (var t = t0; t < t1; t = t + 1u) {
|
| 64 |
+
let idx = (batch * params.seqLength + t) * packed + col;
|
| 65 |
+
prefix = prefix + {{ read_scalar("decay", "idx", queryDtype) }};
|
| 66 |
+
if (log_mode == 0u) {
|
| 67 |
+
gexp[idx] = exp(prefix);
|
| 68 |
+
} else {
|
| 69 |
+
gexp[idx] = prefix;
|
| 70 |
+
}
|
| 71 |
}
|
| 72 |
}
|
| 73 |
}
|
build/webgpu/chunk-scan.wgsl.jinja
CHANGED
|
@@ -2,9 +2,13 @@
|
|
| 2 |
{% macro write_scalar(expr, dtype) %}{% if dtype == "float16" %}f16({{ expr }}){% else %}{{ expr }}{% endif %}{% endmacro %}
|
| 3 |
{% macro decay_at(bt, h, i) %}{% if decayPerElement %}gexp[({{ bt }}) * params.decayPackedDim + ({{ h }}) * HEAD_DIM_K + ({{ i }})]{% else %}gexp[({{ bt }}) * params.decayPackedDim + ({{ h }})]{% endif %}{% endmacro %}
|
| 4 |
{% macro emit_chunk_operands(needKtil=false, needQuery=false) %}
|
|
|
|
|
|
|
|
|
|
| 5 |
fn k_hat(bt: u32, h: u32, i: u32) -> f32 {
|
| 6 |
let raw = {{ read_scalar("key", "bt * params.kPackedDim + (h / KV_PER_KEY_HEAD) * HEAD_DIM_K + i", queryDtype) }};
|
| 7 |
{% if usesDecay %}
|
|
|
|
| 8 |
return raw / {{ decay_at("bt", "h", "i") }};
|
| 9 |
{% else %}
|
| 10 |
return raw;
|
|
@@ -75,6 +79,10 @@ fn main(
|
|
| 75 |
workgroupBarrier();
|
| 76 |
|
| 77 |
for (var chunk = 0u; chunk < num_chunks; chunk = chunk + 1u) {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
let base_t = batch * params.seqLength + chunk * CHUNK;
|
| 79 |
let live = min(CHUNK, params.seqLength - chunk * CHUNK);
|
| 80 |
{% if usesBeta %}
|
|
@@ -129,6 +137,14 @@ fn main(
|
|
| 129 |
var acc: array<f32, ROWS_PER_GROUP>;
|
| 130 |
for (var m = 0u; m < ROWS_PER_GROUP; m = m + 1u) {
|
| 131 |
acc[m] = st[(group * ROWS_PER_GROUP + m) * TILE_V + col];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
}
|
| 133 |
for (var tb = 0u; tb < CHUNK; tb = tb + TOKEN_TILE) {
|
| 134 |
for (var e = tid; e < TOKEN_TILE * HEAD_DIM_K; e = e + WG) {
|
|
@@ -136,6 +152,14 @@ fn main(
|
|
| 136 |
var v = 0.0;
|
| 137 |
if (t < live) {
|
| 138 |
v = k_hat(base_t + t, head, e % HEAD_DIM_K);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
}
|
| 140 |
stage[e] = v;
|
| 141 |
}
|
|
@@ -154,7 +178,9 @@ fn main(
|
|
| 154 |
for (var m = 0u; m < ROWS_PER_GROUP; m = m + 1u) {
|
| 155 |
let i = group * ROWS_PER_GROUP + m;
|
| 156 |
{% if usesDecay %}
|
| 157 |
-
|
|
|
|
|
|
|
| 158 |
{% else %}
|
| 159 |
st[i * TILE_V + col] = acc[m];
|
| 160 |
{% endif %}
|
|
|
|
| 2 |
{% macro write_scalar(expr, dtype) %}{% if dtype == "float16" %}f16({{ expr }}){% else %}{{ expr }}{% endif %}{% endmacro %}
|
| 3 |
{% macro decay_at(bt, h, i) %}{% if decayPerElement %}gexp[({{ bt }}) * params.decayPackedDim + ({{ h }}) * HEAD_DIM_K + ({{ i }})]{% else %}gexp[({{ bt }}) * params.decayPackedDim + ({{ h }})]{% endif %}{% endmacro %}
|
| 4 |
{% macro emit_chunk_operands(needKtil=false, needQuery=false) %}
|
| 5 |
+
{% if usesDecay %}
|
| 6 |
+
var<private> chunk_log_mode: bool;
|
| 7 |
+
{% endif %}
|
| 8 |
fn k_hat(bt: u32, h: u32, i: u32) -> f32 {
|
| 9 |
let raw = {{ read_scalar("key", "bt * params.kPackedDim + (h / KV_PER_KEY_HEAD) * HEAD_DIM_K + i", queryDtype) }};
|
| 10 |
{% if usesDecay %}
|
| 11 |
+
if (chunk_log_mode) { return raw; }
|
| 12 |
return raw / {{ decay_at("bt", "h", "i") }};
|
| 13 |
{% else %}
|
| 14 |
return raw;
|
|
|
|
| 79 |
workgroupBarrier();
|
| 80 |
|
| 81 |
for (var chunk = 0u; chunk < num_chunks; chunk = chunk + 1u) {
|
| 82 |
+
{% if usesDecay %}
|
| 83 |
+
let flag_base = params.batchSize * params.seqLength * params.decayPackedDim;
|
| 84 |
+
chunk_log_mode = gexp[flag_base + (batch * params.kvNumHeads + head) * num_chunks + chunk] != 0.0;
|
| 85 |
+
{% endif %}
|
| 86 |
let base_t = batch * params.seqLength + chunk * CHUNK;
|
| 87 |
let live = min(CHUNK, params.seqLength - chunk * CHUNK);
|
| 88 |
{% if usesBeta %}
|
|
|
|
| 137 |
var acc: array<f32, ROWS_PER_GROUP>;
|
| 138 |
for (var m = 0u; m < ROWS_PER_GROUP; m = m + 1u) {
|
| 139 |
acc[m] = st[(group * ROWS_PER_GROUP + m) * TILE_V + col];
|
| 140 |
+
{% if usesDecay %}
|
| 141 |
+
if (chunk_log_mode) {
|
| 142 |
+
{% if decayPerElement %}
|
| 143 |
+
let i = group * ROWS_PER_GROUP + m;
|
| 144 |
+
{% endif %}
|
| 145 |
+
acc[m] = acc[m] * exp({{ decay_at("base_t + live - 1u", "head", "i") }});
|
| 146 |
+
}
|
| 147 |
+
{% endif %}
|
| 148 |
}
|
| 149 |
for (var tb = 0u; tb < CHUNK; tb = tb + TOKEN_TILE) {
|
| 150 |
for (var e = tid; e < TOKEN_TILE * HEAD_DIM_K; e = e + WG) {
|
|
|
|
| 152 |
var v = 0.0;
|
| 153 |
if (t < live) {
|
| 154 |
v = k_hat(base_t + t, head, e % HEAD_DIM_K);
|
| 155 |
+
{% if usesDecay %}
|
| 156 |
+
if (chunk_log_mode) {
|
| 157 |
+
{% if decayPerElement %}
|
| 158 |
+
let i = e % HEAD_DIM_K;
|
| 159 |
+
{% endif %}
|
| 160 |
+
v = v * exp({{ decay_at("base_t + live - 1u", "head", "i") }} - {{ decay_at("base_t + t", "head", "i") }});
|
| 161 |
+
}
|
| 162 |
+
{% endif %}
|
| 163 |
}
|
| 164 |
stage[e] = v;
|
| 165 |
}
|
|
|
|
| 178 |
for (var m = 0u; m < ROWS_PER_GROUP; m = m + 1u) {
|
| 179 |
let i = group * ROWS_PER_GROUP + m;
|
| 180 |
{% if usesDecay %}
|
| 181 |
+
var result = acc[m];
|
| 182 |
+
if (!chunk_log_mode) { result = result * {{ decay_at("last_t", "head", "i") }}; }
|
| 183 |
+
st[i * TILE_V + col] = result;
|
| 184 |
{% else %}
|
| 185 |
st[i * TILE_V + col] = acc[m];
|
| 186 |
{% endif %}
|
build/webgpu/chunk-ut.wgsl.jinja
CHANGED
|
@@ -1,9 +1,13 @@
|
|
| 1 |
{% macro read_scalar(name, index, dtype) %}{% if dtype == "float16" %}f32({{ name }}[{{ index }}]){% else %}{{ name }}[{{ index }}]{% endif %}{% endmacro %}
|
| 2 |
{% macro decay_at(bt, h, i) %}{% if decayPerElement %}gexp[({{ bt }}) * params.decayPackedDim + ({{ h }}) * HEAD_DIM_K + ({{ i }})]{% else %}gexp[({{ bt }}) * params.decayPackedDim + ({{ h }})]{% endif %}{% endmacro %}
|
| 3 |
{% macro emit_chunk_operands(needKtil=false, needQuery=false) %}
|
|
|
|
|
|
|
|
|
|
| 4 |
fn k_hat(bt: u32, h: u32, i: u32) -> f32 {
|
| 5 |
let raw = {{ read_scalar("key", "bt * params.kPackedDim + (h / KV_PER_KEY_HEAD) * HEAD_DIM_K + i", queryDtype) }};
|
| 6 |
{% if usesDecay %}
|
|
|
|
| 7 |
return raw / {{ decay_at("bt", "h", "i") }};
|
| 8 |
{% else %}
|
| 9 |
return raw;
|
|
@@ -13,6 +17,7 @@ fn k_hat(bt: u32, h: u32, i: u32) -> f32 {
|
|
| 13 |
fn k_til(bt: u32, h: u32, i: u32) -> f32 {
|
| 14 |
let raw = {{ read_scalar("key", "bt * params.kPackedDim + (h / KV_PER_KEY_HEAD) * HEAD_DIM_K + i", queryDtype) }};
|
| 15 |
{% if usesDecay %}
|
|
|
|
| 16 |
return raw * {{ decay_at("bt", "h", "i") }};
|
| 17 |
{% else %}
|
| 18 |
return raw;
|
|
@@ -67,6 +72,10 @@ fn main(
|
|
| 67 |
if (batch >= params.batchSize) {
|
| 68 |
return;
|
| 69 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
let base_t = batch * params.seqLength + chunk * CHUNK;
|
| 71 |
// Tokens past the sequence end carry beta 0, which makes their rows of the system
|
| 72 |
// the identity and their outputs zero, so the tail chunk needs no separate arm.
|
|
@@ -96,7 +105,15 @@ fn main(
|
|
| 96 |
var til = 0.0;
|
| 97 |
if (t < live && i < HEAD_DIM_K) {
|
| 98 |
hat = k_hat(base_t + t, head, i);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
til = k_til(base_t + t, head, i);
|
|
|
|
|
|
|
|
|
|
| 100 |
}
|
| 101 |
tile[e] = hat;
|
| 102 |
tile_til[e] = til;
|
|
@@ -106,10 +123,26 @@ fn main(
|
|
| 106 |
let e = tid + m * WG;
|
| 107 |
let t = e / CHUNK;
|
| 108 |
let s = e % CHUNK;
|
| 109 |
-
if (s < t) {
|
|
|
|
|
|
|
|
|
|
| 110 |
var total = 0.0;
|
| 111 |
for (var u = 0u; u < TK; u = u + 1u) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
total = total + tile_til[t * TK + u] * tile[s * TK + u];
|
|
|
|
|
|
|
|
|
|
| 113 |
}
|
| 114 |
acc[m] = acc[m] + total;
|
| 115 |
}
|
|
|
|
| 1 |
{% macro read_scalar(name, index, dtype) %}{% if dtype == "float16" %}f32({{ name }}[{{ index }}]){% else %}{{ name }}[{{ index }}]{% endif %}{% endmacro %}
|
| 2 |
{% macro decay_at(bt, h, i) %}{% if decayPerElement %}gexp[({{ bt }}) * params.decayPackedDim + ({{ h }}) * HEAD_DIM_K + ({{ i }})]{% else %}gexp[({{ bt }}) * params.decayPackedDim + ({{ h }})]{% endif %}{% endmacro %}
|
| 3 |
{% macro emit_chunk_operands(needKtil=false, needQuery=false) %}
|
| 4 |
+
{% if usesDecay %}
|
| 5 |
+
var<private> chunk_log_mode: bool;
|
| 6 |
+
{% endif %}
|
| 7 |
fn k_hat(bt: u32, h: u32, i: u32) -> f32 {
|
| 8 |
let raw = {{ read_scalar("key", "bt * params.kPackedDim + (h / KV_PER_KEY_HEAD) * HEAD_DIM_K + i", queryDtype) }};
|
| 9 |
{% if usesDecay %}
|
| 10 |
+
if (chunk_log_mode) { return raw; }
|
| 11 |
return raw / {{ decay_at("bt", "h", "i") }};
|
| 12 |
{% else %}
|
| 13 |
return raw;
|
|
|
|
| 17 |
fn k_til(bt: u32, h: u32, i: u32) -> f32 {
|
| 18 |
let raw = {{ read_scalar("key", "bt * params.kPackedDim + (h / KV_PER_KEY_HEAD) * HEAD_DIM_K + i", queryDtype) }};
|
| 19 |
{% if usesDecay %}
|
| 20 |
+
if (chunk_log_mode) { return raw * exp({{ decay_at("bt", "h", "i") }}); }
|
| 21 |
return raw * {{ decay_at("bt", "h", "i") }};
|
| 22 |
{% else %}
|
| 23 |
return raw;
|
|
|
|
| 72 |
if (batch >= params.batchSize) {
|
| 73 |
return;
|
| 74 |
}
|
| 75 |
+
{% if usesDecay %}
|
| 76 |
+
let flag_base = params.batchSize * params.seqLength * params.decayPackedDim;
|
| 77 |
+
chunk_log_mode = gexp[flag_base + (batch * params.kvNumHeads + head) * num_chunks + chunk] != 0.0;
|
| 78 |
+
{% endif %}
|
| 79 |
let base_t = batch * params.seqLength + chunk * CHUNK;
|
| 80 |
// Tokens past the sequence end carry beta 0, which makes their rows of the system
|
| 81 |
// the identity and their outputs zero, so the tail chunk needs no separate arm.
|
|
|
|
| 105 |
var til = 0.0;
|
| 106 |
if (t < live && i < HEAD_DIM_K) {
|
| 107 |
hat = k_hat(base_t + t, head, i);
|
| 108 |
+
{% if usesDecay %}
|
| 109 |
+
if (chunk_log_mode) {
|
| 110 |
+
til = {{ decay_at("base_t + t", "head", "i") }};
|
| 111 |
+
} else {
|
| 112 |
+
{% endif %}
|
| 113 |
til = k_til(base_t + t, head, i);
|
| 114 |
+
{% if usesDecay %}
|
| 115 |
+
}
|
| 116 |
+
{% endif %}
|
| 117 |
}
|
| 118 |
tile[e] = hat;
|
| 119 |
tile_til[e] = til;
|
|
|
|
| 123 |
let e = tid + m * WG;
|
| 124 |
let t = e / CHUNK;
|
| 125 |
let s = e % CHUNK;
|
| 126 |
+
if (s < t && t < live) {
|
| 127 |
+
{% if usesDecay and not decayPerElement %}
|
| 128 |
+
let head_ratio = select(1.0, exp(tile_til[t * TK] - tile_til[s * TK]), chunk_log_mode);
|
| 129 |
+
{% endif %}
|
| 130 |
var total = 0.0;
|
| 131 |
for (var u = 0u; u < TK; u = u + 1u) {
|
| 132 |
+
{% if usesDecay %}
|
| 133 |
+
if (chunk_log_mode) {
|
| 134 |
+
{% if decayPerElement %}
|
| 135 |
+
let ratio = exp(tile_til[t * TK + u] - tile_til[s * TK + u]);
|
| 136 |
+
total = total + (tile[t * TK + u] * ratio) * tile[s * TK + u];
|
| 137 |
+
{% else %}
|
| 138 |
+
total = total + (tile[t * TK + u] * head_ratio) * tile[s * TK + u];
|
| 139 |
+
{% endif %}
|
| 140 |
+
} else {
|
| 141 |
+
{% endif %}
|
| 142 |
total = total + tile_til[t * TK + u] * tile[s * TK + u];
|
| 143 |
+
{% if usesDecay %}
|
| 144 |
+
}
|
| 145 |
+
{% endif %}
|
| 146 |
}
|
| 147 |
acc[m] = acc[m] + total;
|
| 148 |
}
|
build/webgpu/linear-attention.scalar.wgsl.jinja
CHANGED
|
@@ -1,28 +1,29 @@
|
|
|
|
|
| 1 |
{% macro read_scalar(name, index, dtype) %}{% if dtype == "float16" %}f32({{ name }}[{{ index }}]){% else %}{{ name }}[{{ index }}]{% endif %}{% endmacro %}
|
| 2 |
{% macro write_scalar(expr, dtype) %}{% if dtype == "float16" %}f16({{ expr }}){% else %}{{ expr }}{% endif %}{% endmacro %}
|
| 3 |
{% macro emit_tiled_setup(dvGroups=1) %}
|
| 4 |
-
let head_dim_k =
|
| 5 |
-
let head_dim_v =
|
| 6 |
-
let n_key_heads =
|
| 7 |
-
let heads_per_group = max(1u,
|
| 8 |
-
let kv_per_key_head =
|
| 9 |
let dv_tiles = (head_dim_v + TILE_V - 1u) / TILE_V;
|
| 10 |
let scale = select(inverseSqrt(f32(head_dim_k)), params.scale, params.scale != 0.0);
|
| 11 |
|
| 12 |
// 2D-folded flat (batch*head*dv_tile) index: wg.y carries the high bits past
|
| 13 |
// the per-axis dispatch fold width. Reduces to wg.x with no fold; the batch_idx >=
|
| 14 |
-
//
|
| 15 |
let workgroup_idx = wg.x + wg.y * {{ DISPATCH_FOLD_WIDTH }}u;
|
| 16 |
let dv_tile_idx = workgroup_idx % dv_tiles;
|
| 17 |
let bh = workgroup_idx / dv_tiles;
|
| 18 |
-
let head_idx = bh %
|
| 19 |
-
let batch_idx = bh /
|
| 20 |
-
if (batch_idx >=
|
| 21 |
return;
|
| 22 |
}
|
| 23 |
|
| 24 |
let dv_start = dv_tile_idx * TILE_V;
|
| 25 |
-
let packed_out = max(
|
| 26 |
let key_head_idx = head_idx / kv_per_key_head;{% endmacro %}
|
| 27 |
{% macro emit_query_groups(first_group) %}
|
| 28 |
for (var qg = {{ first_group }}u; qg < heads_per_group; qg = qg + 1u) {
|
|
@@ -37,16 +38,16 @@
|
|
| 37 |
q_val = {{ read_scalar("query", "q_idx", queryDtype) }};
|
| 38 |
}
|
| 39 |
{% if useSubgroups %}
|
| 40 |
-
let qg_subgroup_index =
|
| 41 |
-
let qg_subgroup_count =
|
| 42 |
for (var j = 0u; j < TILE_V; j = j + 1u) {
|
| 43 |
let sg_pre = subgroupAdd(state[j] * q_val);
|
| 44 |
-
if (
|
| 45 |
red_preout[j * WG + qg_subgroup_index] = sg_pre;
|
| 46 |
}
|
| 47 |
}
|
| 48 |
workgroupBarrier();
|
| 49 |
-
|
| 50 |
if (tid < TILE_V) {
|
| 51 |
var pre_total = 0.0;
|
| 52 |
for (var i = 1u; i < qg_subgroup_count; i = i + 1u) {
|
|
@@ -95,6 +96,40 @@
|
|
| 95 |
enable subgroups;
|
| 96 |
{% endif %}
|
| 97 |
{{ env.wgsl.resourceDeclarations }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
const WG: u32 = {{ workgroupSize }}u;
|
| 100 |
const TILE_V: u32 = {{ tileV }}u;
|
|
@@ -107,16 +142,26 @@ var<workgroup> red_preout: array<f32, WG * TILE_V>;
|
|
| 107 |
var<workgroup> red_kq: array<f32, WG>;
|
| 108 |
var<workgroup> broadcast_delta: array<f32, TILE_V>;
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
{% endif %}
|
| 111 |
@compute @workgroup_size(WG, 1, 1)
|
| 112 |
fn main(
|
| 113 |
@builtin(workgroup_id) wg: vec3<u32>,
|
| 114 |
@builtin(local_invocation_id) lid: vec3<u32>{% if useSubgroups %},
|
| 115 |
-
@builtin(subgroup_invocation_id) sg_lid: u32
|
| 116 |
-
@builtin(subgroup_size) sg_size: u32{% endif %}
|
| 117 |
) {
|
| 118 |
let tid = lid.x;
|
| 119 |
{{ emit_tiled_setup() }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
var state: array<f32, TILE_V>;
|
| 122 |
for (var j = 0u; j < TILE_V; j = j + 1u) {
|
|
@@ -183,21 +228,21 @@ fn main(
|
|
| 183 |
q0_val = {{ read_scalar("query", "q0_idx", queryDtype) }};
|
| 184 |
}
|
| 185 |
{% if useSubgroups %}
|
| 186 |
-
let subgroup_index_b =
|
| 187 |
-
let subgroup_count_b =
|
| 188 |
for (var j = 0u; j < TILE_V; j = j + 1u) {
|
| 189 |
let sg_rp = subgroupAdd(vec2<f32>(state[j] * k_val, state[j] * q0_val));
|
| 190 |
-
if (
|
| 191 |
red_retrieved[j * WG + subgroup_index_b] = sg_rp.x;
|
| 192 |
red_preout[j * WG + subgroup_index_b] = sg_rp.y;
|
| 193 |
}
|
| 194 |
}
|
| 195 |
let sg_kq = subgroupAdd(k_val * q0_val);
|
| 196 |
-
if (
|
| 197 |
red_kq[subgroup_index_b] = sg_kq;
|
| 198 |
}
|
| 199 |
workgroupBarrier();
|
| 200 |
-
|
| 201 |
if (tid < TILE_V) {
|
| 202 |
var ret_total = 0.0;
|
| 203 |
var pre_total = 0.0;
|
|
@@ -256,22 +301,51 @@ fn main(
|
|
| 256 |
let v_base = bt * params.vPackedDim + head_idx * head_dim_v + dv_start;
|
| 257 |
let out_base = bt * packed_out + out_head_0 * head_dim_v + dv_start;
|
| 258 |
let kq_dot = red_kq[0];
|
|
|
|
| 259 |
for (var j = 0u; j < TILE_V; j = j + 1u) {
|
| 260 |
if (dv_start + j < head_dim_v) {
|
| 261 |
let v_val = {{ read_scalar("value", "v_base + j", valueDtype) }};
|
| 262 |
let delta_j = beta_val * (v_val - red_retrieved[j * WG]);
|
| 263 |
broadcast_delta[j] = delta_j;
|
| 264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
} else {
|
| 266 |
broadcast_delta[j] = 0.0;
|
| 267 |
}
|
| 268 |
}
|
|
|
|
| 269 |
}
|
| 270 |
-
|
| 271 |
|
| 272 |
for (var j = 0u; j < TILE_V; j = j + 1u) {
|
| 273 |
state[j] = state[j] + k_val * broadcast_delta[j];
|
| 274 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
{{ emit_query_groups(1) }}
|
| 277 |
{% else %}
|
|
|
|
| 1 |
+
{% set geometryPrefix = "params." %}
|
| 2 |
{% macro read_scalar(name, index, dtype) %}{% if dtype == "float16" %}f32({{ name }}[{{ index }}]){% else %}{{ name }}[{{ index }}]{% endif %}{% endmacro %}
|
| 3 |
{% macro write_scalar(expr, dtype) %}{% if dtype == "float16" %}f16({{ expr }}){% else %}{{ expr }}{% endif %}{% endmacro %}
|
| 4 |
{% macro emit_tiled_setup(dvGroups=1) %}
|
| 5 |
+
let head_dim_k = {{ geometryPrefix }}qPackedDim / {{ geometryPrefix }}qNumHeads;
|
| 6 |
+
let head_dim_v = {{ geometryPrefix }}vPackedDim / {{ geometryPrefix }}kvNumHeads;
|
| 7 |
+
let n_key_heads = {{ geometryPrefix }}kPackedDim / head_dim_k;
|
| 8 |
+
let heads_per_group = max(1u, {{ geometryPrefix }}qNumHeads / {{ geometryPrefix }}kvNumHeads);
|
| 9 |
+
let kv_per_key_head = {{ geometryPrefix }}kvNumHeads / n_key_heads;
|
| 10 |
let dv_tiles = (head_dim_v + TILE_V - 1u) / TILE_V;
|
| 11 |
let scale = select(inverseSqrt(f32(head_dim_k)), params.scale, params.scale != 0.0);
|
| 12 |
|
| 13 |
// 2D-folded flat (batch*head*dv_tile) index: wg.y carries the high bits past
|
| 14 |
// the per-axis dispatch fold width. Reduces to wg.x with no fold; the batch_idx >=
|
| 15 |
+
// {{ geometryPrefix }}batchSize guard drops the over-dispatched tail.
|
| 16 |
let workgroup_idx = wg.x + wg.y * {{ DISPATCH_FOLD_WIDTH }}u;
|
| 17 |
let dv_tile_idx = workgroup_idx % dv_tiles;
|
| 18 |
let bh = workgroup_idx / dv_tiles;
|
| 19 |
+
let head_idx = bh % {{ geometryPrefix }}kvNumHeads;
|
| 20 |
+
let batch_idx = bh / {{ geometryPrefix }}kvNumHeads;
|
| 21 |
+
if (batch_idx >= {{ geometryPrefix }}batchSize) {
|
| 22 |
return;
|
| 23 |
}
|
| 24 |
|
| 25 |
let dv_start = dv_tile_idx * TILE_V;
|
| 26 |
+
let packed_out = max({{ geometryPrefix }}qNumHeads, {{ geometryPrefix }}kvNumHeads) * head_dim_v;
|
| 27 |
let key_head_idx = head_idx / kv_per_key_head;{% endmacro %}
|
| 28 |
{% macro emit_query_groups(first_group) %}
|
| 29 |
for (var qg = {{ first_group }}u; qg < heads_per_group; qg = qg + 1u) {
|
|
|
|
| 38 |
q_val = {{ read_scalar("query", "q_idx", queryDtype) }};
|
| 39 |
}
|
| 40 |
{% if useSubgroups %}
|
| 41 |
+
let qg_subgroup_index = sg_group.ord;
|
| 42 |
+
let qg_subgroup_count = sg_group_count;
|
| 43 |
for (var j = 0u; j < TILE_V; j = j + 1u) {
|
| 44 |
let sg_pre = subgroupAdd(state[j] * q_val);
|
| 45 |
+
if (sg_group.rank == 0u) {
|
| 46 |
red_preout[j * WG + qg_subgroup_index] = sg_pre;
|
| 47 |
}
|
| 48 |
}
|
| 49 |
workgroupBarrier();
|
| 50 |
+
{
|
| 51 |
if (tid < TILE_V) {
|
| 52 |
var pre_total = 0.0;
|
| 53 |
for (var i = 1u; i < qg_subgroup_count; i = i + 1u) {
|
|
|
|
| 96 |
enable subgroups;
|
| 97 |
{% endif %}
|
| 98 |
{{ env.wgsl.resourceDeclarations }}
|
| 99 |
+
{% if useSubgroups %}
|
| 100 |
+
var<workgroup> sgLaneClaims: atomic<u32>;
|
| 101 |
+
|
| 102 |
+
struct SubgroupLogicalLanes {
|
| 103 |
+
ltid: u32,
|
| 104 |
+
ord: u32,
|
| 105 |
+
rank: u32,
|
| 106 |
+
count: u32,
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
fn subgroup_logical_lanes(lane: u32) -> SubgroupLogicalLanes {
|
| 110 |
+
{% macro active_lanes(lane, prefix="active") %}
|
| 111 |
+
let {{ prefix }}Mask = subgroupBallot(true);
|
| 112 |
+
let {{ prefix }}Word = {{ lane }} / 32u;
|
| 113 |
+
let {{ prefix }}Lower = (1u << ({{ lane }} % 32u)) - 1u;
|
| 114 |
+
let {{ prefix }}Count = max(countOneBits({{ prefix }}Mask.x) + countOneBits({{ prefix }}Mask.y)
|
| 115 |
+
+ countOneBits({{ prefix }}Mask.z) + countOneBits({{ prefix }}Mask.w), 1u);
|
| 116 |
+
var {{ prefix }}Rank = countOneBits({{ prefix }}Mask[{{ prefix }}Word] & {{ prefix }}Lower);
|
| 117 |
+
for (var word = 0u; word < {{ prefix }}Word; word++) {
|
| 118 |
+
{{ prefix }}Rank += countOneBits({{ prefix }}Mask[word]);
|
| 119 |
+
}
|
| 120 |
+
{% endmacro %}
|
| 121 |
+
{{ active_lanes("lane") }}
|
| 122 |
+
// Dense rank zero is the first ACTIVE lane, exactly the BroadcastFirst source.
|
| 123 |
+
// All lanes execute the atomic and collective in uniform control flow.
|
| 124 |
+
let ticket = atomicAdd(&sgLaneClaims, select(0u, activeCount | (1u << 16u), activeRank == 0u));
|
| 125 |
+
let claim = subgroupBroadcastFirst(ticket);
|
| 126 |
+
return SubgroupLogicalLanes((claim & 0xffffu) + activeRank, claim >> 16u, activeRank, activeCount);
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
fn subgroup_logical_count() -> u32 {
|
| 130 |
+
return atomicLoad(&sgLaneClaims) >> 16u;
|
| 131 |
+
}
|
| 132 |
+
{% endif %}
|
| 133 |
|
| 134 |
const WG: u32 = {{ workgroupSize }}u;
|
| 135 |
const TILE_V: u32 = {{ tileV }}u;
|
|
|
|
| 142 |
var<workgroup> red_kq: array<f32, WG>;
|
| 143 |
var<workgroup> broadcast_delta: array<f32, TILE_V>;
|
| 144 |
|
| 145 |
+
// Reassociation is attempted only for a nonfinite cached projection.
|
| 146 |
+
fn linear_attention_finite(value: f32) -> bool {
|
| 147 |
+
return (bitcast<u32>(value) & 0x7f800000u) != 0x7f800000u;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
|
| 151 |
{% endif %}
|
| 152 |
@compute @workgroup_size(WG, 1, 1)
|
| 153 |
fn main(
|
| 154 |
@builtin(workgroup_id) wg: vec3<u32>,
|
| 155 |
@builtin(local_invocation_id) lid: vec3<u32>{% if useSubgroups %},
|
| 156 |
+
@builtin(subgroup_invocation_id) sg_lid: u32{% endif %}
|
|
|
|
| 157 |
) {
|
| 158 |
let tid = lid.x;
|
| 159 |
{{ emit_tiled_setup() }}
|
| 160 |
+
{% if useSubgroups %}
|
| 161 |
+
let sg_group = subgroup_logical_lanes(sg_lid);
|
| 162 |
+
workgroupBarrier();
|
| 163 |
+
let sg_group_count = subgroup_logical_count();
|
| 164 |
+
{% endif %}
|
| 165 |
|
| 166 |
var state: array<f32, TILE_V>;
|
| 167 |
for (var j = 0u; j < TILE_V; j = j + 1u) {
|
|
|
|
| 228 |
q0_val = {{ read_scalar("query", "q0_idx", queryDtype) }};
|
| 229 |
}
|
| 230 |
{% if useSubgroups %}
|
| 231 |
+
let subgroup_index_b = sg_group.ord;
|
| 232 |
+
let subgroup_count_b = sg_group_count;
|
| 233 |
for (var j = 0u; j < TILE_V; j = j + 1u) {
|
| 234 |
let sg_rp = subgroupAdd(vec2<f32>(state[j] * k_val, state[j] * q0_val));
|
| 235 |
+
if (sg_group.rank == 0u) {
|
| 236 |
red_retrieved[j * WG + subgroup_index_b] = sg_rp.x;
|
| 237 |
red_preout[j * WG + subgroup_index_b] = sg_rp.y;
|
| 238 |
}
|
| 239 |
}
|
| 240 |
let sg_kq = subgroupAdd(k_val * q0_val);
|
| 241 |
+
if (sg_group.rank == 0u) {
|
| 242 |
red_kq[subgroup_index_b] = sg_kq;
|
| 243 |
}
|
| 244 |
workgroupBarrier();
|
| 245 |
+
{
|
| 246 |
if (tid < TILE_V) {
|
| 247 |
var ret_total = 0.0;
|
| 248 |
var pre_total = 0.0;
|
|
|
|
| 301 |
let v_base = bt * params.vPackedDim + head_idx * head_dim_v + dv_start;
|
| 302 |
let out_base = bt * packed_out + out_head_0 * head_dim_v + dv_start;
|
| 303 |
let kq_dot = red_kq[0];
|
| 304 |
+
var repair_first = false;
|
| 305 |
for (var j = 0u; j < TILE_V; j = j + 1u) {
|
| 306 |
if (dv_start + j < head_dim_v) {
|
| 307 |
let v_val = {{ read_scalar("value", "v_base + j", valueDtype) }};
|
| 308 |
let delta_j = beta_val * (v_val - red_retrieved[j * WG]);
|
| 309 |
broadcast_delta[j] = delta_j;
|
| 310 |
+
let result = (red_preout[j * WG] + delta_j * kq_dot) * scale;
|
| 311 |
+
output[out_base + j] = {{ write_scalar("result", outputDtype) }};
|
| 312 |
+
// Retrieved values are dead after delta formation; retain the cached
|
| 313 |
+
// outputs here without adding storage or another rendezvous.
|
| 314 |
+
red_retrieved[j * WG] = result;
|
| 315 |
+
repair_first = repair_first || !linear_attention_finite(result);
|
| 316 |
} else {
|
| 317 |
broadcast_delta[j] = 0.0;
|
| 318 |
}
|
| 319 |
}
|
| 320 |
+
red_kq[0] = select(0.0, 1.0, repair_first);
|
| 321 |
}
|
| 322 |
+
let repair_first = workgroupUniformLoad(&red_kq[0]) != 0.0;
|
| 323 |
|
| 324 |
for (var j = 0u; j < TILE_V; j = j + 1u) {
|
| 325 |
state[j] = state[j] + k_val * broadcast_delta[j];
|
| 326 |
}
|
| 327 |
+
if (repair_first) {
|
| 328 |
+
// Contract the physical updated state before multiplying a large query.
|
| 329 |
+
// The fast k.q identity can overflow while this equivalent result is finite.
|
| 330 |
+
for (var j = 0u; j < TILE_V; j = j + 1u) {
|
| 331 |
+
red_preout[j * WG + tid] = state[j] * q0_val;
|
| 332 |
+
}
|
| 333 |
+
workgroupBarrier();
|
| 334 |
+
if (tid == 0u) {
|
| 335 |
+
let out_base = bt * packed_out + out_head_0 * head_dim_v + dv_start;
|
| 336 |
+
for (var j = 0u; j < TILE_V; j = j + 1u) {
|
| 337 |
+
var repaired = red_preout[j * WG];
|
| 338 |
+
for (var i = 1u; i < WG; i = i + 1u) {
|
| 339 |
+
repaired = repaired + red_preout[j * WG + i];
|
| 340 |
+
}
|
| 341 |
+
repaired = repaired * scale;
|
| 342 |
+
if (dv_start + j < head_dim_v && !linear_attention_finite(red_retrieved[j * WG]) && linear_attention_finite(repaired)) {
|
| 343 |
+
output[out_base + j] = {{ write_scalar("repaired", outputDtype) }};
|
| 344 |
+
}
|
| 345 |
+
}
|
| 346 |
+
}
|
| 347 |
+
workgroupBarrier();
|
| 348 |
+
}
|
| 349 |
|
| 350 |
{{ emit_query_groups(1) }}
|
| 351 |
{% else %}
|
build/webgpu/linear-attention.serial.wgsl.jinja
CHANGED
|
@@ -1,16 +1,29 @@
|
|
| 1 |
{% macro read_scalar(name, index, dtype) %}{% if dtype == "float16" %}f32({{ name }}[{{ index }}]){% else %}{{ name }}[{{ index }}]{% endif %}{% endmacro %}
|
| 2 |
{% macro write_scalar(expr, dtype) %}{% if dtype == "float16" %}f16({{ expr }}){% else %}{{ expr }}{% endif %}{% endmacro %}
|
| 3 |
-
{% macro emit_serial_query_groups(first_group) %}
|
| 4 |
-
for (var qg = {{ first_group }}u; qg < heads_per_group; qg = qg + 1u) {
|
| 5 |
let q_head = (head_idx * params.qNumHeads) / params.kvNumHeads + qg;
|
| 6 |
let out_head = head_idx * heads_per_group + qg;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
let q_base = bt * params.qPackedDim + q_head * HEAD_DIM_K;
|
| 8 |
var q_out = 0.0;
|
| 9 |
for (var d = 0u; d < HEAD_DIM_K; d = d + 1u) {
|
| 10 |
q_out = q_out + state[d] * {{ read_scalar("query", "q_base + d", queryDtype) }};
|
| 11 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
let out_idx = bt * packed_out + out_head * head_dim_v + dv_idx;
|
| 13 |
output[out_idx] = {{ write_scalar("q_out * scale", outputDtype) }};
|
|
|
|
| 14 |
}{% endmacro %}
|
| 15 |
{% set gatedDeltaRule = updateRule == "gated_delta" %}
|
| 16 |
{{ env.wgsl.resourceDeclarations }}
|
|
@@ -47,19 +60,18 @@ const HEAD_DIM_K: u32 = {{ headDimK }}u;
|
|
| 47 |
// (batch, kv-head, value-dimension) state column and keeps its complete dk
|
| 48 |
// slice private across the sequence without workgroup synchronization. Small dk
|
| 49 |
// bounds the private state retained by each invocation.
|
| 50 |
-
|
| 51 |
-
fn main(
|
| 52 |
-
@builtin(workgroup_id) wg: vec3<u32>,
|
| 53 |
-
) {
|
| 54 |
-
let flat_idx = wg.x + wg.y * {{ DISPATCH_FOLD_WIDTH }}u;
|
| 55 |
let head_dim_v = params.vPackedDim / params.kvNumHeads;
|
| 56 |
let dv_idx = flat_idx % head_dim_v;
|
| 57 |
let bh = flat_idx / head_dim_v;
|
| 58 |
let head_idx = bh % params.kvNumHeads;
|
| 59 |
let batch_idx = bh / params.kvNumHeads;
|
| 60 |
if (batch_idx >= params.batchSize) {
|
| 61 |
-
return;
|
| 62 |
}
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
let n_key_heads = params.kPackedDim / HEAD_DIM_K;
|
| 65 |
let kv_per_key_head = params.kvNumHeads / n_key_heads;
|
|
@@ -88,7 +100,7 @@ fn main(
|
|
| 88 |
}
|
| 89 |
|
| 90 |
var key_local: array<f32, HEAD_DIM_K>;
|
| 91 |
-
{% if hasStateWindow %}
|
| 92 |
// Slots below max(0, stateWindow - seqLength) hold no token from this call.
|
| 93 |
for (var z = 0u; z + params.seqLength < params.stateWindow; z = z + 1u) {
|
| 94 |
for (var d = 0u; d < HEAD_DIM_K; d = d + 1u) {
|
|
@@ -116,6 +128,12 @@ fn main(
|
|
| 116 |
}
|
| 117 |
}
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
let q_head_0 = (head_idx * params.qNumHeads) / params.kvNumHeads;
|
| 120 |
let out_head_0 = head_idx * heads_per_group;
|
| 121 |
let q0_base = bt * params.qPackedDim + q_head_0 * HEAD_DIM_K;
|
|
@@ -128,6 +146,7 @@ fn main(
|
|
| 128 |
preout = preout + state[d] * q_val;
|
| 129 |
kq_dot = kq_dot + key_local[d] * q_val;
|
| 130 |
}
|
|
|
|
| 131 |
var beta_idx = bt * params.kvNumHeads + head_idx;
|
| 132 |
if (params.betaPackedDim == 1u) {
|
| 133 |
beta_idx = bt;
|
|
@@ -135,12 +154,16 @@ fn main(
|
|
| 135 |
let beta_val = {{ read_scalar("beta", "beta_idx", betaDtype) }};
|
| 136 |
let v_idx = bt * params.vPackedDim + head_idx * head_dim_v + dv_idx;
|
| 137 |
let delta = beta_val * ({{ read_scalar("value", "v_idx", valueDtype) }} - retrieved);
|
|
|
|
| 138 |
let out_idx_0 = bt * packed_out + out_head_0 * head_dim_v + dv_idx;
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
| 140 |
for (var d = 0u; d < HEAD_DIM_K; d = d + 1u) {
|
| 141 |
state[d] = state[d] + key_local[d] * delta;
|
| 142 |
}
|
| 143 |
-
{{ emit_serial_query_groups(1) }}
|
| 144 |
{% else %}
|
| 145 |
let v_idx = bt * params.vPackedDim + head_idx * head_dim_v + dv_idx;
|
| 146 |
let v_val = {{ read_scalar("value", "v_idx", valueDtype) }};
|
|
@@ -149,7 +172,7 @@ fn main(
|
|
| 149 |
}
|
| 150 |
{{ emit_serial_query_groups(0) }}
|
| 151 |
{% endif %}
|
| 152 |
-
{% if hasStateWindow %}
|
| 153 |
// Slot j holds the state after token (seqLength - stateWindow + j), so this
|
| 154 |
// token owns slot (t + stateWindow - seqLength) when that lands in the window.
|
| 155 |
if (t + params.stateWindow >= params.seqLength) {
|
|
@@ -161,10 +184,43 @@ fn main(
|
|
| 161 |
{% endif %}
|
| 162 |
}
|
| 163 |
|
| 164 |
-
{% if not hasStateWindow %}
|
| 165 |
for (var d = 0u; d < HEAD_DIM_K; d = d + 1u) {
|
| 166 |
let state_idx = ((batch_idx * params.kvNumHeads + head_idx) * HEAD_DIM_K + d) * head_dim_v + dv_idx;
|
| 167 |
present_state[state_idx] = {{ write_scalar("state[d]", stateDtype) }};
|
| 168 |
}
|
| 169 |
{% endif %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
}
|
|
|
|
| 1 |
{% macro read_scalar(name, index, dtype) %}{% if dtype == "float16" %}f32({{ name }}[{{ index }}]){% else %}{{ name }}[{{ index }}]{% endif %}{% endmacro %}
|
| 2 |
{% macro write_scalar(expr, dtype) %}{% if dtype == "float16" %}f16({{ expr }}){% else %}{{ expr }}{% endif %}{% endmacro %}
|
| 3 |
+
{% macro emit_serial_query_groups(first_group, finiteRescueOnly=false) %}
|
| 4 |
+
for (var qg = {{ first_group }}u; qg < {{ "1u" if finiteRescueOnly else "heads_per_group" }}; qg = qg + 1u) {
|
| 5 |
let q_head = (head_idx * params.qNumHeads) / params.kvNumHeads + qg;
|
| 6 |
let out_head = head_idx * heads_per_group + qg;
|
| 7 |
+
{% if finiteRescueOnly %}
|
| 8 |
+
let out_idx = bt * packed_out + out_head * head_dim_v + dv_idx;
|
| 9 |
+
let previous_output = {{ read_scalar("output", "out_idx", outputDtype) }};
|
| 10 |
+
if (!linear_attention_serial_finite(previous_output)) {
|
| 11 |
+
{% endif %}
|
| 12 |
let q_base = bt * params.qPackedDim + q_head * HEAD_DIM_K;
|
| 13 |
var q_out = 0.0;
|
| 14 |
for (var d = 0u; d < HEAD_DIM_K; d = d + 1u) {
|
| 15 |
q_out = q_out + state[d] * {{ read_scalar("query", "q_base + d", queryDtype) }};
|
| 16 |
}
|
| 17 |
+
{% if finiteRescueOnly %}
|
| 18 |
+
let alternate = q_out * scale;
|
| 19 |
+
if (linear_attention_serial_finite(alternate)) {
|
| 20 |
+
output[out_idx] = {{ write_scalar("alternate", outputDtype) }};
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
{% else %}
|
| 24 |
let out_idx = bt * packed_out + out_head * head_dim_v + dv_idx;
|
| 25 |
output[out_idx] = {{ write_scalar("q_out * scale", outputDtype) }};
|
| 26 |
+
{% endif %}
|
| 27 |
}{% endmacro %}
|
| 28 |
{% set gatedDeltaRule = updateRule == "gated_delta" %}
|
| 29 |
{{ env.wgsl.resourceDeclarations }}
|
|
|
|
| 60 |
// (batch, kv-head, value-dimension) state column and keeps its complete dk
|
| 61 |
// slice private across the sequence without workgroup synchronization. Small dk
|
| 62 |
// bounds the private state retained by each invocation.
|
| 63 |
+
{% macro emit_serial_recurrence(directStateProjection=false, trackBadOutput=false, finiteRescueOnly=false) %}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
let head_dim_v = params.vPackedDim / params.kvNumHeads;
|
| 65 |
let dv_idx = flat_idx % head_dim_v;
|
| 66 |
let bh = flat_idx / head_dim_v;
|
| 67 |
let head_idx = bh % params.kvNumHeads;
|
| 68 |
let batch_idx = bh / params.kvNumHeads;
|
| 69 |
if (batch_idx >= params.batchSize) {
|
| 70 |
+
return{{ " false" if trackBadOutput else "" }};
|
| 71 |
}
|
| 72 |
+
{% if trackBadOutput %}
|
| 73 |
+
var any_bad_output = false;
|
| 74 |
+
{% endif %}
|
| 75 |
|
| 76 |
let n_key_heads = params.kPackedDim / HEAD_DIM_K;
|
| 77 |
let kv_per_key_head = params.kvNumHeads / n_key_heads;
|
|
|
|
| 100 |
}
|
| 101 |
|
| 102 |
var key_local: array<f32, HEAD_DIM_K>;
|
| 103 |
+
{% if hasStateWindow and not finiteRescueOnly %}
|
| 104 |
// Slots below max(0, stateWindow - seqLength) hold no token from this call.
|
| 105 |
for (var z = 0u; z + params.seqLength < params.stateWindow; z = z + 1u) {
|
| 106 |
for (var d = 0u; d < HEAD_DIM_K; d = d + 1u) {
|
|
|
|
| 128 |
}
|
| 129 |
}
|
| 130 |
|
| 131 |
+
{% if directStateProjection %}
|
| 132 |
+
var retrieved = 0.0;
|
| 133 |
+
for (var d = 0u; d < HEAD_DIM_K; d = d + 1u) {
|
| 134 |
+
retrieved = retrieved + state[d] * key_local[d];
|
| 135 |
+
}
|
| 136 |
+
{% else %}
|
| 137 |
let q_head_0 = (head_idx * params.qNumHeads) / params.kvNumHeads;
|
| 138 |
let out_head_0 = head_idx * heads_per_group;
|
| 139 |
let q0_base = bt * params.qPackedDim + q_head_0 * HEAD_DIM_K;
|
|
|
|
| 146 |
preout = preout + state[d] * q_val;
|
| 147 |
kq_dot = kq_dot + key_local[d] * q_val;
|
| 148 |
}
|
| 149 |
+
{% endif %}
|
| 150 |
var beta_idx = bt * params.kvNumHeads + head_idx;
|
| 151 |
if (params.betaPackedDim == 1u) {
|
| 152 |
beta_idx = bt;
|
|
|
|
| 154 |
let beta_val = {{ read_scalar("beta", "beta_idx", betaDtype) }};
|
| 155 |
let v_idx = bt * params.vPackedDim + head_idx * head_dim_v + dv_idx;
|
| 156 |
let delta = beta_val * ({{ read_scalar("value", "v_idx", valueDtype) }} - retrieved);
|
| 157 |
+
{% if not directStateProjection %}
|
| 158 |
let out_idx_0 = bt * packed_out + out_head_0 * head_dim_v + dv_idx;
|
| 159 |
+
let cached_output = (preout + delta * kq_dot) * scale;
|
| 160 |
+
output[out_idx_0] = {{ write_scalar("cached_output", outputDtype) }};
|
| 161 |
+
any_bad_output = any_bad_output || !linear_attention_serial_finite(cached_output);
|
| 162 |
+
{% endif %}
|
| 163 |
for (var d = 0u; d < HEAD_DIM_K; d = d + 1u) {
|
| 164 |
state[d] = state[d] + key_local[d] * delta;
|
| 165 |
}
|
| 166 |
+
{{ emit_serial_query_groups(0 if directStateProjection else 1, finiteRescueOnly) }}
|
| 167 |
{% else %}
|
| 168 |
let v_idx = bt * params.vPackedDim + head_idx * head_dim_v + dv_idx;
|
| 169 |
let v_val = {{ read_scalar("value", "v_idx", valueDtype) }};
|
|
|
|
| 172 |
}
|
| 173 |
{{ emit_serial_query_groups(0) }}
|
| 174 |
{% endif %}
|
| 175 |
+
{% if hasStateWindow and not finiteRescueOnly %}
|
| 176 |
// Slot j holds the state after token (seqLength - stateWindow + j), so this
|
| 177 |
// token owns slot (t + stateWindow - seqLength) when that lands in the window.
|
| 178 |
if (t + params.stateWindow >= params.seqLength) {
|
|
|
|
| 184 |
{% endif %}
|
| 185 |
}
|
| 186 |
|
| 187 |
+
{% if not hasStateWindow and not finiteRescueOnly %}
|
| 188 |
for (var d = 0u; d < HEAD_DIM_K; d = d + 1u) {
|
| 189 |
let state_idx = ((batch_idx * params.kvNumHeads + head_idx) * HEAD_DIM_K + d) * head_dim_v + dv_idx;
|
| 190 |
present_state[state_idx] = {{ write_scalar("state[d]", stateDtype) }};
|
| 191 |
}
|
| 192 |
{% endif %}
|
| 193 |
+
{% if trackBadOutput %}
|
| 194 |
+
return any_bad_output;
|
| 195 |
+
{% endif %}
|
| 196 |
+
{% endmacro %}
|
| 197 |
+
{% if gatedDeltaRule %}
|
| 198 |
+
fn linear_attention_serial_finite(value: f32) -> bool {
|
| 199 |
+
return (bitcast<u32>(value) & 0x7f800000u) != 0x7f800000u;
|
| 200 |
+
}
|
| 201 |
+
fn linear_attention_serial_cached(flat_idx: u32) -> bool {
|
| 202 |
+
{{ emit_serial_recurrence(false, true, false) -}}
|
| 203 |
+
}
|
| 204 |
+
fn linear_attention_serial_repair(flat_idx: u32) {
|
| 205 |
+
{{ emit_serial_recurrence(true, false, true) -}}
|
| 206 |
+
}
|
| 207 |
+
{% endif %}
|
| 208 |
+
@compute @workgroup_size({{ serialWorkgroupSize }}, 1, 1)
|
| 209 |
+
fn main(
|
| 210 |
+
@builtin(workgroup_id) wg: vec3<u32>,
|
| 211 |
+
{% if serialWorkgroupSize > 1 %}
|
| 212 |
+
@builtin(local_invocation_id) lid: vec3<u32>,
|
| 213 |
+
{% endif %}
|
| 214 |
+
) {
|
| 215 |
+
{% if serialWorkgroupSize > 1 %}
|
| 216 |
+
let flat_idx = (wg.x + wg.y * {{ DISPATCH_FOLD_WIDTH }}u) * {{ serialWorkgroupSize }}u + lid.x;
|
| 217 |
+
{% else %}
|
| 218 |
+
let flat_idx = wg.x + wg.y * {{ DISPATCH_FOLD_WIDTH }}u;
|
| 219 |
+
{% endif %}
|
| 220 |
+
{% if gatedDeltaRule %}
|
| 221 |
+
let needs_repair = linear_attention_serial_cached(flat_idx);
|
| 222 |
+
if (needs_repair) { linear_attention_serial_repair(flat_idx); }
|
| 223 |
+
{% else %}
|
| 224 |
+
{{ emit_serial_recurrence(false, false, false) -}}
|
| 225 |
+
{% endif %}
|
| 226 |
}
|
build/webgpu/linear-attention.vec4.wgsl.jinja
CHANGED
|
@@ -1,11 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
{% macro read_scalar(name, index, dtype) %}{% if dtype == "float16" %}f32({{ name }}[{{ index }}]){% else %}{{ name }}[{{ index }}]{% endif %}{% endmacro %}
|
| 2 |
{% macro write_scalar(expr, dtype) %}{% if dtype == "float16" %}f16({{ expr }}){% else %}{{ expr }}{% endif %}{% endmacro %}
|
| 3 |
{% macro emit_tiled_setup(dvGroups=1) %}
|
| 4 |
-
let head_dim_k =
|
| 5 |
-
let head_dim_v =
|
| 6 |
-
let n_key_heads =
|
| 7 |
-
let heads_per_group = max(1u,
|
| 8 |
-
let kv_per_key_head =
|
| 9 |
{% if dvGroups == 1 %}
|
| 10 |
let dv_tiles = (head_dim_v + TILE_V - 1u) / TILE_V;
|
| 11 |
{% else %}
|
|
@@ -16,13 +31,13 @@
|
|
| 16 |
|
| 17 |
// 2D-folded flat (batch*head*dv_tile) index: wg.y carries the high bits past
|
| 18 |
// the per-axis dispatch fold width. Reduces to wg.x with no fold; the batch_idx >=
|
| 19 |
-
//
|
| 20 |
let workgroup_idx = wg.x + wg.y * {{ DISPATCH_FOLD_WIDTH }}u;
|
| 21 |
let dv_tile_idx = workgroup_idx % dv_tiles;
|
| 22 |
let bh = workgroup_idx / dv_tiles;
|
| 23 |
-
let head_idx = bh %
|
| 24 |
-
let batch_idx = bh /
|
| 25 |
-
if (batch_idx >=
|
| 26 |
return;
|
| 27 |
}
|
| 28 |
|
|
@@ -31,22 +46,57 @@
|
|
| 31 |
{% else %}
|
| 32 |
let dv_start = (dv_tile_idx * DV_GROUPS + dv_group) * TILE_V;
|
| 33 |
{% endif %}
|
| 34 |
-
let packed_out = max(
|
| 35 |
let key_head_idx = head_idx / kv_per_key_head;{% endmacro %}
|
| 36 |
-
{%
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
let out_head = head_idx * heads_per_group + qg;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
var q_vec = vec4<f32>(0.0);
|
| 41 |
if (lane_active) {
|
| 42 |
-
let q_base = (bt *
|
| 43 |
q_vec = vec4<f32>(query[q_base]);
|
| 44 |
}
|
| 45 |
-
|
|
|
|
| 46 |
{% if useSubgroups %}
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
pre_qg[j] = subgroupAdd(dot(state[j], q_vec));
|
| 49 |
}
|
|
|
|
|
|
|
|
|
|
| 50 |
{% else %}
|
| 51 |
{% for v in range(redVecs) %}
|
| 52 |
wg_fold[{{ v }}u * WG + tid] = {{ redT }}({% for c in range(redWidth) %}dot(state[{{ v * redWidth + c }}], q_vec){{ ", " if not loop.last else "" }}{% endfor %});
|
|
@@ -68,24 +118,74 @@
|
|
| 68 |
{% endif %}
|
| 69 |
if (lane == 0u) {
|
| 70 |
let out_base_qg = bt * packed_out + out_head * head_dim_v + dv_start;
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
if (dv_start + j < head_dim_v) {
|
| 73 |
output[out_base_qg + j] = {{ write_scalar("pre_qg[j] * scale", queryDtype) }};
|
| 74 |
}
|
| 75 |
}
|
|
|
|
| 76 |
}
|
| 77 |
-
}{% endmacro %}
|
| 78 |
{% set usesDecay = updateRule == "gated" or updateRule == "gated_delta" %}
|
| 79 |
{% set redWidth = 4 if tileV % 4 == 0 else (2 if tileV % 2 == 0 else 1) %}
|
| 80 |
{% set redVecs = (tileV / redWidth)|int %}
|
| 81 |
{% set redT = ("vec" ~ redWidth ~ "<f32>") if redWidth > 1 else "f32" %}
|
| 82 |
{% set redComps = ["x", "y", "z", "w"] %}
|
| 83 |
{% set usesBeta = updateRule == "delta" or updateRule == "gated_delta" %}
|
| 84 |
-
{% set
|
|
|
|
|
|
|
|
|
|
| 85 |
{% if useSubgroups %}
|
| 86 |
enable subgroups;
|
|
|
|
|
|
|
|
|
|
| 87 |
{% endif %}
|
| 88 |
{{ env.wgsl.resourceDeclarations }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
{% if useSubgroups %}
|
| 90 |
var<workgroup> sgLaneClaims: atomic<u32>;
|
| 91 |
|
|
@@ -96,20 +196,73 @@ struct SubgroupLogicalLanes {
|
|
| 96 |
count: u32,
|
| 97 |
}
|
| 98 |
|
| 99 |
-
fn subgroup_logical_lanes(
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
let claim = subgroupBroadcastFirst(ticket);
|
| 108 |
-
return SubgroupLogicalLanes((claim & 0xffffu) +
|
| 109 |
}
|
| 110 |
{% endif %}
|
| 111 |
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
// share a workgroup so the per-token key/query/decay stream is fetched once for all of
|
| 114 |
// them instead of once per value tile.
|
| 115 |
const LANES: u32 = {{ vec4Lanes }}u;
|
|
@@ -119,13 +272,32 @@ const TILE_V: u32 = {{ tileV }}u;
|
|
| 119 |
const VEC4_LANES: u32 = 4u;
|
| 120 |
// query and key are bound as vec4, so a lane's four reduction components arrive in one
|
| 121 |
// 16-byte fetch instead of four scalar ones. Every row base this shader indexes is a
|
| 122 |
-
// multiple of four -- head_dim_k % 4 == 0
|
| 123 |
// whole numbers of heads -- so the vec4 index is the row base in vec4 units plus the
|
| 124 |
// lane. The remaining scalar reads (decay's per-head arm, value, beta) are not aligned
|
| 125 |
// groups of four and keep their scalar view.
|
| 126 |
-
{% if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
-
//
|
| 129 |
// token reduces is staged before one barrier and read back after it, so the barrier
|
| 130 |
// count is a property of the token and not of how many quantities it reduces.
|
| 131 |
// A lane's TILE_V components are adjacent, so they are stored as one {{ redT }} word
|
|
@@ -135,40 +307,27 @@ const VEC4_LANES: u32 = 4u;
|
|
| 135 |
var<workgroup> wg_fold: array<{{ redT }}, WG * {{ redSlots }}u>;
|
| 136 |
var<workgroup> wg_fold_out: array<{{ redT }}, DV_GROUPS * {{ redSlots }}u>;
|
| 137 |
{% endif %}
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
{%
|
| 142 |
-
|
| 143 |
-
@builtin(subgroup_size) sgWidth: u32,
|
| 144 |
-
{% else %}
|
| 145 |
-
@builtin(local_invocation_id) lid: vec3<u32>,
|
| 146 |
-
{% endif %}
|
| 147 |
-
) {
|
| 148 |
-
{% if useSubgroups %}
|
| 149 |
-
// Subgroup tier: selection admits this arm only on adapters whose fixed subgroup
|
| 150 |
-
// width equals LANES, so every subgroup of the workgroup is exactly one lane group
|
| 151 |
-
// and subgroupAdd over it is the group's reduction. The group and the lane come from
|
| 152 |
-
// the subgroup itself -- the dense claim order of the subgroup and the invocation's
|
| 153 |
-
// dense rank among its active lanes -- not from a partition of local_invocation_id,
|
| 154 |
-
// because WGSL does not promise which invocations share a subgroup.
|
| 155 |
-
let L = subgroup_logical_lanes(sgLane, sgWidth);
|
| 156 |
-
let lane = L.rank;
|
| 157 |
-
let dv_group = L.ord;
|
| 158 |
-
{% else %}
|
| 159 |
-
let tid = lid.x;
|
| 160 |
-
let lane = tid % LANES;
|
| 161 |
-
let dv_group = tid / LANES;
|
| 162 |
-
{% endif %}
|
| 163 |
let dk_base = lane * VEC4_LANES;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
{{ emit_tiled_setup(dvGroups=(2 if useSubgroups else dvGroups)) }}
|
|
|
|
| 165 |
let lane_active = dk_base < head_dim_k;
|
| 166 |
|
| 167 |
// state[j] holds 4 consecutive dk rows (the 4 components) for dv slot j.
|
| 168 |
-
var state: array<vec4<f32>,
|
| 169 |
-
|
|
|
|
| 170 |
state[j] = vec4<f32>(0.0);
|
| 171 |
}
|
|
|
|
| 172 |
{% if hasPastState %}
|
| 173 |
|
| 174 |
if (lane_active) {
|
|
@@ -176,8 +335,9 @@ fn main(
|
|
| 176 |
// A windowed past_state is read only from slot stateWindow-1, the state after
|
| 177 |
// the last token of the previous call.
|
| 178 |
{% endif %}
|
| 179 |
-
let state_row_base = {% if hasStateWindow %}(
|
| 180 |
-
|
|
|
|
| 181 |
if (dv_start + j < head_dim_v) {
|
| 182 |
state[j] = vec4<f32>(
|
| 183 |
{{ read_scalar("past_state", "state_row_base + 0u * head_dim_v + j", stateDtype) }},
|
|
@@ -187,16 +347,18 @@ fn main(
|
|
| 187 |
);
|
| 188 |
}
|
| 189 |
}
|
|
|
|
| 190 |
}
|
| 191 |
|
| 192 |
{% endif %}
|
| 193 |
-
{% if hasStateWindow %}
|
| 194 |
// Slots below max(0, stateWindow - seqLength) hold no token from this call.
|
| 195 |
if (lane_active) {
|
| 196 |
-
let zero_row_base = ((batch_idx *
|
| 197 |
-
for (var z = 0u; z +
|
| 198 |
-
let zb = z *
|
| 199 |
-
|
|
|
|
| 200 |
if (dv_start + j < head_dim_v) {
|
| 201 |
present_state[zb + 0u * head_dim_v + j] = {{ write_scalar("0.0", stateDtype) }};
|
| 202 |
present_state[zb + 1u * head_dim_v + j] = {{ write_scalar("0.0", stateDtype) }};
|
|
@@ -204,85 +366,320 @@ fn main(
|
|
| 204 |
present_state[zb + 3u * head_dim_v + j] = {{ write_scalar("0.0", stateDtype) }};
|
| 205 |
}
|
| 206 |
}
|
|
|
|
| 207 |
}
|
| 208 |
}
|
| 209 |
{% endif %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
// A token's key and query do not depend on the recurrent state. Fetch t+1
|
| 211 |
// before completing the reductions for t so the independent load is issued
|
| 212 |
// ahead of its first use.
|
| 213 |
{% macro load_key(token) %}
|
| 214 |
if (lane_active) {
|
| 215 |
-
let k_base = ({{ token }} *
|
| 216 |
k_next = vec4<f32>(key[k_base]);
|
| 217 |
}{% endmacro %}
|
| 218 |
-
{% if
|
| 219 |
{% macro load_query0(token) %}
|
| 220 |
if (lane_active) {
|
| 221 |
-
let q0_base = ({{ token }} *
|
| 222 |
q0_next = vec4<f32>(query[q0_base]);
|
| 223 |
}{% endmacro %}
|
| 224 |
-
let q_head_0 = (head_idx *
|
| 225 |
let out_head_0 = head_idx * heads_per_group;
|
| 226 |
var q0_next = vec4<f32>(0.0);
|
| 227 |
{% endif %}
|
| 228 |
var k_next = vec4<f32>(0.0);
|
| 229 |
-
let bt_first = batch_idx *
|
| 230 |
{{ load_key("bt_first") }}
|
| 231 |
-
{% if
|
| 232 |
{{ load_query0("bt_first") }}
|
| 233 |
{% endif %}
|
| 234 |
-
for (var t = 0u; t <
|
| 235 |
-
let bt = batch_idx *
|
| 236 |
|
| 237 |
let k_vec = k_next;
|
| 238 |
-
{% if
|
| 239 |
let q0_vec = q0_next;
|
| 240 |
{% endif %}
|
| 241 |
// Issued here, consumed on the next trip.
|
| 242 |
let bt_next = bt + 1u;
|
| 243 |
-
if (t + 1u <
|
| 244 |
{{ load_key("bt_next") }}
|
| 245 |
-
{% if
|
| 246 |
{{ load_query0("bt_next") }}
|
| 247 |
{% endif %}
|
| 248 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
{% if usesDecay %}
|
| 250 |
-
|
| 251 |
var decay_vec = vec4<f32>(1.0);
|
| 252 |
-
if (
|
| 253 |
-
let factor = exp(
|
| 254 |
-
|
| 255 |
} else if (lane_active) {
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
decay_vec = vec4<f32>(
|
| 258 |
exp({{ read_scalar("decay", "decay_base + 0u", queryDtype) }}),
|
| 259 |
exp({{ read_scalar("decay", "decay_base + 1u", queryDtype) }}),
|
| 260 |
exp({{ read_scalar("decay", "decay_base + 2u", queryDtype) }}),
|
| 261 |
exp({{ read_scalar("decay", "decay_base + 3u", queryDtype) }}),
|
| 262 |
);
|
| 263 |
-
}
|
| 264 |
-
|
|
|
|
|
|
|
| 265 |
state[j] = state[j] * decay_vec;
|
| 266 |
}
|
|
|
|
| 267 |
|
| 268 |
{% endif %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
{% if usesBeta %}
|
| 270 |
-
|
| 271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
{% if useSubgroups %}
|
|
|
|
| 273 |
let kq_dot = subgroupAdd(dot(k_vec, q0_vec));
|
| 274 |
-
|
|
|
|
| 275 |
ret_vals[j] = subgroupAdd(dot(state[j], k_vec));
|
|
|
|
| 276 |
pre_vals[j] = subgroupAdd(dot(state[j], q0_vec));
|
|
|
|
| 277 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
{% else %}
|
| 279 |
// kq and both state projections are known before any of them is consumed, so all
|
| 280 |
// three stage together and cross one barrier pair rather than two.
|
| 281 |
{% for v in range(redVecs) %}
|
| 282 |
wg_fold[{{ v }}u * WG + tid] = {{ redT }}({% for c in range(redWidth) %}dot(state[{{ v * redWidth + c }}], k_vec){{ ", " if not loop.last else "" }}{% endfor %});
|
|
|
|
| 283 |
wg_fold[{{ redVecs + v }}u * WG + tid] = {{ redT }}({% for c in range(redWidth) %}dot(state[{{ v * redWidth + c }}], q0_vec){{ ", " if not loop.last else "" }}{% endfor %});
|
|
|
|
| 284 |
{% endfor %}
|
|
|
|
| 285 |
wg_fold[{{ 2 * redVecs }}u * WG + tid] = {{ redT }}(dot(k_vec, q0_vec){% for c in range(redWidth - 1) %}, 0.0{% endfor %});
|
|
|
|
| 286 |
workgroupBarrier();
|
| 287 |
for (var sl = lane; sl < {{ redSlots }}u; sl = sl + LANES) {
|
| 288 |
var t = {{ redT }}(0.0);
|
|
@@ -291,58 +688,106 @@ fn main(
|
|
| 291 |
}
|
| 292 |
wg_fold_out[dv_group * {{ redSlots }}u + sl] = t;
|
| 293 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
workgroupBarrier();
|
|
|
|
|
|
|
| 295 |
let kq_dot = wg_fold_out[dv_group * {{ redSlots }}u + {{ 2 * redVecs }}u]{{ ("." ~ redComps[0]) if redWidth > 1 else "" }};
|
| 296 |
-
{%
|
|
|
|
| 297 |
ret_vals[{{ j }}] = wg_fold_out[dv_group * {{ redSlots }}u + {{ (j / redWidth)|int }}u]{{ ("." ~ redComps[j % redWidth]) if redWidth > 1 else "" }};
|
|
|
|
| 298 |
pre_vals[{{ j }}] = wg_fold_out[dv_group * {{ redSlots }}u + {{ redVecs + (j / redWidth)|int }}u]{{ ("." ~ redComps[j % redWidth]) if redWidth > 1 else "" }};
|
|
|
|
| 299 |
{% endfor %}
|
| 300 |
{% endif %}
|
| 301 |
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
|
|
|
|
|
|
| 306 |
let beta_val = {{ read_scalar("beta", "beta_idx", queryDtype) }};
|
| 307 |
-
let v_base = bt *
|
|
|
|
|
|
|
| 308 |
let out_base = bt * packed_out + out_head_0 * head_dim_v + dv_start;
|
| 309 |
-
var deltas: array<f32,
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
if (dv_start + j < head_dim_v) {
|
| 312 |
-
|
| 313 |
let delta_j = beta_val * (v_val - ret_vals[j]);
|
|
|
|
| 314 |
deltas[j] = delta_j;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
if (lane == 0u) {
|
| 316 |
output[out_base + j] = {{ write_scalar("(pre_vals[j] + delta_j * kq_dot) * scale", queryDtype) }};
|
| 317 |
}
|
|
|
|
| 318 |
} else {
|
| 319 |
deltas[j] = 0.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
}
|
| 321 |
}
|
| 322 |
|
| 323 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
state[j] = state[j] + k_vec * deltas[j];
|
| 325 |
}
|
|
|
|
| 326 |
|
| 327 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
{% else %}
|
| 329 |
-
|
| 330 |
-
|
|
|
|
|
|
|
| 331 |
if (dv_start + j < head_dim_v) {
|
| 332 |
-
|
| 333 |
state[j] = state[j] + k_vec * v_val;
|
| 334 |
}
|
| 335 |
}
|
| 336 |
|
| 337 |
-
{{ emit_vec4_query_groups(0) }}
|
| 338 |
{% endif %}
|
| 339 |
{% if hasStateWindow %}
|
| 340 |
// Slot j holds the state after token (seqLength - stateWindow + j), so this
|
| 341 |
// token owns slot (t + stateWindow - seqLength) when that lands in the window.
|
| 342 |
-
if (t +
|
| 343 |
-
let win_base = (t +
|
| 344 |
-
+ ((batch_idx *
|
| 345 |
-
|
|
|
|
| 346 |
if (dv_start + j < head_dim_v) {
|
| 347 |
present_state[win_base + 0u * head_dim_v + j] = {{ write_scalar("state[j].x", stateDtype) }};
|
| 348 |
present_state[win_base + 1u * head_dim_v + j] = {{ write_scalar("state[j].y", stateDtype) }};
|
|
@@ -350,14 +795,39 @@ fn main(
|
|
| 350 |
present_state[win_base + 3u * head_dim_v + j] = {{ write_scalar("state[j].w", stateDtype) }};
|
| 351 |
}
|
| 352 |
}
|
|
|
|
| 353 |
}
|
| 354 |
{% endif %}
|
| 355 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
|
| 357 |
{% if not hasStateWindow %}
|
| 358 |
if (lane_active) {
|
| 359 |
-
let state_row_base = ((batch_idx *
|
| 360 |
-
|
|
|
|
| 361 |
if (dv_start + j < head_dim_v) {
|
| 362 |
present_state[state_row_base + 0u * head_dim_v + j] = {{ write_scalar("state[j].x", stateDtype) }};
|
| 363 |
present_state[state_row_base + 1u * head_dim_v + j] = {{ write_scalar("state[j].y", stateDtype) }};
|
|
@@ -365,6 +835,47 @@ fn main(
|
|
| 365 |
present_state[state_row_base + 3u * head_dim_v + j] = {{ write_scalar("state[j].w", stateDtype) }};
|
| 366 |
}
|
| 367 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
{% endif %}
|
|
|
|
| 370 |
}
|
|
|
|
| 1 |
+
{% set inlineStateRepair = inlineStateRepair and not directStateProjection %}
|
| 2 |
+
{% set static_batchSize = static_batchSize | default(0) %}
|
| 3 |
+
{% set static_seqLength = static_seqLength | default(0) %}
|
| 4 |
+
{% set static_qNumHeads = static_qNumHeads | default(0) %}
|
| 5 |
+
{% set static_kvNumHeads = static_kvNumHeads | default(0) %}
|
| 6 |
+
{% set static_qPackedDim = static_qPackedDim | default(0) %}
|
| 7 |
+
{% set static_kPackedDim = static_kPackedDim | default(0) %}
|
| 8 |
+
{% set static_vPackedDim = static_vPackedDim | default(0) %}
|
| 9 |
+
{% set static_decayPackedDim = static_decayPackedDim | default(0) %}
|
| 10 |
+
{% set static_betaPackedDim = static_betaPackedDim | default(0) %}
|
| 11 |
+
{% set static_stateWindow = static_stateWindow | default(0) %}
|
| 12 |
+
{% set static_stateSlotStride = static_stateSlotStride | default(0) %}
|
| 13 |
+
{% set staticValueLoops = useSubgroups and vec4SubgroupExact and vec4PinSize == 0 %}
|
| 14 |
+
{% set staticGeometry = staticGeometry | default(false) %}
|
| 15 |
+
{% set geometryPrefix = "GEOMETRY_" if staticGeometry else "params." %}
|
| 16 |
{% macro read_scalar(name, index, dtype) %}{% if dtype == "float16" %}f32({{ name }}[{{ index }}]){% else %}{{ name }}[{{ index }}]{% endif %}{% endmacro %}
|
| 17 |
{% macro write_scalar(expr, dtype) %}{% if dtype == "float16" %}f16({{ expr }}){% else %}{{ expr }}{% endif %}{% endmacro %}
|
| 18 |
{% macro emit_tiled_setup(dvGroups=1) %}
|
| 19 |
+
let head_dim_k = {{ geometryPrefix }}qPackedDim / {{ geometryPrefix }}qNumHeads;
|
| 20 |
+
let head_dim_v = {{ geometryPrefix }}vPackedDim / {{ geometryPrefix }}kvNumHeads;
|
| 21 |
+
let n_key_heads = {{ geometryPrefix }}kPackedDim / head_dim_k;
|
| 22 |
+
let heads_per_group = max(1u, {{ geometryPrefix }}qNumHeads / {{ geometryPrefix }}kvNumHeads);
|
| 23 |
+
let kv_per_key_head = {{ geometryPrefix }}kvNumHeads / n_key_heads;
|
| 24 |
{% if dvGroups == 1 %}
|
| 25 |
let dv_tiles = (head_dim_v + TILE_V - 1u) / TILE_V;
|
| 26 |
{% else %}
|
|
|
|
| 31 |
|
| 32 |
// 2D-folded flat (batch*head*dv_tile) index: wg.y carries the high bits past
|
| 33 |
// the per-axis dispatch fold width. Reduces to wg.x with no fold; the batch_idx >=
|
| 34 |
+
// {{ geometryPrefix }}batchSize guard drops the over-dispatched tail.
|
| 35 |
let workgroup_idx = wg.x + wg.y * {{ DISPATCH_FOLD_WIDTH }}u;
|
| 36 |
let dv_tile_idx = workgroup_idx % dv_tiles;
|
| 37 |
let bh = workgroup_idx / dv_tiles;
|
| 38 |
+
let head_idx = bh % {{ geometryPrefix }}kvNumHeads;
|
| 39 |
+
let batch_idx = bh / {{ geometryPrefix }}kvNumHeads;
|
| 40 |
+
if (batch_idx >= {{ geometryPrefix }}batchSize) {
|
| 41 |
return;
|
| 42 |
}
|
| 43 |
|
|
|
|
| 46 |
{% else %}
|
| 47 |
let dv_start = (dv_tile_idx * DV_GROUPS + dv_group) * TILE_V;
|
| 48 |
{% endif %}
|
| 49 |
+
let packed_out = max({{ geometryPrefix }}qNumHeads, {{ geometryPrefix }}kvNumHeads) * head_dim_v;
|
| 50 |
let key_head_idx = head_idx / kv_per_key_head;{% endmacro %}
|
| 51 |
+
{% set prefetchTokenInputs = not staticGeometry %}
|
| 52 |
+
{% macro value_loop_open(valueIndex, staticValueLoops, engineTileV) %}{% if staticValueLoops %}{
|
| 53 |
+
const j: u32 = {{ valueIndex }}u;
|
| 54 |
+
{% else %}for (var j = 0u; j < {{ engineTileV }}; j = j + 1u) {
|
| 55 |
+
{% endif %}{% endmacro %}
|
| 56 |
+
{% macro read_current_value(engineValues) %}
|
| 57 |
+
{% if not prefetchTokenInputs %}
|
| 58 |
+
let v_val = {{ read_scalar("value", "v_base + j", queryDtype) }};
|
| 59 |
+
{% else %}
|
| 60 |
+
var v_val = v_cur_0;
|
| 61 |
+
{% for currentIndex in range(1, engineValues) %}
|
| 62 |
+
v_val = select(v_val, v_cur_{{ currentIndex }}, j == {{ currentIndex }}u);
|
| 63 |
+
{% endfor %}
|
| 64 |
+
{% endif %}{%- endmacro %}
|
| 65 |
+
{% macro emit_vec4_query_groups(first_group, useSubgroups, engineTileV, compactFold, prefetchQueries, repairFirst=false) %}
|
| 66 |
+
{% if not staticGeometry or queryGroups > first_group %}
|
| 67 |
+
for (var qg = {{ first_group }}u; qg < {% if repairFirst %}1u{% elif staticGeometry %}{{ queryGroups }}u{% else %}heads_per_group{% endif %}; qg = qg + 1u) {
|
| 68 |
+
{% if not prefetchTokenInputs %}
|
| 69 |
+
let q_head = (head_idx * {{ geometryPrefix }}qNumHeads) / {{ geometryPrefix }}kvNumHeads + qg;
|
| 70 |
+
{% endif %}
|
| 71 |
let out_head = head_idx * heads_per_group + qg;
|
| 72 |
+
{% if prefetchQueries and not repairFirst %}
|
| 73 |
+
var q_vec = q_cur_0;
|
| 74 |
+
{% for qg in range(1, queryGroups) %}
|
| 75 |
+
q_vec = select(q_vec, q_cur_{{ qg }}, qg == {{ qg }}u);
|
| 76 |
+
{% endfor %}
|
| 77 |
+
{% else %}
|
| 78 |
+
{% if prefetchTokenInputs %}
|
| 79 |
+
let q_head = (head_idx * {{ geometryPrefix }}qNumHeads) / {{ geometryPrefix }}kvNumHeads + qg;
|
| 80 |
+
{% endif %}
|
| 81 |
var q_vec = vec4<f32>(0.0);
|
| 82 |
if (lane_active) {
|
| 83 |
+
let q_base = (bt * {{ geometryPrefix }}qPackedDim + q_head * head_dim_k) / VEC4_LANES + lane;
|
| 84 |
q_vec = vec4<f32>(query[q_base]);
|
| 85 |
}
|
| 86 |
+
{% endif %}
|
| 87 |
+
var pre_qg: array<f32, {{ engineTileV }}>;
|
| 88 |
{% if useSubgroups %}
|
| 89 |
+
{% if repairFirst %}
|
| 90 |
+
{% for j in range(tileV) %}
|
| 91 |
+
pre_qg[{{ j }}u] = linear_attention_project_state(state[{{ j }}u], q_vec);
|
| 92 |
+
{% endfor %}
|
| 93 |
+
{% else %}
|
| 94 |
+
{{ value_loop_open(0, false, engineTileV) | indent(6, true) }}
|
| 95 |
pre_qg[j] = subgroupAdd(dot(state[j], q_vec));
|
| 96 |
}
|
| 97 |
+
{% endif %}
|
| 98 |
+
{% elif compactFold %}
|
| 99 |
+
pre_qg[0] = linearAttentionVec4Fold(dot(state[0], q_vec), tid, lane, dv_group);
|
| 100 |
{% else %}
|
| 101 |
{% for v in range(redVecs) %}
|
| 102 |
wg_fold[{{ v }}u * WG + tid] = {{ redT }}({% for c in range(redWidth) %}dot(state[{{ v * redWidth + c }}], q_vec){{ ", " if not loop.last else "" }}{% endfor %});
|
|
|
|
| 118 |
{% endif %}
|
| 119 |
if (lane == 0u) {
|
| 120 |
let out_base_qg = bt * packed_out + out_head * head_dim_v + dv_start;
|
| 121 |
+
{% if repairFirst %}
|
| 122 |
+
{% for j in range(tileV) %}
|
| 123 |
+
if (dv_start + {{ j }}u < head_dim_v) {
|
| 124 |
+
let repaired{{ j }} = pre_qg[{{ j }}u] * scale;
|
| 125 |
+
if (repair_values[{{ j }}u] && linear_attention_finite(repaired{{ j }})) {
|
| 126 |
+
output[out_base_qg + {{ j }}u] = {{ write_scalar("repaired" ~ j, queryDtype) }};
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
{% endfor %}
|
| 130 |
+
{% else %}
|
| 131 |
+
{{ value_loop_open(0, false, engineTileV) | indent(8, true) }}
|
| 132 |
if (dv_start + j < head_dim_v) {
|
| 133 |
output[out_base_qg + j] = {{ write_scalar("pre_qg[j] * scale", queryDtype) }};
|
| 134 |
}
|
| 135 |
}
|
| 136 |
+
{% endif %}
|
| 137 |
}
|
| 138 |
+
}{% endif %}{% endmacro %}
|
| 139 |
{% set usesDecay = updateRule == "gated" or updateRule == "gated_delta" %}
|
| 140 |
{% set redWidth = 4 if tileV % 4 == 0 else (2 if tileV % 2 == 0 else 1) %}
|
| 141 |
{% set redVecs = (tileV / redWidth)|int %}
|
| 142 |
{% set redT = ("vec" ~ redWidth ~ "<f32>") if redWidth > 1 else "f32" %}
|
| 143 |
{% set redComps = ["x", "y", "z", "w"] %}
|
| 144 |
{% set usesBeta = updateRule == "delta" or updateRule == "gated_delta" %}
|
| 145 |
+
{% set cachedDeltaProjection = usesBeta and not directStateProjection %}
|
| 146 |
+
{% set deferredFirst = 1 if cachedDeltaProjection else 0 %}
|
| 147 |
+
{% set deferredGroups = queryGroups - deferredFirst %}
|
| 148 |
+
{% set redSlots = (2 * redVecs + 1) if cachedDeltaProjection else redVecs %}
|
| 149 |
{% if useSubgroups %}
|
| 150 |
enable subgroups;
|
| 151 |
+
{% if vec4PinSize > 0 %}
|
| 152 |
+
enable subgroup_size_control;
|
| 153 |
+
{% endif %}
|
| 154 |
{% endif %}
|
| 155 |
{{ env.wgsl.resourceDeclarations }}
|
| 156 |
+
{% if inlineStateRepair and usesBeta %}
|
| 157 |
+
// Reassociation is attempted only for a nonfinite cached projection.
|
| 158 |
+
fn linear_attention_finite(value: f32) -> bool {
|
| 159 |
+
return (bitcast<u32>(value) & 0x7f800000u) != 0x7f800000u;
|
| 160 |
+
}
|
| 161 |
+
{% if useSubgroups %}
|
| 162 |
+
// Tint retains varying dependencies, so its uniformity analysis reports a false positive here.
|
| 163 |
+
// Every caller tests the same cached projections on every active lane. Their
|
| 164 |
+
// reductions and scalar operands are subgroup-uniform after the topology check, but the uniformity analysis can
|
| 165 |
+
// still see varying dependencies, so suppress the diagnostic for this helper only.
|
| 166 |
+
@diagnostic(off, subgroup_uniformity)
|
| 167 |
+
fn linear_attention_project_state(state_value: vec4<f32>, query_value: vec4<f32>) -> f32 {
|
| 168 |
+
return subgroupAdd(dot(state_value, query_value));
|
| 169 |
+
}
|
| 170 |
+
{% endif %}
|
| 171 |
+
{% endif %}
|
| 172 |
+
{% if staticGeometry %}
|
| 173 |
+
{% set geometryFields = [
|
| 174 |
+
["batchSize", static_batchSize], ["seqLength", static_seqLength],
|
| 175 |
+
["qNumHeads", static_qNumHeads], ["kvNumHeads", static_kvNumHeads],
|
| 176 |
+
["qPackedDim", static_qPackedDim], ["kPackedDim", static_kPackedDim],
|
| 177 |
+
["vPackedDim", static_vPackedDim]
|
| 178 |
+
] %}
|
| 179 |
+
{% if usesDecay and static_decayPackedDim != static_kvNumHeads %}
|
| 180 |
+
{% set geometryFields = geometryFields + [["decayPackedDim", static_decayPackedDim]] %}
|
| 181 |
+
{% endif %}
|
| 182 |
+
{% if hasStateWindow %}
|
| 183 |
+
{% set geometryFields = geometryFields + [["stateWindow", static_stateWindow], ["stateSlotStride", static_stateSlotStride]] %}
|
| 184 |
+
{% endif %}
|
| 185 |
+
{% for field in geometryFields %}
|
| 186 |
+
const GEOMETRY_{{ field[0] }}: u32 = {{ field[1] }}u;
|
| 187 |
+
{% endfor %}
|
| 188 |
+
{% endif %}
|
| 189 |
{% if useSubgroups %}
|
| 190 |
var<workgroup> sgLaneClaims: atomic<u32>;
|
| 191 |
|
|
|
|
| 196 |
count: u32,
|
| 197 |
}
|
| 198 |
|
| 199 |
+
fn subgroup_logical_lanes(lane: u32) -> SubgroupLogicalLanes {
|
| 200 |
+
{% macro active_lanes(lane, prefix="active") %}
|
| 201 |
+
let {{ prefix }}Mask = subgroupBallot(true);
|
| 202 |
+
let {{ prefix }}Word = {{ lane }} / 32u;
|
| 203 |
+
let {{ prefix }}Lower = (1u << ({{ lane }} % 32u)) - 1u;
|
| 204 |
+
let {{ prefix }}Count = max(countOneBits({{ prefix }}Mask.x) + countOneBits({{ prefix }}Mask.y)
|
| 205 |
+
+ countOneBits({{ prefix }}Mask.z) + countOneBits({{ prefix }}Mask.w), 1u);
|
| 206 |
+
var {{ prefix }}Rank = countOneBits({{ prefix }}Mask[{{ prefix }}Word] & {{ prefix }}Lower);
|
| 207 |
+
for (var word = 0u; word < {{ prefix }}Word; word++) {
|
| 208 |
+
{{ prefix }}Rank += countOneBits({{ prefix }}Mask[word]);
|
| 209 |
+
}
|
| 210 |
+
{% endmacro %}
|
| 211 |
+
{{ active_lanes("lane") }}
|
| 212 |
+
// Dense rank zero is the first ACTIVE lane, exactly the BroadcastFirst source.
|
| 213 |
+
// All lanes execute the atomic and collective in uniform control flow.
|
| 214 |
+
let ticket = atomicAdd(&sgLaneClaims, select(0u, activeCount | (1u << 16u), activeRank == 0u));
|
| 215 |
let claim = subgroupBroadcastFirst(ticket);
|
| 216 |
+
return SubgroupLogicalLanes((claim & 0xffffu) + activeRank, claim >> 16u, activeRank, activeCount);
|
| 217 |
}
|
| 218 |
{% endif %}
|
| 219 |
|
| 220 |
+
{% if scanOutputBeforeReplay %}var<workgroup> replay_scan_bad: atomic<u32>;
|
| 221 |
+
|
| 222 |
+
fn replay_nonfinite(value: f32) -> bool {
|
| 223 |
+
return (bitcast<u32>(value) & 0x7f800000u) == 0x7f800000u;
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
{% macro emit_linear_attention_output_scan() %}
|
| 227 |
+
// The fast first pass wrote every output. Inspect only this workgroup's
|
| 228 |
+
// cached first-head columns; other heads already project updated state.
|
| 229 |
+
let scan_head_v = {{ geometryPrefix }}vPackedDim / {{ geometryPrefix }}kvNumHeads;
|
| 230 |
+
let scan_tile_cols = TILE_V * DV_GROUPS;
|
| 231 |
+
let scan_value_tiles = (scan_head_v + scan_tile_cols - 1u) / scan_tile_cols;
|
| 232 |
+
let scan_group = wg.x + wg.y * {{ DISPATCH_FOLD_WIDTH }}u;
|
| 233 |
+
let scan_value_band = scan_group % scan_value_tiles;
|
| 234 |
+
let scan_batch_head = scan_group / scan_value_tiles;
|
| 235 |
+
let scan_head = scan_batch_head % {{ geometryPrefix }}kvNumHeads;
|
| 236 |
+
let scan_batch = scan_batch_head / {{ geometryPrefix }}kvNumHeads;
|
| 237 |
+
if (scan_batch >= {{ geometryPrefix }}batchSize) {
|
| 238 |
+
return;
|
| 239 |
+
}
|
| 240 |
+
let scan_value_start = scan_value_band * scan_tile_cols;
|
| 241 |
+
let scan_cols = min(scan_tile_cols, scan_head_v - scan_value_start);
|
| 242 |
+
let scan_query_groups = max(1u, {{ geometryPrefix }}qNumHeads / {{ geometryPrefix }}kvNumHeads);
|
| 243 |
+
let scan_out_head = scan_head * scan_query_groups;
|
| 244 |
+
let scan_out_packed = max({{ geometryPrefix }}qNumHeads, {{ geometryPrefix }}kvNumHeads) * scan_head_v;
|
| 245 |
+
let scan_count = {{ geometryPrefix }}seqLength * scan_cols;
|
| 246 |
+
for (var scan_index = tid; scan_index < scan_count; scan_index += WG) {
|
| 247 |
+
let scan_token = scan_index / scan_cols;
|
| 248 |
+
let scan_col = scan_index % scan_cols;
|
| 249 |
+
let scan_output = (scan_batch * {{ geometryPrefix }}seqLength + scan_token) * scan_out_packed
|
| 250 |
+
+ scan_out_head * scan_head_v + scan_value_start + scan_col;
|
| 251 |
+
if (replay_nonfinite(f32(output[scan_output]))) {
|
| 252 |
+
atomicOr(&replay_scan_bad, 1u);
|
| 253 |
+
}
|
| 254 |
+
}
|
| 255 |
+
workgroupBarrier();
|
| 256 |
+
if (tid == 0u) {
|
| 257 |
+
vec4Accepted = atomicLoad(&replay_scan_bad);
|
| 258 |
+
}
|
| 259 |
+
if (workgroupUniformLoad(&vec4Accepted) == 0u) {
|
| 260 |
+
return;
|
| 261 |
+
}
|
| 262 |
+
// Reconverge surviving workgroups before subgroup topology collectives.
|
| 263 |
+
workgroupBarrier();
|
| 264 |
+
{% endmacro %}
|
| 265 |
+
{% endif %}// LANES threads cooperate on one value tile's reduction axis; DV_GROUPS such groups
|
| 266 |
// share a workgroup so the per-token key/query/decay stream is fetched once for all of
|
| 267 |
// them instead of once per value tile.
|
| 268 |
const LANES: u32 = {{ vec4Lanes }}u;
|
|
|
|
| 272 |
const VEC4_LANES: u32 = 4u;
|
| 273 |
// query and key are bound as vec4, so a lane's four reduction components arrive in one
|
| 274 |
// 16-byte fetch instead of four scalar ones. Every row base this shader indexes is a
|
| 275 |
+
// multiple of four -- head_dim_k % 4 == 0 is required, and the packed dims are
|
| 276 |
// whole numbers of heads -- so the vec4 index is the row base in vec4 units plus the
|
| 277 |
// lane. The remaining scalar reads (decay's per-head arm, value, beta) are not aligned
|
| 278 |
// groups of four and keep their scalar view.
|
| 279 |
+
{% if useSubgroups %}
|
| 280 |
+
var<workgroup> vec4Rejected: atomic<u32>;
|
| 281 |
+
var<workgroup> vec4Accepted: u32;
|
| 282 |
+
var<workgroup> vec4FallbackFold: array<f32, WG>;
|
| 283 |
+
var<workgroup> vec4FallbackOut: array<f32, DV_GROUPS>;
|
| 284 |
+
|
| 285 |
+
fn linearAttentionVec4Fold(value: f32, tid: u32, lane: u32, group: u32) -> f32 {
|
| 286 |
+
vec4FallbackFold[tid] = value;
|
| 287 |
+
workgroupBarrier();
|
| 288 |
+
if (lane == 0u) {
|
| 289 |
+
var total = 0.0;
|
| 290 |
+
for (var i = 0u; i < LANES; i += 1u) {
|
| 291 |
+
total += vec4FallbackFold[group * LANES + i];
|
| 292 |
+
}
|
| 293 |
+
vec4FallbackOut[group] = total;
|
| 294 |
+
}
|
| 295 |
+
workgroupBarrier();
|
| 296 |
+
return vec4FallbackOut[group];
|
| 297 |
+
}
|
| 298 |
+
{% else %}
|
| 299 |
|
| 300 |
+
// Workgroup reductions: shared-memory linear folds replace subgroupAdd. Every quantity a
|
| 301 |
// token reduces is staged before one barrier and read back after it, so the barrier
|
| 302 |
// count is a property of the token and not of how many quantities it reduces.
|
| 303 |
// A lane's TILE_V components are adjacent, so they are stored as one {{ redT }} word
|
|
|
|
| 307 |
var<workgroup> wg_fold: array<{{ redT }}, WG * {{ redSlots }}u>;
|
| 308 |
var<workgroup> wg_fold_out: array<{{ redT }}, DV_GROUPS * {{ redSlots }}u>;
|
| 309 |
{% endif %}
|
| 310 |
+
{% macro packed_term(slot, currentCount, count) %}{% if slot >= count %}0.0{% elif slot >= currentCount %}{% set deferred = slot - currentCount %}deferred{{ deferredFirst + (deferred / tileV) | int }}_{{ deferred % tileV }}{% elif directStateProjection %}dot(state[{{ slot }}], k_vec){% elif slot == 0 %}dot(k_vec, q_cur_0){% elif slot % 2 == 1 %}dot(state[{{ ((slot - 1) / 2) | int }}], k_vec){% else %}dot(state[{{ ((slot - 2) / 2) | int }}], q_cur_0){% endif %}{% endmacro %}
|
| 311 |
+
{% macro packed_ref(slot) %}packed{{ (slot / 4) | int }}.{{ ["x", "y", "z", "w"][slot % 4] }}{% endmacro %}
|
| 312 |
+
{% macro packed_output(o, currentCount, currentOutputs) %}{% if o < currentOutputs %}({{ packed_ref(2 + 2 * o) }} + delta{{ o }} * {{ packed_ref(0) }}){% else %}{{ packed_ref(currentCount + o - currentOutputs) }}{% endif %}{% endmacro %}
|
| 313 |
+
{% macro recurrence(useSubgroups, staticValueLoops, engineTileV, engineValues, compactFold) %}
|
| 314 |
+
{% set cachedDeltaProjection = usesBeta and not (directStateProjection or (inlineStateRepair and compactFold)) %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
let dk_base = lane * VEC4_LANES;
|
| 316 |
+
{% if compactFold %}
|
| 317 |
+
{{ emit_tiled_setup(dvGroups=dvGroups) | replace("let dv_start =", "let dv_start_base =") }}
|
| 318 |
+
let dv_start = dv_start_base + valueOffset;
|
| 319 |
+
{% else %}
|
| 320 |
{{ emit_tiled_setup(dvGroups=(2 if useSubgroups else dvGroups)) }}
|
| 321 |
+
{% endif %}
|
| 322 |
let lane_active = dk_base < head_dim_k;
|
| 323 |
|
| 324 |
// state[j] holds 4 consecutive dk rows (the 4 components) for dv slot j.
|
| 325 |
+
var state: array<vec4<f32>, {{ engineTileV }}>;
|
| 326 |
+
{% for valueIndex in range(engineValues if staticValueLoops else 1) %}
|
| 327 |
+
{{ value_loop_open(valueIndex, staticValueLoops, engineTileV) | indent(2, true) }}
|
| 328 |
state[j] = vec4<f32>(0.0);
|
| 329 |
}
|
| 330 |
+
{% endfor %}
|
| 331 |
{% if hasPastState %}
|
| 332 |
|
| 333 |
if (lane_active) {
|
|
|
|
| 335 |
// A windowed past_state is read only from slot stateWindow-1, the state after
|
| 336 |
// the last token of the previous call.
|
| 337 |
{% endif %}
|
| 338 |
+
let state_row_base = {% if hasStateWindow %}({{ geometryPrefix }}stateWindow - 1u) * {{ geometryPrefix }}stateSlotStride + {% endif %}((batch_idx * {{ geometryPrefix }}kvNumHeads + head_idx) * head_dim_k + dk_base) * head_dim_v + dv_start;
|
| 339 |
+
{% for valueIndex in range(engineValues if staticValueLoops else 1) %}
|
| 340 |
+
{{ value_loop_open(valueIndex, staticValueLoops, engineTileV) | indent(4, true) }}
|
| 341 |
if (dv_start + j < head_dim_v) {
|
| 342 |
state[j] = vec4<f32>(
|
| 343 |
{{ read_scalar("past_state", "state_row_base + 0u * head_dim_v + j", stateDtype) }},
|
|
|
|
| 347 |
);
|
| 348 |
}
|
| 349 |
}
|
| 350 |
+
{% endfor %}
|
| 351 |
}
|
| 352 |
|
| 353 |
{% endif %}
|
| 354 |
+
{% if hasStateWindow and (not staticGeometry or static_seqLength < static_stateWindow) %}
|
| 355 |
// Slots below max(0, stateWindow - seqLength) hold no token from this call.
|
| 356 |
if (lane_active) {
|
| 357 |
+
let zero_row_base = ((batch_idx * {{ geometryPrefix }}kvNumHeads + head_idx) * head_dim_k + dk_base) * head_dim_v + dv_start;
|
| 358 |
+
for (var z = 0u; z + {{ geometryPrefix }}seqLength < {{ geometryPrefix }}stateWindow; z = z + 1u) {
|
| 359 |
+
let zb = z * {{ geometryPrefix }}stateSlotStride + zero_row_base;
|
| 360 |
+
{% for valueIndex in range(engineValues if staticValueLoops else 1) %}
|
| 361 |
+
{{ value_loop_open(valueIndex, staticValueLoops, engineTileV) | indent(6, true) }}
|
| 362 |
if (dv_start + j < head_dim_v) {
|
| 363 |
present_state[zb + 0u * head_dim_v + j] = {{ write_scalar("0.0", stateDtype) }};
|
| 364 |
present_state[zb + 1u * head_dim_v + j] = {{ write_scalar("0.0", stateDtype) }};
|
|
|
|
| 366 |
present_state[zb + 3u * head_dim_v + j] = {{ write_scalar("0.0", stateDtype) }};
|
| 367 |
}
|
| 368 |
}
|
| 369 |
+
{% endfor %}
|
| 370 |
}
|
| 371 |
}
|
| 372 |
{% endif %}
|
| 373 |
+
{% set prefetchQueries = false %}
|
| 374 |
+
{% set packedReductions = false %}
|
| 375 |
+
{% set retimePackedQueries = false %}
|
| 376 |
+
{% if prefetchTokenInputs %}
|
| 377 |
+
// Readonly token inputs are independent of the recurrent state. Fetch the
|
| 378 |
+
// next token before the current token's arithmetic; only load scheduling changes.
|
| 379 |
+
{% set prefetchQueries = useSubgroups and not compactFold and queryGroups <= 4 %}
|
| 380 |
+
{% set packedReductions = prefetchQueries and staticValueLoops and queryGroups * tileV <= vec4Lanes %}
|
| 381 |
+
{% set retimePackedQueries = directStateProjection and usesBeta and packedReductions %}
|
| 382 |
+
{% macro load_token(token) %}
|
| 383 |
+
if (lane_active) {
|
| 384 |
+
let k_base = ({{ token }} * {{ geometryPrefix }}kPackedDim + key_head_idx * head_dim_k) / VEC4_LANES + lane;
|
| 385 |
+
k_next = vec4<f32>(key[k_base]);
|
| 386 |
+
{% if prefetchQueries and not retimePackedQueries %}
|
| 387 |
+
{% for qg in range(queryGroups) %}
|
| 388 |
+
let q_base_{{ qg }} = ({{ token }} * {{ geometryPrefix }}qPackedDim + (q_head_0 + {{ qg }}u) * head_dim_k) / VEC4_LANES + lane;
|
| 389 |
+
q_next[{{ qg }}] = vec4<f32>(query[q_base_{{ qg }}]);
|
| 390 |
+
{% endfor %}
|
| 391 |
+
{% elif cachedDeltaProjection %}
|
| 392 |
+
let q0_base = ({{ token }} * {{ geometryPrefix }}qPackedDim + q_head_0 * head_dim_k) / VEC4_LANES + lane;
|
| 393 |
+
q0_next = vec4<f32>(query[q0_base]);
|
| 394 |
+
{% endif %}
|
| 395 |
+
}
|
| 396 |
+
{% if usesDecay %}
|
| 397 |
+
if ({{ geometryPrefix }}decayPackedDim == {{ geometryPrefix }}kvNumHeads) {
|
| 398 |
+
decay_next = vec4<f32>({{ read_scalar("decay", token ~ " * " ~ geometryPrefix ~ "kvNumHeads + head_idx", queryDtype) }});
|
| 399 |
+
} else if (lane_active) {
|
| 400 |
+
let decay_base = {{ token }} * {{ geometryPrefix }}decayPackedDim + head_idx * head_dim_k + dk_base;
|
| 401 |
+
decay_next = vec4<f32>(
|
| 402 |
+
{{ read_scalar("decay", "decay_base + 0u", queryDtype) }},
|
| 403 |
+
{{ read_scalar("decay", "decay_base + 1u", queryDtype) }},
|
| 404 |
+
{{ read_scalar("decay", "decay_base + 2u", queryDtype) }},
|
| 405 |
+
{{ read_scalar("decay", "decay_base + 3u", queryDtype) }},
|
| 406 |
+
);
|
| 407 |
+
}{% endif %}
|
| 408 |
+
{% if usesBeta %}
|
| 409 |
+
var beta_idx = {{ token }} * {{ geometryPrefix }}kvNumHeads + head_idx;
|
| 410 |
+
if ({{ geometryPrefix }}betaPackedDim == 1u) {
|
| 411 |
+
beta_idx = {{ token }};
|
| 412 |
+
}
|
| 413 |
+
beta_next = {{ read_scalar("beta", "beta_idx", queryDtype) }};
|
| 414 |
+
{% endif %}
|
| 415 |
+
let v_base = {{ token }} * {{ geometryPrefix }}vPackedDim + head_idx * head_dim_v + dv_start;
|
| 416 |
+
{% for valueIndex in range(engineValues if staticValueLoops else 1) %}
|
| 417 |
+
{{ value_loop_open(valueIndex, staticValueLoops, engineTileV) | indent(4, true) }}
|
| 418 |
+
if (dv_start + j < head_dim_v) {
|
| 419 |
+
v_next[j] = {{ read_scalar("value", "v_base + j", queryDtype) }};
|
| 420 |
+
}
|
| 421 |
+
}
|
| 422 |
+
{% endfor %}
|
| 423 |
+
{% endmacro %}
|
| 424 |
+
{% if cachedDeltaProjection or prefetchQueries %}
|
| 425 |
+
let q_head_0 = (head_idx * {{ geometryPrefix }}qNumHeads) / {{ geometryPrefix }}kvNumHeads;
|
| 426 |
+
{% endif %}
|
| 427 |
+
{% if cachedDeltaProjection or packedReductions %}
|
| 428 |
+
let out_head_0 = head_idx * heads_per_group;
|
| 429 |
+
{% endif %}
|
| 430 |
+
{% if prefetchQueries and not retimePackedQueries %}
|
| 431 |
+
var q_next: array<vec4<f32>, {{ queryGroups }}>;
|
| 432 |
+
{% elif cachedDeltaProjection %}
|
| 433 |
+
var q0_next = vec4<f32>(0.0);
|
| 434 |
+
{% endif %}
|
| 435 |
+
var k_next = vec4<f32>(0.0);
|
| 436 |
+
{% if usesDecay %}
|
| 437 |
+
var decay_next = vec4<f32>(0.0);
|
| 438 |
+
{% endif %}
|
| 439 |
+
{% if usesBeta %}
|
| 440 |
+
var beta_next = 0.0;
|
| 441 |
+
{% endif %}
|
| 442 |
+
var v_next: array<f32, {{ engineTileV }}>;
|
| 443 |
+
{% if packedReductions and not retimePackedQueries %}
|
| 444 |
+
{% for g in range(deferredFirst, queryGroups) %}
|
| 445 |
+
var q_prev_{{ g }} = vec4<f32>(0.0);
|
| 446 |
+
{% endfor %}
|
| 447 |
+
{% endif %}
|
| 448 |
+
let bt_first = batch_idx * {{ geometryPrefix }}seqLength;
|
| 449 |
+
{{ load_token("bt_first") }}
|
| 450 |
+
for (var t = 0u; t < {{ geometryPrefix }}seqLength; t = t + 1u) {
|
| 451 |
+
let bt = batch_idx * {{ geometryPrefix }}seqLength + t;
|
| 452 |
+
// Snapshot current inputs before publishing the next token's private buffers.
|
| 453 |
+
// Named values keep current-token reads independent of mutable array storage.
|
| 454 |
+
let k_vec = k_next;
|
| 455 |
+
{% if prefetchQueries and not retimePackedQueries %}
|
| 456 |
+
{% for qg in range(queryGroups) %}
|
| 457 |
+
let q_cur_{{ qg }} = q_next[{{ qg }}];
|
| 458 |
+
{% endfor %}
|
| 459 |
+
{% if cachedDeltaProjection and not packedReductions %}
|
| 460 |
+
let q0_vec = q_cur_0;
|
| 461 |
+
{% endif %}
|
| 462 |
+
{% elif cachedDeltaProjection %}
|
| 463 |
+
let q0_vec = q0_next;
|
| 464 |
+
{% endif %}
|
| 465 |
+
{% if usesDecay %}
|
| 466 |
+
let decay_raw = decay_next;
|
| 467 |
+
{% endif %}
|
| 468 |
+
{% if usesBeta %}
|
| 469 |
+
let beta_val = beta_next;
|
| 470 |
+
{% endif %}
|
| 471 |
+
{% for valueIndex in range(engineValues) %}
|
| 472 |
+
let v_cur_{{ valueIndex }} = v_next[{{ valueIndex }}];
|
| 473 |
+
{% endfor %}
|
| 474 |
+
if (t + 1u < {{ geometryPrefix }}seqLength) {
|
| 475 |
+
let bt_next = bt + 1u;
|
| 476 |
+
{{ load_token("bt_next") | indent(2, true) }}
|
| 477 |
+
}
|
| 478 |
+
{% else %}
|
| 479 |
// A token's key and query do not depend on the recurrent state. Fetch t+1
|
| 480 |
// before completing the reductions for t so the independent load is issued
|
| 481 |
// ahead of its first use.
|
| 482 |
{% macro load_key(token) %}
|
| 483 |
if (lane_active) {
|
| 484 |
+
let k_base = ({{ token }} * {{ geometryPrefix }}kPackedDim + key_head_idx * head_dim_k) / VEC4_LANES + lane;
|
| 485 |
k_next = vec4<f32>(key[k_base]);
|
| 486 |
}{% endmacro %}
|
| 487 |
+
{% if cachedDeltaProjection %}
|
| 488 |
{% macro load_query0(token) %}
|
| 489 |
if (lane_active) {
|
| 490 |
+
let q0_base = ({{ token }} * {{ geometryPrefix }}qPackedDim + q_head_0 * head_dim_k) / VEC4_LANES + lane;
|
| 491 |
q0_next = vec4<f32>(query[q0_base]);
|
| 492 |
}{% endmacro %}
|
| 493 |
+
let q_head_0 = (head_idx * {{ geometryPrefix }}qNumHeads) / {{ geometryPrefix }}kvNumHeads;
|
| 494 |
let out_head_0 = head_idx * heads_per_group;
|
| 495 |
var q0_next = vec4<f32>(0.0);
|
| 496 |
{% endif %}
|
| 497 |
var k_next = vec4<f32>(0.0);
|
| 498 |
+
let bt_first = batch_idx * {{ geometryPrefix }}seqLength;
|
| 499 |
{{ load_key("bt_first") }}
|
| 500 |
+
{% if cachedDeltaProjection %}
|
| 501 |
{{ load_query0("bt_first") }}
|
| 502 |
{% endif %}
|
| 503 |
+
for (var t = 0u; t < {{ geometryPrefix }}seqLength; t = t + 1u) {
|
| 504 |
+
let bt = batch_idx * {{ geometryPrefix }}seqLength + t;
|
| 505 |
|
| 506 |
let k_vec = k_next;
|
| 507 |
+
{% if cachedDeltaProjection %}
|
| 508 |
let q0_vec = q0_next;
|
| 509 |
{% endif %}
|
| 510 |
// Issued here, consumed on the next trip.
|
| 511 |
let bt_next = bt + 1u;
|
| 512 |
+
if (t + 1u < {{ geometryPrefix }}seqLength) {
|
| 513 |
{{ load_key("bt_next") }}
|
| 514 |
+
{% if cachedDeltaProjection %}
|
| 515 |
{{ load_query0("bt_next") }}
|
| 516 |
{% endif %}
|
| 517 |
}
|
| 518 |
+
{% endif %}
|
| 519 |
+
{% if packedReductions and deferredGroups > 0 %}
|
| 520 |
+
// The previous token's remaining heads project its stored state, read here
|
| 521 |
+
// before this token decays it; their sums join this token's reduction round.
|
| 522 |
+
{% for g in range(deferredFirst, queryGroups) %}
|
| 523 |
+
{% if retimePackedQueries %}
|
| 524 |
+
// Read the immutable query only when its completed state is projected.
|
| 525 |
+
// No current/prefetched/previous query vector crosses an iteration.
|
| 526 |
+
var previous_query_{{ g }} = vec4<f32>(0.0);
|
| 527 |
+
if (t > 0u && lane_active) {
|
| 528 |
+
let previous_base_{{ g }} = ((bt - 1u) * {{ geometryPrefix }}qPackedDim + (q_head_0 + {{ g }}u) * head_dim_k) / VEC4_LANES + lane;
|
| 529 |
+
previous_query_{{ g }} = vec4<f32>(query[previous_base_{{ g }}]);
|
| 530 |
+
}
|
| 531 |
+
{% endif %}
|
| 532 |
+
{% for j in range(engineValues) %}
|
| 533 |
+
let deferred{{ g }}_{{ j }} = dot(state[{{ j }}], {% if retimePackedQueries %}previous_query_{{ g }}{% else %}q_prev_{{ g }}{% endif %});
|
| 534 |
+
{% endfor %}
|
| 535 |
+
{% endfor %}
|
| 536 |
+
{% endif %}
|
| 537 |
{% if usesDecay %}
|
| 538 |
+
{% if prefetchTokenInputs %}
|
| 539 |
var decay_vec = vec4<f32>(1.0);
|
| 540 |
+
if ({{ geometryPrefix }}decayPackedDim == {{ geometryPrefix }}kvNumHeads) {
|
| 541 |
+
let factor = exp(decay_raw.x);
|
| 542 |
+
decay_vec = vec4<f32>(factor);
|
| 543 |
} else if (lane_active) {
|
| 544 |
+
decay_vec = vec4<f32>(exp(decay_raw.x), exp(decay_raw.y), exp(decay_raw.z), exp(decay_raw.w));
|
| 545 |
+
}{% else %}
|
| 546 |
+
|
| 547 |
+
{% set perHeadDecay = static_decayPackedDim == static_kvNumHeads %}
|
| 548 |
+
{% if not perHeadDecay %}
|
| 549 |
+
var decay_vec = vec4<f32>(1.0);
|
| 550 |
+
{% endif %}
|
| 551 |
+
{% if perHeadDecay %}
|
| 552 |
+
let factor = exp({{ read_scalar("decay", "bt * " ~ geometryPrefix ~ "kvNumHeads + head_idx", queryDtype) }});
|
| 553 |
+
let decay_vec = vec4<f32>(factor);
|
| 554 |
+
{% endif %}
|
| 555 |
+
{% if not perHeadDecay %}if (lane_active) {
|
| 556 |
+
let decay_base = bt * {{ geometryPrefix }}decayPackedDim + head_idx * head_dim_k + dk_base;
|
| 557 |
decay_vec = vec4<f32>(
|
| 558 |
exp({{ read_scalar("decay", "decay_base + 0u", queryDtype) }}),
|
| 559 |
exp({{ read_scalar("decay", "decay_base + 1u", queryDtype) }}),
|
| 560 |
exp({{ read_scalar("decay", "decay_base + 2u", queryDtype) }}),
|
| 561 |
exp({{ read_scalar("decay", "decay_base + 3u", queryDtype) }}),
|
| 562 |
);
|
| 563 |
+
}{% endif %}
|
| 564 |
+
{% endif %}
|
| 565 |
+
{% for valueIndex in range(engineValues if staticValueLoops else 1) %}
|
| 566 |
+
{{ value_loop_open(valueIndex, staticValueLoops, engineTileV) | indent(4, true) }}
|
| 567 |
state[j] = state[j] * decay_vec;
|
| 568 |
}
|
| 569 |
+
{% endfor %}
|
| 570 |
|
| 571 |
{% endif %}
|
| 572 |
+
{% if packedReductions %}
|
| 573 |
+
{% set currentCount = ((1 + 2 * engineValues) if cachedDeltaProjection else engineValues) if usesBeta else 0 %}
|
| 574 |
+
{% set packedCount = currentCount + deferredGroups * engineValues %}
|
| 575 |
+
{% set currentOutputs = engineValues if cachedDeltaProjection else 0 %}
|
| 576 |
+
{% if directStateProjection %}
|
| 577 |
+
// Retrieve current corrections while projecting the previous token's
|
| 578 |
+
// updated physical state. No large k.q intermediate is formed.
|
| 579 |
+
{% else %}
|
| 580 |
+
// One round of four-wide reductions per token. The delta rules take k.q, the key
|
| 581 |
+
// projection and the first head's projection of the decayed state, whose output
|
| 582 |
+
// is (S + k d^T)^T q = S^T q + d (k . q); every other head projects the stored
|
| 583 |
+
// state one token later.
|
| 584 |
+
{% endif %}
|
| 585 |
+
{% for v in range(((packedCount + 3) / 4) | int) %}
|
| 586 |
+
let packed{{ v }} = subgroupAdd(vec4<f32>({% for c in range(4) %}{{ packed_term(v * 4 + c, currentCount, packedCount) }}{{ ", " if not loop.last else "" }}{% endfor %}));
|
| 587 |
+
{% endfor %}
|
| 588 |
+
{% for j in range(engineValues) %}
|
| 589 |
{% if usesBeta %}
|
| 590 |
+
let delta{{ j }} = select(0.0, beta_val * (v_cur_{{ j }} - {{ packed_ref((1 + 2 * j) if cachedDeltaProjection else j) }}), dv_start + {{ j }}u < head_dim_v);
|
| 591 |
+
{% else %}
|
| 592 |
+
let delta{{ j }} = select(0.0, v_cur_{{ j }}, dv_start + {{ j }}u < head_dim_v);
|
| 593 |
+
{% endif %}
|
| 594 |
+
{% endfor %}
|
| 595 |
+
{% if directStateProjection %}
|
| 596 |
+
// Every head publishes the previous token's updated-state projection;
|
| 597 |
+
// the final token is projected after the loop.
|
| 598 |
+
{% else %}
|
| 599 |
+
// Lanes below {{ currentOutputs }} store this token's first head; the next
|
| 600 |
+
// {{ deferredGroups * engineValues }} store the previous token's other heads.
|
| 601 |
+
{% endif %}
|
| 602 |
+
var out_val = {{ packed_output(0, currentCount, currentOutputs) }};
|
| 603 |
+
{% for o in range(1, queryGroups * engineValues) %}
|
| 604 |
+
out_val = select(out_val, {{ packed_output(o, currentCount, currentOutputs) }}, lane == {{ o }}u);
|
| 605 |
+
{% endfor %}
|
| 606 |
+
{% if inlineStateRepair and cachedDeltaProjection %}
|
| 607 |
+
let repair_value = lane < {{ engineValues }}u && !linear_attention_finite(out_val * scale);
|
| 608 |
+
var repair_first = false;
|
| 609 |
+
{% for j in range(engineValues) %}
|
| 610 |
+
repair_first = repair_first || !linear_attention_finite(({{ packed_ref(2 + 2 * j) }} + delta{{ j }} * {{ packed_ref(0) }}) * scale);
|
| 611 |
+
{% endfor %}
|
| 612 |
+
{% endif %}
|
| 613 |
+
if (lane < {{ queryGroups * engineValues }}u) {
|
| 614 |
+
let deferred_lane = lane >= {{ currentOutputs }}u;
|
| 615 |
+
let out_slot = select(lane, lane - {{ currentOutputs }}u, deferred_lane);
|
| 616 |
+
let out_column = out_slot % {{ engineValues }}u;
|
| 617 |
+
let out_group = select(0u, {{ deferredFirst }}u + out_slot / {{ engineValues }}u, deferred_lane);
|
| 618 |
+
let out_row = select(bt, bt - 1u, deferred_lane);
|
| 619 |
+
if ((!deferred_lane || t > 0u) && dv_start + out_column < head_dim_v) {
|
| 620 |
+
output[out_row * packed_out + (out_head_0 + out_group) * head_dim_v + dv_start + out_column] = {{ write_scalar("out_val * scale", queryDtype) }};
|
| 621 |
+
}
|
| 622 |
+
}
|
| 623 |
+
{% for j in range(engineValues) %}
|
| 624 |
+
state[{{ j }}] = state[{{ j }}] + k_vec * delta{{ j }};
|
| 625 |
+
{% endfor %}
|
| 626 |
+
{% if inlineStateRepair and cachedDeltaProjection %}
|
| 627 |
+
if (repair_first) {
|
| 628 |
+
// Reload only on the Inf/NaN repair path, keeping current-query vectors and
|
| 629 |
+
// cached projections out of the updated state's normal live range.
|
| 630 |
+
var repair_query = vec4<f32>(0.0);
|
| 631 |
+
if (lane_active) {
|
| 632 |
+
let repair_query_base = (bt * {{ geometryPrefix }}qPackedDim + q_head_0 * head_dim_k) / VEC4_LANES + lane;
|
| 633 |
+
repair_query = vec4<f32>(query[repair_query_base]);
|
| 634 |
+
}
|
| 635 |
+
{% for j in range(engineValues) %}
|
| 636 |
+
let repaired{{ j }} = linear_attention_project_state(state[{{ j }}], repair_query) * scale;
|
| 637 |
+
if (lane == {{ j }}u && dv_start + {{ j }}u < head_dim_v && repair_value && linear_attention_finite(repaired{{ j }})) {
|
| 638 |
+
output[bt * packed_out + out_head_0 * head_dim_v + dv_start + {{ j }}u] = {{ write_scalar("repaired" ~ j, queryDtype) }};
|
| 639 |
+
}
|
| 640 |
+
{% endfor %}
|
| 641 |
+
}
|
| 642 |
+
{% endif %}
|
| 643 |
+
{% if not retimePackedQueries %}
|
| 644 |
+
{% for g in range(deferredFirst, queryGroups) %}
|
| 645 |
+
q_prev_{{ g }} = q_cur_{{ g }};
|
| 646 |
+
{% endfor %}
|
| 647 |
+
{% endif %}
|
| 648 |
+
{% elif usesBeta %}
|
| 649 |
+
var ret_vals: array<f32, {{ engineTileV }}>;
|
| 650 |
+
{% if cachedDeltaProjection %}
|
| 651 |
+
var pre_vals: array<f32, {{ engineTileV }}>;
|
| 652 |
+
{% endif %}
|
| 653 |
{% if useSubgroups %}
|
| 654 |
+
{% if cachedDeltaProjection %}
|
| 655 |
let kq_dot = subgroupAdd(dot(k_vec, q0_vec));
|
| 656 |
+
{% endif %}
|
| 657 |
+
{{ value_loop_open(0, false, engineTileV) | indent(4, true) }}
|
| 658 |
ret_vals[j] = subgroupAdd(dot(state[j], k_vec));
|
| 659 |
+
{% if cachedDeltaProjection %}
|
| 660 |
pre_vals[j] = subgroupAdd(dot(state[j], q0_vec));
|
| 661 |
+
{% endif %}
|
| 662 |
}
|
| 663 |
+
{% elif compactFold %}
|
| 664 |
+
{% if cachedDeltaProjection %}
|
| 665 |
+
let kq_dot = linearAttentionVec4Fold(dot(k_vec, q0_vec), tid, lane, dv_group);
|
| 666 |
+
{% endif %}
|
| 667 |
+
ret_vals[0] = linearAttentionVec4Fold(dot(state[0], k_vec), tid, lane, dv_group);
|
| 668 |
+
{% if cachedDeltaProjection %}
|
| 669 |
+
pre_vals[0] = linearAttentionVec4Fold(dot(state[0], q0_vec), tid, lane, dv_group);
|
| 670 |
+
{% endif %}
|
| 671 |
{% else %}
|
| 672 |
// kq and both state projections are known before any of them is consumed, so all
|
| 673 |
// three stage together and cross one barrier pair rather than two.
|
| 674 |
{% for v in range(redVecs) %}
|
| 675 |
wg_fold[{{ v }}u * WG + tid] = {{ redT }}({% for c in range(redWidth) %}dot(state[{{ v * redWidth + c }}], k_vec){{ ", " if not loop.last else "" }}{% endfor %});
|
| 676 |
+
{% if cachedDeltaProjection %}
|
| 677 |
wg_fold[{{ redVecs + v }}u * WG + tid] = {{ redT }}({% for c in range(redWidth) %}dot(state[{{ v * redWidth + c }}], q0_vec){{ ", " if not loop.last else "" }}{% endfor %});
|
| 678 |
+
{% endif %}
|
| 679 |
{% endfor %}
|
| 680 |
+
{% if cachedDeltaProjection %}
|
| 681 |
wg_fold[{{ 2 * redVecs }}u * WG + tid] = {{ redT }}(dot(k_vec, q0_vec){% for c in range(redWidth - 1) %}, 0.0{% endfor %});
|
| 682 |
+
{% endif %}
|
| 683 |
workgroupBarrier();
|
| 684 |
for (var sl = lane; sl < {{ redSlots }}u; sl = sl + LANES) {
|
| 685 |
var t = {{ redT }}(0.0);
|
|
|
|
| 688 |
}
|
| 689 |
wg_fold_out[dv_group * {{ redSlots }}u + sl] = t;
|
| 690 |
}
|
| 691 |
+
{% if inlineStateRepair and cachedDeltaProjection %}
|
| 692 |
+
let repair_kq = workgroupUniformLoad(&wg_fold_out[{{ 2 * redVecs }}u]){{ ("." ~ redComps[0]) if redWidth > 1 else "" }};
|
| 693 |
+
let repair_first = !linear_attention_finite(repair_kq);
|
| 694 |
+
{% else %}
|
| 695 |
workgroupBarrier();
|
| 696 |
+
{% endif %}
|
| 697 |
+
{% if cachedDeltaProjection %}
|
| 698 |
let kq_dot = wg_fold_out[dv_group * {{ redSlots }}u + {{ 2 * redVecs }}u]{{ ("." ~ redComps[0]) if redWidth > 1 else "" }};
|
| 699 |
+
{% endif %}
|
| 700 |
+
{% for j in range(engineValues) %}
|
| 701 |
ret_vals[{{ j }}] = wg_fold_out[dv_group * {{ redSlots }}u + {{ (j / redWidth)|int }}u]{{ ("." ~ redComps[j % redWidth]) if redWidth > 1 else "" }};
|
| 702 |
+
{% if cachedDeltaProjection %}
|
| 703 |
pre_vals[{{ j }}] = wg_fold_out[dv_group * {{ redSlots }}u + {{ redVecs + (j / redWidth)|int }}u]{{ ("." ~ redComps[j % redWidth]) if redWidth > 1 else "" }};
|
| 704 |
+
{% endif %}
|
| 705 |
{% endfor %}
|
| 706 |
{% endif %}
|
| 707 |
|
| 708 |
+
{% if not prefetchTokenInputs %}
|
| 709 |
+
{% if static_betaPackedDim == 1 %}
|
| 710 |
+
let beta_idx = bt;
|
| 711 |
+
{% else %}
|
| 712 |
+
let beta_idx = bt * {{ geometryPrefix }}kvNumHeads + head_idx;
|
| 713 |
+
{% endif %}
|
| 714 |
let beta_val = {{ read_scalar("beta", "beta_idx", queryDtype) }};
|
| 715 |
+
let v_base = bt * {{ geometryPrefix }}vPackedDim + head_idx * head_dim_v + dv_start;
|
| 716 |
+
{% endif %}
|
| 717 |
+
{% if cachedDeltaProjection %}
|
| 718 |
let out_base = bt * packed_out + out_head_0 * head_dim_v + dv_start;
|
| 719 |
+
var deltas: array<f32, {{ engineTileV }}>;
|
| 720 |
+
{% if inlineStateRepair %}
|
| 721 |
+
var repair_values: array<bool, {{ engineTileV }}>;
|
| 722 |
+
{% endif %}
|
| 723 |
+
{% endif %}
|
| 724 |
+
{{ value_loop_open(0, false, engineTileV) | indent(4, true) }}
|
| 725 |
if (dv_start + j < head_dim_v) {
|
| 726 |
+
{{ read_current_value(engineValues) | trim | indent(8, true) }}
|
| 727 |
let delta_j = beta_val * (v_val - ret_vals[j]);
|
| 728 |
+
{% if cachedDeltaProjection %}
|
| 729 |
deltas[j] = delta_j;
|
| 730 |
+
{% if inlineStateRepair %}
|
| 731 |
+
let cached = (pre_vals[j] + delta_j * kq_dot) * scale;
|
| 732 |
+
repair_values[j] = !linear_attention_finite(cached);
|
| 733 |
+
if (lane == 0u) {
|
| 734 |
+
output[out_base + j] = {{ write_scalar("cached", queryDtype) }};
|
| 735 |
+
}
|
| 736 |
+
{% else %}
|
| 737 |
if (lane == 0u) {
|
| 738 |
output[out_base + j] = {{ write_scalar("(pre_vals[j] + delta_j * kq_dot) * scale", queryDtype) }};
|
| 739 |
}
|
| 740 |
+
{% endif %}
|
| 741 |
} else {
|
| 742 |
deltas[j] = 0.0;
|
| 743 |
+
{% if inlineStateRepair %}
|
| 744 |
+
repair_values[j] = false;
|
| 745 |
+
{% endif %}
|
| 746 |
+
{% else %}
|
| 747 |
+
state[j] = state[j] + k_vec * delta_j;
|
| 748 |
+
{% endif %}
|
| 749 |
}
|
| 750 |
}
|
| 751 |
|
| 752 |
+
{% if inlineStateRepair and cachedDeltaProjection and useSubgroups %}
|
| 753 |
+
var repair_first = false;
|
| 754 |
+
{{ value_loop_open(0, false, engineTileV) | indent(4, true) }}
|
| 755 |
+
repair_first = repair_first || repair_values[j];
|
| 756 |
+
}
|
| 757 |
+
{% endif %}
|
| 758 |
+
{% if cachedDeltaProjection %}
|
| 759 |
+
{{ value_loop_open(0, false, engineTileV) | indent(4, true) }}
|
| 760 |
state[j] = state[j] + k_vec * deltas[j];
|
| 761 |
}
|
| 762 |
+
{% endif %}
|
| 763 |
|
| 764 |
+
{% if inlineStateRepair and cachedDeltaProjection %}
|
| 765 |
+
if (repair_first) {
|
| 766 |
+
{{ emit_vec4_query_groups(0, useSubgroups, engineTileV, compactFold, prefetchQueries, true) | indent(2, true) }}
|
| 767 |
+
}
|
| 768 |
+
{% endif %}
|
| 769 |
+
{{ emit_vec4_query_groups(1 if cachedDeltaProjection else 0, useSubgroups, engineTileV, compactFold, prefetchQueries) }}
|
| 770 |
{% else %}
|
| 771 |
+
{% if not prefetchTokenInputs %}
|
| 772 |
+
let v_base = bt * {{ geometryPrefix }}vPackedDim + head_idx * head_dim_v + dv_start;
|
| 773 |
+
{% endif %}
|
| 774 |
+
{{ value_loop_open(0, false, engineTileV) | indent(4, true) }}
|
| 775 |
if (dv_start + j < head_dim_v) {
|
| 776 |
+
{{ read_current_value(engineValues) | trim | indent(8, true) }}
|
| 777 |
state[j] = state[j] + k_vec * v_val;
|
| 778 |
}
|
| 779 |
}
|
| 780 |
|
| 781 |
+
{{ emit_vec4_query_groups(0, useSubgroups, engineTileV, compactFold, prefetchQueries) }}
|
| 782 |
{% endif %}
|
| 783 |
{% if hasStateWindow %}
|
| 784 |
// Slot j holds the state after token (seqLength - stateWindow + j), so this
|
| 785 |
// token owns slot (t + stateWindow - seqLength) when that lands in the window.
|
| 786 |
+
if (t + {{ geometryPrefix }}stateWindow >= {{ geometryPrefix }}seqLength && lane_active) {
|
| 787 |
+
let win_base = (t + {{ geometryPrefix }}stateWindow - {{ geometryPrefix }}seqLength) * {{ geometryPrefix }}stateSlotStride
|
| 788 |
+
+ ((batch_idx * {{ geometryPrefix }}kvNumHeads + head_idx) * head_dim_k + dk_base) * head_dim_v + dv_start;
|
| 789 |
+
{% for valueIndex in range(engineValues if staticValueLoops else 1) %}
|
| 790 |
+
{{ value_loop_open(valueIndex, staticValueLoops, engineTileV) | indent(6, true) }}
|
| 791 |
if (dv_start + j < head_dim_v) {
|
| 792 |
present_state[win_base + 0u * head_dim_v + j] = {{ write_scalar("state[j].x", stateDtype) }};
|
| 793 |
present_state[win_base + 1u * head_dim_v + j] = {{ write_scalar("state[j].y", stateDtype) }};
|
|
|
|
| 795 |
present_state[win_base + 3u * head_dim_v + j] = {{ write_scalar("state[j].w", stateDtype) }};
|
| 796 |
}
|
| 797 |
}
|
| 798 |
+
{% endfor %}
|
| 799 |
}
|
| 800 |
{% endif %}
|
| 801 |
}
|
| 802 |
+
{% if packedReductions and deferredGroups > 0 %}
|
| 803 |
+
// The last token's remaining heads project the final state.
|
| 804 |
+
{% if retimePackedQueries %}
|
| 805 |
+
{% for g in range(deferredFirst, queryGroups) %}
|
| 806 |
+
var final_query_{{ g }} = vec4<f32>(0.0);
|
| 807 |
+
if ({{ geometryPrefix }}seqLength > 0u && lane_active) {
|
| 808 |
+
let final_base_{{ g }} = ((batch_idx * {{ geometryPrefix }}seqLength + {{ geometryPrefix }}seqLength - 1u) * {{ geometryPrefix }}qPackedDim + (q_head_0 + {{ g }}u) * head_dim_k) / VEC4_LANES + lane;
|
| 809 |
+
final_query_{{ g }} = vec4<f32>(query[final_base_{{ g }}]);
|
| 810 |
+
}
|
| 811 |
+
{% endfor %}
|
| 812 |
+
{% endif %}
|
| 813 |
+
{% for v in range(((deferredGroups * engineValues + 3) / 4) | int) %}
|
| 814 |
+
let final{{ v }} = subgroupAdd(vec4<f32>({% for c in range(4) %}{% set slot = v * 4 + c %}{% if slot < deferredGroups * engineValues %}dot(state[{{ slot % engineValues }}], {% if retimePackedQueries %}final_query_{{ deferredFirst + (slot / engineValues) | int }}{% else %}q_prev_{{ deferredFirst + (slot / engineValues) | int }}{% endif %}){% else %}0.0{% endif %}{{ ", " if not loop.last else "" }}{% endfor %}));
|
| 815 |
+
{% endfor %}
|
| 816 |
+
var final_val = final0.x;
|
| 817 |
+
{% for o in range(1, deferredGroups * engineValues) %}
|
| 818 |
+
final_val = select(final_val, final{{ (o / 4) | int }}.{{ ["x", "y", "z", "w"][o % 4] }}, lane == {{ o }}u);
|
| 819 |
+
{% endfor %}
|
| 820 |
+
if ({{ geometryPrefix }}seqLength > 0u && lane < {{ deferredGroups * engineValues }}u && dv_start + lane % {{ engineValues }}u < head_dim_v) {
|
| 821 |
+
let final_row = batch_idx * {{ geometryPrefix }}seqLength + {{ geometryPrefix }}seqLength - 1u;
|
| 822 |
+
output[final_row * packed_out + (out_head_0 + {{ deferredFirst }}u + lane / {{ engineValues }}u) * head_dim_v + dv_start + lane % {{ engineValues }}u] = {{ write_scalar("final_val * scale", queryDtype) }};
|
| 823 |
+
}
|
| 824 |
+
{% endif %}
|
| 825 |
|
| 826 |
{% if not hasStateWindow %}
|
| 827 |
if (lane_active) {
|
| 828 |
+
let state_row_base = ((batch_idx * {{ geometryPrefix }}kvNumHeads + head_idx) * head_dim_k + dk_base) * head_dim_v + dv_start;
|
| 829 |
+
{% for valueIndex in range(engineValues if staticValueLoops else 1) %}
|
| 830 |
+
{{ value_loop_open(valueIndex, staticValueLoops, engineTileV) | indent(4, true) }}
|
| 831 |
if (dv_start + j < head_dim_v) {
|
| 832 |
present_state[state_row_base + 0u * head_dim_v + j] = {{ write_scalar("state[j].x", stateDtype) }};
|
| 833 |
present_state[state_row_base + 1u * head_dim_v + j] = {{ write_scalar("state[j].y", stateDtype) }};
|
|
|
|
| 835 |
present_state[state_row_base + 3u * head_dim_v + j] = {{ write_scalar("state[j].w", stateDtype) }};
|
| 836 |
}
|
| 837 |
}
|
| 838 |
+
{% endfor %}
|
| 839 |
+
}
|
| 840 |
+
{% endif %}
|
| 841 |
+
{% endmacro %}
|
| 842 |
+
{% if useSubgroups %}
|
| 843 |
+
fn linearAttentionVec4Portable(wg: vec3<u32>, tid: u32, valueOffset: u32) {
|
| 844 |
+
let lane = tid % LANES;
|
| 845 |
+
let dv_group = tid / LANES;
|
| 846 |
+
{{ recurrence(false, false, "1u", 1, true) }}
|
| 847 |
+
}
|
| 848 |
+
{% endif %}
|
| 849 |
+
@compute @workgroup_size(WG, 1, 1){% if vec4PinSize > 0 %} @subgroup_size({{ vec4PinSize }}){% endif %}
|
| 850 |
+
fn main(
|
| 851 |
+
@builtin(workgroup_id) wg: vec3<u32>,
|
| 852 |
+
@builtin(local_invocation_index) tid: u32,
|
| 853 |
+
{% if useSubgroups %}
|
| 854 |
+
@builtin(subgroup_invocation_id) sgLane: u32,
|
| 855 |
+
{% endif %}
|
| 856 |
+
) {
|
| 857 |
+
{% if scanOutputBeforeReplay %}{{ emit_linear_attention_output_scan() }}{% endif %}{% if useSubgroups %}
|
| 858 |
+
let L = subgroup_logical_lanes(sgLane);
|
| 859 |
+
if (subgroupElect() && L.count != LANES) {
|
| 860 |
+
atomicStore(&vec4Rejected, 1u);
|
| 861 |
+
}
|
| 862 |
+
workgroupBarrier();
|
| 863 |
+
if (tid == 0u) {
|
| 864 |
+
vec4Accepted = select(0u, 1u, atomicLoad(&vec4Rejected) == 0u);
|
| 865 |
+
}
|
| 866 |
+
if (workgroupUniformLoad(&vec4Accepted) == 0u) {
|
| 867 |
+
// The portable local-id lane groups cover each value and K component once.
|
| 868 |
+
// Reusing one fold bank per value bounds fallback storage independently of TILE_V.
|
| 869 |
+
for (var valueOffset = 0u; valueOffset < TILE_V; valueOffset += 1u) {
|
| 870 |
+
linearAttentionVec4Portable(wg, tid, valueOffset);
|
| 871 |
+
}
|
| 872 |
+
return;
|
| 873 |
}
|
| 874 |
+
let lane = L.rank;
|
| 875 |
+
let dv_group = L.ord;
|
| 876 |
+
{% else %}
|
| 877 |
+
let lane = tid % LANES;
|
| 878 |
+
let dv_group = tid / LANES;
|
| 879 |
{% endif %}
|
| 880 |
+
{{ recurrence(useSubgroups, staticValueLoops, "TILE_V", tileV, false) }}
|
| 881 |
}
|
build/webgpu/manifest.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
build/webgpu/metadata.json
CHANGED
|
@@ -1,56 +1 @@
|
|
| 1 |
-
{
|
| 2 |
-
"name": "com.microsoft.LinearAttention",
|
| 3 |
-
"id": "_com_microsoft_linearattention_webgpu_f649015",
|
| 4 |
-
"version": 1,
|
| 5 |
-
"license": "Apache-2.0",
|
| 6 |
-
"backend": { "type": "webgpu" },
|
| 7 |
-
"digest": {
|
| 8 |
-
"algorithm": "sha256",
|
| 9 |
-
"files": {
|
| 10 |
-
"bench.json": "SADv8TDNnXFGZdNUCqGMV5D9W9IKy2+gSRRNOZvxh/4=",
|
| 11 |
-
"chunk-out.wgsl.jinja": "NtmWHwJETU/jVQ4S7IhzVPTMr5/EIgigBd9vXPff+X4=",
|
| 12 |
-
"chunk-prep.wgsl.jinja": "EFWTZh76y5wM+swu+IaId+L491AVXqYz2QbzwKIc+Oc=",
|
| 13 |
-
"chunk-scan.wgsl.jinja": "Jexma8+n/iicl/obGiEGFg8+et86bGJZV7CXvXemrtA=",
|
| 14 |
-
"chunk-ut.wgsl.jinja": "I5ALen2vUhua450yTy5D1/aoUk+c32yDnOYRiBxkRrM=",
|
| 15 |
-
"linear-attention.scalar.wgsl.jinja": "NVuNbrDhuK97SfPabpsEUBJhQ6PXf8cKpTa7O62BZss=",
|
| 16 |
-
"linear-attention.serial.wgsl.jinja": "LOksC9OSD5RnpMPXqtEFWi37cOPlUnEHtn5es2B0bOw=",
|
| 17 |
-
"linear-attention.vec4.wgsl.jinja": "7KHl8IX+MVeQWq9dA97PSIJb8FKm3JgyDnuVndvg7fk=",
|
| 18 |
-
"manifest.json": "VLl0IELx7QOCW+y935REgTG7VUHQF5o/Up17IFamO/8=",
|
| 19 |
-
"test.json": "2sKl1HvKpMz2KteF7M+SRa+Csr/a4gw2ZyHqwA4Ow44="
|
| 20 |
-
}
|
| 21 |
-
},
|
| 22 |
-
"provenance": { "kernel": { "sha": "6fdf6301e2bbcc2f03bf1eaf493b7ad55ef33afc", "dirty": false } },
|
| 23 |
-
"webgpu": {
|
| 24 |
-
"manifestSpec": "2.1",
|
| 25 |
-
"variants": {
|
| 26 |
-
"linear_zero_chunked": ["chunk-out.wgsl.jinja", "chunk-scan.wgsl.jinja"],
|
| 27 |
-
"linear_zero_scalar": ["linear-attention.scalar.wgsl.jinja"],
|
| 28 |
-
"linear_zero_vec4": ["linear-attention.vec4.wgsl.jinja"],
|
| 29 |
-
"linear_state_chunked": ["chunk-out.wgsl.jinja", "chunk-scan.wgsl.jinja"],
|
| 30 |
-
"linear_state_scalar": ["linear-attention.scalar.wgsl.jinja"],
|
| 31 |
-
"linear_state_vec4": ["linear-attention.vec4.wgsl.jinja"],
|
| 32 |
-
"gated_zero_chunked": ["chunk-out.wgsl.jinja", "chunk-prep.wgsl.jinja", "chunk-scan.wgsl.jinja"],
|
| 33 |
-
"gated_zero_scalar": ["linear-attention.scalar.wgsl.jinja"],
|
| 34 |
-
"gated_zero_vec4": ["linear-attention.vec4.wgsl.jinja"],
|
| 35 |
-
"gated_state_chunked": ["chunk-out.wgsl.jinja", "chunk-prep.wgsl.jinja", "chunk-scan.wgsl.jinja"],
|
| 36 |
-
"gated_state_scalar": ["linear-attention.scalar.wgsl.jinja"],
|
| 37 |
-
"gated_state_vec4": ["linear-attention.vec4.wgsl.jinja"],
|
| 38 |
-
"delta_zero_chunked": ["chunk-out.wgsl.jinja", "chunk-scan.wgsl.jinja", "chunk-ut.wgsl.jinja"],
|
| 39 |
-
"delta_zero_scalar": ["linear-attention.scalar.wgsl.jinja"],
|
| 40 |
-
"delta_zero_vec4": ["linear-attention.vec4.wgsl.jinja"],
|
| 41 |
-
"delta_state_chunked": ["chunk-out.wgsl.jinja", "chunk-scan.wgsl.jinja", "chunk-ut.wgsl.jinja"],
|
| 42 |
-
"delta_state_scalar": ["linear-attention.scalar.wgsl.jinja"],
|
| 43 |
-
"delta_state_vec4": ["linear-attention.vec4.wgsl.jinja"],
|
| 44 |
-
"gated_delta_zero_chunked": ["chunk-out.wgsl.jinja", "chunk-prep.wgsl.jinja", "chunk-scan.wgsl.jinja", "chunk-ut.wgsl.jinja"],
|
| 45 |
-
"gated_delta_zero_scalar": ["linear-attention.scalar.wgsl.jinja"],
|
| 46 |
-
"gated_delta_zero_vec4": ["linear-attention.vec4.wgsl.jinja"],
|
| 47 |
-
"gated_delta_state_chunked": ["chunk-out.wgsl.jinja", "chunk-prep.wgsl.jinja", "chunk-scan.wgsl.jinja", "chunk-ut.wgsl.jinja"],
|
| 48 |
-
"gated_delta_state_scalar": ["linear-attention.scalar.wgsl.jinja"],
|
| 49 |
-
"gated_delta_state_vec4": ["linear-attention.vec4.wgsl.jinja"],
|
| 50 |
-
"linear_zero_serial_small_dk": ["linear-attention.serial.wgsl.jinja"],
|
| 51 |
-
"linear_state_serial_small_dk": ["linear-attention.serial.wgsl.jinja"],
|
| 52 |
-
"gated_delta_zero_serial_small_dk": ["linear-attention.serial.wgsl.jinja"],
|
| 53 |
-
"gated_delta_state_serial_small_dk": ["linear-attention.serial.wgsl.jinja"]
|
| 54 |
-
}
|
| 55 |
-
}
|
| 56 |
-
}
|
|
|
|
| 1 |
+
{"name":"com.microsoft.LinearAttention","id":"_com_microsoft_linearattention_webgpu_15cc27e","version":1,"license":"Apache-2.0","backend":{"type":"webgpu"},"digest":{"algorithm":"sha256","files":{"bench.json":"OchNkwuxZE51kM5dp9eWyAKc2YKghHn/zBUHYuCjSug=","chunk-out.wgsl.jinja":"t9Kchus+HqECPXKlW4jrj1WBMCJ5Jn1f8rkFeofWfl8=","chunk-prep.wgsl.jinja":"MUxyUUwyXv1fUr5vRSc+QeXKWy3fFay+w7vaDY0Pzi8=","chunk-scan.wgsl.jinja":"9nor3ZC0wetDYgyFPPw5ZBxjfLlCl0/qqP8AyFmSESs=","chunk-ut.wgsl.jinja":"SBnMC9fFduH4npDQ4bufWITNqacc6qicjJfAi/gUZ9g=","linear-attention.scalar.wgsl.jinja":"oaNdJ8zMVnCtjGWffWISgl+sFTPP28ngGd44WfqlLcI=","linear-attention.serial.wgsl.jinja":"7IFl4pCfCR1fZuvvV7SRneoaWFpr8OzUx0NJmqA1pzs=","linear-attention.vec4.wgsl.jinja":"4nPNc7amjXmqju7ehrFXqaZV8V9uzQ+4pDVEu5tbL7A=","manifest.json":"3xbpxVWEJAg8vFTk6tUzK4/GCvRayBDYE3aETDlZpLY=","test.json":"nQESvR6QzJ/tPQ+yZpgmz8N7YFZIZYsTCumOla3AoIg="}},"provenance":{"kernel":{"sha":"4385228f788ddaa1bd7aec6a80a20f3ddffd8011","dirty":false}},"webgpu":{"manifestSpec":"2.2"}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
build/webgpu/test.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|