cache-dit 0.3.2__py3-none-any.whl → 1.0.14__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cache_dit/__init__.py +37 -19
- cache_dit/_version.py +2 -2
- cache_dit/caching/__init__.py +36 -0
- cache_dit/{cache_factory → caching}/block_adapters/__init__.py +149 -18
- cache_dit/{cache_factory → caching}/block_adapters/block_adapters.py +91 -7
- cache_dit/caching/block_adapters/block_registers.py +118 -0
- cache_dit/caching/cache_adapters/__init__.py +1 -0
- cache_dit/{cache_factory → caching}/cache_adapters/cache_adapter.py +262 -123
- cache_dit/caching/cache_blocks/__init__.py +226 -0
- cache_dit/caching/cache_blocks/offload_utils.py +115 -0
- cache_dit/caching/cache_blocks/pattern_0_1_2.py +26 -0
- cache_dit/caching/cache_blocks/pattern_3_4_5.py +543 -0
- cache_dit/caching/cache_blocks/pattern_base.py +748 -0
- cache_dit/caching/cache_blocks/pattern_utils.py +86 -0
- cache_dit/caching/cache_contexts/__init__.py +28 -0
- cache_dit/caching/cache_contexts/cache_config.py +120 -0
- cache_dit/{cache_factory → caching}/cache_contexts/cache_context.py +29 -90
- cache_dit/{cache_factory → caching}/cache_contexts/cache_manager.py +138 -10
- cache_dit/{cache_factory → caching}/cache_contexts/calibrators/__init__.py +25 -3
- cache_dit/{cache_factory → caching}/cache_contexts/calibrators/foca.py +1 -1
- cache_dit/{cache_factory → caching}/cache_contexts/calibrators/taylorseer.py +81 -9
- cache_dit/caching/cache_contexts/context_manager.py +36 -0
- cache_dit/caching/cache_contexts/prune_config.py +63 -0
- cache_dit/caching/cache_contexts/prune_context.py +155 -0
- cache_dit/caching/cache_contexts/prune_manager.py +167 -0
- cache_dit/caching/cache_interface.py +358 -0
- cache_dit/{cache_factory → caching}/cache_types.py +19 -2
- cache_dit/{cache_factory → caching}/forward_pattern.py +14 -14
- cache_dit/{cache_factory → caching}/params_modifier.py +10 -10
- cache_dit/caching/patch_functors/__init__.py +15 -0
- cache_dit/{cache_factory → caching}/patch_functors/functor_chroma.py +1 -1
- cache_dit/{cache_factory → caching}/patch_functors/functor_dit.py +1 -1
- cache_dit/{cache_factory → caching}/patch_functors/functor_flux.py +1 -1
- cache_dit/{cache_factory → caching}/patch_functors/functor_hidream.py +2 -4
- cache_dit/{cache_factory → caching}/patch_functors/functor_hunyuan_dit.py +1 -1
- cache_dit/caching/patch_functors/functor_qwen_image_controlnet.py +263 -0
- cache_dit/caching/utils.py +68 -0
- cache_dit/metrics/__init__.py +11 -0
- cache_dit/metrics/metrics.py +3 -0
- cache_dit/parallelism/__init__.py +3 -0
- cache_dit/parallelism/backends/native_diffusers/__init__.py +6 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/__init__.py +164 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/attention/__init__.py +4 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/attention/_attention_dispatch.py +304 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/cp_plan_chroma.py +95 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/cp_plan_cogvideox.py +202 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/cp_plan_cogview.py +299 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/cp_plan_cosisid.py +123 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/cp_plan_dit.py +94 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/cp_plan_flux.py +88 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/cp_plan_hunyuan.py +729 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/cp_plan_ltxvideo.py +264 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/cp_plan_nunchaku.py +407 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/cp_plan_pixart.py +285 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/cp_plan_qwen_image.py +104 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/cp_plan_registers.py +84 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/cp_plan_wan.py +101 -0
- cache_dit/parallelism/backends/native_diffusers/context_parallelism/cp_planners.py +117 -0
- cache_dit/parallelism/backends/native_diffusers/parallel_difffusers.py +49 -0
- cache_dit/parallelism/backends/native_diffusers/utils.py +11 -0
- cache_dit/parallelism/backends/native_pytorch/__init__.py +6 -0
- cache_dit/parallelism/backends/native_pytorch/parallel_torch.py +62 -0
- cache_dit/parallelism/backends/native_pytorch/tensor_parallelism/__init__.py +48 -0
- cache_dit/parallelism/backends/native_pytorch/tensor_parallelism/tp_plan_flux.py +171 -0
- cache_dit/parallelism/backends/native_pytorch/tensor_parallelism/tp_plan_kandinsky5.py +79 -0
- cache_dit/parallelism/backends/native_pytorch/tensor_parallelism/tp_plan_qwen_image.py +78 -0
- cache_dit/parallelism/backends/native_pytorch/tensor_parallelism/tp_plan_registers.py +65 -0
- cache_dit/parallelism/backends/native_pytorch/tensor_parallelism/tp_plan_wan.py +153 -0
- cache_dit/parallelism/backends/native_pytorch/tensor_parallelism/tp_planners.py +14 -0
- cache_dit/parallelism/parallel_backend.py +26 -0
- cache_dit/parallelism/parallel_config.py +88 -0
- cache_dit/parallelism/parallel_interface.py +77 -0
- cache_dit/quantize/__init__.py +7 -0
- cache_dit/quantize/backends/__init__.py +1 -0
- cache_dit/quantize/backends/bitsandbytes/__init__.py +0 -0
- cache_dit/quantize/backends/torchao/__init__.py +1 -0
- cache_dit/quantize/{quantize_ao.py → backends/torchao/quantize_ao.py} +44 -30
- cache_dit/quantize/quantize_backend.py +0 -0
- cache_dit/quantize/quantize_config.py +0 -0
- cache_dit/quantize/quantize_interface.py +3 -16
- cache_dit/summary.py +593 -0
- cache_dit/utils.py +46 -290
- cache_dit-1.0.14.dist-info/METADATA +301 -0
- cache_dit-1.0.14.dist-info/RECORD +102 -0
- cache_dit-1.0.14.dist-info/licenses/LICENSE +203 -0
- cache_dit/cache_factory/__init__.py +0 -28
- cache_dit/cache_factory/block_adapters/block_registers.py +0 -90
- cache_dit/cache_factory/cache_adapters/__init__.py +0 -1
- cache_dit/cache_factory/cache_blocks/__init__.py +0 -72
- cache_dit/cache_factory/cache_blocks/pattern_0_1_2.py +0 -16
- cache_dit/cache_factory/cache_blocks/pattern_3_4_5.py +0 -238
- cache_dit/cache_factory/cache_blocks/pattern_base.py +0 -404
- cache_dit/cache_factory/cache_blocks/utils.py +0 -41
- cache_dit/cache_factory/cache_contexts/__init__.py +0 -14
- cache_dit/cache_factory/cache_interface.py +0 -217
- cache_dit/cache_factory/patch_functors/__init__.py +0 -12
- cache_dit/cache_factory/utils.py +0 -57
- cache_dit-0.3.2.dist-info/METADATA +0 -753
- cache_dit-0.3.2.dist-info/RECORD +0 -56
- cache_dit-0.3.2.dist-info/licenses/LICENSE +0 -53
- /cache_dit/{cache_factory → caching}/.gitignore +0 -0
- /cache_dit/{cache_factory → caching}/cache_contexts/calibrators/base.py +0 -0
- /cache_dit/{cache_factory → caching}/patch_functors/functor_base.py +0 -0
- /cache_dit/{custom_ops → kernels}/__init__.py +0 -0
- /cache_dit/{custom_ops → kernels}/triton_taylorseer.py +0 -0
- {cache_dit-0.3.2.dist-info → cache_dit-1.0.14.dist-info}/WHEEL +0 -0
- {cache_dit-0.3.2.dist-info → cache_dit-1.0.14.dist-info}/entry_points.txt +0 -0
- {cache_dit-0.3.2.dist-info → cache_dit-1.0.14.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved
|
|
2
|
+
|
|
3
|
+
Apache License
|
|
4
|
+
Version 2.0, January 2004
|
|
5
|
+
http://www.apache.org/licenses/
|
|
6
|
+
|
|
7
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
8
|
+
|
|
9
|
+
1. Definitions.
|
|
10
|
+
|
|
11
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
12
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
13
|
+
|
|
14
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
15
|
+
the copyright owner that is granting the License.
|
|
16
|
+
|
|
17
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
18
|
+
other entities that control, are controlled by, or are under common
|
|
19
|
+
control with that entity. For the purposes of this definition,
|
|
20
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
21
|
+
direction or management of such entity, whether by contract or
|
|
22
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
23
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
24
|
+
|
|
25
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
26
|
+
exercising permissions granted by this License.
|
|
27
|
+
|
|
28
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
29
|
+
including but not limited to software source code, documentation
|
|
30
|
+
source, and configuration files.
|
|
31
|
+
|
|
32
|
+
"Object" form shall mean any form resulting from mechanical
|
|
33
|
+
transformation or translation of a Source form, including but
|
|
34
|
+
not limited to compiled object code, generated documentation,
|
|
35
|
+
and conversions to other media types.
|
|
36
|
+
|
|
37
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
38
|
+
Object form, made available under the License, as indicated by a
|
|
39
|
+
copyright notice that is included in or attached to the work
|
|
40
|
+
(an example is provided in the Appendix below).
|
|
41
|
+
|
|
42
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
43
|
+
form, that is based on (or derived from) the Work and for which the
|
|
44
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
45
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
46
|
+
of this License, Derivative Works shall not include works that remain
|
|
47
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
48
|
+
the Work and Derivative Works thereof.
|
|
49
|
+
|
|
50
|
+
"Contribution" shall mean any work of authorship, including
|
|
51
|
+
the original version of the Work and any modifications or additions
|
|
52
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
53
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
54
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
55
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
56
|
+
means any form of electronic, verbal, or written communication sent
|
|
57
|
+
to the Licensor or its representatives, including but not limited to
|
|
58
|
+
communication on electronic mailing lists, source code control systems,
|
|
59
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
60
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
61
|
+
excluding communication that is conspicuously marked or otherwise
|
|
62
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
63
|
+
|
|
64
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
65
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
66
|
+
subsequently incorporated within the Work.
|
|
67
|
+
|
|
68
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
69
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
70
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
71
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
72
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
73
|
+
Work and such Derivative Works in Source or Object form.
|
|
74
|
+
|
|
75
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
76
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
77
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
78
|
+
(except as stated in this section) patent license to make, have made,
|
|
79
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
80
|
+
where such license applies only to those patent claims licensable
|
|
81
|
+
by such Contributor that are necessarily infringed by their
|
|
82
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
83
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
84
|
+
institute patent litigation against any entity (including a
|
|
85
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
86
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
87
|
+
or contributory patent infringement, then any patent licenses
|
|
88
|
+
granted to You under this License for that Work shall terminate
|
|
89
|
+
as of the date such litigation is filed.
|
|
90
|
+
|
|
91
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
92
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
93
|
+
modifications, and in Source or Object form, provided that You
|
|
94
|
+
meet the following conditions:
|
|
95
|
+
|
|
96
|
+
(a) You must give any other recipients of the Work or
|
|
97
|
+
Derivative Works a copy of this License; and
|
|
98
|
+
|
|
99
|
+
(b) You must cause any modified files to carry prominent notices
|
|
100
|
+
stating that You changed the files; and
|
|
101
|
+
|
|
102
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
103
|
+
that You distribute, all copyright, patent, trademark, and
|
|
104
|
+
attribution notices from the Source form of the Work,
|
|
105
|
+
excluding those notices that do not pertain to any part of
|
|
106
|
+
the Derivative Works; and
|
|
107
|
+
|
|
108
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
109
|
+
distribution, then any Derivative Works that You distribute must
|
|
110
|
+
include a readable copy of the attribution notices contained
|
|
111
|
+
within such NOTICE file, excluding those notices that do not
|
|
112
|
+
pertain to any part of the Derivative Works, in at least one
|
|
113
|
+
of the following places: within a NOTICE text file distributed
|
|
114
|
+
as part of the Derivative Works; within the Source form or
|
|
115
|
+
documentation, if provided along with the Derivative Works; or,
|
|
116
|
+
within a display generated by the Derivative Works, if and
|
|
117
|
+
wherever such third-party notices normally appear. The contents
|
|
118
|
+
of the NOTICE file are for informational purposes only and
|
|
119
|
+
do not modify the License. You may add Your own attribution
|
|
120
|
+
notices within Derivative Works that You distribute, alongside
|
|
121
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
122
|
+
that such additional attribution notices cannot be construed
|
|
123
|
+
as modifying the License.
|
|
124
|
+
|
|
125
|
+
You may add Your own copyright statement to Your modifications and
|
|
126
|
+
may provide additional or different license terms and conditions
|
|
127
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
128
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
129
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
130
|
+
the conditions stated in this License.
|
|
131
|
+
|
|
132
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
133
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
134
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
135
|
+
this License, without any additional terms or conditions.
|
|
136
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
137
|
+
the terms of any separate license agreement you may have executed
|
|
138
|
+
with Licensor regarding such Contributions.
|
|
139
|
+
|
|
140
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
141
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
142
|
+
except as required for reasonable and customary use in describing the
|
|
143
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
144
|
+
|
|
145
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
146
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
147
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
148
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
149
|
+
implied, including, without limitation, any warranties or conditions
|
|
150
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
151
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
152
|
+
appropriateness of using or redistributing the Work and assume any
|
|
153
|
+
risks associated with Your exercise of permissions under this License.
|
|
154
|
+
|
|
155
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
156
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
157
|
+
unless required by applicable law (such as deliberate and grossly
|
|
158
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
159
|
+
liable to You for damages, including any direct, indirect, special,
|
|
160
|
+
incidental, or consequential damages of any character arising as a
|
|
161
|
+
result of this License or out of the use or inability to use the
|
|
162
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
163
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
164
|
+
other commercial damages or losses), even if such Contributor
|
|
165
|
+
has been advised of the possibility of such damages.
|
|
166
|
+
|
|
167
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
168
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
169
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
170
|
+
or other liability obligations and/or rights consistent with this
|
|
171
|
+
License. However, in accepting such obligations, You may act only
|
|
172
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
173
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
174
|
+
defend, and hold each Contributor harmless for any liability
|
|
175
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
176
|
+
of your accepting any such warranty or additional liability.
|
|
177
|
+
|
|
178
|
+
END OF TERMS AND CONDITIONS
|
|
179
|
+
|
|
180
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
181
|
+
|
|
182
|
+
To apply the Apache License to your work, attach the following
|
|
183
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
184
|
+
replaced with your own identifying information. (Don't include
|
|
185
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
186
|
+
comment syntax for the file format. We also recommend that a
|
|
187
|
+
file or class name and description of purpose be included on the
|
|
188
|
+
same "printed page" as the copyright notice for easier
|
|
189
|
+
identification within third-party archives.
|
|
190
|
+
|
|
191
|
+
Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
|
192
|
+
|
|
193
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
194
|
+
you may not use this file except in compliance with the License.
|
|
195
|
+
You may obtain a copy of the License at
|
|
196
|
+
|
|
197
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
198
|
+
|
|
199
|
+
Unless required by applicable law or agreed to in writing, software
|
|
200
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
201
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
202
|
+
See the License for the specific language governing permissions and
|
|
203
|
+
limitations under the License.
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
from cache_dit.cache_factory.cache_types import CacheType
|
|
2
|
-
from cache_dit.cache_factory.cache_types import cache_type
|
|
3
|
-
from cache_dit.cache_factory.cache_types import block_range
|
|
4
|
-
|
|
5
|
-
from cache_dit.cache_factory.forward_pattern import ForwardPattern
|
|
6
|
-
from cache_dit.cache_factory.params_modifier import ParamsModifier
|
|
7
|
-
from cache_dit.cache_factory.patch_functors import PatchFunctor
|
|
8
|
-
|
|
9
|
-
from cache_dit.cache_factory.block_adapters import BlockAdapter
|
|
10
|
-
from cache_dit.cache_factory.block_adapters import BlockAdapterRegistry
|
|
11
|
-
|
|
12
|
-
from cache_dit.cache_factory.cache_contexts import CachedContext
|
|
13
|
-
from cache_dit.cache_factory.cache_contexts import BasicCacheConfig
|
|
14
|
-
from cache_dit.cache_factory.cache_contexts import CachedContextManager
|
|
15
|
-
from cache_dit.cache_factory.cache_contexts import CalibratorConfig
|
|
16
|
-
from cache_dit.cache_factory.cache_contexts import TaylorSeerCalibratorConfig
|
|
17
|
-
from cache_dit.cache_factory.cache_contexts import FoCaCalibratorConfig
|
|
18
|
-
|
|
19
|
-
from cache_dit.cache_factory.cache_blocks import CachedBlocks
|
|
20
|
-
|
|
21
|
-
from cache_dit.cache_factory.cache_adapters import CachedAdapter
|
|
22
|
-
|
|
23
|
-
from cache_dit.cache_factory.cache_interface import enable_cache
|
|
24
|
-
from cache_dit.cache_factory.cache_interface import disable_cache
|
|
25
|
-
from cache_dit.cache_factory.cache_interface import supported_pipelines
|
|
26
|
-
from cache_dit.cache_factory.cache_interface import get_adapter
|
|
27
|
-
|
|
28
|
-
from cache_dit.cache_factory.utils import load_options
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
from typing import Any, Tuple, List, Dict, Callable
|
|
2
|
-
|
|
3
|
-
from diffusers import DiffusionPipeline
|
|
4
|
-
from cache_dit.cache_factory.block_adapters.block_adapters import BlockAdapter
|
|
5
|
-
|
|
6
|
-
from cache_dit.logger import init_logger
|
|
7
|
-
|
|
8
|
-
logger = init_logger(__name__)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
class BlockAdapterRegistry:
|
|
12
|
-
_adapters: Dict[str, Callable[..., BlockAdapter]] = {}
|
|
13
|
-
_predefined_adapters_has_separate_cfg: List[str] = [
|
|
14
|
-
"QwenImage",
|
|
15
|
-
"Wan",
|
|
16
|
-
"CogView4",
|
|
17
|
-
"Cosmos",
|
|
18
|
-
"SkyReelsV2",
|
|
19
|
-
"Chroma",
|
|
20
|
-
"Lumina2",
|
|
21
|
-
]
|
|
22
|
-
|
|
23
|
-
@classmethod
|
|
24
|
-
def register(cls, name: str, supported: bool = True):
|
|
25
|
-
def decorator(
|
|
26
|
-
func: Callable[..., BlockAdapter]
|
|
27
|
-
) -> Callable[..., BlockAdapter]:
|
|
28
|
-
if supported:
|
|
29
|
-
cls._adapters[name] = func
|
|
30
|
-
return func
|
|
31
|
-
|
|
32
|
-
return decorator
|
|
33
|
-
|
|
34
|
-
@classmethod
|
|
35
|
-
def get_adapter(
|
|
36
|
-
cls,
|
|
37
|
-
pipe: DiffusionPipeline | str | Any,
|
|
38
|
-
**kwargs,
|
|
39
|
-
) -> BlockAdapter:
|
|
40
|
-
if not isinstance(pipe, str):
|
|
41
|
-
pipe_cls_name: str = pipe.__class__.__name__
|
|
42
|
-
else:
|
|
43
|
-
pipe_cls_name = pipe
|
|
44
|
-
|
|
45
|
-
for name in cls._adapters:
|
|
46
|
-
if pipe_cls_name.startswith(name):
|
|
47
|
-
return cls._adapters[name](pipe, **kwargs)
|
|
48
|
-
|
|
49
|
-
return BlockAdapter()
|
|
50
|
-
|
|
51
|
-
@classmethod
|
|
52
|
-
def has_separate_cfg(
|
|
53
|
-
cls,
|
|
54
|
-
pipe_or_adapter: DiffusionPipeline | BlockAdapter | Any,
|
|
55
|
-
) -> bool:
|
|
56
|
-
|
|
57
|
-
# Prefer custom setting from block adapter.
|
|
58
|
-
if isinstance(pipe_or_adapter, BlockAdapter):
|
|
59
|
-
return pipe_or_adapter.has_separate_cfg
|
|
60
|
-
|
|
61
|
-
has_separate_cfg = False
|
|
62
|
-
if isinstance(pipe_or_adapter, DiffusionPipeline):
|
|
63
|
-
has_separate_cfg = cls.get_adapter(
|
|
64
|
-
pipe_or_adapter,
|
|
65
|
-
skip_post_init=True, # check cfg setting only
|
|
66
|
-
).has_separate_cfg
|
|
67
|
-
|
|
68
|
-
if has_separate_cfg:
|
|
69
|
-
return True
|
|
70
|
-
|
|
71
|
-
pipe_cls_name = pipe_or_adapter.__class__.__name__
|
|
72
|
-
for name in cls._predefined_adapters_has_separate_cfg:
|
|
73
|
-
if pipe_cls_name.startswith(name):
|
|
74
|
-
return True
|
|
75
|
-
|
|
76
|
-
return False
|
|
77
|
-
|
|
78
|
-
@classmethod
|
|
79
|
-
def is_supported(cls, pipe) -> bool:
|
|
80
|
-
pipe_cls_name: str = pipe.__class__.__name__
|
|
81
|
-
|
|
82
|
-
for name in cls._adapters:
|
|
83
|
-
if pipe_cls_name.startswith(name):
|
|
84
|
-
return True
|
|
85
|
-
return False
|
|
86
|
-
|
|
87
|
-
@classmethod
|
|
88
|
-
def supported_pipelines(cls, **kwargs) -> Tuple[int, List[str]]:
|
|
89
|
-
val_pipelines = cls._adapters.keys()
|
|
90
|
-
return len(val_pipelines), [p + "*" for p in val_pipelines]
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
from cache_dit.cache_factory.cache_adapters.cache_adapter import CachedAdapter
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import torch
|
|
2
|
-
|
|
3
|
-
from cache_dit.cache_factory import ForwardPattern
|
|
4
|
-
from cache_dit.cache_factory.cache_contexts.cache_context import CachedContext
|
|
5
|
-
from cache_dit.cache_factory.cache_contexts.cache_manager import (
|
|
6
|
-
CachedContextManager,
|
|
7
|
-
)
|
|
8
|
-
|
|
9
|
-
from cache_dit.cache_factory.cache_blocks.pattern_0_1_2 import (
|
|
10
|
-
CachedBlocks_Pattern_0_1_2,
|
|
11
|
-
)
|
|
12
|
-
from cache_dit.cache_factory.cache_blocks.pattern_3_4_5 import (
|
|
13
|
-
CachedBlocks_Pattern_3_4_5,
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
from cache_dit.logger import init_logger
|
|
17
|
-
|
|
18
|
-
logger = init_logger(__name__)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
class CachedBlocks:
|
|
22
|
-
def __new__(
|
|
23
|
-
cls,
|
|
24
|
-
# 0. Transformer blocks configuration
|
|
25
|
-
transformer_blocks: torch.nn.ModuleList,
|
|
26
|
-
transformer: torch.nn.Module = None,
|
|
27
|
-
forward_pattern: ForwardPattern = None,
|
|
28
|
-
check_forward_pattern: bool = True,
|
|
29
|
-
check_num_outputs: bool = True,
|
|
30
|
-
# 1. Cache context configuration
|
|
31
|
-
# 'transformer_blocks', 'blocks', 'single_transformer_blocks',
|
|
32
|
-
# 'layers', 'single_stream_blocks', 'double_stream_blocks'
|
|
33
|
-
cache_prefix: str = None, # cache_prefix maybe un-need.
|
|
34
|
-
# Usually, blocks_name, etc.
|
|
35
|
-
cache_context: CachedContext | str = None,
|
|
36
|
-
cache_manager: CachedContextManager = None,
|
|
37
|
-
**kwargs,
|
|
38
|
-
):
|
|
39
|
-
assert transformer is not None, "transformer can't be None."
|
|
40
|
-
assert forward_pattern is not None, "forward_pattern can't be None."
|
|
41
|
-
assert cache_context is not None, "cache_context can't be None."
|
|
42
|
-
assert cache_manager is not None, "cache_manager can't be None."
|
|
43
|
-
if forward_pattern in CachedBlocks_Pattern_0_1_2._supported_patterns:
|
|
44
|
-
return CachedBlocks_Pattern_0_1_2(
|
|
45
|
-
# 0. Transformer blocks configuration
|
|
46
|
-
transformer_blocks,
|
|
47
|
-
transformer=transformer,
|
|
48
|
-
forward_pattern=forward_pattern,
|
|
49
|
-
check_forward_pattern=check_forward_pattern,
|
|
50
|
-
check_num_outputs=check_num_outputs,
|
|
51
|
-
# 1. Cache context configuration
|
|
52
|
-
cache_prefix=cache_prefix,
|
|
53
|
-
cache_context=cache_context,
|
|
54
|
-
cache_manager=cache_manager,
|
|
55
|
-
**kwargs,
|
|
56
|
-
)
|
|
57
|
-
elif forward_pattern in CachedBlocks_Pattern_3_4_5._supported_patterns:
|
|
58
|
-
return CachedBlocks_Pattern_3_4_5(
|
|
59
|
-
# 0. Transformer blocks configuration
|
|
60
|
-
transformer_blocks,
|
|
61
|
-
transformer=transformer,
|
|
62
|
-
forward_pattern=forward_pattern,
|
|
63
|
-
check_forward_pattern=check_forward_pattern,
|
|
64
|
-
check_num_outputs=check_num_outputs,
|
|
65
|
-
# 1. Cache context configuration
|
|
66
|
-
cache_prefix=cache_prefix,
|
|
67
|
-
cache_context=cache_context,
|
|
68
|
-
cache_manager=cache_manager,
|
|
69
|
-
**kwargs,
|
|
70
|
-
)
|
|
71
|
-
else:
|
|
72
|
-
raise ValueError(f"Pattern {forward_pattern} is not supported now!")
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
from cache_dit.cache_factory import ForwardPattern
|
|
2
|
-
from cache_dit.cache_factory.cache_blocks.pattern_base import (
|
|
3
|
-
CachedBlocks_Pattern_Base,
|
|
4
|
-
)
|
|
5
|
-
from cache_dit.logger import init_logger
|
|
6
|
-
|
|
7
|
-
logger = init_logger(__name__)
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class CachedBlocks_Pattern_0_1_2(CachedBlocks_Pattern_Base):
|
|
11
|
-
_supported_patterns = [
|
|
12
|
-
ForwardPattern.Pattern_0,
|
|
13
|
-
ForwardPattern.Pattern_1,
|
|
14
|
-
ForwardPattern.Pattern_2,
|
|
15
|
-
]
|
|
16
|
-
...
|
|
@@ -1,238 +0,0 @@
|
|
|
1
|
-
import torch
|
|
2
|
-
|
|
3
|
-
from cache_dit.cache_factory import ForwardPattern
|
|
4
|
-
from cache_dit.cache_factory.cache_blocks.pattern_base import (
|
|
5
|
-
CachedBlocks_Pattern_Base,
|
|
6
|
-
)
|
|
7
|
-
from cache_dit.logger import init_logger
|
|
8
|
-
|
|
9
|
-
logger = init_logger(__name__)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class CachedBlocks_Pattern_3_4_5(CachedBlocks_Pattern_Base):
|
|
13
|
-
_supported_patterns = [
|
|
14
|
-
ForwardPattern.Pattern_3,
|
|
15
|
-
ForwardPattern.Pattern_4,
|
|
16
|
-
ForwardPattern.Pattern_5,
|
|
17
|
-
]
|
|
18
|
-
|
|
19
|
-
def forward(
|
|
20
|
-
self,
|
|
21
|
-
hidden_states: torch.Tensor,
|
|
22
|
-
*args,
|
|
23
|
-
**kwargs,
|
|
24
|
-
):
|
|
25
|
-
# Use it's own cache context.
|
|
26
|
-
self.cache_manager.set_context(self.cache_context)
|
|
27
|
-
self._check_cache_params()
|
|
28
|
-
|
|
29
|
-
original_hidden_states = hidden_states
|
|
30
|
-
# Call first `n` blocks to process the hidden states for
|
|
31
|
-
# more stable diff calculation.
|
|
32
|
-
hidden_states, new_encoder_hidden_states = self.call_Fn_blocks(
|
|
33
|
-
hidden_states,
|
|
34
|
-
*args,
|
|
35
|
-
**kwargs,
|
|
36
|
-
)
|
|
37
|
-
|
|
38
|
-
Fn_hidden_states_residual = hidden_states - original_hidden_states
|
|
39
|
-
del original_hidden_states
|
|
40
|
-
|
|
41
|
-
self.cache_manager.mark_step_begin()
|
|
42
|
-
# Residual L1 diff or Hidden States L1 diff
|
|
43
|
-
can_use_cache = self.cache_manager.can_cache(
|
|
44
|
-
(
|
|
45
|
-
Fn_hidden_states_residual
|
|
46
|
-
if not self.cache_manager.is_l1_diff_enabled()
|
|
47
|
-
else hidden_states
|
|
48
|
-
),
|
|
49
|
-
parallelized=self._is_parallelized(),
|
|
50
|
-
prefix=(
|
|
51
|
-
f"{self.cache_prefix}_Fn_residual"
|
|
52
|
-
if not self.cache_manager.is_l1_diff_enabled()
|
|
53
|
-
else f"{self.cache_prefix}_Fn_hidden_states"
|
|
54
|
-
),
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
torch._dynamo.graph_break()
|
|
58
|
-
if can_use_cache:
|
|
59
|
-
self.cache_manager.add_cached_step()
|
|
60
|
-
del Fn_hidden_states_residual
|
|
61
|
-
hidden_states, new_encoder_hidden_states = (
|
|
62
|
-
self.cache_manager.apply_cache(
|
|
63
|
-
hidden_states,
|
|
64
|
-
new_encoder_hidden_states, # encoder_hidden_states not use cache
|
|
65
|
-
prefix=(
|
|
66
|
-
f"{self.cache_prefix}_Bn_residual"
|
|
67
|
-
if self.cache_manager.is_cache_residual()
|
|
68
|
-
else f"{self.cache_prefix}_Bn_hidden_states"
|
|
69
|
-
),
|
|
70
|
-
encoder_prefix=(
|
|
71
|
-
f"{self.cache_prefix}_Bn_residual"
|
|
72
|
-
if self.cache_manager.is_encoder_cache_residual()
|
|
73
|
-
else f"{self.cache_prefix}_Bn_hidden_states"
|
|
74
|
-
),
|
|
75
|
-
)
|
|
76
|
-
)
|
|
77
|
-
torch._dynamo.graph_break()
|
|
78
|
-
# Call last `n` blocks to further process the hidden states
|
|
79
|
-
# for higher precision.
|
|
80
|
-
if self.cache_manager.Bn_compute_blocks() > 0:
|
|
81
|
-
hidden_states, new_encoder_hidden_states = self.call_Bn_blocks(
|
|
82
|
-
hidden_states,
|
|
83
|
-
*args,
|
|
84
|
-
**kwargs,
|
|
85
|
-
)
|
|
86
|
-
else:
|
|
87
|
-
self.cache_manager.set_Fn_buffer(
|
|
88
|
-
Fn_hidden_states_residual,
|
|
89
|
-
prefix=f"{self.cache_prefix}_Fn_residual",
|
|
90
|
-
)
|
|
91
|
-
if self.cache_manager.is_l1_diff_enabled():
|
|
92
|
-
# for hidden states L1 diff
|
|
93
|
-
self.cache_manager.set_Fn_buffer(
|
|
94
|
-
hidden_states,
|
|
95
|
-
f"{self.cache_prefix}_Fn_hidden_states",
|
|
96
|
-
)
|
|
97
|
-
del Fn_hidden_states_residual
|
|
98
|
-
torch._dynamo.graph_break()
|
|
99
|
-
old_encoder_hidden_states = new_encoder_hidden_states
|
|
100
|
-
(
|
|
101
|
-
hidden_states,
|
|
102
|
-
new_encoder_hidden_states,
|
|
103
|
-
hidden_states_residual,
|
|
104
|
-
) = self.call_Mn_blocks( # middle
|
|
105
|
-
hidden_states,
|
|
106
|
-
*args,
|
|
107
|
-
**kwargs,
|
|
108
|
-
)
|
|
109
|
-
|
|
110
|
-
torch._dynamo.graph_break()
|
|
111
|
-
if self.cache_manager.is_cache_residual():
|
|
112
|
-
self.cache_manager.set_Bn_buffer(
|
|
113
|
-
hidden_states_residual,
|
|
114
|
-
prefix=f"{self.cache_prefix}_Bn_residual",
|
|
115
|
-
)
|
|
116
|
-
else:
|
|
117
|
-
self.cache_manager.set_Bn_buffer(
|
|
118
|
-
hidden_states,
|
|
119
|
-
prefix=f"{self.cache_prefix}_Bn_hidden_states",
|
|
120
|
-
)
|
|
121
|
-
|
|
122
|
-
if new_encoder_hidden_states is not None:
|
|
123
|
-
new_encoder_hidden_states_residual = (
|
|
124
|
-
new_encoder_hidden_states - old_encoder_hidden_states
|
|
125
|
-
)
|
|
126
|
-
if self.cache_manager.is_encoder_cache_residual():
|
|
127
|
-
if new_encoder_hidden_states is not None:
|
|
128
|
-
self.cache_manager.set_Bn_encoder_buffer(
|
|
129
|
-
new_encoder_hidden_states_residual,
|
|
130
|
-
prefix=f"{self.cache_prefix}_Bn_residual",
|
|
131
|
-
)
|
|
132
|
-
else:
|
|
133
|
-
if new_encoder_hidden_states is not None:
|
|
134
|
-
self.cache_manager.set_Bn_encoder_buffer(
|
|
135
|
-
new_encoder_hidden_states_residual,
|
|
136
|
-
prefix=f"{self.cache_prefix}_Bn_hidden_states",
|
|
137
|
-
)
|
|
138
|
-
torch._dynamo.graph_break()
|
|
139
|
-
# Call last `n` blocks to further process the hidden states
|
|
140
|
-
# for higher precision.
|
|
141
|
-
if self.cache_manager.Bn_compute_blocks() > 0:
|
|
142
|
-
hidden_states, new_encoder_hidden_states = self.call_Bn_blocks(
|
|
143
|
-
hidden_states,
|
|
144
|
-
*args,
|
|
145
|
-
**kwargs,
|
|
146
|
-
)
|
|
147
|
-
|
|
148
|
-
torch._dynamo.graph_break()
|
|
149
|
-
|
|
150
|
-
return (
|
|
151
|
-
hidden_states
|
|
152
|
-
if self.forward_pattern.Return_H_Only
|
|
153
|
-
else (
|
|
154
|
-
(hidden_states, new_encoder_hidden_states)
|
|
155
|
-
if self.forward_pattern.Return_H_First
|
|
156
|
-
else (new_encoder_hidden_states, hidden_states)
|
|
157
|
-
)
|
|
158
|
-
)
|
|
159
|
-
|
|
160
|
-
def call_Fn_blocks(
|
|
161
|
-
self,
|
|
162
|
-
hidden_states: torch.Tensor,
|
|
163
|
-
*args,
|
|
164
|
-
**kwargs,
|
|
165
|
-
):
|
|
166
|
-
new_encoder_hidden_states = None
|
|
167
|
-
for block in self._Fn_blocks():
|
|
168
|
-
hidden_states = block(
|
|
169
|
-
hidden_states,
|
|
170
|
-
*args,
|
|
171
|
-
**kwargs,
|
|
172
|
-
)
|
|
173
|
-
if not isinstance(hidden_states, torch.Tensor): # Pattern 4, 5
|
|
174
|
-
hidden_states, new_encoder_hidden_states = hidden_states
|
|
175
|
-
if not self.forward_pattern.Return_H_First:
|
|
176
|
-
hidden_states, new_encoder_hidden_states = (
|
|
177
|
-
new_encoder_hidden_states,
|
|
178
|
-
hidden_states,
|
|
179
|
-
)
|
|
180
|
-
|
|
181
|
-
return hidden_states, new_encoder_hidden_states
|
|
182
|
-
|
|
183
|
-
def call_Mn_blocks(
|
|
184
|
-
self,
|
|
185
|
-
hidden_states: torch.Tensor,
|
|
186
|
-
*args,
|
|
187
|
-
**kwargs,
|
|
188
|
-
):
|
|
189
|
-
original_hidden_states = hidden_states
|
|
190
|
-
new_encoder_hidden_states = None
|
|
191
|
-
for block in self._Mn_blocks():
|
|
192
|
-
hidden_states = block(
|
|
193
|
-
hidden_states,
|
|
194
|
-
*args,
|
|
195
|
-
**kwargs,
|
|
196
|
-
)
|
|
197
|
-
if not isinstance(hidden_states, torch.Tensor): # Pattern 4, 5
|
|
198
|
-
hidden_states, new_encoder_hidden_states = hidden_states
|
|
199
|
-
if not self.forward_pattern.Return_H_First:
|
|
200
|
-
hidden_states, new_encoder_hidden_states = (
|
|
201
|
-
new_encoder_hidden_states,
|
|
202
|
-
hidden_states,
|
|
203
|
-
)
|
|
204
|
-
# compute hidden_states residual
|
|
205
|
-
hidden_states = hidden_states.contiguous()
|
|
206
|
-
hidden_states_residual = hidden_states - original_hidden_states
|
|
207
|
-
|
|
208
|
-
return (
|
|
209
|
-
hidden_states,
|
|
210
|
-
new_encoder_hidden_states,
|
|
211
|
-
hidden_states_residual,
|
|
212
|
-
)
|
|
213
|
-
|
|
214
|
-
def call_Bn_blocks(
|
|
215
|
-
self,
|
|
216
|
-
hidden_states: torch.Tensor,
|
|
217
|
-
*args,
|
|
218
|
-
**kwargs,
|
|
219
|
-
):
|
|
220
|
-
new_encoder_hidden_states = None
|
|
221
|
-
if self.cache_manager.Bn_compute_blocks() == 0:
|
|
222
|
-
return hidden_states, new_encoder_hidden_states
|
|
223
|
-
|
|
224
|
-
for block in self._Bn_blocks():
|
|
225
|
-
hidden_states = block(
|
|
226
|
-
hidden_states,
|
|
227
|
-
*args,
|
|
228
|
-
**kwargs,
|
|
229
|
-
)
|
|
230
|
-
if not isinstance(hidden_states, torch.Tensor): # Pattern 4,5
|
|
231
|
-
hidden_states, new_encoder_hidden_states = hidden_states
|
|
232
|
-
if not self.forward_pattern.Return_H_First:
|
|
233
|
-
hidden_states, new_encoder_hidden_states = (
|
|
234
|
-
new_encoder_hidden_states,
|
|
235
|
-
hidden_states,
|
|
236
|
-
)
|
|
237
|
-
|
|
238
|
-
return hidden_states, new_encoder_hidden_states
|