



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






							"""simple docstring"""

							def __init__(      self				:     Union[str, Any]							,	a				:     Optional[int]							,	a				:     str=13							,	a				:     Tuple=7							,	a				:     str=True							,	a				:     int=True							,	a				:     Any=True							,	a				:     List[str]=True							,	a				:     Dict=99							,	a				:     Dict=[1, 1, 2]							,	a				:     Tuple=1							,	a				:     Tuple=32							,	a				:     str=4							,	a				:     Optional[int]=8							,	a				:     Optional[Any]=37							,	a				:     str="gelu_new"							,	a				:     Dict=0.1							,	a				:     str=0.1							,	a				:     Optional[int]=0.0							,	a				:     Any=5_12							,	a				:     Optional[Any]=3							,	a				:     List[Any]=0.02							,	a				:     Optional[int]=3							,	a				:     str=4							,	a				:     Any=None							,	a				:     Tuple=False							,	)	->Tuple:
													SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					parent
													SCREAMING_SNAKE_CASE__     :				Tuple   =					batch_size
													SCREAMING_SNAKE_CASE__     :				int   =					seq_length
													SCREAMING_SNAKE_CASE__     :				List[Any]   =					is_training
													SCREAMING_SNAKE_CASE__     :				List[str]   =					use_input_mask
													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					use_token_type_ids
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					use_labels
													SCREAMING_SNAKE_CASE__     :				Dict   =					vocab_size
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					block_sizes
													SCREAMING_SNAKE_CASE__     :				int   =					num_decoder_layers
													SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					d_model
													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					n_head
													SCREAMING_SNAKE_CASE__     :				Any   =					d_head
													SCREAMING_SNAKE_CASE__     :				int   =					d_inner
													SCREAMING_SNAKE_CASE__     :				List[str]   =					hidden_act
													SCREAMING_SNAKE_CASE__     :				int   =					hidden_dropout
													SCREAMING_SNAKE_CASE__     :				Dict   =					attention_dropout
													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					activation_dropout
													SCREAMING_SNAKE_CASE__     :				int   =					max_position_embeddings
													SCREAMING_SNAKE_CASE__     :				List[str]   =					type_vocab_size
													SCREAMING_SNAKE_CASE__     :				List[Any]   =					2
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					num_labels
													SCREAMING_SNAKE_CASE__     :				Any   =					num_choices
													SCREAMING_SNAKE_CASE__     :				Dict   =					scope
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					initializer_std

													# Used in the tests to check the size of the first attention layer
													SCREAMING_SNAKE_CASE__     :				Any   =					n_head
													# Used in the tests to check the size of the first hidden state
													SCREAMING_SNAKE_CASE__     :				Tuple   =					self.d_model
													# Used in the tests to check the number of output hidden states/attentions
													SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					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:
																			SCREAMING_SNAKE_CASE__     :				Optional[int]   =					self.num_hidden_layers + 2

							def      A_  (      self				:     Any    )	->Tuple:
													SCREAMING_SNAKE_CASE__     :				Tuple   =					ids_tensor([self.batch_size, self.seq_length]							,	self.vocab_size    )

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

													SCREAMING_SNAKE_CASE__     :				Tuple   =					None
													if self.use_token_type_ids:
																			SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					ids_tensor([self.batch_size, self.seq_length]							,	self.type_vocab_size    )

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

													SCREAMING_SNAKE_CASE__     :				int   =					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      A_  (      self				:     Any							,	a				:     Union[str, Any]							,	a				:     Optional[int]							,	a				:     Tuple							,	a				:     Any							,	a				:     Tuple							,	a				:     Tuple							,	a				:     str							,	)	->List[Any]:
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					TFFunnelModel(config=a    )
													SCREAMING_SNAKE_CASE__     :				Dict   =					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					model(a    )

													SCREAMING_SNAKE_CASE__     :				int   =					[input_ids, input_mask]
													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					model(a    )

													SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					model(a    )
													self.parent.assertEqual(result.last_hidden_state.shape							,	(self.batch_size, self.seq_length, self.d_model)    )

													SCREAMING_SNAKE_CASE__     :				str   =					False
													SCREAMING_SNAKE_CASE__     :				Tuple   =					TFFunnelModel(config=a    )
													SCREAMING_SNAKE_CASE__     :				Dict   =					model(a    )
													self.parent.assertEqual(result.last_hidden_state.shape							,	(self.batch_size, self.seq_length, self.d_model)    )

													SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					False
													SCREAMING_SNAKE_CASE__     :				Dict   =					TFFunnelModel(config=a    )
													SCREAMING_SNAKE_CASE__     :				int   =					model(a    )
													self.parent.assertEqual(result.last_hidden_state.shape							,	(self.batch_size, self.seq_length, self.d_model)    )

							def      A_  (      self				:     Any							,	a				:     str							,	a				:     Any							,	a				:     Dict							,	a				:     str							,	a				:     Union[str, Any]							,	a				:     List[str]							,	a				:     List[Any]							,	)	->str:
													SCREAMING_SNAKE_CASE__     :				str   =					TFFunnelBaseModel(config=a    )
													SCREAMING_SNAKE_CASE__     :				Tuple   =					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
													SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					model(a    )

													SCREAMING_SNAKE_CASE__     :				Tuple   =					[input_ids, input_mask]
													SCREAMING_SNAKE_CASE__     :				Tuple   =					model(a    )

													SCREAMING_SNAKE_CASE__     :				List[Any]   =					model(a    )
													self.parent.assertEqual(result.last_hidden_state.shape							,	(self.batch_size, 2, self.d_model)    )

													SCREAMING_SNAKE_CASE__     :				int   =					False
													SCREAMING_SNAKE_CASE__     :				List[str]   =					TFFunnelBaseModel(config=a    )
													SCREAMING_SNAKE_CASE__     :				Dict   =					model(a    )
													self.parent.assertEqual(result.last_hidden_state.shape							,	(self.batch_size, 3, self.d_model)    )

													SCREAMING_SNAKE_CASE__     :				str   =					False
													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					TFFunnelBaseModel(config=a    )
													SCREAMING_SNAKE_CASE__     :				List[str]   =					model(a    )
													self.parent.assertEqual(result.last_hidden_state.shape							,	(self.batch_size, 2, self.d_model)    )

							def      A_  (      self				:     Any							,	a				:     str							,	a				:     Union[str, Any]							,	a				:     Union[str, Any]							,	a				:     int							,	a				:     Any							,	a				:     Optional[Any]							,	a				:     Tuple							,	)	->Optional[Any]:
													SCREAMING_SNAKE_CASE__     :				int   =					TFFunnelForPreTraining(config=a    )
													SCREAMING_SNAKE_CASE__     :				Tuple   =					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
													SCREAMING_SNAKE_CASE__     :				Tuple   =					model(a    )
													self.parent.assertEqual(result.logits.shape							,	(self.batch_size, self.seq_length)    )

							def      A_  (      self				:     int							,	a				:     int							,	a				:     Optional[int]							,	a				:     Union[str, Any]							,	a				:     int							,	a				:     Optional[int]							,	a				:     Optional[Any]							,	a				:     Tuple							,	)	->List[str]:
													SCREAMING_SNAKE_CASE__     :				str   =					TFFunnelForMaskedLM(config=a    )
													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
													SCREAMING_SNAKE_CASE__     :				List[str]   =					model(a    )
													self.parent.assertEqual(result.logits.shape							,	(self.batch_size, self.seq_length, self.vocab_size)    )

							def      A_  (      self				:     Union[str, Any]							,	a				:     Dict							,	a				:     List[str]							,	a				:     int							,	a				:     Optional[Any]							,	a				:     Tuple							,	a				:     Any							,	a				:     Dict							,	)	->int:
													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					self.num_labels
													SCREAMING_SNAKE_CASE__     :				Any   =					TFFunnelForSequenceClassification(config=a    )
													SCREAMING_SNAKE_CASE__     :				str   =					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
													SCREAMING_SNAKE_CASE__     :				int   =					model(a    )
													self.parent.assertEqual(result.logits.shape							,	(self.batch_size, self.num_labels)    )

							def      A_  (      self				:     Optional[Any]							,	a				:     Optional[Any]							,	a				:     str							,	a				:     int							,	a				:     int							,	a				:     Any							,	a				:     Optional[Any]							,	a				:     Union[str, Any]							,	)	->Optional[int]:
													SCREAMING_SNAKE_CASE__     :				Any   =					self.num_choices
													SCREAMING_SNAKE_CASE__     :				int   =					TFFunnelForMultipleChoice(config=a    )
													SCREAMING_SNAKE_CASE__     :				List[Any]   =					tf.tile(tf.expand_dims(a							,	1    )							,	(1, self.num_choices, 1)    )
													SCREAMING_SNAKE_CASE__     :				List[str]   =					tf.tile(tf.expand_dims(a							,	1    )							,	(1, self.num_choices, 1)    )
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					tf.tile(tf.expand_dims(a							,	1    )							,	(1, self.num_choices, 1)    )
													SCREAMING_SNAKE_CASE__     :				str   =					{
													    "input_ids": multiple_choice_inputs_ids,
													    "attention_mask": multiple_choice_input_mask,
													    "token_type_ids": multiple_choice_token_type_ids,
													}
													SCREAMING_SNAKE_CASE__     :				Any   =					model(a    )
													self.parent.assertEqual(result.logits.shape							,	(self.batch_size, self.num_choices)    )

							def      A_  (      self				:     Optional[Any]							,	a				:     Any							,	a				:     Dict							,	a				:     Union[str, Any]							,	a				:     List[str]							,	a				:     Tuple							,	a				:     Optional[int]							,	a				:     Dict							,	)	->Union[str, Any]:
													SCREAMING_SNAKE_CASE__     :				List[Any]   =					self.num_labels
													SCREAMING_SNAKE_CASE__     :				Tuple   =					TFFunnelForTokenClassification(config=a    )
													SCREAMING_SNAKE_CASE__     :				int   =					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
													SCREAMING_SNAKE_CASE__     :				List[Any]   =					model(a    )
													self.parent.assertEqual(result.logits.shape							,	(self.batch_size, self.seq_length, self.num_labels)    )

							def      A_  (      self				:     Union[str, Any]							,	a				:     str							,	a				:     Optional[Any]							,	a				:     Any							,	a				:     Union[str, Any]							,	a				:     int							,	a				:     str							,	a				:     Tuple							,	)	->List[Any]:
													SCREAMING_SNAKE_CASE__     :				List[Any]   =					TFFunnelForQuestionAnswering(config=a    )
													SCREAMING_SNAKE_CASE__     :				int   =					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
													SCREAMING_SNAKE_CASE__     :				int   =					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				:     Optional[int]    )	->int:
													SCREAMING_SNAKE_CASE__     :				Dict   =					self.prepare_config_and_inputs()
													(
													    SCREAMING_SNAKE_CASE__							
													)     :				Tuple   =					config_and_inputs
													SCREAMING_SNAKE_CASE__     :				str   =					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
													return config, inputs_dict







@require_tf
class    _a     (				lowercase__					,							lowercase__					,							unittest.TestCase							):






							"""simple docstring"""

							snake_case_							 =						(
							    (
							        TFFunnelModel,
							        TFFunnelForMaskedLM,
							        TFFunnelForPreTraining,
							        TFFunnelForQuestionAnswering,
							        TFFunnelForTokenClassification,
							    )
							    if is_tf_available()
							    else ()
							)
							snake_case_							 =						(
							    {
							        "feature-extraction": (TFFunnelBaseModel, TFFunnelModel),
							        "fill-mask": TFFunnelForMaskedLM,
							        "question-answering": TFFunnelForQuestionAnswering,
							        "text-classification": TFFunnelForSequenceClassification,
							        "token-classification": TFFunnelForTokenClassification,
							        "zero-shot": TFFunnelForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							snake_case_							 =						False
							snake_case_							 =						False

							def      A_  (      self				:     Any    )	->List[Any]:
													SCREAMING_SNAKE_CASE__     :				str   =					TFFunnelModelTester(self    )
													SCREAMING_SNAKE_CASE__     :				Any   =					ConfigTester(self							,	config_class=a    )

							def      A_  (      self				:     Any    )	->str:
													self.config_tester.run_common_tests()

							def      A_  (      self				:     int    )	->int:
													SCREAMING_SNAKE_CASE__     :				Any   =					self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*a    )

							def      A_  (      self				:     Union[str, Any]    )	->Any:
													SCREAMING_SNAKE_CASE__     :				str   =					self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_pretraining(*a    )

							def      A_  (      self				:     Optional[int]    )	->List[Any]:
													SCREAMING_SNAKE_CASE__     :				Tuple   =					self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_masked_lm(*a    )

							def      A_  (      self				:     Optional[int]    )	->Any:
													SCREAMING_SNAKE_CASE__     :				str   =					self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_token_classification(*a    )






							def      A_  (      self				:     Tuple    )	->Any:
													SCREAMING_SNAKE_CASE__     :				Any   =					self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*a    )







