


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       UpperCAmelCase_   :

   '''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		,							):






     """simple docstring"""





     lowerCamelCase					:   Dict       =							parent
     lowerCamelCase					:   Tuple       =							13
     lowerCamelCase					:   List[Any]       =							7
     lowerCamelCase					:   Tuple       =							True
     lowerCamelCase					:   Dict       =							True
     lowerCamelCase					:   Any       =							True
     lowerCamelCase					:   int       =							True
     lowerCamelCase					:   str       =							99
     lowerCamelCase					:   List[str]       =							384
     lowerCamelCase					:   int       =							2
     lowerCamelCase					:   str       =							4
     lowerCamelCase					:   Optional[int]       =							37
     lowerCamelCase					:   Union[str, Any]       =							"gelu"
     lowerCamelCase					:   Tuple       =							0.1
     lowerCamelCase					:   str       =							0.1
     lowerCamelCase					:   Union[str, Any]       =							512
     lowerCamelCase					:   Union[str, Any]       =							16
     lowerCamelCase					:   Union[str, Any]       =							2
     lowerCamelCase					:   List[str]       =							0.02
     lowerCamelCase					:   int       =							3
     lowerCamelCase					:   Any       =							4
     lowerCamelCase					:   Union[str, Any]       =							128
     lowerCamelCase					:   Any       =							2
     lowerCamelCase					:   int       =							9
     lowerCamelCase					:   Union[str, Any]       =							1
     lowerCamelCase					:   str       =							None






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   List[str]       =							ids_tensor([self.batch_size, self.seq_length]		,							self.vocab_size					)

     lowerCamelCase					:   str       =							None
     if self.use_input_mask:
       lowerCamelCase					:   Optional[Any]       =							random_attention_mask([self.batch_size, self.seq_length]					)

     lowerCamelCase					:   Optional[int]       =							None
     if self.use_token_type_ids:
       lowerCamelCase					:   Union[str, Any]       =							ids_tensor([self.batch_size, self.seq_length]		,							self.type_vocab_size					)

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

     lowerCamelCase					:   int       =							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 _snake_case       (						self		,							__A		,							__A		,							__A		,							__A		,							__A		,							__A		,							__A					):






     """simple docstring"""





     lowerCamelCase					:   Optional[int]       =							TFConvBertModel(config=__A					)
     lowerCamelCase					:   Tuple       =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

     lowerCamelCase					:   List[str]       =							[input_ids, input_mask]
     lowerCamelCase					:   List[str]       =							model(__A					)

     lowerCamelCase					:   Any       =							model(__A					)

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






   def _snake_case       (						self		,							__A		,							__A		,							__A		,							__A		,							__A		,							__A		,							__A					):






     """simple docstring"""





     lowerCamelCase					:   Optional[int]       =							TFConvBertForMaskedLM(config=__A					)
     lowerCamelCase					:   str       =							{
         "input_ids": input_ids,
         "attention_mask": input_mask,
         "token_type_ids": token_type_ids,
     }
     lowerCamelCase					:   List[str]       =							model(__A					)
     self.parent.assertEqual(result.logits.shape		,							(self.batch_size, self.seq_length, self.vocab_size)					)






   def _snake_case       (						self		,							__A		,							__A		,							__A		,							__A		,							__A		,							__A		,							__A					):






     """simple docstring"""





     lowerCamelCase					:   Optional[Any]       =							self.num_labels
     lowerCamelCase					:   Any       =							TFConvBertForSequenceClassification(config=__A					)
     lowerCamelCase					:   List[Any]       =							{
         "input_ids": input_ids,
         "attention_mask": input_mask,
         "token_type_ids": token_type_ids,
     }

     lowerCamelCase					:   Union[str, Any]       =							model(__A					)
     self.parent.assertEqual(result.logits.shape		,							(self.batch_size, self.num_labels)					)






   def _snake_case       (						self		,							__A		,							__A		,							__A		,							__A		,							__A		,							__A		,							__A					):






     """simple docstring"""





     lowerCamelCase					:   Any       =							self.num_choices
     lowerCamelCase					:   Optional[int]       =							TFConvBertForMultipleChoice(config=__A					)
     lowerCamelCase					:   Any       =							tf.tile(tf.expand_dims(__A		,							1					)		,							(1, self.num_choices, 1)					)
     lowerCamelCase					:   int       =							tf.tile(tf.expand_dims(__A		,							1					)		,							(1, self.num_choices, 1)					)
     lowerCamelCase					:   List[Any]       =							tf.tile(tf.expand_dims(__A		,							1					)		,							(1, self.num_choices, 1)					)
     lowerCamelCase					:   Tuple       =							{
         "input_ids": multiple_choice_inputs_ids,
         "attention_mask": multiple_choice_input_mask,
         "token_type_ids": multiple_choice_token_type_ids,
     }
     lowerCamelCase					:   Dict       =							model(__A					)
     self.parent.assertEqual(result.logits.shape		,							(self.batch_size, self.num_choices)					)






   def _snake_case       (						self		,							__A		,							__A		,							__A		,							__A		,							__A		,							__A		,							__A					):






     """simple docstring"""





     lowerCamelCase					:   int       =							self.num_labels
     lowerCamelCase					:   int       =							TFConvBertForTokenClassification(config=__A					)
     lowerCamelCase					:   str       =							{
         "input_ids": input_ids,
         "attention_mask": input_mask,
         "token_type_ids": token_type_ids,
     }
     lowerCamelCase					:   Any       =							model(__A					)
     self.parent.assertEqual(result.logits.shape		,							(self.batch_size, self.seq_length, self.num_labels)					)






   def _snake_case       (						self		,							__A		,							__A		,							__A		,							__A		,							__A		,							__A		,							__A					):






     """simple docstring"""





     lowerCamelCase					:   int       =							TFConvBertForQuestionAnswering(config=__A					)
     lowerCamelCase					:   Any       =							{
         "input_ids": input_ids,
         "attention_mask": input_mask,
         "token_type_ids": token_type_ids,
     }

     lowerCamelCase					:   Tuple       =							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 _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Any       =							self.prepare_config_and_inputs()
     (
         (
         lowerCamelCase    
     )       ,      (
         lowerCamelCase    
     )       ,      (
         lowerCamelCase    
     )       ,      (
         lowerCamelCase    
     )       ,      (
         lowerCamelCase    
     )       ,      (
         lowerCamelCase    
     )       ,      (
         lowerCamelCase    
     )       ,          
     )					:   str       =							config_and_inputs
     lowerCamelCase					:   List[str]       =							{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
     return config, inputs_dict



@require_tf
class       UpperCAmelCase_   (   UpperCAmelCase__						,					UpperCAmelCase__						,					unittest.TestCase       ):

   '''simple docstring'''




   __A					: Any							=						(
       (
           TFConvBertModel,
           TFConvBertForMaskedLM,
           TFConvBertForQuestionAnswering,
           TFConvBertForSequenceClassification,
           TFConvBertForTokenClassification,
           TFConvBertForMultipleChoice,
       )
       if is_tf_available()
       else ()
   )
   __A					: 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 {}
   )
   __A					: List[Any]							=						False
   __A					: Dict							=						False
   __A					: Any							=						False






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   str       =							TFConvBertModelTester(self					)
     lowerCamelCase					:   int       =							ConfigTester(self		,							config_class=__A		,							hidden_size=37					)






   def _snake_case       (						self					):






     """simple docstring"""





     self.config_tester.run_common_tests()






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Optional[int]       =							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__A					)






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Tuple       =							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*__A					)






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   int       =							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*__A					)






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Any       =							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*__A					)






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Tuple       =							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*__A					)






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Any       =							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*__A					)






   @slow
   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase       ,      lowerCamelCase					:   Union[str, Any]       =							self.model_tester.prepare_config_and_inputs_for_common()
     lowerCamelCase					:   List[str]       =							True
     lowerCamelCase					:   List[str]       =							True

     if hasattr(__A		,							"use_cache"					):
       lowerCamelCase					:   List[Any]       =							True

     lowerCamelCase					:   Union[str, Any]       =							getattr(self.model_tester		,							"encoder_seq_length"		,							self.model_tester.seq_length					)
     lowerCamelCase					:   List[Any]       =							getattr(self.model_tester		,							"key_length"		,							__A					)

     for model_class in self.all_model_classes:
       lowerCamelCase					:   List[Any]       =							self._prepare_for_class(__A		,							__A					)
       lowerCamelCase					:   List[Any]       =							model_class(__A					)
       lowerCamelCase					:   Tuple       =							len(model(__A					)					)

       with tempfile.TemporaryDirectory() as tmpdirname:
         model.save_pretrained(__A		,							saved_model=__A					)
         lowerCamelCase					:   Tuple       =							os.path.join(__A		,							"saved_model"		,							"1"					)
         lowerCamelCase					:   Optional[Any]       =							tf.keras.models.load_model(__A					)
         lowerCamelCase					:   str       =							model(__A					)

         if self.is_encoder_decoder:
           lowerCamelCase					:   str       =							outputs["encoder_hidden_states"]
           lowerCamelCase					:   Dict       =							outputs["encoder_attentions"]
         else:
           lowerCamelCase					:   Optional[Any]       =							outputs["hidden_states"]
           lowerCamelCase					:   List[str]       =							outputs["attentions"]

         self.assertEqual(len(__A					)		,							__A					)

         lowerCamelCase					:   List[str]       =							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 _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Tuple       =							TFConvBertModel.from_pretrained("YituTech/conv-bert-base"					)
     self.assertIsNotNone(__A					)






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase       ,      lowerCamelCase					:   Union[str, Any]       =							self.model_tester.prepare_config_and_inputs_for_common()
     lowerCamelCase					:   List[str]       =							True
     lowerCamelCase					:   Optional[Any]       =							getattr(self.model_tester		,							"decoder_seq_length"		,							self.model_tester.seq_length					)
     lowerCamelCase					:   int       =							getattr(self.model_tester		,							"encoder_seq_length"		,							self.model_tester.seq_length					)
     lowerCamelCase					:   Any       =							getattr(self.model_tester		,							"key_length"		,							__A					)
     lowerCamelCase					:   Union[str, Any]       =							getattr(self.model_tester		,							"key_length"		,							__A					)

     def check_decoder_attentions_output(__A					):
       lowerCamelCase					:   Tuple       =							len(__A					)
       self.assertEqual(out_len % 2		,							0					)
       lowerCamelCase					:   List[str]       =							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					):
       lowerCamelCase					:   Dict       =							[
           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:
       lowerCamelCase					:   Dict       =							True
       lowerCamelCase					:   str       =							False
       lowerCamelCase					:   Union[str, Any]       =							model_class(__A					)
       lowerCamelCase					:   Union[str, Any]       =							model(self._prepare_for_class(__A		,							__A					)					)
       lowerCamelCase					:   int       =							len(__A					)
       self.assertEqual(config.output_hidden_states		,							__A					)
       check_encoder_attentions_output(__A					)

       if self.is_encoder_decoder:
         lowerCamelCase					:   Optional[int]       =							model_class(__A					)
         lowerCamelCase					:   Any       =							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"]
       lowerCamelCase					:   Optional[int]       =							True
       lowerCamelCase					:   List[str]       =							model_class(__A					)
       lowerCamelCase					:   Optional[Any]       =							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
       lowerCamelCase					:   Dict       =							True
       lowerCamelCase					:   Optional[Any]       =							True
       lowerCamelCase					:   List[str]       =							model_class(__A					)
       lowerCamelCase					:   Tuple       =							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       UpperCAmelCase_   (   unittest.TestCase       ):

   '''simple docstring'''




   @slow
   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Any       =							TFConvBertModel.from_pretrained("YituTech/conv-bert-base"					)
     lowerCamelCase					:   int       =							tf.constant([[0, 1, 2, 3, 4, 5]]					)
     lowerCamelCase					:   Optional[int]       =							model(__A					)[0]

     lowerCamelCase					:   Optional[int]       =							[1, 6, 768]
     self.assertEqual(output.shape		,							__A					)

     lowerCamelCase					:   Any       =							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 ....configuration_utils import PretrainedConfig
from ....utils import logging


__UpperCAmelCase					=logging.get_logger(__name__)

__UpperCAmelCase					={
    "Visual-Attention-Network/van-base": (
        "https://huggingface.co/Visual-Attention-Network/van-base/blob/main/config.json"
    ),
}




class        a__ (  UpperCAmelCase__					):
     lowerCamelCase    :	Optional[Any]				="van"






     def __init__(					self						:						Union[str, Any]    ,							a						:						List[Any]=2_24    ,							a						:						Optional[int]=3    ,							a						:						Optional[Any]=[7, 3, 3, 3]    ,							a						:						int=[4, 2, 2, 2]    ,							a						:						Optional[int]=[64, 1_28, 3_20, 5_12]    ,							a						:						str=[3, 3, 12, 3]    ,							a						:						str=[8, 8, 4, 4]    ,							a						:						int="gelu"    ,							a						:						int=0.02    ,							a						:						Optional[int]=1e-6    ,							a						:						List[str]=1e-2    ,							a						:						Union[str, Any]=0.0    ,							a						:						int=0.0    ,							**a						:						Optional[Any]    ,							):





         """simple docstring"""



         super().__init__(**a       )
         __lowerCamelCase            =    image_size
         __lowerCamelCase            =    num_channels
         __lowerCamelCase            =    patch_sizes
         __lowerCamelCase            =    strides
         __lowerCamelCase            =    hidden_sizes
         __lowerCamelCase            =    depths
         __lowerCamelCase            =    mlp_ratios
         __lowerCamelCase            =    hidden_act
         __lowerCamelCase            =    initializer_range
         __lowerCamelCase            =    layer_norm_eps
         __lowerCamelCase            =    layer_scale_init_value
         __lowerCamelCase            =    drop_path_rate
         __lowerCamelCase            =    dropout_rate
