fsdp_load ¶
Classes¶
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, dense_lora_patch: DenseLoRAPatch | None = None) -> _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 dense_lora_patch (Optional[DenseLoRAPatch]): whole-parameter adapter payload, folded into each full tensor before it is sharded so that FSDP/TP placement is inherited from this function rather than reimplemented downstream 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
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 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 | |
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, inference_regional_compile: bool = False, inference_vsa_tile_size: int | None = None, lora_path: str | None = None, lora_strength: float = 1.0) -> Module
Load the model with FSDP if is training, else load the model without FSDP.
lora_path is consulted only for the part of an adapter that addresses whole parameters (.diff / .set_weight); see :mod:fastvideo.models.loader.lora_patch. The low-rank half is merged later by LoRAPipeline. Passing it here is what lets an adapter contribute a parameter the base checkpoint does not contain, which has to happen while the tensor is still unsharded.
Source code in fastvideo/models/loader/fsdp_load.py
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 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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |
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
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 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 | |