From 4c09a960960a550f6103a126b1e98c7511301485 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Mon, 23 Dec 2019 21:20:54 +0100 Subject: [PATCH] Simplify re-raising exceptions. Most module use the simpler `raise` version. Normalize those that don't. --- src/transformers/modeling_tf_pytorch_utils.py | 16 ++++++++-------- src/transformers/modeling_utils.py | 4 ++-- src/transformers/tokenization_xlm.py | 12 ++++++------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/transformers/modeling_tf_pytorch_utils.py b/src/transformers/modeling_tf_pytorch_utils.py index 964b412173..94d15ba74b 100644 --- a/src/transformers/modeling_tf_pytorch_utils.py +++ b/src/transformers/modeling_tf_pytorch_utils.py @@ -76,12 +76,12 @@ def load_pytorch_checkpoint_in_tf2_model(tf_model, pytorch_checkpoint_path, tf_i try: import tensorflow as tf # noqa: F401 import torch # noqa: F401 - except ImportError as e: + except ImportError: logger.error( "Loading a PyTorch model in TensorFlow, requires both PyTorch and TensorFlow to be installed. Please see " "https://pytorch.org/ and https://www.tensorflow.org/install/ for installation instructions." ) - raise e + raise pt_path = os.path.abspath(pytorch_checkpoint_path) logger.info("Loading PyTorch weights from {}".format(pt_path)) @@ -111,12 +111,12 @@ def load_pytorch_weights_in_tf2_model(tf_model, pt_state_dict, tf_inputs=None, a import torch # noqa: F401 import tensorflow as tf # noqa: F401 from tensorflow.python.keras import backend as K - except ImportError as e: + except ImportError: logger.error( "Loading a PyTorch model in TensorFlow, requires both PyTorch and TensorFlow to be installed. Please see " "https://pytorch.org/ and https://www.tensorflow.org/install/ for installation instructions." ) - raise e + raise if tf_inputs is None: tf_inputs = tf_model.dummy_inputs @@ -209,12 +209,12 @@ def load_tf2_checkpoint_in_pytorch_model(pt_model, tf_checkpoint_path, tf_inputs try: import tensorflow as tf # noqa: F401 import torch # noqa: F401 - except ImportError as e: + except ImportError: logger.error( "Loading a TensorFlow model in PyTorch, requires both PyTorch and TensorFlow to be installed. Please see " "https://pytorch.org/ and https://www.tensorflow.org/install/ for installation instructions." ) - raise e + raise import transformers @@ -251,12 +251,12 @@ def load_tf2_weights_in_pytorch_model(pt_model, tf_weights, allow_missing_keys=F try: import tensorflow as tf # noqa: F401 import torch # noqa: F401 - except ImportError as e: + except ImportError: logger.error( "Loading a TensorFlow model in PyTorch, requires both PyTorch and TensorFlow to be installed. Please see " "https://pytorch.org/ and https://www.tensorflow.org/install/ for installation instructions." ) - raise e + raise new_pt_params_dict = {} current_pt_params_dict = dict(pt_model.named_parameters()) diff --git a/src/transformers/modeling_utils.py b/src/transformers/modeling_utils.py index e9ecfd60dc..8413aad595 100644 --- a/src/transformers/modeling_utils.py +++ b/src/transformers/modeling_utils.py @@ -454,12 +454,12 @@ class PreTrainedModel(nn.Module): from transformers import load_tf2_checkpoint_in_pytorch_model model = load_tf2_checkpoint_in_pytorch_model(model, resolved_archive_file, allow_missing_keys=True) - except ImportError as e: + except ImportError: logger.error( "Loading a TensorFlow model in PyTorch, requires both PyTorch and TensorFlow to be installed. Please see " "https://pytorch.org/ and https://www.tensorflow.org/install/ for installation instructions." ) - raise e + raise else: # Convert old format to new format if needed from a PyTorch state_dict old_keys = [] diff --git a/src/transformers/tokenization_xlm.py b/src/transformers/tokenization_xlm.py index de1101f52f..c59d28feee 100644 --- a/src/transformers/tokenization_xlm.py +++ b/src/transformers/tokenization_xlm.py @@ -646,7 +646,7 @@ class XLMTokenizer(PreTrainedTokenizer): self.ja_word_tokenizer = Mykytea.Mykytea( "-model %s/local/share/kytea/model.bin" % os.path.expanduser("~") ) - except (AttributeError, ImportError) as e: + except (AttributeError, ImportError): logger.error( "Make sure you install KyTea (https://github.com/neubig/kytea) and it's python wrapper (https://github.com/chezou/Mykytea-python) with the following steps" ) @@ -655,7 +655,7 @@ class XLMTokenizer(PreTrainedTokenizer): logger.error("3. ./configure --prefix=$HOME/local") logger.error("4. make && make install") logger.error("5. pip install kytea") - raise e + raise return list(self.ja_word_tokenizer.getWS(text)) @property @@ -760,12 +760,12 @@ class XLMTokenizer(PreTrainedTokenizer): from pythainlp.tokenize import word_tokenize as th_word_tokenize else: th_word_tokenize = sys.modules["pythainlp"].word_tokenize - except (AttributeError, ImportError) as e: + except (AttributeError, ImportError): logger.error( "Make sure you install PyThaiNLP (https://github.com/PyThaiNLP/pythainlp) with the following steps" ) logger.error("1. pip install pythainlp") - raise e + raise text = th_word_tokenize(text) elif lang == "zh": try: @@ -773,10 +773,10 @@ class XLMTokenizer(PreTrainedTokenizer): import jieba else: jieba = sys.modules["jieba"] - except (AttributeError, ImportError) as e: + except (AttributeError, ImportError): logger.error("Make sure you install Jieba (https://github.com/fxsjy/jieba) with the following steps") logger.error("1. pip install jieba") - raise e + raise text = " ".join(jieba.cut(text)) text = self.moses_pipeline(text, lang=lang) text = text.split()