MCPcopy Index your code
hub / github.com/modelscope/modelscope / make_same_shape

Function make_same_shape

tools/weight_diff.py:54–79  ·  view source on GitHub ↗
(model_raw: Model, model_convert: Model, tokenizer_raw,
                    tokenizer_convert)

Source from the content-addressed store, hash-verified

52
53
54def make_same_shape(model_raw: Model, model_convert: Model, tokenizer_raw,
55 tokenizer_convert):
56 if model_raw.__class__ != model_convert.__class__:
57 logger.error(
58 f'weight diff: These two models should be of the same class. model_raw:'
59 f'{model_raw.__class__} vs model_convert: {model_convert.__class__}.'
60 )
61
62 special_tokens = {}
63 for k, v in tokenizer_convert.special_tokens_map_extended.items():
64 if k not in tokenizer_raw.special_tokens_map_extended:
65 special_tokens[k] = v
66
67 smart_tokenizer_and_embedding_resize(
68 special_tokens_dict=special_tokens,
69 model=model_raw,
70 tokenizer=tokenizer_raw,
71 )
72
73 state_dict_tuned = model_convert.state_dict()
74 state_dict_raw = model_raw.state_dict()
75 for key in tqdm.tqdm(state_dict_tuned):
76 if state_dict_tuned[key].shape != state_dict_raw[key].shape:
77 logger.error(
78 f'weight diff: shape mismatch. {key}, model_raw shape: {state_dict_raw[key].shape}'
79 f' vs model_convert shape: {state_dict_tuned[key].shape}.')
80
81
82def _weight_diff(model_raw,

Callers 1

_weight_diffFunction · 0.85

Calls 3

itemsMethod · 0.45
state_dictMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…