| 2115 | state_dict._metadata = metadata # type: ignore[attr-defined] |
| 2116 | |
| 2117 | def load(module, local_state_dict, prefix=''): |
| 2118 | local_metadata = {} if metadata is None else metadata.get(prefix[:-1], {}) |
| 2119 | if assign: |
| 2120 | local_metadata['assign_to_params_buffers'] = assign |
| 2121 | module._load_from_state_dict( |
| 2122 | local_state_dict, prefix, local_metadata, True, missing_keys, unexpected_keys, error_msgs) |
| 2123 | for name, child in module._modules.items(): |
| 2124 | if child is not None: |
| 2125 | child_prefix = prefix + name + '.' |
| 2126 | child_state_dict = {k: v for k, v in local_state_dict.items() if k.startswith(child_prefix)} |
| 2127 | load(child, child_state_dict, child_prefix) |
| 2128 | |
| 2129 | # Note that the hook can modify missing_keys and unexpected_keys. |
| 2130 | incompatible_keys = _IncompatibleKeys(missing_keys, unexpected_keys) |
| 2131 | for hook in module._load_state_dict_post_hooks.values(): |
| 2132 | out = hook(module, incompatible_keys) |
| 2133 | assert out is None, ( |
| 2134 | "Hooks registered with ``register_load_state_dict_post_hook`` are not" |
| 2135 | "expected to return new values, if incompatible_keys need to be modified," |
| 2136 | "it should be done inplace." |
| 2137 | ) |
| 2138 | |
| 2139 | load(self, state_dict) |
| 2140 | del load |