





'''simple docstring'''


import argparse
import json
import logging
import os
import shutil
import sys
import tempfile
import unittest
from unittest import mock

import torch
from accelerate.utils import write_basic_config

from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device
from transformers.utils import is_apex_available


logging.basicConfig(level=logging.DEBUG)

_SCREAMING_SNAKE_CASE	 =							logging.getLogger()


def 	__a():



				'''simple docstring'''





				_lowerCAmelCase				=      argparse.ArgumentParser()
				parser.add_argument("-f"    )
				_lowerCAmelCase				=      parser.parse_args()
				return args.f


def 	__a(SCREAMING_SNAKE_CASE_  :					Optional[Any]    ):



				'''simple docstring'''





				_lowerCAmelCase				=      {}
				_lowerCAmelCase				=      os.path.join(lowercase__		,       "all_results.json"    )
				if os.path.exists(lowercase__    ):
								with open(lowercase__		,       "r"    ) as f:
												_lowerCAmelCase				=      json.load(lowercase__    )
				else:
								raise ValueError(F'''can\'t find {path}'''    )
				return results


def 	__a():



				'''simple docstring'''





				_lowerCAmelCase				=      torch.cuda.is_available() and torch_device == "cuda"
				return is_using_cuda and is_apex_available()


_SCREAMING_SNAKE_CASE	 =							logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)





