|
Download README.md from webgpu-kernels/com.microsoft.MatMulNBitsMlp: direct link, hf CLI and curl.
- Browser
- Download file 9.51 kB
-
https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MatMulNBitsMlp/resolve/v1/README.md
- Command line
-
hf download hf://webgpu-kernels/com.microsoft.MatMulNBitsMlp@v1/README.md
-
curl -L -o README.md https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MatMulNBitsMlp/resolve/v1/README.md
9.51 kB
| library_name: kernels | |
| license: apache-2.0 | |
| tags: | |
| - kernel | |
| - webgpu | |
| - wgsl | |
| # com.microsoft.MatMulNBitsMlp | |
| `com.microsoft` · ONNX Runtime contrib operator · contrib since_version 1 | |
| ## Description | |
| Fuses a gated MLP over two block-quantized projections that share one activation: `Y = silu(A_norm @ gate + gate_bias) * (A_norm @ up + up_bias)`, using the `MatMulNBits` weight packing with no zero-point input. `A_norm` is `A`, `SimplifiedLayerNormalization(A, norm_scale)`, or `SkipSimplifiedLayerNormalization(A, skip, norm_scale)`, whose residual sum may be returned as a second output. Only `silu` and the default `accuracy_level = 0` are implemented; bfloat16 is not implemented. | |
| See the [ONNX Runtime `MatMulNBitsMlp` contrib-operator spec](https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#com.microsoft.MatMulNBitsMlp) for the reference semantics. | |
| ## 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 normalization, with `A`'s shape. Requires `norm_scale`. | optional | | |
| | `normScaleT` | `norm_scale` | `T1` | `1` | — | Simplified-layer-normalization (RMS) gain of shape `[K]`. Absent means the projections read `A` unnormalized. | optional | | |
| | `gateBT` | `gate_B` | `uint8` | `3` | — | Bit-packed uint8 gate weights of shape `(N, k_blocks, blob_size)`. The kernels read the uint8 data directly as 32-bit words, four bytes each, so no repacking is needed. | required | | |
| | `gateScalesT` | `gate_scales` | `T1` | `2` | — | Per-block gate scales of shape `(N, k_blocks)`, with the same dtype as `A`. Quantization is symmetric: this operator has no zero-point input, so codes are offset by the midpoint `2^(bits - 1)`. | required | | |
| | `gateBiasT` | `gate_bias` | `T1` | `1` | — | Optional gate bias of shape `[N]`, added before the activation. | optional | | |
| | `upBT` | `up_B` | `uint8` | `3` | — | Bit-packed up weights, same shape and packing as gate_B. The kernels read the uint8 data directly as 32-bit words, four bytes each, so no repacking is needed. | required | | |
| | `upScalesT` | `up_scales` | `T1` | `2` | — | Per-block up scales of shape `(N, k_blocks)`. | required | | |
| | `upBiasT` | `up_bias` | `T1` | `1` | — | Optional up bias of shape `[N]`, added before the product. | optional | | |
| ## Outputs | |
| | Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence | | |
| | --- | --- | --- | --- | --- | --- | --- | | |
| | `yT` | `Y` | `T1` | same as `aT` | derived | Gated MLP output: A's leading axes with a trailing N. | 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 both quantized weight matrices. | | |
| | `N` | — | Output feature dimension shared by both quantized weight matrices. | | |
| | `accuracy_level` | `0` | Minimum internal accuracy level: 0 (unset), 1 (float32), 2 (float16), 3 (bfloat16), or 4 (int8). | | |
| | `activation` | — | Activation applied to the gate projection; this implementation supports `silu`. | | |
| | `bits` | `4` | Bit width used to quantize both weight matrices; this implementation supports 2, 4, and 8. | | |
| | `block_size` | — | Size of each quantization block along K. | | |
| | `epsilon` | `0.00001` | Epsilon used by the optional fused RMS normalization. | | |
| ## Type constraints | |
| | Variable | Allowed dtypes | | |
| | --- | --- | | |
| | `T1` | `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. | |
| - `planes_block` — Normalizes each row into two half planes times a power of two, multiplies both by the raw 4-bit gate and up codes on subgroup matrices, folds each block into float32 sums times its scale and applies silu(gate) x up. | |
| - `paired_reg8x4` — Shares normalized f32 activations and two f32-decoded streams across a 64-row register tile with f32 accumulators. Each tile covers 16 or 32 output columns, chosen from the size of the output grid. | |
| - `hybrid` — Shares normalized activations across matrix tiles covering the complete row prefix, then evaluates the remaining rows with the packed block-reduction projection. It is used only when there are enough column workgroups and reduction work to amortize the extra projection dispatch. | |
| - `gemv_norm` — Centers packed four-bit gate and up weights before the dot products. A row that fits across the workgroup normalizes the folded projections once. For longer rows, lanes normalize the inputs before accumulating successive vectors. The first workgroup also writes the optional residual. | |
| - `fused` — Projects the activations directly, or normalizes a single decode row inside the projection loop: activations times gains feed both projections, their squares are summed from the same reads, and the inverse RMS scales the folded sums once. A half-precision row on the vector walk reads activations, skip and gains as packed words. | |
| ## 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 | |
| - [`metadata.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MatMulNBitsMlp/blob/v1/build/webgpu/metadata.json) — kernel metadata (id, file digests, manifest spec, provenance) | |
| - [`manifest.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MatMulNBitsMlp/blob/v1/build/webgpu/manifest.json) — the op contract (source of truth) | |
| - [`test.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MatMulNBitsMlp/blob/v1/build/webgpu/test.json) — correctness cases | |
| - [`bench.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MatMulNBitsMlp/blob/v1/build/webgpu/bench.json) — benchmark cases | |
| - [`matmul-nbits-fused-rms-norm.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MatMulNBitsMlp/blob/v1/build/webgpu/matmul-nbits-fused-rms-norm.wgsl.jinja) | |
| - [`matmul-nbits-rms-norm-half-planes.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MatMulNBitsMlp/blob/v1/build/webgpu/matmul-nbits-rms-norm-half-planes.wgsl.jinja) | |
| - [`mlp-block-scale.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MatMulNBitsMlp/blob/v1/build/webgpu/mlp-block-scale.wgsl.jinja) | |
| - [`mlp-gate-up-tiled.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MatMulNBitsMlp/blob/v1/build/webgpu/mlp-gate-up-tiled.wgsl.jinja) | |
| - [`mlp-gate-up.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MatMulNBitsMlp/blob/v1/build/webgpu/mlp-gate-up.wgsl.jinja) | |
| - [`mlp-gemv-norm.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MatMulNBitsMlp/blob/v1/build/webgpu/mlp-gemv-norm.wgsl.jinja) | |
| - [`mlp-refine.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.MatMulNBitsMlp/blob/v1/build/webgpu/mlp-refine.wgsl.jinja) | |
| ## Use with `@huggingface/kernels` | |
| ```sh | |
| 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`. | |
| ```js | |
| import { getKernel } from "@huggingface/kernels"; | |
| const kernel = await getKernel("webgpu-kernels/com.microsoft.MatMulNBitsMlp", { version: 1 }); | |
| const aTData = Float32Array.of( | |
| 0.9181, 1.1011, 1.115, 0.9522, 0.6333, 0.2043, -0.2696, -0.7143, -1.0586, -1.2453, | |
| -1.2411, -1.0421, -0.6753, -0.1947, 0.3263, 0.8068, 1.1707, 1.3587, 1.3385, 1.1098, | |
| 0.7051, 0.1852, -0.3707, -0.8762, -1.2521, -1.4384, -1.4042, -1.1526, -0.7208, -0.1744, | |
| 0.4029, 0.9218, | |
| ); | |
| const gateBTData = Uint8Array.of( | |
| 52, 93, 210, 163, 160, 89, 30, 255, 204, 21, 42, 27, 184, 145, 246, 247, 100, 205, 130, | |
| 147, 208, 201, 206, 239, 252, 133, 218, 11, 232, 1, 166, 231, | |
| ); | |
| const gateScalesTData = Float32Array.of(0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1); | |
| const upBTData = Uint8Array.of( | |
| 248, 209, 54, 55, 164, 13, 194, 211, 16, 9, 14, 47, 60, 197, 26, 75, 40, 65, 230, 39, | |
| 212, 125, 114, 195, 64, 121, 190, 31, 108, 53, 202, 59, | |
| ); | |
| const upScalesTData = Float32Array.of(0.05, 0.046, 0.042, 0.038, 0.034, 0.03, 0.026, 0.022); | |
| const { yT } = await kernel({ | |
| aT: { data: aTData, shape: [2, 16] }, | |
| gateBT: { data: gateBTData, shape: [4, 2, 4] }, | |
| gateScalesT: { data: gateScalesTData, shape: [4, 2] }, | |
| upBT: { data: upBTData, shape: [4, 2, 4] }, | |
| upScalesT: { data: upScalesTData, shape: [4, 2] }, | |
| }, { | |
| attrs: { | |
| K: 16, | |
| N: 4, | |
| block_size: 8, | |
| activation: "silu", | |
| }, | |
| }); | |
| console.log({ yT }); | |
| ``` | |