--- library_name: kernels license: apache-2.0 tags: - kernel - webgpu - wgsl --- # com.microsoft.GatherBlockQuantized `com.microsoft` · ONNX Runtime contrib operator · contrib since_version 1 ## Description Gathers rows from a block-wise quantized weight matrix and dequantizes them. This inference implementation supports the standard `gather_axis = 0`, `quantize_axis = 1` matrix subset with uint8 `data`, 2-bit/4-bit packed or 8-bit values, rank-1 non-negative in-bounds int64 `indices` projected to uint32 WebGPU storage, and float32 scales/output. Higher-rank gathers, negative indices, int32 indices, int4/uint4 data, float16/bfloat16 output, and non-default axes are not implemented. See the [ONNX Runtime `GatherBlockQuantized` contrib-operator spec](https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#com.microsoft.GatherBlockQuantized) for the reference semantics. ## Inputs | Name | Upstream name | Logical dtype | WebGPU storage | Rank | Shape | Description | Presence | | --- | --- | --- | --- | --- | --- | --- | --- | | `dataT` | `data` | `T1` | runtime-selected; narrow integers and bool use 32-bit slots | `2` | — | Constant uint8 weight matrix with a physical byte shape. Each byte stores four 2-bit values, two 4-bit values, or one 8-bit value, low bits first. The logical column count is physical columns times 8/bits; arbitrary partial-byte logical tails require a separate adaptation. GPU storage widens each byte to one u32 word. | required | | `indicesT` | `indices` | `Tind` | `uint32` | `1` | — | Non-negative logical int64 indices selecting rows from axis 0 of `data`. Every index must be less than the row count; values use checked uint32 WebGPU storage. | required | | `scalesT` | `scales` | `T2` | same as logical dtype | `2` | — | Per-block dequantization scale factors of shape `(rows, ceil(output_columns / block_size))`. | required | | `zeroPointsT` | `zero_points` | `T1` | runtime-selected; narrow integers and bool use 32-bit slots | `2` | — | Optional uint8 zero points packed along each row of the quantized axis: four per byte at 2 bits, two per byte at 4 bits, or one per byte at 8 bits, low bits first. Every row rounds its byte count independently to ceil(blocks / (8/bits)). Padding bits are ignored. If absent, uint8 data uses 2^(bits-1), including 2 at 2 bits. | optional | ## Outputs | Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence | | --- | --- | --- | --- | --- | --- | --- | | `outputT` | `output` | `T2` | `2` | derived | Dequantized floating-point output rows corresponding to the gathered indices. | required | ## Attributes Default values (overridable per request): | Attribute | Default | Description | | --- | --- | --- | | `bits` | `4` | Bits per quantized value. The schema default is 4; this matrix implementation supports 2, 4 or 8. | | `block_size` | `128` | Number of values sharing a scale. Defaults to 128 and must be a power of two at least 16. | | `gather_axis` | `0` | Axis from which values are gathered. This matrix implementation supports the standard default, axis 0. | | `quantize_axis` | `1` | Axis split into quantization blocks. This matrix implementation supports the standard default, axis 1. | ## Type constraints | Variable | Allowed dtypes | | --- | --- | | `T1` | `uint8` | | `T2` | `float32` | | `Tind` | `int64` | ## Files - [`metadata.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.GatherBlockQuantized/blob/v1/build/webgpu/metadata.json) — kernel metadata (id, file digests, manifest spec, provenance) - [`manifest.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.GatherBlockQuantized/blob/v1/build/webgpu/manifest.json) — the op contract (source of truth) - [`test.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.GatherBlockQuantized/blob/v1/build/webgpu/test.json) — correctness cases - [`bench.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.GatherBlockQuantized/blob/v1/build/webgpu/bench.json) — benchmark cases - [`gather-block-quantized-packed-byte.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.GatherBlockQuantized/blob/v1/build/webgpu/gather-block-quantized-packed-byte.wgsl.jinja) - [`gather-block-quantized-q8-vec4.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.GatherBlockQuantized/blob/v1/build/webgpu/gather-block-quantized-q8-vec4.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.GatherBlockQuantized", { version: 1 }); const dataTData = Uint8Array.from({ length: 32 }, (_, i) => [7, 12, 3, 9, 14, 1, 5, 11][i % 8] ?? 7); const indicesTData = Uint32Array.of(3, 0); const scalesTData = Float32Array.of(0.5, 0.25, 1.5, 2); const { outputT } = await kernel({ dataT: { data: dataTData, shape: [4, 8] }, indicesT: { data: indicesTData, shape: [2] }, scalesT: { data: scalesTData, shape: [4, 1] }, }); console.log({ outputT }); ```