Skip to content

Add a Kimi K3 LoRA recipe on the MXFP4 release - #290

Merged
Micah Yong (micahtyong) merged 72 commits into
mainfrom
micah/kimi-k3
Nov 18, 2026
Merged

Micah Yong (micahtyong) merged 72 commits into
mainfrom
micah/kimi-k3

Conversation

@micahtyong

@micahtyong Micah Yong (micahtyong) commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Kimi K3 LoRA runs on Spindle from the official MXFP4 release (moonshotai/Kimi-K3@f831ab66) with the stock Tinker SDK.

Recipe. kimi_k3_lora_32k: a 2×8 B300 trainer and one 8×B300 sampler, 32K context, LoRA rank up to 32, two training clients per trainer. The sampler needs B300: no other GPU holds the 1.5 TB checkpoint on one node.

What trains. Adapters cover attention, the dense first layer and the shared experts. The 896 routed experts stay frozen in MXFP4 on both sides. A rank-32 adapter is about 0.4 GB.

One weights setting. The trainer and the sampler both read the release. The trainer keeps routed experts as MXFP4 bytes and dequantizes them inside the expert GEMM. The sampler serves them with Marlin.

Sampler settings that matter.

  • The recipe keeps Spindle's default weight_loader_disable_mmap: True. With mmap on, this sampler kept each loaded adapter file open on the snapshot volume. The sampler then could not reload the volume, so new adapter versions never arrived.
  • The tokenizer stays loaded, so clients can stop on strings.
  • SGLANG_K3_AR_FUSION=0: the fused all-reduce skips the LoRA wrappers on o_proj and the shared experts.

Images. No new images. The trainer uses miles-v0.2.0-e814245, which already includes modal-projects/miles#39. The sampler uses sglang-v0.5.23-c4182b2, which already includes modal-projects/sglang#97.

Raw mode. Kimi K3 has no Megatron-Bridge mapping, so the recipe sets megatron_to_hf_mode: raw with lora_type: canonical_lora, the layout the built-in v0.2.0 images accept for raw mode.

Client notes.

  • tinker-cookbook 0.6.1 has no renderer for moonshotai/Kimi-K3, so it needs an explicit renderer_name. Its kimi_k31_pro_* renderers produce the same tokens as K3's own chat encoding, but the cookbook only loads the matching tokenizer for K3.1-Pro model names, so they fail on K3. Until the cookbook maps K3 to them, renderer_name=role_colon works but does not use K3's chat format.
  • Pass loss_fn=importance_sampling (or ppo) to math_rl. Its default, stable_reinforce, is not supported by the Miles backend.
  • A cold trainer takes 35–50 minutes to start and a cold sampler 20–35 minutes. The trainer stops a few seconds after its last client finishes, so start the next run before the previous one ends. Run the client somewhere that stays awake: a client that stops heartbeating for 5 minutes loses its session.

Depends on #348, which runs Miles v0.2.0 trainers across nodes. Kimi K3 needs that: its trainer spans two nodes.

E2E results

On this head (55a68d0, merged with main). The same SFT and RL runs passed again, now with main's FLA kernel pinning (#373) and checkpoint restore changes (#382). RL sampler–trainer KL stayed between 0.0009 and 0.0030 per step, and training steps took 14–15 s after the first. Checkpoints saved from both trainer nodes. A third run loaded the SFT run's final checkpoint and trained 2 more steps; its loss on the first batches was 1.424 / 1.469 against 1.481 / 1.547 from the base weights. The trainer started in 43 minutes and the sampler in 38. W&B: SFT, RL, restore.

The detailed run below is from #348's head.

