Skip to content

[Module] Remove LocalMapInnerAttention, use static LocalMapSpec - #2986

Merged
fegin merged 76 commits into
mainfrom
gh/fegin/110/head
Apr 28, 2026
Merged

fegin merged 76 commits into
mainfrom
gh/fegin/110/head

Conversation

@fegin

@fegin fegin commented Apr 15, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Replace runtime DTensor detection in LocalMapInnerAttention with static LocalMapSpec on inner attention configs via sharding.py. All inner attention types (SDPA, FlexAttention, Varlen) now use Module.parallelize() local_map for TP DTensor→local conversion.

  • Set LocalMapSpec on inner attention in all model sharding.py files
  • Migrate Flux CP from apply_cp_to_attention_module to apply_cp_to_forward
  • Remove LocalMapInnerAttention class; subclasses inherit Module directly
  • Remove apply_cp_to_attention_module (no remaining callers)
  • Migrate experiments/rl to config-based sharding:
    - VLLMAttentionWrapper inherits Module directly (no runtime DTensor bridge).
    - parallelize_qwen3 replaced with model.parallelize(tp_mesh); legacy parallelize_module plan deleted.
    - set_qwen3_sharding_spec(..., include_positions=True) installed on the config before build() in vllm_wrapper and in trainer.PolicyTrainer._build_model.
    - compute_logprobs materializes DTensor logits to plain via full_tensor() (downstream RL uses plain-tensor indexing).
  • Add include_positions kwarg to set_gqa_attention_sharding so the generator path annotates the positions argument.

fegin added 2 commits April 15, 2026 15:14
[ghstack-poisoned]
[ghstack-poisoned]
fegin added 8 commits April 16, 2026 10:39
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@fegin
fegin marked this pull request as ready for review April 17, 2026 16:51
fegin added 11 commits April 17, 2026 11:31
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
fegin added 7 commits April 24, 2026 13:55
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
# code (gather with plain-tensor indices, slicing per-sample) expects a
# plain tensor — materialize once here.
if isinstance(logits, DTensor):
logits = logits.full_tensor()

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.

why full_tensor not to_local? I'm worried when full DTensor on DP mesh is enabled, this is correct or not

Comment on lines +220 to +229
# Fill sharding configs on the config BEFORE build so every sub-module
# (including VLLMAttentionWrapper) is constructed with its
# ShardingConfig / LocalMapConfig attached. Uses the model-agnostic
# ``update_from_config`` hook. vLLM inference doesn't need RoPE
# cache extension here (that happens explicitly below via
# ``max_model_len``) nor MoE force-load-balance, so ``training`` and
# ``debug`` are omitted.
self.config.update_from_config(
trainer_config=SimpleNamespace(parallelism=parallelism)
)

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.

sounds hacky, please leave a TODO for @wwwjn to refactor TrainingConfig

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 agree this is very hacky. Claude suggests that update_from_config can accept TrainingConfig | ParallelismConfig. But I don't have high confidence this is a good alternative for RL. Will leave a TODO and let @wwwjn work on this. Thanks.

