High-performance GPU kernels for mixture-of-experts models whose experts have different intermediate widths.
MegaBlocks Variable began as a fork of Databricks MegaBlocks and now evolves as an independent project. It preserves the upstream Git history, license, and attribution, while focusing narrowly on variable-width routing, inference, and training.
- A fused Triton inference path with device-side counting-sort routing and row tiles sized for decode and prefill.
- A differentiable grouped-MoE training path with no STK dependency whose forward and backward are bitwise reproducible from run to run.
- An optional CuTe DSL backend for the dominant Ampere training GEMMs.
- Score-aware nearest-block route dropping for reducing training padding,
including
token_rounding='nearest'in the grouped training path. - Extension-free installation for the default Triton paths.
The implementation is intentionally compact. Legacy upstream model layers and unused sparse operations were removed; this package is a kernel backend for projects that own their model and routing layers.
On an RTX 3090, the fused variable-width serving path improved measured keep-50 Qwen3.5-MoE decode throughput from 688 to 1536 tok/s (2.21x) while preserving greedy continuations.
For one training layer with 54 experts, 4096 tokens, hidden size 2048, and top-k 8, on an RTX 3090 at a 275 W limit with the BF16 unlock, under sustained load:
| backend | forward + backward |
|---|---|
| Triton | 9.23 ms |
| CuTe | 8.85 ms |
See KERNEL_OPTIMIZATION.md for methodology, rejected
fusions, memory tradeoffs, and shape-specific results.
MegaBlocks Variable currently keeps the Python import/package name
megablocks for downstream compatibility. Install this repository explicitly;
pip install megablocks may resolve the upstream Databricks distribution.
uv pip install "megablocks @ git+https://github.com/hbfreed/megablocks-variable.git"For an editable development environment:
git clone https://github.com/hbfreed/megablocks-variable.git
cd megablocks-variable
uv sync --extra devThe lock file pins PyTorch 2.12 with its CUDA 13.0 wheels, the stack the numbers above were measured on.
The fused and grouped paths need only PyTorch. megablocks.ops and
megablocks.layers.relu_squared, the older block-sparse path, also need
STK: install megablocks[stk].
The default Triton paths do not build CUDA extensions. Set
MEGABLOCKS_BUILD_EXTENSIONS=1 only for legacy consumers that still need
nanomoe_ops; that build needs PyTorch in the build environment (for uv,
list torch under [tool.uv.extra-build-dependencies]).
Install the CuTe extra:
uv sync --extra cuteThe CuTe kernels target BF16 on Ampere (compute capability 8.x) with hidden
sizes divisible by 256, where they run 3-7% faster than Triton per layer. The
default down_proj_backend="auto" uses them whenever the extra is installed and
the call qualifies, and Triton otherwise, so no flag is needed. Pass "cute" or
"triton" to force one:
from megablocks.backend.grouped_moe import grouped_moe
output = grouped_moe(
x,
top_weights,
expert_ids,
plan,
top_k,
w_gate,
w_up,
w_down,
recompute_activation=False,
)recompute_activation=False is the fastest mode but retains about 77.5 MiB
for the measured layer shape. Keep the default True when activation memory
is more valuable than the roughly 0.31 ms backward improvement.
With gradient accumulation, autograd writes each micro-batch's weight
gradients to fresh buffers and then adds them to .grad in a separate pass.
Registering the weights once moves that add into the weight-gradient kernels,
which accumulate in fp32 and round once:
from megablocks.backend.grouped_moe import register_weight_grads
key = register_weight_grads(w_gate, w_up, w_down) # once, outside torch.compile
output = grouped_moe(x, top_weights, expert_ids, plan, top_k,
w_gate, w_up, w_down, weight_grad_key=key)The first backward after zero_grad(set_to_none=True) writes .grad, as
autograd would, and later ones add to it. Autograd then receives no gradient
for those weights, so gradient hooks on them do not run.
GPU operator tests are marked explicitly:
pytest -m gpu tests/opsThe optional CuTe tests require its dependencies and Ampere hardware, and are
skipped without them. A plain pytest runs only the few tests that need no
GPU.
The descriptions of the eight pull requests merged before this repository
left the GitHub fork network are preserved in
docs/pull-request-history.md. All commits and
authorship remain in Git.
MegaBlocks Variable is based on the original MegaBlocks work:
@article{megablocks,
title={{MegaBlocks: Efficient Sparse Training with Mixture-of-Experts}},
author={Trevor Gale and Deepak Narayanan and Cliff Young and Matei Zaharia},
journal={Proceedings of Machine Learning and Systems},
volume={5},
year={2023}
}See LICENSE and file-level notices for licensing and attribution.