From 4c379daf644b06520c9a2e705f5eff11982426be Mon Sep 17 00:00:00 2001 From: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> Date: Mon, 15 Mar 2021 19:29:54 -0400 Subject: [PATCH] Add minimum version check in examples (#10724) * Add minimum version check in examples * Style * No need for new line maybe? * Add helpful comment --- examples/README.md | 41 +++++++++++++++---- examples/language-modeling/run_clm.py | 4 ++ examples/language-modeling/run_mlm.py | 4 ++ examples/language-modeling/run_plm.py | 4 ++ examples/multiple-choice/run_swag.py | 4 ++ examples/question-answering/run_qa.py | 4 ++ .../question-answering/run_qa_beam_search.py | 4 ++ examples/seq2seq/run_summarization.py | 4 ++ examples/seq2seq/run_translation.py | 4 ++ examples/text-classification/run_glue.py | 4 ++ examples/text-classification/run_xnli.py | 4 ++ examples/token-classification/run_ner.py | 4 ++ src/transformers/utils/__init__.py | 38 +++++++++++++++++ 13 files changed, 116 insertions(+), 7 deletions(-) diff --git a/examples/README.md b/examples/README.md index 6e800cc4ab..f95d76d8df 100644 --- a/examples/README.md +++ b/examples/README.md @@ -33,10 +33,43 @@ Then cd in the example folder of your choice and run pip install -r requirements.txt ``` -Alternatively, you can run the version of the examples as they were for your current version of Transformers via (for instance with v3.5.1): +To browse the examples corresponding to released versions of 🤗 Transformers, click on the line below and then on your desired version of the library: + +
+ Examples for older versions of 🤗 Transformers + + - [v4.3.3](https://github.com/huggingface/transformers/tree/v4.3.3/examples) + - [v4.2.2](https://github.com/huggingface/transformers/tree/v4.2.2/examples) + - [v4.1.1](https://github.com/huggingface/transformers/tree/v4.1.1/examples) + - [v4.0.1](https://github.com/huggingface/transformers/tree/v4.0.1/examples) + - [v3.5.1](https://github.com/huggingface/transformers/tree/v3.5.1/examples) + - [v3.4.0](https://github.com/huggingface/transformers/tree/v3.4.0/examples) + - [v3.3.1](https://github.com/huggingface/transformers/tree/v3.3.1/examples) + - [v3.2.0](https://github.com/huggingface/transformers/tree/v3.2.0/examples) + - [v3.1.0](https://github.com/huggingface/transformers/tree/v3.1.0/examples) + - [v3.0.2](https://github.com/huggingface/transformers/tree/v3.0.2/examples) + - [v2.11.0](https://github.com/huggingface/transformers/tree/v2.11.0/examples) + - [v2.10.0](https://github.com/huggingface/transformers/tree/v2.10.0/examples) + - [v2.9.1](https://github.com/huggingface/transformers/tree/v2.9.1/examples) + - [v2.8.0](https://github.com/huggingface/transformers/tree/v2.8.0/examples) + - [v2.7.0](https://github.com/huggingface/transformers/tree/v2.7.0/examples) + - [v2.6.0](https://github.com/huggingface/transformers/tree/v2.6.0/examples) + - [v2.5.1](https://github.com/huggingface/transformers/tree/v2.5.1/examples) + - [v2.4.0](https://github.com/huggingface/transformers/tree/v2.4.0/examples) + - [v2.3.0](https://github.com/huggingface/transformers/tree/v2.3.0/examples) + - [v2.2.0](https://github.com/huggingface/transformers/tree/v2.2.0/examples) + - [v2.1.1](https://github.com/huggingface/transformers/tree/v2.1.0/examples) + - [v2.0.0](https://github.com/huggingface/transformers/tree/v2.0.0/examples) + - [v1.2.0](https://github.com/huggingface/transformers/tree/v1.2.0/examples) + - [v1.1.0](https://github.com/huggingface/transformers/tree/v1.1.0/examples) + - [v1.0.0](https://github.com/huggingface/transformers/tree/v1.0.0/examples) +
+ +Alternatively, you can find switch your cloned 🤗 Transformers to a specific version (for instance with v3.5.1) with ```bash git checkout tags/v3.5.1 ``` +and run the example command as usual afterward. ## The Big Table of Tasks @@ -62,12 +95,6 @@ Coming soon! | [**`translation`**](https://github.com/huggingface/transformers/tree/master/examples/seq2seq) | WMT | ✅ | - | - | - - - ## Distributed training and mixed precision All the PyTorch scripts mentioned above work out of the box with distributed training and mixed precision, thanks to diff --git a/examples/language-modeling/run_clm.py b/examples/language-modeling/run_clm.py index 7129acbb0f..833a8ccc87 100755 --- a/examples/language-modeling/run_clm.py +++ b/examples/language-modeling/run_clm.py @@ -44,8 +44,12 @@ from transformers import ( set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process +from transformers.utils import check_min_version +# Will error if the minimal version of Transformers is not installed. Remove at your own risks. +check_min_version("4.4.0.dev0") + logger = logging.getLogger(__name__) diff --git a/examples/language-modeling/run_mlm.py b/examples/language-modeling/run_mlm.py index d090dc3bfc..a58ac2ed0e 100755 --- a/examples/language-modeling/run_mlm.py +++ b/examples/language-modeling/run_mlm.py @@ -44,8 +44,12 @@ from transformers import ( set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process +from transformers.utils import check_min_version +# Will error if the minimal version of Transformers is not installed. Remove at your own risks. +check_min_version("4.4.0.dev0") + logger = logging.getLogger(__name__) MODEL_CONFIG_CLASSES = list(MODEL_FOR_MASKED_LM_MAPPING.keys()) MODEL_TYPES = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) diff --git a/examples/language-modeling/run_plm.py b/examples/language-modeling/run_plm.py index 2521557863..7305b393f5 100755 --- a/examples/language-modeling/run_plm.py +++ b/examples/language-modeling/run_plm.py @@ -40,8 +40,12 @@ from transformers import ( set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process +from transformers.utils import check_min_version +# Will error if the minimal version of Transformers is not installed. Remove at your own risks. +check_min_version("4.4.0.dev0") + logger = logging.getLogger(__name__) diff --git a/examples/multiple-choice/run_swag.py b/examples/multiple-choice/run_swag.py index 6b7cb289c4..8a0a78401d 100755 --- a/examples/multiple-choice/run_swag.py +++ b/examples/multiple-choice/run_swag.py @@ -42,8 +42,12 @@ from transformers import ( from transformers.file_utils import PaddingStrategy from transformers.tokenization_utils_base import PreTrainedTokenizerBase from transformers.trainer_utils import get_last_checkpoint, is_main_process +from transformers.utils import check_min_version +# Will error if the minimal version of Transformers is not installed. Remove at your own risks. +check_min_version("4.4.0.dev0") + logger = logging.getLogger(__name__) diff --git a/examples/question-answering/run_qa.py b/examples/question-answering/run_qa.py index 0beacfa8c8..144c04e0f0 100755 --- a/examples/question-answering/run_qa.py +++ b/examples/question-answering/run_qa.py @@ -41,9 +41,13 @@ from transformers import ( set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process +from transformers.utils import check_min_version from utils_qa import postprocess_qa_predictions +# Will error if the minimal version of Transformers is not installed. Remove at your own risks. +check_min_version("4.4.0.dev0") + logger = logging.getLogger(__name__) diff --git a/examples/question-answering/run_qa_beam_search.py b/examples/question-answering/run_qa_beam_search.py index a55ebe2bfd..0fbea56bfe 100755 --- a/examples/question-answering/run_qa_beam_search.py +++ b/examples/question-answering/run_qa_beam_search.py @@ -40,9 +40,13 @@ from transformers import ( set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process +from transformers.utils import check_min_version from utils_qa import postprocess_qa_predictions_with_beam_search +# Will error if the minimal version of Transformers is not installed. Remove at your own risks. +check_min_version("4.4.0.dev0") + logger = logging.getLogger(__name__) diff --git a/examples/seq2seq/run_summarization.py b/examples/seq2seq/run_summarization.py index 4aac21570e..211045ed92 100755 --- a/examples/seq2seq/run_summarization.py +++ b/examples/seq2seq/run_summarization.py @@ -43,8 +43,12 @@ from transformers import ( ) from transformers.file_utils import is_offline_mode from transformers.trainer_utils import get_last_checkpoint, is_main_process +from transformers.utils import check_min_version +# Will error if the minimal version of Transformers is not installed. Remove at your own risks. +check_min_version("4.4.0.dev0") + logger = logging.getLogger(__name__) try: diff --git a/examples/seq2seq/run_translation.py b/examples/seq2seq/run_translation.py index 5bae61c9f0..62e68c19cb 100755 --- a/examples/seq2seq/run_translation.py +++ b/examples/seq2seq/run_translation.py @@ -42,8 +42,12 @@ from transformers import ( set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process +from transformers.utils import check_min_version +# Will error if the minimal version of Transformers is not installed. Remove at your own risks. +check_min_version("4.4.0.dev0") + logger = logging.getLogger(__name__) diff --git a/examples/text-classification/run_glue.py b/examples/text-classification/run_glue.py index 0c20feaf0b..b473953ca2 100755 --- a/examples/text-classification/run_glue.py +++ b/examples/text-classification/run_glue.py @@ -41,8 +41,12 @@ from transformers import ( set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process +from transformers.utils import check_min_version +# Will error if the minimal version of Transformers is not installed. Remove at your own risks. +check_min_version("4.4.0.dev0") + task_to_keys = { "cola": ("sentence", None), "mnli": ("premise", "hypothesis"), diff --git a/examples/text-classification/run_xnli.py b/examples/text-classification/run_xnli.py index ebf3eff0e5..15d2c404a6 100755 --- a/examples/text-classification/run_xnli.py +++ b/examples/text-classification/run_xnli.py @@ -41,8 +41,12 @@ from transformers import ( set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process +from transformers.utils import check_min_version +# Will error if the minimal version of Transformers is not installed. Remove at your own risks. +check_min_version("4.4.0.dev0") + logger = logging.getLogger(__name__) diff --git a/examples/token-classification/run_ner.py b/examples/token-classification/run_ner.py index 61941b58d6..f9cb70881f 100755 --- a/examples/token-classification/run_ner.py +++ b/examples/token-classification/run_ner.py @@ -41,8 +41,12 @@ from transformers import ( set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process +from transformers.utils import check_min_version +# Will error if the minimal version of Transformers is not installed. Remove at your own risks. +check_min_version("4.4.0.dev0") + logger = logging.getLogger(__name__) diff --git a/src/transformers/utils/__init__.py b/src/transformers/utils/__init__.py index e69de29bb2..848724d3f5 100644 --- a/src/transformers/utils/__init__.py +++ b/src/transformers/utils/__init__.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +# coding=utf-8 +# Copyright 2021 The HuggingFace Inc. team. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from packaging import version + +from .. import __version__ + + +def check_min_version(min_version): + if version.parse(__version__) < version.parse(min_version): + if "dev" in min_version: + error_message = ( + "This example requires a source install from 🤗 Transformers (see " + "`https://huggingface.co/transformers/installation.html#installing-from-source`)," + ) + else: + error_message = f"This example requires a minimum version of {min_version}," + error_message += f" but the version found is {__version__}.\n" + raise ImportError( + error_message + + ( + "Check out https://huggingface.co/transformers/examples.html for the examples corresponding to other " + "versions of 🤗 Transformers." + ) + )