




import argparse
import json
import logging
import os
import sys
from unittest import skip
from unittest.mock import patch

import tensorflow as tf

from transformers.testing_utils import TestCasePlus, get_gpu_count, slow


__a  :		Dict		     =   [
    os.path.join(os.path.dirname(__file__), dirname)
    for dirname in [
        '''text-generation''',
        '''text-classification''',
        '''token-classification''',
        '''language-modeling''',
        '''multiple-choice''',
        '''question-answering''',
        '''summarization''',
        '''translation''',
        '''image-classification''',
    ]
]
sys.path.extend(SRC_DIRS)


if SRC_DIRS is not None:
						import run_clm
						import run_image_classification
						import run_mlm
						import run_ner
						import run_qa as run_squad
						import run_summarization
						import run_swag
						import run_text_classification
						import run_translation


logging.basicConfig(level=logging.DEBUG)

__a  :		Any		     =   logging.getLogger()
def  snake_case_					(		)      ->			str:
							lowercase__  :	Optional[Any]        =							argparse.ArgumentParser()
							parser.add_argument("-f"    )
							lowercase__  :	str        =							parser.parse_args()
							return args.f
def  snake_case_					(		SCREAMING_SNAKE_CASE_    )      ->			List[str]:
							lowercase__  :	Any        =							{}
							lowercase__  :	List[str]        =							os.path.join(SCREAMING_SNAKE_CASE_    ,"all_results.json"    )
							if os.path.exists(SCREAMING_SNAKE_CASE_    ):
														with open(SCREAMING_SNAKE_CASE_    ,"r"    ) as f:
																					lowercase__  :	str        =							json.load(SCREAMING_SNAKE_CASE_    )
							else:
														raise ValueError(F"""can't find {path}"""    )
							return results
def  snake_case_					(		)      ->			List[str]:
							return bool(tf.config.list_physical_devices("GPU"    )    )


__a  :		Tuple		     =   logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)







