Skip to content
LearnAI

5.3Kimi (Moonshot AI): K2 and beyond

A trillion-parameter MoE trained without a single loss spike, a pipeline that manufactures agentic training data, and the road to Kimi K3's 2.8T parameters.

⏱ 13 minAdvanced

Who is Moonshot, and what is K2?

Moonshot AI is a Beijing startup best known in China for its Kimi chat assistant. In July 2025 it open-sourced Kimi K2, and the open-model community took notice for two reasons. First, its size: 1T total parameters, 32B active, the first open model at that scale to be widely used. Second, its focus: K2 was built from the start as an agentic model, meant to call tools, write and run code, and work through multi-step tasks rather than just chat.

Architecturally, K2 is a close cousin of DeepSeek-V3 (see the DeepSeek lesson). It uses MLAMulti-head latent attention (MLA)DeepSeek's attention variant that compresses keys and values into a small latent vector per token, greatly reducing KV-cache size. attention, 61 layers (one of them dense), and an MoE with 384 experts, 8 active per token, plus 1 shared expert. Moonshot made two deliberate tweaks for efficiency. It used more experts (384 versus 256), because its scaling experiments showed that a sparser MoE with the same active parameters reached lower loss. It also used fewer attention heads (64 versus 128): the report found doubling the heads gave only modest loss gains while raising inference FLOPs by 83% at 128K context, a bad trade for agents that carry long contexts. The real novelty was in how it was trained.

ANALOGY· A choir with a volume limiter

Imagine a huge choir where each singer (an attention head) decides how loudly to sing toward each other singer. Normally everyone keeps a sensible volume. But a few singers get into a feedback loop: the louder they sing, the more they are encouraged to sing louder, until they drown everyone out and the performance collapses. That collapse is a loss spike.

The conductor’s fix is QK-clip. After every rehearsal, the conductor checks each singer’s loudest note. Anyone who went past the limit τ gets their microphone gain turned down, just enough that their loudest note now sits exactly at the limit. Singers who stayed below τ are left alone, and nobody is told what to sing: only how loud. The gain knob has two halves (the query and key weights), and the conductor can turn down either half or both; the split is α.

Muon, and a new way to blow up

Most LLMs are trained with AdamW, which adapts the step size for each individual parameter. MuonMuonAn optimizer that orthogonalizes the momentum update of each weight matrix (via Newton–Schulz iterations) before applying it. takes a different view: a weight matrix is a whole object, so its update should be shaped as a whole. Muon takes the momentum matrix for each layer and orthogonalizes it (approximately, with a few Newton–Schulz iterations). That keeps the update’s directions but sets all its singular values to about 1, so no single direction dominates. Moonshot’s earlier “Muon is Scalable” work found Muon reached the same loss as AdamW with substantially less compute. (Background: optimizers.)

But scaling Muon to a trillion parameters exposed a failure mode. In some attention heads, the attention logits (the scores q⋅k/dq \cdot k / \sqrt{d} before the softmaxSoftmaxA function that turns a vector of scores into probabilities by exponentiating each score and dividing by the total.) kept growing. A softmax over very large logits becomes nearly one-hot, its gradients become extreme, and training suffers loss spikes or diverges. Moonshot found this happened more readily with Muon than with AdamW. Existing fixes didn’t fit: QK-norm (normalizing queries and keys) can’t be applied to MLA, because MLA never materializes full keys at inference time, and logit soft-capping limits the output without stopping the weights underneath from growing.

QK-clip: rescale the weights, not the logits

QK-clipQK-clipA weight-clipping step from Kimi K2: if a head's maximum attention logit exceeds a threshold τ, its query and key projection weights are scaled down so that logit returns to τ. is a direct fix. After every optimizer step, for every attention head, look at the largest attention logit that head produced in the batch, Smax⁡S_{\max}. If it exceeds a threshold τ\tau, shrink that head’s query and key projection weights by just enough to bring Smax⁡S_{\max} back down to exactly τ\tau. Heads that behave are left alone.

Because the logit is a product of a query and a key, the shrink factor can be split between them. Scaling WqW_q by γα\gamma^{\alpha} and WkW_k by γ1−α\gamma^{1-\alpha} scales the logit by exactly γ\gamma. Muon plus this step is MuonClipMuonClipThe optimizer used to train Kimi K2: Muon plus QK-clip, which rescales query and key weights whenever an attention head's maximum logit exceeds a threshold.. For K2, Moonshot used τ=100\tau = 100. The report shows max logits pinned at 100 early in training, then decaying naturally into a stable range after roughly 30% of the training steps, at which point the clip simply stops firing. K2’s 15.5T-token pretraining run had zero loss spikes.

