--- library_name: kernels license: apache-2.0 tags: - kernel - webgpu - wgsl --- # com.microsoft.MultiHeadAttention `com.microsoft` · ONNX Runtime contrib operator · contrib since_version 1 ## Description Computes multi-head self- or cross-attention over explicit query, key, and value tensors, with an optional fused QKV projection bias and additive attention bias. Supports causal masking through `unidirectional` and a configurable score scale that defaults to `1 / sqrt(head_size)`. The current kernels require query, key, and value to have the same hidden width; distinct value-head widths, KV-cache, key-padding-mask, and diagnostic-QK modes are not yet implemented. See the [ONNX Runtime `MultiHeadAttention` contrib-operator spec](https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#com.microsoft.MultiHeadAttention) for the reference semantics. ## Inputs | Name | Bind key | Logical dtype | Rank | Shape | Description | Presence | | --- | --- | --- | --- | --- | --- | --- | | `query` | `queryT` | `T` | `3` | — | Query tensor of shape `(batch_size, sequence_length, hidden_size)`. | required | | `key` | `keyT` | `T` | `3` | — | Key tensor of shape `(batch_size, kv_sequence_length, hidden_size)`; the current kernels require its hidden width to equal the query hidden width. | required | | `value` | `valueT` | `T` | `3` | — | Value tensor of shape `(batch_size, kv_sequence_length, v_hidden_size)`; the current supported subset requires `v_hidden_size` to equal the query hidden width. | required | | `bias` | `biasT` | `T` | `1` | — | Optional fused bias from input projection with shape `(hidden_size + hidden_size + v_hidden_size)`. | optional | | `attention_bias` | `attentionBiasT` | `T` | `4` | — | Optional additive bias applied to the attention scores before softmax, of shape `(batch_size or 1, num_heads or 1, sequence_length, kv_sequence_length)`; the first two dimensions broadcast. | optional | ## Outputs | Name | Bind key | Logical dtype | Rank | Shape | Description | Presence | | --- | --- | --- | --- | --- | --- | --- | | `output` | `outputT` | `T` | `3` | `[query[0], query[1], value[2]]` | Attention output of shape `(batch_size, sequence_length, v_hidden_size)`. | required | ## Attributes Attributes and default values (overridable per request): | Attribute | Default | Description | | --- | --- | --- | | `unidirectional` | `0` | When 1, applies an upper-left causal mask: query position `i` attends only to key positions `0..min(i, kv_sequence_length - 1)`. | | `num_heads` | — | Number of attention heads. | | `scale` | — | Optional score scale; zero or omission selects `1 / sqrt(head_size)`. | ## Type constraints | Variable | Allowed dtypes | | --- | --- | | `T` | `float32`, `float16` | ## Device requirements Some implementation variants require `subgroup-matrix`, `shader-f16`, and `subgroups`. These are route-specific capabilities, not package-wide requirements; availability also depends on the request shape and dtype. ## Files - [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, provenance) - [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth) - [`test.json`](build/webgpu/test.json) — correctness cases - [`bench.json`](build/webgpu/bench.json) — benchmark + tuning cases - [`attention-rank4-tiled.wgsl.jinja`](build/webgpu/attention-rank4-tiled.wgsl.jinja) - [`attn-flash-decode-splitk-merge.wgsl.jinja`](build/webgpu/attn-flash-decode-splitk-merge.wgsl.jinja) - [`attn-flash-decode-splitk.wgsl.jinja`](build/webgpu/attn-flash-decode-splitk.wgsl.jinja) - [`attn-flash-online.wgsl.jinja`](build/webgpu/attn-flash-online.wgsl.jinja) - [`attn-flash-prefill-cluster.wgsl.jinja`](build/webgpu/attn-flash-prefill-cluster.wgsl.jinja) - [`attn-flash-q32-broadcast.wgsl.jinja`](build/webgpu/attn-flash-q32-broadcast.wgsl.jinja) - [`attn-materialized-apply-f32.wgsl.jinja`](build/webgpu/attn-materialized-apply-f32.wgsl.jinja) - [`attn-materialized-rowstats-combine-f32.wgsl.jinja`](build/webgpu/attn-materialized-rowstats-combine-f32.wgsl.jinja) - [`attn-materialized-score-f32.wgsl.jinja`](build/webgpu/attn-materialized-score-f32.wgsl.jinja) - [`attn-materialized-sgmat-f32.wgsl.jinja`](build/webgpu/attn-materialized-sgmat-f32.wgsl.jinja) - [`attn-materialized-softmax-f32.wgsl.jinja`](build/webgpu/attn-materialized-softmax-f32.wgsl.jinja) - [`attn-online-scalar.wgsl.jinja`](build/webgpu/attn-online-scalar.wgsl.jinja) - [`attn-small-head-parallel.wgsl.jinja`](build/webgpu/attn-small-head-parallel.wgsl.jinja) - [`mha-small-seq.wgsl.jinja`](build/webgpu/mha-small-seq.wgsl.jinja) ## Use with `@huggingface/kernels` The loader derives every required output's shape and logical dtype from the manifest contract and this call. It then allocates the result tensors automatically. The `version: 1` option selects the published kernel contract; it is independent of any operator opset, contrib `since_version`, or model version. Replace each `*Data` placeholder with a typed array containing the corresponding input data. ```js import { getKernel } from "@huggingface/kernels"; const kernel = await getKernel("webgpu-kernels/com.microsoft.MultiHeadAttention", { version: 1 }); const { outputT } = await kernel({ queryT: { data: queryTData, shape: [2, 2, 4] }, keyT: { data: keyTData, shape: [2, 3, 4] }, valueT: { data: valueTData, shape: [2, 3, 4] }, }, { attrs: { num_heads: 2 }, }); ```