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

Function _find_mismatched_keys

modelscope/utils/checkpoint.py:194–222  ·  view source on GitHub ↗
(
        state_dicts,
        model_state_dict,
        loaded_keys,
        prefix,
        add_prefix_to_model,
        remove_prefix_from_model,
        ignore_mismatched_sizes,
    )

Source from the content-addressed store, hash-verified

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
217 if checkpoint_shape != model_shape:
218 mismatched_key.append(
219 (checkpoint_key, state_dicts[checkpoint_key].shape,
220 model_state_dict[model_key].shape))
221 del state_dicts[checkpoint_key]
222 return mismatched_key
223
224 def _load_state_dict_into_model(
225 model,

Callers 2

_load_checkpointFunction · 0.85
_load_checkpointMethod · 0.85

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…