Skip to content

Add shape suffix annotations to attention module - #3443

Merged
acisseJZhong merged 1 commit into
mainfrom
fix_attention_shape_annotation
Jun 17, 2026
Merged

acisseJZhong merged 1 commit into
mainfrom
fix_attention_shape_annotation

Conversation

@acisseJZhong

@acisseJZhong acisseJZhong commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add shape suffix annotations to attention.py following the Noam Shazeer shape suffix convention, matching the style introduced for MoE in #3425.

Each attention backend's layout transformation is now explicit in variable names:

  • _BLNH: shared input/output layout (batch, seq, heads, head_dim)
  • _TNH: VarlenAttention packed layout (T = B*L)
  • _BNLH: FlexAttention / SDPA heads-first layout

No functional changes — rename only.

Test plan

  • pre-commit run --all-files
  • Existing attention unit and integration tests (no behavioral change)
NCCL_NVLS_ENABLE=0 python scripts/loss_compare.py . main --baseline-module='llama3' 
--baseline-config='llama3_debugmodel' --baseline-options="
--parallelism.data_parallel_shard_degree 2 --parallelism.t
ensor_parallel_degree 2" --baseline-ngpus=4 --test-module=
'llama3' --test-config='llama3_debugmodel' --test-options=
"--parallelism.data_parallel_shard_degree 2 --parallelism.
tensor_parallel_degree 2" --test-ngpus=4 --assert-equal --
no-seed-checkpoint

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 28, 2026
@acisseJZhong
acisseJZhong force-pushed the fix_attention_shape_annotation branch from b35f16c to 9a5459a Compare May 28, 2026 18:09
@acisseJZhong
acisseJZhong marked this pull request as ready for review May 28, 2026 18:11
@acisseJZhong
acisseJZhong force-pushed the fix_attention_shape_annotation branch 2 times, most recently from f20722c to 799b399 Compare June 16, 2026 22:02

@fegin fegin left a comment

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.

Should we do 1) create a doc for coding convention, 2) update CLAUDE.md to add the convention/

@acisseJZhong
acisseJZhong force-pushed the fix_attention_shape_annotation branch 2 times, most recently from 2200b62 to 6cf44ed Compare June 16, 2026 22:23
@acisseJZhong
acisseJZhong force-pushed the fix_attention_shape_annotation branch from 6cf44ed to 404bf7f Compare June 16, 2026 23:21
@acisseJZhong

Copy link
Copy Markdown
Contributor Author

updated CLAUDE.md and CONTRIBUTING.md. The 8gpu feature tests are failing on main, 8 gpu model tests should be fixed by #3681

@acisseJZhong
acisseJZhong merged commit 196a889 into main Jun 17, 2026
21 of 24 checks passed
@acisseJZhong
acisseJZhong deleted the fix_attention_shape_annotation branch June 17, 2026 18:16
acisseJZhong added a commit that referenced this pull request Jun 18, 2026
fixed attention qkv notation after
#3443 where the RL unit test
was skipped
acisseJZhong added a commit to acisseJZhong/torchtitan that referenced this pull request Jun 18, 2026
…x rename

The shape-suffix rename in pytorch#3443 changed GQAttention.forward's first
parameter from `x` to `x_BLD`, but set_gqa_attention_sharding still keyed the
input redistribution under "x". The redistribution engine matches
in_src/in_dst keys against the actual forward parameter names (via
inspect.signature), so in_*_shardings.get("x_BLD") returned None and the
attention block's "gather x to Replicate on TP" became a silent no-op.

Under SP this left the activation Shard(1) entering the QKV projection. The
non-fused QKVLinear has no input check and silently tolerated it, but the
fused path's in_src check rejected it, failing the 8-GPU
qwen3_fsdp+tp+cp_no_sp_fused_qkv integration test:

  ValueError: FusedQKVLinear.x: input DTensor has placements (Shard(dim=1),),
  but in_src_shardings expects (Replicate(),).

