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


a__		: Dict  							=							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 			UpperCamelCase__	(     SCREAMING_SNAKE_CASE):






  def __init__(      self	:Union[str, Any]						,					_A	:int = 101      )					->      Tuple:







        '''simple docstring'''



        __A													=      length






  def __len__(      self	:Tuple      )					->      Union[str, Any]:







        '''simple docstring'''



        return self.length






  def __getitem__(      self	:Union[str, Any]						,					_A	:Optional[int]      )					->      int:







        '''simple docstring'''



        return i


 class 			UpperCamelCase__	:






  def __call__(      self	:str						,					_A	:List[str]      )					->      Optional[int]:







        '''simple docstring'''



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


 class 			UpperCamelCase__	(     nn.Module):






  def __init__(      self	:Any      )					->      Optional[Any]:







        '''simple docstring'''



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






  def 						lowercase_    (      self	:Optional[int]						,					_A	:Tuple						,					_A	:int=None      )					->      Optional[Any]:







        '''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				(		)->      Any:







      """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_0_0_1, 2_5_6, 1_5]:
            __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 							snake_case				(		UpperCAmelCase						)->      Dict:







      """simple docstring"""
      # For xla_spawn (TPUs)
      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 							snake_case				(		UpperCAmelCase   ,	UpperCAmelCase   ,	UpperCAmelCase						)->      Union[str, Any]:







      """simple docstring"""
      # Initialise PyTorch model
      __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__":
 a__		: Tuple  							=							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."
 )
 a__		: Union[str, Any]  							=							parser.parse_args()
 convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)