if layer_cfg.moe is not None:
layer_cfg.moe.router._debug_force_load_balance = (
debug.moe_force_load_balance
# ``training`` and ``debug`` are optional: the RL vLLM generator

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.

add a TODO: at least we shouldn't call it trainer_config because it's also used in generator

fegin added 2 commits April 27, 2026 09:27
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Apr 27, 2026
…a3 adoption (#2963)

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0)
(oldest at bottom):
* #2987
* #3113
* #3080
* #2986
* #2969
* __->__ #2963

**Summary**
Replace string-keyed plan dicts passed to `parallelize_module()` with
`ShardingSpec` attached to each `Module.Config`. Sharding is co-located
with the rest of a submodule's configuration and applied by
`Module.parallelize(mesh)` via auto-recursion.

**NOTE that we have to do some compromise designs due to the backward
compatibility issues, these should be removed either full DTensor is
applied or at some time in the future**

**Known TODOs for follow-up:**
   
- apply_cp_to_forward: This is a workaround to keep the current CP work.
Remove once all models use config-based sharding + full DTensor
- input_layouts: Another workaround. We can remove it once all inputs
flow as DTensors
- LocalMapInnerAttention: remove entire class once all models use
config-based sharding (#2986)
- parallelize(tp_mesh): pass full SPMD mesh once FSDP/CP also uses
DTensor (#2987)
- _shard_outputs: extend to support nested outputs (tuples/dicts) for
multi-tensor forward
fegin added a commit that referenced this pull request Apr 27, 2026
…eek V3, GPT-OSS (#2969)

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0)
(oldest at bottom):
* #2987
* #3113
* #3080
* #2986
* __->__ #2969
* #2963

Convert dense-layer TP and attention for all remaining models to
config-based `ShardingSpec`, leaving MoE part unchanged, still using
`apply_moe_ep_tp()`. After this PR, every model's attention uses
`Module.parallelize()`.

**We will remove LocalInnerAttention in the next PR.**
**Models that are not converted: Flux and qwen3-vl.**

1. Flux doesn't support TP. So it is out of the current scope. We can
convert it during the full dtensor PRs.
2. qwen3-vl vision part is hard to convert without full dtensor support.
It is doable but let's not do the hacky way and wait until the full
dtensor PRs.
[ghstack-poisoned]
@fegin fegin added the ciflow/h100.8 Trigger H100.8 CI label Apr 27, 2026
fegin added 2 commits April 27, 2026 14:11
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@fegin
fegin changed the base branch from gh/fegin/110/base to main April 28, 2026 05:52
@fegin
fegin changed the base branch from main to gh/fegin/110/base April 28, 2026 05:55
fegin added 4 commits April 27, 2026 23:55
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@fegin
fegin changed the base branch from gh/fegin/110/base to main April 28, 2026 08:09
@fegin
fegin merged commit b9e3352 into main Apr 28, 2026
11 of 14 checks passed
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Apr 28, 2026
Upstream pytorch#2986 removed the LocalMapInnerAttention class that did runtime
DTensor detection. Replaced by a static LocalMapConfig set on the inner-
attention sharding_config via set_gqa_inner_attention_local_map(). All
inner attention types (SDPA, FlexAttention, Varlen) now inherit Module
directly.

- agpt/__init__.py:
  - SoftcappedFlexAttention: base class LocalMapInnerAttention -> Module.
    Now relies on the static LocalMapConfig wired in sharding.py rather
    than runtime DTensor detection.
  - _ezpz_get_attention_config: return type annotation
    LocalMapInnerAttention.Config -> Module.Config.
- moe/model.py: Attention.Config.inner_attention type
  LocalMapInnerAttention.Config -> Module.Config.
- agpt/sharding.py + moe/sharding.py: add
  set_gqa_inner_attention_local_map(layer_cfg.attention.inner_attention)
  call so inner attention gets the LocalMapConfig it now needs to bridge
  TP DTensors -> local tensors at the kernel boundary.
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Apr 28, 2026
- moe/__init__.py: extend model_registry() with a quantization parameter
  that takes list[QuantizationConverter.Config]. Each converter is
  applied to the model config via q.build().convert(config) before the
  ModelSpec is returned. Mirrors upstream's deepseek_v3 pattern.
- moe/config_registry.py: moe_671b() now re-registers via
  cfg.model_spec = model_registry("671B", quantization=[...]) instead
  of mutating cfg.model_converters (which no longer exists). Drop the
  ModelConvertersContainer import.
- upstream-sync.md: 22nd-sync entry covering pytorch#3127 (quantize-on-config),
  pytorch#2986 (LocalMapInnerAttention removal), and the MeshDimName ->
  MeshAxisName rename, plus what was replayed.
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Apr 29, 2026
22nd sync (pytorch#3127 quantize-on-config, pytorch#2986 LocalMapInnerAttention removal,
pytorch#3113 MeshDimName rename). Documents the four replay commits, the
Float8GroupedMMConverter -> Float8GroupedExpertsConverter rename caught
during smoke-testing, and the smoke-test results (agpt 12.92 -> 7.14
across 50 steps, exit 0; moe in flight on the same trajectory).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/h100.8 Trigger H100.8 CI ciflow/8gpu 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.

3 participants