Skip to content

Remove apply_cp_to_forward, the partial_dtensor Context Parallel path - #4218

Merged
fegin merged 10 commits into
mainfrom
gh/fegin/159/head
Aug 20, 2026
Merged

fegin merged 10 commits into
mainfrom
gh/fegin/159/head

Conversation

@fegin

@fegin fegin commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

It's too much for us to maintain difference CP paths. This PR removes apply_cp_to_forward CP mechanism. Only config-based CP is preserved.

This disables:

  1. graph_trainer + CP (once spmd_types is enabled for graph_trainer, we can re-enable it)
  2. partial_dtensor + CP

fegin added 2 commits August 18, 2026 23:05
[ghstack-poisoned]
[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 19, 2026
if ac_config is not None:
apply_ac(model)

if parallelism.spmd_backend == "spmd_types":

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.

else raise?

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.

Use the same validate_cp_backend to validate.

Comment thread torchtitan/models/flux/parallelize.py Outdated
ac_config: ActivationCheckpointingConfig,
dump_folder: str,
):
validate_cp_backend(parallel_dims)

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.

For FLUX, right now the only supported attention backend is SDPA with full causal / full bidirectional masking.

If we deprecate the CP support for SDPA, we should at least support Varlen / Flex (and deprecate SDPA?). Another option is to have a Ring-SDPA with your pytorch side API called within.

@fegin fegin Aug 19, 2026 •

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.

With spmd_types, Flux is doing allgather + SDPA. We can deprecate SDPA usage. I don't think it worths to maintain SDPA only for Flux. Flex + FA3/FA4 can achieve a better performance. We can do this in a separate PR if we decide to switch the attention to Flex.

fegin added 2 commits August 19, 2026 13:08
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Aug 19, 2026
It's too much for us to maintain difference CP path. This PR remove apply_cp_to_forward CP mechanism. Only config-based CP is preserved.

This disables:
1. graph_trainer + CP (once spmd_types is enabled for graph_trainer, we can re-enable it)
2. partial_dtensor + CP

ghstack-source-id: 108cc24
Pull-Request: #4218
fegin added 2 commits August 19, 2026 15:11
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Aug 19, 2026
It's too much for us to maintain difference CP path. This PR remove apply_cp_to_forward CP mechanism. Only config-based CP is preserved.

This disables:
1. graph_trainer + CP (once spmd_types is enabled for graph_trainer, we can re-enable it)
2. partial_dtensor + CP

ghstack-source-id: e776be1
Pull-Request: #4218
@fegin
fegin marked this pull request as ready for review August 19, 2026 22:16

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

With spmd_types, Flux is doing allgather + SDPA.

Oh I wasn't aware of this. Good to know.

Flex + FA3/FA4 can achieve a better performance. We can do this in a separate PR if we decide to switch the attention to Flex.

There's one caveat though. @acisseJZhong is removing batch dim for decoder models, but for FLUX, it's still having batch dim + seq dim -- each row in the batch uses full causal / bidirectional mask.

  • If we also fold for flux, would Flex/Varlen be faster? Probably, but I don't know.
  • If we don't fold for flux, we have to have if-else in Flex/Varlen on num_dims, which is not ideal.

fegin added a commit that referenced this pull request Aug 20, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* #4218
* __->__ #4217

Now that spmd_types is the default backend (thanks to @pianpwk) and
partial_dtensor is the fallback, drop `parallelism.spmd_backend ==
"full_dtensor"` and everything that only existed to support it. This
makes people's life easier.
@fegin

fegin commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author
  • If we also fold for flux, would Flex/Varlen be faster? Probably, but I don't know.

I think FA3/FA4 should be faster, SDPA is using FA2. But I don't know if we can fold for Flux?

  • If we don't fold for flux, we have to have if-else in Flex/Varlen on num_dims, which is not ideal.

Okay, this is a good point. Then we either fold for Flux if possible, or just make Flux maintain its own FlexAttention kernel.

@tianyu-l

Copy link
Copy Markdown
Contributor

But I don't know if we can fold for Flux?

we can always fold, just need to define proper masks -- the comparison of perf is between

  • SDPA with regular shape and default masks
  • Flex / Varlen with document masking

@fegin
fegin changed the base branch from gh/fegin/159/base to main August 20, 2026 00:48
@fegin
fegin changed the base branch from main to gh/fegin/159/base August 20, 2026 00:49
fegin added 2 commits August 19, 2026 17:49
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Aug 20, 2026
It's too much for us to maintain difference CP path. This PR remove apply_cp_to_forward CP mechanism. Only config-based CP is preserved.

This disables:
1. graph_trainer + CP (once spmd_types is enabled for graph_trainer, we can re-enable it)
2. partial_dtensor + CP

ghstack-source-id: bcdbd97
Pull-Request: #4218
@fegin
fegin changed the base branch from gh/fegin/159/base to main August 20, 2026 00:50
@fegin fegin mentioned this pull request Aug 20, 2026
@fegin
fegin changed the base branch from main to gh/fegin/159/base August 20, 2026 01:09
fegin added 2 commits August 19, 2026 19:47
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Aug 20, 2026
It's too much for us to maintain difference CP path. This PR remove apply_cp_to_forward CP mechanism. Only config-based CP is preserved.

This disables:
1. graph_trainer + CP (once spmd_types is enabled for graph_trainer, we can re-enable it)
2. partial_dtensor + CP

ghstack-source-id: be6a623
Pull-Request: #4218
@fegin
fegin changed the base branch from gh/fegin/159/base to main August 20, 2026 03:25
@fegin
fegin merged commit 5dd944e into main Aug 20, 2026
14 checks passed
@github-actions github-actions Bot mentioned this pull request Aug 21, 2026
@sdmyzlp

sdmyzlp commented Sep 7, 2026 •

Copy link
Copy Markdown
Collaborator

@fegin Hello, with spmd_type now enabled for graph_trainer in pytorch/torchtitan#4325, I was wondering if context parallel can be re-enabled for graph_trainer at this point? Or are there still pending dependencies—perhaps pytorch/torchtitan#4419 or later PRs—that need to be merged first? Would appreciate any insight on the roadmap. Thanks!

@fegin

fegin commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

@sdmyzlp I'll revisit the CP + graph trainer with the PR stack: #4322

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