Repository navigation
[CP] Fix positions handling with CP + block_causal + flexattention - #2780
Merged
Merged
Conversation
[ghstack-poisoned]
fegin
added a commit
that referenced
this pull request
Apr 1, 2026
**Issue:** When CP is enabled with block_causal + FlexAttention, the dataloader provides per-document positions in `extra_inputs` AND `prepare_context_parallel_input()` creates its own sequential positions in extra_kwargs. Both dicts are unpacked in the model call, causing "got multiple values for keyword argument 'positions'". Additionally, the sequential positions overwrote the dataloader's per-document positions, which are needed for correct RoPE with packed sequences. **Fix:** Consolidate all positions resolution in `post_dataloading_process` and make `prepare_context_parallel_input` a pure sharding function. - `post_dataloading_process` now always pops `positions` from `extra_inputs` and places them in `extra_kwargs` when needed: dataloader per-document `positions` for block_causal, sequential arange `positions` for CP with causal attention, or nothing (model defaults to sequential RoPE slice). - `prepare_context_parallel_input` no longer creates `positions` — it just shards whatever the caller provides in `extra_kwargs["positions"]`. - `Positions` always go into `extra_kwargs` (forwarded to all PP stages) rather than extra_inputs (first stage only), which is correct since every stage needs them for RoPE. **Verification:** - CP2+TP2+FlexAttention - FlexAttention-only - SDPA+CP2+TP2 - SDPA-only ghstack-source-id: 236331b Pull-Request: #2780
tianyu-l
approved these changes
Apr 1, 2026
[ghstack-poisoned]
fegin
added a commit
that referenced
this pull request
Apr 2, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * #2785 * #2780 * __->__ #2633 ## Motivation Parameter initialization should be configurable without modifying module code. Previously, each module defined its own `init_weights` method with hardcoded init logic (e.g., `nn.init.trunc_normal_` with a fixed std). This couples initialization strategy to module definitions, making it impossible to change init behavior without editing model code. ## Design - **Expand the config to be per-module config** - `param_init` is a `dict[str, Callable]` on `Module.Config`, mapping local parameter names to init functions. It is set on every sub-config in the model config registry. - `init_states` auto-recurses the module tree (children first, then self), calling `_init_self_parameters` and `_init_self_buffers` on each module. Buffer initialization remains module-specific (e.g., RoPE cache, MoE counters) via `_init_self_buffers` overrides. - `DeferredCallable.Config` wraps init functions that vary by layer depth (e.g., depth-scaled std). `resolve_deferred` walks the config tree and resolves these markers during per-layer config expansion. - `expand_layer_configs` (per model) deep-copies the layer template N times, resolves deferred markers, and handles model-specific layer interleaving (MoE vs dense, iRoPE, sliding attention). ## Summary of changes - Remove all `init_weights` methods from modules (Linear, Embedding, RMSNorm, FeedForward, GQAttention, MoE, GroupedExperts, TokenChoiceTopKRouter, RoPE, Decoder). - Add `param_init` field to `Module.Config`; `Config.build()` propagates it to the instance as `_param_init`. - Add `init_states` with auto-recursion, `_init_self_parameters` (dict-lookup init), and `_init_self_buffers` (module-specific buffer init). - Add `DeferredCallable` (in `torchtitan/config/`) for depth-varying init. - Add `param_init.py` (in `torchtitan/models/common/`) with `resolve_deferred`, `skip_param_init`, and `depth_scaled_std` utilities. - Remove `init_mean`/`init_std` fields from `Linear.Config` and `Embedding.Config` — init is now expressed entirely through `param_init` dicts. - Add `layers` list and `expand_layer_configs()` pattern to all model registries, replacing per-layer config construction in `Decoder.__init__`. - Add `w1`/`w2w3` fields to `FeedForward.Config` and `wqkv`/`wo` fields to `GQAttention.Config` (replacing `linear_bias: bool`), so each linear sub-module gets its own `param_init`. ## Verification Bitwise identical loss against `origin/main` with `--debug.seed=42 --debug.deterministic`: | Model | Steps | Result | |--------|-------|-------------------| | Llama3 | 10 | Bitwise identical |
[ghstack-poisoned]
fegin
added a commit
that referenced
this pull request
Apr 2, 2026
**Issue:** When CP is enabled with block_causal + FlexAttention, the dataloader provides per-document positions in `extra_inputs` AND `prepare_context_parallel_input()` creates its own sequential positions in extra_kwargs. Both dicts are unpacked in the model call, causing "got multiple values for keyword argument 'positions'". Additionally, the sequential positions overwrote the dataloader's per-document positions, which are needed for correct RoPE with packed sequences. **Fix:** Consolidate all positions resolution in `post_dataloading_process` and make `prepare_context_parallel_input` a pure sharding function. - `post_dataloading_process` now always pops `positions` from `extra_inputs` and places them in `extra_kwargs` when needed: dataloader per-document `positions` for block_causal, sequential arange `positions` for CP with causal attention, or nothing (model defaults to sequential RoPE slice). - `prepare_context_parallel_input` no longer creates `positions` — it just shards whatever the caller provides in `extra_kwargs["positions"]`. - `Positions` always go into `extra_kwargs` (forwarded to all PP stages) rather than extra_inputs (first stage only), which is correct since every stage needs them for RoPE. **Verification:** - CP2+TP2+FlexAttention - FlexAttention-only - SDPA+CP2+TP2 - SDPA-only ghstack-source-id: 74d3309 Pull-Request: #2780
Contributor
|
this seems to be causing CI error https://github.com/pytorch/torchtitan/actions/runs/23934353369/job/69807768323?pr=2741 . Happen to debug this for my PR: #2741 |
TXacs
pushed a commit
to McmillanTAC/torchtitan
that referenced
this pull request
Apr 13, 2026
…orch#2633) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * pytorch#2785 * pytorch#2780 * __->__ pytorch#2633 ## Motivation Parameter initialization should be configurable without modifying module code. Previously, each module defined its own `init_weights` method with hardcoded init logic (e.g., `nn.init.trunc_normal_` with a fixed std). This couples initialization strategy to module definitions, making it impossible to change init behavior without editing model code. ## Design - **Expand the config to be per-module config** - `param_init` is a `dict[str, Callable]` on `Module.Config`, mapping local parameter names to init functions. It is set on every sub-config in the model config registry. - `init_states` auto-recurses the module tree (children first, then self), calling `_init_self_parameters` and `_init_self_buffers` on each module. Buffer initialization remains module-specific (e.g., RoPE cache, MoE counters) via `_init_self_buffers` overrides. - `DeferredCallable.Config` wraps init functions that vary by layer depth (e.g., depth-scaled std). `resolve_deferred` walks the config tree and resolves these markers during per-layer config expansion. - `expand_layer_configs` (per model) deep-copies the layer template N times, resolves deferred markers, and handles model-specific layer interleaving (MoE vs dense, iRoPE, sliding attention). ## Summary of changes - Remove all `init_weights` methods from modules (Linear, Embedding, RMSNorm, FeedForward, GQAttention, MoE, GroupedExperts, TokenChoiceTopKRouter, RoPE, Decoder). - Add `param_init` field to `Module.Config`; `Config.build()` propagates it to the instance as `_param_init`. - Add `init_states` with auto-recursion, `_init_self_parameters` (dict-lookup init), and `_init_self_buffers` (module-specific buffer init). - Add `DeferredCallable` (in `torchtitan/config/`) for depth-varying init. - Add `param_init.py` (in `torchtitan/models/common/`) with `resolve_deferred`, `skip_param_init`, and `depth_scaled_std` utilities. - Remove `init_mean`/`init_std` fields from `Linear.Config` and `Embedding.Config` — init is now expressed entirely through `param_init` dicts. - Add `layers` list and `expand_layer_configs()` pattern to all model registries, replacing per-layer config construction in `Decoder.__init__`. - Add `w1`/`w2w3` fields to `FeedForward.Config` and `wqkv`/`wo` fields to `GQAttention.Config` (replacing `linear_bias: bool`), so each linear sub-module gets its own `param_init`. ## Verification Bitwise identical loss against `origin/main` with `--debug.seed=42 --debug.deterministic`: | Model | Steps | Result | |--------|-------|-------------------| | Llama3 | 10 | Bitwise identical |
TXacs
pushed a commit
to McmillanTAC/torchtitan
that referenced
this pull request
Apr 13, 2026
…ytorch#2780) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * __->__ pytorch#2780 **Issue:** When CP is enabled with block_causal + FlexAttention, the dataloader provides per-document positions in `extra_inputs` AND `prepare_context_parallel_input()` creates its own sequential positions in extra_kwargs. Both dicts are unpacked in the model call, causing "got multiple values for keyword argument 'positions'". Additionally, the sequential positions overwrote the dataloader's per-document positions, which are needed for correct RoPE with packed sequences. **Fix:** Consolidate all positions resolution in `post_dataloading_process` and make `prepare_context_parallel_input` a pure sharding function. - `post_dataloading_process` now always pops `positions` from `extra_inputs` and places them in `extra_kwargs` when needed: dataloader per-document `positions` for block_causal, sequential arange `positions` for CP with causal attention, or nothing (model defaults to sequential RoPE slice). - `prepare_context_parallel_input` no longer creates `positions` — it just shards whatever the caller provides in `extra_kwargs["positions"]`. - `Positions` always go into `extra_kwargs` (forwarded to all PP stages) rather than extra_inputs (first stage only), which is correct since every stage needs them for RoPE. **Verification:** - CP2+TP2+FlexAttention - FlexAttention-only - SDPA+CP2+TP2 - SDPA-only
ACharacterInASimulation
pushed a commit
to ACharacterInASimulation/torchtitan
that referenced
this pull request
Apr 21, 2026
…orch#2633) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * pytorch#2785 * pytorch#2780 * __->__ pytorch#2633 ## Motivation Parameter initialization should be configurable without modifying module code. Previously, each module defined its own `init_weights` method with hardcoded init logic (e.g., `nn.init.trunc_normal_` with a fixed std). This couples initialization strategy to module definitions, making it impossible to change init behavior without editing model code. ## Design - **Expand the config to be per-module config** - `param_init` is a `dict[str, Callable]` on `Module.Config`, mapping local parameter names to init functions. It is set on every sub-config in the model config registry. - `init_states` auto-recurses the module tree (children first, then self), calling `_init_self_parameters` and `_init_self_buffers` on each module. Buffer initialization remains module-specific (e.g., RoPE cache, MoE counters) via `_init_self_buffers` overrides. - `DeferredCallable.Config` wraps init functions that vary by layer depth (e.g., depth-scaled std). `resolve_deferred` walks the config tree and resolves these markers during per-layer config expansion. - `expand_layer_configs` (per model) deep-copies the layer template N times, resolves deferred markers, and handles model-specific layer interleaving (MoE vs dense, iRoPE, sliding attention). ## Summary of changes - Remove all `init_weights` methods from modules (Linear, Embedding, RMSNorm, FeedForward, GQAttention, MoE, GroupedExperts, TokenChoiceTopKRouter, RoPE, Decoder). - Add `param_init` field to `Module.Config`; `Config.build()` propagates it to the instance as `_param_init`. - Add `init_states` with auto-recursion, `_init_self_parameters` (dict-lookup init), and `_init_self_buffers` (module-specific buffer init). - Add `DeferredCallable` (in `torchtitan/config/`) for depth-varying init. - Add `param_init.py` (in `torchtitan/models/common/`) with `resolve_deferred`, `skip_param_init`, and `depth_scaled_std` utilities. - Remove `init_mean`/`init_std` fields from `Linear.Config` and `Embedding.Config` — init is now expressed entirely through `param_init` dicts. - Add `layers` list and `expand_layer_configs()` pattern to all model registries, replacing per-layer config construction in `Decoder.__init__`. - Add `w1`/`w2w3` fields to `FeedForward.Config` and `wqkv`/`wo` fields to `GQAttention.Config` (replacing `linear_bias: bool`), so each linear sub-module gets its own `param_init`. ## Verification Bitwise identical loss against `origin/main` with `--debug.seed=42 --debug.deterministic`: | Model | Steps | Result | |--------|-------|-------------------| | Llama3 | 10 | Bitwise identical |
ACharacterInASimulation
pushed a commit
to ACharacterInASimulation/torchtitan
that referenced
this pull request
Apr 21, 2026
…ytorch#2780) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * __->__ pytorch#2780 **Issue:** When CP is enabled with block_causal + FlexAttention, the dataloader provides per-document positions in `extra_inputs` AND `prepare_context_parallel_input()` creates its own sequential positions in extra_kwargs. Both dicts are unpacked in the model call, causing "got multiple values for keyword argument 'positions'". Additionally, the sequential positions overwrote the dataloader's per-document positions, which are needed for correct RoPE with packed sequences. **Fix:** Consolidate all positions resolution in `post_dataloading_process` and make `prepare_context_parallel_input` a pure sharding function. - `post_dataloading_process` now always pops `positions` from `extra_inputs` and places them in `extra_kwargs` when needed: dataloader per-document `positions` for block_causal, sequential arange `positions` for CP with causal attention, or nothing (model defaults to sequential RoPE slice). - `prepare_context_parallel_input` no longer creates `positions` — it just shards whatever the caller provides in `extra_kwargs["positions"]`. - `Positions` always go into `extra_kwargs` (forwarded to all PP stages) rather than extra_inputs (first stage only), which is correct since every stage needs them for RoPE. **Verification:** - CP2+TP2+FlexAttention - FlexAttention-only - SDPA+CP2+TP2 - SDPA-only
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.
Stack from ghstack (oldest at bottom):
Issue:
When CP is enabled with block_causal + FlexAttention, the dataloader provides per-document positions in
extra_inputsANDprepare_context_parallel_input()creates its own sequential positions inextra_kwargs. Both dicts are unpacked in the model call, causing "got multiple values for keyword argument 'positions'". Additionally, the sequential positions overwrote the dataloader's per-document positions, which are needed for correct RoPE with packed sequences.
Fix:
Consolidate all positions resolution in
post_dataloading_processand makeprepare_context_parallel_inputa pure sharding function.post_dataloading_processnow always popspositionsfromextra_inputsand places them in
extra_kwargswhen needed: dataloader per-documentpositionsfor block_causal, sequential arangepositionsfor CP with causal attention, or nothing (model defaults to sequential RoPE slice).prepare_context_parallel_inputno longer createspositions— it just shards whatever the caller provides inextra_kwargs["positions"].Positionsalways go intoextra_kwargs(forwarded to all PP stages) rather than extra_inputs (first stage only), which is correct since every stage needs them for RoPE.Verification: