


"""simple docstring"""






from __future__ import annotations

import unittest

from transformers import FunnelConfig, is_tf_available
from transformers.testing_utils import require_tf

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 (
       TFFunnelBaseModel,
       TFFunnelForMaskedLM,
       TFFunnelForMultipleChoice,
       TFFunnelForPreTraining,
       TFFunnelForQuestionAnswering,
       TFFunnelForSequenceClassification,
       TFFunnelForTokenClassification,
       TFFunnelModel,
   )


class     __lowerCamelCase  :

     '''simple docstring'''


     def __init__(      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase=13    ,				__UpperCAmelCase=7    ,				__UpperCAmelCase=True    ,				__UpperCAmelCase=True    ,				__UpperCAmelCase=True    ,				__UpperCAmelCase=True    ,				__UpperCAmelCase=99    ,				__UpperCAmelCase=[1, 1, 2]    ,				__UpperCAmelCase=1    ,				__UpperCAmelCase=32    ,				__UpperCAmelCase=4    ,				__UpperCAmelCase=8    ,				__UpperCAmelCase=37    ,				__UpperCAmelCase="gelu_new"    ,				__UpperCAmelCase=0.1    ,				__UpperCAmelCase=0.1    ,				__UpperCAmelCase=0.0    ,				__UpperCAmelCase=512    ,				__UpperCAmelCase=3    ,				__UpperCAmelCase=0.02    ,				__UpperCAmelCase=3    ,				__UpperCAmelCase=4    ,				__UpperCAmelCase=None    ,				__UpperCAmelCase=False    ,				)       ->							Dict:
          _a    	=				parent
          _a    	=				batch_size
          _a    	=				seq_length
          _a    	=				is_training
          _a    	=				use_input_mask
          _a    	=				use_token_type_ids
          _a    	=				use_labels
          _a    	=				vocab_size
          _a    	=				block_sizes
          _a    	=				num_decoder_layers
          _a    	=				d_model
          _a    	=				n_head
          _a    	=				d_head
          _a    	=				d_inner
          _a    	=				hidden_act
          _a    	=				hidden_dropout
          _a    	=				attention_dropout
          _a    	=				activation_dropout
          _a    	=				max_position_embeddings
          _a    	=				type_vocab_size
          _a    	=				2
          _a    	=				num_labels
          _a    	=				num_choices
          _a    	=				scope
          _a    	=				initializer_std

          # Used in the tests to check the size of the first attention layer
          _a    	=				n_head
          # Used in the tests to check the size of the first hidden state
          _a    	=				self.d_model
          # Used in the tests to check the number of output hidden states/attentions
          _a    	=				sum(self.block_sizes							) + (0 if base else self.num_decoder_layers)
          # FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
          # the last hidden state of the first block (which is the first hidden state of the decoder).
          if not base:
               _a    	=				self.num_hidden_layers + 2


     def 							_UpperCAmelCase      (      self							)       ->							str:
          _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    	=				FunnelConfig(
              vocab_size=self.vocab_size    ,				block_sizes=self.block_sizes    ,				num_decoder_layers=self.num_decoder_layers    ,				d_model=self.d_model    ,				n_head=self.n_head    ,				d_head=self.d_head    ,				d_inner=self.d_inner    ,				hidden_act=self.hidden_act    ,				hidden_dropout=self.hidden_dropout    ,				attention_dropout=self.attention_dropout    ,				activation_dropout=self.activation_dropout    ,				max_position_embeddings=self.max_position_embeddings    ,				type_vocab_size=self.type_vocab_size    ,				initializer_std=self.initializer_std    ,				)

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


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				)       ->							str:
          _a    	=				TFFunnelModel(config=__UpperCAmelCase							)
          _a    	=				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          _a    	=				model(__UpperCAmelCase							)

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

          _a    	=				model(__UpperCAmelCase							)
          self.parent.assertEqual(result.last_hidden_state.shape    ,				(self.batch_size, self.seq_length, self.d_model)							)

          _a    	=				False
          _a    	=				TFFunnelModel(config=__UpperCAmelCase							)
          _a    	=				model(__UpperCAmelCase							)
          self.parent.assertEqual(result.last_hidden_state.shape    ,				(self.batch_size, self.seq_length, self.d_model)							)

          _a    	=				False
          _a    	=				TFFunnelModel(config=__UpperCAmelCase							)
          _a    	=				model(__UpperCAmelCase							)
          self.parent.assertEqual(result.last_hidden_state.shape    ,				(self.batch_size, self.seq_length, self.d_model)							)


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				)       ->							int:
          _a    	=				TFFunnelBaseModel(config=__UpperCAmelCase							)
          _a    	=				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          _a    	=				model(__UpperCAmelCase							)

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

          _a    	=				model(__UpperCAmelCase							)
          self.parent.assertEqual(result.last_hidden_state.shape    ,				(self.batch_size, 2, self.d_model)							)

          _a    	=				False
          _a    	=				TFFunnelBaseModel(config=__UpperCAmelCase							)
          _a    	=				model(__UpperCAmelCase							)
          self.parent.assertEqual(result.last_hidden_state.shape    ,				(self.batch_size, 3, self.d_model)							)

          _a    	=				False
          _a    	=				TFFunnelBaseModel(config=__UpperCAmelCase							)
          _a    	=				model(__UpperCAmelCase							)
          self.parent.assertEqual(result.last_hidden_state.shape    ,				(self.batch_size, 2, self.d_model)							)


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				)       ->							int:
          _a    	=				TFFunnelForPreTraining(config=__UpperCAmelCase							)
          _a    	=				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          _a    	=				model(__UpperCAmelCase							)
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.seq_length)							)


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				)       ->							Any:
          _a    	=				TFFunnelForMaskedLM(config=__UpperCAmelCase							)
          _a    	=				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          _a    	=				model(__UpperCAmelCase							)
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.seq_length, self.vocab_size)							)


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				)       ->							int:
          _a    	=				self.num_labels
          _a    	=				TFFunnelForSequenceClassification(config=__UpperCAmelCase							)
          _a    	=				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          _a    	=				model(__UpperCAmelCase							)
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.num_labels)							)


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				)       ->							Dict:
          _a    	=				self.num_choices
          _a    	=				TFFunnelForMultipleChoice(config=__UpperCAmelCase							)
          _a    	=				tf.tile(tf.expand_dims(__UpperCAmelCase    ,				1							)    ,				(1, self.num_choices, 1)							)
          _a    	=				tf.tile(tf.expand_dims(__UpperCAmelCase    ,				1							)    ,				(1, self.num_choices, 1)							)
          _a    	=				tf.tile(tf.expand_dims(__UpperCAmelCase    ,				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(__UpperCAmelCase							)
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.num_choices)							)


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				)       ->							Tuple:
          _a    	=				self.num_labels
          _a    	=				TFFunnelForTokenClassification(config=__UpperCAmelCase							)
          _a    	=				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          _a    	=				model(__UpperCAmelCase							)
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.seq_length, self.num_labels)							)


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				)       ->							List[str]:
          _a    	=				TFFunnelForQuestionAnswering(config=__UpperCAmelCase							)
          _a    	=				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          _a    	=				model(__UpperCAmelCase							)
          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							)       ->							Optional[Any]:
          _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     __lowerCamelCase  (					a__  ,					a__  ,					unittest.TestCase				):

     '''simple docstring'''
     A_       :	Dict    =		(
         (
             TFFunnelModel,
             TFFunnelForMaskedLM,
             TFFunnelForPreTraining,
             TFFunnelForQuestionAnswering,
             TFFunnelForTokenClassification,
         )
         if is_tf_available()
         else ()
     )
     A_       :	Tuple    =		(
         {
             'feature-extraction': (TFFunnelBaseModel, TFFunnelModel),
             'fill-mask': TFFunnelForMaskedLM,
             'question-answering': TFFunnelForQuestionAnswering,
             'text-classification': TFFunnelForSequenceClassification,
             'token-classification': TFFunnelForTokenClassification,
             'zero-shot': TFFunnelForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     A_       :	Tuple    =		False
     A_       :	Optional[Any]    =		False


     def 							_UpperCAmelCase      (      self							)       ->							int:
          _a    	=				TFFunnelModelTester(self							)
          _a    	=				ConfigTester(self    ,				config_class=__UpperCAmelCase							)


     def 							_UpperCAmelCase      (      self							)       ->							Optional[Any]:
          self.config_tester.run_common_tests()


     def 							_UpperCAmelCase      (      self							)       ->							List[str]:
          _a    	=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__UpperCAmelCase							)


     def 							_UpperCAmelCase      (      self							)       ->							Optional[Any]:
          _a    	=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_pretraining(*__UpperCAmelCase							)


     def 							_UpperCAmelCase      (      self							)       ->							List[str]:
          _a    	=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase							)


     def 							_UpperCAmelCase      (      self							)       ->							str:
          _a    	=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase							)





     def 							_UpperCAmelCase      (      self							)       ->							Any:
          _a    	=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*__UpperCAmelCase							)







@require_tf
class     __lowerCamelCase  (					a__  ,					unittest.TestCase				):

     '''simple docstring'''
     A_       :	Optional[Any]    =		(
         (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
     )
     A_       :	Union[str, Any]    =		False
     A_       :	int    =		False


     def 							_UpperCAmelCase      (      self							)       ->							str:
          _a    	=				TFFunnelModelTester(self    ,				base=__UpperCAmelCase							)
          _a    	=				ConfigTester(self    ,				config_class=__UpperCAmelCase							)


     def 							_UpperCAmelCase      (      self							)       ->							Tuple:
          self.config_tester.run_common_tests()


     def 							_UpperCAmelCase      (      self							)       ->							List[str]:
          _a    	=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_base_model(*__UpperCAmelCase							)


     def 							_UpperCAmelCase      (      self							)       ->							Optional[Any]:
          _a    	=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*__UpperCAmelCase							)





     def 							_UpperCAmelCase      (      self							)       ->							List[str]:
          _a    	=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*__UpperCAmelCase							)



"""simple docstring"""






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding


class     __lowerCamelCase  (					a__				):

     '''simple docstring'''
     A_       :	Optional[int]    =		['image_processor', 'tokenizer']
     A_       :	Union[str, Any]    =		'ChineseCLIPImageProcessor'
     A_       :	Union[str, Any]    =		('BertTokenizer', 'BertTokenizerFast')


     def __init__(      self    ,				__UpperCAmelCase=None    ,				__UpperCAmelCase=None    ,				**__UpperCAmelCase							)       ->							List[str]:
          _a    	=				None
          if "feature_extractor" in kwargs:
               warnings.warn(
                   '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
                   ''' instead.'''    ,				__UpperCAmelCase    ,				)
               _a    	=				kwargs.pop('''feature_extractor'''							)

          _a    	=				image_processor if image_processor is not None else feature_extractor
          if image_processor is None:
               raise ValueError('''You need to specify an `image_processor`.'''							)
          if tokenizer is None:
               raise ValueError('''You need to specify a `tokenizer`.'''							)

          super().__init__(__UpperCAmelCase    ,				__UpperCAmelCase							)
          _a    	=				self.image_processor


     def __call__(      self    ,				__UpperCAmelCase=None    ,				__UpperCAmelCase=None    ,				__UpperCAmelCase=None    ,				**__UpperCAmelCase							)       ->							List[str]:

          if text is None and images is None:
               raise ValueError('''You have to specify either text or images. Both cannot be none.'''							)

          if text is not None:
               _a    	=				self.tokenizer(__UpperCAmelCase    ,				return_tensors=__UpperCAmelCase    ,				**__UpperCAmelCase							)

          if images is not None:
               _a    	=				self.image_processor(__UpperCAmelCase    ,				return_tensors=__UpperCAmelCase    ,				**__UpperCAmelCase							)

          if text is not None and images is not None:
               _a    	=				image_features.pixel_values
               return encoding
          elif text is not None:
               return encoding
          else:
               return BatchEncoding(data=dict(**__UpperCAmelCase							)    ,				tensor_type=__UpperCAmelCase							)


     def 							_UpperCAmelCase      (      self    ,				*__UpperCAmelCase    ,				**__UpperCAmelCase							)       ->							List[str]:
          return self.tokenizer.batch_decode(*__UpperCAmelCase    ,				**__UpperCAmelCase							)


     def 							_UpperCAmelCase      (      self    ,				*__UpperCAmelCase    ,				**__UpperCAmelCase							)       ->							Optional[int]:
          return self.tokenizer.decode(*__UpperCAmelCase    ,				**__UpperCAmelCase							)


     @property
     def 							_UpperCAmelCase      (      self							)       ->							List[str]:
          _a    	=				self.tokenizer.model_input_names
          _a    	=				self.image_processor.model_input_names
          return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names							)							)





     @property
     def 							_UpperCAmelCase      (      self							)       ->							Tuple:
          warnings.warn(
              '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''    ,				__UpperCAmelCase    ,				)
          return self.image_processor_class