Skip to content

[CP] Add Ulysses CP for varlen attention - #4459

Merged
fegin merged 16 commits into
mainfrom
gh/fegin/167/head
Sep 14, 2026
Merged

fegin merged 16 commits into
mainfrom
gh/fegin/167/head

Conversation

@fegin

@fegin fegin commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Share Ulysses resharding between FlexAttention and VarlenAttention. Add a varlen integration recipe and reject Ulysses kernels for GPT-OSS, whose TP-only sink sharding is incompatible.

Tests: Add CPU coverage and a 2-GPU CP integration case. Over 10 deterministic steps, flex and varlen differ by at most 7.534e-05 in loss and 5.960e-05 in grad norm.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Sep 4, 2026
Share Ulysses resharding between FlexAttention and VarlenAttention. Add a varlen integration recipe and reject Ulysses kernels for GPT-OSS, whose TP-only sink sharding is incompatible.

Tests: Add CPU coverage and a 2-GPU CP integration case. Over 10 deterministic steps, flex and varlen differ by at most 7.534e-05 in loss and 5.960e-05 in grad norm.
ghstack-source-id: 6a80e6f
Pull-Request: #4459
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Sep 9, 2026
Add Ulysses CP for VarlenInnerAttention and share resharding across Ulysses kernels. Reject all Ulysses kernels for GPT-OSS.

Tests: Add CPU coverage and a 2-GPU CP integration case. Over 10 deterministic steps, flex and varlen differ by at most 7.534e-05 in loss and 5.960e-05 in grad norm.

ghstack-source-id: 9b53bfc
Pull-Request: #4459
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Sep 11, 2026
Add Ulysses CP for VarlenInnerAttention and share resharding across Ulysses kernels. Reject all Ulysses kernels for GPT-OSS.

Tests: Add CPU coverage and a 2-GPU CP integration case. Over 10 deterministic steps, flex and varlen differ by at most 7.534e-05 in loss and 5.960e-05 in grad norm.

ghstack-source-id: 255c50c
Pull-Request: #4459
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Sep 11, 2026
Add Ulysses CP for VarlenInnerAttention and share resharding across Ulysses kernels. Reject all Ulysses kernels for GPT-OSS.

Tests: Add CPU coverage and a 2-GPU CP integration case. Over 10 deterministic steps, flex and varlen differ by at most 7.534e-05 in loss and 5.960e-05 in grad norm.

ghstack-source-id: 9c3b118
Pull-Request: #4459
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 added a commit that referenced this pull request Sep 11, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* #4459
* #4450
* #4533
* __->__ #4449
* #4322

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]
fegin added a commit that referenced this pull request Sep 11, 2026
Add Ulysses CP for VarlenInnerAttention and share resharding across Ulysses kernels. Reject all Ulysses kernels for GPT-OSS.

Tests: Add CPU coverage and a 2-GPU CP integration case. Over 10 deterministic steps, flex and varlen differ by at most 7.534e-05 in loss and 5.960e-05 in grad norm.

ghstack-source-id: 60127b6
Pull-Request: #4459
fegin added a commit that referenced this pull request Sep 11, 2026
Add Ulysses CP for VarlenInnerAttention and share resharding across Ulysses kernels. Reject all Ulysses kernels for GPT-OSS.

Tests: Add CPU coverage and a 2-GPU CP integration case. Over 10 deterministic steps, flex and varlen differ by at most 7.534e-05 in loss and 5.960e-05 in grad norm.

ghstack-source-id: 60127b6
Pull-Request: #4459
fegin added a commit that referenced this pull request Sep 11, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* #4459
* #4608
* __->__ #4450

Add Ulysses CP with S(0)-to-S(1) all-to-all reshards around
FlexAttention.

Keep its attention mask global, reject load balancing, and validate that
attention heads are divisible by the combined TP and CP degree.
Consolidate CP config validation and reject GPT-OSS because its per-head
sinks are not sharded across CP.

Add unit and integration tests.
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Sep 11, 2026
Add Ulysses CP for VarlenInnerAttention and share resharding across Ulysses kernels. Reject all Ulysses kernels for GPT-OSS.

Tests: Add CPU coverage and a 2-GPU CP integration case. Over 10 deterministic steps, flex and varlen differ by at most 7.534e-05 in loss and 5.960e-05 in grad norm.

ghstack-source-id: 9327834
Pull-Request: #4459
@fegin
fegin requested a review from tianyu-l September 11, 2026 22:38
tianyu-l pushed a commit that referenced this pull request Sep 12, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* #4459
* __->__ #4608

loss_compare.py compares a baseline config and a test config. It already
supports a separate seed config, but the integration test code
previously let it default to the baseline config. This stopped working
after CP became a config transform. The inner attention is swapped with
a parallelized one and seed checkpoint run set CP degree to 1. The
validation will raise.

This PR fixes the problem
@fegin
fegin changed the base branch from gh/fegin/167/base to main September 14, 2026 21:53
@fegin
fegin changed the base branch from main to gh/fegin/167/base September 14, 2026 21:53
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Sep 14, 2026
Add Ulysses CP for VarlenInnerAttention and share resharding across Ulysses kernels. Reject all Ulysses kernels for GPT-OSS.

Tests: Add CPU coverage and a 2-GPU CP integration case. Over 10 deterministic steps, flex and varlen differ by at most 7.534e-05 in loss and 5.960e-05 in grad norm.

ghstack-source-id: 1974d6e
Pull-Request: #4459
@fegin
fegin changed the base branch from gh/fegin/167/base to main September 14, 2026 21:54
@fegin
fegin merged commit baff3c6 into main Sep 14, 2026
9 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