INTERACTIVEMuonClip and QK-clip, simulated

Illustrative simulation, not real training data: four attention heads whose max logit S = q·k/√d drifts upward during training. Head 1 has a runaway feedback loop; Head 2 drifts high but levels off.

QK-clip
Y axis
10301003001000logit explosion zone (illustrative)loss spike risktraining step →max logit S
Headpeak Sfinal |W_q|final |W_k|clip events
Head 12000+35.0429.03—
Head 22975.635.42—
Head 3382.042.17—
Head 4231.571.28—

Without clipping, Head 1 leaves the safe range (peak ≈ 2000+). In a real run this is where loss spikes appear.

Bonus: why Muon updates look different from Adam's

Bars are the singular values of a weight-matrix update (8 directions). The raw momentum is dominated by one or two directions. Muon orthogonalizes it with a few Newton–Schulz steps, pushing every singular value toward ≈1, so rare directions get as much step as common ones.

σ = 10.840.460.250.130.080.040.030.01

Iteration 0: the raw (normalized) momentum. Adam rescales each entry separately and keeps this lopsided shape.

An illustrative simulation (not real training data). Turn QK-clip on, then drag τ and the split α. Only heads that exceed τ are rescaled; α changes how the shrink is shared between W_q and W_k, but the logit is capped either way. The panel below shows what Muon's orthogonalization does to an update.

Things to try:

  1. With QK-clip off, switch the Y axis to Linear. Head 1 grows faster than exponentially once its feedback loop kicks in.
  2. Turn the clip on and watch the clip events column: Heads 3 and 4 are never touched. Per-head clipping is minimal intervention.
  3. Drag α from 0 to 1. Peak S never changes, but the final |W_q| and |W_k| trade places.
  4. In the bonus panel, move Newton–Schulz iterations from 0 to 5 and watch the singular values pile up near 1.

Why rescale weights rather than cap the logits directly? A cap applied at the softmax hides the symptom: the weights underneath can keep growing, and their gradients stay distorted. Rescaling WqW_q and WkW_k removes the cause, because the next forward pass simply produces smaller logits. And because the step happens after the optimizer update, it doesn’t change Muon’s update rule at all; it is a cheap correction that only runs for heads that misbehave. For MLA, the clip has to respect the fact that part of each key (its rotary slice) is shared across all heads, which the Math Corner explains.

Manufacturing agentic data

Pretraining teaches knowledge; it doesn’t teach a model to drive tools for twenty steps without getting lost. Real transcripts of agents using tools are rare, so Moonshot built a pipeline to synthesize them at scale:

  1. Tools. Collect more than 3,000 real MCPModel Context Protocol (MCP)An open protocol for connecting AI applications to tools and data sources through a standard client–server interface. tools from GitHub repositories and evolve over 20,000 synthetic tools across domains.
  2. Agents and tasks. Create varied agent personas with different tool sets, and generate tasks for them, each with an explicit success rubric.
  3. Simulated environments. Run the agent against simulated tools (and, for code, real sandboxes), producing multi-turn trajectories.
  4. Judging. An LLM judge scores each trajectory against the rubric; only successful ones are kept for fine-tuning.

After that SFT, K2 went through joint reinforcement learning combining verifiable rewards (math, code, tests in real environments) with a self-critique rubric reward for tasks without a checkable answer. This mix is now standard for agentic models. (See tool use and agents.)

K2’s later versions

Kimi K3 and K3.1 Pro

Kimi K3 (July 2026) was the largest open-weight model at its release: 2.8T total, 104B active, with a 1M-token context. Its model card shows how much the blueprint changed:

  • 93 layers in a hybrid layout: 69 Kimi Delta Attention layers and 24 Gated MLA layers. Linear attention does most of the work; a minority of full-attention layers keeps exact recall.
  • A much sparser MoE: 896 experts, 16 selected per token, 2 shared, in a “Stable LatentMoE” design that sets expert allocation by “Quantile Balancing” of router scores.
  • Per-Head Muon, which optimizes each attention head separately. Interestingly, the K3 blog does not mention MuonClip.
  • Low precision from the start of post-training: QAT from the SFT stage onward, with MXFP4 weights and MXFP8 activations.
  • Native vision through a 401M-parameter MoonViT-V2 encoder.

Moonshot claims about 2.5× better scaling efficiency than K2 and does not disclose the pretraining token count. Kimi K3.1 Pro (late 2026) is post-trained from K3 and swaps the 24 full-attention layers for Gated DSA (DeepSeek-style sparse attention).

