From 3bfd3e17bbfa181d6acdff23c3b978a3954bc28e Mon Sep 17 00:00:00 2001 From: Morgan Date: Thu, 11 Mar 2021 17:17:26 +0100 Subject: [PATCH 1/6] Allow to pass kwargs to model's from_pretrained when using pipeline. --- src/transformers/pipelines/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/transformers/pipelines/__init__.py b/src/transformers/pipelines/__init__.py index 05e28b4d5c23..5f45e629a5ab 100755 --- a/src/transformers/pipelines/__init__.py +++ b/src/transformers/pipelines/__init__.py @@ -246,6 +246,7 @@ def pipeline( framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, + model_kwargs: Dict[str, Any] = {}, **kwargs ) -> Pipeline: """ @@ -307,6 +308,9 @@ def pipeline( artifacts on huggingface.co, so ``revision`` can be any identifier allowed by git. use_fast (:obj:`bool`, `optional`, defaults to :obj:`True`): Whether or not to use a Fast tokenizer if possible (a :class:`~transformers.PreTrainedTokenizerFast`). + model_kwargs: + Additional dictionary of keyword arguments passed along to the model's from_pretrained(..., **model_kwargs) + function. kwargs: Additional keyword arguments passed along to the specific pipeline init (see the documentation for the corresponding pipeline class for possible values). @@ -383,7 +387,7 @@ def pipeline( # Instantiate model if needed if isinstance(model, str): # Handle transparent TF/PT model conversion - model_kwargs = {} + # model_kwargs = {} if framework == "pt" and model.endswith(".h5"): model_kwargs["from_tf"] = True logger.warning( From 4a6c8a49ca7523b64ac795d628c1560524147247 Mon Sep 17 00:00:00 2001 From: Morgan Date: Thu, 11 Mar 2021 17:17:53 +0100 Subject: [PATCH 2/6] Disable the use of past_keys_values for GPT2 when exporting to ONNX. --- src/transformers/convert_graph_to_onnx.py | 9 ++++---- tests/test_onnx.py | 26 +++++++++++++---------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/transformers/convert_graph_to_onnx.py b/src/transformers/convert_graph_to_onnx.py index 25ca790c18c2..620eb570072a 100644 --- a/src/transformers/convert_graph_to_onnx.py +++ b/src/transformers/convert_graph_to_onnx.py @@ -222,7 +222,7 @@ def build_shape_dict(name: str, tensor, is_input: bool, seq_len: int): return input_vars, output_names, dynamic_axes, tokens -def load_graph_from_args(pipeline_name: str, framework: str, model: str, tokenizer: Optional[str] = None) -> Pipeline: +def load_graph_from_args(pipeline_name: str, framework: str, model: str, tokenizer: Optional[str] = None, **models_kwargs) -> Pipeline: """ Convert the set of arguments provided through the CLI to an actual pipeline reference (tokenizer + model @@ -248,7 +248,7 @@ def load_graph_from_args(pipeline_name: str, framework: str, model: str, tokeniz print(f"Loading pipeline (model: {model}, tokenizer: {tokenizer})") # Allocate tokenizer and model - return pipeline(pipeline_name, model=model, tokenizer=tokenizer, framework=framework) + return pipeline(pipeline_name, model=model, tokenizer=tokenizer, framework=framework, model_kwargs=models_kwargs) def convert_pytorch(nlp: Pipeline, opset: int, output: Path, use_external_format: bool): @@ -335,6 +335,7 @@ def convert( tokenizer: Optional[str] = None, use_external_format: bool = False, pipeline_name: str = "feature-extraction", + **model_kwargs ): """ Convert the pipeline object to the ONNX Intermediate Representation (IR) format @@ -347,14 +348,14 @@ def convert( tokenizer: The name of the model to load for the pipeline, default to the model's name if not provided use_external_format: Split the model definition from its parameters to allow model bigger than 2GB (PyTorch only) pipeline_name: The kind of pipeline to instantiate (ner, question-answering, etc.) - + model_kwargs: Keyword arguments to be forwarded to the model constructor Returns: """ print(f"ONNX opset version set to: {opset}") # Load the pipeline - nlp = load_graph_from_args(pipeline_name, framework, model, tokenizer) + nlp = load_graph_from_args(pipeline_name, framework, model, tokenizer, **model_kwargs) if not output.parent.exists(): print(f"Creating folder {output.parent}") diff --git a/tests/test_onnx.py b/tests/test_onnx.py index 9de0d34dd081..fa6f8d414cf6 100644 --- a/tests/test_onnx.py +++ b/tests/test_onnx.py @@ -38,19 +38,23 @@ def forward(self, input_ids, some_other_args, token_type_ids, attention_mask): class OnnxExportTestCase(unittest.TestCase): - MODEL_TO_TEST = ["bert-base-cased", "gpt2", "roberta-base"] + MODEL_TO_TEST = [ + # (model_name, model_kwargs) + ("bert-base-cased", {}), + ("gpt2", {"use_cache": False}), # We don't support exporting GPT2 past keys anymore + ] @require_tf @slow def test_export_tensorflow(self): - for model in OnnxExportTestCase.MODEL_TO_TEST: - self._test_export(model, "tf", 12) + for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: + self._test_export(model, "tf", 12, **model_kwargs) @require_torch @slow def test_export_pytorch(self): - for model in OnnxExportTestCase.MODEL_TO_TEST: - self._test_export(model, "pt", 12) + for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: + self._test_export(model, "pt", 12, **model_kwargs) @require_torch @slow @@ -71,8 +75,8 @@ def test_export_custom_bert_model(self): @require_tf @slow def test_quantize_tf(self): - for model in OnnxExportTestCase.MODEL_TO_TEST: - path = self._test_export(model, "tf", 12) + for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: + path = self._test_export(model, "tf", 12, **model_kwargs) quantized_path = quantize(Path(path)) # Ensure the actual quantized model is not bigger than the original one @@ -82,15 +86,15 @@ def test_quantize_tf(self): @require_torch @slow def test_quantize_pytorch(self): - for model in OnnxExportTestCase.MODEL_TO_TEST: - path = self._test_export(model, "pt", 12) + for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST: + path = self._test_export(model, "pt", 12, **model_kwargs) quantized_path = quantize(path) # Ensure the actual quantized model is not bigger than the original one if quantized_path.stat().st_size >= Path(path).stat().st_size: self.fail("Quantized model is bigger than initial ONNX model") - def _test_export(self, model, framework, opset, tokenizer=None): + def _test_export(self, model, framework, opset, tokenizer=None, **model_kwargs): try: # Compute path with TemporaryDirectory() as tempdir: @@ -101,7 +105,7 @@ def _test_export(self, model, framework, opset, tokenizer=None): path.parent.rmdir() # Export - convert(framework, model, path, opset, tokenizer) + convert(framework, model, path, opset, tokenizer, **model_kwargs) return path except Exception as e: From 3ec667efbeb7c2437cc3bc0c86f46004388e5f68 Mon Sep 17 00:00:00 2001 From: Morgan Funtowicz Date: Thu, 11 Mar 2021 17:33:04 +0100 Subject: [PATCH 3/6] style --- src/transformers/convert_graph_to_onnx.py | 5 ++++- tests/test_onnx.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/transformers/convert_graph_to_onnx.py b/src/transformers/convert_graph_to_onnx.py index 620eb570072a..eaf3c9104b51 100644 --- a/src/transformers/convert_graph_to_onnx.py +++ b/src/transformers/convert_graph_to_onnx.py @@ -222,7 +222,9 @@ def build_shape_dict(name: str, tensor, is_input: bool, seq_len: int): return input_vars, output_names, dynamic_axes, tokens -def load_graph_from_args(pipeline_name: str, framework: str, model: str, tokenizer: Optional[str] = None, **models_kwargs) -> Pipeline: +def load_graph_from_args( + pipeline_name: str, framework: str, model: str, tokenizer: Optional[str] = None, **models_kwargs +) -> Pipeline: """ Convert the set of arguments provided through the CLI to an actual pipeline reference (tokenizer + model @@ -349,6 +351,7 @@ def convert( use_external_format: Split the model definition from its parameters to allow model bigger than 2GB (PyTorch only) pipeline_name: The kind of pipeline to instantiate (ner, question-answering, etc.) model_kwargs: Keyword arguments to be forwarded to the model constructor + Returns: """ diff --git a/tests/test_onnx.py b/tests/test_onnx.py index fa6f8d414cf6..009197b5c5ef 100644 --- a/tests/test_onnx.py +++ b/tests/test_onnx.py @@ -41,7 +41,7 @@ class OnnxExportTestCase(unittest.TestCase): MODEL_TO_TEST = [ # (model_name, model_kwargs) ("bert-base-cased", {}), - ("gpt2", {"use_cache": False}), # We don't support exporting GPT2 past keys anymore + ("gpt2", {"use_cache": False}), # We don't support exporting GPT2 past keys anymore ] @require_tf From fddcf3dd5e40482d661c3929a5c9ec3ae6da761b Mon Sep 17 00:00:00 2001 From: Morgan Date: Thu, 11 Mar 2021 17:40:32 +0100 Subject: [PATCH 4/6] Remove comment. --- src/transformers/pipelines/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/transformers/pipelines/__init__.py b/src/transformers/pipelines/__init__.py index 5f45e629a5ab..e48cf8a5d881 100755 --- a/src/transformers/pipelines/__init__.py +++ b/src/transformers/pipelines/__init__.py @@ -387,7 +387,6 @@ def pipeline( # Instantiate model if needed if isinstance(model, str): # Handle transparent TF/PT model conversion - # model_kwargs = {} if framework == "pt" and model.endswith(".h5"): model_kwargs["from_tf"] = True logger.warning( From 2d6c609511b3c3e7474126461e0fadde7c54a7b1 Mon Sep 17 00:00:00 2001 From: Lysandre Date: Thu, 11 Mar 2021 11:44:52 -0500 Subject: [PATCH 5/6] Appease the documentation gods --- src/transformers/pipelines/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transformers/pipelines/__init__.py b/src/transformers/pipelines/__init__.py index e48cf8a5d881..f6a772a68ad9 100755 --- a/src/transformers/pipelines/__init__.py +++ b/src/transformers/pipelines/__init__.py @@ -309,8 +309,8 @@ def pipeline( use_fast (:obj:`bool`, `optional`, defaults to :obj:`True`): Whether or not to use a Fast tokenizer if possible (a :class:`~transformers.PreTrainedTokenizerFast`). model_kwargs: - Additional dictionary of keyword arguments passed along to the model's from_pretrained(..., **model_kwargs) - function. + Additional dictionary of keyword arguments passed along to the model's + :obj:`from_pretrained(..., **model_kwargs)` function. kwargs: Additional keyword arguments passed along to the specific pipeline init (see the documentation for the corresponding pipeline class for possible values). From 6d573f0adf9560410f398a11fff9d9c6a1173a17 Mon Sep 17 00:00:00 2001 From: Lysandre Date: Thu, 11 Mar 2021 12:25:22 -0500 Subject: [PATCH 6/6] Fix style --- src/transformers/pipelines/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transformers/pipelines/__init__.py b/src/transformers/pipelines/__init__.py index f6a772a68ad9..762994fa8614 100755 --- a/src/transformers/pipelines/__init__.py +++ b/src/transformers/pipelines/__init__.py @@ -309,8 +309,8 @@ def pipeline( use_fast (:obj:`bool`, `optional`, defaults to :obj:`True`): Whether or not to use a Fast tokenizer if possible (a :class:`~transformers.PreTrainedTokenizerFast`). model_kwargs: - Additional dictionary of keyword arguments passed along to the model's - :obj:`from_pretrained(..., **model_kwargs)` function. + Additional dictionary of keyword arguments passed along to the model's :obj:`from_pretrained(..., + **model_kwargs)` function. kwargs: Additional keyword arguments passed along to the specific pipeline init (see the documentation for the corresponding pipeline class for possible values).