Skip to content

[Cosmos3] Read checkpoint mixed-precision policy through wrapped transformers - #15480

Merged
yiyixuxu merged 2 commits into
huggingface:mainfrom
ConstBob:fix/cosmos3-mixed-precision-wrapped-transformer
Dec 4, 2026
Merged

yiyixuxu merged 2 commits into
huggingface:mainfrom
ConstBob:fix/cosmos3-mixed-precision-wrapped-transformer

Conversation

@ConstBob

@ConstBob ConstBob commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

The on-disk policy lookup used type(module).load_config. A torch.compile wrapper (OptimizedModule) does not have that method, so the policy silently fell back to all-W8A8. Look up load_config on the module instance instead of its class.

Other wrappers that forward attribute access have the same problem, so the lookup now uses getattr(module, "load_config", None).

Fixes #15169

Impact: with nvidia/Cosmos3-Nano-FP8 (revision 722946a6), wrapping pipe.transformer with torch.compile changed the resolved policy from fp8 to none, so the first and last 3 steps ran W8A8 instead of W8A16. With this change the wrapped output matches the unwrapped default output exactly. Without the policy (mixed_precision_format="none") the output differs.

Tests

tests/pipelines/cosmos/test_cosmos3.py::TestCosmos3OmniPipeline::test_mixed_precision_policy_resolves_through_torch_compile builds the real Cosmos3OmniTransformer from the dummy components, writes a ModelOpt FP8 step policy into its config, and checks that torch.compile resolves the same policy as the bare model.

$ pytest tests/pipelines/cosmos/test_cosmos3.py -k test_mixed_precision_policy_resolves_through_torch_compile
1 passed

$ same test with the change reverted
1 failed (resolved policy format is `none`)

$ ruff check / ruff format --check on the changed files: passed

I ran these locally with the tiny tokenizer download stubbed, because this sandbox cannot reach the Hub. The test itself is unchanged.

End-to-end on nvidia/Cosmos3-Nano-FP8 (256x448, 17 frames, 8 steps, seed 123, block-level group offload, torch.compile(..., backend="eager"), torch 2.13, modelopt 0.44):

pipe.transformer resolved policy per-step precision
bare fp8, first/last 3 W8A16 x3, W8A8 x2, W8A16 x3
bare, mixed_precision_format="none" none all base
torch.compile fp8, first/last 3 W8A16 x3, W8A8 x2, W8A16 x3

Max abs diff of the output: bare default vs compiled = 0.0, bare default vs bare none = 1.0.

Self-review notes

Ran the self-review skill on the diff before the test was moved into test_cosmos3.py. No blocking findings, no dead code, no stale docs. Left for review:

  • The policy test covers torch.compile only. The attribute-forwarding wrapper goes through the same lookup, but it is not tested separately.
  • No warning when the on-disk policy cannot be read. Distilled FP8 checkpoints legitimately have no policy, so telling the two cases apart needs a design decision.

Before submitting

Who can review?

@sayakpaul @yiyixuxu

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.

@ConstBob

ConstBob commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

CC @piotrm-nvidia since he's our colleague who reported the related issue

…sformers

Look up load_config on the module instance instead of its class.
torch.compile's OptimizedModule and other attribute-forwarding wrappers
expose the original module's load_config, so the checkpoint policy
resolves through them.
@ConstBob
ConstBob force-pushed the fix/cosmos3-mixed-precision-wrapped-transformer branch from 5e77385 to f66aa2e Compare December 3, 2026 18:39
Move the regression test into test_cosmos3.py as a single pipeline test
that uses the real Cosmos3OmniTransformer.

@piotrm-nvidia piotrm-nvidia left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks @ConstBob! I tested this PR with FlexTensor, which wraps the transformer. It works: the Cosmos3-Nano FP8 video is identical to the run without the wrapper. It also lets me simplify my example code.

Drafted with AI assistance; reviewed and verified by me.

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

@yiyixuxu
yiyixuxu merged commit 0a52e87 into huggingface:main Dec 4, 2026
11 checks passed
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.

Cosmos 3 FP8: wrapping pipe.transformer (e.g. torch.compile) silently disables the checkpoint's W8A16 step policy

4 participants