Setup: a staging deployment of this branch at aadfb95 (on #348's head 67f1d07), recipe kimi_k3_lora_32k (2×8 B300 trainer, 8×B300 sampler). The next commit, bc001ee, only drops two Miles flags that equal Miles' defaults and edits comments. Stock tinker-cookbook 0.6.1 drove it from a container with renderer_name=role_colon and rank 32. The RL client started while SFT was still waiting for the trainer, so both runs shared one trainer, one client per slot. W&B: SFT, RL.

RL: math_rl on gsm8k, 8 groups × 4 samples, up to 1024 tokens, lr 1e-5, loss_fn=importance_sampling, save_every=2, 5 steps.

Step Sampler–trainer KL (v1 / v2) Reward Step time Sampling Train step Export + new sampling client
0 0.0048 / 0.0046 0.95 514 s 78 s 402 s 33 s
1 0.0028 / 0.0021 0.89 254 s 44 s 51 s 159 s (with checkpoint)
2 0.0057 / 0.0115 0.80 140 s 99 s 21 s 21 s
3 0.0014 / 0.0021 0.98 168 s 72 s 15 s 82 s (with checkpoint)
4 0.0021 / 0.0025 0.82 72 s 38 s 15 s 19 s
  • Steps 0 and 1 ran while SFT was training and saving on the same trainer, so they waited behind SFT's work. RL's own forward_backward takes 12–23 s on the trainer.
  • Exporting the adapter takes 9–48 s; the slow exports overlap a checkpoint save from the other run. Publishing takes 4–8 s.
  • No sample was lost or retried, and both runs finished cleanly.

SFT: chat_sl on NoRobots, batch 32, max length 8192, lr 1e-4, save_every=2, 5 steps. forward_backward takes 427 s for the first step, then 39, 15, 12 and 12 s. Train NLL is 1.48 at step 0 and 1.48 at step 4, with batch-to-batch noise from 1.47 to 1.57.

Checkpoints on two nodes. Each run saved two full checkpoints during training and one at the end. Both nodes' shards land in one checkpoint: the save takes 22–33 s on the trainer, committing the shared volume over 16 workers 2–18 s, and persisting 10–36 s.

Startup.

Time
Trainer cold start 42.5 min from the first request to serving: 39 s to get 2×8 B300, then about 37 min to load the checkpoint. That is about 42 of the 60 minutes Spindle gives a trainer backend to start.
Sampler cold start 36.8 min to healthy: 25 min to load weights, then 8 min to capture CUDA graphs.

Not covered yet: resuming from a checkpoint.

Previous run on an earlier multi-node Miles v0.2.0 runtime (Miles f45c63c, SGLang bb327ef, 5 RL steps, 5 SFT steps)

Setup: a staging deployment of this branch at 78c62ee (on an earlier multi-node Miles v0.2.0 runtime), recipe kimi_k3_lora_32k (2×8 B300 trainer, 8×B300 sampler). Stock tinker-cookbook 0.6.1 drove it from a container with renderer_name=role_colon and rank 32. The RL client started while SFT was still waiting for the trainer, so both runs shared one trainer, one client per slot. W&B: SFT, RL.

RL: math_rl on gsm8k, 8 groups × 4 samples, up to 1024 tokens, lr 1e-5, loss_fn=importance_sampling, save_every=2, 5 steps.

Step Sampler–trainer KL (v1 / v2) Reward Step time Sampling Train step Export + new sampling client
0 0.0009 / 0.0024 0.87 215 s 78 s 48 s 89 s
1 0.0010 / 0.0026 0.92 115 s 39 s 14 s 62 s (with checkpoint)
2 0.0024 / 0.0030 0.88 123 s 91 s 14 s 18 s
3 0.0052 / 0.0033 0.72 158 s 86 s 16 s 55 s (with checkpoint)
4 0.0021 / 0.0029 0.95 115 s 81 s 14 s 20 s
  • Steps 0 and 1 ran while SFT was training and saving on the same trainer, so they waited behind SFT's work.
  • On the trainer, RL's forward_backward takes about 12 s. Exporting the adapter takes 11–36 s and publishing it about 5 s.
  • Both runs finished cleanly.

SFT: chat_sl on NoRobots, batch 32, max length 8192, lr 1e-4, save_every=2, 5 steps. forward_backward takes 307 s for the first step, then 28, 19, 15 and 12 s. Train NLL is 1.48 at step 0 and 1.48 at step 4, with batch-to-batch noise from 1.47 to 1.57.

Checkpoints on two nodes. Each run saved full checkpoints at its steps 2 and 4 and at the end. Both nodes' shards land in one checkpoint: the save takes 19–22 s on the trainer, committing the shared volume 10–11 s, and persisting 9–27 s.

Startup.

Time
Trainer cold start 47 min from the first request to serving: 1 min to get 2×8 B300, then about 42 min to load the checkpoint. The earlier run below loaded in about 28 min.
Sampler cold start 19 min to healthy.

Not covered yet: resuming from a checkpoint.

Earlier run on a K3-only trainer image (K3-only trainer image, 12 RL steps, 20 SFT steps)

Setup: a staging deployment of this branch's head (6070287) merged with #291, recipe kimi_k3_lora_32k (2×8 B300 trainer, 8×B300 sampler). Stock tinker-cookbook 0.6.1 drove it from a container with renderer_name=role_colon and rank 32. W&B: SFT, RL.

RL: math_rl on gsm8k, 8 groups × 4 samples, up to 1024 tokens, lr 1e-5, loss_fn=importance_sampling, 12 steps. Each step samples from the adapter the previous step trained.

Step Sampler–trainer KL (v1 / v2) Reward Step time Sampling Train step Export + new sampling client
0 0.0023 / 0.0021 0.98 290 s 119 s 154 s (first step) 16.3 s
1 0.0034 / 0.0032 0.85 106 s 77 s 18 s 10.7 s
2 0.0032 / 0.0027 0.91 122 s 96 s 18 s 7.9 s
3 0.0006 / 0.0021 0.97 71 s 47 s 13 s 10.9 s
4 0.0036 / 0.0023 0.95 55 s 32 s 16 s 7.3 s
5 0.0028 / 0.0029 0.88 135 s 101 s 21 s 12.8 s
6 0.0012 / 0.0025 0.85 80 s 57 s 14 s 10.0 s
7 0.0026 / 0.0025 0.98 84 s 62 s 14 s 8.6 s
8 0.0028 / 0.0034 0.89 112 s 89 s 14 s 10.3 s
9 0.0006 / 0.0034 0.92 76 s 53 s 13 s 9.8 s
10 0.0037 / 0.0031 0.91 112 s 91 s 14 s 7.6 s
11 0.0039 / 0.0026 0.76 117 s 91 s 14 s 12.1 s
  • Sampler–trainer KL stays between 0.0006 and 0.0039 as the adapter moves.
  • On the trainer, forward_backward takes 12.5–20 s per step after the first (153 s). Exporting the adapter takes 2–12 s (median 5 s) and publishing it 1.7–4.7 s. The sampler loads each new adapter version in under 1 s.
  • No sample was lost or retried, and the run finished cleanly.
  • Twelve steps are too few to show learning: the base model already gets most gsm8k problems right.

SFT: chat_sl on NoRobots, batch 32, max length 8192, lr 1e-4, 20 steps. forward_backward takes 182 s for the first step, then 12.4–20 s. Train NLL is 1.48 at step 0 and 1.44 at step 19, with batch-to-batch noise from 1.17 to 1.67. The final checkpoint saves in 29 s, and the sampler adapter exports in 10 s and publishes in 2.5 s.

Startup.

Time
Weights One download of the release: 1.56 TB, 119 files, 18 min with 8 parallel workers.
Trainer cold start 33 min from the first request to serving: 1 min to get 2×8 B300, then about 28 min to load the checkpoint. A second start in the same run took 32.5 min.
Sampler cold start 35.5 min: 24 min to load weights, then 8 min to capture CUDA graphs.

A trainer stops as soon as its last training client finishes. Here the RL run started 13 s after SFT finished, so it waited for a new trainer (the second start above). To reuse a warm trainer, start the next run before the previous one ends.

🤖 Generated with Claude Code

Use the stock Tinker client with Spindle routing replay for the unfiltered Beam recipe. Persist metrics and evaluation responses incrementally, preserve pending behavior policies across checkpoint restarts, and bound execution and cancellation. Include partial-run summaries and a digitized blog comparison.

Refresh the Beam sampler pin to include the latest fork base. Validation: 34 focused tests, 1294 full-suite tests passed with eight skips, locked pre-commit, independent review, and exact grading agreement on 5280 prior responses. Fresh deployment and learning validation remain pending.
When the restored update equals --stop-after, save a roundtrip checkpoint and exit before publication, evaluation or training. Preserve the pending behavior policy for continuation from the audited state. Reject stop boundaries earlier than the restored update.

Independent delta review and 35 focused CPU tests passed. Full CPU suite: 1295 passed, eight skipped. Locked pre-commit passed. Actual GPU restore comparison remains pending.
Detect the public ServiceClient header parameter instead of forwarding an unsupported keyword. Verify proxy credentials reach the real SDK transport under both 0.26 and 0.39 before deliberate model-creation refusal. Keeps expert-routing replay unchanged.
Check in actual configuration, runtime provenance, wall-clock identity and complete startup journal records. The initial SDK-header error and retry are retained. No optimizer updates or evaluation results are claimed in this snapshot.
Miles multi-LoRA rejects explicit model seeds before accepting a model. Keep dataset shuffle and sampling seeds fixed, and let the deployment initialize adapters. Restrict the test service creation signature so this argument cannot return unnoticed.
A durable successful optimizer result must not be treated like an unknown submission if subsequent diagnostics or reporting fail. Record one accepted update with unavailable diagnostics marked, keep the next behavior-policy snapshot, and save the accepted state before exiting. Existing accepted records remain unique; rejection and unknown-outcome handling stay unchanged.

Six fault/cache regressions fail before the change and pass afterward. Focused CPU tests: 42 passed. Full CPU suite: 1302 passed, 8 skipped. Locked pre-commit passed; independent review found no remaining issue. The active GPU process remains on its original client code until the planned checkpoint restart.
Beam needs two H200 nodes, but miles-v0.2.0 rejected multi-node trainers.
Each node sees only its own uncommitted writes to the checkpoint volume, so
Miles' native adapter saves broke in two ways across nodes: rank 0 alone
creates the adapter directory, and the head checks shards other nodes have
not committed.

Every rank now creates the directory before writing its shards, each node
commits the volume after a save or sampler export, and the head reloads it
before checking and publishing the capture.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review of the multi-node change found three problems:

- The head reloaded the checkpoint volume after every save and sampler
  export, even when the capture sat on local disk. A reload fails while
  files on the volume are open, so a save during a checkpoint copy failed.
- A failed volume commit inside the trainer actors marked the whole
  trainer as failed.
- Every save on single-node trainers paid for a barrier, a commit and a
  reload it didn't need.

The commit and reload now run only for captures on the checkpoint volume.
The commit is a separate trainer call that fails only its own request and
removes the partial capture. Saves, exports and publishes of captures on
the volume hold the checkpoint I/O lock that persistence already holds.

The per-rank directory wrapper is gone: torch's FileSystemWriter already
creates the checkpoint directory on every rank.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
modal-projects/sglang#95 was rebased onto the current spindle-sglang-v0.5.23
tip and now adds the Beam attention gate to auto-detected LoRA targets only
for models that declare it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	CHANGELOG.md
#	src/spindle/providers/modal/rollout_image.py
Merge #252's current head. Its Miles v0.2.0 image already includes the
Kimi K3 change, so the K3 recipe now uses trainer_image "miles-v0.2.0"
and the K3-only trainer image and the fork_image() helper are gone.
kimi_k3_image.py keeps only the sampler image.

Kimi K3 trains with megatron_to_hf_mode "raw", which Spindle only passes
to Miles, so miles-v0.2.0 no longer rejects it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Spindle passes --lora-type lora to every Miles trainer, but Miles v0.2.0's
raw mode gives each projection its own lora_A and asserts canonical_lora
at startup. The Kimi K3 recipe now sets lora_type canonical_lora, and a
raw deployment on miles-v0.2.0 without it is rejected before a trainer
starts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@frrad
Fred Robinson (frrad) force-pushed the bb/https-modal-com-slack-com-archives-d0ce0tq2f7f-p-thr_dbnae2xvsn branch 9 times, most recently from f7ed75a to b5140f9 Compare November 18, 2026 04:20
@frrad
Fred Robinson (frrad) force-pushed the bb/https-modal-com-slack-com-archives-d0ce0tq2f7f-p-thr_dbnae2xvsn branch from b5140f9 to 84163b1 Compare November 18, 2026 05:46
Merge reference-dapo-multinode (#348) and keep only the Kimi K3 changes on
top of it: the recipe on the miles-v0.2.0-e814245 trainer image and the
default sglang-v0.5.23-c4182b2 sampler image, which both contain the K3
fork changes. The Beam and old-runtime changes from the previous base are
dropped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@micahtyong
Micah Yong (micahtyong) changed the base branch from bb/https-modal-com-slack-com-archives-d0ce0tq2f7f-p-thr_dbnae2xvsn to reference-dapo-multinode November 18, 2026 16:45
qkv_format thd is the Miles default, and Miles native LoRA already requires
moe_shared_expert_overlap off. Also correct the sampler start-time comment.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@micahtyong Micah Yong (micahtyong) changed the title Add Kimi K3 LoRA recipes on the MXFP4 release Add a Kimi K3 LoRA recipe on the MXFP4 release Nov 18, 2026
Base automatically changed from reference-dapo-multinode to main November 18, 2026 20:03
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.

2 participants