class    lowerCAmelCase_		(		__snake_case		):




	@classmethod
	def 							_snake_case   (	cls		)    ->							Dict:
					_lowerCAmelCase				=      tempfile.mkdtemp()
					_lowerCAmelCase				=      os.path.join(cls.tmpdir			,     "default_config.yml"		)
					write_basic_config(save_location=cls.configPath		)
					_lowerCAmelCase				=      ["accelerate", "launch", "--config_file", cls.configPath]




	@classmethod
	def 							_snake_case   (	cls		)    ->							Tuple:
					shutil.rmtree(cls.tmpdir		)




	@mock.patch.dict(os.environ			,     {"WANDB_MODE": "offline"}		)
	def 							_snake_case   (	self		)    ->							int:
					_lowerCAmelCase				=      self.get_auto_remove_tmp_dir()
					_lowerCAmelCase				=      f'''
            {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.py
            --model_name_or_path distilbert-base-uncased
            --output_dir {tmp_dir}
            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv
            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --learning_rate=1e-4
            --seed=42
            --checkpointing_steps epoch
            --with_tracking
        '''.split()

					if is_cuda_and_apex_available():
									testargs.append("--fp16"		)

					run_command(self._launch_args + testargs		)
					_lowerCAmelCase				=      get_results(__UpperCamelCase		)
					self.assertGreaterEqual(result["eval_accuracy"]			,     0.75		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "epoch_0"		)		)		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "glue_no_trainer"		)		)		)




	@mock.patch.dict(os.environ			,     {"WANDB_MODE": "offline"}		)
	def 							_snake_case   (	self		)    ->							List[Any]:
					_lowerCAmelCase				=      self.get_auto_remove_tmp_dir()
					_lowerCAmelCase				=      f'''
            {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py
            --model_name_or_path distilgpt2
            --train_file ./tests/fixtures/sample_text.txt
            --validation_file ./tests/fixtures/sample_text.txt
            --block_size 128
            --per_device_train_batch_size 5
            --per_device_eval_batch_size 5
            --num_train_epochs 2
            --output_dir {tmp_dir}
            --checkpointing_steps epoch
            --with_tracking
        '''.split()

					if torch.cuda.device_count() > 1:
									# Skipping because there are not enough batches to train the model + would need a drop_last to work.
									return

					run_command(self._launch_args + testargs		)
					_lowerCAmelCase				=      get_results(__UpperCamelCase		)
					self.assertLess(result["perplexity"]			,     100		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "epoch_0"		)		)		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "clm_no_trainer"		)		)		)




	@mock.patch.dict(os.environ			,     {"WANDB_MODE": "offline"}		)
	def 							_snake_case   (	self		)    ->							str:
					_lowerCAmelCase				=      self.get_auto_remove_tmp_dir()
					_lowerCAmelCase				=      f'''
            {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.py
            --model_name_or_path distilroberta-base
            --train_file ./tests/fixtures/sample_text.txt
            --validation_file ./tests/fixtures/sample_text.txt
            --output_dir {tmp_dir}
            --num_train_epochs=1
            --checkpointing_steps epoch
            --with_tracking
        '''.split()

					run_command(self._launch_args + testargs		)
					_lowerCAmelCase				=      get_results(__UpperCamelCase		)
					self.assertLess(result["perplexity"]			,     42		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "epoch_0"		)		)		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "mlm_no_trainer"		)		)		)




	@mock.patch.dict(os.environ			,     {"WANDB_MODE": "offline"}		)
	def 							_snake_case   (	self		)    ->							List[str]:
					_lowerCAmelCase				=      7 if get_gpu_count() > 1 else 2

					_lowerCAmelCase				=      self.get_auto_remove_tmp_dir()
					_lowerCAmelCase				=      f'''
            {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.py
            --model_name_or_path bert-base-uncased
            --train_file tests/fixtures/tests_samples/conll/sample.json
            --validation_file tests/fixtures/tests_samples/conll/sample.json
            --output_dir {tmp_dir}
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=2
            --num_train_epochs={epochs}
            --seed 7
            --checkpointing_steps epoch
            --with_tracking
        '''.split()

					run_command(self._launch_args + testargs		)
					_lowerCAmelCase				=      get_results(__UpperCamelCase		)
					self.assertGreaterEqual(result["eval_accuracy"]			,     0.75		)
					self.assertLess(result["train_loss"]			,     0.5		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "epoch_0"		)		)		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "ner_no_trainer"		)		)		)




	@unittest.skip(reason="Fix me @muellerzr"		)
	@mock.patch.dict(os.environ			,     {"WANDB_MODE": "offline"}		)
	def 							_snake_case   (	self		)    ->							List[str]:
					_lowerCAmelCase				=      self.get_auto_remove_tmp_dir()
					_lowerCAmelCase				=      f'''
            {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.py
            --model_name_or_path bert-base-uncased
            --version_2_with_negative
            --train_file tests/fixtures/tests_samples/SQUAD/sample.json
            --validation_file tests/fixtures/tests_samples/SQUAD/sample.json
            --output_dir {tmp_dir}
            --seed=42
            --max_train_steps=10
            --num_warmup_steps=2
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --checkpointing_steps epoch
            --with_tracking
        '''.split()

					run_command(self._launch_args + testargs		)
					_lowerCAmelCase				=      get_results(__UpperCamelCase		)
					# Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics.
					self.assertGreaterEqual(result["eval_f1"]			,     28		)
					self.assertGreaterEqual(result["eval_exact"]			,     28		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "epoch_0"		)		)		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "qa_no_trainer"		)		)		)




	@mock.patch.dict(os.environ			,     {"WANDB_MODE": "offline"}		)
	def 							_snake_case   (	self		)    ->							str:
					_lowerCAmelCase				=      self.get_auto_remove_tmp_dir()
					_lowerCAmelCase				=      f'''
            {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py
            --model_name_or_path bert-base-uncased
            --train_file tests/fixtures/tests_samples/swag/sample.json
            --validation_file tests/fixtures/tests_samples/swag/sample.json
            --output_dir {tmp_dir}
            --max_train_steps=20
            --num_warmup_steps=2
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --with_tracking
        '''.split()

					run_command(self._launch_args + testargs		)
					_lowerCAmelCase				=      get_results(__UpperCamelCase		)
					self.assertGreaterEqual(result["eval_accuracy"]			,     0.8		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "swag_no_trainer"		)		)		)




	@slow
	@mock.patch.dict(os.environ			,     {"WANDB_MODE": "offline"}		)
	def 							_snake_case   (	self		)    ->							Union[str, Any]:
					_lowerCAmelCase				=      self.get_auto_remove_tmp_dir()
					_lowerCAmelCase				=      f'''
            {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.py
            --model_name_or_path t5-small
            --train_file tests/fixtures/tests_samples/xsum/sample.json
            --validation_file tests/fixtures/tests_samples/xsum/sample.json
            --output_dir {tmp_dir}
            --max_train_steps=50
            --num_warmup_steps=8
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --checkpointing_steps epoch
            --with_tracking
        '''.split()

					run_command(self._launch_args + testargs		)
					_lowerCAmelCase				=      get_results(__UpperCamelCase		)
					self.assertGreaterEqual(result["eval_rouge1"]			,     10		)
					self.assertGreaterEqual(result["eval_rouge2"]			,     2		)
					self.assertGreaterEqual(result["eval_rougeL"]			,     7		)
					self.assertGreaterEqual(result["eval_rougeLsum"]			,     7		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "epoch_0"		)		)		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "summarization_no_trainer"		)		)		)




	@slow
	@mock.patch.dict(os.environ			,     {"WANDB_MODE": "offline"}		)
	def 							_snake_case   (	self		)    ->							Optional[int]:
					_lowerCAmelCase				=      self.get_auto_remove_tmp_dir()
					_lowerCAmelCase				=      f'''
            {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py
            --model_name_or_path sshleifer/student_marian_en_ro_6_1
            --source_lang en
            --target_lang ro
            --train_file tests/fixtures/tests_samples/wmt16/sample.json
            --validation_file tests/fixtures/tests_samples/wmt16/sample.json
            --output_dir {tmp_dir}
            --max_train_steps=50
            --num_warmup_steps=8
            --num_beams=6
            --learning_rate=3e-3
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --source_lang en_XX
            --target_lang ro_RO
            --checkpointing_steps epoch
            --with_tracking
        '''.split()

					run_command(self._launch_args + testargs		)
					_lowerCAmelCase				=      get_results(__UpperCamelCase		)
					self.assertGreaterEqual(result["eval_bleu"]			,     30		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "epoch_0"		)		)		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "translation_no_trainer"		)		)		)




	@slow
	def 							_snake_case   (	self		)    ->							Tuple:
					_lowerCAmelCase				=      logging.StreamHandler(sys.stdout		)
					logger.addHandler(__UpperCamelCase		)

					_lowerCAmelCase				=      self.get_auto_remove_tmp_dir()
					_lowerCAmelCase				=      f'''
            {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py
            --dataset_name huggingface/semantic-segmentation-test-sample
            --output_dir {tmp_dir}
            --max_train_steps=10
            --num_warmup_steps=2
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --checkpointing_steps epoch
        '''.split()

					run_command(self._launch_args + testargs		)
					_lowerCAmelCase				=      get_results(__UpperCamelCase		)
					self.assertGreaterEqual(result["eval_overall_accuracy"]			,     0.10		)





	@mock.patch.dict(os.environ			,     {"WANDB_MODE": "offline"}		)
	def 							_snake_case   (	self		)    ->							str:
					_lowerCAmelCase				=      self.get_auto_remove_tmp_dir()
					_lowerCAmelCase				=      f'''
            {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py
            --model_name_or_path google/vit-base-patch16-224-in21k
            --dataset_name hf-internal-testing/cats_vs_dogs_sample
            --learning_rate 1e-4
            --per_device_train_batch_size 2
            --per_device_eval_batch_size 1
            --max_train_steps 2
            --train_val_split 0.1
            --seed 42
            --output_dir {tmp_dir}
            --with_tracking
            --checkpointing_steps 1
        '''.split()

					if is_cuda_and_apex_available():
									testargs.append("--fp16"		)

					run_command(self._launch_args + testargs		)
					_lowerCAmelCase				=      get_results(__UpperCamelCase		)
					# The base model scores a 25%
					self.assertGreaterEqual(result["eval_accuracy"]			,     0.6		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "step_1"		)		)		)
					self.assertTrue(os.path.exists(os.path.join(__UpperCamelCase			,     "image_classification_no_trainer"		)		)		)









