com.microsoft.MatMulNBitsQkv

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

Description

Fuses RMS normalization with three block-quantized attention projections: A_norm = RMSNorm(A + skip, norm_scale) (or without skip), followed by Q, K, and V projections. The optional fourth output returns A + skip. Only 4-bit weights with block_size = 32 are supported; projection biases, bfloat16, and non-default accuracy_level values are not implemented.

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

Differences from the ONNX Runtime spec

  • Input q_bias, input k_bias, input v_bias: Projection biases are not implemented by this inference package.

Inputs

Name Upstream name Logical dtype Rank Shape Description Presence
aT A T1 — — Shared activation of rank 2 (M, K) or rank 3 (batch, sequence, K); only the last axis is the reduction axis. required
skipT skip T1 — — Residual added to A before the normalization, with A's shape. optional
normScaleT norm_scale T1 1 — Simplified-layer-normalization (RMS) gain of shape [K]. required
qBT q_B T2 3 — Bit-packed uint8 Q weights of shape (Nq, k_blocks, blob_size). The kernels read the uint8 data directly as 32-bit words, four bytes each, so no repacking is needed. required
qScalesT q_scales T1 2 — Per-block Q scales of shape (Nq, k_blocks). Quantization is symmetric: there is no zero-point input, so codes are offset by the midpoint 2^(bits - 1). required
kBT k_B T2 3 — Bit-packed K weights of shape (Nkv, k_blocks, blob_size). The kernels read the uint8 data directly as 32-bit words, four bytes each, so no repacking is needed. required
kScalesT k_scales T1 2 — Per-block K scales of shape (Nkv, k_blocks). required
vBT v_B T2 3 — Bit-packed V weights of shape (Nkv, k_blocks, blob_size). The kernels read the uint8 data directly as 32-bit words, four bytes each, so no repacking is needed. required
vScalesT v_scales T1 2 — Per-block V scales of shape (Nkv, k_blocks). required

Outputs

Name Upstream name Logical dtype Rank Shape Description Presence
qT Q T1 same as aT derived Query projection: A's leading axes with a trailing Nq. required
kT K T1 same as aT derived Key projection: A's leading axes with a trailing Nkv. required
vT V T1 same as aT derived Value projection: A's leading axes with a trailing Nkv. required
residualT input_skip_bias_sum T1 same as aT same as aT The residual sum A + skip, with A's shape. Requires the skip input. optional

Attributes

Attributes and default values (overridable per request):

Attribute Default Description
K — Input feature dimension shared by the normalized input and all projection weights.
Nkv — Output feature dimension shared by the K and V projections.
Nq — Output feature dimension of the Q projection.
accuracy_level 0 Minimum internal accuracy level, following MatMulNBits semantics; this implementation supports the standard default 0.
bits 4 Bit width used to quantize all three weight matrices; only 4 is supported.
block_size — Size of each quantization block along K; only 32 is supported.
epsilon 9.999999974752427e-7 Epsilon used by the simplified layer-normalization reduction.

Type constraints

Variable Allowed dtypes
T1 float32, float16
T2 uint8

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.

  • fused — Normalizes the single half-precision row inside the decode walk of all three projections: activations times their gains feed the dot products and their squares ride the same reads, so the RMS scale multiplies each folded column once and no normalization pass runs first.
  • planes_block — Normalizes each row into two half planes times a power of two, then multiplies both planes by the raw 4-bit codes on subgroup matrices, folding each block into float32 sums times its scale.
  • wide_reg8x8_k32, wide_reg8x4_k32 — Keeps normalization, weight decode and dot accumulation in f32 while sharing a larger register tile across rows and output columns.

Device requirements

Some implementation variants require subgroup-matrix, shader-f16, and subgroups. Other variants run without them; 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.MatMulNBitsQkv", { version: 1 });

const aTData = Float32Array.of(
  0.44933435, 0.41971043, 0.36874023, 0.30724308, 0.24657547, 0.19757758, 0.16957055,
  0.1694898, 0.20122835, 0.265246, 0.35847852, 0.47455502, 0.60430604, 0.7365207,
  0.85888886, 0.9590511, 1.0256665, 1.0494103, 1.023816, 0.9458911, 0.81645346,
);
const normScaleTData = Float32Array.of(
  1.1807667, 1.1590475, 1.1310474, 1.1101198, 1.1069292, 1.1273043, 1.1709933, 1.2315404,
  1.2973149, 1.353523, 1.3848648, 1.3783855, 1.3260317, 1.2264667, 1.0858197, 0.9172162,
  0.73913896, 0.57286274, 0.43936187, 0.35618073, 0.33476692,
);
const qBTData = Uint8Array.from({ length: 80 }, (_, i) => [27, 180, 75, 226, 33, 150, 201, 108, 57, 246, 129, 66, 195][i % 13] ?? 27);
const qScalesTData = Float32Array.of(0.04, 0.055, 0.05, 0.065, 0.06);
const kBTData = Uint8Array.from({ length: 48 }, (_, i) => [211, 44, 137, 98, 165, 20, 233, 121, 78, 190, 15, 252, 87, 143, 61][i % 15] ?? 211);
const kScalesTData = Float32Array.of(0.045, 0.03, 0.07);
const vBTData = Uint8Array.of(
  90, 17, 203, 156, 64, 241, 112, 38, 175, 229, 83, 11, 198, 147, 52, 220, 105, 90, 17,
  203, 156, 64, 241, 112, 38, 175, 229, 83, 11, 198, 147, 52, 220, 105, 90, 17, 203, 156,
  64, 241, 112, 38, 175, 229, 83, 11, 198, 147,
);
const vScalesTData = Float32Array.of(0.052, 0.038, 0.061);

const { qT, kT, vT } = await kernel({
  aT: { data: aTData, shape: [1, 21] },
  normScaleT: { data: normScaleTData, shape: [21] },
  qBT: { data: qBTData, shape: [5, 1, 16] },
  qScalesT: { data: qScalesTData, shape: [5, 1] },
  kBT: { data: kBTData, shape: [3, 1, 16] },
  kScalesT: { data: kScalesTData, shape: [3, 1] },
  vBT: { data: vBTData, shape: [3, 1, 16] },
  vScalesT: { data: vScalesTData, shape: [3, 1] },
}, {
  attrs: {
    K: 21,
    Nq: 5,
    Nkv: 3,
    block_size: 32,
  },
});
console.log({ qT, kT, vT });
Downloads last month
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.