Skip to content

[CP] Add Ulysses context parallel - #4450

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

fegin merged 12 commits into
mainfrom
gh/fegin/166/head

Conversation

@fegin

@fegin fegin commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

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]
[ghstack-poisoned]
[ghstack-poisoned]
Comment on lines +70 to +77
if parallel_dims.cp_enabled and isinstance(
model.config.first_full_attention_backend, UlyssesCPFlexAttention.Config
):
raise NotImplementedError(
"GPT-OSS does not support Ulysses context parallel because its "
"per-head sinks are sharded over TP but not CP. Use "
"AllGatherCPFlexAttention instead."
)

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.

this validation on configs could have happened at config time, but yeah, not sure what's the best place to call it

@dataclass(kw_only=True, slots=True)
class Config(FlexAttention.Config):
shard_attention_mask: ClassVar[bool] = False
shard_attention_heads: ClassVar[bool] = True

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.

not used?

input_seq_dims: int | tuple[int, ...] = 0,
ptrr_mask_key: str | None = None,
*,
shard_attention_mask: bool = True,

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.

I feel this is too much indirection and hard to understand / maintain / extend.

A slightly more OOD way is to let each CPAttention backend own its own cp_shard method, and the Ulysses one would be different from the all-gather KV one.

Also there's implicit assumption that all layers have to use the same CP backend.

@QIU023 QIU023 mentioned this pull request Sep 8, 2026
14 of 18 tasks
[ghstack-poisoned]
[ghstack-poisoned]

@tianyu-l tianyu-l 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.

please address inline comments

acisseJZhong pushed a commit to acisseJZhong/torchtitan that referenced this pull request Sep 11, 2026
Add Ulysses CP for FlexInnerAttention. Keep attention masks global and validate its load-balancing and head-sharding requirements.

Reject GPT-OSS because its per-head sinks are not sharded across CP.

ghstack-source-id: adb0af0
Pull-Request: pytorch#4450
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
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
fegin changed the base branch from gh/fegin/166/base to main September 11, 2026 19:36
@fegin
fegin changed the base branch from main to gh/fegin/166/base September 11, 2026 19:37
@fegin
fegin changed the base branch from gh/fegin/166/base to main September 11, 2026 19:37
@fegin
fegin changed the base branch from main to gh/fegin/166/base September 11, 2026 19:38
@fegin
fegin changed the base branch from gh/fegin/166/base to main September 11, 2026 19:42
@fegin
fegin merged commit 15a4532 into main Sep 11, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/rl 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