com.microsoft.QAttention
com.microsoft · ONNX Runtime contrib operator · contrib since_version 1
Description
Fused 8-bit self-attention: an exact int32 QKV projection of a uint8 activation by an int8 or uint8 weight, dequantized by input_scale * weight_scale and biased, followed by masked multi-head attention over the projected heads. Weight columns are laid out [Q | K | V], and per-column weight_scale / weight_zero_point are indexed in that column space. mask_index is additive with mask_filter_value; a unidirectional exclusion overwrites it. Only the uint8-activation, float32 form the reference CPU kernel supports is implemented; do_rotary and a shared past/present buffer are not.
See the ONNX Runtime QAttention contrib-operator spec for the reference semantics.
Differences from the ONNX Runtime spec
- Attribute
mask_filter_value: Sets the value padded keys receive; it has no effect when no key is padded.
Inputs
| Name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|
input |
T1 |
— | — | Quantized activations with shape (batch, sequence, input_hidden_size). The input hidden size may exceed hidden_size when the projection was pruned. |
required |
weight |
T2 |
— | — | Quantized projection weight with shape (input_hidden_size, 3 * hidden_size); the column space is [Q all heads | K all heads | V all heads]. |
required |
bias |
T3 |
— | — | Float bias with shape (3 * hidden_size), added after dequantization in the same column space as weight. |
required |
input_scale |
T3 |
— | — | Per-tensor activation scale; a scalar or a one-element tensor. | required |
weight_scale |
T3 |
— | — | Weight scale: a scalar for per-tensor quantization, or (3 * hidden_size) values indexed in the weight column space for per-column quantization. |
required |
mask_index |
T4 |
— | — | Optional attention mask. Accepted spellings are (batch) end positions, (2 * batch) end positions followed by start positions, a raw (batch, total_sequence_length) key mask, and a raw (batch, sequence, total_sequence_length) mask. Every other spelling is rejected. |
optional |
input_zero_point |
T1 |
— | — | Optional per-tensor activation zero point; a scalar or a one-element tensor. Absent means zero. | optional |
weight_zero_point |
T2 |
— | — | Optional weight zero point: a scalar, or (3 * hidden_size) values indexed in the weight column space. Absent means zero. |
optional |
past |
T3 |
— | — | Optional cached keys and values with shape (2, batch, num_heads, past_sequence_length, head_size); the cached tokens precede the projected ones. |
optional |
Outputs
| Name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|
output |
T3 |
3 |
derived | Attention output with shape (batch, sequence, hidden_size). |
required |
present |
T3 |
5 |
derived | Optional joined keys and values with shape (2, batch, num_heads, past_sequence_length + sequence, head_size). It is required whenever past is supplied. |
optional |
Attributes
Attributes and default values (overridable per request):
| Attribute | Default | Description |
|---|---|---|
do_rotary |
0 |
Rotary position embedding switch. Defaults to 0, and only 0 is accepted: the reference CPU kernel ignores the attribute, so no reference definition of the rotation exists for this operator. |
mask_filter_value |
-10000 |
Value added to the score of a masked-out key. Defaults to -10000.0. |
num_heads |
— | Number of attention heads; hidden_size must be divisible by it. The request must supply it. |
past_present_share_buffer |
— | Kept to match the ONNX Runtime schema. Only 0 is accepted: past and present are distinct tensors with distinct bindings in every variant of this package, so there is no shared buffer for a non-zero value to name. |
scale |
— | Scale applied to the query-key product. When absent or 0, 1 / sqrt(head_size) is used. |
unidirectional |
0 |
When 1, a token attends only to itself and earlier tokens. Defaults to 0. A single-token query is never restricted, matching the reference kernel. |
Type constraints
| Variable | Allowed dtypes |
|---|---|
T1 |
uint8 |
T2 |
uint8, int8 |
T3 |
float32 |
T4 |
int32 |
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.
sgmat— Projects QKV with exact integer products through bounded float32 matrix partials, then dequantizes and scatters into the head-padded working buffers the flash scoring pass reads.packed— Computes exact int32 QKV projections with packed byte dot products or tiled, centered byte operands. Dequantizes and scatters the projections into the head-padded buffers used by the attention pass.tiled_i8— Projects QKV with the general 8-bit subgroup-matrix GEMM over byte-packed input and weight, then dequantizes and scatters into the head-padded working buffers the flash scoring pass reads.sgmat_integer_16x16x16— Experimental16x16x16unsigned-byte matrices project QKV with exactint32zero-point correction. The mask, dequantization, cache and attention passes are shared with the other variants. If the subgroups are not laid out as expected, the kernel uses plain integer arithmetic instead.sgmat_integer_16x16x32— Experimental16x16x32unsigned-byte matrices project QKV with exactint32zero-point correction. The mask, dequantization, cache and attention passes are shared with the other variants. If the subgroups are not laid out as expected, the kernel uses plain integer arithmetic instead.sgmat_integer_16x8x32— Experimental16x8x32unsigned-byte matrices project QKV with exactint32zero-point correction. The mask, dequantization, cache and attention passes are shared with the other variants. If the subgroups are not laid out as expected, the kernel uses plain integer arithmetic instead.
Device requirements
Some implementation variants require subgroup-matrix and subgroups. Other variants run without them; which variant runs also depends on the request's shapes and dtypes.
Files
metadata.json— kernel metadata (id, file digests, manifest spec, provenance)manifest.json— the op contract (source of truth)test.json— correctness casesbench.json— benchmark casesinteger-pack-columns.wgsl.jinjainteger-pack-rows.wgsl.jinjamatrix-shaped-integer-gemm.wgsl.jinjaqattention-attend.wgsl.jinjaqattention-kv-permute.wgsl.jinjaqattention-mask.wgsl.jinjaqattention-project.wgsl.jinjaqattention-qkv-scatter.wgsl.jinjaquant-exact-matrix.wgsl.jinjasubgroup-matrix-gemm.wgsl.jinja
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.QAttention", { version: 1 });
const inputData = new Uint8Array(4);
const weightData = new Int8Array(48);
const biasData = Float32Array.of(2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
const input_scaleData = Float32Array.of(1);
const weight_scaleData = Float32Array.of(1);
const { output } = await kernel({
input: { data: inputData, shape: [1, 1, 4] },
weight: { data: weightData, shape: [4, 12] },
bias: { data: biasData, shape: [12] },
input_scale: { data: input_scaleData, shape: [1] },
weight_scale: { data: weight_scaleData, shape: [1] },
}, {
attrs: { num_heads: 1 },
});
console.log({ output });
- Downloads last month
- -
Requires WebGPU support. See the compatibility table.