|
| 1 | +# Copyright 2025 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 | +import inspect |
| 16 | +from dataclasses import dataclass |
| 17 | +from typing import Any, Callable, Dict, Type |
| 18 | + |
| 19 | + |
| 20 | +@dataclass |
| 21 | +class AttentionProcessorMetadata: |
| 22 | + skip_processor_output_fn: Callable[[Any], Any] |
| 23 | + |
| 24 | + |
| 25 | +@dataclass |
| 26 | +class TransformerBlockMetadata: |
| 27 | + return_hidden_states_index: int = None |
| 28 | + return_encoder_hidden_states_index: int = None |
| 29 | + |
| 30 | + _cls: Type = None |
| 31 | + _cached_parameter_indices: Dict[str, int] = None |
| 32 | + |
| 33 | + def _get_parameter_from_args_kwargs(self, identifier: str, args=(), kwargs=None): |
| 34 | + kwargs = kwargs or {} |
| 35 | + if identifier in kwargs: |
| 36 | + return kwargs[identifier] |
| 37 | + if self._cached_parameter_indices is not None: |
| 38 | + return args[self._cached_parameter_indices[identifier]] |
| 39 | + if self._cls is None: |
| 40 | + raise ValueError("Model class is not set for metadata.") |
| 41 | + parameters = list(inspect.signature(self._cls.forward).parameters.keys()) |
| 42 | + parameters = parameters[1:] # skip `self` |
| 43 | + self._cached_parameter_indices = {param: i for i, param in enumerate(parameters)} |
| 44 | + if identifier not in self._cached_parameter_indices: |
| 45 | + raise ValueError(f"Parameter '{identifier}' not found in function signature but was requested.") |
| 46 | + index = self._cached_parameter_indices[identifier] |
| 47 | + if index >= len(args): |
| 48 | + raise ValueError(f"Expected {index} arguments but got {len(args)}.") |
| 49 | + return args[index] |
| 50 | + |
| 51 | + |
| 52 | +class AttentionProcessorRegistry: |
| 53 | + _registry = {} |
| 54 | + # TODO(aryan): this is only required for the time being because we need to do the registrations |
| 55 | + # for classes. If we do it eagerly, i.e. call the functions in global scope, we will get circular |
| 56 | + # import errors because of the models imported in this file. |
| 57 | + _is_registered = False |
| 58 | + |
| 59 | + @classmethod |
| 60 | + def register(cls, model_class: Type, metadata: AttentionProcessorMetadata): |
| 61 | + cls._register() |
| 62 | + cls._registry[model_class] = metadata |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def get(cls, model_class: Type) -> AttentionProcessorMetadata: |
| 66 | + cls._register() |
| 67 | + if model_class not in cls._registry: |
| 68 | + raise ValueError(f"Model class {model_class} not registered.") |
| 69 | + return cls._registry[model_class] |
| 70 | + |
| 71 | + @classmethod |
| 72 | + def _register(cls): |
| 73 | + if cls._is_registered: |
| 74 | + return |
| 75 | + cls._is_registered = True |
| 76 | + _register_attention_processors_metadata() |
| 77 | + |
| 78 | + |
| 79 | +class TransformerBlockRegistry: |
| 80 | + _registry = {} |
| 81 | + # TODO(aryan): this is only required for the time being because we need to do the registrations |
| 82 | + # for classes. If we do it eagerly, i.e. call the functions in global scope, we will get circular |
| 83 | + # import errors because of the models imported in this file. |
| 84 | + _is_registered = False |
| 85 | + |
| 86 | + @classmethod |
| 87 | + def register(cls, model_class: Type, metadata: TransformerBlockMetadata): |
| 88 | + cls._register() |
| 89 | + metadata._cls = model_class |
| 90 | + cls._registry[model_class] = metadata |
| 91 | + |
| 92 | + @classmethod |
| 93 | + def get(cls, model_class: Type) -> TransformerBlockMetadata: |
| 94 | + cls._register() |
| 95 | + if model_class not in cls._registry: |
| 96 | + raise ValueError(f"Model class {model_class} not registered.") |
| 97 | + return cls._registry[model_class] |
| 98 | + |
| 99 | + @classmethod |
| 100 | + def _register(cls): |
| 101 | + if cls._is_registered: |
| 102 | + return |
| 103 | + cls._is_registered = True |
| 104 | + _register_transformer_blocks_metadata() |
| 105 | + |
| 106 | + |
| 107 | +def _register_attention_processors_metadata(): |
| 108 | + from ..models.attention_processor import AttnProcessor2_0 |
| 109 | + from ..models.transformers.transformer_cogview4 import CogView4AttnProcessor |
| 110 | + from ..models.transformers.transformer_flux import FluxAttnProcessor |
| 111 | + from ..models.transformers.transformer_wan import WanAttnProcessor2_0 |
| 112 | + |
| 113 | + # AttnProcessor2_0 |
| 114 | + AttentionProcessorRegistry.register( |
| 115 | + model_class=AttnProcessor2_0, |
| 116 | + metadata=AttentionProcessorMetadata( |
| 117 | + skip_processor_output_fn=_skip_proc_output_fn_Attention_AttnProcessor2_0, |
| 118 | + ), |
| 119 | + ) |
| 120 | + |
| 121 | + # CogView4AttnProcessor |
| 122 | + AttentionProcessorRegistry.register( |
| 123 | + model_class=CogView4AttnProcessor, |
| 124 | + metadata=AttentionProcessorMetadata( |
| 125 | + skip_processor_output_fn=_skip_proc_output_fn_Attention_CogView4AttnProcessor, |
| 126 | + ), |
| 127 | + ) |
| 128 | + |
| 129 | + # WanAttnProcessor2_0 |
| 130 | + AttentionProcessorRegistry.register( |
| 131 | + model_class=WanAttnProcessor2_0, |
| 132 | + metadata=AttentionProcessorMetadata( |
| 133 | + skip_processor_output_fn=_skip_proc_output_fn_Attention_WanAttnProcessor2_0, |
| 134 | + ), |
| 135 | + ) |
| 136 | + # FluxAttnProcessor |
| 137 | + AttentionProcessorRegistry.register( |
| 138 | + model_class=FluxAttnProcessor, |
| 139 | + metadata=AttentionProcessorMetadata(skip_processor_output_fn=_skip_proc_output_fn_Attention_FluxAttnProcessor), |
| 140 | + ) |
| 141 | + |
| 142 | + |
| 143 | +def _register_transformer_blocks_metadata(): |
| 144 | + from ..models.attention import BasicTransformerBlock |
| 145 | + from ..models.transformers.cogvideox_transformer_3d import CogVideoXBlock |
| 146 | + from ..models.transformers.transformer_cogview4 import CogView4TransformerBlock |
| 147 | + from ..models.transformers.transformer_flux import FluxSingleTransformerBlock, FluxTransformerBlock |
| 148 | + from ..models.transformers.transformer_hunyuan_video import ( |
| 149 | + HunyuanVideoSingleTransformerBlock, |
| 150 | + HunyuanVideoTokenReplaceSingleTransformerBlock, |
| 151 | + HunyuanVideoTokenReplaceTransformerBlock, |
| 152 | + HunyuanVideoTransformerBlock, |
| 153 | + ) |
| 154 | + from ..models.transformers.transformer_ltx import LTXVideoTransformerBlock |
| 155 | + from ..models.transformers.transformer_mochi import MochiTransformerBlock |
| 156 | + from ..models.transformers.transformer_qwenimage import QwenImageTransformerBlock |
| 157 | + from ..models.transformers.transformer_wan import WanTransformerBlock |
| 158 | + |
| 159 | + # BasicTransformerBlock |
| 160 | + TransformerBlockRegistry.register( |
| 161 | + model_class=BasicTransformerBlock, |
| 162 | + metadata=TransformerBlockMetadata( |
| 163 | + return_hidden_states_index=0, |
| 164 | + return_encoder_hidden_states_index=None, |
| 165 | + ), |
| 166 | + ) |
| 167 | + |
| 168 | + # CogVideoX |
| 169 | + TransformerBlockRegistry.register( |
| 170 | + model_class=CogVideoXBlock, |
| 171 | + metadata=TransformerBlockMetadata( |
| 172 | + return_hidden_states_index=0, |
| 173 | + return_encoder_hidden_states_index=1, |
| 174 | + ), |
| 175 | + ) |
| 176 | + |
| 177 | + # CogView4 |
| 178 | + TransformerBlockRegistry.register( |
| 179 | + model_class=CogView4TransformerBlock, |
| 180 | + metadata=TransformerBlockMetadata( |
| 181 | + return_hidden_states_index=0, |
| 182 | + return_encoder_hidden_states_index=1, |
| 183 | + ), |
| 184 | + ) |
| 185 | + |
| 186 | + # Flux |
| 187 | + TransformerBlockRegistry.register( |
| 188 | + model_class=FluxTransformerBlock, |
| 189 | + metadata=TransformerBlockMetadata( |
| 190 | + return_hidden_states_index=1, |
| 191 | + return_encoder_hidden_states_index=0, |
| 192 | + ), |
| 193 | + ) |
| 194 | + TransformerBlockRegistry.register( |
| 195 | + model_class=FluxSingleTransformerBlock, |
| 196 | + metadata=TransformerBlockMetadata( |
| 197 | + return_hidden_states_index=1, |
| 198 | + return_encoder_hidden_states_index=0, |
| 199 | + ), |
| 200 | + ) |
| 201 | + |
| 202 | + # HunyuanVideo |
| 203 | + TransformerBlockRegistry.register( |
| 204 | + model_class=HunyuanVideoTransformerBlock, |
| 205 | + metadata=TransformerBlockMetadata( |
| 206 | + return_hidden_states_index=0, |
| 207 | + return_encoder_hidden_states_index=1, |
| 208 | + ), |
| 209 | + ) |
| 210 | + TransformerBlockRegistry.register( |
| 211 | + model_class=HunyuanVideoSingleTransformerBlock, |
| 212 | + metadata=TransformerBlockMetadata( |
| 213 | + return_hidden_states_index=0, |
| 214 | + return_encoder_hidden_states_index=1, |
| 215 | + ), |
| 216 | + ) |
| 217 | + TransformerBlockRegistry.register( |
| 218 | + model_class=HunyuanVideoTokenReplaceTransformerBlock, |
| 219 | + metadata=TransformerBlockMetadata( |
| 220 | + return_hidden_states_index=0, |
| 221 | + return_encoder_hidden_states_index=1, |
| 222 | + ), |
| 223 | + ) |
| 224 | + TransformerBlockRegistry.register( |
| 225 | + model_class=HunyuanVideoTokenReplaceSingleTransformerBlock, |
| 226 | + metadata=TransformerBlockMetadata( |
| 227 | + return_hidden_states_index=0, |
| 228 | + return_encoder_hidden_states_index=1, |
| 229 | + ), |
| 230 | + ) |
| 231 | + |
| 232 | + # LTXVideo |
| 233 | + TransformerBlockRegistry.register( |
| 234 | + model_class=LTXVideoTransformerBlock, |
| 235 | + metadata=TransformerBlockMetadata( |
| 236 | + return_hidden_states_index=0, |
| 237 | + return_encoder_hidden_states_index=None, |
| 238 | + ), |
| 239 | + ) |
| 240 | + |
| 241 | + # Mochi |
| 242 | + TransformerBlockRegistry.register( |
| 243 | + model_class=MochiTransformerBlock, |
| 244 | + metadata=TransformerBlockMetadata( |
| 245 | + return_hidden_states_index=0, |
| 246 | + return_encoder_hidden_states_index=1, |
| 247 | + ), |
| 248 | + ) |
| 249 | + |
| 250 | + # Wan |
| 251 | + TransformerBlockRegistry.register( |
| 252 | + model_class=WanTransformerBlock, |
| 253 | + metadata=TransformerBlockMetadata( |
| 254 | + return_hidden_states_index=0, |
| 255 | + return_encoder_hidden_states_index=None, |
| 256 | + ), |
| 257 | + ) |
| 258 | + |
| 259 | + # QwenImage |
| 260 | + TransformerBlockRegistry.register( |
| 261 | + model_class=QwenImageTransformerBlock, |
| 262 | + metadata=TransformerBlockMetadata( |
| 263 | + return_hidden_states_index=1, |
| 264 | + return_encoder_hidden_states_index=0, |
| 265 | + ), |
| 266 | + ) |
| 267 | + |
| 268 | + |
| 269 | +# fmt: off |
| 270 | +def _skip_attention___ret___hidden_states(self, *args, **kwargs): |
| 271 | + hidden_states = kwargs.get("hidden_states", None) |
| 272 | + if hidden_states is None and len(args) > 0: |
| 273 | + hidden_states = args[0] |
| 274 | + return hidden_states |
| 275 | + |
| 276 | + |
| 277 | +def _skip_attention___ret___hidden_states___encoder_hidden_states(self, *args, **kwargs): |
| 278 | + hidden_states = kwargs.get("hidden_states", None) |
| 279 | + encoder_hidden_states = kwargs.get("encoder_hidden_states", None) |
| 280 | + if hidden_states is None and len(args) > 0: |
| 281 | + hidden_states = args[0] |
| 282 | + if encoder_hidden_states is None and len(args) > 1: |
| 283 | + encoder_hidden_states = args[1] |
| 284 | + return hidden_states, encoder_hidden_states |
| 285 | + |
| 286 | + |
| 287 | +_skip_proc_output_fn_Attention_AttnProcessor2_0 = _skip_attention___ret___hidden_states |
| 288 | +_skip_proc_output_fn_Attention_CogView4AttnProcessor = _skip_attention___ret___hidden_states___encoder_hidden_states |
| 289 | +_skip_proc_output_fn_Attention_WanAttnProcessor2_0 = _skip_attention___ret___hidden_states |
| 290 | +# not sure what this is yet. |
| 291 | +_skip_proc_output_fn_Attention_FluxAttnProcessor = _skip_attention___ret___hidden_states |
| 292 | +# fmt: on |
0 commit comments