Skip to content

Kimi-K3 W4A16 LoRA for Spindle - #39

Merged
micahtyong merged 4 commits into
spindle-miles-v0.2.0from
micah/kimi-k3-w4a16
Nov 15, 2026
Merged

micahtyong merged 4 commits into
spindle-miles-v0.2.0from
micah/kimi-k3-w4a16

Conversation

@micahtyong

@micahtyong micahtyong commented Nov 12, 2026 •

Copy link
Copy Markdown

Summary

Train Kimi-K3 LoRA from Moonshot's MXFP4 release with no conversion step. A LoRA run that loads moonshotai/Kimi-K3 keeps each frozen routed expert as its MXFP4 bytes on the GPU. The bytes are turned back into BF16 inside the expert matmul, one layer at a time. The experts then take a quarter of their BF16 memory, so the trainer fits on far fewer GPUs and reads the release directly. You no longer need the offline BF16 and torch_dist copies (about 5.6 TB).

This is the Kimi-K3 part of radixark/miles#4667, ported onto v0.2.0. It leaves out the K3.1 indexer (radixark/miles#4666) and the weight-sync changes that only colocated runs use.

The K3 load also drops two per-weight costs. For every weight, mbridge rebuilt the model's state dict and reopened the weight's checkpoint shard. With tens of thousands of expert weights per GPU, these took most of the load's CPU time. The K3 bridge now builds the state dict once and keeps each shard open. On network storage the load is still bound by reads (see below).

Multi-LoRA can now train Kimi-K3. Multi-LoRA slots cover dense projections only, but the K3 adapter layout required routed-expert adapters. Routed experts are now optional. A multi-LoRA run trains attention, the dense MLP and the shared experts, and leaves the routed experts frozen.

The MXFP4 path also gets three guards. It finds the MXFP4 checkpoint through --ref-load when --load points elsewhere, as it does on a resume. It refuses --save-hf and eval snapshots, which cannot write packed experts as HF weights. And a routed-expert adapter must select w1 and w3 together, since it always trains both.

The memory saving assumes full recomputation (--recompute-granularity full), which the K3 recipes use. Without it, every MoE layer keeps its BF16 experts until backward.

E2E results

All runs use the radixark/miles:v0.2.0 image and drive the trainer through a multi-LoRA Tinker runtime: load_slot, forward_backward (cross-entropy), optim_step and a PEFT adapter export. Each run has rank-32 slots and lr 1e-4. The 4-layer runs use 8 rows of 1,024 tokens.

4-layer prune (Pinaster/Kimi-K3-4layer-64experts), 8×H200, TP8/EP8. The MXFP4 run reads the release format. The BF16 run reads the same weights after tools/convert_mxfp4_to_bf16.py.

Step MXFP4 BF16 BF16, second run
0 13.6914 13.6913 13.6902
1 13.6876 13.6887 13.6875
2 13.6777 13.6766 13.6766
3 13.6606 13.6605 13.6603
4 13.6415 13.6417 13.6398

The MXFP4 and BF16 runs differ by no more than two BF16 runs differ from each other. After training, per-token log-probs differ by 0.049 on average between MXFP4 and BF16, and by 0.052 between the two BF16 runs.

forward_backward takes 0.42 s (MXFP4) and 0.34 s (BF16) after the first call. The first call takes 262 s in both runs, which is kernel compilation. W&B: MXFP4, BF16, BF16 second run, MXFP4 with the load changes.

Full model (moonshotai/Kimi-K3, MXFP4 release), 8×B300 on one node, TP8/EP8, 8 rows of 2,048 tokens:

Step NLL forward_backward
0 12.2823 435 s (includes kernel compilation)
1 12.2549 64 s
2 12.2093 64 s

Each GPU peaks at 216 GB allocated (232 GB reserved), so the whole trainer fits on one node. Startup takes 74 minutes, almost all of it the checkpoint load, which reads each expert's tensors from network storage one at a time. optim_step takes 0.04 s, and the adapter export (840 tensors) takes 1.1 s. W&B: full model.

Tests: the new GPU test checks that the MXFP4 grouped linear matches a BF16 grouped linear bit for bit, forward and backward, with no weight gradient. 308 tests pass across the K3 native-LoRA, bridge, conversion and target suites.

🤖 Generated with Claude Code

micahtyong and others added 2 commits November 12, 2026 15:03
A LoRA run that loads the compressed-tensors MXFP4 release keeps each
routed expert as its packed codes and E8M0 scales, and dequantizes them
exactly into BF16 inside the grouped GEMM. The experts take a quarter of
their BF16 memory, and the trainer reads the official checkpoint without
an offline BF16 or torch_dist conversion.

Port of the Kimi K3 parts of radixark#4667 onto v0.2.0, without
the K3.1 DSA indexer (radixark#4666) or the colocated weight-sync
changes.

Co-Authored-By: Jiajun Li <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Multi-LoRA slots cover dense projections only, so the native K3 layout
must allow leaving every routed-expert projection frozen. The adapters
then cover attention, the dense MLP and the shared experts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
micahtyong and others added 2 commits November 12, 2026 19:33
- Detect MXFP4 experts from --ref-load too, since a resume or peer
  recovery points --load elsewhere.
- Refuse HF snapshot exports, which cannot write packed experts as HF
  weights.
- Require w1 and w3 together: the routed-expert gate/up adapter always
  trains both.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
For every weight it loads, mbridge rebuilds the model's state dict and
reopens the weight's shard, header included. With tens of thousands of
expert weights per rank, these took most of the load's CPU time. Build
the state dict once and keep each shard open for the load.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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