Skip to content

feat(phyai): Support Cosmos3, TP and CFG Parallel#37

Merged
chenghuaWang merged 4 commits into
MEmbodied:mainfrom
chenghuaWang:dev/chenghua
Jul 1, 2026
Merged

feat(phyai): Support Cosmos3, TP and CFG Parallel#37
chenghuaWang merged 4 commits into
MEmbodied:mainfrom
chenghuaWang:dev/chenghua

Conversation

@chenghuaWang

@chenghuaWang chenghuaWang commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • New Features

    • Added support for Cosmos3 multi-GPU generation and policy workflows, including new example launch scripts.
  • Documentation

    • Expanded Cosmos3 docs in English and Chinese with run instructions, topology details, and usage examples.
    • Added a community code of conduct and updated the news section.
  • Bug Fixes

    • Improved multi-process launch handling and parallel decoding behavior for more reliable distributed runs.
  • Chores

    • Added repository ownership coverage and updated pre-commit spellcheck exclusions.

@chenghuaWang chenghuaWang merged commit e902ae0 into MEmbodied:main Jul 1, 2026
1 of 2 checks passed
@coderabbitai

coderabbitai Bot commented Jul 1, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 65fc9c9f-2d40-4fce-b3cf-269cf4d7aa10

📥 Commits

Reviewing files that changed from the base of the PR and between 301112b and bbd85f3.

⛔ Files ignored due to path filters (2)
  • docs/images/models/cosmos/tp4-cfg2-topology.svg is excluded by !**/*.svg
  • docs/images/models/cosmos/vae8-tile-split.svg is excluded by !**/*.svg
📒 Files selected for processing (26)
  • .github/CODEOWNERS
  • .pre-commit-config.yaml
  • CODE_OF_CONDUCT.md
  • README.md
  • docs/docs.json
  • docs/models/cosmos/wn.mdx
  • docs/models/cosmos/wn_policy.mdx
  • docs/zh/models/cosmos/wn.mdx
  • docs/zh/models/cosmos/wn_policy.mdx
  • examples/cosmos3/run_cosmos3_policy_wn.py
  • examples/cosmos3/run_cosmos3_wn.py
  • phyai/src/phyai/engine.py
  • phyai/src/phyai/engine_config.py
  • phyai/src/phyai/env.py
  • phyai/src/phyai/models/cosmos3/__init__.py
  • phyai/src/phyai/models/cosmos3/main_cosmos3_policy_wn.py
  • phyai/src/phyai/models/cosmos3/main_cosmos3_wn.py
  • phyai/src/phyai/models/cosmos3/model_runner_vae_cosmos3.py
  • phyai/src/phyai/models/cosmos3/modeling_cosmos3.py
  • phyai/src/phyai/models/cosmos3/scheduler_wn_cosmos3.py
  • phyai/src/phyai/models/cosmos3/scheduler_wn_cosmos3_policy.py
  • phyai/src/phyai/models/cosmos3/vae_wan.py
  • phyai/src/phyai/parallel/backends/pynccl.py
  • phyai/src/phyai/parallel/dist.py
  • phyai/src/phyai/parallel/ops.py
  • third_party/mirage

📝 Walkthrough

Walkthrough

This PR adds a multi-GPU ("wn") tensor-parallel and CFG-parallel execution path for Cosmos3 models, including a new cfg parallel mesh axis, fused QKV attention projections, tiled parallel VAE decoding, new WN schedulers and plugin entries for T2V and policy inference, example scripts, and documentation. It also adds unrelated repository housekeeping files (CODEOWNERS, Code of Conduct) and bumps a submodule.

Changes

Cosmos3 WN Multi-GPU Path

