Skip to content

Serve Kimi K3 LoRA adapters on MXFP4 Marlin experts - #97

Merged
micahtyong merged 3 commits into
spindle-sglang-v0.5.23from
spindle-sglang-v0.5.23-kimi-k3
Nov 15, 2026
Merged

micahtyong merged 3 commits into
spindle-sglang-v0.5.23from
spindle-sglang-v0.5.23-kimi-k3

Conversation

@micahtyong

@micahtyong micahtyong commented Nov 12, 2026 •

Copy link
Copy Markdown

Summary

Lets Kimi K3 (moonshotai/Kimi-K3, the official MXFP4 release) serve LoRA adapters loaded by path. Routed experts stay MXFP4 on the Marlin MoE backend and adapters stay BF16. Adapters can target the MLA q_a_proj/kv_a_proj_with_mqa, o_proj on both KDA and MLA layers, the dense MLP, the shared experts, and the routed experts (w1/w2/w3 with shared outer factors).

This backports the serving part of sgl-project#37704 (sglang-miles "Kimi K3 LoRA"):

  • K3 runs the shared-expert gate_up_proj inside one merged front GEMM and writes the shared down_proj into a fused buffer. Both bypass the LoRA wrappers, so the model adds their LoRA deltas itself.
  • The MLA output gate is a patch on o_proj.forward, which the LoRA wrapper skips. The wrapper now applies it through lora_input_transform.
  • Routed experts read a 3584-wide latent activation, not the 7168 hidden size. LoRA buffers use routed_expert_hidden_size, and K3's num_shared_experts sizes the shared-expert buffers.
  • K3 sets gemm1_alpha to its SiTU beta. The Marlin LoRA runner now uses the same gated activation as the base Marlin path, so K3 gets SiTU and GPT-OSS keeps its SwiGLU. K3's w1/w3 are not interleaved, so FusedMoEWithLoRA also checks gate_up_interleaved.

One addition: adapters name routed experts block_sparse_moe.experts.*.w1/w2/w3. K3 maps those leaves to the MoE gate_up_proj/down_proj targets, so they load against the usual target list.

Effects on other models:

  • The Marlin LoRA runner now applies the clamp for models that set a clamp but no gemm1_alpha, as the base Marlin path already does. Before, their LoRA path ran an unclamped SiLU. GPT-OSS is unchanged.
  • MoE configs that say num_shared_experts instead of n_shared_experts now get shared-expert LoRA buffers sized for their shared experts.

Left out of the backport: the streamed CUDA-IPC adapter install (--lora-no-cpu-backup), the in-place MXFP4 reload, and fused shared-expert pool slots. Adapters here load from disk, the base weights never reload, and K3 does not fuse its BF16 shared experts into the MXFP4 MoE.

Serve LoRA with SGLANG_K3_AR_FUSION=0, as Miles' K3 launcher does. K3's fused all-reduce needs o_proj to write into a symmetric buffer, which the LoRA wrapper cannot do. List the targets explicitly. --lora-target-modules all also picks the KDA f_a_proj, which has no LoRA support:

SGLANG_K3_AR_FUSION=0
--moe-runner-backend marlin --enable-lora --lora-backend triton --experts-shared-outer-loras
--lora-target-modules q_a_proj kv_a_proj_with_mqa o_proj gate_proj up_proj down_proj

E2E results

Setup: 8x B300, TP8, lmsysorg/sglang:v0.5.23 with this branch, rank-32 adapters loaded by path, context 8192, --mem-fraction-static 0.85, CUDA graphs and radix cache on. W&B: full model, trained adapter, full model, synthetic adapters, 4-layer LoRA, merged BF16 reference, BF16 base.

Full model, moonshotai/Kimi-K3@f831ab66:

Weights per GPU 193.5 GB
GPU memory used after startup 229 GiB of 268.6
Weight load from a Modal volume 24-27 min
Server ready, cold (incl. Marlin JIT compile) 35-37 min
Load a trained adapter (attention, dense and shared experts, 0.38 GB) 1-5 s
Load an adapter with routed experts (49 GB) 50-62 s
  • The trained adapter is a Miles export from a full-K3 run (840 tensors). Base and adapter requests return coherent text. 17 * 23 = 391, 45 * 67 = 3015, 89 * 12 = 1068.
  • Decode log-probs match a prefill re-score of the same 2048 tokens within 0.018 nats mean without an adapter, and 0.022 with the trained adapter.
  • Rechecked at 16da102 with SGLANG_K3_AR_FUSION=0 (run): the fusion stays off, the trained adapter loads in 1 s, and decode matches prefill within 0.022 nats without an adapter and 0.025 with it.

LoRA math against a reference, on the 4-layer prune Pinaster/Kimi-K3-4layer-64experts:

  • Each adapter is served on the MXFP4 release, and compared with its BF16 dequantization with the adapter merged into the weights, served without LoRA.
  • Log-probs are scored on fixed texts (88 tokens).
Adapter targets LoRA effect (nats) Merged effect (nats) Correlation
all 0.376 0.383 0.986
attention 0.245 0.254 0.947
shared experts 0.308 0.313 0.971
routed experts 0.239 0.247 0.941

The two paths differ by 0.021 nats mean without any adapter, and the same server re-scoring the same text differs by 0.010 to 0.013 nats. A zero adapter stays within that range.

Unit tests on B300: test_kimi_k3_lora_unit.py (new), test_mxfp4_marlin_lora.py (now also SiTU), test_deepseek_v4_lora_unit.py and test_laguna_hidden_dim_unit.py pass.

Known issue: synthetic random adapters with very large deltas (about 0.3 nats of effect per token) make rare decode tokens disagree with the prefill re-score by up to 15 nats, on 0.2 to 0.9% of tokens. The trained adapter and the base model do not show this, and the 4-layer prunes do not reproduce it in 4096 tokens.

🤖 Generated with Claude Code

micahtyong and others added 2 commits November 12, 2026 16:58
Backports the serving part of sglang-miles "Kimi K3 LoRA"
(sgl-project#37704): LoRA deltas for the shared-expert front GEMM
and shared down projection, the gated MLA o_proj, latent routed-expert
dims, num_shared_experts, and SiTU in the Marlin LoRA runner. The
colocated IPC adapter streaming and in-place MXFP4 reload are left out.

K3's fused attention all-reduce is off when LoRA is enabled, because the
LoRA o_proj wrapper cannot write into its symmetric buffer. Routed-expert
targets named w1/w2/w3 map to the MoE gate_up/down targets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The LoRA o_proj wrapper cannot write into the fusion's symmetric buffer,
and with the fusion on the shared experts run LoRA kernels on a side
stream next to the routed experts. Miles' Kimi K3 launcher already sets
SGLANG_K3_AR_FUSION=0 with LoRA; make that the default. This replaces the
narrower attention-only gate.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@micahtyong
micahtyong force-pushed the spindle-sglang-v0.5.23-kimi-k3 branch from ed2b369 to 9acab08 Compare November 13, 2026 02:04
@micahtyong
micahtyong marked this pull request as ready for review November 15, 2026 22:40
Serving K3 with LoRA needs SGLANG_K3_AR_FUSION=0, as Miles' Kimi K3
launcher sets. Drop the LoRA auto-off and the triton_v2 fused-front guard.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@micahtyong
micahtyong merged commit bb327ef into spindle-sglang-v0.5.23 Nov 15, 2026
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