-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Implement TeaCache #12652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LawJarp-A
wants to merge
34
commits into
huggingface:main
Choose a base branch
from
LawJarp-A:teacache-flux
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Implement TeaCache #12652
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
781c7e3
base implement teacahce flux; follow original impl
LawJarp-A 549bf97
update cache utils with teacache
LawJarp-A 29d4ffc
change hook to inline transformer processing instead of calling origi…
LawJarp-A 07c6718
add extensive docstring (auto gen); add repr
LawJarp-A a7598a1
add param validation and error messages
LawJarp-A d9648e5
add basic logging
LawJarp-A 59cb890
add compatible test
LawJarp-A 5227937
Merge branch 'main' into teacache-flux
LawJarp-A 296aa8d
Merge branch 'main' into teacache-flux
LawJarp-A 44663de
update it to make it model agnostic
LawJarp-A 4d34020
add TeaCache hook tests and ensure cache integration passes style and…
LawJarp-A 9dab52f
Add multi-model TeaCache support for Mochi, Lumina2, and CogVideoX wi…
LawJarp-A 4cb71d6
simplify model extract; use logger
LawJarp-A ad90044
Merge branch 'main' into teacache-flux
LawJarp-A f2793fd
Merge branch 'main' into teacache-flux
LawJarp-A f0abb3c
fix(teacache): fix return_dict handling, CogVideoX fallback bug, add …
LawJarp-A 4a6afef
Fix TeaCache state management and add num_inference_steps param
LawJarp-A 8646908
fixed counter manage, cogvoideox missing norm proj added
LawJarp-A c62ddc2
Merge branch 'main' into teacache-flux
LawJarp-A c2c4c76
Merge branch 'huggingface:main' into teacache-flux
LawJarp-A 38effa1
Refactor TeaCache hook into adapters
LawJarp-A 9f0fbff
Merge branch 'huggingface:main' into teacache-flux
LawJarp-A c605f4d
refactor teacache: replace adapter classes with standalone functions
LawJarp-A 883699e
refactor teacache: remove closure pattern, use standalone utility fun…
LawJarp-A 29110bf
cleanup: remove dead comments
LawJarp-A 308e4c3
Merge branch 'main' into teacache-flux
sayakpaul e328ccc
cleanup: code quality fixes
LawJarp-A 8946aeb
refactor: improve code quality, type hints
LawJarp-A abb24e0
fix: move TeaCache context setting to denoising loop for proper state…
LawJarp-A c45af51
simplify implementation and reduce code duplication
LawJarp-A 90eb746
simplify TeaCache tests with module-level imports and model factory h…
LawJarp-A c6c5339
Merge branch 'main' into teacache-flux
LawJarp-A b38315d
refactor: enhance type hints and add ControlNet support in TeaCache f…
LawJarp-A 08deb44
Apply style fixes
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,11 +40,14 @@ def __init__(self, state_cls: BaseState, init_args=None, init_kwargs=None): | |
| self._current_context = None | ||
|
|
||
| def get_state(self): | ||
| if self._current_context is None: | ||
| raise ValueError("No context is set. Please set a context before retrieving the state.") | ||
| if self._current_context not in self._state_cache.keys(): | ||
| self._state_cache[self._current_context] = self._state_cls(*self._init_args, **self._init_kwargs) | ||
| return self._state_cache[self._current_context] | ||
| context = self._current_context | ||
| if context is None: | ||
| # Fallback to default context for backward compatibility with | ||
| # pipelines that don't call cache_context() | ||
| context = "_default" | ||
| if context not in self._state_cache: | ||
| self._state_cache[context] = self._state_cls(*self._init_args, **self._init_kwargs) | ||
| return self._state_cache[context] | ||
|
|
||
| def set_context(self, name: str) -> None: | ||
| self._current_context = name | ||
|
|
@@ -133,7 +136,7 @@ def reset_state(self, module: torch.nn.Module): | |
| return module | ||
|
|
||
| def _set_context(self, module: torch.nn.Module, name: str) -> None: | ||
| # Iterate over all attributes of the hook to see if any of them have the type `StateManager`. If so, call `set_context` on them. | ||
| """Set context on all StateManager attributes of this hook.""" | ||
|
Comment on lines
-136
to
+139
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we please revert the changes unrelated to this PR? Makes reviewing a bit easier since the diff becomes smaller. |
||
| for attr_name in dir(self): | ||
| attr = getattr(self, attr_name) | ||
| if isinstance(attr, StateManager): | ||
|
|
@@ -142,22 +145,12 @@ def _set_context(self, module: torch.nn.Module, name: str) -> None: | |
|
|
||
|
|
||
| class HookFunctionReference: | ||
| def __init__(self) -> None: | ||
| """A container class that maintains mutable references to forward pass functions in a hook chain. | ||
|
|
||
| Its mutable nature allows the hook system to modify the execution chain dynamically without rebuilding the | ||
| entire forward pass structure. | ||
| """Mutable container for forward pass function references in a hook chain. | ||
|
|
||
| Attributes: | ||
| pre_forward: A callable that processes inputs before the main forward pass. | ||
| post_forward: A callable that processes outputs after the main forward pass. | ||
| forward: The current forward function in the hook chain. | ||
| original_forward: The original forward function, stored when a hook provides a custom new_forward. | ||
| Enables dynamic modification of the execution chain without rebuilding. | ||
| """ | ||
|
|
||
| The class enables hook removal by allowing updates to the forward chain through reference modification rather | ||
| than requiring reconstruction of the entire chain. When a hook is removed, only the relevant references need to | ||
| be updated, preserving the execution order of the remaining hooks. | ||
| """ | ||
| def __init__(self) -> None: | ||
| self.pre_forward = None | ||
| self.post_forward = None | ||
| self.forward = None | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this branch not error out like previous?