-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
836cf85
commit 30ecce7
Showing
12 changed files
with
506 additions
and
3 deletions.
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
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.