| 734 | # PyTorch's `_load_from_state_dict` does not copy parameters in a module's descendants |
| 735 | # so we need to apply the function recursively. |
| 736 | def load(module: nn.Module, prefix=""): |
| 737 | local_metadata = {} if metadata is None else metadata.get(prefix[:-1], {}) |
| 738 | module._load_from_state_dict( |
| 739 | state_dict, prefix, local_metadata, True, missing_keys, unexpected_keys, error_msgs, |
| 740 | ) |
| 741 | for name, child in module._modules.items(): |
| 742 | if child is not None: |
| 743 | load(child, prefix + name + ".") |
| 744 | |
| 745 | # Make sure we are able to load base models as well as derived models (with heads) |
| 746 | start_prefix = "" |