'''simple docstring'''


import argparse
import collections
import os
import re

from transformers.utils import direct_transformers_import


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_table.py
_SCREAMING_SNAKE_CASE	 =							"src/transformers"
_SCREAMING_SNAKE_CASE	 =							"docs/source/en"
_SCREAMING_SNAKE_CASE	 =							"."


def 	__a(SCREAMING_SNAKE_CASE_  :					int		,       SCREAMING_SNAKE_CASE_  :					Tuple		,       SCREAMING_SNAKE_CASE_  :					Union[str, Any]    ):



    '''simple docstring'''





    with open(SCREAMING_SNAKE_CASE_		,       "r"		,       encoding="utf-8"		,       newline="\n"    ) as f:
        _lowerCAmelCase				=      f.readlines()
    # Find the start prompt.
    _lowerCAmelCase				=      0
    while not lines[start_index].startswith(SCREAMING_SNAKE_CASE_    ):
        start_index += 1
    start_index += 1

    _lowerCAmelCase				=      start_index
    while not lines[end_index].startswith(SCREAMING_SNAKE_CASE_    ):
        end_index += 1
    end_index -= 1

    while len(lines[start_index]    ) <= 1:
        start_index += 1
    while len(lines[end_index]    ) <= 1:
        end_index -= 1
    end_index += 1
    return "".join(lines[start_index:end_index]    ), start_index, end_index, lines


