[core] Large/full refactor of from_pretrained (#36033)
* squash everything together start to simplify inner logic Update modeling_utils.py Update modeling_utils.py Update modeling_utils.py Update modeling_utils.py continue refactor fix small fixes add type hints/docstring Update modeling_utils.py remove _fast_init keep improving Update modeling_utils.py Update modeling_utils.py new first tp loading version style fix weird in-place op trigger CIs Update modeling_utils.py much clearer renaming of keys fix update Update test_modeling_common.py trigger CIs update update style Update modeling_utils.py Update modeling_utils.py Update modeling_utils.py fix fast download first prototype remove old function remove old functions Remove unused function and move back _get_tp_registry fix tp plan registry simplify CIs Update hub.py Update modeling_utils.py simplify simplify renaming logic remove unused check add sanity check back (a test depends on it) Update modeling_utils.py finalize sound renaming logic style add forgotten check Update modeling_utils.py add key_mapping keyword style Update modeling_utils.py add comment minor updates minor change for clarity fix small prefix issue and simplify style trigger CIs typo fix Post rebase fix post rebase cleanup simplify tp typo oupsi typo correctly escape improvements based on Marc's review finalize Marc's review comments squash everything * improve * Update modeling_utils.py * Update modeling_utils.py * fix * Update modeling_utils.py * Update modeling_utils.py * style * Update modeling_utils.py * simplify * style * Update modeling_utils.py * Update modeling_utils.py * Update modeling_utils.py * Update modeling_utils.py * Update modeling_utils.py * Update modeling_utils.py * fix dtype issue * Update modeling_utils.py * style * remove test that does not make sense * style * small fixes * style * fix * cleanup after rebase * style * typo * escape * tp for task specific top modules * Update modeling_utils.py * Update modeling_utils.py * fix allocation * CIs * CIs * CIs * improve docstring * CIs * Update modeling_utils.py * fix
This commit is contained in:
@@ -2368,10 +2368,9 @@ class ModelTesterMixin:
|
||||
safe_save_file(placeholder_dict, os.path.join(tmp_dir, "model.safetensors"), metadata={"format": "pt"})
|
||||
model_reloaded, infos = model_class.from_pretrained(tmp_dir, output_loading_info=True)
|
||||
|
||||
prefix = f"{model_reloaded.base_model_prefix}."
|
||||
params = dict(model_reloaded.named_parameters())
|
||||
params.update(dict(model_reloaded.named_buffers()))
|
||||
param_names = {k[len(prefix) :] if k.startswith(prefix) else k for k in params.keys()}
|
||||
param_names = set(params.keys())
|
||||
|
||||
missing_keys = set(infos["missing_keys"])
|
||||
|
||||
@@ -2383,9 +2382,8 @@ class ModelTesterMixin:
|
||||
ptrs[id_tensor_storage(tensor)].append(name)
|
||||
tied_params = [names for _, names in ptrs.items() if len(names) > 1]
|
||||
for group in tied_params:
|
||||
group = {k[len(prefix) :] if k.startswith(prefix) else k for k in group}
|
||||
# We remove the group from extra_missing if not all weights from group are in it
|
||||
if len(group - extra_missing) > 0:
|
||||
if len(set(group) - extra_missing) > 0:
|
||||
extra_missing = extra_missing - set(group)
|
||||
|
||||
self.assertEqual(
|
||||
@@ -2399,15 +2397,14 @@ class ModelTesterMixin:
|
||||
# Remove nonpersistent buffers from missed_missing
|
||||
buffers = [n for n, _ in model_reloaded.named_buffers()]
|
||||
nonpersistent_buffers = {n for n in buffers if n not in model_reloaded.state_dict()}
|
||||
nonpersistent_buffers = {
|
||||
k[len(prefix) :] if k.startswith(prefix) else k for k in nonpersistent_buffers
|
||||
}
|
||||
missed_missing = missed_missing - nonpersistent_buffers
|
||||
|
||||
if model_reloaded._keys_to_ignore_on_load_missing is None:
|
||||
expected_missing = set()
|
||||
else:
|
||||
expected_missing = set(model_reloaded._keys_to_ignore_on_load_missing)
|
||||
expected_missing = set()
|
||||
for pattern in model_reloaded._keys_to_ignore_on_load_missing:
|
||||
expected_missing.update({k for k in param_names if re.search(pattern, k) is not None})
|
||||
self.assertEqual(
|
||||
missed_missing,
|
||||
expected_missing,
|
||||
|
||||
Reference in New Issue
Block a user