


# 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 A  (       UpperCamelCase_					):






       def __init__( self						:					Union[str, Any]      ,       lowercase_						:					int = 101			) ->						Optional[int]:
              """simple docstring"""




              _lowerCamelCase	:		Tuple          =length






       def __len__( self						:					Union[str, Any]			) ->						str:
              """simple docstring"""




              return self.length






       def __getitem__( self						:					Union[str, Any]      ,       lowercase_						:					Optional[int]			) ->						int:
              """simple docstring"""




              return i




 class A  :






       def __call__( self						:					Optional[Any]      ,       lowercase_						:					List[Any]			) ->						Tuple:
              """simple docstring"""




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




 class A  (       nn.Module					):






       def __init__( self						:					Dict			) ->						Dict:
              """simple docstring"""




              super().__init__()
              # Add some (unused) params otherwise DDP will complain.
              _lowerCamelCase	:		Union[str, Any]          =nn.Linear(120      ,       80			)






       def 	lowerCamelCase	( self						:					Tuple      ,       lowercase_						:					Dict      ,       lowercase_						:					Optional[int]=None			) ->						Tuple:
              """simple docstring"""




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




def      a_				(   ):
       '''simple docstring'''





       _lowerCamelCase	:		List[str]          =HfArgumentParser((TrainingArguments,)					)
       sys.argv += ["--output_dir", "./examples"]
       _lowerCamelCase	:		Tuple          =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_001, 256, 15]:
              _lowerCamelCase	:		Optional[Any]          =DummyDataset(SCREAMING_SNAKE_CASE__					)

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

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

              _lowerCamelCase	:		Optional[Any]          =trainer.predict(SCREAMING_SNAKE_CASE__					)
              logger.info(p.metrics					)
              if p.metrics["test_success"] is not True:
                     logger.error(p.metrics					)
                     exit(1					)

              _lowerCamelCase	:		List[Any]          =2

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

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

              _lowerCamelCase	:		str          =None

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




def      a_				(   SCREAMING_SNAKE_CASE__ :				int					):
       '''simple docstring'''





       main()


if __name__ == "__main__":
 main()






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowerCamelCase						 =	{'configuration_visual_bert': ['VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'VisualBertConfig']}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase						 =	[
     'VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST',
     'VisualBertForMultipleChoice',
     'VisualBertForPreTraining',
     'VisualBertForQuestionAnswering',
     'VisualBertForRegionToPhraseAlignment',
     'VisualBertForVisualReasoning',
     'VisualBertLayer',
     'VisualBertModel',
     'VisualBertPreTrainedModel',
 ]


if TYPE_CHECKING:
 from .configuration_visual_bert import VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, VisualBertConfig

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_visual_bert import (
      VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      VisualBertForMultipleChoice,
      VisualBertForPreTraining,
      VisualBertForQuestionAnswering,
      VisualBertForRegionToPhraseAlignment,
      VisualBertForVisualReasoning,
      VisualBertLayer,
      VisualBertModel,
      VisualBertPreTrainedModel,
  )


else:
 import sys

 lowerCamelCase						 =	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)


