moe-affine

CUDA kernels for small-batch (decode, 1-4 tokens) mixture-of-experts layers whose experts are stored in the affine 4-bit MetalExperts / MLX layout: codes LSB-first in uint32 words, per-group (32/64/128) bf16 scales and biases, w = scale * code + bias. Used by transformers (integrations/cuda_affine_moe.py) to run e.g. ArthurZ/Qwen3.8-Flash-Next-metal-experts-4bit on CUDA without dequantizing the experts.

Ops

op what
qmv4_tc6(x, pool, ids, top_k, x_div, group_size, host=None, slots=None, miss=None, weights=None) tensor-core expert matvec (group size 64; gate/up K=2560 and down K=640 with <= 10 routes). Per block: one cp.async batch stages a 16-row tile of every routed expert + scales + activations, codes are dequantized as bf16 128 + c with one lop3 per pair, mma.m16n8k16. With host/slots/miss it reads missed experts from pinned host memory (UVA) and installs them into the device pool.
gather_qmv4, down_route4 scalar (FMA) expert matvecs, any group size: per-route gate/up outputs, and down fused with the routing-weighted sum
gemv_bf16(x, W, bias) bf16 GEMV for 1-4 (or 8) rows, short outputs
router_topk(x, W, k, norm) router logits + softmax + top-k (+ renorm) in one launch, up to 4 tokens
offload_plan, qmv4_offload, qmv4_offload_v2, offload_install, offload_lookup*, refresh_plan GPU-driven expert offload: device-side LRU over a slot pool, misses read over PCIe and installed while computing
host_tier_*, host_post, host_wait, moe_layer_host experimental: misses computed by AVX-512 CPU workers through a mapped-memory mailbox

All ops have fake (meta) implementations, so torch.compile + CUDA graphs capture them.

Numbers (B200, Qwen3.8-Flash-Next 4-bit experts, 512 experts top-10)

Expert matvecs, gate_up + down per layer, random experts each call (from HBM, not L2):

tokens scalar qmv4_tc6
1 30.4 us 22.4 us
3 69.3 us 44.9 us
4 85.1 us 53.0 us

End to end, generate with torch.compile (max-autotune, static cache), all experts resident: 110.3 -> 117.2 tok/s.

Host compute tier (v2)

host_tier2_start / host_post2 / host_fetch2 / host_guess2 / host_refresh2 / host_tier2_installer: CPU workers (AVX-512 BF16) compute an offloaded MoE layer's missed experts straight from pinned host banks while the GPU runs the resident ones; a copy-engine installer refreshes the GPU pool (LRU) off the critical path; per-layer hints stage likely misses in the workers' caches. Work is split in claimable, stealable slices (no all-thread barrier), optionally over two tiers (one per socket). Used by transformers.integrations.cuda_expert_offload (Qwen3.8-Flash-Next at 25% of the experts on the GPU: ~85-90 tok/s plain decode, ~96 with MTP, on a B200).

Builds

build/torch211-cu130-x86_64-linux (torch 2.11, CUDA 13.0) and build/torch215-cu132-x86_64-linux (torch 2.15, CUDA 13.2; C++20), sm_100a only, were built with build_local.py (local nvcc + g++, -march=sapphirerapids for the host tier), not with kernel-builder. Other variants: build from csrc/ + torch-ext/ with build_local.py, or with kernel-builder from build.toml.

Downloads last month
12
kernels
cuda
Mixture of Experts
apache-2.0
Supported hardwares new
CUDA
10.0
GPU
B300
288GB
NVIDIA SXM
B200
192GB
OS
linux
Arch
x86_64