Unverified Commit 5f092194 authored by Sylvain Gugger's avatar Sylvain Gugger Committed by GitHub
Browse files

Fix from_pretrained when model is instantiated on the meta device (#22837)

parent 5f9b825c
...@@ -2935,10 +2935,11 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix ...@@ -2935,10 +2935,11 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix
unexpected_keys = list(set(loaded_keys) - set(expected_keys)) unexpected_keys = list(set(loaded_keys) - set(expected_keys))
# Some tensors maybe have been already filled by another key (tied weights). # Some tensors maybe have been already filled by another key (tied weights).
existing_ptrs = {model_state_dict[k].data_ptr() for k in loaded_keys if k in model_state_dict} # TODO: Sylvain -> make this work even on meta device.
missing_keys = [ # existing_ptrs = {model_state_dict[k].data_ptr() for k in loaded_keys if k in model_state_dict}
k for k in missing_keys if k in model_state_dict and model_state_dict[k].data_ptr() not in existing_ptrs # missing_keys = [
] # k for k in missing_keys if k in model_state_dict and model_state_dict[k].data_ptr() not in existing_ptrs
# ]
# Some models may have keys that are not in the state by design, removing them before needlessly warning # Some models may have keys that are not in the state by design, removing them before needlessly warning
# the user. # the user.
if cls._keys_to_ignore_on_load_missing is not None: if cls._keys_to_ignore_on_load_missing is not None:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment