MCPcopy Create free account
hub / github.com/modelscope/modelscope / load_task_model_checkpoint

Function load_task_model_checkpoint

modelscope/utils/checkpoint.py:159–571  ·  view source on GitHub ↗

Load model checkpoint file and feed the parameters into the model. Args: model_to_load: The model to be load model_local_dir: The actual checkpoint dir on local disk. default_dtype: Set the default float type by 'torch.set_default_dtype' load_state_fn: An opt

(model_to_load,
                               model_local_dir,
                               default_dtype=None,
                               load_state_fn=None,
                               **kwargs)

Source from the content-addressed store, hash-verified

157
158
159def load_task_model_checkpoint(model_to_load,
160 model_local_dir,
161 default_dtype=None,
162 load_state_fn=None,
163 **kwargs):
164 """
165 Load model checkpoint file and feed the parameters into the model.
166 Args:
167 model_to_load: The model to be load
168 model_local_dir: The actual checkpoint dir on local disk.
169 default_dtype: Set the default float type by 'torch.set_default_dtype'
170 load_state_fn: An optional load_state_fn used to load state_dict into the model.
171
172 Returns:
173
174 """
175
176 def _add_head_prefix_to_state_dict(state_dicts, head_prefix,
177 expected_keys_without_head_prefix,
178 missing_keys):
179 new_state_dict = OrderedDict()
180 for name, module in state_dicts.items():
181 if name in expected_keys_without_head_prefix:
182 name_with_head = '.'.join([head_prefix, name])
183 new_state_dict[name_with_head] = module
184 expected_keys_without_head_prefix.remove(name)
185 missing_keys = list(set(missing_keys) - set([name_with_head]))
186 else:
187 new_state_dict[name] = module
188
189 missing_head_keys = []
190 if len(expected_keys_without_head_prefix) > 0:
191 missing_head_keys = expected_keys_without_head_prefix.copy()
192 return new_state_dict, missing_head_keys, missing_keys
193
194 def _find_mismatched_keys(
195 state_dicts,
196 model_state_dict,
197 loaded_keys,
198 prefix,
199 add_prefix_to_model,
200 remove_prefix_from_model,
201 ignore_mismatched_sizes,
202 ):
203 mismatched_key = []
204 if ignore_mismatched_sizes:
205 for checkpoint_key in loaded_keys:
206 model_key = checkpoint_key
207 if remove_prefix_from_model:
208 # The model key starts with `prefix` but `checkpoint_key` doesn't, so we add it.
209 model_key = f'{prefix}.{checkpoint_key}'
210 elif add_prefix_to_model:
211 # The model key doesn't start with `prefix` but `checkpoint_key` does, so we remove it.
212 model_key = '.'.join(checkpoint_key.split('.')[1:])
213
214 if model_key in model_state_dict:
215 model_shape = model_state_dict[model_key].shape
216 checkpoint_shape = state_dicts[checkpoint_key].shape

Callers 1

_instantiateMethod · 0.90

Calls 4

tie_weightsFunction · 0.85
_load_checkpointFunction · 0.70
loadMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…