Repository navigation
Conversation
QIU023
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
September 10, 2026 09:07
QIU023
marked this pull request as draft
September 10, 2026 09:07
4 of 18 tasks
trainable_state_dict returns the parameters left trainable, keyed as in state_dict(): under LoRA, the adapters, which is what an adapter checkpoint or a trainer-to-rollout sync ships. merge_lora_state_dict returns state_dict() with each base weight replaced by W + (alpha / rank) * B @ A and the adapter keys removed, the form a state-dict adapter can convert like the unadapted model's. The merge runs in fp32 on each rank's local shards and casts back to the base dtype. B is sharded like the base's output rows, A is gathered only where FSDP cuts its rank dimension, and the result keeps the base's DTensor placements, so no base weight is gathered. DTensor ops on the sharded tensors fail on a stacked w13 under FSDP and TP, whose _StridedShard placement cannot be unflattened.
_adapter_sharding compared the base weight's placement with dense_param_placement(tp=S(0)) and asserted that every other base was dense_param_placement(tp=S(1)). Two kinds of base failed that assert at build, so LoRATransform with its default targets could not build deepseek_v3, deepseek_v4, gpt_oss, qwen3_5, kimi_k2_5 or kimi_k3: bases that TP does not shard (R when tokens are TP-sharded, I otherwise), such as the MoE router gate and the MLA down projections, and the vision encoder's column and row parallel linears, which are I rather than R on CP. The adapters now follow the TP entry of the base placement: S(0) and S(1) keep the column and row rules, R and I give both adapters the base placement, and any other TP placement raises. The replicated adapter copies the base placement with TP set to R, so its DP and CP entries are the base's; for a dense base that is dense_param_placement(tp=R) as before.
Adapter training under pipeline parallelism can leave a stage with nothing trainable, for example a stage whose layers no LoRA target matches. The optimizer container raised "matched no parameters" for such a model part; it now skips the part with a log line, and a part that trains still raises on a pattern that matches nothing. When every part on a rank is frozen, the container still initializes as an Optimizer with one empty parameter group, the LR scheduler container holds no scheduler and saves and loads no state, and clip_grad_norm_ puts the rank's zero norm on the accelerator so it joins the pipeline all-reduce; it was a CPU tensor, which NCCL rejects.
|
The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:
Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows. |
pyrefly reads module.lora_a.weight as a Parameter, so the DTensor calls in the sharded branch of _merged_weight failed the type check. The adapters are DTensors whenever the base is; the branch now asserts it.
Both tests spawn a process group (2 and 4 ranks). Without the marker they land in the single-GPU lane, where the device-count guard skips them, and the multi_gpu lane does not select them.
QIU023
marked this pull request as ready for review
October 14, 2026 08:47
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
With its default targets, LoRA on main fails to build the Kimi K3 debug model, cannot export a trained adapter, and fails on a pipeline rank with nothing to train; this PR fixes all three.
torchtitan/models/common/lora.py:trainable_state_dict,merge_lora_state_dict, and adapter shardings that follow the TP entry of the base placement.torchtitan/components/optim/optimizer.py,lr_scheduler.py,torchtitan/distributed/utils.py: a rank whose model parts are all frozen gets an empty optimizer and scheduler and still joins the grad norm all-reduce.Design
trainable_state_dictreturns the trainable parameters keyed as instate_dict(); under LoRA, the adapters.merge_lora_state_dictreturnsstate_dict()with_StridedShardplacement of a stackedw13under FSDP and TP._adapter_shardingasserts that every base isdense_param_placement(tp=S(0))ordense_param_placement(tp=S(1)); bases that TP does not shard (router gate, MLAwkv_a,forget_a) and vision linears (Ion CP) fail that assert when the model is built.S(0)andS(1)keep the column and row rules,RandIgive both adapters the base placement, anything else raises.R; for a dense base this is the placement main already uses.tensor(0.), which the NCCL all-reduce rejects.Results
8 x RTX 5060 Ti,$W + \frac{\alpha}{r} B A$ computed from the gathered tensors. Measured at
kimi_k3_debugmodel(17 layers, dim 256) on the multimodalcc12m-test, 4096 tokens per step, seed 42, deterministic.LoRATransform(rank=8, alpha=16.0)adapts all 290 supported projections. Every cell loads one seed checkpoint, trains 10 steps, then compares the output ofmerge_lora_state_dicton every rank withb61c686dc; the two later commits only add a type assertion and themulti_gputest markers. The cell configs, the merge check and the runners are in this kit, not in this PR.Step 1 is identical to its reference in every layout without TP or CP and within 0.005% with TP or CP. On every rank of every layout, every merged weight is bitwise identical to$W + \frac{\alpha}{r} B A$ computed from the gathered tensors.
8.136237.092630290 on 1 rank
8.13623identical
7.091420290 on 2 ranks
8.13623identical
7.093440290 on 4 ranks
8.13623identical
7.091420290 on 8 ranks
8.13642+0.0023%
7.094880580 on 2 ranks
8.13640+0.0021%
7.0951701160 on 4 ranks
8.13626+0.0004%
7.095200580 on 2 ranks
8.13628+0.0006%
7.0916001160 on 8 ranks
8.187277.170450580 on 2 ranks
8.18727identical
7.172500580 on 2 ranks
8.18761+0.0042%
7.1703401160 on 8 ranks
8.18761+0.0042%
7.1722001160 on 8 ranks
8.18745+0.0022%
7.1725201160 on 8 ranks
8.18745+0.0022%
7.1707301160 on 8 ranks
8.18752+0.0031%
7.1726002320 on 8 ranks
8.18752+0.0031%
7.1726002320 on 8 ranks
8.162207.0730502320 on 8 ranks
8.148167.3426701160 on 4 ranks
8.100627.97847025 on 4 ranks
8.10062identical
7.97848025 on 8 ranks
target_modules= the MLA projections, so the stages that hold only KDA layers have nothing to train: pp4 x vpp2 puts a frozen stage next to a trainable one on three ranks, and pp8 leaves three ranks with nothing to train; both layouts rely on this PR's optimizer, scheduler and grad norm changesTest plan
The GPU merge test covers a stacked
w13under TP2 x FSDP2; the clip test raisesNo backend type associated with device type cpuon main.pyrefly checkreports the same errors as main.