@require_tf
class    _a     (				lowercase__					,							unittest.TestCase							):






							"""simple docstring"""

							snake_case_							 =						(
							    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
							)
							snake_case_							 =						False
							snake_case_							 =						False

							def      A_  (      self				:     List[str]    )	->Any:
													SCREAMING_SNAKE_CASE__     :				List[Any]   =					TFFunnelModelTester(self							,	base=a    )
													SCREAMING_SNAKE_CASE__     :				List[str]   =					ConfigTester(self							,	config_class=a    )

							def      A_  (      self				:     Optional[Any]    )	->Union[str, Any]:
													self.config_tester.run_common_tests()

							def      A_  (      self				:     Any    )	->Optional[Any]:
													SCREAMING_SNAKE_CASE__     :				Dict   =					self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_base_model(*a    )

							def      A_  (      self				:     Dict    )	->int:
													SCREAMING_SNAKE_CASE__     :				Tuple   =					self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_sequence_classification(*a    )






							def      A_  (      self				:     Union[str, Any]    )	->str:
													SCREAMING_SNAKE_CASE__     :				Tuple   =					self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_multiple_choice(*a    )






import warnings

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



class    _a     (				lowercase__							):






							"""simple docstring"""

							snake_case_							 =						["image_processor", "tokenizer"]
							snake_case_							 =						"CLIPImageProcessor"
							snake_case_							 =						("CLIPTokenizer", "CLIPTokenizerFast")

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

													SCREAMING_SNAKE_CASE__     :				int   =					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__(a							,	a    )

							def __call__(      self				:     Tuple							,	a				:     Tuple=None							,	a				:     Union[str, Any]=None							,	a				:     List[str]=None							,	**a				:     Optional[Any]    )	->Optional[Any]:

													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:
																			SCREAMING_SNAKE_CASE__     :				str   =					self.tokenizer(a							,	return_tensors=a							,	**a    )

													if images is not None:
																			SCREAMING_SNAKE_CASE__     :				int   =					self.image_processor(a							,	return_tensors=a							,	**a    )

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

							def      A_  (      self				:     Optional[int]							,	*a				:     Any							,	**a				:     List[str]    )	->Any:
													return self.tokenizer.batch_decode(*a							,	**a    )

							def      A_  (      self				:     Any							,	*a				:     Optional[int]							,	**a				:     Dict    )	->Any:
													return self.tokenizer.decode(*a							,	**a    )

							@property
							def      A_  (      self				:     List[str]    )	->Union[str, Any]:
													SCREAMING_SNAKE_CASE__     :				Dict   =					self.tokenizer.model_input_names
													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					self.image_processor.model_input_names
													return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names    )    )

							@property
							def      A_  (      self				:     Optional[int]    )	->List[Any]:
													warnings.warn(
													    "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."							,	a							,	)
													return self.image_processor_class






							@property
							def      A_  (      self				:     Dict    )	->str:
													warnings.warn(
													    "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."							,	a							,	)
													return self.image_processor