loader
¶
Modules¶
fastvideo.models.loader.benchmarks
¶
Modules¶
fastvideo.models.loader.benchmarks.benchmark_weight_loading
¶
Benchmark for model weight loading speed.
Measures the time to load model weights from safetensors files using different strategies (CPU vs GPU, broadcast vs independent).
Usage (single GPU): python fastvideo/models/loader/benchmarks/benchmark_weight_loading.py --model-path /path/to/model
Usage (multi-GPU, e.g. 4 GPUs): torchrun --nproc_per_node=4 fastvideo/models/loader/benchmarks/benchmark_weight_loading.py --model-path /path/to/model
Functions:¶
fastvideo.models.loader.benchmarks.benchmark_weight_loading.benchmark_loading
¶benchmark_loading(files: list[str], to_cpu: bool, broadcast: bool, warmup: int, repeats: int, label: str) -> None
Run the weight loading benchmark and print results.
Source code in fastvideo/models/loader/benchmarks/benchmark_weight_loading.py
fastvideo.models.loader.benchmarks.benchmark_weight_loading_comparison
¶
A/B benchmark: independent-read vs rank-0-broadcast weight loading.
Compares two strategies
- "before" (independent): every rank reads safetensors from disk to GPU
- "after" (broadcast): rank 0 reads from disk, broadcasts to other ranks
Usage
1 GPU¶
torchrun --nproc_per_node=1 fastvideo/models/loader/benchmarks/benchmark_weight_loading_comparison.py --model-path /path/to/model --subfolder transformer
2 GPUs¶
torchrun --nproc_per_node=2 fastvideo/models/loader/benchmarks/benchmark_weight_loading_comparison.py --model-path /path/to/model --subfolder transformer
4 GPUs¶
torchrun --nproc_per_node=4 fastvideo/models/loader/benchmarks/benchmark_weight_loading_comparison.py --model-path /path/to/model --subfolder transformer
Functions:¶
fastvideo.models.loader.benchmarks.benchmark_weight_loading_comparison.load_broadcast
¶After-PR behavior: rank 0 reads from disk, broadcasts to other ranks.
Source code in fastvideo/models/loader/benchmarks/benchmark_weight_loading_comparison.py
fastvideo.models.loader.benchmarks.benchmark_weight_loading_comparison.load_independent
¶Before-PR behavior: every rank reads every tensor from disk to GPU.
Source code in fastvideo/models/loader/benchmarks/benchmark_weight_loading_comparison.py
fastvideo.models.loader.component_loader
¶
Classes¶
fastvideo.models.loader.component_loader.AudioDecoderLoader
¶
Bases: ComponentLoader
Loader for LTX-2 audio decoder (audio_vae component).
Source code in fastvideo/models/loader/component_loader.py
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
Methods:¶
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.ConditionerLoader
¶
Bases: ComponentLoader
Loader for multi-conditioner components (e.g. Stable Audio's
StableAudioMultiConditioner, which bundles T5 + NumberConditioners
and is neither a pure text encoder nor a Diffusers-shaped module).
Reads <subfolder>/config.json to resolve the class via
ModelRegistry, instantiates with no args (the class pulls its own
defaults from its FastVideo config), then loads
diffusion_pytorch_model.safetensors non-strictly so externally
fetched sub-encoders (T5) don't trip the missing-key check.
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
Methods:¶
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
Methods:¶
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
Methods:¶
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.
Methods:¶
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
Methods:¶
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.
Methods:¶
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
252 253 254 255 256 257 258 259 260 261 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 | |
fastvideo.models.loader.component_loader.TokenizerLoader
¶
Bases: ComponentLoader
Loader for tokenizers.
Source code in fastvideo/models/loader/component_loader.py
Methods:¶
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
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 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 | |
fastvideo.models.loader.component_loader.TransformerLoader
¶
Bases: ComponentLoader
Loader for transformer.
Source code in fastvideo/models/loader/component_loader.py
Methods:¶
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
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 | |
fastvideo.models.loader.component_loader.UpsamplerLoader
¶
Bases: ComponentLoader
Loader for upsamplers (incl. LTX-2 spatial/temporal upsamplers).
Source code in fastvideo/models/loader/component_loader.py
Methods:¶
fastvideo.models.loader.component_loader.UpsamplerLoader.load
¶load(model_path: str, fastvideo_args: FastVideoArgs)
Load the upsampler based on the model path, and inference args.
Source code in fastvideo/models/loader/component_loader.py
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 | |
fastvideo.models.loader.component_loader.VAELoader
¶
Bases: ComponentLoader
Loader for VAE.
Source code in fastvideo/models/loader/component_loader.py
Methods:¶
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
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 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 | |
fastvideo.models.loader.component_loader.VocoderLoader
¶
Bases: ComponentLoader
Loader for LTX-2 vocoder.
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
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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | |
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
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 167 168 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 | |
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
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 259 260 261 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 | |
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 = False, broadcast: bool = True) -> Generator[tuple[str, Tensor], None, None]
Iterate over the weights in the model bin/pt files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hf_weights_files
|
list[str]
|
List of bin/pt files to load. |
required |
to_cpu
|
bool
|
Whether to load the weights to CPU. |
False
|
broadcast
|
bool
|
Accepted for API symmetry. PT weights are loaded through torch.load and do not use the safetensors broadcast path. |
True
|
Source code in fastvideo/models/loader/weight_utils.py
fastvideo.models.loader.weight_utils.resolve_safetensors_files
¶
Discover safetensors files in a model directory.
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 = False, broadcast: bool = True, async_broadcast: bool = False) -> Generator[tuple[str, Tensor], None, None]
Iterate over the weights in the model safetensor files. Args: hf_weights_files: List of safetensor files to load. to_cpu: Whether to load the weights to CPU. If False, will load to the GPU device bound to the current process. broadcast: Whether local rank 0 should read GPU weights and broadcast them to the other local ranks. async_broadcast: Whether to overlap loading from disk and broadcasting to other ranks. If True, must iterate over all the weights before use. Only used when broadcast is True and to_cpu is False.