From 0f71c290535672d7b0eff5858a3dc3d7bd07a983 Mon Sep 17 00:00:00 2001 From: lewtun Date: Mon, 14 Feb 2022 18:03:07 +0100 Subject: [PATCH] Remove redundant error logging in from_pretrained() method (#15631) * Remove error logging in from_pretrained() method --- src/transformers/configuration_utils.py | 15 +++++---------- src/transformers/feature_extraction_utils.py | 15 +++++---------- src/transformers/file_utils.py | 6 ++---- src/transformers/modeling_flax_utils.py | 16 +++++----------- src/transformers/modeling_tf_utils.py | 16 +++++----------- src/transformers/modeling_utils.py | 16 +++++----------- src/transformers/tokenization_utils_base.py | 6 ++---- 7 files changed, 29 insertions(+), 61 deletions(-) diff --git a/src/transformers/configuration_utils.py b/src/transformers/configuration_utils.py index 8548281631..ba20b00d3e 100755 --- a/src/transformers/configuration_utils.py +++ b/src/transformers/configuration_utils.py @@ -602,36 +602,31 @@ class PretrainedConfig(PushToHubMixin): user_agent=user_agent, ) - except RepositoryNotFoundError as err: - logger.error(err) + except RepositoryNotFoundError: raise EnvironmentError( f"{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier listed on " "'https://huggingface.co/models'\nIf this is a private repository, make sure to pass a token having " "permission to this repo with `use_auth_token` or log in with `huggingface-cli login` and pass " "`use_auth_token=True`." ) - except RevisionNotFoundError as err: - logger.error(err) + except RevisionNotFoundError: raise EnvironmentError( f"{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for this " f"model name. Check the model page at 'https://huggingface.co/{pretrained_model_name_or_path}' for " "available revisions." ) - except EntryNotFoundError as err: - logger.error(err) + except EntryNotFoundError: raise EnvironmentError( f"{pretrained_model_name_or_path} does not appear to have a file named {configuration_file}." ) - except HTTPError as err: - logger.error(err) + except HTTPError: raise EnvironmentError( "We couldn't connect to 'https://huggingface.co/' to load this model and it looks like " f"{pretrained_model_name_or_path} is not the path to a directory conaining a {configuration_file} " "file.\nCheckout your internet connection or see how to run the library in offline mode at " "'https://huggingface.co/docs/transformers/installation#offline-mode'." ) - except EnvironmentError as err: - logger.error(err) + except EnvironmentError: raise EnvironmentError( f"Can't load config for '{pretrained_model_name_or_path}'. If you were trying to load it from " "'https://huggingface.co/models', make sure you don't have a local directory with the same name. " diff --git a/src/transformers/feature_extraction_utils.py b/src/transformers/feature_extraction_utils.py index 453d73e360..f4877cd3a6 100644 --- a/src/transformers/feature_extraction_utils.py +++ b/src/transformers/feature_extraction_utils.py @@ -412,36 +412,31 @@ class FeatureExtractionMixin(PushToHubMixin): user_agent=user_agent, ) - except RepositoryNotFoundError as err: - logger.error(err) + except RepositoryNotFoundError: raise EnvironmentError( f"{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier listed on " "'https://huggingface.co/models'\nIf this is a private repository, make sure to pass a token having " "permission to this repo with `use_auth_token` or log in with `huggingface-cli login` and pass " "`use_auth_token=True`." ) - except RevisionNotFoundError as err: - logger.error(err) + except RevisionNotFoundError: raise EnvironmentError( f"{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for this " f"model name. Check the model page at 'https://huggingface.co/{pretrained_model_name_or_path}' for " "available revisions." ) - except EntryNotFoundError as err: - logger.error(err) + except EntryNotFoundError: raise EnvironmentError( f"{pretrained_model_name_or_path} does not appear to have a file named {FEATURE_EXTRACTOR_NAME}." ) - except HTTPError as err: - logger.error(err) + except HTTPError: raise EnvironmentError( "We couldn't connect to 'https://huggingface.co/' to load this model and it looks like " f"{pretrained_model_name_or_path} is not the path to a directory conaining a " f"{FEATURE_EXTRACTOR_NAME} file.\nCheckout your internet connection or see how to run the library in " "offline mode at 'https://huggingface.co/docs/transformers/installation#offline-mode'." ) - except EnvironmentError as err: - logger.error(err) + except EnvironmentError: raise EnvironmentError( f"Can't load feature extractor for '{pretrained_model_name_or_path}'. If you were trying to load it " "from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. " diff --git a/src/transformers/file_utils.py b/src/transformers/file_utils.py index 5d1a086e12..21194cb6f9 100644 --- a/src/transformers/file_utils.py +++ b/src/transformers/file_utils.py @@ -2302,16 +2302,14 @@ def get_file_from_repo( use_auth_token=use_auth_token, ) - except RepositoryNotFoundError as err: - logger.error(err) + except RepositoryNotFoundError: raise EnvironmentError( f"{path_or_repo} is not a local folder and is not a valid model identifier " "listed on 'https://huggingface.co/models'\nIf this is a private repository, make sure to " "pass a token having permission to this repo with `use_auth_token` or log in with " "`huggingface-cli login` and pass `use_auth_token=True`." ) - except RevisionNotFoundError as err: - logger.error(err) + except RevisionNotFoundError: raise EnvironmentError( f"{revision} is not a valid git identifier (branch name, tag name or commit id) that exists " "for this model name. Check the model page at " diff --git a/src/transformers/modeling_flax_utils.py b/src/transformers/modeling_flax_utils.py index 969fd6daf2..1d7c824210 100644 --- a/src/transformers/modeling_flax_utils.py +++ b/src/transformers/modeling_flax_utils.py @@ -492,23 +492,20 @@ class FlaxPreTrainedModel(PushToHubMixin, FlaxGenerationMixin): user_agent=user_agent, ) - except RepositoryNotFoundError as err: - logger.error(err) + except RepositoryNotFoundError: raise EnvironmentError( f"{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier " "listed on 'https://huggingface.co/models'\nIf this is a private repository, make sure to pass a " "token having permission to this repo with `use_auth_token` or log in with `huggingface-cli " "login` and pass `use_auth_token=True`." ) - except RevisionNotFoundError as err: - logger.error(err) + except RevisionNotFoundError: raise EnvironmentError( f"{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for " "this model name. Check the model page at " f"'https://huggingface.co/{pretrained_model_name_or_path}' for available revisions." ) - except EntryNotFoundError as err: - logger.error(err) + except EntryNotFoundError: if filename == FLAX_WEIGHTS_NAME: has_file_kwargs = {"revision": revision, "proxies": proxies, "use_auth_token": use_auth_token} if has_file(pretrained_model_name_or_path, WEIGHTS_NAME, **has_file_kwargs): @@ -518,7 +515,6 @@ class FlaxPreTrainedModel(PushToHubMixin, FlaxGenerationMixin): "those weights." ) else: - logger.error(err) raise EnvironmentError( f"{pretrained_model_name_or_path} does not appear to have a file named {FLAX_WEIGHTS_NAME} " f"or {WEIGHTS_NAME}." @@ -527,8 +523,7 @@ class FlaxPreTrainedModel(PushToHubMixin, FlaxGenerationMixin): raise EnvironmentError( f"{pretrained_model_name_or_path} does not appear to have a file named {filename}." ) - except HTTPError as err: - logger.error(err) + except HTTPError: raise EnvironmentError( "We couldn't connect to 'https://huggingface.co/' to load this model and it looks like " f"{pretrained_model_name_or_path} is not the path to a directory conaining a a file named " @@ -536,8 +531,7 @@ class FlaxPreTrainedModel(PushToHubMixin, FlaxGenerationMixin): "Checkout your internet connection or see how to run the library in offline mode at " "'https://huggingface.co/docs/transformers/installation#offline-mode'." ) - except EnvironmentError as err: - logger.error(err) + except EnvironmentError: raise EnvironmentError( f"Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it from " "'https://huggingface.co/models', make sure you don't have a local directory with the same name. " diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 428af92b2b..e34b7aa2ca 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1588,23 +1588,20 @@ class TFPreTrainedModel(tf.keras.Model, TFModelUtilsMixin, TFGenerationMixin, Pu user_agent=user_agent, ) - except RepositoryNotFoundError as err: - logger.error(err) + except RepositoryNotFoundError: raise EnvironmentError( f"{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier " "listed on 'https://huggingface.co/models'\nIf this is a private repository, make sure to pass a " "token having permission to this repo with `use_auth_token` or log in with `huggingface-cli " "login` and pass `use_auth_token=True`." ) - except RevisionNotFoundError as err: - logger.error(err) + except RevisionNotFoundError: raise EnvironmentError( f"{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for " "this model name. Check the model page at " f"'https://huggingface.co/{pretrained_model_name_or_path}' for available revisions." ) - except EntryNotFoundError as err: - logger.error(err) + except EntryNotFoundError: if filename == TF2_WEIGHTS_NAME: has_file_kwargs = { "revision": revision, @@ -1619,7 +1616,6 @@ class TFPreTrainedModel(tf.keras.Model, TFModelUtilsMixin, TFGenerationMixin, Pu "those weights." ) else: - logger.error(err) raise EnvironmentError( f"{pretrained_model_name_or_path} does not appear to have a file named {TF2_WEIGHTS_NAME} " f"or {WEIGHTS_NAME}." @@ -1628,8 +1624,7 @@ class TFPreTrainedModel(tf.keras.Model, TFModelUtilsMixin, TFGenerationMixin, Pu raise EnvironmentError( f"{pretrained_model_name_or_path} does not appear to have a file named {filename}." ) - except HTTPError as err: - logger.error(err) + except HTTPError: raise EnvironmentError( "We couldn't connect to 'https://huggingface.co/' to load this model and it looks like " f"{pretrained_model_name_or_path} is not the path to a directory conaining a a file named " @@ -1637,8 +1632,7 @@ class TFPreTrainedModel(tf.keras.Model, TFModelUtilsMixin, TFGenerationMixin, Pu "Checkout your internet connection or see how to run the library in offline mode at " "'https://huggingface.co/docs/transformers/installation#offline-mode'." ) - except EnvironmentError as err: - logger.error(err) + except EnvironmentError: raise EnvironmentError( f"Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it from " "'https://huggingface.co/models', make sure you don't have a local directory with the same name. " diff --git a/src/transformers/modeling_utils.py b/src/transformers/modeling_utils.py index bc93e0616b..10a313065b 100644 --- a/src/transformers/modeling_utils.py +++ b/src/transformers/modeling_utils.py @@ -1366,23 +1366,20 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix user_agent=user_agent, ) - except RepositoryNotFoundError as err: - logger.error(err) + except RepositoryNotFoundError: raise EnvironmentError( f"{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier " "listed on 'https://huggingface.co/models'\nIf this is a private repository, make sure to pass a " "token having permission to this repo with `use_auth_token` or log in with `huggingface-cli " "login` and pass `use_auth_token=True`." ) - except RevisionNotFoundError as err: - logger.error(err) + except RevisionNotFoundError: raise EnvironmentError( f"{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for " "this model name. Check the model page at " f"'https://huggingface.co/{pretrained_model_name_or_path}' for available revisions." ) - except EntryNotFoundError as err: - logger.error(err) + except EntryNotFoundError: if filename == WEIGHTS_NAME: has_file_kwargs = { "revision": revision, @@ -1403,7 +1400,6 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix "weights." ) else: - logger.error(err) raise EnvironmentError( f"{pretrained_model_name_or_path} does not appear to have a file named {WEIGHTS_NAME}, " f"{TF2_WEIGHTS_NAME}, {TF_WEIGHTS_NAME} or {FLAX_WEIGHTS_NAME}." @@ -1412,8 +1408,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix raise EnvironmentError( f"{pretrained_model_name_or_path} does not appear to have a file named {filename}." ) - except HTTPError as err: - logger.error(err) + except HTTPError: raise EnvironmentError( "We couldn't connect to 'https://huggingface.co/' to load this model and it looks like " f"{pretrained_model_name_or_path} is not the path to a directory conaining a a file named " @@ -1421,8 +1416,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix "Checkout your internet connection or see how to run the library in offline mode at " "'https://huggingface.co/docs/transformers/installation#offline-mode'." ) - except EnvironmentError as err: - logger.error(err) + except EnvironmentError: raise EnvironmentError( f"Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it from " "'https://huggingface.co/models', make sure you don't have a local directory with the same name. " diff --git a/src/transformers/tokenization_utils_base.py b/src/transformers/tokenization_utils_base.py index 27ed8b8984..0f3f14c575 100644 --- a/src/transformers/tokenization_utils_base.py +++ b/src/transformers/tokenization_utils_base.py @@ -1738,16 +1738,14 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): else: raise error - except RepositoryNotFoundError as err: - logger.error(err) + except RepositoryNotFoundError: raise EnvironmentError( f"{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier " "listed on 'https://huggingface.co/models'\nIf this is a private repository, make sure to " "pass a token having permission to this repo with `use_auth_token` or log in with " "`huggingface-cli login` and pass `use_auth_token=True`." ) - except RevisionNotFoundError as err: - logger.error(err) + except RevisionNotFoundError: raise EnvironmentError( f"{revision} is not a valid git identifier (branch name, tag name or commit id) that exists " "for this model name. Check the model page at "