Repository navigation
[Cosmos3] Add native tensor parallelism - #15137
yzhautouskay wants to merge 8 commits into
Conversation
|
@sayakpaul @yiyixuxu Could you please this PR to enable native TP in Cosmos3? |
|
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. 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 |
sayakpaul
left a comment
There was a problem hiding this comment.
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?
|
@sayakpaul I updated this PR after #15147 got merged, thank you! Could we merge this one now? |
| ## 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). |
There was a problem hiding this comment.
We usually don't include a separate section like this in pipeline docs. Cc: @stevhliu
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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) |
| 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) |
There was a problem hiding this comment.
Let's keep it explicit and avoid this.
| return inputs | ||
|
|
||
|
|
||
| class TestCosmos3OmniTransformerTensorParallelConfig(Cosmos3TensorParallelTesterConfig): |
| @pytest.fixture(autouse=True, params=["silu", "relu2"]) | ||
| def activation(self, request): | ||
| self.hidden_act = request.param |
# Conflicts: # src/diffusers/models/modeling_utils.py
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
Shared TP change
modeling_utils.pynow usesmodel._get_tp_plan()for checkpoint sharding and TP hooks, following @sayakpaul's proposed yzhautouskay@ef4beac. Cosmos models using SiLU havegate_projlayers; 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_planunchanged.Validation
make style,make fix-copiesandgit diff --checkpassed.Before submitting
self-reviewskill on the diff?documentation guidelines, and
here are tips on formatting docstrings.
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.