


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






       def __init__(     self    ,				snake_case    ,				snake_case=1_3    ,				snake_case=7    ,				snake_case=True    ,				snake_case=True    ,				snake_case=True    ,				snake_case=True    ,				snake_case=9_9    ,				snake_case=3_2    ,				snake_case=2    ,				snake_case=4    ,				snake_case=3_7    ,				snake_case="gelu"    ,				snake_case=0.1    ,				snake_case=0.1    ,				snake_case=5_1_2    ,				snake_case=1_6    ,				snake_case=2    ,				snake_case=0.02    ,				snake_case=3    ,				snake_case=4    ,				snake_case=None    ,				)		->    List[Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  List[Any]				       =parent
          _UpperCAmelCase  :  int				       =1_3
          _UpperCAmelCase  :  Optional[int]				       =7
          _UpperCAmelCase  :  Dict				       =True
          _UpperCAmelCase  :  List[str]				       =True
          _UpperCAmelCase  :  Optional[Any]				       =True
          _UpperCAmelCase  :  str				       =True
          _UpperCAmelCase  :  Optional[Any]				       =9_9
          _UpperCAmelCase  :  Optional[Any]				       =3_8_4
          _UpperCAmelCase  :  Tuple				       =2
          _UpperCAmelCase  :  List[Any]				       =4
          _UpperCAmelCase  :  Union[str, Any]				       =3_7
          _UpperCAmelCase  :  Dict				       ='gelu'
          _UpperCAmelCase  :  Optional[int]				       =0.1
          _UpperCAmelCase  :  Union[str, Any]				       =0.1
          _UpperCAmelCase  :  Dict				       =5_1_2
          _UpperCAmelCase  :  List[Any]				       =1_6
          _UpperCAmelCase  :  int				       =2
          _UpperCAmelCase  :  str				       =0.02
          _UpperCAmelCase  :  Tuple				       =3
          _UpperCAmelCase  :  int				       =4
          _UpperCAmelCase  :  Any				       =1_2_8
          _UpperCAmelCase  :  Tuple				       =2
          _UpperCAmelCase  :  int				       =9
          _UpperCAmelCase  :  Tuple				       =1
          _UpperCAmelCase  :  Any				       =None






       def      lowerCAmelCase						(     self)		->    Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase  :  Any				       =ids_tensor([self.batch_size, self.seq_length]    ,				self.vocab_size)

          _UpperCAmelCase  :  List[Any]				       =None
          if self.use_input_mask:
             _UpperCAmelCase  :  Any				       =random_attention_mask([self.batch_size, self.seq_length])

          _UpperCAmelCase  :  Optional[int]				       =None
          if self.use_token_type_ids:
             _UpperCAmelCase  :  List[str]				       =ids_tensor([self.batch_size, self.seq_length]    ,				self.type_vocab_size)

          _UpperCAmelCase  :  Union[str, Any]				       =None
          _UpperCAmelCase  :  Any				       =None
          _UpperCAmelCase  :  Dict				       =None
          if self.use_labels:
             _UpperCAmelCase  :  Optional[int]				       =ids_tensor([self.batch_size]    ,				self.type_sequence_label_size)
             _UpperCAmelCase  :  Any				       =ids_tensor([self.batch_size, self.seq_length]    ,				self.num_labels)
             _UpperCAmelCase  :  Dict				       =ids_tensor([self.batch_size]    ,				self.num_choices)

          _UpperCAmelCase  :  Any				       =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=snake_case    ,				)

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






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case)		->    str:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[int]				       =TFConvBertModel(config=snake_case)
          _UpperCAmelCase  :  str				       ={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

          _UpperCAmelCase  :  str				       =[input_ids, input_mask]
          _UpperCAmelCase  :  Union[str, Any]				       =model(snake_case)

          _UpperCAmelCase  :  Any				       =model(snake_case)

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






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case)		->    int:




          '''simple docstring'''
          _UpperCAmelCase  :  Dict				       =TFConvBertForMaskedLM(config=snake_case)
          _UpperCAmelCase  :  Optional[int]				       ={
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }
          _UpperCAmelCase  :  Union[str, Any]				       =model(snake_case)
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.seq_length, self.vocab_size))






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case)		->    Optional[Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  Any				       =self.num_labels
          _UpperCAmelCase  :  Tuple				       =TFConvBertForSequenceClassification(config=snake_case)
          _UpperCAmelCase  :  List[str]				       ={
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }

          _UpperCAmelCase  :  Any				       =model(snake_case)
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.num_labels))






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  Any				       =self.num_choices
          _UpperCAmelCase  :  Dict				       =TFConvBertForMultipleChoice(config=snake_case)
          _UpperCAmelCase  :  str				       =tf.tile(tf.expand_dims(snake_case    ,				1)    ,				(1, self.num_choices, 1))
          _UpperCAmelCase  :  int				       =tf.tile(tf.expand_dims(snake_case    ,				1)    ,				(1, self.num_choices, 1))
          _UpperCAmelCase  :  Optional[Any]				       =tf.tile(tf.expand_dims(snake_case    ,				1)    ,				(1, self.num_choices, 1))
          _UpperCAmelCase  :  Optional[int]				       ={
              'input_ids': multiple_choice_inputs_ids,
              'attention_mask': multiple_choice_input_mask,
              'token_type_ids': multiple_choice_token_type_ids,
          }
          _UpperCAmelCase  :  Tuple				       =model(snake_case)
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.num_choices))






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case)		->    List[str]:




          '''simple docstring'''
          _UpperCAmelCase  :  Any				       =self.num_labels
          _UpperCAmelCase  :  List[str]				       =TFConvBertForTokenClassification(config=snake_case)
          _UpperCAmelCase  :  List[Any]				       ={
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }
          _UpperCAmelCase  :  int				       =model(snake_case)
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.seq_length, self.num_labels))






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case)		->    Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase  :  Dict				       =TFConvBertForQuestionAnswering(config=snake_case)
          _UpperCAmelCase  :  Tuple				       ={
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }

          _UpperCAmelCase  :  List[str]				       =model(snake_case)
          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      lowerCAmelCase						(     self)		->    Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase  :  List[Any]				       =self.prepare_config_and_inputs()
          (
              (
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			
          )  :  List[Any]				       =config_and_inputs
          _UpperCAmelCase  :  Tuple				       ={'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
          return config, inputs_dict






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






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




          '''simple docstring'''
          _UpperCAmelCase  :  List[str]				       =TFConvBertModelTester(self)
          _UpperCAmelCase  :  Any				       =ConfigTester(self    ,				config_class=snake_case    ,				hidden_size=3_7)






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




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






       def      lowerCAmelCase						(     self)		->    List[str]:




          '''simple docstring'''
          _UpperCAmelCase  :  int				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*snake_case)






       def      lowerCAmelCase						(     self)		->    Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase  :  int				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*snake_case)






       def      lowerCAmelCase						(     self)		->    List[str]:




          '''simple docstring'''
          _UpperCAmelCase  :  int				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*snake_case)






       def      lowerCAmelCase						(     self)		->    List[str]:




          '''simple docstring'''
          _UpperCAmelCase  :  int				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*snake_case)






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




          '''simple docstring'''
          _UpperCAmelCase  :  Any				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*snake_case)






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




          '''simple docstring'''
          _UpperCAmelCase  :  Any				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*snake_case)






       @slow
       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase       ,			_UpperCAmelCase  :  int				       =self.model_tester.prepare_config_and_inputs_for_common()
          _UpperCAmelCase  :  List[str]				       =True
          _UpperCAmelCase  :  int				       =True

          if hasattr(snake_case    ,				'use_cache'):
             _UpperCAmelCase  :  List[Any]				       =True

          _UpperCAmelCase  :  List[str]				       =getattr(self.model_tester    ,				'encoder_seq_length'    ,				self.model_tester.seq_length)
          _UpperCAmelCase  :  str				       =getattr(self.model_tester    ,				'key_length'    ,				snake_case)

          for model_class in self.all_model_classes:
             _UpperCAmelCase  :  Tuple				       =self._prepare_for_class(snake_case    ,				snake_case)
             _UpperCAmelCase  :  Optional[Any]				       =model_class(snake_case)
             _UpperCAmelCase  :  Dict				       =len(model(snake_case))

             with tempfile.TemporaryDirectory() as tmpdirname:
                model.save_pretrained(snake_case    ,				saved_model=snake_case)
                _UpperCAmelCase  :  Optional[Any]				       =os.path.join(snake_case    ,				'saved_model'    ,				'1')
                _UpperCAmelCase  :  int				       =tf.keras.models.load_model(snake_case)
                _UpperCAmelCase  :  Union[str, Any]				       =model(snake_case)

                if self.is_encoder_decoder:
                   _UpperCAmelCase  :  Tuple				       =outputs['encoder_hidden_states']
                   _UpperCAmelCase  :  Union[str, Any]				       =outputs['encoder_attentions']
                else:
                   _UpperCAmelCase  :  Optional[Any]				       =outputs['hidden_states']
                   _UpperCAmelCase  :  Optional[int]				       =outputs['attentions']

                self.assertEqual(len(snake_case)    ,				snake_case)

                _UpperCAmelCase  :  str				       =getattr(
                    self.model_tester    ,				'expected_num_hidden_layers'    ,				self.model_tester.num_hidden_layers + 1)

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

                self.assertEqual(len(snake_case)    ,				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      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  Any				       =TFConvBertModel.from_pretrained('YituTech/conv-bert-base')
          self.assertIsNotNone(snake_case)






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




          '''simple docstring'''
          _UpperCAmelCase       ,			_UpperCAmelCase  :  int				       =self.model_tester.prepare_config_and_inputs_for_common()
          _UpperCAmelCase  :  Optional[Any]				       =True
          _UpperCAmelCase  :  List[Any]				       =getattr(self.model_tester    ,				'decoder_seq_length'    ,				self.model_tester.seq_length)
          _UpperCAmelCase  :  Optional[int]				       =getattr(self.model_tester    ,				'encoder_seq_length'    ,				self.model_tester.seq_length)
          _UpperCAmelCase  :  Optional[Any]				       =getattr(self.model_tester    ,				'key_length'    ,				snake_case)
          _UpperCAmelCase  :  List[Any]				       =getattr(self.model_tester    ,				'key_length'    ,				snake_case)

          def check_decoder_attentions_output(snake_case):
             _UpperCAmelCase  :  Dict				       =len(snake_case)
             self.assertEqual(out_len % 2    ,				0)
             _UpperCAmelCase  :  Dict				       =outputs.decoder_attentions
             self.assertEqual(len(snake_case)    ,				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(snake_case):
             _UpperCAmelCase  :  List[str]				       =[
                 t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
             ]
             self.assertEqual(len(snake_case)    ,				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:
             _UpperCAmelCase  :  Any				       =True
             _UpperCAmelCase  :  List[Any]				       =False
             _UpperCAmelCase  :  List[Any]				       =model_class(snake_case)
             _UpperCAmelCase  :  int				       =model(self._prepare_for_class(snake_case    ,				snake_case))
             _UpperCAmelCase  :  Optional[int]				       =len(snake_case)
             self.assertEqual(config.output_hidden_states    ,				snake_case)
             check_encoder_attentions_output(snake_case)

             if self.is_encoder_decoder:
                _UpperCAmelCase  :  Dict				       =model_class(snake_case)
                _UpperCAmelCase  :  List[str]				       =model(self._prepare_for_class(snake_case    ,				snake_case))
                self.assertEqual(config.output_hidden_states    ,				snake_case)
                check_decoder_attentions_output(snake_case)

             # Check that output attentions can also be changed via the config
             del inputs_dict["output_attentions"]
             _UpperCAmelCase  :  Optional[int]				       =True
             _UpperCAmelCase  :  Tuple				       =model_class(snake_case)
             _UpperCAmelCase  :  List[str]				       =model(self._prepare_for_class(snake_case    ,				snake_case))
             self.assertEqual(config.output_hidden_states    ,				snake_case)
             check_encoder_attentions_output(snake_case)

             # Check attention is always last and order is fine
             _UpperCAmelCase  :  Optional[int]				       =True
             _UpperCAmelCase  :  Union[str, Any]				       =True
             _UpperCAmelCase  :  int				       =model_class(snake_case)
             _UpperCAmelCase  :  str				       =model(self._prepare_for_class(snake_case    ,				snake_case))

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






@require_tf
class    __magic_name__				(						unittest.TestCase							):






       @slow
       def      lowerCAmelCase						(     self)		->    Tuple:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[Any]				       =TFConvBertModel.from_pretrained('YituTech/conv-bert-base')
          _UpperCAmelCase  :  Any				       =tf.constant([[0, 1, 2, 3, 4, 5]])
          _UpperCAmelCase  :  List[Any]				       =model(snake_case)[0]

          _UpperCAmelCase  :  Dict				       =[1, 6, 7_6_8]
          self.assertEqual(output.shape    ,				snake_case)

          _UpperCAmelCase  :  Union[str, Any]				       =tf.constant(
              [
                  [
                      [-0.03_47_54_93, -0.4_68_60_34, -0.30_63_88_32],
                      [0.22_63_72_48, -0.26_98_86_46, -0.7_42_34_24],
                      [0.10_32_48_68, -0.45_01_35_08, -0.58_28_07_84],
                  ]
              ])
          tf.debugging.assert_near(output[:, :3, :3]    ,				snake_case    ,				atol=1E-4)





'''simple docstring'''






from typing import Any

import numpy as np





def lowerCamelCase__     (		__lowerCamelCase			:				np.ndarray				):







   '''simple docstring'''




   return np.array_equal(__lowerCamelCase			,       matrix.conjugate().T				)





def lowerCamelCase__     (		__lowerCamelCase			:				np.ndarray			,       __lowerCamelCase			:				np.ndarray				):







   '''simple docstring'''




   _UpperCAmelCase  :  str				       =v.conjugate().T
   _UpperCAmelCase  :  Optional[int]				       =v_star.dot(__lowerCamelCase				)
   assert isinstance(__lowerCamelCase			,       np.ndarray				)
   return (v_star_dot.dot(__lowerCamelCase				)) / (v_star.dot(__lowerCamelCase				))





def lowerCamelCase__     (		):







   '''simple docstring'''




   _UpperCAmelCase  :  Optional[Any]				       =np.array([[2, 2 + 1J, 4], [2 - 1J, 3, 1J], [4, -1J, 1]]				)
   _UpperCAmelCase  :  List[str]				       =np.array([[1], [2], [3]]				)
   assert is_hermitian(__lowerCamelCase				), f"{a} is not hermitian."
   print(rayleigh_quotient(__lowerCamelCase			,       __lowerCamelCase				)				)

   _UpperCAmelCase  :  List[str]				       =np.array([[1, 2, 4], [2, 3, -1], [4, -1, 1]]				)
   assert is_hermitian(__lowerCamelCase				), f"{a} is not hermitian."
   assert rayleigh_quotient(__lowerCamelCase			,       __lowerCamelCase				) == float(3				)


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 tests()

