r"""Copy parameters and buffers from :attr:`state_dict` into this module and its descendants. If :attr:`strict` is ``True``, then the keys of :attr:`state_dict` must exactly match the keys returned by this module's :meth:`~torch.nn.Module.state_dict` function. .. warning::
(self, state_dict: Mapping[str, Any],
strict: bool = True, assign: bool = False)
| 560 | unexpected_keys.append(key) |
| 561 | |
| 562 | def zero3_load_state_dict(self, state_dict: Mapping[str, Any], |
| 563 | strict: bool = True, assign: bool = False): |
| 564 | r"""Copy parameters and buffers from :attr:`state_dict` into this module and its descendants. |
| 565 | |
| 566 | If :attr:`strict` is ``True``, then |
| 567 | the keys of :attr:`state_dict` must exactly match the keys returned |
| 568 | by this module's :meth:`~torch.nn.Module.state_dict` function. |
| 569 | |
| 570 | .. warning:: |
| 571 | If :attr:`assign` is ``True`` the optimizer must be created after |
| 572 | the call to :attr:`load_state_dict`. |
| 573 | |
| 574 | Args: |
| 575 | state_dict (dict): a dict containing parameters and |
| 576 | persistent buffers. |
| 577 | strict (bool, optional): whether to strictly enforce that the keys |
| 578 | in :attr:`state_dict` match the keys returned by this module's |
| 579 | :meth:`~torch.nn.Module.state_dict` function. Default: ``True`` |
| 580 | assign (bool, optional): whether to assign items in the state |
| 581 | dictionary to their corresponding keys in the module instead |
| 582 | of copying them inplace into the module's current parameters and buffers. |
| 583 | When ``False``, the properties of the tensors in the current |
| 584 | module are preserved while when ``True``, the properties of the |
| 585 | Tensors in the state dict are preserved. |
| 586 | Default: ``False`` |
| 587 | |
| 588 | Returns: |
| 589 | ``NamedTuple`` with ``missing_keys`` and ``unexpected_keys`` fields: |
| 590 | * **missing_keys** is a list of str containing the missing keys |
| 591 | * **unexpected_keys** is a list of str containing the unexpected keys |
| 592 | |
| 593 | Note: |
| 594 | If a parameter or buffer is registered as ``None`` and its corresponding key |
| 595 | exists in :attr:`state_dict`, :meth:`load_state_dict` will raise a |
| 596 | ``RuntimeError``. |
| 597 | """ |
| 598 | if not isinstance(state_dict, Mapping): |
| 599 | raise TypeError(f"Expected state_dict to be dict-like, got {type(state_dict)}.") |
| 600 | |
| 601 | missing_keys: List[str] = [] |
| 602 | unexpected_keys: List[str] = [] |
| 603 | error_msgs: List[str] = [] |
| 604 | |
| 605 | # copy state_dict so _load_from_state_dict can modify it |
| 606 | metadata = getattr(state_dict, '_metadata', None) |
| 607 | state_dict = OrderedDict(state_dict) |
| 608 | if metadata is not None: |
| 609 | # mypy isn't aware that "_metadata" exists in state_dict |
| 610 | state_dict._metadata = metadata # type: ignore[attr-defined] |
| 611 | |
| 612 | def load(module, local_state_dict, prefix=''): |
| 613 | local_metadata = {} if metadata is None else metadata.get(prefix[:-1], {}) |
| 614 | if assign: |
| 615 | local_metadata['assign_to_params_buffers'] = assign |
| 616 | _zero3_load_from_state_dict(module, |
| 617 | local_state_dict, prefix, local_metadata, True, missing_keys, unexpected_keys, error_msgs) |
| 618 | for name, child in module._modules.items(): |
| 619 | if child is not None: |
nothing calls this directly
no test coverage detected