loader
¶
Modules¶
fastvideo.models.loader.component_loader
¶
Classes¶
fastvideo.models.loader.component_loader.ComponentLoader
¶
Bases: ABC
Base class for loading a specific type of model component.
Source code in fastvideo/models/loader/component_loader.py
Functions¶
fastvideo.models.loader.component_loader.ComponentLoader.for_module_type
classmethod
¶for_module_type(module_type: str, transformers_or_diffusers: str) -> ComponentLoader
Factory method to create a component loader for a specific module type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module_type
|
str
|
Type of module (e.g., "vae", "text_encoder", "transformer", "scheduler") |
required |
transformers_or_diffusers
|
str
|
Whether the module is from transformers or diffusers |
required |
Returns:
| Type | Description |
|---|---|
ComponentLoader
|
A component loader for the specified module type |
Source code in fastvideo/models/loader/component_loader.py
fastvideo.models.loader.component_loader.ComponentLoader.load
abstractmethod
¶load(model_path: str, fastvideo_args: FastVideoArgs)
Load the component based on the model path, architecture, and inference args.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
str
|
Path to the component model |
required |
fastvideo_args
|
FastVideoArgs
|
FastVideoArgs |
required |
Returns:
| Type | Description |
|---|---|
|
The loaded component |
Source code in fastvideo/models/loader/component_loader.py
fastvideo.models.loader.component_loader.GenericComponentLoader
¶
Bases: ComponentLoader
Generic loader for components that don't have a specific loader.
Source code in fastvideo/models/loader/component_loader.py
Functions¶
fastvideo.models.loader.component_loader.GenericComponentLoader.load
¶load(model_path: str, fastvideo_args: FastVideoArgs)
Load a generic component based on the model path, and inference args.
Source code in fastvideo/models/loader/component_loader.py
fastvideo.models.loader.component_loader.ImageEncoderLoader
¶
Bases: TextEncoderLoader
Source code in fastvideo/models/loader/component_loader.py
Functions¶
fastvideo.models.loader.component_loader.ImageEncoderLoader.load
¶load(model_path: str, fastvideo_args: FastVideoArgs)
Load the text encoders based on the model path, and inference args.
Source code in fastvideo/models/loader/component_loader.py
fastvideo.models.loader.component_loader.ImageProcessorLoader
¶
Bases: ComponentLoader
Loader for image processor.
Source code in fastvideo/models/loader/component_loader.py
Functions¶
fastvideo.models.loader.component_loader.ImageProcessorLoader.load
¶load(model_path: str, fastvideo_args: FastVideoArgs)
Load the image processor based on the model path, and inference args.
Source code in fastvideo/models/loader/component_loader.py
fastvideo.models.loader.component_loader.PipelineComponentLoader
¶
Utility class for loading pipeline components. This replaces the chain of if-else statements in load_pipeline_module.
Functions¶
fastvideo.models.loader.component_loader.PipelineComponentLoader.load_module
staticmethod
¶load_module(module_name: str, component_model_path: str, transformers_or_diffusers: str, fastvideo_args: FastVideoArgs)
Load a pipeline module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module_name
|
str
|
Name of the module (e.g., "vae", "text_encoder", "transformer", "scheduler") |
required |
component_model_path
|
str
|
Path to the component model |
required |
transformers_or_diffusers
|
str
|
Whether the module is from transformers or diffusers |
required |
pipeline_args
|
Inference arguments |
required |
Returns:
| Type | Description |
|---|---|
|
The loaded module |
Source code in fastvideo/models/loader/component_loader.py
fastvideo.models.loader.component_loader.SchedulerLoader
¶
Bases: ComponentLoader
Loader for scheduler.
Source code in fastvideo/models/loader/component_loader.py
Functions¶
fastvideo.models.loader.component_loader.SchedulerLoader.load
¶load(model_path: str, fastvideo_args: FastVideoArgs)
Load the scheduler based on the model path, and inference args.
Source code in fastvideo/models/loader/component_loader.py
fastvideo.models.loader.component_loader.TextEncoderLoader
¶
Bases: ComponentLoader
Loader for text encoders.
Source code in fastvideo/models/loader/component_loader.py
Classes¶
fastvideo.models.loader.component_loader.TextEncoderLoader.Source
dataclass
¶Source(model_or_path: str, prefix: str = '', fall_back_to_pt: bool = True, allow_patterns_overrides: list[str] | None = None)
A source for weights.
fastvideo.models.loader.component_loader.TextEncoderLoader.Source.allow_patterns_overrides
class-attribute
instance-attribute
¶If defined, weights will load exclusively using these patterns.
fastvideo.models.loader.component_loader.TextEncoderLoader.Source.fall_back_to_pt
class-attribute
instance-attribute
¶fall_back_to_pt: bool = True
Whether .pt weights can be used.
Functions¶
fastvideo.models.loader.component_loader.TextEncoderLoader.load
¶load(model_path: str, fastvideo_args: FastVideoArgs)
Load the text encoders based on the model path, and inference args.
Source code in fastvideo/models/loader/component_loader.py
fastvideo.models.loader.component_loader.TokenizerLoader
¶
Bases: ComponentLoader
Loader for tokenizers.
Source code in fastvideo/models/loader/component_loader.py
Functions¶
fastvideo.models.loader.component_loader.TokenizerLoader.load
¶load(model_path: str, fastvideo_args: FastVideoArgs)
Load the tokenizer based on the model path, and inference args.
Source code in fastvideo/models/loader/component_loader.py
fastvideo.models.loader.component_loader.TransformerLoader
¶
Bases: ComponentLoader
Loader for transformer.
Source code in fastvideo/models/loader/component_loader.py
Functions¶
fastvideo.models.loader.component_loader.TransformerLoader.load
¶load(model_path: str, fastvideo_args: FastVideoArgs)
Load the transformer based on the model path, and inference args.
Source code in fastvideo/models/loader/component_loader.py
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 | |
fastvideo.models.loader.component_loader.VAELoader
¶
Bases: ComponentLoader
Loader for VAE.
Source code in fastvideo/models/loader/component_loader.py
Functions¶
fastvideo.models.loader.component_loader.VAELoader.load
¶load(model_path: str, fastvideo_args: FastVideoArgs)
Load the VAE based on the model path, and inference args.
Source code in fastvideo/models/loader/component_loader.py
Functions¶
fastvideo.models.loader.fsdp_load
¶
Functions¶
fastvideo.models.loader.fsdp_load.load_model_from_full_model_state_dict
¶
load_model_from_full_model_state_dict(model: FSDPModule | Module, full_sd_iterator: Generator[tuple[str, Tensor], None, None], device: device, param_dtype: dtype, strict: bool = False, cpu_offload: bool = False, param_names_mapping: Callable[[str], tuple[str, Any, Any]] | None = None, training_mode: bool = True) -> _IncompatibleKeys
Converting full state dict into a sharded state dict
and loading it into FSDP model (if training) or normal huggingface model
Args:
model (Union[FSDPModule, torch.nn.Module]): Model to generate fully qualified names for cpu_state_dict
full_sd_iterator (Generator): an iterator yielding (param_name, tensor) pairs
device (torch.device): device used to move full state dict tensors
param_dtype (torch.dtype): dtype used to move full state dict tensors
strict (bool): flag to check if to load the model in strict mode
cpu_offload (bool): flag to check if FSDP offload is enabled
param_names_mapping (Optional[Callable[[str], str]]): a function that maps full param name to sharded param name
training_mode (bool): apply FSDP only for training
Returns:
NamedTuple with missing_keys and unexpected_keys fields:
* missing_keys is a list of str containing the missing keys
* unexpected_keys is a list of str containing the unexpected keys
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If got FSDP with more than 1D. |
Source code in fastvideo/models/loader/fsdp_load.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | |
fastvideo.models.loader.fsdp_load.maybe_load_fsdp_model
¶
maybe_load_fsdp_model(model_cls: type[Module], init_params: dict[str, Any], weight_dir_list: list[str], device: device, hsdp_replicate_dim: int, hsdp_shard_dim: int, default_dtype: dtype, param_dtype: dtype, reduce_dtype: dtype, strict: bool = True, cpu_offload: bool = False, fsdp_inference: bool = False, output_dtype: dtype | None = None, training_mode: bool = True, pin_cpu_memory: bool = True, enable_torch_compile: bool = False, torch_compile_kwargs: dict[str, Any] | None = None) -> Module
Load the model with FSDP if is training, else load the model without FSDP.
Source code in fastvideo/models/loader/fsdp_load.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
fastvideo.models.loader.fsdp_load.set_default_dtype
¶
set_default_dtype(dtype: dtype) -> Generator[None, None, None]
Context manager to set torch's default dtype.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dtype
|
dtype
|
The desired default dtype inside the context manager. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ContextManager |
None
|
context manager for setting default dtype. |
Example
with set_default_dtype(torch.bfloat16): x = torch.tensor([1, 2, 3]) x.dtype torch.bfloat16
Source code in fastvideo/models/loader/fsdp_load.py
fastvideo.models.loader.fsdp_load.shard_model
¶
shard_model(model, *, cpu_offload: bool, reshard_after_forward: bool = True, mp_policy: MixedPrecisionPolicy | None = MixedPrecisionPolicy(), mesh: DeviceMesh | None = None, fsdp_shard_conditions: list[Callable[[str, Module], bool]] = [], pin_cpu_memory: bool = True) -> None
Utility to shard a model with FSDP using the PyTorch Distributed fully_shard API.
This method will over the model's named modules from the bottom-up and apply shard modules based on whether they meet any of the criteria from shard_conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
TransformerDecoder
|
Model to shard with FSDP. |
required |
shard_conditions
|
List[Callable[[str, Module], bool]]
|
A list of functions to determine which modules to shard with FSDP. Each function should take module name (relative to root) and the module itself, returning True if FSDP should shard the module and False otherwise. If any of shard_conditions return True for a given module, it will be sharded by FSDP. |
required |
cpu_offload
|
bool
|
If set to True, FSDP will offload parameters, gradients, and optimizer states to CPU. |
required |
reshard_after_forward
|
bool
|
Whether to reshard parameters and buffers after the forward pass. Setting this to True corresponds to the FULL_SHARD sharding strategy from FSDP1, while setting it to False corresponds to the SHARD_GRAD_OP sharding strategy. |
True
|
mesh
|
Optional[DeviceMesh]
|
Device mesh to use for FSDP sharding under multiple parallelism. Default to None. |
None
|
fsdp_shard_conditions
|
List[Callable[[str, Module], bool]]
|
A list of functions to determine which modules to shard with FSDP. |
[]
|
pin_cpu_memory
|
bool
|
If set to True, FSDP will pin the CPU memory of the offloaded parameters. |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If no layer modules were sharded, indicating that no shard_condition was triggered. |
Source code in fastvideo/models/loader/fsdp_load.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
fastvideo.models.loader.utils
¶
Utilities for selecting and loading models.
Functions¶
fastvideo.models.loader.utils.get_param_names_mapping
¶
Creates a mapping function that transforms parameter names using regex patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping_dict
|
Dict[str, str]
|
Dictionary mapping regex patterns to replacement patterns |
required |
param_name
|
str
|
The parameter name to be transformed |
required |
Returns:
| Type | Description |
|---|---|
Callable[[str], tuple[str, Any, Any]]
|
Callable[[str], str]: A function that maps parameter names from source to target format |
Source code in fastvideo/models/loader/utils.py
fastvideo.models.loader.utils.hf_to_custom_state_dict
¶
hf_to_custom_state_dict(hf_param_sd: dict[str, Tensor] | Iterator[tuple[str, Tensor]], param_names_mapping: Callable[[str], tuple[str, Any, Any]]) -> tuple[dict[str, Tensor], dict[str, tuple[str, Any, Any]]]
Converts a Hugging Face parameter state dictionary to a custom parameter state dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hf_param_sd
|
Dict[str, Tensor]
|
The Hugging Face parameter state dictionary |
required |
param_names_mapping
|
Callable[[str], tuple[str, Any, Any]]
|
A function that maps parameter names from source to target format |
required |
Returns:
| Name | Type | Description |
|---|---|---|
custom_param_sd |
Dict[str, Tensor]
|
The custom formatted parameter state dict |
reverse_param_names_mapping |
Dict[str, Tuple[str, Any, Any]]
|
Maps back from custom to hf |
Source code in fastvideo/models/loader/utils.py
fastvideo.models.loader.utils.set_default_torch_dtype
¶
Sets the default torch dtype to the given dtype.
Source code in fastvideo/models/loader/utils.py
fastvideo.models.loader.weight_utils
¶
Utilities for downloading and initializing model weights.
Functions¶
fastvideo.models.loader.weight_utils.default_weight_loader
¶
Default weight loader.
Source code in fastvideo/models/loader/weight_utils.py
fastvideo.models.loader.weight_utils.enable_hf_transfer
¶
automatically activates hf_transfer
Source code in fastvideo/models/loader/weight_utils.py
fastvideo.models.loader.weight_utils.filter_files_not_needed_for_inference
¶
Exclude files that are not needed for inference.
See https://github.com/huggingface/transformers/blob/v4.34.0/src/transformers/trainer.py#L227-L233
Source code in fastvideo/models/loader/weight_utils.py
fastvideo.models.loader.weight_utils.maybe_remap_kv_scale_name
¶
Remap the name of FP8 k/v_scale parameters.
This function handles the remapping of FP8 k/v_scale parameter names. It detects if the given name ends with a suffix and attempts to remap it to the expected name format in the model. If the remapped name is not found in the params_dict, a warning is printed and None is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The original loaded checkpoint parameter name. |
required |
params_dict
|
dict
|
Dictionary containing the model's named parameters. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str | None
|
The remapped parameter name if successful, or the original name if no remapping is needed. |
None |
str | None
|
If the remapped name is not found in params_dict. |
Source code in fastvideo/models/loader/weight_utils.py
fastvideo.models.loader.weight_utils.pt_weights_iterator
¶
pt_weights_iterator(hf_weights_files: list[str], to_cpu: bool = True) -> Generator[tuple[str, Tensor], None, None]
Iterate over the weights in the model bin/pt files.
Source code in fastvideo/models/loader/weight_utils.py
fastvideo.models.loader.weight_utils.safetensors_weights_iterator
¶
safetensors_weights_iterator(hf_weights_files: list[str], to_cpu: bool = True) -> Generator[tuple[str, Tensor], None, None]
Iterate over the weights in the model safetensor files.