com.microsoft.DecoderMaskedMultiHeadAttention
com.microsoft · ONNX Runtime contrib operator · contrib since_version 1
Description
Single-token self/cross decode attention without QKV projection. Cross attention reads encoder K/V; self attention appends to a cache allocated at max_sequence_length, of which past_sequence_length rows are filled. Zero mask entries add mask_filter_value. Cross attention can return scaled pre-softmax qk. Negative lengths yield an empty attention range and skip the append; lengths above the capacity are clamped to it. Lengths are range-checked as signed values before use as counts.
See the ONNX Runtime DecoderMaskedMultiHeadAttention contrib-operator spec for the reference semantics.
Differences from the ONNX Runtime spec
- Input
beam_width: Beam-search decoding is not implemented; the beam width is always one. - Input
cache_indirection: The per-position beam remap belongs to beam-search decoding, which is not implemented. - Input
bias: The packed Q/K/V projection bias is not implemented; fold it into the preceding projection, as decoder exports that emit this operator do. - Input
keyT(ONNXkey), inputvalueT(ONNXvalue): Explicit key and value tensors are required; packed QKV input is not supported.
Inputs
| Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|---|
queryT |
query |
T |
3 |
— | Query row for the single decoded token, with shape (batch_size, 1, num_heads * head_size). |
required |
keyT |
key |
T |
— | — | Cross attention: the full encoder keys with shape (batch_size, num_heads, kv_sequence_length, head_size). Self attention: this token's key row with shape (batch_size, 1, num_heads * head_size). |
required |
valueT |
value |
T |
— | — | Cross attention: the full encoder values, shaped like key. Self attention: this token's value row, shaped like key. |
required |
maskIndexT |
mask_index |
M |
2 |
— | Key-padding mask with shape (batch_size, total_sequence_length). A zero entry ADDS mask_filter_value to that key's score; it does not remove the key. |
optional |
attentionBiasT |
attention_bias |
T |
4 |
— | Additive score bias with shape (batch_size or 1, num_heads or 1, 1, total_sequence_length). Self attention only. |
optional |
pastKeyT |
past_key |
T |
4 |
— | Key cache at capacity, with shape (batch_size, num_heads, max_sequence_length, head_size). Its presence selects self attention. |
optional |
pastValueT |
past_value |
T |
4 |
— | Value cache at capacity, shaped like past_key. |
optional |
pastSequenceLengthT |
past_sequence_length |
M |
1 |
— | Live cache length before this token, as a single value. It selects the cache row this token is written to and bounds the key loop, so it is read from tensor data rather than baked. | optional |
Outputs
| Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|---|
outputT |
output |
T |
3 |
[queryT[0], 1, queryT[2]] |
Attention result with shape (batch_size, 1, num_heads * head_size). |
required |
presentKeyT |
present_key |
T |
4 |
same as pastKeyT |
Key cache after appending this token, at capacity. Every row other than the appended one keeps its past value. | optional |
presentValueT |
present_value |
T |
4 |
same as pastValueT |
Value cache after appending this token, shaped like present_key. |
optional |
qkT |
qk |
QK |
4 |
[queryT[0], num_heads, 1, keyT[2]] |
Scaled Q * K^T with shape (batch_size, num_heads, 1, kv_sequence_length), taken after the mask and BEFORE the softmax despite the schema calling it normalized. Cross attention only. |
optional |
Attributes
Attributes and default values (overridable per request):
| Attribute | Default | Description |
|---|---|---|
mask_filter_value |
-10000 |
Value added to the score of a key whose mask entry is zero. |
num_heads |
— | Number of attention heads. The head size is the query hidden size divided by this count. |
output_qk |
— | Set to 1 to emit the pre-softmax qk output. It must agree with whether the caller supplies that output. |
past_present_share_buffer |
— | Set to 1 when the past and present caches are one buffer allocated at max_sequence_length. Self attention requires it; cross attention has no cache and leaves it at 0. |
scale |
— | Multiplier applied to Q * K^T. Omitted or zero selects the default 1 / sqrt(head_size). |
Type constraints
| Variable | Allowed dtypes |
|---|---|
T |
float32, float16 |
QK |
float32, float16 |
M |
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.
cross_direct,cross_nosg_direct— A single key partition writes normalized attention directly, removing partial buffers and the merge dispatch while preserving masks and diagnostic-score outputs.
Device requirements
Some implementation variants require subgroups. Other variants run without it; which variant runs also depends on the request's shapes and dtypes.
Files
metadata.json— kernel metadata (id, file digests, manifest spec, provenance)manifest.json— the op contract (source of truth)test.json— correctness casesbench.json— benchmark casesattn-flash-decode-splitk-merge.wgsl.jinjaattn-flash-decode-splitk.wgsl.jinjadmmha-present.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.DecoderMaskedMultiHeadAttention", { version: 1 });
const queryTData = 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, 0.2603464, 0.2247556, 0.16936424, 0.09606541, 0.008510161,
-0.08814437, -0.18763463, -0.28310272, -0.3676744, -0.43505034, -0.48005563,
-0.49909735, -0.49048677, -0.4545969, -0.3938397, -0.31246522,
);
const keyTData = Float32Array.of(
0.18795393, 0.18723874, 0.17931889, 0.16589965, 0.14892825, 0.13047631, 0.11261654,
0.09730019, 0.08624214, 0.08081959, 0.08198991, 0.09023206, 0.10551491, 0.12729393,
0.1545368, 0.1857763, 0.21918805, 0.25268874, 0.28404972, 0.3110201, 0.33145243,
0.3434246, 0.3453513, 0.33607924, 0.31496024, 0.28189945, 0.23737474, 0.18242685,
0.11862041, 0.04797748, -0.027112655, -0.104003295,
);
const valueTData = Float32Array.of(
0.7881646, 0.7279641, 0.66986233, 0.6268161, 0.609237, 0.6232503, 0.66966325,
0.74381626, 0.83634037, 0.93469596, 1.0252374, 1.0954623, 1.1360711, 1.1424973,
1.1156542, 1.0617731, 0.9913622, 0.917457, 0.8534525, 0.81087774, 0.79748005,
0.81593734, 0.8634123, 0.9320213, 1.0101415, 1.0843385, 1.1415955, 1.1714747, 1.167852,
1.1299378, 1.0624146, 0.97466904,
);
const { outputT } = await kernel({
queryT: { data: queryTData, shape: [1, 1, 32] },
keyT: { data: keyTData, shape: [1, 2, 1, 16] },
valueT: { data: valueTData, shape: [1, 2, 1, 16] },
}, {
attrs: { num_heads: 2 },
});
console.log({ outputT });
- Downloads last month
- -
Requires WebGPU support. See the compatibility table.