


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


__snake_case												=							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     ,					__UpperCAmelCase = 101				)							->				int:
             _a     						=					length


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





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







   class      __lowerCamelCase	:

        '''simple docstring'''


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







   class      __lowerCamelCase	(       nn.Module					):

        '''simple docstring'''


        def __init__(   self				)							->				Optional[int]:
             super().__init__()
             # Add some (unused) params otherwise DDP will complain.
             _a     						=					nn.Linear(120     ,					80				)





        def 				_UpperCAmelCase   (   self     ,					__UpperCAmelCase     ,					__UpperCAmelCase=None				)							->				Union[str, Any]:
             if labels is not None:
                  return torch.tensor(0.0     ,					device=input_ids.device				), input_ids
             else:
                  return input_ids
def    A_ ( ):






     """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 [10_01, 2_56, 15]:
          _a     						=					DummyDataset(_lowerCAmelCase							)

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

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

          _a     						=					trainer.predict(_lowerCAmelCase							)
          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(_lowerCAmelCase							)
          if metrics["eval_success"] is not True:
               logger.error(_lowerCAmelCase							)
               exit(1							)

          _a     						=					trainer.predict(_lowerCAmelCase							)
          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    A_ ( _lowerCAmelCase  :       Dict							):






     """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    A_ ( _lowerCAmelCase  :       Dict,  _lowerCAmelCase  :       Tuple,  _lowerCAmelCase  :       Optional[Any]							):






     """simple docstring"""

     _a     						=					RoFormerConfig.from_json_file(_lowerCAmelCase							)
     print(f'Building PyTorch model from configuration: {config}'							)
     _a     						=					RoFormerForMaskedLM(_lowerCAmelCase							)

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

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


if __name__ == "__main__":
   __snake_case												=							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.'''
   )
   __snake_case												=							parser.parse_args()
   convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)