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





       '''simple docstring'''



       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.0_2	,   __lowerCAmelCase=3	,   __lowerCAmelCase=4	,   __lowerCAmelCase=None	,   )  ->  int:
        lowercase__				:    List[Any]  =					parent
        lowercase__				:    Any  =					13
        lowercase__				:    List[str]  =					7
        lowercase__				:    Dict  =					True
        lowercase__				:    Optional[Any]  =					True
        lowercase__				:    Tuple  =					True
        lowercase__				:    List[str]  =					True
        lowercase__				:    List[str]  =					99
        lowercase__				:    Tuple  =					384
        lowercase__				:    Optional[Any]  =					2
        lowercase__				:    Any  =					4
        lowercase__				:    str  =					37
        lowercase__				:    Optional[Any]  =					"gelu"
        lowercase__				:    List[Any]  =					0.1
        lowercase__				:    Union[str, Any]  =					0.1
        lowercase__				:    Dict  =					512
        lowercase__				:    int  =					16
        lowercase__				:    Optional[int]  =					2
        lowercase__				:    Any  =					0.0_2
        lowercase__				:    str  =					3
        lowercase__				:    int  =					4
        lowercase__				:    Dict  =					128
        lowercase__				:    Any  =					2
        lowercase__				:    Tuple  =					9
        lowercase__				:    List[Any]  =					1
        lowercase__				:    Any  =					None

       def _lowerCAmelCase(						self )  ->  List[Any]:
        lowercase__				:    List[str]  =					ids_tensor([self.batch_size, self.seq_length]	,   self.vocab_size )

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

        lowercase__				:    List[str]  =					None
        if self.use_token_type_ids:
         lowercase__				:    List[Any]  =					ids_tensor([self.batch_size, self.seq_length]	,   self.type_vocab_size )

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

        lowercase__				:    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=_A	,   )

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

       def _lowerCAmelCase(						self	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase )  ->  str:
        lowercase__				:    Optional[int]  =					TFConvBertModel(config=_A )
        lowercase__				:    Optional[int]  =					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

        lowercase__				:    str  =					[input_ids, input_mask]
        lowercase__				:    List[str]  =					model(_A )

        lowercase__				:    Dict  =					model(_A )

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

       def _lowerCAmelCase(						self	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase )  ->  List[str]:
        lowercase__				:    Dict  =					TFConvBertForMaskedLM(config=_A )
        lowercase__				:    Union[str, Any]  =					{
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }
        lowercase__				:    List[Any]  =					model(_A )
        self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.seq_length, self.vocab_size) )

       def _lowerCAmelCase(						self	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase )  ->  str:
        lowercase__				:    List[Any]  =					self.num_labels
        lowercase__				:    Any  =					TFConvBertForSequenceClassification(config=_A )
        lowercase__				:    Union[str, Any]  =					{
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }

        lowercase__				:    Optional[Any]  =					model(_A )
        self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.num_labels) )

       def _lowerCAmelCase(						self	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase )  ->  Tuple:
        lowercase__				:    Optional[Any]  =					self.num_choices
        lowercase__				:    Optional[int]  =					TFConvBertForMultipleChoice(config=_A )
        lowercase__				:    Any  =					tf.tile(tf.expand_dims(_A	,   1 )	,   (1, self.num_choices, 1) )
        lowercase__				:    Any  =					tf.tile(tf.expand_dims(_A	,   1 )	,   (1, self.num_choices, 1) )
        lowercase__				:    Union[str, Any]  =					tf.tile(tf.expand_dims(_A	,   1 )	,   (1, self.num_choices, 1) )
        lowercase__				:    int  =					{
            "input_ids": multiple_choice_inputs_ids,
            "attention_mask": multiple_choice_input_mask,
            "token_type_ids": multiple_choice_token_type_ids,
        }
        lowercase__				:    int  =					model(_A )
        self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.num_choices) )

       def _lowerCAmelCase(						self	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase )  ->  Optional[Any]:
        lowercase__				:    List[str]  =					self.num_labels
        lowercase__				:    Union[str, Any]  =					TFConvBertForTokenClassification(config=_A )
        lowercase__				:    Tuple  =					{
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }
        lowercase__				:    str  =					model(_A )
        self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.seq_length, self.num_labels) )

       def _lowerCAmelCase(						self	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase )  ->  int:
        lowercase__				:    List[Any]  =					TFConvBertForQuestionAnswering(config=_A )
        lowercase__				:    Dict  =					{
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }

        lowercase__				:    Any  =					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 _lowerCAmelCase(						self )  ->  Tuple:
        lowercase__				:    Any  =					self.prepare_config_and_inputs()
        (
            lowercase__    
        )				:    List[Any]  =					config_and_inputs
        lowercase__				:    Any  =					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
        return config, inputs_dict



