






import math
import time
from typing import Dict, List, Optional

from torch.utils.data import Dataset

from transformers import SeqaSeqTrainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
  import torch_xla.core.xla_model as xm
  import torch_xla.debug.metrics as met
class 							A_    (						__lowercase			):






 '''simple docstring'''




 def __init__(	self							,  *_A							,  _A=None							,  _A=None							,  **_A) ->		Dict:

     """simple docstring"""



     super().__init__(*_A							,  **_A)
     _UpperCAmelCase				:      int			  =				eval_examples
     _UpperCAmelCase				:      List[str]			  =				post_process_function


 def  snake_case__				(	self							,  _A = None							,  _A=None							,  _A = None							,  _A = "eval"							,  **_A							,  ) ->		Dict[str, float]:

     """simple docstring"""



     _UpperCAmelCase				:      Dict			  =				gen_kwargs.copy()
     _UpperCAmelCase				:      Dict			  =				(
         gen_kwargs['''max_length'''] if gen_kwargs.get('''max_length''') is not None else self.args.generation_max_length
     )
     _UpperCAmelCase				:      Tuple			  =				(
         gen_kwargs['''num_beams'''] if gen_kwargs.get('''num_beams''') is not None else self.args.generation_num_beams
     )
     _UpperCAmelCase				:      Dict			  =				gen_kwargs

     _UpperCAmelCase				:      List[str]			  =				self.eval_dataset if eval_dataset is None else eval_dataset
     _UpperCAmelCase				:      Any			  =				self.get_eval_dataloader(_A)
     _UpperCAmelCase				:      Any			  =				self.eval_examples if eval_examples is None else eval_examples

     # Temporarily disable metric computation, we will do it in the loop here.
     _UpperCAmelCase				:      str			  =				self.compute_metrics
     _UpperCAmelCase				:      Optional[Any]			  =				None
     _UpperCAmelCase				:      List[Any]			  =				time.time()
     _UpperCAmelCase				:      int			  =				self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
     try:
         _UpperCAmelCase				:      str			  =				eval_loop(
             _A							,  description='''Evaluation'''							,  prediction_loss_only=True if compute_metrics is None else None							,  ignore_keys=_A							,  metric_key_prefix=_A							,  )
     finally:
         _UpperCAmelCase				:      Optional[Any]			  =				compute_metrics
     _UpperCAmelCase				:      str			  =				self.args.eval_batch_size * self.args.world_size
     if f'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
         start_time += output.metrics[f'''{metric_key_prefix}_jit_compilation_time''']
     output.metrics.update(
         speed_metrics(
             _A							,  _A							,  num_samples=output.num_samples							,  num_steps=math.ceil(output.num_samples / total_batch_size)							,  ))

     if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
         # Only the main node write the results by default
         _UpperCAmelCase				:      int			  =				self.post_process_function(_A							,  _A							,  _A)
         _UpperCAmelCase				:      Optional[Any]			  =				self.compute_metrics(_A)

         # Prefix all keys with metric_key_prefix + '_'
         for key in list(metrics.keys()):
             if not key.startswith(f'''{metric_key_prefix}_'''):
                 _UpperCAmelCase				:      Any			  =				metrics.pop(_A)

         metrics.update(output.metrics)
     else:
         _UpperCAmelCase				:      Optional[int]			  =				output.metrics

     if self.args.should_log:
         # Only the main node log the results by default
         self.log(_A)

     if self.args.tpu_metrics_debug or self.args.debug:
         # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
         xm.master_print(met.metrics_report())

     _UpperCAmelCase				:      Optional[int]			  =				self.callback_handler.on_evaluate(self.args							,  self.state							,  self.control							,  _A)
     return metrics


 def  snake_case__				(	self							,  _A							,  _A							,  _A=None							,  _A = "test"							,  **_A) ->		List[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[Any]			  =				gen_kwargs.copy()

     _UpperCAmelCase				:      int			  =				self.get_test_dataloader(_A)

     # Temporarily disable metric computation, we will do it in the loop here.
     _UpperCAmelCase				:      str			  =				self.compute_metrics
     _UpperCAmelCase				:      Any			  =				None
     _UpperCAmelCase				:      List[str]			  =				time.time()
     _UpperCAmelCase				:      int			  =				self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
     try:
         _UpperCAmelCase				:      Optional[int]			  =				eval_loop(
             _A							,  description='''Prediction'''							,  prediction_loss_only=True if compute_metrics is None else None							,  ignore_keys=_A							,  metric_key_prefix=_A							,  )
     finally:
         _UpperCAmelCase				:      str			  =				compute_metrics

     _UpperCAmelCase				:      Optional[Any]			  =				self.args.eval_batch_size * self.args.world_size
     if f'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
         start_time += output.metrics[f'''{metric_key_prefix}_jit_compilation_time''']
     output.metrics.update(
         speed_metrics(
             _A							,  _A							,  num_samples=output.num_samples							,  num_steps=math.ceil(output.num_samples / total_batch_size)							,  ))
     if self.post_process_function is None or self.compute_metrics is None:
         return output

     _UpperCAmelCase				:      List[Any]			  =				self.post_process_function(_A							,  _A							,  _A							,  '''predict''')
     _UpperCAmelCase				:      List[str]			  =				self.compute_metrics(_A)

     # Prefix all keys with metric_key_prefix + '_'
     for key in list(metrics.keys()):
         if not key.startswith(f'''{metric_key_prefix}_'''):
             _UpperCAmelCase				:      Optional[int]			  =				metrics.pop(_A)
     metrics.update(output.metrics)
     return PredictionOutput(predictions=predictions.predictions							,  label_ids=predictions.label_ids							,  metrics=_A)












import argparse

import torch

from transformers import RemBertConfig, RemBertModel, load_tf_weights_in_rembert
from transformers.utils import logging


logging.set_verbosity_info()

def      _lowerCamelCase	(						__A    :							int		,		__A    :							Optional[Any]		,		__A    :							int )      ->					int:
    # Initialise PyTorch model
    _UpperCAmelCase				:      Dict			  =				RemBertConfig.from_json_file(__A )
    print('''Building PyTorch model from configuration: {}'''.format(str(__A ) ) )
    _UpperCAmelCase				:      int			  =				RemBertModel(__A )

    # Load weights from tf checkpoint
    load_tf_weights_in_rembert(__A		,		__A		,		__A )

    # Save pytorch-model
    print('''Save PyTorch model to {}'''.format(__A ) )
    torch.save(model.state_dict()		,		__A )


if __name__ == "__main__":
  SCREAMING_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(
      '--rembert_config_file',
      default=None,
      type=str,
      required=True,
      help=(
          'The config json file corresponding to the pre-trained RemBERT 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.'
  )
  SCREAMING_SNAKE_CASE 							=						parser.parse_args()
  convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)