Layer / File(s) Summary
CFG parallel axis wiring
phyai/src/phyai/engine.py, phyai/src/phyai/engine_config.py, phyai/src/phyai/env.py
Adds a cfg mesh axis to Engine.__init__'s 6-axis mesh layout, a cfg_size field to ParallelConfig with validation, and a PHYAI_CFG_SIZE env var overlay.
Distributed communicator fixes
phyai/src/phyai/parallel/backends/pynccl.py, phyai/src/phyai/parallel/dist.py, phyai/src/phyai/parallel/ops.py
Skips NCCL comm setup for singleton axes, adds local synchronization for CPU bootstrap groups, fixes rank/local_rank resolution from torchrun env vars, and safely deserializes ReduceOp integers in custom ops.
Fused QKV attention
phyai/src/phyai/models/cosmos3/modeling_cosmos3.py
Replaces separate Q/K/V projections with fused QKVParallelLinear in causal and cross attention, adding a local_head_counts helper for per-rank head sizing.
Tiled parallel VAE decode
phyai/src/phyai/models/cosmos3/vae_wan.py, phyai/src/phyai/models/cosmos3/model_runner_vae_cosmos3.py
Adds decode_parallel with tile-grid splitting and feather-weighted blending for multi-rank decode, routed from Cosmos3VAERunner.decode based on computed parallel size.
WN schedulers
phyai/src/phyai/models/cosmos3/scheduler_wn_cosmos3.py, phyai/src/phyai/models/cosmos3/scheduler_wn_cosmos3_policy.py, phyai/src/phyai/models/cosmos3/__init__.py
Adds Cosmos3T2VWNScheduler and Cosmos3PolicyWNScheduler implementing CFG-parallel/sequential UniPC denoise loops, exported from the package.
WN plugin entries
phyai/src/phyai/models/cosmos3/main_cosmos3_wn.py, phyai/src/phyai/models/cosmos3/main_cosmos3_policy_wn.py
Registers cosmos3_wn and cosmos3_policy_wn engine plugins with full setup/step/close/dump_targets lifecycles.
Example run scripts
examples/cosmos3/run_cosmos3_wn.py, examples/cosmos3/run_cosmos3_policy_wn.py
Adds torchrun-launchable demo scripts for T2V and policy generation with topology resolution and rank-0 output saving.
Documentation
docs/models/cosmos/wn.mdx, docs/models/cosmos/wn_policy.mdx, docs/zh/models/cosmos/*, docs/docs.json, README.md
Adds English/Chinese docs for the WN and policy paths, navigation entries, and a README news item.

Estimated code review effort: 4 (Complex) | ~75 minutes

Repository Housekeeping

Layer / File(s) Summary
Governance files
.github/CODEOWNERS, .pre-commit-config.yaml, CODE_OF_CONDUCT.md
Adds CODEOWNERS ownership rules, a codespell exclusion pattern, and a full Code of Conduct document.
Submodule bump
third_party/mirage
Updates the pinned mirage submodule commit.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Script as run_cosmos3_wn.py
  participant Engine
  participant Scheduler as Cosmos3T2VWNScheduler
  participant VAE as Cosmos3WanVAE
  participant Disk

  Script->>Engine: construct(cosmos3_wn, ParallelConfig cfg_size/tp_size)
  Script->>Engine: step(request) on all ranks
  Engine->>Scheduler: run UniPC denoise loop
  alt cfg_size > 1
    Scheduler->>Scheduler: cond/uncond branches on cfg axis
    Scheduler->>Scheduler: all_gather velocities across cfg
  else
    Scheduler->>Scheduler: sequential cond/uncond on single rank
  end
  Scheduler->>VAE: decode_parallel(latents) if tp/cfg > 1
  VAE-->>Scheduler: blended pixel tiles
  Scheduler-->>Engine: video/sound result
  Engine-->>Script: identical result across ranks
  Script->>Disk: save mp4 (rank 0 only)
Loading

Possibly related PRs

  • MEmbodied/phyai#26: Modifies the same Cosmos3WanVAE decode logic in vae_wan.py and attention/transformer code in modeling_cosmos3.py that this PR extends for the multi-GPU WN path.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces multi-GPU tensor-parallel and classifier-free-guidance (CFG) parallel support for Cosmos3 generation and policy models, including new orchestrators, parallel VAE decoding, and fused QKV projections. Key feedback includes adding guards to prevent potential TypeError and incorrect slicing when optional parameters are None in the schedulers, and using the global RANK instead of LOCAL_RANK in the example scripts to ensure correct behavior in multi-node distributed environments.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

.standard_normal((batch, chunk, ad))
.astype("float32")
).to(dev, dt)
action[:, :, raw:] = 0.0

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.

high

If raw (i.e., request.raw_action_dim) is None, the slice raw: becomes None:, which is equivalent to 0: in Python slicing. This will silently zero out the entire action tensor instead of just the padding. A guard should be added to ensure raw is not None before zeroing out the padding. This also applies to other lines where raw: is used for slicing (lines 177, 242, 249, 283, 290).

Suggested change
action[:, :, raw:] = 0.0
if raw is not None:
action[:, :, raw:] = 0.0

.contiguous()
)

cond_idx = list(request.cond_frame_indexes)

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.

medium

If request.cond_frame_indexes is None, calling list(request.cond_frame_indexes) will raise a TypeError: 'NoneType' object is not iterable. We should add a guard to handle the None case gracefully.

Suggested change
cond_idx = list(request.cond_frame_indexes)
cond_idx = list(request.cond_frame_indexes) if request.cond_frame_indexes is not None else []

f"launched under torchrun (WORLD_SIZE={env_world}) but --cfg*--tp is 1; "
f"set --cfg/--tp to use all ranks."
)
return local_rank, cfg, tp, local_rank == 0

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.

medium

In a multi-node distributed environment, LOCAL_RANK will be 0 on the first GPU of every node. Consequently, is_main (which is local_rank == 0) will evaluate to True on multiple nodes, causing multiple processes to write to the same output file or print duplicate logs. It is safer and more standard to use the global RANK environment variable to determine the main process.

Suggested change
return local_rank, cfg, tp, local_rank == 0
global_rank = int(os.environ.get("RANK", "0"))
return local_rank, cfg, tp, global_rank == 0

f"launched under torchrun (WORLD_SIZE={env_world}) but --cfg*--tp is 1; "
f"set --cfg/--tp to use all ranks."
)
return local_rank, cfg, tp, local_rank == 0

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.

medium

In a multi-node distributed environment, LOCAL_RANK will be 0 on the first GPU of every node. Consequently, is_main (which is local_rank == 0) will evaluate to True on multiple nodes, causing multiple processes to write to the same output file or print duplicate logs. It is safer and more standard to use the global RANK environment variable to determine the main process.

Suggested change
return local_rank, cfg, tp, local_rank == 0
global_rank = int(os.environ.get("RANK", "0"))
return local_rank, cfg, tp, global_rank == 0

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant