
"""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_						   =   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       (     SCREAMING_SNAKE_CASE_		):







         def __init__( self       ,							__UpperCAmelCase = 1_01)   ->Any:
            a_						   =     length







         def __len__( self)   ->Optional[Any]:
            return self.length







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






      class 							snake_case       :







         def __call__( self       ,							__UpperCAmelCase)   ->Any:
            return {"input_ids": torch.tensor(__UpperCAmelCase), "labels": torch.tensor(__UpperCAmelCase)}






      class 							snake_case       (     nn.Module		):







         def __init__( self)   ->Any:
            super().__init__()
            # Add some (unused) params otherwise DDP will complain.
            a_						   =     nn.Linear(1_20       ,							80)







         def    UpperCAmelCase__	( self       ,							__UpperCAmelCase       ,							__UpperCAmelCase=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						(					)						->List[str]:

   """simple docstring"""



   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 [1_001, 256, 15]:
      a_						   =     DummyDataset(UpperCAmelCase     )

      def compute_metrics(UpperCAmelCase     ) -> 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    UpperCamelCase						(					UpperCAmelCase     )						->Optional[int]:

   """simple docstring"""



   main()


if __name__ == "__main__":
      main()

"""simple docstring"""






import argparse

import torch

from transformers import RoFormerConfig, RoFormerForMaskedLM, load_tf_weights_in_roformer
from transformers.utils import logging


logging.set_verbosity_info()
def    UpperCamelCase						(					UpperCAmelCase ,   UpperCAmelCase ,   UpperCAmelCase     )						->Dict:

   """simple docstring"""



   a_						   =     RoFormerConfig.from_json_file(UpperCAmelCase     )
   print(F'''Building PyTorch model from configuration: {config}'''     )
   a_						   =     RoFormerForMaskedLM(UpperCAmelCase     )

   # Load weights from tf checkpoint
   load_tf_weights_in_roformer(UpperCAmelCase ,   UpperCAmelCase ,   UpperCAmelCase     )

   # Save pytorch-model
   print(F'''Save PyTorch model to {pytorch_dump_path}'''     )
   torch.save(model.state_dict() ,   UpperCAmelCase ,   _use_new_zipfile_serialization=UpperCAmelCase     )


if __name__ == "__main__":
      UpperCamelCase_						   =   argparse.ArgumentParser()
      # Required parameters
      parser.add_argument(
          '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.'
      )
      parser.add_argument(
          '--bert_config_file',
          default=None,
          type=str,
          required=True,
          help=(
              'The config json file corresponding to the pre-trained BERT model. \n'
              'This specifies the model architecture.'
          ),
      )
      parser.add_argument(
          '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
      )
      UpperCamelCase_						   =   parser.parse_args()
      convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)