Repository navigation
Conversation
…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.
tianyu-l
left a comment
There was a problem hiding this comment.
numerics difference look too huge, are you testing with identical global batch size across?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
Summary
spmd_typesbackend 4446 turned on, what 4446 left toannotate_replicated_parameters: the dense path's weight types and stream conversions, the MoonViT tower over cp, the latent MoE's seams, the multimodal inputs.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'skimi_k3_debugmodel_mm_fsdp2cell).kimi_k3/sharding.pycarries the declarations (declarations only, applied through the Module protocol, the qwen3_5 shape), issued at tp = 1 wherespmd_typesreads every one of them;preprocess_inputsgivespixel_valuesandgrid_thwthe DP-local, TP-invariant layout every VLM decoder shares.Design
norm_config's rule. Without SP the stream is invariant on TP and every module converts itI -> Ron entry, so the residual projections,output_res_projandrouted_upsee one gradient on every rank and areI; under SP (the TP/SP PR) they areRand FSDP sums the per-rank partials.routed_down,wq_a,wkv_aandforget_aareReither way: their consumers are TP-sharded, each rank's gradient is a partial sum.I -> Ron 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_normreduces the experts' Partial output at its boundary (keyedx, the argumentnn.RMSNorm.forwardtakes);routed_upre-enters Partial so core's MoE exit reduces once.include_cp_axis=Truehelpers 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:Iwithout SP,Runder SP, where the exit's backward all-reduce sums the shards' feature gradients.spmd.local()regions re-typed head-sharded on TP; KDA's head splits go through core'slocal_head_split, a plain view cannot split a feature-sharded dim.multimodal_context()and the stream is re-asserted on the decoder's layout after it, as qwen3_5 and kimi_k2_7 do.forward()parameter names (q_THK,k_THK,v_THV;raw_gate_THK,raw_beta_TH,cu_seqlensonInnerKDA): alocal_mapneeds an entry per positional parameter, and a key matching nothing declares nothing.parallelize.pyis 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/mainb19162eand its SM100/SM103 guard inkda.pylifted 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.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:Changed files
CI/CD Coverage
kimi_k3_debugmodel_mm_fsdp2(this flavor underspmd_typeswith type checking, dp2) is this path; no new cell. As merged it raises at step 1 on the flavor's inputs (reproduced on390e2985bwith the cell's own recipe); the dp2 row with type checking above is that cell.