Skip to content

[Kimi K3] Full spmd backend declaration for K3 CP, EP and TP/SP: the dense path, the tower over cp, the MoE seams, the multimodal inputs - #4492

Closed
QIU023 wants to merge 1 commit into
pytorch:mainfrom
QIU023:k3_spmd_decl
Closed

QIU023 wants to merge 1 commit into
pytorch:mainfrom
QIU023:k3_spmd_decl

Conversation

@QIU023

@QIU023 QIU023 commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Declares, for the spmd_types backend 4446 turned on, what 4446 left to annotate_replicated_parameters: the dense path's weight types and stream conversions, the MoonViT tower over cp, the latent MoE's seams, the multimodal inputs.
  • Before: only the MoE modules declare layouts, and the multimodal debug flavor stops at step 1 under the now-default backend with ValueError: spmd_types backend requires an SPMD layout for every tensor input, but these have no entry in input_sharding: ['grid_thw', 'pixel_values'] (the path of 4446's kimi_k3_debugmodel_mm_fsdp2 cell).
  • After: kimi_k3/sharding.py carries the declarations (declarations only, applied through the Module protocol, the qwen3_5 shape), issued at tp = 1 where spmd_types reads every one of them; preprocess_inputs gives pixel_values and grid_thw the DP-local, TP-invariant layout every VLM decoder shares.
  • Tensor parallelism stays on the unsupported list here; the TP/SP PR stacked on this one turns it on.

Design

  • Stream weights follow norm_config's rule. Without SP the stream is invariant on TP and every module converts it I -> R on entry, so the residual projections, output_res_proj and routed_up see one gradient on every rank and are I; under SP (the TP/SP PR) they are R and FSDP sums the per-rank partials.
  • routed_down, wq_a, wkv_a and forget_a are R either way: their consumers are TP-sharded, each rank's gradient is a partial sum.
  • MLA and KDA convert the stream I -> R on entry (colwise projections, so the backward all-reduces the input gradient) and their rowwise exits hand it back invariant. The empty residual stack is cut from the stream, so it carries the stream's layout.
  • routed_norm reduces the experts' Partial output at its boundary (keyed x, the argument nn.RMSNorm.forward takes); routed_up re-enters Partial so core's MoE exit reduces once.
  • The MoonViT tower is invariant at TP and rank-local over cp (main's include_cp_axis=True helpers and its own attention entry, since the shared plan shards the tower over TP and all-gathers k/v over cp). Its exit follows the stream: I without SP, R under SP, where the exit's backward all-reduce sums the shards' feature gradients.
  • Local regions: the MLA head unflatten and the rope join run in spmd.local() regions re-typed head-sharded on TP; KDA's head splits go through core's local_head_split, a plain view cannot split a feature-sharded dim.
  • The vision prep runs in main's multimodal_context() and the stream is re-asserted on the decoder's layout after it, as qwen3_5 and kimi_k2_7 do.
  • Every declaration is keyed by main's forward() parameter names (q_THK, k_THK, v_THV; raw_gate_THK, raw_beta_TH, cu_seqlens on InnerKDA): a local_map needs an entry per positional parameter, and a key matching nothing declares nothing.
  • parallelize.py is untouched: 4446's replicated annotation seeds the parameters this PR does not declare, and its FSDP mesh resolution is the one every model shares.

Results

Every cell is bitwise identical under the two backends through step 10; the declarations change what the backend is told, not what is computed.

kimi_k3_debugmodel (multimodal), --debug.seed 42 --debug.deterministic, one seed checkpoint, 8192 tokens per step in micro-batches of 256, on an RTX 5060 Ti with Attention Gym at upstream/main b19162e and its SM100/SM103 guard in kda.py lifted locally (KDA on Attention Gym's portable kernels). The table reads in adjacent PAIRS: two rows are one cell under the two backends, and only a pair is a comparison. Across pairs the numbers move for two reasons that are not the backend's: the loader shards documents by dp rank (components/data/sources.py), so dp1 and dp2 read different samples from step 1 on, and turning EP on regroups the expert GEMMs, which in bf16 flips the sign of 1.4% of the step-1 gradient elements on the same samples. All six cells share one compile cache, each warmed once and then run for 10 steps.

torchrun --nproc_per_node=2 -m torchtitan.train --module kimi_k3 --config kimi_k3_debugmodel \
  --debug.seed 42 --debug.deterministic --training.steps 10 --metrics.log_freq 1 \
  --training.num-tokens-per-train-step 8192 --training.num-tokens-per-microbatch-per-dp-rank 256 \
  --parallelism.data_parallel_shard_degree 2 --parallelism.expert_parallel_degree 2 \
  --parallelism.spmd_backend spmd_types
cell world backend step 1 step 3 step 10
dp1 1 partial_dtensor 12.52977 7.27107 2.98077
dp1 1 spmd_types 12.52977 7.27107 2.98077
dp2 2 partial_dtensor 12.53137 7.31248 3.15823
dp2 2 spmd_types 12.53137 7.31248 3.15823
dp2 x ep2 2 partial_dtensor 12.53146 7.20212 3.10296
dp2 x ep2 2 spmd_types 12.53146 7.20212 3.10296

Step-1 gradients of the same cell under the two backends, every parameter (rank 0, own dtype, before clipping): dp2 750/750 bitwise, dp2 x ep2 750/750 bitwise, zero sign flips over 1.12e9 elements each.

Noise floor of this flavor (bf16 end to end, lr 8e-4, 2-step warm-up, so Adam's first update is lr times sign(g)): the same dp1 cell on another compile cache reads 12.52977 / 7.36833 / 2.91045 with bitwise step-1 gradients; EP on with the same samples (the dp2 and dp2 x ep2 rows) flips the sign of 1.4% of the gradient elements at step 1. Any two runs that round differently separate by a few percent by step 10; the pairs above do not.

4446's CI cell (kimi_k3_debugmodel_mm_fsdp2: _use_spmd_types(typechecking=True), which turns activation checkpointing off since the checker rejects selective AC with FlexAttention), the cell this PR unbreaks, on its own compile cache; its dp1 is bitwise the AC-on dp1 of that cache:

cell world run step 1 step 3 step 10
dp1 1 AC on, this cache's reference 12.52977 7.36833 2.91045
dp1 1 type checking on, AC off 12.52977 7.36833 2.91045
dp2 2 type checking on, AC off 12.53137 7.22561 3.20438
dp2 x ep2 2 type checking on, AC off 12.53146 7.15088 3.14271

Changed files

torchtitan/models/kimi_k3/
  sharding.py           +332/-0   the declarations: weight types, stream conversions, module boundaries, the tower's plan, the local-map keys; the TP/SP seams, issued at tp = 1
  model.py              +164/-26  the declared modules, the local regions, the multimodal input layout and region, the stream's stack
  kda.py                +13/-7    cu_seqlens keyword-only on InnerKDA.forward; head splits through core's local_head_split

CI/CD Coverage

  • 4446's B200 cell kimi_k3_debugmodel_mm_fsdp2 (this flavor under spmd_types with type checking, dp2) is this path; no new cell. As merged it raises at step 1 on the flavor's inputs (reproduced on 390e2985b with the cell's own recipe); the dp2 row with type checking above is that cell.
  • The TP entries are inert here (tp = 1); the TP/SP PR exercises them: at tp2, with and without SP, float32 per-parameter gradient dumps against dp1 show every replicated parameter bitwise identical across the TP ranks and within noise of dp1.

…er over cp, the MoE seams

Ported from the spmd_types work on the tensor-parallel branch, without
tensor parallelism: the declarations are issued at tp = 1, where spmd_types
still consumes every one of them, so every parameter is a DTensor on the
full mesh and the stream, the tower and the MoE seams carry their layouts.

- weights whose inputs and consumers are TP-invariant (the attention- and
  ffn-residual projections, the output residual projection, routed_up) are
  tp=I; R would sum their identical gradients across TP. routed_down, wq_a,
  wkv_a and forget_a stay R: their consumers are TP-sharded and each rank's
  gradient is a partial sum.
- the empty residual stack is converted R -> I at the model entry; the MLA
  module converts its input I -> R; q_norm/kv_norm are replicated, state
  only; routed_norm reduces the experts' Partial output at its boundary;
  routed_up re-enters Partial so core's MoE exit reduces once; the MoE exit
  returns invariant.
- the MoonViT tower is declared invariant at TP and rank-local over cp, with
  a cp axis on every vision layout (main's include_cp_axis helpers, as
  muse_glimmer passes them) and its own attention entry.
- the MLA head unflatten and rope join run in local regions re-typed
  head-sharded, with the head count derived from the projection width;
  KDA's head views use -1.
- the backend branch every other model carries: FSDP meshes through
  resolve_fsdp_mesh / resolve_sparse_fsdp_mesh with the DataParallelMeshDims
  handed to the decoder and the vision encoder, and model.parallelize
  whenever spmd_types drives the model. preprocess_inputs gives pixel_values
  and grid_thw the DP-local, TP-invariant layout every VLM decoder shares.

Tensor parallelism stays on the unsupported list here; the TP/SP branch
carries it, on the same declarations.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 5, 2026
@QIU023 QIU023 changed the title [Kimi K3] The declarations spmd_types reads: the dense path, the tower over cp, the MoE seams, the multimodal inputs [Kimi K3] Full spmd declaration for K3 CP, EP and TP/SP: the dense path, the tower over cp, the MoE seams, the multimodal inputs Sep 5, 2026
@QIU023 QIU023 changed the title [Kimi K3] Full spmd declaration for K3 CP, EP and TP/SP: the dense path, the tower over cp, the MoE seams, the multimodal inputs [Kimi K3] Full spmd backend declaration for K3 CP, EP and TP/SP: the dense path, the tower over cp, the MoE seams, the multimodal inputs Sep 5, 2026
@QIU023
QIU023 marked this pull request as draft September 5, 2026 09:29
@QIU023
QIU023 marked this pull request as ready for review September 5, 2026 15:02
@tianyu-l
tianyu-l requested a review from shuhuayu September 5, 2026 23:09

@tianyu-l tianyu-l left a comment

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.

numerics difference look too huge, are you testing with identical global batch size across?

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.

Note this change made sharding declaration for CP, EP and TP/SP but not including full TP/SP changes in the model layers, so all run results are having tp=1

what's the gap for TP?

@QIU023 QIU023 Sep 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, I used identical global batch size across.

Regarding the numeric difference, has re-verified with above results and re-ordered the closest comparison experiment (all other setup same, only spmd vs partial_dtensor) and they are completely the same (bit identical) from step1 to step10 loss, so I believe it should be good

Different DP shard number itself changes the DP-shard gradient and enabling EP also adds small numerical diff due to MoE router changes, (especially the bf16 norm reduction issue fix pytorch/pytorch#194033 is still not merged and not get applied in my local run most of the time)

@QIU023 QIU023 Sep 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There is no gap for TP/SP PR since full spmd backend support made TP straightforward, just would like to separate the diff out to make review simplier, the draft PR is already pre-opened and results are verified: #4499,

feel free to give an earlier glance recently or we merge this SPMD first (hold off TP/SP a bit since it's not mentioned in K3 report, cc @shuhuayu ) to unblock CP side rebase and we can save bandwidth to proceed PP and CP PR

@QIU023

QIU023 commented Sep 13, 2026

Copy link
Copy Markdown
Contributor Author

closed this since TP/SP changes moved to a part of #4499 and CP changes moved to a part of #4500

@QIU023 QIU023 closed this Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants