


# 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


snake_case       :					Optional[Any]        =  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 	_snake_case						(   lowerCamelCase__							):




						def __init__(	self      ,						_lowerCamelCase = 101					):
												a		:str											= length




						def __len__(	self					):
												return self.length




						def __getitem__(	self      ,						_lowerCamelCase					):
												return i







					class 	_snake_case						:




						def __call__(	self      ,						_lowerCamelCase					):
												return {"input_ids": torch.tensor(lowercase_					), "labels": torch.tensor(lowercase_					)}







					class 	_snake_case						(   nn.Module							):




						def __init__(	self					):
												super().__init__()
												# Add some (unused) params otherwise DDP will complain.
												a		:Tuple											= nn.Linear(120      ,						80					)




						def  SCREAMING_SNAKE_CASE__       (	self      ,						_lowerCamelCase      ,						_lowerCamelCase=None					):






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




						"""simple docstring"""





						a		:Union[str, Any]											= HfArgumentParser((TrainingArguments,)		)
						sys.argv += ["--output_dir", "./examples"]
						a		:Dict											= 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]:
												a		:Union[str, Any]											= DummyDataset(_A		)

												def compute_metrics(UpperCAmelCase_		:	EvalPrediction		) -> Dict:
																		a		:Dict											= list(range(len(_A		)		)		)
																		a		:int											= p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential
																		return {"success": success}

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

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

												a		:Union[str, Any]											= 2

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

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

												a		:List[str]											= None

						logger.info('''🔥 All distributed tests successful'''		)
def        __lowerCamelCase		(				UpperCAmelCase_		:	List[Any]		):




						"""simple docstring"""





						main()


if __name__ == "__main__":
					main()









from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


snake_case       :					Dict        =  {
    '''configuration_align''': [
        '''ALIGN_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''AlignConfig''',
        '''AlignTextConfig''',
        '''AlignVisionConfig''',
    ],
    '''processing_align''': ['''AlignProcessor'''],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     snake_case       :					List[str]        =  [
         '''ALIGN_PRETRAINED_MODEL_ARCHIVE_LIST''',
         '''AlignModel''',
         '''AlignPreTrainedModel''',
         '''AlignTextModel''',
         '''AlignVisionModel''',
     ]

if TYPE_CHECKING:
     from .configuration_align import (
         ALIGN_PRETRAINED_CONFIG_ARCHIVE_MAP,
         AlignConfig,
         AlignTextConfig,
         AlignVisionConfig,
     )
     from .processing_align import AlignProcessor

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_align import (
              ALIGN_PRETRAINED_MODEL_ARCHIVE_LIST,
              AlignModel,
              AlignPreTrainedModel,
              AlignTextModel,
              AlignVisionModel,
          )

else:
     import sys

     snake_case       :					int        =  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





