Skip to content

feat(moe): add MoK MXFP8 expert support - #3606

Draft
HuiyingLi wants to merge 3 commits into
mainfrom
huiyingl/feat/mok-mxfp8-core
Draft

feat(moe): add MoK MXFP8 expert support#3606
HuiyingLi wants to merge 3 commits into
mainfrom
huiyingl/feat/mok-mxfp8-core

Conversation

@HuiyingLi

@HuiyingLi HuiyingLi commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Add MXFP8 routed-expert compute to the Mixture-of-Kittens backend.

  • Add backend.mok.precision: bf16 | mxfp8 with BF16 as the default.
  • Quantize each routed Gate, Up, and Down weight into MoK's normal and transposed MXFP8 layouts.
  • Reuse quantized weights across activation-checkpoint recomputation and microbatches, then invalidate them immediately after optimizer.step() updates the BF16 master weights.
  • Keep the existing shared-expert and BF16 paths unchanged.
  • Reject ep_shard > 1 with a clear error. Expert-FSDP support is intentionally outside this PR.

Performance

Both comparisons use full pretrained weights, FineWeb data, 8K sequence length, global batch size 64, world size 64, EP64, PP1, 20 steps with 5 warmup steps, and fake balanced routing disabled. Speedup is normalized to the HybridEP BF16 step time for the same model. Lower step time and higher speedup are better.

DeepSeek V4 Flash — TileLang attention

%%{init: {"xyChart": {"width": 520, "height": 300}}}%%
xychart-beta
    title "DeepSeek V4 Flash 8K speedup vs HybridEP BF16"
    x-axis ["HEP BF16", "HEP MXFP8", "MoK BF16", "MoK MXFP8"]
    y-axis "Speedup (x)" 0.9 --> 1.3
    bar [1.000, 1.066, 1.136, 1.234]
Loading
Backend Step time Speedup vs HEP BF16 W&B
HybridEP BF16 (torch_mm) 9.1640 s 1.000x run
HybridEP MXFP8 (TE) 8.5959 s 1.066x run
MoK BF16 8.0644 s 1.136x run
MoK MXFP8 7.4245 s 1.234x run

MoK MXFP8 is 1.086x faster than MoK BF16 and 1.158x faster than HybridEP MXFP8.

GLM-5.2 — cuDNN attention

%%{init: {"xyChart": {"width": 520, "height": 300}}}%%
xychart-beta
    title "GLM-5.2 8K speedup vs HybridEP BF16"
    x-axis ["HEP BF16", "HEP MXFP8", "MoK BF16", "MoK MXFP8"]
    y-axis "Speedup (x)" 0.9 --> 1.3
    bar [1.000, 1.111, 1.174, 1.295]
Loading
Backend Step time Speedup vs HEP BF16 W&B
HybridEP BF16 (torch_mm) 6.2183 s 1.000x run
HybridEP MXFP8 (TE) 5.5979 s 1.111x run
MoK BF16 5.2987 s 1.174x run
MoK MXFP8 4.8013 s 1.295x run

MoK MXFP8 is 1.104x faster than MoK BF16 and 1.166x faster than HybridEP MXFP8.

100-step convergence parity

The full pretrained models were trained on the same deterministic FineWeb shard with 8K sequence length, global batch size 64, world size 64, EP64, PP1, activation checkpointing enabled, and fake balanced routing disabled. Each comparison uses the same initial checkpoint and data order. Step time excludes the first five warmup steps.

Model HybridEP BF16 loss MoK MXFP8 loss 100-step MAE Max gap Pearson r HEP / MoK step time W&B overlay
DeepSeek V4 Flash 9.1327 → 6.4058 9.1295 → 6.4084 0.00109 0.0060 0.999996 8.8030 / 7.3304 s HEP / MoK
GLM-5.2 1.9717 → 0.0191 1.9725 → 0.0221 0.02125 0.0511 0.999693 6.5405 / 4.8640 s HEP / MoK

Validation

  • 218 passed: focused MoK, MoE parallelizer, and training-recipe unit tests.
  • Four full-model, 100-step HybridEP BF16 versus MoK MXFP8 convergence runs completed successfully.
  • Ruff checks and formatting checks pass for every changed file.
  • git diff --check passes.

Signed-off-by: Huiying Li <huiyingl@nvidia.com>
@copy-pr-bot

copy-pr-bot Bot commented Aug 21, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@HuiyingLi

Copy link
Copy Markdown
Contributor Author

/claude review

@HuiyingLi

Copy link
Copy Markdown
Contributor Author

/ok to test ad5daa2

@claude

claude Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Review Summary

Reviewed all 6 changed files (uv.lock excluded per policy). This PR adds an MXFP8 routed-expert precision path to the MoK backend, with a per-layer prequantized-weight cache retained across microbatches until the optimizer step. The design is careful: the cache key includes storage identity + _version so a modified/rematerialized BF16 weight cannot reuse stale quantized data; the cache is invalidated after opt.step() in the recipe (clear_mok_mxfp8_optimizer_step_cache) and defensively cleared in the backward finally block otherwise; the autograd Function saves the MXFP8 tensors via save_for_backward with explicit count validation (12 tensors) on unflatten; EP-shard incompatibility is guarded with a clear ValueError; and the validation / lazy-import / optimizer-step-cache paths have focused CPU unit tests.

The forward/backward math is delegated to MoK kernels (unchanged reference), and the changed Python routing is well tested with identity assertions on which tensors/layouts flow to forward vs backward.

Findings — two minor tensor-contract documentation gaps on changed public signatures (inline):

  1. _MoKRuntime.forward Returns still documents 3 elements but now returns 4 — the new mxfp8_weights structure / None semantics are undocumented.
  2. _MoKRuntime.backward Args does not document the new mxfp8_weights parameter layout and None/precision contract.

No correctness, gradient-determinism, ownership, or security issues found in the changed code.

routed_gate_weights: torch.Tensor,
routed_up_weights: torch.Tensor,
routed_down_weights: torch.Tensor,
mxfp8_weights: tuple[tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor], ...] | None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new tensor-bearing mxfp8_weights parameter added to backward's signature is not documented in the Args: section. It is a tuple of (gate, up, down), each a 4-tuple of MXFP8 tensors (weight_fp8, weight_scale, weight_t_fp8, weight_t_scale), and must be None for bf16 / non-None for mxfp8 (the body raises on the mismatched case). Please add an Args entry documenting the layout and the None/precision contract.

Signed-off-by: Huiying Li <huiyingl@nvidia.com>
Signed-off-by: Huiying Li <huiyingl@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant