-
Notifications
You must be signed in to change notification settings - Fork 133
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
Add TeaCache and FBCache #451
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
""" | ||
adapted from https://github.com/ali-vilab/TeaCache.git | ||
adapted from https://github.com/chengzeyi/ParaAttention.git | ||
""" |
This file contains 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
""" | ||
adapted from https://github.com/ali-vilab/TeaCache.git | ||
adapted from https://github.com/chengzeyi/ParaAttention.git | ||
""" |
17 changes: 17 additions & 0 deletions
17
xfuser/model_executor/plugins/cache_/diffusers_adapters/__init__.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
""" | ||
adapted from https://github.com/ali-vilab/TeaCache.git | ||
adapted from https://github.com/chengzeyi/ParaAttention.git | ||
""" | ||
import importlib | ||
from typing import Type, Dict, TypeVar | ||
from xfuser.model_executor.plugins.cache_.diffusers_adapters.registry import TRANSFORMER_ADAPTER_REGISTRY | ||
|
||
|
||
def apply_cache_on_transformer(transformer, *args, **kwargs): | ||
adapter_name = TRANSFORMER_ADAPTER_REGISTRY.get(type(transformer)) | ||
if not adapter_name: | ||
raise ValueError(f"Unknown transformer class: {transformer.__class__.__name__}") | ||
|
||
adapter_module = importlib.import_module(f".{adapter_name}", __package__) | ||
apply_cache_on_transformer_fn = getattr(adapter_module, "apply_cache_on_transformer") | ||
return apply_cache_on_transformer_fn(transformer, *args, **kwargs) |
74 changes: 74 additions & 0 deletions
74
xfuser/model_executor/plugins/cache_/diffusers_adapters/flux.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
""" | ||
adapted from https://github.com/ali-vilab/TeaCache.git | ||
adapted from https://github.com/chengzeyi/ParaAttention.git | ||
""" | ||
import functools | ||
import unittest | ||
|
||
import torch | ||
from torch import nn | ||
from diffusers import DiffusionPipeline, FluxTransformer2DModel | ||
from xfuser.model_executor.plugins.cache_.diffusers_adapters.registry import TRANSFORMER_ADAPTER_REGISTRY | ||
|
||
from xfuser.model_executor.plugins.cache_ import utils | ||
|
||
def create_cached_transformer_blocks(use_cache, transformer, rel_l1_thresh, return_hidden_states_first, num_steps): | ||
cached_transformer_class = { | ||
"Fb": utils.FBCachedTransformerBlocks, | ||
"Tea": utils.TeaCachedTransformerBlocks, | ||
}.get(use_cache) | ||
|
||
if not cached_transformer_class: | ||
raise ValueError(f"Unsupported use_cache value: {use_cache}") | ||
|
||
return cached_transformer_class( | ||
transformer.transformer_blocks, | ||
transformer.single_transformer_blocks, | ||
transformer=transformer, | ||
rel_l1_thresh=rel_l1_thresh, | ||
return_hidden_states_first=return_hidden_states_first, | ||
num_steps=num_steps, | ||
name=TRANSFORMER_ADAPTER_REGISTRY.get(type(transformer)), | ||
) | ||
|
||
|
||
def apply_cache_on_transformer( | ||
transformer: FluxTransformer2DModel, | ||
*, | ||
rel_l1_thresh=0.6, | ||
return_hidden_states_first=False, | ||
num_steps=8, | ||
use_cache="Fb", | ||
): | ||
cached_transformer_blocks = nn.ModuleList([ | ||
create_cached_transformer_blocks(use_cache, transformer, rel_l1_thresh, return_hidden_states_first, num_steps) | ||
]) | ||
|
||
dummy_single_transformer_blocks = torch.nn.ModuleList() | ||
|
||
original_forward = transformer.forward | ||
|
||
@functools.wraps(original_forward) | ||
def new_forward( | ||
self, | ||
*args, | ||
**kwargs, | ||
): | ||
with unittest.mock.patch.object( | ||
self, | ||
"transformer_blocks", | ||
cached_transformer_blocks, | ||
), unittest.mock.patch.object( | ||
self, | ||
"single_transformer_blocks", | ||
dummy_single_transformer_blocks, | ||
): | ||
return original_forward( | ||
*args, | ||
**kwargs, | ||
) | ||
|
||
transformer.forward = new_forward.__get__(transformer) | ||
|
||
return transformer | ||
|
16 changes: 16 additions & 0 deletions
16
xfuser/model_executor/plugins/cache_/diffusers_adapters/registry.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
adapted from https://github.com/ali-vilab/TeaCache.git | ||
adapted from https://github.com/chengzeyi/ParaAttention.git | ||
""" | ||
from typing import Type, Dict | ||
from diffusers.models.transformers.transformer_flux import FluxTransformer2DModel | ||
from xfuser.model_executor.models.transformers.transformer_flux import xFuserFluxTransformer2DWrapper | ||
|
||
TRANSFORMER_ADAPTER_REGISTRY: Dict[Type, str] = {} | ||
|
||
def register_transformer_adapter(transformer_class: Type, adapter_name: str): | ||
TRANSFORMER_ADAPTER_REGISTRY[transformer_class] = adapter_name | ||
|
||
register_transformer_adapter(FluxTransformer2DModel, "flux") | ||
register_transformer_adapter(xFuserFluxTransformer2DWrapper, "flux") | ||
|
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.
加 copy 代码来源。
目录cache_不要加下划线?