Skip to content

feat: add compiled_region option to torch.compile the VAE encoder/decoder#120

Open
xbcReal wants to merge 1 commit into
NVIDIA:mainfrom
xbcReal:feature/vae-compile-upstream
Open

feat: add compiled_region option to torch.compile the VAE encoder/decoder#120
xbcReal wants to merge 1 commit into
NVIDIA:mainfrom
xbcReal:feature/vae-compile-upstream

Conversation

@xbcReal

@xbcReal xbcReal commented Jul 21, 2026

Copy link
Copy Markdown

What does this PR do?

Adds a compiled_region knob to CompileConfig ('language' | 'all'):

  • 'language' (default) keeps the existing behavior: compile only the MoT TransformerBlocks.
  • 'all' additionally applies torch.compile to the WanVAE_ Encoder3d/Decoder3d (the CausalConv3d layers). The existing apply_compile path never touches these, since it only wraps the _encode_vision/_decode_vision post-processing functions (patchify + Linear + pos_embed, no 3D Conv).

Implementation notes

The VAE is compiled with the torch.compile defaults (fullgraph=False, dynamic=None):

  • The causal-conv caching logic mutates a Python list (feat_cache[idx] = cache_x), which creates graph breaks, so fullgraph must stay off; dynamo compiles the sub-graphs between breaks.
  • With automatic dynamic shapes (dynamic=None), the first call compiles a static kernel and dynamo recompiles once with dynamic shapes when a different temporal size shows up. The chunk temporal size only takes two values (1 frame for prime, temporal_window frames for steady state), so compilation stabilizes after at most a couple of recompiles without forcing dynamic=True.
  • The VAE runs under @torch.no_grad() with requires_grad_(False), so no backward graph is involved.

Changes

  • cosmos_framework/configs/toml_config/sft_config.py: new compiled_region field on CompileConfig
  • cosmos_framework/model/generator/omni_mot_model.py: new _compile_vae() method, invoked when compile.enabled and compiled_region == 'all'
  • examples/toml/sft_config/vision_sft_nano.toml: enable compiled_region = 'all' in the nano SFT example

…oder

Add a 'compiled_region' knob to CompileConfig ('language' | 'all').
'language' keeps the existing behavior (compile only the MoT
TransformerBlocks); 'all' additionally applies torch.compile to the
WanVAE_ Encoder3d/Decoder3d (CausalConv3d layers), which the existing
apply_compile path never touches since it only wraps the
_encode_vision/_decode_vision post-processing functions.

The VAE is compiled with the torch.compile defaults (fullgraph=False,
dynamic=None): the causal-conv feat_cache list mutation creates graph
breaks, so fullgraph must stay off; and with automatic dynamic shapes
the chunk temporal size only takes two values (1 frame for prime,
temporal_window frames for steady state), so compilation stabilizes
after at most a couple of recompiles without forcing dynamic=True.
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