| 724 | # PyTorch's `_load_from_state_dict` does not copy parameters in a module's descendants |
| 725 | # so we need to apply the function recursively. |
| 726 | def load(module: nn.Module, prefix=""): |
| 727 | local_metadata = {} if metadata is None else metadata.get(prefix[:-1], {}) |
| 728 | module._load_from_state_dict( |
| 729 | state_dict, prefix, local_metadata, True, missing_keys, unexpected_keys, error_msgs, |
| 730 | ) |
| 731 | for name, child in module._modules.items(): |
| 732 | if child is not None: |
| 733 | load(child, prefix + name + ".") |
| 734 | |
| 735 | # Make sure we are able to load base models as well as derived models (with heads) |
| 736 | start_prefix = "" |