@require_tf
class 			UpperCAmelCase  (						A__    ,						A__    ,						unittest.TestCase					):





       '''simple docstring'''



       SCREAMING_SNAKE_CASE							=    (
           (
               TFConvBertModel,
               TFConvBertForMaskedLM,
               TFConvBertForQuestionAnswering,
               TFConvBertForSequenceClassification,
               TFConvBertForTokenClassification,
               TFConvBertForMultipleChoice,
           )
           if is_tf_available()
           else ()
       )
       SCREAMING_SNAKE_CASE							=    (
           {
               "feature-extraction": TFConvBertModel,
               "fill-mask": TFConvBertForMaskedLM,
               "question-answering": TFConvBertForQuestionAnswering,
               "text-classification": TFConvBertForSequenceClassification,
               "token-classification": TFConvBertForTokenClassification,
               "zero-shot": TFConvBertForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       SCREAMING_SNAKE_CASE							=    False
       SCREAMING_SNAKE_CASE							=    False
       SCREAMING_SNAKE_CASE							=    False

       def _lowerCAmelCase(						self )  ->  Optional[int]:
        lowercase__				:    str  =					TFConvBertModelTester(self )
        lowercase__				:    Tuple  =					ConfigTester(self	,   config_class=_A	,   hidden_size=37 )

       def _lowerCAmelCase(						self )  ->  str:
        self.config_tester.run_common_tests()

       def _lowerCAmelCase(						self )  ->  Optional[int]:
        lowercase__				:    Union[str, Any]  =					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_A )

       def _lowerCAmelCase(						self )  ->  Optional[int]:
        lowercase__				:    int  =					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*_A )

       def _lowerCAmelCase(						self )  ->  str:
        lowercase__				:    Dict  =					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*_A )

       def _lowerCAmelCase(						self )  ->  Dict:
        lowercase__				:    List[str]  =					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*_A )

       def _lowerCAmelCase(						self )  ->  List[Any]:
        lowercase__				:    List[Any]  =					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*_A )

       def _lowerCAmelCase(						self )  ->  int:
        lowercase__				:    List[str]  =					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*_A )

       @slow
       def _lowerCAmelCase(						self )  ->  str:
        lowercase__				:    Union[str, Any]  =					self.model_tester.prepare_config_and_inputs_for_common()
        lowercase__				:    List[str]  =					True
        lowercase__				:    Any  =					True

        if hasattr(_A	,   '''use_cache''' ):
         lowercase__				:    List[Any]  =					True

        lowercase__				:    int  =					getattr(self.model_tester	,   '''encoder_seq_length'''	,   self.model_tester.seq_length )
        lowercase__				:    Optional[Any]  =					getattr(self.model_tester	,   '''key_length'''	,   _A )

        for model_class in self.all_model_classes:
         lowercase__				:    List[str]  =					self._prepare_for_class(_A	,   _A )
         lowercase__				:    List[Any]  =					model_class(_A )
         lowercase__				:    Optional[int]  =					len(model(_A ) )

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

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

          self.assertEqual(len(_A )	,   _A )

          lowercase__				:    Any  =					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 _lowerCAmelCase(						self )  ->  List[str]:
        lowercase__				:    str  =					TFConvBertModel.from_pretrained('''YituTech/conv-bert-base''' )
        self.assertIsNotNone(_A )


       def _lowerCAmelCase(						self )  ->  str:
        lowercase__				:    Any  =					self.model_tester.prepare_config_and_inputs_for_common()
        lowercase__				:    List[str]  =					True
        lowercase__				:    List[str]  =					getattr(self.model_tester	,   '''decoder_seq_length'''	,   self.model_tester.seq_length )
        lowercase__				:    Any  =					getattr(self.model_tester	,   '''encoder_seq_length'''	,   self.model_tester.seq_length )
        lowercase__				:    Optional[int]  =					getattr(self.model_tester	,   '''key_length'''	,   _A )
        lowercase__				:    int  =					getattr(self.model_tester	,   '''key_length'''	,   _A )

        def check_decoder_attentions_output(__lowerCAmelCase ):
         lowercase__				:    int  =					len(_A )
         self.assertEqual(out_len % 2	,   0 )
         lowercase__				:    Tuple  =					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(__lowerCAmelCase ):
         lowercase__				:    int  =					[
             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:
         lowercase__				:    Optional[Any]  =					True
         lowercase__				:    Optional[Any]  =					False
         lowercase__				:    Tuple  =					model_class(_A )
         lowercase__				:    Any  =					model(self._prepare_for_class(_A	,   _A ) )
         lowercase__				:    Tuple  =					len(_A )
         self.assertEqual(config.output_hidden_states	,   _A )
         check_encoder_attentions_output(_A )

         if self.is_encoder_decoder:
          lowercase__				:    Optional[Any]  =					model_class(_A )
          lowercase__				:    int  =					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"]
         lowercase__				:    str  =					True
         lowercase__				:    int  =					model_class(_A )
         lowercase__				:    List[str]  =					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
         lowercase__				:    str  =					True
         lowercase__				:    int  =					True
         lowercase__				:    Dict  =					model_class(_A )
         lowercase__				:    str  =					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 _lowerCAmelCase(						self )  ->  Tuple:
        lowercase__				:    List[str]  =					TFConvBertModel.from_pretrained('''YituTech/conv-bert-base''' )
        lowercase__				:    int  =					tf.constant([[0, 1, 2, 3, 4, 5]] )
        lowercase__				:    Tuple  =					model(_A )[0]

        lowercase__				:    List[Any]  =					[1, 6, 768]
        self.assertEqual(output.shape	,   _A )

        lowercase__				:    Union[str, Any]  =					tf.constant(
            [
                [
                    [-0.0_3_4_7_5_4_9_3, -0.4_6_8_6_0_3_4, -0.3_0_6_3_8_8_3_2],
                    [0.2_2_6_3_7_2_4_8, -0.2_6_9_8_8_6_4_6, -0.7_4_2_3_4_2_4],
                    [0.1_0_3_2_4_8_6_8, -0.4_5_0_1_3_5_0_8, -0.5_8_2_8_0_7_8_4],
                ]
            ] )
        tf.debugging.assert_near(output[:, :3, :3]	,   _A	,   atol=1E-4 )


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__lowerCamelCase					:	Union[str, Any]       =  {
    '''configuration_chinese_clip''': [
        '''CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''ChineseCLIPConfig''',
        '''ChineseCLIPOnnxConfig''',
        '''ChineseCLIPTextConfig''',
        '''ChineseCLIPVisionConfig''',
    ],
    '''processing_chinese_clip''': ['''ChineseCLIPProcessor'''],
}

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__lowerCamelCase					:	Any       =  ['''ChineseCLIPFeatureExtractor''']
		__lowerCamelCase					:	Optional[int]       =  ['''ChineseCLIPImageProcessor''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__lowerCamelCase					:	int       =  [
		    '''CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''ChineseCLIPModel''',
		    '''ChineseCLIPPreTrainedModel''',
		    '''ChineseCLIPTextModel''',
		    '''ChineseCLIPVisionModel''',
		]

if TYPE_CHECKING:
		from .configuration_chinese_clip import (
		    CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    ChineseCLIPConfig,
		    ChineseCLIPOnnxConfig,
		    ChineseCLIPTextConfig,
		    ChineseCLIPVisionConfig,
		)
		from .processing_chinese_clip import ChineseCLIPProcessor

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_chinese_clip import ChineseCLIPFeatureExtractor, ChineseCLIPImageProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_chinese_clip import (
				    CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
				    ChineseCLIPModel,
				    ChineseCLIPPreTrainedModel,
				    ChineseCLIPTextModel,
				    ChineseCLIPVisionModel,
				)

else:
		import sys

		__lowerCamelCase					:	Any       =  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