Fix: key the GQAttention input redistribution under "x_BLD" so the gather
runs. With the gather restored, the FusedQKVLinear wrapper config is
redundant -- the in_dst was a Replicate->Replicate no-op, and the tuple
out_src is a no-op under default/full_dtensor and raises under spmd_types --
so remove it; the fused branch now mirrors the non-fused one (colwise on
wqkv only). q/k/v outputs stay Shard(2) via DTensor view/split propagation
and are validated at the inner-attention and qk-norm boundaries.

Affects all GQAttention models (qwen3, llama3); gpt_oss is unaffected (its
Attention.forward still uses `x`).
acisseJZhong added a commit that referenced this pull request Jun 18, 2026
…3708)

## Summary

`set_gqa_attention_sharding` keyed the attention input redistribution
under
`"x"`, but #3443 renamed `GQAttention.forward`'s first parameter to
`x_BLD`.
The redistribution engine looks up `in_src`/`in_dst` by the actual
forward
parameter name, so the lookup missed and the "gather x to Replicate on
TP"
silently no-op'd. Under sequence parallelism the activation reached the
QKV
projection still `Shard(1)`, which the fused path rejected:

```
ValueError: FusedQKVLinear.x: input DTensor has placements (Shard(dim=1),),
but in_src_shardings expects (Replicate(),).
```

## Changes

- Key the GQAttention input redistribution under `x_BLD` so the gather
runs.
- Drop the now-redundant `FusedQKVLinear` wrapper config (its `in_dst`
was a
  no-op and its tuple `out_src` does nothing in default/full_dtensor and
raises under spmd_types); the fused branch now matches the non-fused
one.
HosseinKaviani-H pushed a commit to HosseinKaviani-H/torchtitan_local that referenced this pull request Jun 23, 2026
Follow core torchtitan convention (PR pytorch#3443): name Q/K/V/out tensors with
shape suffixes (q_BLNH native layout, q_BNLH flex-kernel layout) and add a
legend comment. Pure rename + ASCII-ize the layout-transition comments; no
behavior change.
HosseinKaviani-H pushed a commit to HosseinKaviani-H/torchtitan_local that referenced this pull request Jun 29, 2026
Follow core torchtitan convention (PR pytorch#3443): name Q/K/V/out tensors with
shape suffixes (q_BLNH native layout, q_BNLH flex-kernel layout) and add a
legend comment. Pure rename + ASCII-ize the layout-transition comments; no
behavior change.
HosseinKaviani-H pushed a commit to HosseinKaviani-H/torchtitan_local that referenced this pull request Jun 30, 2026
Follow core torchtitan convention (PR pytorch#3443): name Q/K/V/out tensors with
shape suffixes (q_BLNH native layout, q_BNLH flex-kernel layout) and add a
legend comment. Pure rename + ASCII-ize the layout-transition comments; no
behavior change.
HosseinKaviani-H pushed a commit to HosseinKaviani-H/torchtitan_local that referenced this pull request Jul 7, 2026
Follow core torchtitan convention (PR pytorch#3443): name Q/K/V/out tensors with
shape suffixes (q_BLNH native layout, q_BNLH flex-kernel layout) and add a
legend comment. Pure rename + ASCII-ize the layout-transition comments; no
behavior change.
HosseinKaviani-H pushed a commit to HosseinKaviani-H/torchtitan_local that referenced this pull request Jul 8, 2026
Follow core torchtitan convention (PR pytorch#3443): name Q/K/V/out tensors with
shape suffixes (q_BLNH native layout, q_BNLH flex-kernel layout) and add a
legend comment. Pure rename + ASCII-ize the layout-transition comments; no
behavior change.
HosseinKaviani-H pushed a commit to HosseinKaviani-H/torchtitan_local that referenced this pull request Jul 9, 2026
Follow core torchtitan convention (PR pytorch#3443): name Q/K/V/out tensors with
shape suffixes (q_BLNH native layout, q_BNLH flex-kernel layout) and add a
legend comment. Pure rename + ASCII-ize the layout-transition comments; no
behavior change.
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.

3 participants