# Add here suffixes that are used to identify models, separated by |
_SCREAMING_SNAKE_CASE	 =							"Model|Encoder|Decoder|ForConditionalGeneration"
# Regexes that match TF/Flax/PT model names.
_SCREAMING_SNAKE_CASE	 =							re.compile(r"TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)")
_SCREAMING_SNAKE_CASE	 =							re.compile(r"Flax(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)")
# Will match any TF or Flax model too so need to be in an else branch afterthe two previous regexes.
_SCREAMING_SNAKE_CASE	 =							re.compile(r"(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)")


# This is to make sure the transformers module imported is the one in the repo.
_SCREAMING_SNAKE_CASE	 =							direct_transformers_import(TRANSFORMERS_PATH)


def 	__a(SCREAMING_SNAKE_CASE_  :					Union[str, Any]    ):



    '''simple docstring'''





    _lowerCAmelCase				=      re.finditer(".+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)"		,       SCREAMING_SNAKE_CASE_    )
    return [m.group(0    ) for m in matches]


def 	__a(SCREAMING_SNAKE_CASE_  :					Dict		,       SCREAMING_SNAKE_CASE_  :					int    ):



    '''simple docstring'''





    _lowerCAmelCase				=      2 if text == "✅" or text == "❌" else len(SCREAMING_SNAKE_CASE_    )
    _lowerCAmelCase				=      (width - text_length) // 2
    _lowerCAmelCase				=      width - text_length - left_indent
    return " " * left_indent + text + " " * right_indent


