Skip to content

[Cosmos3] Add native tensor parallelism - #15137

Open
yzhautouskay wants to merge 8 commits into
huggingface:mainfrom
yzhautouskay:yzhautouskay/cosmos3-native-tp-only
Open

yzhautouskay wants to merge 8 commits into
huggingface:mainfrom
yzhautouskay:yzhautouskay/cosmos3-native-tp-only

Conversation

@yzhautouskay

@yzhautouskay yzhautouskay commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Context

Cosmos TP/CP currently lives in the examples following #14054, where it was kept outside the core model while the library parallelism APIs were evolving. Those example helpers and their CLI behavior remain unchanged.

Diffusers now provides native model TP (#13718) and sharded checkpoint loading (#14544), so this PR integrates Cosmos with that infrastructure. Following the review direction in #14609, this PR is TP-only. Native Cosmos CP and TP+CP integration can follow separately after the generic composition work in #14725 is settled.

Cosmos native TP

  • Adds a TP plan for both understanding and generation attention projections and dense MLPs.
  • Infers rank-local attention head counts from sharded projection widths and validates that query/KV heads divide across the TP mesh.
  • Handles ReLU2 models with an instance plan that omits nonexistent gate projections.
  • Keeps the standard attention processor and backend dispatch unchanged.
  • Documents native TP with the existing 189-frame Super modular text-to-video workflow.

Shared TP change

  • modeling_utils.py now uses model._get_tp_plan() for checkpoint sharding and TP hooks, following @sayakpaul's proposed yzhautouskay@ef4beac. Cosmos models using SiLU have gate_proj layers; models using ReLU2 do not. The getter adds these layers to a copy of the TP plan only for SiLU, leaving the class-level plan unchanged. Other models retain their existing behavior because the default getter returns their _tp_plan unchanged.

Validation

  • 53 CPU/model tests passed; the focused TP/loading suite passed 11 tests across SiLU/ReLU2 and FP32/BF16; the modular pipeline suite passed 31 tests.
  • Twelve old/new non-TP comparisons were bit-exact. Small-model TP4 checks covered numerical parity, both loading paths, replicated parameters and buffers.
  • Native and existing example TP4 produced byte-identical full Super videos with the official 189-frame, 720x1280 workflow.
  • Across Nano/49, Super/49 and Nano/189 on four GB200 GPUs, median latency differed by at most 1.8% and peak allocated memory by at most 0.051%. Native reserved memory was up to 1.455 GB higher.
  • make style, make fix-copies and git diff --check passed.

Before submitting

  • Did you use an AI agent (Claude Code, Codex, Cursor, etc.) to help with this PR? If so:
    • Did you read the Coding with AI agents guide?
    • Did you run the self-review skill on the diff?
    • Did you share the final self-review notes in the PR description or a comment?
  • Did you read the contributor guideline?
  • Did you read our philosophy doc? (important for complex PRs)
  • Was this discussed/approved via a GitHub issue or the forum? Please add a link to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?
  • Are you the author (or part of the team) of the model/pipeline (only applicable for model/pipeline related PRs)?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@github-actions github-actions Bot added documentation Improvements or additions to documentation models tests size/L PR with diff > 200 LOC labels Oct 27, 2026
@yzhautouskay

Copy link
Copy Markdown
Contributor Author

@sayakpaul @yiyixuxu Could you please this PR to enable native TP in Cosmos3?

@github-actions

Copy link
Copy Markdown
Contributor

Hi @yzhautouskay, thanks for the PR! It does not appear to link an issue it fixes. If this PR addresses an existing issue, please add a closing keyword (e.g. Fixes #1234) to the PR description so the issue is linked. See the contribution guide for more details. If this PR intentionally does not fix a tracked issue, a maintainer can add the no-issue-needed label to silence this reminder.

Please note that PRs without a linked issue are likely to be automatically closed 10 days after this notice.

Once the PR links an issue (or gets the no-issue-needed label), you can ignore this message — it stays here as a comment, but it no longer applies.

@sayakpaul sayakpaul added the no-issue-needed for PRs that do not require link to an issue label Oct 28, 2026

@sayakpaul sayakpaul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

Some changes are a bit intrusive so I did some work in https://github.com/huggingface/diffusers/compare/cosmos3-tp-plan-resolution?expand=1 which should solve the issues in a less intrusive way. Does that work for you?

@yiyixuxu @DN6 thoughts?

Comment thread docs/source/en/api/pipelines/cosmos3.md Resolved
@yzhautouskay

Copy link
Copy Markdown
Contributor Author

@sayakpaul I updated this PR after #15147 got merged, thank you! Could we merge this one now?

Comment thread docs/source/en/api/pipelines/cosmos3.md Outdated
Comment on lines +728 to +741
## Native tensor parallelism

Follow the [tensor parallelism guide](../../training/distributed_inference#tensor-parallelism) for distributed setup,
launching, seeding, and rank-zero output handling. For Cosmos, adapt the
[existing modular workflow](#modular-examples-for-all-existing-workflows) as follows:

- Use `nvidia/Cosmos3-Super` or `nvidia/Cosmos3-Nano` with `Cosmos3OmniTransformer.from_pretrained`,
`subfolder="transformer"`, `dtype=torch.bfloat16`, and `parallel_config=TensorParallelConfig(tp_degree=4)`.
- Create `Cosmos3OmniModularPipeline` from the same checkpoint, then call
`update_components(transformer=transformer)` before `load_components`. Place the remaining components
on each rank's local device and reuse the workflow's prompts, scheduler, and generation settings.

Nano and Super support TP degrees 1, 2, 4, and 8; the degree must divide the query and KV head counts
and the MLP intermediate size. For Cosmos CP or combined TP+CP, use the [existing example helpers](#context-parallelism).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We usually don't include a separate section like this in pipeline docs. Cc: @stevhliu

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.

I just wanted to show in the context of previous "examples helpers" level of CP and TP implementation: https://github.com/yzhautouskay/diffusers/blob/f09416b7f4707d36f47aefe143d2d2d54614b8ca/docs/source/en/api/pipelines/cosmos3.md#context-parallelism

I move this doc section under it

dist.destroy_process_group()


def _tensor_parallel_output(output):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we add an accompanying comment to explain where it happens?


model = model_class(**init_dict)
model.load_state_dict(state_dict)
model.load_state_dict(state_dict, assign=True)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we need assign=True?

model.eval()

inputs_on_device = {k: v.to(device) if isinstance(v, torch.Tensor) else v for k, v in inputs_dict.items()}
inputs_on_device = send_to_device(inputs_dict, device)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's keep it explicit and avoid this.

return inputs


class TestCosmos3OmniTransformerTensorParallelConfig(Cosmos3TensorParallelTesterConfig):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No need.

Comment on lines +796 to +798
@pytest.fixture(autouse=True, params=["silu", "relu2"])
def activation(self, request):
self.hidden_act = request.param

Copy link
Copy Markdown
Member

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 fixture?

# Conflicts:
#	src/diffusers/models/modeling_utils.py

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation models no-issue-needed for PRs that do not require link to an issue size/L PR with diff > 200 LOC tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants