




# 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


__a			:	Dict							     =		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     A					(				lowerCamelCase_       ):







											def __init__(    self   :	Union[str, Any]   ,     __UpperCAmelCase   :	int = 101				)		->					Union[str, Any]:

														"""simple docstring"""



														UpperCamelCase_     				= length







											def __len__(    self   :	Any				)		->					str:

														"""simple docstring"""



														return self.length







											def __getitem__(    self   :	Dict   ,     __UpperCAmelCase   :	Tuple				)		->					int:

														"""simple docstring"""



														return i







				class     A					:







											def __call__(    self   :	Any   ,     __UpperCAmelCase   :	Optional[Any]				)		->					Optional[Any]:

														"""simple docstring"""



														return {"input_ids": torch.tensor(__UpperCAmelCase				), "labels": torch.tensor(__UpperCAmelCase				)}







				class     A					(				nn.Module       ):







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

														"""simple docstring"""



														super().__init__()
														# Add some (unused) params otherwise DDP will complain.
														UpperCamelCase_     				= nn.Linear(120   ,     80				)







											def        lowercase__							(    self   :	Optional[int]   ,     __UpperCAmelCase   :	Any   ,     __UpperCAmelCase   :	Union[str, Any]=None				)		->					Any:

														"""simple docstring"""



														if labels is not None:
																	return torch.tensor(0.0   ,     device=input_ids.device				), input_ids
														else:
																	return input_ids




def  a_		(							)	->			Any:

			'''simple docstring'''


			UpperCamelCase_     				= HfArgumentParser((TrainingArguments,)					)
			sys.argv += ["--output_dir", "./examples"]
			UpperCamelCase_     				= 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]:
						UpperCamelCase_     				= DummyDataset(__snake_case					)

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

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

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

						UpperCamelCase_     				= 2

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

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

						UpperCamelCase_     				= None

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




def  a_		(							__snake_case					)	->			List[Any]:

			'''simple docstring'''


			main()


if __name__ == "__main__":
				main()






from collections import Counter




def  a_		(							__snake_case					)	->			int:

			'''simple docstring'''


			return sum(socks_by_color // 2 for socks_by_color in Counter(__snake_case					).values()					)


if __name__ == "__main__":
				import doctest

				doctest.testmod()

				__a			:	List[Any]							     =		[int(x) for x in input("""Enter socks by color :""").rstrip().split()]
				print(f"""sock_merchant({colors}) = {sock_merchant(colors)}""")
