From 35d48db881edec8a5ea60db9cf54cda7dd42506c Mon Sep 17 00:00:00 2001 From: Zachary Mueller Date: Mon, 2 May 2022 11:56:15 -0400 Subject: [PATCH] Update no_trainer examples to use new logger (#17044) * Propagate and fix imports --- examples/pytorch/README.md | 4 ++-- .../run_image_classification_no_trainer.py | 9 +++------ .../pytorch/language-modeling/run_clm_no_trainer.py | 9 +++------ .../pytorch/language-modeling/run_mlm_no_trainer.py | 9 +++------ .../pytorch/multiple-choice/run_swag_no_trainer.py | 9 +++------ .../run_qa_beam_search_no_trainer.py | 9 +++------ .../pytorch/question-answering/run_qa_no_trainer.py | 9 +++------ .../run_semantic_segmentation_no_trainer.py | 10 +++------- .../run_wav2vec2_pretraining_no_trainer.py | 10 +++------- .../summarization/run_summarization_no_trainer.py | 9 +++------ .../pytorch/text-classification/run_glue_no_trainer.py | 9 +++------ .../pytorch/token-classification/run_ner_no_trainer.py | 9 +++------ .../pytorch/translation/run_translation_no_trainer.py | 9 +++------ 13 files changed, 38 insertions(+), 76 deletions(-) diff --git a/examples/pytorch/README.md b/examples/pytorch/README.md index c19bcfc955..95d42bfc8b 100644 --- a/examples/pytorch/README.md +++ b/examples/pytorch/README.md @@ -167,10 +167,10 @@ python xla_spawn.py --num_cores 8 \ Most PyTorch example scripts have a version using the [🤗 Accelerate](https://github.com/huggingface/accelerate) library that exposes the training loop so it's easy for you to customize or tweak them to your needs. They all require you to -install `accelerate` with +install `accelerate` with the latest development version ```bash -pip install accelerate +pip install git+https://github.com/huggingface/accelerate ``` Then you can easily launch any of the scripts by running diff --git a/examples/pytorch/image-classification/run_image_classification_no_trainer.py b/examples/pytorch/image-classification/run_image_classification_no_trainer.py index 64f4601f3a..6d435753f3 100644 --- a/examples/pytorch/image-classification/run_image_classification_no_trainer.py +++ b/examples/pytorch/image-classification/run_image_classification_no_trainer.py @@ -37,6 +37,7 @@ from tqdm.auto import tqdm import transformers from accelerate import Accelerator +from accelerate.logging import get_logger from accelerate.utils import set_seed from huggingface_hub import Repository from transformers import ( @@ -50,7 +51,7 @@ from transformers.utils import get_full_repo_name from transformers.utils.versions import require_version -logger = logging.getLogger(__name__) +logger = get_logger(__name__) require_version("datasets>=2.0.0", "To fix: pip install -r examples/pytorch/image-classification/requirements.txt") @@ -188,11 +189,7 @@ def main(): datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, ) - logger.info(accelerator.state) - - # Setup logging, we only want one process per machine to log things on the screen. - # accelerator.is_local_main_process is only True for one process per machine. - logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR) + logger.info(accelerator.state, main_process_only=False) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() diff --git a/examples/pytorch/language-modeling/run_clm_no_trainer.py b/examples/pytorch/language-modeling/run_clm_no_trainer.py index 3e7cfaa3aa..6d0ea73dcb 100755 --- a/examples/pytorch/language-modeling/run_clm_no_trainer.py +++ b/examples/pytorch/language-modeling/run_clm_no_trainer.py @@ -39,6 +39,7 @@ from tqdm.auto import tqdm import transformers from accelerate import Accelerator, DistributedType +from accelerate.logging import get_logger from accelerate.utils import set_seed from huggingface_hub import Repository from transformers import ( @@ -56,7 +57,7 @@ from transformers.utils import get_full_repo_name from transformers.utils.versions import require_version -logger = logging.getLogger(__name__) +logger = get_logger(__name__) require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/language-modeling/requirements.txt") @@ -234,11 +235,7 @@ def main(): datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, ) - logger.info(accelerator.state) - - # Setup logging, we only want one process per machine to log things on the screen. - # accelerator.is_local_main_process is only True for one process per machine. - logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR) + logger.info(accelerator.state, main_process_only=False) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() diff --git a/examples/pytorch/language-modeling/run_mlm_no_trainer.py b/examples/pytorch/language-modeling/run_mlm_no_trainer.py index d7d8d011ac..b46e7b013c 100755 --- a/examples/pytorch/language-modeling/run_mlm_no_trainer.py +++ b/examples/pytorch/language-modeling/run_mlm_no_trainer.py @@ -39,6 +39,7 @@ from tqdm.auto import tqdm import transformers from accelerate import Accelerator, DistributedType +from accelerate.logging import get_logger from accelerate.utils import set_seed from huggingface_hub import Repository from transformers import ( @@ -56,7 +57,7 @@ from transformers.utils import get_full_repo_name from transformers.utils.versions import require_version -logger = logging.getLogger(__name__) +logger = get_logger(__name__) require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/language-modeling/requirements.txt") MODEL_CONFIG_CLASSES = list(MODEL_MAPPING.keys()) MODEL_TYPES = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @@ -245,11 +246,7 @@ def main(): datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, ) - logger.info(accelerator.state) - - # Setup logging, we only want one process per machine to log things on the screen. - # accelerator.is_local_main_process is only True for one process per machine. - logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR) + logger.info(accelerator.state, main_process_only=False) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() diff --git a/examples/pytorch/multiple-choice/run_swag_no_trainer.py b/examples/pytorch/multiple-choice/run_swag_no_trainer.py index 2c39d29cb1..193efb29f1 100755 --- a/examples/pytorch/multiple-choice/run_swag_no_trainer.py +++ b/examples/pytorch/multiple-choice/run_swag_no_trainer.py @@ -37,6 +37,7 @@ from tqdm.auto import tqdm import transformers from accelerate import Accelerator +from accelerate.logging import get_logger from accelerate.utils import set_seed from huggingface_hub import Repository from transformers import ( @@ -54,7 +55,7 @@ from transformers import ( from transformers.utils import PaddingStrategy, get_full_repo_name -logger = logging.getLogger(__name__) +logger = get_logger(__name__) # You should update this to your particular problem to have better documentation of `model_type` MODEL_CONFIG_CLASSES = list(MODEL_MAPPING.keys()) MODEL_TYPES = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @@ -272,11 +273,7 @@ def main(): datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, ) - logger.info(accelerator.state) - - # Setup logging, we only want one process per machine to log things on the screen. - # accelerator.is_local_main_process is only True for one process per machine. - logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR) + logger.info(accelerator.state, main_process_only=False) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() diff --git a/examples/pytorch/question-answering/run_qa_beam_search_no_trainer.py b/examples/pytorch/question-answering/run_qa_beam_search_no_trainer.py index 6e365c9814..c16f77c5f6 100644 --- a/examples/pytorch/question-answering/run_qa_beam_search_no_trainer.py +++ b/examples/pytorch/question-answering/run_qa_beam_search_no_trainer.py @@ -35,6 +35,7 @@ from tqdm.auto import tqdm import transformers from accelerate import Accelerator +from accelerate.logging import get_logger from accelerate.utils import set_seed from huggingface_hub import Repository from transformers import ( @@ -58,7 +59,7 @@ check_min_version("4.19.0.dev0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/question-answering/requirements.txt") -logger = logging.getLogger(__name__) +logger = get_logger(__name__) def save_prefixed_metrics(results, output_dir, file_name: str = "all_results.json", metric_key_prefix: str = "eval"): @@ -289,11 +290,7 @@ def main(): datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, ) - logger.info(accelerator.state) - - # Setup logging, we only want one process per machine to log things on the screen. - # accelerator.is_local_main_process is only True for one process per machine. - logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR) + logger.info(accelerator.state, main_process_only=False) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() diff --git a/examples/pytorch/question-answering/run_qa_no_trainer.py b/examples/pytorch/question-answering/run_qa_no_trainer.py index 530df23fd2..aa697ed114 100755 --- a/examples/pytorch/question-answering/run_qa_no_trainer.py +++ b/examples/pytorch/question-answering/run_qa_no_trainer.py @@ -35,6 +35,7 @@ from tqdm.auto import tqdm import transformers from accelerate import Accelerator +from accelerate.logging import get_logger from accelerate.utils import set_seed from huggingface_hub import Repository from transformers import ( @@ -60,7 +61,7 @@ check_min_version("4.19.0.dev0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/question-answering/requirements.txt") -logger = logging.getLogger(__name__) +logger = get_logger(__name__) # You should update this to your particular problem to have better documentation of `model_type` MODEL_CONFIG_CLASSES = list(MODEL_MAPPING.keys()) MODEL_TYPES = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @@ -318,11 +319,7 @@ def main(): datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, ) - logger.info(accelerator.state) - - # Setup logging, we only want one process per machine to log things on the screen. - # accelerator.is_local_main_process is only True for one process per machine. - logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR) + logger.info(accelerator.state, main_process_only=False) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() diff --git a/examples/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py b/examples/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py index 77fc95b2a6..18053bf4d6 100644 --- a/examples/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py +++ b/examples/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py @@ -16,7 +16,6 @@ import argparse import json -import logging import math import os import random @@ -34,6 +33,7 @@ from tqdm.auto import tqdm import transformers from accelerate import Accelerator +from accelerate.logging import get_logger from accelerate.utils import set_seed from huggingface_hub import Repository, hf_hub_download from transformers import ( @@ -48,7 +48,7 @@ from transformers.utils import get_full_repo_name from transformers.utils.versions import require_version -logger = logging.getLogger(__name__) +logger = get_logger(__name__) require_version("datasets>=2.0.0", "To fix: pip install -r examples/pytorch/semantic-segmentation/requirements.txt") @@ -308,11 +308,7 @@ def main(): # Initialize the accelerator. We will let the accelerator handle device placement for us in this example. # If we're using tracking, we also need to initialize it here and it will pick up all supported trackers in the environment accelerator = Accelerator(log_with="all", logging_dir=args.output_dir) if args.with_tracking else Accelerator() - logger.info(accelerator.state) - - # Setup logging, we only want one process per machine to log things on the screen. - # accelerator.is_local_main_process is only True for one process per machine. - logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR) + logger.info(accelerator.state, main_process_only=False) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() diff --git a/examples/pytorch/speech-pretraining/run_wav2vec2_pretraining_no_trainer.py b/examples/pytorch/speech-pretraining/run_wav2vec2_pretraining_no_trainer.py index 680808f2e7..a66d1f5493 100755 --- a/examples/pytorch/speech-pretraining/run_wav2vec2_pretraining_no_trainer.py +++ b/examples/pytorch/speech-pretraining/run_wav2vec2_pretraining_no_trainer.py @@ -16,7 +16,6 @@ """ Pre-Training a 🤗 Wav2Vec2 model on unlabeled audio data """ import argparse -import logging import math import os from dataclasses import dataclass @@ -31,6 +30,7 @@ from tqdm.auto import tqdm import transformers from accelerate import Accelerator +from accelerate.logging import get_logger from huggingface_hub import Repository from transformers import ( AdamW, @@ -46,7 +46,7 @@ from transformers.models.wav2vec2.modeling_wav2vec2 import _compute_mask_indices from transformers.utils import get_full_repo_name -logger = logging.getLogger(__name__) +logger = get_logger(__name__) def parse_args(): @@ -362,11 +362,7 @@ def main(): # Initialize the accelerator. We will let the accelerator handle device placement for us in this example. accelerator = Accelerator() - logger.info(accelerator.state) - - # Setup logging, we only want one process per machine to log things on the screen. - # accelerator.is_local_main_process is only True for one process per machine. - logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR) + logger.info(accelerator.state, main_process_only=False) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() diff --git a/examples/pytorch/summarization/run_summarization_no_trainer.py b/examples/pytorch/summarization/run_summarization_no_trainer.py index e08edbf513..b6726889f1 100644 --- a/examples/pytorch/summarization/run_summarization_no_trainer.py +++ b/examples/pytorch/summarization/run_summarization_no_trainer.py @@ -36,6 +36,7 @@ from tqdm.auto import tqdm import transformers from accelerate import Accelerator +from accelerate.logging import get_logger from accelerate.utils import set_seed from filelock import FileLock from huggingface_hub import Repository @@ -54,7 +55,7 @@ from transformers.utils import get_full_repo_name, is_offline_mode from transformers.utils.versions import require_version -logger = logging.getLogger(__name__) +logger = get_logger(__name__) require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/summarization/requirements.txt") # You should update this to your particular problem to have better documentation of `model_type` @@ -322,11 +323,7 @@ def main(): datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, ) - logger.info(accelerator.state) - - # Setup logging, we only want one process per machine to log things on the screen. - # accelerator.is_local_main_process is only True for one process per machine. - logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR) + logger.info(accelerator.state, main_process_only=False) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() diff --git a/examples/pytorch/text-classification/run_glue_no_trainer.py b/examples/pytorch/text-classification/run_glue_no_trainer.py index 860e4035a3..977c6df1c1 100644 --- a/examples/pytorch/text-classification/run_glue_no_trainer.py +++ b/examples/pytorch/text-classification/run_glue_no_trainer.py @@ -29,6 +29,7 @@ from tqdm.auto import tqdm import transformers from accelerate import Accelerator +from accelerate.logging import get_logger from accelerate.utils import set_seed from huggingface_hub import Repository from transformers import ( @@ -46,7 +47,7 @@ from transformers.utils import get_full_repo_name from transformers.utils.versions import require_version -logger = logging.getLogger(__name__) +logger = get_logger(__name__) require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/text-classification/requirements.txt") @@ -200,11 +201,7 @@ def main(): datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, ) - logger.info(accelerator.state) - - # Setup logging, we only want one process per machine to log things on the screen. - # accelerator.is_local_main_process is only True for one process per machine. - logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR) + logger.info(accelerator.state, main_process_only=False) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() diff --git a/examples/pytorch/token-classification/run_ner_no_trainer.py b/examples/pytorch/token-classification/run_ner_no_trainer.py index 6281ee162d..d0514f6812 100755 --- a/examples/pytorch/token-classification/run_ner_no_trainer.py +++ b/examples/pytorch/token-classification/run_ner_no_trainer.py @@ -34,6 +34,7 @@ from tqdm.auto import tqdm import transformers from accelerate import Accelerator +from accelerate.logging import get_logger from accelerate.utils import set_seed from huggingface_hub import Repository from transformers import ( @@ -53,7 +54,7 @@ from transformers.utils import get_full_repo_name from transformers.utils.versions import require_version -logger = logging.getLogger(__name__) +logger = get_logger(__name__) require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/token-classification/requirements.txt") # You should update this to your particular problem to have better documentation of `model_type` @@ -253,11 +254,7 @@ def main(): datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, ) - logger.info(accelerator.state) - - # Setup logging, we only want one process per machine to log things on the screen. - # accelerator.is_local_main_process is only True for one process per machine. - logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR) + logger.info(accelerator.state, main_process_only=False) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() diff --git a/examples/pytorch/translation/run_translation_no_trainer.py b/examples/pytorch/translation/run_translation_no_trainer.py index f01267a288..1d93ae645e 100644 --- a/examples/pytorch/translation/run_translation_no_trainer.py +++ b/examples/pytorch/translation/run_translation_no_trainer.py @@ -35,6 +35,7 @@ from tqdm.auto import tqdm import transformers from accelerate import Accelerator +from accelerate.logging import get_logger from accelerate.utils import set_seed from huggingface_hub import Repository from transformers import ( @@ -55,7 +56,7 @@ from transformers.utils import get_full_repo_name from transformers.utils.versions import require_version -logger = logging.getLogger(__name__) +logger = get_logger(__name__) require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/translation/requirements.txt") # You should update this to your particular problem to have better documentation of `model_type` @@ -295,11 +296,7 @@ def main(): datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, ) - logger.info(accelerator.state) - - # Setup logging, we only want one process per machine to log things on the screen. - # accelerator.is_local_main_process is only True for one process per machine. - logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR) + logger.info(accelerator.state, main_process_only=False) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info()