

'''simple docstring'''

# This test is meant to be run in on an instance with TPUs like this:
#
#   python examples/pytorch/xla_spawn.py --num_cores=8 tests/test_trainer_tpu.py
#
# Replace 8 with the number of TPU cores you have.
#

import sys
from typing import Dict

from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available
from transformers.utils import logging


lowerCAmelCase_     =      logging.get_logger(__name__)


if is_torch_available():
						import torch
						from torch import nn
						from torch.utils.data import Dataset

						from transformers import Trainer







						class lowerCamelCase   (    __lowerCAmelCase				):

										def __init__(       self,						lowercase_ = 101 )    ->							Optional[Any]:
											snake_case							=					length

										def __len__(       self )    ->							str:
											return self.length






										def __getitem__(       self,						lowercase_ )    ->							int:
											return i







						class lowerCamelCase   :

										def __call__(       self,						lowercase_ )    ->							Optional[int]:
											return {"input_ids": torch.tensor(lowercase_ ), "labels": torch.tensor(lowercase_ )}







						class lowerCamelCase   (    nn.Module				):

										def __init__(       self )    ->							List[str]:
											super().__init__()
											# Add some (unused) params otherwise DDP will complain.
											snake_case							=					nn.Linear(120,						80 )






										def 					_lowerCamelCase    (       self,						lowercase_,						lowercase_=None )    ->							Any:
											if labels is not None:
												return torch.tensor(0.0,						device=input_ids.device ), input_ids
											else:
												return input_ids

def      __magic_name__							(			)      ->		List[str]:
	snake_case							=					HfArgumentParser((TrainingArguments,)				)
	sys.argv += ["--output_dir", "./examples"]
	snake_case							=					parser.parse_args_into_dataclasses()[0]

	logger.warning(
	    F'''Process rank: {training_args.local_rank}, device: {training_args.device}, '''
	    F'''tpu_num_cores: {training_args.tpu_num_cores}'''       , )

	# Essentially, what we want to verify in the distributed case is
	# that we get all samples back, in the right order.
	# (this is crucial for prediction for instance)
	for dataset_length in [1_0_0_1, 2_5_6, 1_5]:
		snake_case							=					DummyDataset(A				)

		def compute_metrics(A				) -> Dict:
			snake_case							=					list(range(len(A				)				)				)
			snake_case							=					p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential
			return {"success": success}

		snake_case							=					Trainer(
		    model=DummyModel()       , args=A       , data_collator=DummyDataCollator()       , eval_dataset=A       , compute_metrics=A       , )
		snake_case							=					trainer.evaluate()
		logger.info(A				)
		if metrics["eval_success"] is not True:
			logger.error(A				)
			exit(1				)

		snake_case							=					trainer.predict(A				)
		logger.info(p.metrics				)
		if p.metrics["test_success"] is not True:
			logger.error(p.metrics				)
			exit(1				)

		snake_case							=					2

		snake_case							=					trainer.evaluate()
		logger.info(A				)
		if metrics["eval_success"] is not True:
			logger.error(A				)
			exit(1				)

		snake_case							=					trainer.predict(A				)
		logger.info(p.metrics				)
		if p.metrics["test_success"] is not True:
			logger.error(p.metrics				)
			exit(1				)

		snake_case							=					None

	logger.info('🔥 All distributed tests successful'				)

def      __magic_name__							(			A				)      ->		Optional[int]:
	# For xla_spawn (TPUs)
	main()


if __name__ == "__main__":
						main()








from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


A__							=				logging.get_logger(__name__)

A__							=				{
    "nghuyong/ernie-1.0-base-zh": "https://huggingface.co/nghuyong/ernie-1.0-base-zh/resolve/main/config.json",
    "nghuyong/ernie-2.0-base-en": "https://huggingface.co/nghuyong/ernie-2.0-base-en/resolve/main/config.json",
    "nghuyong/ernie-2.0-large-en": "https://huggingface.co/nghuyong/ernie-2.0-large-en/resolve/main/config.json",
    "nghuyong/ernie-3.0-base-zh": "https://huggingface.co/nghuyong/ernie-3.0-base-zh/resolve/main/config.json",
    "nghuyong/ernie-3.0-medium-zh": "https://huggingface.co/nghuyong/ernie-3.0-medium-zh/resolve/main/config.json",
    "nghuyong/ernie-3.0-mini-zh": "https://huggingface.co/nghuyong/ernie-3.0-mini-zh/resolve/main/config.json",
    "nghuyong/ernie-3.0-micro-zh": "https://huggingface.co/nghuyong/ernie-3.0-micro-zh/resolve/main/config.json",
    "nghuyong/ernie-3.0-nano-zh": "https://huggingface.co/nghuyong/ernie-3.0-nano-zh/resolve/main/config.json",
    "nghuyong/ernie-gram-zh": "https://huggingface.co/nghuyong/ernie-gram-zh/resolve/main/config.json",
    "nghuyong/ernie-health-zh": "https://huggingface.co/nghuyong/ernie-health-zh/resolve/main/config.json",
}

class 					__UpperCamelCase					(       SCREAMING_SNAKE_CASE      ):
	_lowercase	: Dict												=	"ernie"







	def __init__( self:			List[Any]	,			__UpperCamelCase:			Dict=3_05_22	,			__UpperCamelCase:			str=7_68	,			__UpperCamelCase:			Tuple=12	,			__UpperCamelCase:			int=12	,			__UpperCamelCase:			Optional[Any]=30_72	,			__UpperCamelCase:			str="gelu"	,			__UpperCamelCase:			Any=0.1	,			__UpperCamelCase:			int=0.1	,			__UpperCamelCase:			int=5_12	,			__UpperCamelCase:			Any=2	,			__UpperCamelCase:			Tuple=3	,			__UpperCamelCase:			Any=False	,			__UpperCamelCase:			Optional[int]=0.02	,			__UpperCamelCase:			List[Any]=1E-12	,			__UpperCamelCase:			Union[str, Any]=0	,			__UpperCamelCase:			Tuple="absolute"	,			__UpperCamelCase:			Union[str, Any]=True	,			__UpperCamelCase:			Optional[Any]=None	,			**__UpperCamelCase:			List[Any]	,			):

						'''simple docstring'''





						super().__init__(pad_token_id=__UpperCamelCase	,			**__UpperCamelCase   )

						__magic_name__				=     vocab_size
						__magic_name__				=     hidden_size
						__magic_name__				=     num_hidden_layers
						__magic_name__				=     num_attention_heads
						__magic_name__				=     hidden_act
						__magic_name__				=     intermediate_size
						__magic_name__				=     hidden_dropout_prob
						__magic_name__				=     attention_probs_dropout_prob
						__magic_name__				=     max_position_embeddings
						__magic_name__				=     type_vocab_size
						__magic_name__				=     task_type_vocab_size
						__magic_name__				=     use_task_id
						__magic_name__				=     initializer_range
						__magic_name__				=     layer_norm_eps
						__magic_name__				=     position_embedding_type
						__magic_name__				=     use_cache
						__magic_name__				=     classifier_dropout




class 					__UpperCamelCase					(       SCREAMING_SNAKE_CASE      ):







	@property
	def 						_SCREAMING_SNAKE_CASE				( self:			Tuple   ):

						'''simple docstring'''





						if self.task == "multiple-choice":
											__magic_name__				=     {0: 'batch', 1: 'choice', 2: 'sequence'}
						else:
											__magic_name__				=     {0: 'batch', 1: 'sequence'}
						return OrderedDict(
						    [
						        ('input_ids', dynamic_axis),
						        ('attention_mask', dynamic_axis),
						        ('token_type_ids', dynamic_axis),
						        ('task_type_ids', dynamic_axis),
						    ]   )




