First Block Cache (#11180) · huggingface/diffusers@0454fbb · GitHub
Skip to content

Commit 0454fbb

Browse files
a-r-r-o-wDN6
andauthored
First Block Cache (#11180)
* update * modify flux single blocks to make compatible with cache techniques (without too much model-specific intrusion code) * remove debug logs * update * cache context for different batches of data * fix hs residual bug for single return outputs; support ltx * fix controlnet flux * support flux, ltx i2v, ltx condition * update * update * Update docs/source/en/api/cache.md * Update src/diffusers/hooks/hooks.py Co-authored-by: Dhruv Nair <dhruv.nair@gmail.com> * address review comments pt. 1 * address review comments pt. 2 * cache context refacotr; address review pt. 3 * address review comments * metadata registration with decorators instead of centralized * support cogvideox * support mochi * fix * remove unused function * remove central registry based on review * update --------- Co-authored-by: Dhruv Nair <dhruv.nair@gmail.com>
1 parent cbc8ced commit 0454fbb

32 files changed

Lines changed: 902 additions & 170 deletions

‎docs/source/en/api/cache.md‎

Lines changed: 6 additions & 0 deletions

‎src/diffusers/__init__.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@
133133
_import_structure["hooks"].extend(
134134
[
135135
"FasterCacheConfig",
136+
"FirstBlockCacheConfig",
136137
"HookRegistry",
137138
"PyramidAttentionBroadcastConfig",
138139
"apply_faster_cache",
140+
"apply_first_block_cache",
139141
"apply_pyramid_attention_broadcast",
140142
]
141143
)
@@ -751,9 +753,11 @@
751753
else:
752754
from .hooks import (
753755
FasterCacheConfig,
756+
FirstBlockCacheConfig,
754757
HookRegistry,
755758
PyramidAttentionBroadcastConfig,
756759
apply_faster_cache,
760+
apply_first_block_cache,
757761
apply_pyramid_attention_broadcast,
758762
)
759763
from .models import (

‎src/diffusers/hooks/__init__.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1+
# Copyright 2024 The HuggingFace Team. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
from ..utils import is_torch_available
216

317

418
if is_torch_available():
519
from .faster_cache import FasterCacheConfig, apply_faster_cache
20+
from .first_block_cache import FirstBlockCacheConfig, apply_first_block_cache
621
from .group_offloading import apply_group_offloading
722
from .hooks import HookRegistry, ModelHook
823
from .layerwise_casting import apply_layerwise_casting, apply_layerwise_casting_hook

‎src/diffusers/hooks/_common.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2024 The HuggingFace Team. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from ..models.attention_processor import Attention, MochiAttention
16+
17+
18+
_ATTENTION_CLASSES = (Attention, MochiAttention)
19+
20+
_SPATIAL_TRANSFORMER_BLOCK_IDENTIFIERS = ("blocks", "transformer_blocks", "single_transformer_blocks", "layers")
21+
_TEMPORAL_TRANSFORMER_BLOCK_IDENTIFIERS = ("temporal_transformer_blocks",)
22+
_CROSS_TRANSFORMER_BLOCK_IDENTIFIERS = ("blocks", "transformer_blocks", "layers")
23+
24+
_ALL_TRANSFORMER_BLOCK_IDENTIFIERS = tuple(
25+
{
26+
*_SPATIAL_TRANSFORMER_BLOCK_IDENTIFIERS,
27+
*_TEMPORAL_TRANSFORMER_BLOCK_IDENTIFIERS,
28+
*_CROSS_TRANSFORMER_BLOCK_IDENTIFIERS,
29+
}
30+
)

‎src/diffusers/hooks/_helpers.py‎

Lines changed: 264 additions & 0 deletions

0 commit comments

Comments
 (0)