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

Function _weight_diff

tools/weight_diff.py:82–103  ·  view source on GitHub ↗
(model_raw,
                 model_convert,
                 tokenizer_raw,
                 tokenizer_convert,
                 path_to_save=None,
                 make_diff_or_recover='diff')

Source from the content-addressed store, hash-verified

80
81
82def _weight_diff(model_raw,
83 model_convert,
84 tokenizer_raw,
85 tokenizer_convert,
86 path_to_save=None,
87 make_diff_or_recover='diff'):
88 make_same_shape(model_raw, model_convert, tokenizer_raw, tokenizer_convert)
89
90 state_dict_raw = model_raw.state_dict()
91 state_dict_convert = model_convert.state_dict()
92 if make_diff_or_recover == 'diff':
93 for key in tqdm.tqdm(state_dict_convert):
94 state_dict_convert[key].add_(-state_dict_raw[key])
95 elif make_diff_or_recover == 'recover':
96 for key in tqdm.tqdm(state_dict_convert):
97 state_dict_convert[key].add_(state_dict_raw[key])
98
99 if path_to_save:
100 model_convert.save_pretrained(path_to_save, 'pytorch_model.bin')
101 tokenizer_convert.save_pretrained(path_to_save)
102
103 return model_convert, tokenizer_convert
104
105
106@torch.inference_mode()

Callers 1

weight_diffFunction · 0.85

Calls 3

make_same_shapeFunction · 0.85
state_dictMethod · 0.45
save_pretrainedMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…