



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



 '''simple docstring'''







 def __init__(self				,     A				,     A=13				,     A=7				,     A=True				,     A=True				,     A=True				,     A=True				,     A=99				,     A=32				,     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=None				,     )					->  List[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       a__						(self )					->  List[str]:


      """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=A				,     )

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



 def       a__						(self				,     A				,     A				,     A				,     A				,     A				,     A				,     A )					->  Dict:


      """simple docstring"""



      _a											=						TFConvBertModel(config=A )
      _a											=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

      _a											=						[input_ids, input_mask]
      _a											=						model(A )

      _a											=						model(A )

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



 def       a__						(self				,     A				,     A				,     A				,     A				,     A				,     A				,     A )					->  Any:


      """simple docstring"""



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



 def       a__						(self				,     A				,     A				,     A				,     A				,     A				,     A				,     A )					->  Tuple:


      """simple docstring"""



      _a											=						self.num_labels
      _a											=						TFConvBertForSequenceClassification(config=A )
      _a											=						{
          '''input_ids''': input_ids,
          '''attention_mask''': input_mask,
          '''token_type_ids''': token_type_ids,
      }

      _a											=						model(A )
      self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_labels) )



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


      """simple docstring"""



      _a											=						self.num_choices
      _a											=						TFConvBertForMultipleChoice(config=A )
      _a											=						tf.tile(tf.expand_dims(A				,     1 )				,     (1, self.num_choices, 1) )
      _a											=						tf.tile(tf.expand_dims(A				,     1 )				,     (1, self.num_choices, 1) )
      _a											=						tf.tile(tf.expand_dims(A				,     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(A )
      self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_choices) )



 def       a__						(self				,     A				,     A				,     A				,     A				,     A				,     A				,     A )					->  int:


      """simple docstring"""



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



 def       a__						(self				,     A				,     A				,     A				,     A				,     A				,     A				,     A )					->  Dict:


      """simple docstring"""



      _a											=						TFConvBertForQuestionAnswering(config=A )
      _a											=						{
          '''input_ids''': input_ids,
          '''attention_mask''': input_mask,
          '''token_type_ids''': token_type_ids,
      }

      _a											=						model(A )
      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       a__						(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 __A    (						A     ,     A     ,     unittest.TestCase		):



 '''simple docstring'''







 __lowerCamelCase					: List[str]    = (
     (
         TFConvBertModel,
         TFConvBertForMaskedLM,
         TFConvBertForQuestionAnswering,
         TFConvBertForSequenceClassification,
         TFConvBertForTokenClassification,
         TFConvBertForMultipleChoice,
     )
     if is_tf_available()
     else ()
 )
 __lowerCamelCase					: Optional[int]    = (
     {
         'feature-extraction': TFConvBertModel,
         'fill-mask': TFConvBertForMaskedLM,
         'question-answering': TFConvBertForQuestionAnswering,
         'text-classification': TFConvBertForSequenceClassification,
         'token-classification': TFConvBertForTokenClassification,
         'zero-shot': TFConvBertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 __lowerCamelCase					: List[Any]    = False
 __lowerCamelCase					: List[str]    = False
 __lowerCamelCase					: Union[str, Any]    = False



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


      """simple docstring"""



      _a											=						TFConvBertModelTester(self )
      _a											=						ConfigTester(self				,     config_class=A				,     hidden_size=37 )



 def       a__						(self )					->  str:


      """simple docstring"""



      self.config_tester.run_common_tests()



 def       a__						(self )					->  int:


      """simple docstring"""



      _a											=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*A )



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


      """simple docstring"""



      _a											=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*A )



 def       a__						(self )					->  Dict:


      """simple docstring"""



      _a											=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_multiple_choice(*A )



 def       a__						(self )					->  Tuple:


      """simple docstring"""



      _a											=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*A )



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


      """simple docstring"""



      _a											=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*A )



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


      """simple docstring"""



      _a											=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*A )



 @slow
 def       a__						(self )					->  int:


      """simple docstring"""



      _a						,					_a											=						self.model_tester.prepare_config_and_inputs_for_common()
      _a											=						True
      _a											=						True

      if hasattr(A				,     '''use_cache''' ):
           _a											=						True

      _a											=						getattr(self.model_tester				,     '''encoder_seq_length'''				,     self.model_tester.seq_length )
      _a											=						getattr(self.model_tester				,     '''key_length'''				,     A )

      for model_class in self.all_model_classes:
           _a											=						self._prepare_for_class(A				,     A )
           _a											=						model_class(A )
           _a											=						len(model(A ) )

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

                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(A )				,     A )

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

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

                self.assertEqual(len(A )				,     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       a__						(self )					->  Tuple:


      """simple docstring"""



      _a											=						TFConvBertModel.from_pretrained('''YituTech/conv-bert-base''' )
      self.assertIsNotNone(A )



 def       a__						(self )					->  int:


      """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'''				,     A )
      _a											=						getattr(self.model_tester				,     '''key_length'''				,     A )

      def check_decoder_attentions_output(A ):
           _a											=						len(A )
           self.assertEqual(out_len % 2				,     0 )
           _a											=						outputs.decoder_attentions
           self.assertEqual(len(A )				,     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(A ):
           _a											=						[
               t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
           ]
           self.assertEqual(len(A )				,     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(A )
           _a											=						model(self._prepare_for_class(A				,     A ) )
           _a											=						len(A )
           self.assertEqual(config.output_hidden_states				,     A )
           check_encoder_attentions_output(A )

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

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

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

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






@require_tf
class __A    (						unittest.TestCase		):



 '''simple docstring'''







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


      """simple docstring"""



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

      _a											=						[1, 6, 768]
      self.assertEqual(output.shape				,     A )

      _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]				,     A				,     atol=1E-4 )








'''simple docstring'''





from __future__ import annotations

import math




def lowerCAmelCase						(__A						,			__A						,			__A						,			__A						,			__A):



     """simple docstring"""



     if depth < 0:
          raise ValueError('''Depth cannot be less than 0''')

     if not scores:
          raise ValueError('''Scores cannot be empty''')

     if depth == height:
          return scores[node_index]

     return (
         max(
             minimax(depth + 1						,			node_index * 2						,			__A						,			__A						,			__A)						,			minimax(depth + 1						,			node_index * 2 + 1						,			__A						,			__A						,			__A)						,			)
         if is_max
         else min(
             minimax(depth + 1						,			node_index * 2						,			__A						,			__A						,			__A)						,			minimax(depth + 1						,			node_index * 2 + 1						,			__A						,			__A						,			__A)						,			)
     )




def lowerCAmelCase						():



     """simple docstring"""



     _a											=						[90, 23, 6, 33, 21, 65, 123, 34_423]
     _a											=						math.log(len(__A)						,			2)
     print(F'''Optimal value : {minimax(0						,			0						,			__A						,			__A						,			__A)}''')


if __name__ == "__main__":
  import doctest

  doctest.testmod()
  main()



