



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


UpperCamelCase__    :     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 							_lowerCAmelCase	(  __A							):


   """simple docstring"""

   def __init__(      self					,    _lowerCamelCase = 101  )    ->    List[str]:
      A_  :		List[Any]						=					length







   def __len__(      self  )    ->    str:
      return self.length







   def __getitem__(      self					,    _lowerCamelCase  )    ->    int:
      return i


  class 							_lowerCAmelCase	:


   """simple docstring"""

   def __call__(      self					,    _lowerCamelCase  )    ->    List[str]:
      return {"input_ids": torch.tensor(_lowerCamelCase  ), "labels": torch.tensor(_lowerCamelCase  )}


  class 							_lowerCAmelCase	(  nn.Module							):


   """simple docstring"""

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







   def       UpperCAmelCase_       (      self					,    _lowerCamelCase					,    _lowerCamelCase=None  )    ->    List[str]:
      if labels is not None:
         return torch.tensor(0.0					,    device=input_ids.device  ), input_ids
      else:
         return input_ids



def     UpperCAmelCase     (    )   ->       Optional[int]:



   """simple docstring"""






   A_  :		Any						=					HfArgumentParser((TrainingArguments,)		)
   sys.argv += ["--output_dir", "./examples"]
   A_  :		Optional[int]						=					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_0_0_1, 2_5_6, 1_5]:
      A_  :		List[str]						=					DummyDataset(a_		)

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

      A_  :		List[Any]						=					Trainer(
          model=DummyModel()      ,		args=a_      ,		data_collator=DummyDataCollator()      ,		eval_dataset=a_      ,		compute_metrics=a_      ,		)
      A_  :		List[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_  :		Optional[int]						=					2

      A_  :		Union[str, Any]						=					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_  :		Dict						=					None

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



def     UpperCAmelCase     (    a_		)   ->       Optional[int]:



   """simple docstring"""






   main()


if __name__ == "__main__":
  main()





'''simple docstring'''





from typing import TYPE_CHECKING

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


UpperCamelCase__    :     List[Any]			  =   {
    'configuration_layoutlm': ['LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LayoutLMConfig', 'LayoutLMOnnxConfig'],
    'tokenization_layoutlm': ['LayoutLMTokenizer'],
}

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

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCamelCase__    :     Optional[Any]			  =   [
      'LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST',
      'LayoutLMForMaskedLM',
      'LayoutLMForSequenceClassification',
      'LayoutLMForTokenClassification',
      'LayoutLMForQuestionAnswering',
      'LayoutLMModel',
      'LayoutLMPreTrainedModel',
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCamelCase__    :     str			  =   [
      'TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST',
      'TFLayoutLMForMaskedLM',
      'TFLayoutLMForSequenceClassification',
      'TFLayoutLMForTokenClassification',
      'TFLayoutLMForQuestionAnswering',
      'TFLayoutLMMainLayer',
      'TFLayoutLMModel',
      'TFLayoutLMPreTrainedModel',
  ]


if TYPE_CHECKING:
  from .configuration_layoutlm import LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMConfig, LayoutLMOnnxConfig
  from .tokenization_layoutlm import LayoutLMTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_layoutlm_fast import LayoutLMTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_layoutlm import (
        LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST,
        LayoutLMForMaskedLM,
        LayoutLMForQuestionAnswering,
        LayoutLMForSequenceClassification,
        LayoutLMForTokenClassification,
        LayoutLMModel,
        LayoutLMPreTrainedModel,
    )
  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_layoutlm import (
        TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFLayoutLMForMaskedLM,
        TFLayoutLMForQuestionAnswering,
        TFLayoutLMForSequenceClassification,
        TFLayoutLMForTokenClassification,
        TFLayoutLMMainLayer,
        TFLayoutLMModel,
        TFLayoutLMPreTrainedModel,
    )

else:
  import sys

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