Skip to content

[Attention] Remove MATH from ScaledDotProductAttention default backends - #3080

Merged
fegin merged 34 commits into
mainfrom
gh/fegin/112/head
Apr 28, 2026
Merged

fegin merged 34 commits into
mainfrom
gh/fegin/112/head

Conversation

@fegin

@fegin fegin commented Apr 24, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

MATH backend has been an issue in the past, e.g., silently fall back to MATH backend causing CP debugging issue. While we have local_map to avoid complicated interaction between MATH attention with DTensor, it doesn't worth to keep another one if it make developers like harder and we don't proactively develop with it.

This PR just removes it.

fegin added 2 commits April 23, 2026 18:04
[ghstack-poisoned]
[ghstack-poisoned]
fegin added 18 commits April 23, 2026 21:40
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
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.
fegin added 10 commits April 27, 2026 11:47
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Apr 28, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0)
(oldest at bottom):
* #2987
* #3113
* #3080
* __->__ #2986

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 28, 2026 01:31
[ghstack-poisoned]
[ghstack-poisoned]
@fegin
fegin changed the base branch from gh/fegin/112/base to main April 28, 2026 16:40
@fegin
fegin changed the base branch from main to gh/fegin/112/base April 28, 2026 16:42
@fegin
fegin changed the base branch from gh/fegin/112/base to main April 28, 2026 16:43
@fegin
fegin merged commit 112fd92 into main Apr 28, 2026
9 of 11 checks passed
fegin added a commit that referenced this pull request Apr 28, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0)
(oldest at bottom):
* #2987
* __->__ #3113
* #3080

Cosmetic follow-up to PR1 review:

**The major convention change is using `axis` for Mesh. So this changes
many docstring, comment and code. But we cannot change the upstream
PyTorch DeviceMesh API yet due to the BC issues**

- Move ``StrEnum`` and the mesh-axis-name enum to
``torchtitan/protocols/typespy``.
- Rename ``MeshDimName`` -> ``MeshAxisName``. Establish the convention
that throughout torchtitan code we own, ``axis``/``axes`` refers to a
``DeviceMesh`` axis and ``dim``/``dimension`` is reserved for tensor
dimensions. The exception: when calling into PyTorch upstream APIs
(``DeviceMesh.mesh_dim_names``, ``DataParallelMeshDims``) we keep the
upstream spelling at the call site since we cannot rename it; values
flowing through our code are then named with ``axis``.
- Add the rule to ``.claude/CLAUDE.md`` (Naming) and update
``.claude/rules/distributed.md`` to match.

No behavior change; pre-commit clean.
fegin added a commit that referenced this pull request Apr 28, 2026
…t backends (#3080)"

This reverts commit 112fd92.

After landing #3080, many CI jobs report "no kernel" errors because
without MATH in the default backend list SDPA cannot dispatch for
several training shapes/configurations. Restoring MATH as a fallback
unblocks CI; we can revisit a narrower fix later.


ghstack-source-id: d48523b
Pull-Request: #3135
fegin added a commit that referenced this pull request Apr 28, 2026
…t backends (#3080)"

This reverts commit 112fd92.

After landing #3080, many CI jobs report "no kernel" errors because
without MATH in the default backend list SDPA cannot dispatch for
several training shapes/configurations. Restoring MATH as a fallback
unblocks CI; we can revisit a narrower fix later.


ghstack-source-id: 86193fa
Pull-Request: #3135
fegin added a commit that referenced this pull request Apr 28, 2026
…t backends (#3080)"

This reverts commit 112fd92.

After landing #3080, many CI jobs report "no kernel" errors because
without MATH in the default backend list SDPA cannot dispatch for
several training shapes/configurations. Restoring MATH as a fallback
unblocks CI; we can revisit a narrower fix later.

ghstack-source-id: 4a377f1
Pull-Request: #3135
fegin added a commit that referenced this pull request Apr 28, 2026
…t backends (#3080)" (#3135)

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0)
(oldest at bottom):
* __->__ #3135

This reverts commit 112fd92.

After landing #3080, many CI jobs report "no kernel" errors because
without MATH in the default backend list SDPA cannot dispatch for
several training shapes/configurations. Restoring MATH as a fallback
unblocks CI; we can revisit a narrower fix later.


**Any lint errors are pre-existing. **
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

2 participants