





# 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__:  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  (  UpperCamelCase__):

         """simple docstring"""


         def __init__(						self:	int					,  __lowerCamelCase:	int = 101			):
             '''simple docstring'''


             UpperCamelCase__:					List[Any]												=      length






         def __len__(						self:	List[Any]			):
             '''simple docstring'''


             return self.length






         def __getitem__(						self:	List[str]					,  __lowerCamelCase:	List[Any]			):
             '''simple docstring'''


             return i




   class    _a  :

         """simple docstring"""


         def __call__(						self:	Union[str, Any]					,  __lowerCamelCase:	Dict			):
             '''simple docstring'''


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




   class    _a  (  nn.Module):

         """simple docstring"""


         def __init__(						self:	Tuple			):
             '''simple docstring'''


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






         def 	UpperCAmelCase_   (						self:	Any					,  __lowerCamelCase:	Optional[int]					,  __lowerCamelCase:	Union[str, Any]=None			):
             '''simple docstring'''


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



def 							lowerCAmelCase_				(    ):
    UpperCamelCase__:					List[str]												=      HfArgumentParser((TrainingArguments,))
    sys.argv += ["--output_dir", "./examples"]
    UpperCamelCase__:					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 [10_01, 2_56, 15]:
        UpperCamelCase__:					Optional[int]												=      DummyDataset(A_)

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

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

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

        UpperCamelCase__:					Any												=      2

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

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

        UpperCamelCase__:					Union[str, Any]												=      None

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



def 							lowerCAmelCase_				(    A_):
    # For xla_spawn (TPUs)
    main()


if __name__ == "__main__":
   main()








from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


A__:  List[str]       					=					{
    '''configuration_wav2vec2_conformer''': [
        '''WAV2VEC2_CONFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''Wav2Vec2ConformerConfig''',
    ],
}


try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   A__:  int       					=					[
       '''WAV2VEC2_CONFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
       '''Wav2Vec2ConformerForAudioFrameClassification''',
       '''Wav2Vec2ConformerForCTC''',
       '''Wav2Vec2ConformerForPreTraining''',
       '''Wav2Vec2ConformerForSequenceClassification''',
       '''Wav2Vec2ConformerForXVector''',
       '''Wav2Vec2ConformerModel''',
       '''Wav2Vec2ConformerPreTrainedModel''',
   ]

if TYPE_CHECKING:
   from .configuration_wavaveca_conformer import (
       WAV2VEC2_CONFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
       WavaVecaConformerConfig,
   )

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_wavaveca_conformer import (
          WAV2VEC2_CONFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
          WavaVecaConformerForAudioFrameClassification,
          WavaVecaConformerForCTC,
          WavaVecaConformerForPreTraining,
          WavaVecaConformerForSequenceClassification,
          WavaVecaConformerForXVector,
          WavaVecaConformerModel,
          WavaVecaConformerPreTrainedModel,
      )

else:
   import sys

   A__:  Dict       					=					_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

