ai.onnx.LinearAttention

ai.onnx · standard ONNX operator · ONNX opset ≥ 27

Description

Computes ONNX recurrent linear attention for packed [B, T, H*D] tensors. Standard GQA requires positive H_q divisible by H_kv; each KV head owns one state, and d_v must be positive. Supports all four update rules, optional rank-4 state, and float16 or float32 activations/state; bfloat16 is not implemented. Every positive d_k through 256 is supported; larger four-aligned widths are accepted when their required resources fit the WebGPU limits. Other head widths are unsupported.

See the ONNX LinearAttention spec for the reference semantics.

Differences from the ONNX spec

  • Attribute chunk_size: The schema defines chunk_size as a correctness-neutral scheduling hint. The recurrent kernels accept every integer value without using it to change the result.

Inputs

Name Logical dtype Rank Shape Description Presence
query T 3 — Query vectors with packed shape (B, T, H_q * d_k); H_q must be a positive multiple of H_kv. required
key T 3 — Key vectors with packed shape (B, T, H_kv * d_k); keys should be L2-normalized for the delta update rules. required
value T 3 — Value vectors with packed shape (B, T, H_kv * d_v). required
past_state S 4 derived Optional recurrent state with shape (B, H_kv, d_k, d_v). When absent, the recurrence starts from zero. optional
decay T 3 — Log-space decay with shape (B, T, H_kv * d_k) or (B, T, H_kv). Required only by gated and gated_delta. optional
beta T 3 — Update rate with shape (B, T, H_kv) or (B, T, 1). Required only by delta and gated_delta. optional

Outputs

Name Logical dtype Rank Shape Description Presence
output T 3 derived Attention output with packed shape (B, T, H_q * d_v). required
present_state S 4 derived Recurrent state after the final token, with shape (B, H_kv, d_k, d_v). Its dtype matches past_state when supplied, otherwise it matches query. required

Attributes

Attributes and default values (overridable per request):

Attribute Default Description
chunk_size 64 Correctness-neutral chunk-size hint for parallel prefill implementations. Every integer value has identical semantics.
kv_num_heads — Required positive number of key/value heads.
q_num_heads — Required positive number of query heads; must be a multiple of kv_num_heads.
scale 0 Output scaling factor. Zero selects 1 / sqrt(d_k).
update_rule "gated_delta" Recurrent update rule: linear, gated, delta, or gated_delta.

Type constraints

Variable Allowed dtypes
T float32, float16
S float32, float16

Files

Use with @huggingface/kernels

npm install --save-exact @huggingface/kernels@0.0.1-preview.4

Paste the example into a browser app that resolves npm imports. Serve the app over HTTPS or localhost with WebGPU available.

Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.

The version: 1 option selects the published kernel contract; it is independent of any operator opset, contrib since_version, or model version. It follows the v1 branch as fixes land. To pin exact artifact bytes, pass a 40-character commit revision instead of version.

import { getKernel } from "@huggingface/kernels";

const kernel = await getKernel("webgpu-kernels/ai.onnx.LinearAttention", { version: 1 });

const queryData = Float32Array.of(
  0.17091933, 0.16533019, 0.15063293, 0.1313149, 0.11202312, 0.097040445, 0.08980678,
  0.092533216, 0.10594729, 0.12919523, 0.15991104, 0.19444641, 0.22823927, 0.25628528,
  0.2736664, 0.2760843,
);
const keyData = Float32Array.of(
  0.18795393, 0.18723874, 0.17931889, 0.16589965, 0.14892825, 0.13047631, 0.11261654,
  0.09730019,
);
const valueData = Float32Array.of(
  0.088164635, 0.027964097, -0.030137641, -0.07318393, -0.09076297, -0.0767497,
);
const decayData = Float32Array.of(
  -0.2, -0.17285714, -0.14571428, -0.11857143, -0.09142857, -0.06428572, -0.037142858,
  -0.01,
);
const betaData = Float32Array.of(0.8, 0.6);

const { output, present_state } = await kernel({
  query: { data: queryData, shape: [1, 2, 8] },
  key: { data: keyData, shape: [1, 2, 4] },
  value: { data: valueData, shape: [1, 2, 3] },
  decay: { data: decayData, shape: [1, 2, 4] },
  beta: { data: betaData, shape: [1, 2, 1] },
}, {
  attrs: { q_num_heads: 2, kv_num_heads: 1 },
});
console.log({ output, present_state });
Downloads last month
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.