






'''simple docstring'''



from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers import (
     TFConvBertForMaskedLM,
     TFConvBertForMultipleChoice,
     TFConvBertForQuestionAnswering,
     TFConvBertForSequenceClassification,
     TFConvBertForTokenClassification,
     TFConvBertModel,
 )
class 				__SCREAMING_SNAKE_CASE							:




      def __init__(	self			, lowerCamelCase			, lowerCamelCase=13			, lowerCamelCase=7			, lowerCamelCase=True			, lowerCamelCase=True			, lowerCamelCase=True			, lowerCamelCase=True			, lowerCamelCase=99			, lowerCamelCase=32			, lowerCamelCase=2			, lowerCamelCase=4			, lowerCamelCase=37			, lowerCamelCase="gelu"			, lowerCamelCase=0.1			, lowerCamelCase=0.1			, lowerCamelCase=512			, lowerCamelCase=16			, lowerCamelCase=2			, lowerCamelCase=0.02			, lowerCamelCase=3			, lowerCamelCase=4			, lowerCamelCase=None			, )				->Union[str, Any]:



         '''simple docstring'''
         __a					  =     parent
         __a					  =     13
         __a					  =     7
         __a					  =     True
         __a					  =     True
         __a					  =     True
         __a					  =     True
         __a					  =     99
         __a					  =     384
         __a					  =     2
         __a					  =     4
         __a					  =     37
         __a					  =     'gelu'
         __a					  =     0.1
         __a					  =     0.1
         __a					  =     512
         __a					  =     16
         __a					  =     2
         __a					  =     0.02
         __a					  =     3
         __a					  =     4
         __a					  =     128
         __a					  =     2
         __a					  =     9
         __a					  =     1
         __a					  =     None




      def 			__UpperCamelCase				(	self   )				->Any:



         '''simple docstring'''
         __a					  =     ids_tensor([self.batch_size, self.seq_length]			, self.vocab_size   )

         __a					  =     None
         if self.use_input_mask:
            __a					  =     random_attention_mask([self.batch_size, self.seq_length]   )

         __a					  =     None
         if self.use_token_type_ids:
            __a					  =     ids_tensor([self.batch_size, self.seq_length]			, self.type_vocab_size   )

         __a					  =     None
         __a					  =     None
         __a					  =     None
         if self.use_labels:
            __a					  =     ids_tensor([self.batch_size]			, self.type_sequence_label_size   )
            __a					  =     ids_tensor([self.batch_size, self.seq_length]			, self.num_labels   )
            __a					  =     ids_tensor([self.batch_size]			, self.num_choices   )

         __a					  =     ConvBertConfig(
             vocab_size=self.vocab_size			, hidden_size=self.hidden_size			, num_hidden_layers=self.num_hidden_layers			, num_attention_heads=self.num_attention_heads			, intermediate_size=self.intermediate_size			, hidden_act=self.hidden_act			, hidden_dropout_prob=self.hidden_dropout_prob			, attention_probs_dropout_prob=self.attention_probs_dropout_prob			, max_position_embeddings=self.max_position_embeddings			, type_vocab_size=self.type_vocab_size			, initializer_range=self.initializer_range			, return_dict=lowerCamelCase			, )

         return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase   )				->Any:



         '''simple docstring'''
         __a					  =     TFConvBertModel(config=lowerCamelCase   )
         __a					  =     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

         __a					  =     [input_ids, input_mask]
         __a					  =     model(lowerCamelCase   )

         __a					  =     model(lowerCamelCase   )

         self.parent.assertEqual(result.last_hidden_state.shape			, (self.batch_size, self.seq_length, self.hidden_size)   )




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase   )				->Union[str, Any]:



         '''simple docstring'''
         __a					  =     TFConvBertForMaskedLM(config=lowerCamelCase   )
         __a					  =     {
             'input_ids': input_ids,
             'attention_mask': input_mask,
             'token_type_ids': token_type_ids,
         }
         __a					  =     model(lowerCamelCase   )
         self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.seq_length, self.vocab_size)   )




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase   )				->str:



         '''simple docstring'''
         __a					  =     self.num_labels
         __a					  =     TFConvBertForSequenceClassification(config=lowerCamelCase   )
         __a					  =     {
             'input_ids': input_ids,
             'attention_mask': input_mask,
             'token_type_ids': token_type_ids,
         }

         __a					  =     model(lowerCamelCase   )
         self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.num_labels)   )




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase   )				->Any:



         '''simple docstring'''
         __a					  =     self.num_choices
         __a					  =     TFConvBertForMultipleChoice(config=lowerCamelCase   )
         __a					  =     tf.tile(tf.expand_dims(lowerCamelCase			, 1   )			, (1, self.num_choices, 1)   )
         __a					  =     tf.tile(tf.expand_dims(lowerCamelCase			, 1   )			, (1, self.num_choices, 1)   )
         __a					  =     tf.tile(tf.expand_dims(lowerCamelCase			, 1   )			, (1, self.num_choices, 1)   )
         __a					  =     {
             'input_ids': multiple_choice_inputs_ids,
             'attention_mask': multiple_choice_input_mask,
             'token_type_ids': multiple_choice_token_type_ids,
         }
         __a					  =     model(lowerCamelCase   )
         self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.num_choices)   )




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase   )				->List[Any]:



         '''simple docstring'''
         __a					  =     self.num_labels
         __a					  =     TFConvBertForTokenClassification(config=lowerCamelCase   )
         __a					  =     {
             'input_ids': input_ids,
             'attention_mask': input_mask,
             'token_type_ids': token_type_ids,
         }
         __a					  =     model(lowerCamelCase   )
         self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.seq_length, self.num_labels)   )




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase   )				->Dict:



         '''simple docstring'''
         __a					  =     TFConvBertForQuestionAnswering(config=lowerCamelCase   )
         __a					  =     {
             'input_ids': input_ids,
             'attention_mask': input_mask,
             'token_type_ids': token_type_ids,
         }

         __a					  =     model(lowerCamelCase   )
         self.parent.assertEqual(result.start_logits.shape			, (self.batch_size, self.seq_length)   )
         self.parent.assertEqual(result.end_logits.shape			, (self.batch_size, self.seq_length)   )






      def 			__UpperCamelCase				(	self   )				->str:



         '''simple docstring'''
         __a					  =     self.prepare_config_and_inputs()
         (
             (
             __a
         )  ,      (
             __a
         )  ,      (
             __a
         )  ,      (
             __a
         )  ,      (
             __a
         )  ,      (
             __a
         )  ,      (
             __a
         )  ,      
         )					  =     config_and_inputs
         __a					  =     {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
         return config, inputs_dict







@require_tf
class 				__SCREAMING_SNAKE_CASE							(				_lowerCAmelCase   ,   _lowerCAmelCase   ,   unittest.TestCase  ):
      __a	       =(
          (
              TFConvBertModel,
              TFConvBertForMaskedLM,
              TFConvBertForQuestionAnswering,
              TFConvBertForSequenceClassification,
              TFConvBertForTokenClassification,
              TFConvBertForMultipleChoice,
          )
          if is_tf_available()
          else ()
      )
      __a	       =(
          {
              "feature-extraction": TFConvBertModel,
              "fill-mask": TFConvBertForMaskedLM,
              "question-answering": TFConvBertForQuestionAnswering,
              "text-classification": TFConvBertForSequenceClassification,
              "token-classification": TFConvBertForTokenClassification,
              "zero-shot": TFConvBertForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      __a	       =False
      __a	       =False
      __a	       =False




      def 			__UpperCamelCase				(	self   )				->List[Any]:



         '''simple docstring'''
         __a					  =     TFConvBertModelTester(self   )
         __a					  =     ConfigTester(self			, config_class=lowerCamelCase			, hidden_size=37   )




      def 			__UpperCamelCase				(	self   )				->int:



         '''simple docstring'''
         self.config_tester.run_common_tests()




      def 			__UpperCamelCase				(	self   )				->Any:



         '''simple docstring'''
         __a					  =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*lowerCamelCase   )




      def 			__UpperCamelCase				(	self   )				->List[Any]:



         '''simple docstring'''
         __a					  =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase   )




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



         '''simple docstring'''
         __a					  =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase   )




      def 			__UpperCamelCase				(	self   )				->Dict:



         '''simple docstring'''
         __a					  =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*lowerCamelCase   )




      def 			__UpperCamelCase				(	self   )				->List[Any]:



         '''simple docstring'''
         __a					  =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase   )




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



         '''simple docstring'''
         __a					  =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*lowerCamelCase   )




      @slow
      def 			__UpperCamelCase				(	self   )				->Dict:



         '''simple docstring'''
         __a  ,      __a					  =     self.model_tester.prepare_config_and_inputs_for_common()
         __a					  =     True
         __a					  =     True

         if hasattr(lowerCamelCase			, 'use_cache'   ):
            __a					  =     True

         __a					  =     getattr(self.model_tester			, 'encoder_seq_length'			, self.model_tester.seq_length   )
         __a					  =     getattr(self.model_tester			, 'key_length'			, lowerCamelCase   )

         for model_class in self.all_model_classes:
            __a					  =     self._prepare_for_class(lowerCamelCase			, lowerCamelCase   )
            __a					  =     model_class(lowerCamelCase   )
            __a					  =     len(model(lowerCamelCase   )   )

            with tempfile.TemporaryDirectory() as tmpdirname:
               model.save_pretrained(lowerCamelCase			, saved_model=lowerCamelCase   )
               __a					  =     os.path.join(lowerCamelCase			, 'saved_model'			, '1'   )
               __a					  =     tf.keras.models.load_model(lowerCamelCase   )
               __a					  =     model(lowerCamelCase   )

               if self.is_encoder_decoder:
                  __a					  =     outputs['encoder_hidden_states']
                  __a					  =     outputs['encoder_attentions']
               else:
                  __a					  =     outputs['hidden_states']
                  __a					  =     outputs['attentions']

               self.assertEqual(len(lowerCamelCase   )			, lowerCamelCase   )

               __a					  =     getattr(
                   self.model_tester			, 'expected_num_hidden_layers'			, self.model_tester.num_hidden_layers + 1   )

               self.assertEqual(len(lowerCamelCase   )			, lowerCamelCase   )
               self.assertListEqual(
                   list(output_hidden_states[0].shape[-2:]   )			, [self.model_tester.seq_length, self.model_tester.hidden_size]			, )

               self.assertEqual(len(lowerCamelCase   )			, self.model_tester.num_hidden_layers   )
               self.assertListEqual(
                   list(output_attentions[0].shape[-3:]   )			, [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]			, )




      @slow
      def 			__UpperCamelCase				(	self   )				->Dict:



         '''simple docstring'''
         __a					  =     TFConvBertModel.from_pretrained('YituTech/conv-bert-base'   )
         self.assertIsNotNone(lowerCamelCase   )






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



         '''simple docstring'''
         __a  ,      __a					  =     self.model_tester.prepare_config_and_inputs_for_common()
         __a					  =     True
         __a					  =     getattr(self.model_tester			, 'decoder_seq_length'			, self.model_tester.seq_length   )
         __a					  =     getattr(self.model_tester			, 'encoder_seq_length'			, self.model_tester.seq_length   )
         __a					  =     getattr(self.model_tester			, 'key_length'			, lowerCamelCase   )
         __a					  =     getattr(self.model_tester			, 'key_length'			, lowerCamelCase   )

         def check_decoder_attentions_output(lowerCamelCase   ):
            __a					  =     len(lowerCamelCase   )
            self.assertEqual(out_len % 2			, 0   )
            __a					  =     outputs.decoder_attentions
            self.assertEqual(len(lowerCamelCase   )			, self.model_tester.num_hidden_layers   )
            self.assertListEqual(
                list(decoder_attentions[0].shape[-3:]   )			, [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]			, )

         def check_encoder_attentions_output(lowerCamelCase   ):
            __a					  =     [
                t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
            ]
            self.assertEqual(len(lowerCamelCase   )			, self.model_tester.num_hidden_layers   )
            self.assertListEqual(
                list(attentions[0].shape[-3:]   )			, [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]			, )

         for model_class in self.all_model_classes:
            __a					  =     True
            __a					  =     False
            __a					  =     model_class(lowerCamelCase   )
            __a					  =     model(self._prepare_for_class(lowerCamelCase			, lowerCamelCase   )   )
            __a					  =     len(lowerCamelCase   )
            self.assertEqual(config.output_hidden_states			, lowerCamelCase   )
            check_encoder_attentions_output(lowerCamelCase   )

            if self.is_encoder_decoder:
               __a					  =     model_class(lowerCamelCase   )
               __a					  =     model(self._prepare_for_class(lowerCamelCase			, lowerCamelCase   )   )
               self.assertEqual(config.output_hidden_states			, lowerCamelCase   )
               check_decoder_attentions_output(lowerCamelCase   )

            # Check that output attentions can also be changed via the config
            del inputs_dict["output_attentions"]
            __a					  =     True
            __a					  =     model_class(lowerCamelCase   )
            __a					  =     model(self._prepare_for_class(lowerCamelCase			, lowerCamelCase   )   )
            self.assertEqual(config.output_hidden_states			, lowerCamelCase   )
            check_encoder_attentions_output(lowerCamelCase   )

            # Check attention is always last and order is fine
            __a					  =     True
            __a					  =     True
            __a					  =     model_class(lowerCamelCase   )
            __a					  =     model(self._prepare_for_class(lowerCamelCase			, lowerCamelCase   )   )

            self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)			, len(lowerCamelCase   )   )
            self.assertEqual(model.config.output_hidden_states			, lowerCamelCase   )
            check_encoder_attentions_output(lowerCamelCase   )







@require_tf
class 				__SCREAMING_SNAKE_CASE							(				unittest.TestCase  ):




      @slow
      def 			__UpperCamelCase				(	self   )				->Optional[Any]:



         '''simple docstring'''
         __a					  =     TFConvBertModel.from_pretrained('YituTech/conv-bert-base'   )
         __a					  =     tf.constant([[0, 1, 2, 3, 4, 5]]   )
         __a					  =     model(lowerCamelCase   )[0]

         __a					  =     [1, 6, 768]
         self.assertEqual(output.shape			, lowerCamelCase   )

         __a					  =     tf.constant(
             [
                 [
                     [-0.03475493, -0.4686034, -0.30638832],
                     [0.22637248, -0.26988646, -0.7423424],
                     [0.10324868, -0.45013508, -0.58280784],
                 ]
             ]   )
         tf.debugging.assert_near(output[:, :3, :3]			, lowerCamelCase			, atol=1e-4   )







'''simple docstring'''



import argparse

import torch

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


logging.set_verbosity_info()



def  __UpperCAmelCase						(					SCREAMING_SNAKE_CASE__:						Optional[Any],       SCREAMING_SNAKE_CASE__:						Tuple,       SCREAMING_SNAKE_CASE__:						Dict     )	->      List[Any]:

   """simple docstring"""
   # Initialise PyTorch model
   __a					  =     RemBertConfig.from_json_file(SCREAMING_SNAKE_CASE__     )
   print('Building PyTorch model from configuration: {}'.format(str(SCREAMING_SNAKE_CASE__     )     )     )
   __a					  =     RemBertModel(SCREAMING_SNAKE_CASE__     )

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

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


if __name__ == "__main__":
 __UpperCamelCase   :							List[str]         =   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."""
 )
 __UpperCamelCase   :							List[str]         =   parser.parse_args()
 convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)