def 	__a():



    '''simple docstring'''





    _lowerCAmelCase				=      transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES
    _lowerCAmelCase				=      {
        name: config_maping_names[code]
        for code, name in transformers_module.MODEL_NAMES_MAPPING.items()
        if code in config_maping_names
    }
    _lowerCAmelCase				=      {name: config.replace("Config"		,       ""    ) for name, config in model_name_to_config.items()}

    # Dictionaries flagging if each model prefix has a slow/fast tokenizer, backend in PT/TF/Flax.
    _lowerCAmelCase				=      collections.defaultdict(SCREAMING_SNAKE_CASE_    )
    _lowerCAmelCase				=      collections.defaultdict(SCREAMING_SNAKE_CASE_    )
    _lowerCAmelCase				=      collections.defaultdict(SCREAMING_SNAKE_CASE_    )
    _lowerCAmelCase				=      collections.defaultdict(SCREAMING_SNAKE_CASE_    )
    _lowerCAmelCase				=      collections.defaultdict(SCREAMING_SNAKE_CASE_    )

    # Let's lookup through all transformers object (once).
    for attr_name in dir(SCREAMING_SNAKE_CASE_    ):
        _lowerCAmelCase				=      None
        if attr_name.endswith("Tokenizer"    ):
            _lowerCAmelCase				=      slow_tokenizers
            _lowerCAmelCase				=      attr_name[:-9]
        elif attr_name.endswith("TokenizerFast"    ):
            _lowerCAmelCase				=      fast_tokenizers
            _lowerCAmelCase				=      attr_name[:-13]
        elif _re_tf_models.match(SCREAMING_SNAKE_CASE_    ) is not None:
            _lowerCAmelCase				=      tf_models
            _lowerCAmelCase				=      _re_tf_models.match(SCREAMING_SNAKE_CASE_    ).groups()[0]
        elif _re_flax_models.match(SCREAMING_SNAKE_CASE_    ) is not None:
            _lowerCAmelCase				=      flax_models
            _lowerCAmelCase				=      _re_flax_models.match(SCREAMING_SNAKE_CASE_    ).groups()[0]
        elif _re_pt_models.match(SCREAMING_SNAKE_CASE_    ) is not None:
            _lowerCAmelCase				=      pt_models
            _lowerCAmelCase				=      _re_pt_models.match(SCREAMING_SNAKE_CASE_    ).groups()[0]

        if lookup_dict is not None:
            while len(SCREAMING_SNAKE_CASE_    ) > 0:
                if attr_name in model_name_to_prefix.values():
                    _lowerCAmelCase				=      True
                    break
                # Try again after removing the last word in the name
                _lowerCAmelCase				=      "".join(camel_case_split(SCREAMING_SNAKE_CASE_    )[:-1]    )

    # Let's build that table!
    _lowerCAmelCase				=      list(model_name_to_config.keys()    )
    model_names.sort(key=str.lower    )
    _lowerCAmelCase				=      ["Model", "Tokenizer slow", "Tokenizer fast", "PyTorch support", "TensorFlow support", "Flax Support"]
    # We'll need widths to properly display everything in the center (+2 is to leave one extra space on each side).
    _lowerCAmelCase				=      [len(SCREAMING_SNAKE_CASE_    ) + 2 for c in columns]
    _lowerCAmelCase				=      max([len(SCREAMING_SNAKE_CASE_    ) for name in model_names]    ) + 2

    # Build the table per se
    _lowerCAmelCase				=      "|" + "|".join([_center_text(SCREAMING_SNAKE_CASE_		,       SCREAMING_SNAKE_CASE_    ) for c, w in zip(SCREAMING_SNAKE_CASE_		,       SCREAMING_SNAKE_CASE_    )]    ) + "|\n"
    # Use ":-----:" format to center-aligned table cell texts
    table += "|" + "|".join([":" + "-" * (w - 2) + ":" for w in widths]    ) + "|\n"

    _lowerCAmelCase				=      {True: "✅", False: "❌"}
    for name in model_names:
        _lowerCAmelCase				=      model_name_to_prefix[name]
        _lowerCAmelCase				=      [
            name,
            check[slow_tokenizers[prefix]],
            check[fast_tokenizers[prefix]],
            check[pt_models[prefix]],
            check[tf_models[prefix]],
            check[flax_models[prefix]],
        ]
        table += "|" + "|".join([_center_text(SCREAMING_SNAKE_CASE_		,       SCREAMING_SNAKE_CASE_    ) for l, w in zip(SCREAMING_SNAKE_CASE_		,       SCREAMING_SNAKE_CASE_    )]    ) + "|\n"
    return table


def 	__a(SCREAMING_SNAKE_CASE_  :					Union[str, Any]=False    ):



    '''simple docstring'''





    _lowerCAmelCase			,			_lowerCAmelCase			,			_lowerCAmelCase			,			_lowerCAmelCase				=      _find_text_in_file(
        filename=os.path.join(SCREAMING_SNAKE_CASE_		,       "index.md"    )		,       start_prompt="<!--This table is updated automatically from the auto modules"		,       end_prompt="<!-- End table-->"		,       )
    _lowerCAmelCase				=      get_model_table_from_auto_modules()

    if current_table != new_table:
        if overwrite:
            with open(os.path.join(SCREAMING_SNAKE_CASE_		,       "index.md"    )		,       "w"		,       encoding="utf-8"		,       newline="\n"    ) as f:
                f.writelines(lines[:start_index] + [new_table] + lines[end_index:]    )
        else:
            raise ValueError(
                "The model table in the `index.md` has not been updated. Run `make fix-copies` to fix this."    )


if __name__ == "__main__":
       _SCREAMING_SNAKE_CASE	 =							argparse.ArgumentParser()
       parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.")
       _SCREAMING_SNAKE_CASE	 =							parser.parse_args()

       check_model_table(args.fix_and_overwrite)


