Repository navigation
Conversation
|
|
tianyu-l
left a comment
There was a problem hiding this comment.
When checkpoint loading excludes the optimizer with exclude_from_loading=["optimizer"], the newly constructed optimizer retains its initialization learning rate. The scheduler metric reports the restored rate, but the first optimizer.step() uses the stale rate; scheduler.step() only realigns it afterward.
I wonder if we should ban this case: exclude_from_loading should always include optimizer and lr_scheduler or exclude them simultaneously. Or is there a valid use case to independently exclude them when resuming checkpoint?
3d17be2 to
478fdf7
Compare
fegin
left a comment
There was a problem hiding this comment.
I don't know why people would only load LRScheduler but not optimizer state_dict. From the document, exclude_from_loading seems to be introduced to do resharding as LRScheduler and dataloader do not support resharding. Maybe we should just re-brand this flag to do resharding only for model and optimizer unless there is a specific use case other than resharding.
|
@tianyu-l Problem summaryTorchTitan's ChatDataset pads incomplete packed sequences with EOS tokens and sets the corresponding labels to IGNORE_INDEX. However, neither IGNORE_INDEX nor an attention mask prevents those padding positions from passing through the Transformer blocks and reaching the MoE router. An attention mask controls which keys/values are visible during attention. It does not remove the query position from the model, skip the FFN/MoE sublayer, or suppress stateful side effects inside the router. IGNORE_INDEX only removes the final language-model loss for that position. The resulting execution is still: In the current TorchTitan MoE implementation, tokens_per_expert_E is computed by summing the complete routing map. Consequently, padding tokens are treated as real expert load. These invalid counts are later globally reduced and used by the auxiliary-loss-free load-balancing hook to update expert_bias_E. This can cause:
The validity mask cannot safely be inferred from labels because prompt tokens can also have IGNORE_INDEX while remaining valid model inputs. It also cannot be inferred from the token ID because TorchTitan uses EOS for padding, while EOS is also a valid document-boundary token. TorchTitan source locationsRepository:
Source path: Relevant class/function: Relevant upstream lines at the time of investigation:
Source path: Relevant class/function: Relevant upstream lines:
Source path: Relevant class/function: Relevant upstream lines:
Source path: Relevant function: Relevant upstream lines:
Why this is an architectural issueThere are three different token-mask semantics:
TorchTitan currently has the first two concepts in different forms, but no first-class valid-token contract that can be propagated through the data pipeline, model input API, CP/TP/SPMD layouts, and the MoE router. This is why a seemingly small MoE fix quickly expands into dataset, Trainer/Validator, model-forward, context-parallel, SPMD-sharding, and MoE changes. I do not think such a broad patch should be submitted before the intended input contract is agreed upon. Research: other distributed/MoE frameworks
Megatron-LM currently has the most complete explicit design. MoELayer.forward accepts padding_mask and passes it into the router. The router uses it when calculating router z-loss, auxiliary load-balancing losses, valid-token counts, and auxiliary-loss-free expert-bias statistics. Important detail: for expert-bias statistics, Megatron masks the routing map used for statistics. The routing/dispatch result can remain separate from the statistical count. This distinction is useful for a conservative correctness fix. Source paths:
Relevant symbols:
Research URLs:
DeepSpeed's MoE Top-1 gating path accepts used_token. It multiplies the one-hot expert mask by used_token before calculating exp_counts, capacity, combine weights, and the dispatch mask. However, support is not uniform across every Top-K implementation: TopKGate forwards used_token to the Top-1 path, while the Top-2 and general Top-K paths have different interfaces. It is therefore useful prior art, but not a complete framework-wide token-validity contract. Source path: Relevant symbols:
Flaxformer exposes ignore_padding_tokens in its router. Depending on the router implementation, padding tokens may be removed from routing masks or have their routing probabilities down-weighted. Its load-balancing calculation is also based on the masked routing information. The implementation infers padding by checking whether the token hidden state is all zero. That convention is suitable for its T5-style stack, but it would be fragile as a general TorchTitan contract because residual connections, biases, and model-specific embeddings do not guarantee zero hidden states for padding positions. Source path: Relevant symbols:
NLLB-MoE passes padding information into its router and masks padding positions in the expert routing masks. Transformers also contains MoE load-balancing functions that use attention_mask to exclude padding from tokens_per_expert and router-probability statistics. This demonstrates the expected mathematical behavior, but the implementation is model-specific and overloads attention_mask rather than defining a universal valid-token abstraction. Source path: Relevant symbols:
DeepSeek-V3 technical report, including auxiliary-loss-free load balancing: Switch Transformers, including expert load-balancing loss: GShard, including sparse expert routing and capacity: Suggested directionI suggest agreeing on a first-class token-validity contract before implementing a broad patch. The conceptual API should keep attention visibility, loss participation, and token validity separate. A staged approach could be: Phase 1: correctness only
Phase 2: reduce padding at the source
Phase 3: performance optimization
Suggested correctness criterionFor the same real-token sequence, adding arbitrary right padding must not change:
The existing behavior should remain unchanged when no padding is present. I initially explored an implementation, but correctly propagating a new mask required changes across the dataset, Trainer/Validator input processing, model forward APIs, context parallelism, SPMD layouts, and MoE sharding. That seems too broad for a small bug-fix PR, so I am raising this new architectural concern here first and would prefer to agree on the contract and scope before proposing code. |
478fdf7 to
27b58a4
Compare
|
@taking-lying-flat thanks a lot. We are aware of this issue in torchtitan, and plan to fix. It'd be great if you could help. Let's go with what you said
At a high level, we should pass this padding mask from dataloader (for pretraining) and batcher (for RL training). In particular, the megatron direction sounds proper. If you have a more concrete (pseudo)code proposal, I can help view. Thanks again! btw we are consolidating bsz and seq_len dimension, in #4121 |
|
could you rebase and fix lint |
27b58a4 to
c220794
Compare
Summary
lr_schedulerwhile excludingoptimizerlr_scheduleris excluded andoptimizeris loadedLRSchedulersContainer.load_state_dict()responsible only for restoring scheduler stateRoot cause
The previous version of this PR repaired the optimizer learning rate after loading scheduler state without optimizer state. Review feedback identified that this mixed restore has no established supported use case: the scheduler and optimizer states are coupled, and restoring only the scheduler leaves a newly initialized optimizer with inconsistent state.
The configuration is now rejected before checkpoint loading instead of silently repairing part of an unsupported state combination.
Impact
exclude_from_loading=["optimizer"]now raises a clear configuration error unlesslr_scheduleris also excluded. Full optimizer and scheduler restores are unchanged. Existing resharding configurations that load the optimizer while excluding the scheduler remain supported.Validation
ufmtformatting checkpyreflytype checkNo test files are added or changed.