class 				UpperCAmelCase(					snake_case_					):



	"""simple docstring"""






	@skip("Skipping until shape inference for to_tf_dataset PR is merged."    )
	def    __a     (					self    ) ->      str:





								"""simple docstring"""


								lowercase__  :	List[str]        =							self.get_auto_remove_tmp_dir()
								lowercase__  :	Optional[Any]        =							f"""
            run_text_classification.py
            --model_name_or_path distilbert-base-uncased
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv
            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv
            --do_train
            --do_eval
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --learning_rate=1e-4
            --max_steps=10
            --warmup_steps=2
            --seed=42
            --max_seq_length=128
            """.split()

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

								with patch.object(lowerCamelCase	,				"argv"	,				lowerCamelCase    ):
															run_text_classification.main()
															# Reset the mixed precision policy so we don't break other tests
															tf.keras.mixed_precision.set_global_policy("float32"    )
															lowercase__  :	Union[str, Any]        =							get_results(lowerCamelCase    )
															self.assertGreaterEqual(result["eval_accuracy"]	,				0.75    )

	def    __a     (					self    ) ->      Tuple:





								"""simple docstring"""


								lowercase__  :	Any        =							self.get_auto_remove_tmp_dir()
								lowercase__  :	Optional[Any]        =							f"""
            run_clm.py
            --model_name_or_path distilgpt2
            --train_file ./tests/fixtures/sample_text.txt
            --validation_file ./tests/fixtures/sample_text.txt
            --do_train
            --do_eval
            --block_size 128
            --per_device_train_batch_size 2
            --per_device_eval_batch_size 1
            --num_train_epochs 2
            --output_dir {tmp_dir}
            --overwrite_output_dir
            """.split()

								if len(tf.config.list_physical_devices("GPU"    )    ) > 1:
															# Skipping because there are not enough batches to train the model + would need a drop_last to work.
															return

								with patch.object(lowerCamelCase	,				"argv"	,				lowerCamelCase    ):
															run_clm.main()
															lowercase__  :	Any        =							get_results(lowerCamelCase    )
															self.assertLess(result["eval_perplexity"]	,				100    )

	def    __a     (					self    ) ->      List[str]:





								"""simple docstring"""


								lowercase__  :	List[Any]        =							self.get_auto_remove_tmp_dir()
								lowercase__  :	List[Any]        =							f"""
            run_mlm.py
            --model_name_or_path distilroberta-base
            --train_file ./tests/fixtures/sample_text.txt
            --validation_file ./tests/fixtures/sample_text.txt
            --max_seq_length 64
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --do_train
            --do_eval
            --prediction_loss_only
            --num_train_epochs=1
            --learning_rate=1e-4
        """.split()

								with patch.object(lowerCamelCase	,				"argv"	,				lowerCamelCase    ):
															run_mlm.main()
															lowercase__  :	int        =							get_results(lowerCamelCase    )
															self.assertLess(result["eval_perplexity"]	,				42    )

	def    __a     (					self    ) ->      Union[str, Any]:





								"""simple docstring"""


								lowercase__  :	Dict        =							7 if get_gpu_count() > 1 else 2

								lowercase__  :	Optional[int]        =							self.get_auto_remove_tmp_dir()
								lowercase__  :	Any        =							f"""
            run_ner.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}
            --overwrite_output_dir
            --do_train
            --do_eval
            --warmup_steps=2
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=2
            --num_train_epochs={epochs}
            --seed 7
        """.split()

								with patch.object(lowerCamelCase	,				"argv"	,				lowerCamelCase    ):
															run_ner.main()
															lowercase__  :	List[Any]        =							get_results(lowerCamelCase    )
															self.assertGreaterEqual(result["accuracy"]	,				0.75    )

	def    __a     (					self    ) ->      int:





								"""simple docstring"""


								lowercase__  :	Any        =							self.get_auto_remove_tmp_dir()
								lowercase__  :	int        =							f"""
            run_qa.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}
            --overwrite_output_dir
            --max_steps=10
            --warmup_steps=2
            --do_train
            --do_eval
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
        """.split()

								with patch.object(lowerCamelCase	,				"argv"	,				lowerCamelCase    ):
															run_squad.main()
															lowercase__  :	int        =							get_results(lowerCamelCase    )
															self.assertGreaterEqual(result["f1"]	,				30    )
															self.assertGreaterEqual(result["exact"]	,				30    )

	def    __a     (					self    ) ->      Optional[Any]:





								"""simple docstring"""


								lowercase__  :	Union[str, Any]        =							self.get_auto_remove_tmp_dir()
								lowercase__  :	Dict        =							f"""
            run_swag.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}
            --overwrite_output_dir
            --max_steps=20
            --warmup_steps=2
            --do_train
            --do_eval
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
        """.split()

								with patch.object(lowerCamelCase	,				"argv"	,				lowerCamelCase    ):
															run_swag.main()
															lowercase__  :	Any        =							get_results(lowerCamelCase    )
															self.assertGreaterEqual(result["val_accuracy"]	,				0.8    )

	@slow
	def    __a     (					self    ) ->      int:





								"""simple docstring"""


								lowercase__  :	Any        =							self.get_auto_remove_tmp_dir()
								lowercase__  :	Tuple        =							f"""
            run_summarization.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}
            --overwrite_output_dir
            --max_steps=50
            --warmup_steps=8
            --do_train
            --do_eval
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
        """.split()

								with patch.object(lowerCamelCase	,				"argv"	,				lowerCamelCase    ):
															run_summarization.main()
															lowercase__  :	List[Any]        =							get_results(lowerCamelCase    )
															self.assertGreaterEqual(result["rouge1"]	,				10    )
															self.assertGreaterEqual(result["rouge2"]	,				2    )
															self.assertGreaterEqual(result["rougeL"]	,				7    )
															self.assertGreaterEqual(result["rougeLsum"]	,				7    )

	@slow
	def    __a     (					self    ) ->      Optional[Any]:





								"""simple docstring"""


								lowercase__  :	List[str]        =							self.get_auto_remove_tmp_dir()
								lowercase__  :	Dict        =							f"""
            run_translation.py
            --model_name_or_path Rocketknight1/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}
            --overwrite_output_dir
            --warmup_steps=8
            --do_train
            --do_eval
            --learning_rate=3e-3
            --num_train_epochs 12
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --source_lang en_XX
            --target_lang ro_RO
        """.split()

								with patch.object(lowerCamelCase	,				"argv"	,				lowerCamelCase    ):
															run_translation.main()
															lowercase__  :	Dict        =							get_results(lowerCamelCase    )
															self.assertGreaterEqual(result["bleu"]	,				30    )







	def    __a     (					self    ) ->      List[str]:





								"""simple docstring"""


								lowercase__  :	str        =							self.get_auto_remove_tmp_dir()
								lowercase__  :	Dict        =							f"""
            run_image_classification.py
            --dataset_name hf-internal-testing/cats_vs_dogs_sample
            --model_name_or_path microsoft/resnet-18
            --do_train
            --do_eval
            --learning_rate 1e-4
            --per_device_train_batch_size 2
            --per_device_eval_batch_size 1
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --dataloader_num_workers 16
            --num_train_epochs 2
            --train_val_split 0.1
            --seed 42
            --ignore_mismatched_sizes True
            """.split()

								with patch.object(lowerCamelCase	,				"argv"	,				lowerCamelCase    ):
															run_image_classification.main()
															lowercase__  :	int        =							get_results(lowerCamelCase    )
															self.assertGreaterEqual(result["accuracy"]	,				0.7    )





import os
from shutil import copyfile
from typing import Optional, Tuple

from ...file_utils import is_sentencepiece_available
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging


if is_sentencepiece_available():
						from .tokenization_deberta_va import DebertaVaTokenizer
else:
						__a  :		Optional[Any]		     =   None

__a  :		List[str]		     =   logging.get_logger(__name__)

__a  :		Dict		     =   {'''vocab_file''': '''spm.model''', '''tokenizer_file''': '''tokenizer.json'''}

