

# 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


UpperCamelCase__     :					Union[str, 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  ,						_A = 101):
             SCREAMING_SNAKE_CASE_													=  length

         def __len__(    self):
             return self.length


         def __getitem__(    self  ,						_A):
             return i






       class 					__snake_case     :

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






       class 					__snake_case     (			nn.Module				):

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


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


def     _UpperCAmelCase				(     ):





    """simple docstring"""


    SCREAMING_SNAKE_CASE_													=  HfArgumentParser((TrainingArguments,)				)
    sys.argv += ["--output_dir", "./examples"]
    SCREAMING_SNAKE_CASE_													=  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]:
        SCREAMING_SNAKE_CASE_													=  DummyDataset(_SCREAMING_SNAKE_CASE				)

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

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

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

        SCREAMING_SNAKE_CASE_													=  2

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

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

        SCREAMING_SNAKE_CASE_													=  None

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


def     _UpperCAmelCase				(     _SCREAMING_SNAKE_CASE    :						str				):





    """simple docstring"""


    main()


if __name__ == "__main__":
       main()





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCamelCase__     :					int								=			{
    "configuration_camembert": ["CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "CamembertConfig", "CamembertOnnxConfig"],
}

try:
       if not is_sentencepiece_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       UpperCamelCase__     :					str								=			["CamembertTokenizer"]

try:
       if not is_tokenizers_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       UpperCamelCase__     :					Any								=			["CamembertTokenizerFast"]

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       UpperCamelCase__     :					Union[str, Any]								=			[
           "CAMEMBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
           "CamembertForCausalLM",
           "CamembertForMaskedLM",
           "CamembertForMultipleChoice",
           "CamembertForQuestionAnswering",
           "CamembertForSequenceClassification",
           "CamembertForTokenClassification",
           "CamembertModel",
           "CamembertPreTrainedModel",
       ]

try:
       if not is_tf_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       UpperCamelCase__     :					Optional[Any]								=			[
           "TF_CAMEMBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
           "TFCamembertForCausalLM",
           "TFCamembertForMaskedLM",
           "TFCamembertForMultipleChoice",
           "TFCamembertForQuestionAnswering",
           "TFCamembertForSequenceClassification",
           "TFCamembertForTokenClassification",
           "TFCamembertModel",
           "TFCamembertPreTrainedModel",
       ]


if TYPE_CHECKING:
       from .configuration_camembert import CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, CamembertConfig, CamembertOnnxConfig

       try:
              if not is_sentencepiece_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_camembert import CamembertTokenizer

       try:
              if not is_tokenizers_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_camembert_fast import CamembertTokenizerFast

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_camembert import (
                  CAMEMBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                  CamembertForCausalLM,
                  CamembertForMaskedLM,
                  CamembertForMultipleChoice,
                  CamembertForQuestionAnswering,
                  CamembertForSequenceClassification,
                  CamembertForTokenClassification,
                  CamembertModel,
                  CamembertPreTrainedModel,
              )

       try:
              if not is_tf_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_tf_camembert import (
                  TF_CAMEMBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                  TFCamembertForCausalLM,
                  TFCamembertForMaskedLM,
                  TFCamembertForMultipleChoice,
                  TFCamembertForQuestionAnswering,
                  TFCamembertForSequenceClassification,
                  TFCamembertForTokenClassification,
                  TFCamembertModel,
                  TFCamembertPreTrainedModel,
              )

else:
       import sys

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


