Unverified Commit f0dde601 authored by Stas Bekman's avatar Stas Bekman Committed by GitHub
Browse files

[model.from_pretrained] raise exception early on failed load (#12574)




* [model.from_pretrained] raise exception early on failed load

Currently if `load` pretrained weights fails in `from_pretrained`, we first print a whole bunch of successful messages and then fail - this PR puts the exception first to avoid all the misleading messages.

* style
Co-authored-by: default avatarSuraj Patil <surajp815@gmail.com>
parent 75e63dbf
...@@ -1431,6 +1431,10 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix ...@@ -1431,6 +1431,10 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix
load(model_to_load, prefix=start_prefix) load(model_to_load, prefix=start_prefix)
if len(error_msgs) > 0:
error_msg = "\n\t".join(error_msgs)
raise RuntimeError(f"Error(s) in loading state_dict for {model.__class__.__name__}:\n\t{error_msg}")
if len(unexpected_keys) > 0: if len(unexpected_keys) > 0:
logger.warning( logger.warning(
f"Some weights of the model checkpoint at {pretrained_model_name_or_path} were not used when " f"Some weights of the model checkpoint at {pretrained_model_name_or_path} were not used when "
...@@ -1454,9 +1458,6 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix ...@@ -1454,9 +1458,6 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix
f"If your task is similar to the task the model of the checkpoint was trained on, " f"If your task is similar to the task the model of the checkpoint was trained on, "
f"you can already use {model.__class__.__name__} for predictions without further training." f"you can already use {model.__class__.__name__} for predictions without further training."
) )
if len(error_msgs) > 0:
error_msg = "\n\t".join(error_msgs)
raise RuntimeError(f"Error(s) in loading state_dict for {model.__class__.__name__}:\n\t{error_msg}")
return model, missing_keys, unexpected_keys, error_msgs return model, missing_keys, unexpected_keys, error_msgs
......
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