Skip to content

[CP] Gather K/V with spmd_types.redistribute - #4449

Merged
fegin merged 11 commits into
mainfrom
gh/fegin/165/head
Sep 11, 2026
Merged

fegin merged 11 commits into
mainfrom
gh/fegin/165/head

Conversation

@fegin

@fegin fegin commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Switch to use spmd_types.redistribute instead of using flex_cp_allgather.

The backward of the gather is a reduce-scatter, and the API requires an explicit dtype for it under reduced precision. AllGatherCPFlexAttention.Config.reduce_dtype exposes that choice. It defaults to None, which keeps the reduction in the input dtype and so matches flex_cp_allgather. Set it to float32 to trade two casts for accuracy, which matters as the CP degree grows and the reduce-scatter sums more contributions.

Numerics Verification

Loss and grad_norm are bitwise identical before and after, with the default reduce_dtype. scripts/loss_compare.py over llama3_debugmodel_fsdp2_cp2 (FSDP 2 + CP 2, 4 H100s, spmd_types, bf16 params), shared seed checkpoint, --debug.seed=42 --debug.deterministic, 10 steps, values read from the TensorBoard scalars:

metric     max |before - after|
loss       0.0
grad_norm  0.0

Step 1 loss is 8.048094749450684 on both sides and step 10 is 4.038208961486816. --assert-equal passed for both metrics.

[ghstack-poisoned]
[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 3, 2026
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Sep 4, 2026
Switch to use `spmd_types.redistribute` instead of using `flex_cp_allgather`.

The backward of the gather is a reduce-scatter, and the API requires an explicit dtype for it under reduced precision. `AllGatherCPFlexAttention.Config.reduce_dtype` exposes that choice. It defaults to None, which keeps the reduction in the input dtype and so matches `flex_cp_allgather`. Set it to float32 to trade two casts for accuracy, which matters as the CP degree grows and the reduce-scatter sums more contributions.

**Numerics Verification**

Loss and grad_norm are bitwise identical before and after, with the default `reduce_dtype`. `scripts/loss_compare.py` over `llama3_debugmodel_fsdp2_cp2` (FSDP 2 + CP 2, 4 H100s, spmd_types, bf16 params), shared seed checkpoint, `--debug.seed=42 --debug.deterministic`, 10 steps, values read from the TensorBoard scalars:

    metric     max |before - after|
    loss       0.0
    grad_norm  0.0

Step 1 loss is 8.048094749450684 on both sides and step 10 is 4.038208961486816. `--assert-equal` passed for both metrics.


ghstack-source-id: 81cd589
Pull-Request: pytorch/torchtitan#4449
[ghstack-poisoned]
[ghstack-poisoned]
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Sep 4, 2026
Switch to use `spmd_types.redistribute` instead of using `flex_cp_allgather`.

The backward of the gather is a reduce-scatter, and the API requires an explicit dtype for it under reduced precision. `AllGatherCPFlexAttention.Config.reduce_dtype` exposes that choice. It defaults to None, which keeps the reduction in the input dtype and so matches `flex_cp_allgather`. Set it to float32 to trade two casts for accuracy, which matters as the CP degree grows and the reduce-scatter sums more contributions.

**Numerics Verification**

Loss and grad_norm are bitwise identical before and after, with the default `reduce_dtype`. `scripts/loss_compare.py` over `llama3_debugmodel_fsdp2_cp2` (FSDP 2 + CP 2, 4 H100s, spmd_types, bf16 params), shared seed checkpoint, `--debug.seed=42 --debug.deterministic`, 10 steps, values read from the TensorBoard scalars:

    metric     max |before - after|
    loss       0.0
    grad_norm  0.0

Step 1 loss is 8.048094749450684 on both sides and step 10 is 4.038208961486816. `--assert-equal` passed for both metrics.

ghstack-source-id: f807cc7
Pull-Request: pytorch/torchtitan#4449
Comment thread torchtitan/config/transform/base.py Resolved
[ghstack-poisoned]
[ghstack-poisoned]
Comment thread torchtitan/config/transform/base.py Resolved
inner_attention: type[Module]
"""Replacement inner attention; must inherit ``CPInnerAttention``."""

inner_attention_config_updates: dict[str, object] = field(default_factory=dict)

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 do we need this?

  • not used in this PR
  • also I wonder why we don't expand the dict into this config

I'll need to check where it's actually used, but maybe can be removed from this PR.

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.

both left over from the previous version, which I should have deleted them since this flexibility is not used in this version.

acisseJZhong pushed a commit to acisseJZhong/torchtitan that referenced this pull request Sep 11, 2026
Use float32 for the backward reduce-scatter by default. Allow explicit bfloat16 reduction through the CP transform.

ghstack-source-id: 03b61ef
Pull-Request: pytorch#4449
[ghstack-poisoned]
[ghstack-poisoned]
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Sep 11, 2026
Use float32 for backward reduce-scatter by default. The CP attention config can select bfloat16.

ghstack-source-id: 6eccc3c
Pull-Request: pytorch/torchtitan#4449
[ghstack-poisoned]
[ghstack-poisoned]
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Sep 11, 2026
Use float32 for backward reduce-scatter by default. The CP attention config can select bfloat16.

ghstack-source-id: c5ee883
Pull-Request: pytorch/torchtitan#4449
fegin added a commit that referenced this pull request Sep 11, 2026
…rst example (#4322)

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* #4459
* #4450
* #4533
* #4449
* __->__ #4322

1. Implement ModelTransform interface .
2. Rewrite the current CP:
Move the K/V all-gather from local-map redistribution in
`Module.forward()` into AllGatherCPFlexAttention. Add kernel selection
and validation, then update CP recipes and tests.

All CP implementation will follow this approach.
@fegin
fegin changed the base branch from gh/fegin/165/base to main September 11, 2026 17:06
@fegin
fegin changed the base branch from main to gh/fegin/165/base September 11, 2026 17:08
@fegin
fegin changed the base branch from gh/fegin/165/base to main September 11, 2026 17:10
@fegin
fegin changed the base branch from main to gh/fegin/165/base September 11, 2026 17:40
@fegin
fegin changed the base branch from gh/fegin/165/base to main September 11, 2026 17:40
@fegin
fegin merged commit 0fb67ad into main Sep 11, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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