com.microsoft.LinearAttention

com.microsoft · ONNX Runtime contrib operator · contrib since_version 1

Description

Recurrent linear attention for packed [B, T, H*D] decode and prefill. It supports all four update rules, standard and inverse GQA, shared-key heads, and rollback states through state_window. Activations and state may independently use float16 or float32; bfloat16 is not implemented. past_state is optional for every update rule and defaults to zeros.

See the ONNX Runtime LinearAttention contrib-operator spec for the reference semantics.

Differences from the ONNX Runtime spec

  • Attribute chunk_size: The schema defines chunk_size as a scheduling hint. Every value is accepted and none changes the result.

Inputs

Name Logical dtype Rank Shape Description Presence
query T 3 — Query vectors with 3D packed shape (B, T, H_q * d_k); heads are packed into the last dimension. required
key T 3 — Key vectors with 3D packed shape (B, T, H_k * d_k), where positive H_k divides H_kv; H_k < H_kv shares each key head across multiple KV-state heads. Keys should be L2-normalized for delta/gated_delta modes. required
value T 3 — Value vectors with 3D packed shape (B, T, H_kv * d_v). required
past_state S derived derived Recurrent state from the previous step with shape (B, H_kv, d_k, d_v), or (W, B, H_kv, d_k, d_v) when state_window = W > 0; defaults to zeros if absent. optional
decay T 3 — Exponential decay gate in log-space with shape (B, T, H_kv * d_k) or (B, T, H_kv); required for gated and gated_delta modes. optional
beta T 3 — Update rate (sigmoid output) with shape (B, T, H_kv) or (B, T, 1); required for delta and gated_delta modes. optional

Outputs

Name Logical dtype Rank Shape Description Presence
output T 3 derived Attention output with 3D packed shape (B, T, max(H_q, H_kv) * d_v). required
present_state S derived derived Updated recurrent state with shape (B, H_kv, d_k, d_v), or (W, B, H_kv, d_k, d_v) when state_window = W > 0. required

Attributes

Attributes and default values (overridable per request):

Attribute Default Description
chunk_size 64 Accepted for schema compatibility; does not affect the result.
kv_num_heads — Number of key/value heads.
q_num_heads — Number of query heads.
scale 0 Scale applied to query-key products. Zero selects 1 / sqrt(d_k).
state_window 0 Number of recent recurrent states retained in present_state, in the supported range 0 to 8; zero returns only the current state.
update_rule "gated_delta" Recurrent update rule: linear, gated, delta, or gated_delta.

Type constraints

Variable Allowed dtypes
T float32, float16
S float32, float16

Implementation variants

One implementation is selected per call from the device capabilities, the request shapes and the dtypes; these notes say what each one covers.

  • vec4_static — Specializes recurrent indexing to tensor geometry while retaining the scalar scale uniform and device-bounded lane layout.
  • serial_small_dk — Small key dimensions keep each state column private across the sequence. Linear updates specialize shape-known indexing and loop bounds, while gated-delta updates retain their uniform-driven recurrence.

Device requirements

Some implementation variants require subgroups. Other variants run without it; which variant runs also depends on the request's shapes and dtypes.

Files

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/com.microsoft.LinearAttention", { version: 1 });

const queryData = Float32Array.of(
  0.16359815, 0.16205266, 0.149098, 0.1309263, 0.1135055, 0.10155348, 0.09775273,
  0.10232112, 0.11300086, 0.12546599, 0.13408697, 0.13293777, 0.11689379, 0.0826538,
  0.029526938, -0.04014262, -0.12099168, -0.20511103, -0.28292736, -0.34438634,
  -0.38028362, -0.3835658, -0.35042152, -0.28100657,
);
const keyData = Float32Array.of(
  0.070980474, 0.023776323, -0.008363308, -0.008291626, 0.032685805, 0.11251383,
  0.21865256, 0.33084837, 0.42576405, 0.48238245, 0.4869145, 0.4360576,
);
const valueData = Float32Array.of(
  0.011000581, -0.053772997, -0.07845871, -0.044196326, 0.048658974, 0.18047206,
  0.31837228, 0.42649853, 0.47720772, 0.45970497, 0.3833686, 0.2747198,
);
const past_stateData = Float32Array.of(
  0.09853272, 0.07704112, 0.05560378, 0.044387434, 0.05080398, 0.07792509, 0.123736635,
  0.18137693, 0.2403211, 0.28829855, 0.31359476, 0.307315, 0.26519224, 0.18860176,
  0.084587306, -0.03511816,
);
const decayData = Float32Array.of(-0.09725125, -0.0639406, -0.08095616);
const betaData = Float32Array.of(0.68809795, 0.7210754, 0.68271667);

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

Requires WebGPU support. See the compatibility table.