Fix _is_cached class/instance mismatch for multi-transformer pipelines - #1102
Merged
DefTruth merged 2 commits intoSep 2, 2026
Merged
Conversation
CachedAdapter.create_context() sets `_is_cached` on `block_adapter.pipe.__class__` while `_context_manager` is set on the instance. Pipelines that wrap more than one transformer without a real pipe (e.g. a dual-DiT model calling enable_cache once per transformer) get a fresh FakeDiffusionPipeline() instance per transformer, but every instance shares the same class. Once the first instance's cache setup flips the class-level `_is_cached` flag, `BlockAdapter.is_cached()` (a plain getattr, so it can't distinguish instance vs. inherited class attributes) reports every sibling instance as already cached too -- even though they never got their own `_context_manager` -- so create_context() returns early and the caller's `assert hasattr(block_adapter.pipe, "_context_manager")` fails. Only skip context creation when this specific pipe instance actually already has a context manager. Added a regression test (tests/api/test_multi_transformer_is_cached.py) that enables cache on two independent transformer-only BlockAdapters and verifies the second one gets its own context manager instead of crashing.
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a caching-state leak that occurs when multiple transformer-only BlockAdapters each create their own FakeDiffusionPipeline() instance but share the same pipeline class, causing class-level _is_cached to incorrectly short-circuit cache setup for sibling instances.
Changes:
- Tighten
CachedAdapter.create_context()’s early-return condition so it only skips when the current pipe instance already has a context manager. - Add a regression test covering two independent transformer-only adapters to ensure both get distinct context managers.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/api/test_multi_transformer_is_cached.py | Adds regression coverage for multi-transformer transformer-only caching with shared FakeDiffusionPipeline class. |
| src/cache_dit/caching/cache_adapters/cache_adapter.py | Prevents premature early-return in create_context() when _is_cached is inherited from the pipe class but the instance lacks _context_manager. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Member
|
The pre-commit ci failed, please init and run pre-commit |
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CachedAdapter.create_context()sets_is_cachedonblock_adapter.pipe.__class__,but sets
_context_manageron the instance:For pipelines with more than one transformer that don't have a real
pipeobject(a
BlockAdapter(transformer=..., ...)call per transformer, as one would do for adual/multi-DiT model), each transformer gets wrapped in its own fresh
FakeDiffusionPipeline()instance -- but all of those instances share the sameclass. Once the first transformer's setup flips the class-level
_is_cachedflag,BlockAdapter.is_cached()-- ultimately a plaingetattr(pipe, "_is_cached", False),which can't distinguish an instance attribute from an inherited class attribute --
reports every sibling instance as already cached too, even though it never got its
own
_context_manager.create_context()then returns early, and the caller'sassert hasattr(block_adapter.pipe, "_context_manager")fails for the secondtransformer.
Fix: only take the early-return/skip path when this pipe instance actually already
carries its own
_context_manager.Repro / verification
Added
tests/api/test_multi_transformer_is_cached.py, which callsenable_cacheontwo independent transformer-only
BlockAdapters (mirroring what a dual-DiT modeldoes) and asserts the second one gets its own context manager.
main: fails withPipeline has been already cached, skip creating cache context again.for the second transformer -- confirming the class-level flag leaked from the first._context_manager.Notes
Found while integrating a dual-DiT pipeline (video + audio branches, each wrapped
via the transformer-only API) with cache-dit.