Skip to content

[core] Enable CP for kernels-based attention backends - #12812

Merged
sayakpaul merged 13 commits into
mainfrom
enable-cp-kernels
Feb 19, 2026
Merged

[core] Enable CP for kernels-based attention backends#12812
sayakpaul merged 13 commits into
mainfrom
enable-cp-kernels

Conversation

@sayakpaul

@sayakpaulsayakpaul commented Dec 9, 2025

Copy link
Copy Markdown
Member

What does this PR do?

Adds CP support to the kernels-based attention backends.

Our CP support is quickly gaining traction. Currently, we have a few attention backends that are fully based on kernels. In order for their adoption to grow and make them a bit more complete in terms of feature parity, I think we should make them CP-compatible, too.

Code to test:
importargparseimporttorchfromtorchimportdistributedasdistfromdiffusersimportDiffusionPipeline, ContextParallelConfig, AutoModelCKPT_ID="black-forest-labs/FLUX.1-dev"defparse_args():
parser=argparse.ArgumentParser()
parser.add_argument(
"--cp-backend",
type=str,
choices=["ring", "ulysses", "unified"],
default="ulysses",
help="Context parallel backend to use.",
)
parser.add_argument(
"--attn-backend",
type=str,
choices=["flash_hub", "_flash_3_hub", "sage_hub"],
default="flash_hub",
help="Attention backend to use.",
)
returnparser.parse_args()
defsetup_distributed():
ifnotdist.is_initialized():
dist.init_process_group(backend="nccl")
rank=dist.get_rank()
device=torch.device(f"cuda:{rank}")
torch.cuda.set_device(device)
returndevicedefmain():
args=parse_args()
device=setup_distributed()
world_size=dist.get_world_size()
ifargs.cp_backend=="ring":
cp_config=ContextParallelConfig(ring_degree=world_size)
elifargs.cp_backend=="unified":
cp_config=ContextParallelConfig(ring_degree=world_size//2, ulysses_degree=world_size//2)
else:
cp_config=ContextParallelConfig(ulysses_degree=world_size)
transformer=AutoModel.from_pretrained(
CKPT_ID, subfolder="transformer", torch_dtype=torch.bfloat16, parallel_config=cp_config
)
pipeline=DiffusionPipeline.from_pretrained(
CKPT_ID, transformer=transformer, torch_dtype=torch.bfloat16,
).to(device)
pipeline.transformer.set_attention_backend(args.attn_backend)
prompt=""" cinematic film still of a cat sipping a margarita in a pool in Palm Springs, California highly detailed, high budget hollywood movie, cinemascope, moody, epic, gorgeous, film grain """generator=torch.Generator().manual_seed(42)
image=pipeline(
prompt,
guidance_scale=3.5,
num_inference_steps=50,
generator=generator,
).images[0]
ifdist.get_rank() ==0:
image.save(f"output_{args.cp_backend}_{args.attn_backend}.png")
ifdist.is_initialized():
dist.destroy_process_group()
if__name__=="__main__":
main()

Outputs:

FA2+ UlyssesFA3 + UlyssesSAGE + Ulysses
RingUlyssesUnified

@sayakpaul
sayakpaul requested a review from DN6December 9, 2025 09:47
@sayakpaulsayakpaul added the performance Anything related to performance improvements, profiling and benchmarking label Dec 9, 2025
Comment on lines +280 to +281
wrapped_forward_attr="flash_attn_interface._wrapped_flash_attn_forward",
wrapped_backward_attr="flash_attn_interface._wrapped_flash_attn_backward",

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only FA2 provides these.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you take a closer look, there is an equivalent for FA3. FA2 just renames its backward for wrapped_xxx

So I expect that when torch may come around FA3, we get the same standardization but for now the equivalent is just

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@github-actions

Copy link
Copy Markdown
Contributor

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

@github-actionsgithub-actionsBot added the stale Issues that haven't received updates label Jan 10, 2026
@sayakpaulsayakpaul removed the stale Issues that haven't received updates label Jan 11, 2026
@sayakpaul

Copy link
Copy Markdown
MemberAuthor

@DN6 a gentle ping.

@sayakpaulsayakpaul added the roadmap Add to current release roadmap label Feb 16, 2026
DN6
DN6 approved these changes Feb 19, 2026

@DN6DN6 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment about the FA3 backward. Not a merge blocker since it mostly affects CP based training

key_r = key.detach().requires_grad_(True)
value_r = value.detach().requires_grad_(True)

out = kernel_fn(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would result in a second in a forward pass during the backward op right? Would it make sense to just raise an error here similar to sage attention?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment in 9465231

@sayakpaul
sayakpaul merged commit 99daaa8 into mainFeb 19, 2026
12 checks passed
@github-project-automationgithub-project-automationBot moved this from In Progress to Done in Diffusers RoadmapFeb 19, 2026
@sayakpaul
sayakpaul deleted the enable-cp-kernels branch February 19, 2026 12:46
tolgacangoz pushed a commit to tolgacangoz/diffusers that referenced this pull request Feb 25, 2026
)
* up
* up
* up
* up
---------
Co-authored-by: Dhruv Nair <dhruv.nair@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performanceAnything related to performance improvements, profiling and benchmarkingroadmapAdd to current release roadmap

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants

@sayakpaul@HuggingFaceDocBuilderDev@DN6@vasqu