MATH CORNER · optionalWhy QK-clip caps the logit exactly

For head hh, a query and key come from projections of the inputs: qi=Wqxiq_i = W_q x_i and kj=Wkxjk_j = W_k x_j. The attention logit is

sij=qi⊤kjd=(Wqxi)⊤(Wkxj)d.s_{ij} = \frac{q_i^\top k_j}{\sqrt{d}} = \frac{(W_q x_i)^\top (W_k x_j)}{\sqrt{d}} .

Let Smax⁡=max⁡i,jsijS_{\max} = \max_{i,j} s_{ij} over the batch. If Smax⁡>τS_{\max} > \tau, set γ=τ/Smax⁡<1\gamma = \tau / S_{\max} < 1 and rescale

Wq←γαWq,Wk←γ1−αWk.W_q \leftarrow \gamma^{\alpha} W_q, \qquad W_k \leftarrow \gamma^{1-\alpha} W_k .

Every logit of that head is multiplied by γαγ1−α=γ\gamma^{\alpha}\gamma^{1-\alpha} = \gamma, so the new maximum is exactly γSmax⁡=τ\gamma S_{\max} = \tau. The pattern of attention (which logits are larger than which) is unchanged; only its sharpness is reduced.

Worked example with τ=100\tau = 100, Smax⁡=250S_{\max} = 250:

value
γ=τ/Smax⁡\gamma = \tau / S_{\max}0.40
α=0.5\alpha = 0.5: scale on WqW_q and WkW_k0.4≈0.632\sqrt{0.4} \approx 0.632 each
α=0.8\alpha = 0.8: scale on WqW_q, WkW_k0.40.8≈0.4800.4^{0.8} \approx 0.480, 0.40.2≈0.8330.4^{0.2} \approx 0.833
new Smax⁡S_{\max} (either α)0.4×250=1000.4 \times 250 = 100

With MLA each head’s query and key have a head-specific “content” part (qC,kCq^C, k^C) and a rotary part (qRq^R, plus a key part kRk^R that is shared across heads). K2 scales qCq^C and kCk^C by γ\sqrt{\gamma} and qRq^R by γ\gamma, and leaves the shared kRk^R alone so other heads aren’t affected. Both parts of the logit are again scaled by exactly γ\gamma.

In code

A minimal per-head QK-clip step for standard multi-head attention, applied after optimizer.step():

import torch

@torch.no_grad()
def qk_clip_(W_q, W_k, max_logits, tau=100.0, alpha=0.5):
    """W_q, W_k: [n_heads, d_head, d_model]; max_logits: [n_heads],
    the largest q·k/sqrt(d) each head produced in this batch."""
    gamma = (tau / max_logits).clamp(max=1.0)   # 1.0 = head untouched
    W_q.mul_(gamma.pow(alpha).view(-1, 1, 1))
    W_k.mul_(gamma.pow(1 - alpha).view(-1, 1, 1))
    return (gamma < 1).sum().item()             # number of heads clipped

# In the training loop:
#   loss.backward(); muon.step()
#   n = qk_clip_(attn.W_q, attn.W_k, attn.last_max_logits)

The max logits come for free from the forward pass: the attention kernel just has to record each head’s maximum.

COMMON MISCONCEPTIONS
  • “QK-clip clips the logits.” It rescales the weights that produce them, after the optimizer step. Logit soft-capping limits outputs but lets the weights keep growing underneath.
  • “Kimi K2 is a new architecture.” It is essentially DeepSeek-V3’s MLA + MoE design with more experts and fewer heads. Its distinguishing contributions are the optimizer (MuonClip) and the agentic data and RL pipeline.
  • “1T parameters means 1T of compute per token.” Only 32B parameters are active per token, about 3.2% of the model.
  • “Muon replaces the whole optimizer stack.” In practice Muon is used for the large 2D weight matrices; embeddings, the output head and normalization parameters typically still use AdamW-style updates.

Check your understanding

0/5 answered
  1. Q1.A head's max logit is 400 and τ = 100. With α = 0.5, what does QK-clip do?

  2. Q2.Why couldn't K2 simply use QK-norm to control logits?

  3. Q3.What does Muon's Newton–Schulz step do to a layer's momentum matrix?

  4. Q4.Kimi K3 uses 69 Kimi Delta Attention layers and 24 Gated MLA layers. Why mix them?

  5. Q5.Kimi K2 Thinking reports all benchmarks at INT4. Why is that notable?

Further reading

Progress is saved in this browser only.
Type to search across all lessons and glossary terms.