Skip to content

Fix CompatibleDiT len() crash with cache_model + torch.compile (#502)#588

Open
Deaquay wants to merge 1 commit into
numz:mainfrom
Deaquay:fix/optimized-module-truthiness
Open

Fix CompatibleDiT len() crash with cache_model + torch.compile (#502)#588
Deaquay wants to merge 1 commit into
numz:mainfrom
Deaquay:fix/optimized-module-truthiness

Conversation

@Deaquay

@Deaquay Deaquay commented Jun 1, 2026

Copy link
Copy Markdown

When cache_model=True is combined with torch.compile, cached DiT/VAE modules are wrapped in torch._dynamo.OptimizedModule. Several call sites do implicit truthiness checks on these wrapped modules, which invoke __bool__, fall back to __len__, and raise TypeError: CompatibleDiT does not support len() because the underlying nn.Module subclass doesn't implement __len__.

This PR replaces those checks with explicit is None / is not None identity checks on potentially-compiled module objects. src/core/generation_utils.py already uses this convention (lines 799, 807); this brings generation_phases.py and model_configuration.py in line with it.

Sites patched

  • src/core/model_configuration.py:596, 616if cached_model:
  • src/core/generation_phases.py:298, 620, 896if runner.X and ...
  • src/core/generation_phases.py:310, 632and not ctx[...]['cached_X']:
  • src/core/generation_phases.py:319, 641ctx[...]['cached_X'] or ctx[...]['X_newly_cached']

Testing

RTX 5090, 7B fp8 model (seedvr2_ema_7b_fp8_e4m3fn_mixed_block35_fp16.safetensors), cache_model=True, torch.compile enabled.

  • Without the patch: gen 2 of any session crashes with TypeError: CompatibleDiT does not support len() at the first if cached_model: (line 596 of model_configuration.py), then progressively at the other sites if individual sites are patched in isolation.
  • With the patch: two sequential generations succeed; second gen correctly hits the cached runner (♻️ Reusing cached runner template) and skips re-materialization.

Refs #502.

)

When cache_model=True is combined with torch.compile, cached DiT/VAE modules are wrapped in torch._dynamo.OptimizedModule. Several call sites do implicit truthiness checks on these wrapped modules which invoke __bool__, fall back to __len__, and raise TypeError because the underlying nn.Module subclass does not implement __len__.

This patch replaces those checks with explicit is None / is not None identity checks on potentially-compiled module objects. generation_utils.py already uses this convention (lines 799, 807); this brings generation_phases.py and model_configuration.py in line with it.

Sites patched:
- src/core/model_configuration.py:596, 616 (if cached_model:)
- src/core/generation_phases.py:298, 620, 896 (if runner.X and ...)
- src/core/generation_phases.py:310, 632 (and not ctx[...][cached_X])
- src/core/generation_phases.py:319, 641 (ctx[...][cached_X] or ...)

Tested on RTX 5090 with 7B fp8 model: without the patch, gen 2 of any session with cache_model=True and torch.compile enabled crashes with TypeError. With the patch, two sequential generations succeed and the second hits the cached runner.
@Deaquay

Deaquay commented Jun 1, 2026

Copy link
Copy Markdown
Author

Note: Issue #502 was closed by its reporter after they worked around the crash by disabling torch.compile (removing the compile-settings node from their workflow). However, the underlying bug remains — cache_model=True + torch.compile is still incompatible. This PR fixes the root cause, allowing both features to be used together.

@jtreminio

Copy link
Copy Markdown

This should be merged.

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.

2 participants