

"""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


SCREAMING_SNAKE_CASE_       :						List[str]					=    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					):



     """simple docstring"""



     def __init__(  self:			str     ,    UpperCamelCase:			int = 1_01							):






          """simple docstring"""


          A__				      =	length

     def __len__(  self:			int							):






          """simple docstring"""


          return self.length





     def __getitem__(  self:			Optional[Any]     ,    UpperCamelCase:			Tuple							):






          """simple docstring"""


          return i



 class        a    :



     """simple docstring"""



     def __call__(  self:			Optional[Any]     ,    UpperCamelCase:			List[str]							):






          """simple docstring"""


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



 class        a    (							nn.Module					):



     """simple docstring"""



     def __init__(  self:			Optional[Any]							):






          """simple docstring"""


          super().__init__()
          # Add some (unused) params otherwise DDP will complain.
          A__				      =	nn.Linear(1_20     ,    80							)





     def 					UpperCamelCase						(  self:			Union[str, Any]     ,    UpperCamelCase:			Any     ,    UpperCamelCase:			int=None							):






          """simple docstring"""


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





def 			_snake_case  (     ):
     A__				      =	HfArgumentParser((TrainingArguments,)      )
     sys.argv += ["--output_dir", "./examples"]
     A__				      =	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__				      =	DummyDataset(UpperCAmelCase_      )

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

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

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

          A__				      =	2

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

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

          A__				      =	None

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





def 			_snake_case  (     UpperCAmelCase_	:       Dict      ):
     # For xla_spawn (TPUs)
     main()


if __name__ == "__main__":
 main()









"""simple docstring"""



from typing import TYPE_CHECKING

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


SCREAMING_SNAKE_CASE_       :						Union[str, Any]					=    {'configuration_gptj': ['GPTJ_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GPTJConfig', 'GPTJOnnxConfig']}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE_       :						Optional[int]					=    [
     'GPTJ_PRETRAINED_MODEL_ARCHIVE_LIST',
     'GPTJForCausalLM',
     'GPTJForQuestionAnswering',
     'GPTJForSequenceClassification',
     'GPTJModel',
     'GPTJPreTrainedModel',
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE_       :						List[str]					=    [
     'TFGPTJForCausalLM',
     'TFGPTJForQuestionAnswering',
     'TFGPTJForSequenceClassification',
     'TFGPTJModel',
     'TFGPTJPreTrainedModel',
 ]

try:
 if not is_flax_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE_       :						Tuple					=    [
     'FlaxGPTJForCausalLM',
     'FlaxGPTJModel',
     'FlaxGPTJPreTrainedModel',
 ]


if TYPE_CHECKING:
 from .configuration_gptj import GPTJ_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTJConfig, GPTJOnnxConfig

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_gptj import (
      GPTJ_PRETRAINED_MODEL_ARCHIVE_LIST,
      GPTJForCausalLM,
      GPTJForQuestionAnswering,
      GPTJForSequenceClassification,
      GPTJModel,
      GPTJPreTrainedModel,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_gptj import (
      TFGPTJForCausalLM,
      TFGPTJForQuestionAnswering,
      TFGPTJForSequenceClassification,
      TFGPTJModel,
      TFGPTJPreTrainedModel,
  )

 try:
  if not is_flax_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel, FlaxGPTJPreTrainedModel

else:
 import sys

 SCREAMING_SNAKE_CASE_       :						List[str]					=    _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)






