




# 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							    =	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		,		snake_case_ = 101							)    ->      List[Any]:
														SCREAMING_SNAKE_CASE			: str	     =length




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







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







			class 							_lowerCAmelCase		:




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







			class 							_lowerCAmelCase		(     nn.Module	):




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







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






														if labels is not None:
																					return torch.tensor(0.0		,		device=input_ids.device							), input_ids
														else:
																					return input_ids
def 						lowerCAmelCase_       (	)       ->			Optional[Any]:





							"""simple docstring"""





							SCREAMING_SNAKE_CASE			: Dict	     =HfArgumentParser((TrainingArguments,) )
							sys.argv += ["--output_dir", "./examples"]
							SCREAMING_SNAKE_CASE			: List[str]	     =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 [1001, 256, 15]:
														SCREAMING_SNAKE_CASE			: Dict	     =DummyDataset(_A )

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

														SCREAMING_SNAKE_CASE			: Union[str, Any]	     =Trainer(
														    model=DummyModel()	,	args=_A	,	data_collator=DummyDataCollator()	,	eval_dataset=_A	,	compute_metrics=_A	,	)
														SCREAMING_SNAKE_CASE			: Tuple	     =trainer.evaluate()
														logger.info(_A )
														if metrics["eval_success"] is not True:
																					logger.error(_A )
																					exit(1 )

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

														SCREAMING_SNAKE_CASE			: Dict	     =2

														SCREAMING_SNAKE_CASE			: List[Any]	     =trainer.evaluate()
														logger.info(_A )
														if metrics["eval_success"] is not True:
																					logger.error(_A )
																					exit(1 )

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

														SCREAMING_SNAKE_CASE			: List[Any]	     =None

							logger.info('''🔥 All distributed tests successful''' )
def 						lowerCAmelCase_       (	__a )       ->			List[Any]:





							"""simple docstring"""





							main()


if __name__ == "__main__":
			main()












def 						lowerCAmelCase_       (	__a = "mytext.txt"	,	__a = False )       ->			None:





							"""simple docstring"""





							import socket

							SCREAMING_SNAKE_CASE			: int	     =12312  # Reserve a port for your service.
							SCREAMING_SNAKE_CASE			: Dict	     =socket.socket()  # Create a socket object
							SCREAMING_SNAKE_CASE			: Any	     =socket.gethostname()  # Get local machine name
							sock.bind((host, port) )  # Bind to the port
							sock.listen(5 )  # Now wait for client connection.

							print('''Server listening....''' )

							while True:
														SCREAMING_SNAKE_CASE     ,							SCREAMING_SNAKE_CASE			: Optional[int]	     =sock.accept()  # Establish connection with client.
														print(f'Got connection from {addr}' )
														SCREAMING_SNAKE_CASE			: List[str]	     =conn.recv(1024 )
														print(f'Server received: {data = }' )

														with open(__a	,	'''rb''' ) as in_file:
																					SCREAMING_SNAKE_CASE			: Dict	     =in_file.read(1024 )
																					while data:
																												conn.send(__a )
																												print(f'Sent {data!r}' )
																												SCREAMING_SNAKE_CASE			: str	     =in_file.read(1024 )

														print('''Done sending''' )
														conn.close()
														if testing:  # Allow the test to complete
																					break

							sock.shutdown(1 )
							sock.close()


if __name__ == "__main__":
							send_file()