__a  :		int		     =   {
    '''vocab_file''': {
        '''microsoft/deberta-v2-xlarge''': '''https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/spm.model''',
        '''microsoft/deberta-v2-xxlarge''': '''https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/spm.model''',
        '''microsoft/deberta-v2-xlarge-mnli''': (
            '''https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/spm.model'''
        ),
        '''microsoft/deberta-v2-xxlarge-mnli''': (
            '''https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/spm.model'''
        ),
    }
}

__a  :		Any		     =   {
    '''microsoft/deberta-v2-xlarge''': 5_1_2,
    '''microsoft/deberta-v2-xxlarge''': 5_1_2,
    '''microsoft/deberta-v2-xlarge-mnli''': 5_1_2,
    '''microsoft/deberta-v2-xxlarge-mnli''': 5_1_2,
}

__a  :		Union[str, Any]		     =   {
    '''microsoft/deberta-v2-xlarge''': {'''do_lower_case''': False},
    '''microsoft/deberta-v2-xxlarge''': {'''do_lower_case''': False},
    '''microsoft/deberta-v2-xlarge-mnli''': {'''do_lower_case''': False},
    '''microsoft/deberta-v2-xxlarge-mnli''': {'''do_lower_case''': False},
}







class 				UpperCAmelCase(					snake_case_					):



	"""simple docstring"""






	a     : List[Any]	  =					VOCAB_FILES_NAMES
	a     : Optional[int]	  =					PRETRAINED_VOCAB_FILES_MAP
	a     : int	  =					PRETRAINED_INIT_CONFIGURATION
	a     : List[str]	  =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
	a     : int	  =					DebertaVaTokenizer

	def __init__(					self	,				lowerCamelCase=None	,				lowerCamelCase=None	,				lowerCamelCase=False	,				lowerCamelCase=False	,				lowerCamelCase="[CLS]"	,				lowerCamelCase="[SEP]"	,				lowerCamelCase="[UNK]"	,				lowerCamelCase="[SEP]"	,				lowerCamelCase="[PAD]"	,				lowerCamelCase="[CLS]"	,				lowerCamelCase="[MASK]"	,				**lowerCamelCase	,				) ->      None:





								"""simple docstring"""


								super().__init__(
								    lowerCamelCase	,				tokenizer_file=lowerCamelCase	,				do_lower_case=lowerCamelCase	,				bos_token=lowerCamelCase	,				eos_token=lowerCamelCase	,				unk_token=lowerCamelCase	,				sep_token=lowerCamelCase	,				pad_token=lowerCamelCase	,				cls_token=lowerCamelCase	,				mask_token=lowerCamelCase	,				split_by_punct=lowerCamelCase	,				**lowerCamelCase	,				)

								lowercase__  :	Optional[int]        =							do_lower_case
								lowercase__  :	Optional[Any]        =							split_by_punct
								lowercase__  :	Tuple        =							vocab_file
								lowercase__  :	Optional[int]        =							False if not self.vocab_file else True

	def    __a     (					self	,				lowerCamelCase	,				lowerCamelCase=None    ) ->      List[Any]:





								"""simple docstring"""


								if token_ids_a is None:
															return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
								lowercase__  :	Union[str, Any]        =							[self.cls_token_id]
								lowercase__  :	Union[str, Any]        =							[self.sep_token_id]
								return cls + token_ids_a + sep + token_ids_a + sep

	def    __a     (					self	,				lowerCamelCase	,				lowerCamelCase=None	,				lowerCamelCase=False    ) ->      Union[str, Any]:





								"""simple docstring"""


								if already_has_special_tokens:
															return super().get_special_tokens_mask(
															    token_ids_a=lowerCamelCase	,				token_ids_a=lowerCamelCase	,				already_has_special_tokens=lowerCamelCase    )

								if token_ids_a is not None:
															return [1] + ([0] * len(lowerCamelCase    )) + [1] + ([0] * len(lowerCamelCase    )) + [1]
								return [1] + ([0] * len(lowerCamelCase    )) + [1]

	def    __a     (					self	,				lowerCamelCase	,				lowerCamelCase=None    ) ->      Optional[int]:





								"""simple docstring"""


								lowercase__  :	Any        =							[self.sep_token_id]
								lowercase__  :	List[str]        =							[self.cls_token_id]
								if token_ids_a is None:
															return len(cls + token_ids_a + sep    ) * [0]
								return len(cls + token_ids_a + sep    ) * [0] + len(token_ids_a + sep    ) * [1]







	def    __a     (					self	,				lowerCamelCase	,				lowerCamelCase = None    ) ->      Tuple[str]:





								"""simple docstring"""


								if not self.can_save_slow_tokenizer:
															raise ValueError(
															    "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow "
															    "tokenizer."    )

								if not os.path.isdir(lowerCamelCase    ):
															logger.error(f"""Vocabulary path ({save_directory}) should be a directory"""    )
															return
								lowercase__  :	int        =							os.path.join(
								    lowerCamelCase	,				(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]    )

								if os.path.abspath(self.vocab_file    ) != os.path.abspath(lowerCamelCase    ):
															copyfile(self.vocab_file	,				lowerCamelCase    )

								return (out_vocab_file,)