com.microsoft.QMoE
com.microsoft · ONNX Runtime contrib operator · contrib since_version 1
Description
Quantized mixture of experts over float32 activations and uint8-packed integer expert weights. The package implements 2-, 4- and 8-bit symmetric dequantization, ReLU, and interleaved SwiGLU for rank-2 or rank-3 input. With router_weights absent, default routing uses full-expert softmax probabilities; normalize_routing_weights=1 renormalizes the selected top-k experts. A NaN router logit propagates NaN to that token's output when probabilities are computed from the logits. Biases, explicit zero points, FC3, sparse mixing, provider-prepacked layouts, and FP4/FP8 modes are not implemented.
See the ONNX Runtime QMoE contrib-operator spec for the reference semantics.
Differences from the ONNX Runtime spec
- Attribute
accuracy_level: The accuracy hint is not implemented; dequantization and accumulation always use float32. - Attribute
fc1_expert_weight_bits: Per-projection bit-width overrides are not implemented; FC1 uses the common expert_weight_bits attribute. - Attribute
fc2_expert_weight_bits: Per-projection bit-width overrides are not implemented; FC2 uses the common expert_weight_bits attribute. - Attribute
fc3_expert_weight_bits: The separate FC3 projection and its bit-width override are not implemented. - Input
fc1_experts_bias, inputfc2_experts_bias: Expert biases are unsupported by this package. - Input
fc3_experts_weights: The separate FC3 projection used by unfused SwiGLU is not implemented; supported SwiGLU graphs use interleaved FC1 weights. - Input
fc3_scales: The omitted FC3 projection has no scale input in this package. - Input
fc3_experts_bias: The omitted FC3 projection has no bias input in this package. - Input
fc1_zero_points, inputfc2_zero_points: Only the standard implicit midpoint zero point 2^(expert_weight_bits - 1) is implemented. - Input
fc3_zero_points: The omitted FC3 projection and explicit zero points are not implemented. - Input
router_weights: Selection and aggregation both use router_probs; separate aggregation weights are not implemented. - Input
fc1_global_scale, inputfc2_global_scale: FP4 and FP8 expert-weight modes are not implemented. - Input
fc1_act_scale, inputfc2_act_scale: FP8 activation modes are not implemented. - Input
fc1_act_block_scale, inputfc2_act_block_scale: Block-scaled FP8 activation modes are not implemented. - Input
fc1ScalesT(ONNXfc1_scales), inputfc2ScalesT(ONNXfc2_scales): Integer expert weights require explicit dequantization scales in this package.
Inputs
| Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|---|
inputT |
input |
T |
— | — | Token activations of shape (num_tokens, hidden_size) or (batch_size, sequence_length, hidden_size). |
required |
routerT |
router_probs |
T |
2 |
— | Routing logits of shape (num_tokens, num_experts), where num_tokens is the product of the leading input dimensions. |
required |
fc1T |
fc1_experts_weights |
T1 |
3 |
— | Raw packed FC1 weights of shape (num_experts, fusion_size * inter_size, hidden_size / pack_size), where fusion_size is 2 only for interleaved SwiGLU and pack_size is 8 / expert_weight_bits weights. |
required |
fc1ScalesT |
fc1_scales |
T2 |
— | — | Required FC1 dequantization scales: rank 2 (num_experts, fusion_size * inter_size) for column-wise quantization, or rank 3 with a trailing hidden_size / block_size axis. |
required |
fc2T |
fc2_experts_weights |
T1 |
3 |
— | Raw packed FC2 weights of shape (num_experts, hidden_size, inter_size / pack_size). The pack size is 8 / expert_weight_bits. |
required |
fc2ScalesT |
fc2_scales |
T2 |
— | — | Required FC2 dequantization scales: rank 2 (num_experts, hidden_size) for column-wise quantization, or rank 3 with a trailing inter_size / block_size axis. |
required |
Outputs
| Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|---|
outputT |
output |
T |
same as inputT |
same as inputT |
Routed expert output with the same shape as input. |
required |
Attributes
Attributes and default values (overridable per request):
| Attribute | Default | Description |
|---|---|---|
activation_alpha |
1 |
Alpha used by SwiGLU; the exact standard default is 1. |
activation_beta |
0 |
Beta added to the SwiGLU linear branch; the exact standard default is 0. |
activation_type |
"relu" |
Activation applied after FC1. This package supports relu and swiglu; the exact standard default is relu. |
block_size |
— | Optional quantization block size along the reduction dimension. Omission selects column-wise scaling; a supplied value must be a power of two of at least 16 and divide both hidden_size and inter_size. |
expert_weight_bits |
4 |
Integer expert-weight bit width. This package supports 2, 4 and 8; the exact standard default is 4. |
k |
1 |
Number of experts selected per token; the exact standard default is 1. |
normalize_routing_weights |
0 |
With router_weights absent, omitted/default 0 uses the softmax denominator over all experts, so selected weights may sum to less than 1 (including k=1). Value 1 normalizes over the selected top-k logits. This matches ONNX Runtime's WebGPU provider; set 1 for selected-only normalization. ONNX Runtime's CPU provider normalizes over the selected experts for either value. |
quant_type |
"int" |
Quantization family. This package supports only the exact standard default int. |
swiglu_fusion |
0 |
SwiGLU packing mode. ReLU uses the exact standard default 0; supported SwiGLU interleaves gate/up FC1 rows with value 1. |
swiglu_limit |
— | Optional SwiGLU clamp limit. Omission means no finite clamp. |
use_sparse_mixer |
0 |
Whether to use sparse-mixer routing. The exact standard default and only supported value is 0. |
weights_prepacked |
-1 |
Provider weight-layout selector. Values -1 and 0 both consume the public raw packed tensor layout; provider-specific prepacked value 1 is not portable and is rejected. |
Type constraints
| Variable | Allowed dtypes |
|---|---|
T |
float32 |
T1 |
uint8 |
T2 |
float32 |
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.
quant_grouped_sgmat_routed— Expert-grouped f32 matrix multiplication with per-invocation routing slots and reuse of the input tile's workgroup memory to stage the output. The larger of the operand and output tiles must fit the device's workgroup storage limit.quant_grouped_vector_routed— Aligned eight-bit expert-grouped float32 tiles read four widened weight codes through a vector view. This preserves quantization, accumulation and tensor storage while reducing separate scalar weight loads. Unaligned rows use scalar views.quant_grouped_routed— Portable expert-grouped float32 tiles reuse dequantized weights. Register rows follow routed density; register columns and shared-weight layout follow the subgroup width and column tile size. Without subgroups, 8-bit weights use scalar fused multiply-add accumulation.quant_gemv_routed— Cooperative float32 row reductions use four aligned integer codes per lane when the advertised subgroup width covers the logical lane group, and paired codes otherwise. Odd 8-bit reduction tails use scalar access.
Device requirements
Some implementation variants require subgroup-matrix and subgroups. Other variants run without them; 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 casesexpert-group-slots.wgsl.jinjaexpert-slot-mix.wgsl.jinjaqmoe-fc1-activation-gemv.wgsl.jinjaqmoe-fc1-activation-grouped-sgmat.wgsl.jinjaqmoe-fc1-activation-grouped.wgsl.jinjaqmoe-fc1-activation-stage.wgsl.jinjaqmoe-output-gemv.wgsl.jinjaqmoe-output-grouped-sgmat.wgsl.jinjaqmoe-output-grouped.wgsl.jinjaqmoe-output-stage.wgsl.jinjaqmoe-output-zero-inter.wgsl.jinjaqmoe-route-stage.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.QMoE", { version: 1 });
const inputTData = Float32Array.of(1, 1, 2, 2, 3, 3);
const routerTData = Float32Array.of(1000, 999, 998, 997, -1000, -999, -998, -997, 1000, 1000, 1000, 1000);
const fc1TData = new Uint8Array(8).fill(170);
const fc1ScalesTData = new Float32Array(8).fill(1);
const fc2TData = Uint8Array.of(170, 204, 170, 204, 170, 204, 170, 204);
const fc2ScalesTData = new Float32Array(8).fill(1);
const { outputT } = await kernel({
inputT: { data: inputTData, shape: [1, 3, 2] },
routerT: { data: routerTData, shape: [3, 4] },
fc1T: { data: fc1TData, shape: [4, 2, 1] },
fc1ScalesT: { data: fc1ScalesTData, shape: [4, 2] },
fc2T: { data: fc2TData, shape: [4, 2, 1] },
fc2ScalesT: { data: fc2ScalesTData, shape: [4, 2] },
});
console.log({ outputT });
- Downloads last month
- -
Requires WebGPU support. See the compatibility table.