
'''simple docstring'''
from collections import OrderedDict
from typing import Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...feature_extraction_utils import FeatureExtractionMixin
from ...onnx import OnnxConfig
from ...onnx.utils import compute_effective_axis_dimension
from ...tokenization_utils_base import PreTrainedTokenizerBase
from ...utils import TensorType, logging


lowerCAmelCase					:    Any           = logging.get_logger(__name__)

lowerCAmelCase					:    Optional[int]           = {
    'deepmind/language-perceiver': 'https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json',
    # See all Perceiver models at https://huggingface.co/models?filter=perceiver
}


class 			SCREAMING_SNAKE_CASE__      (     snake_case_):
				lowerCAmelCase_     			=							"""perceiver"""







				def __init__(       self						,				A_=256						,				A_=1280						,				A_=768						,				A_=1						,				A_=26						,				A_=8						,				A_=8						,				A_=None						,				A_=None						,				A_="kv"						,				A_=1						,				A_=1						,				A_="gelu"						,				A_=0.1						,				A_=0.02						,				A_=1e-12						,				A_=True						,				A_=262						,				A_=2048						,				A_=56						,				A_=[368, 496]						,				A_=16						,				A_=1920						,				A_=16						,				A_=[1, 16, 224, 224]						,				**A_						,				)->		str:







											'''simple docstring'''






											super().__init__(**A_     )

											UpperCamelCase   			=     num_latents
											UpperCamelCase   			=     d_latents
											UpperCamelCase   			=     d_model
											UpperCamelCase   			=     num_blocks
											UpperCamelCase   			=     num_self_attends_per_block
											UpperCamelCase   			=     num_self_attention_heads
											UpperCamelCase   			=     num_cross_attention_heads
											UpperCamelCase   			=     qk_channels
											UpperCamelCase   			=     v_channels
											UpperCamelCase   			=     cross_attention_shape_for_attention
											UpperCamelCase   			=     self_attention_widening_factor
											UpperCamelCase   			=     cross_attention_widening_factor
											UpperCamelCase   			=     hidden_act
											UpperCamelCase   			=     attention_probs_dropout_prob
											UpperCamelCase   			=     initializer_range
											UpperCamelCase   			=     layer_norm_eps
											UpperCamelCase   			=     use_query_residual
											# masked language modeling attributes
											UpperCamelCase   			=     vocab_size
											UpperCamelCase   			=     max_position_embeddings
											# image classification attributes
											UpperCamelCase   			=     image_size
											# flow attributes
											UpperCamelCase   			=     train_size
											# multimodal autoencoding attributes
											UpperCamelCase   			=     num_frames
											UpperCamelCase   			=     audio_samples_per_frame
											UpperCamelCase   			=     samples_per_patch
											UpperCamelCase   			=     output_shape


class 			SCREAMING_SNAKE_CASE__      (     snake_case_):







				@property
				def 						UpperCAmelCase_    (       self     )->		Mapping[str, Mapping[int, str]]:







											'''simple docstring'''






											if self.task == "multiple-choice":
																		UpperCamelCase   			=     {0: 'batch', 1: 'choice', 2: 'sequence'}
											else:
																		UpperCamelCase   			=     {0: 'batch', 1: 'sequence'}
											return OrderedDict(
											    [
											        ('inputs', dynamic_axis),
											        ('attention_mask', dynamic_axis),
											    ]     )







				@property
				def 						UpperCAmelCase_    (       self     )->		float:







											'''simple docstring'''






											return 1e-4







				def 						UpperCAmelCase_    (       self						,				A_						,				A_ = -1						,				A_ = -1						,				A_ = -1						,				A_ = False						,				A_ = None						,				A_ = 3						,				A_ = 40						,				A_ = 40						,				)->		Mapping[str, Any]:







											'''simple docstring'''






											if isinstance(A_						,				A_     ):
																		# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
																		UpperCamelCase   			=     compute_effective_axis_dimension(
																		    A_						,				fixed_dimension=OnnxConfig.default_fixed_batch						,				num_token_to_add=0     )
																		# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
																		UpperCamelCase   			=     preprocessor.num_special_tokens_to_add(A_     )
																		UpperCamelCase   			=     compute_effective_axis_dimension(
																		    A_						,				fixed_dimension=OnnxConfig.default_fixed_sequence						,				num_token_to_add=A_     )
																		# Generate dummy inputs according to compute batch and sequence
																		UpperCamelCase   			=     [' '.join(['a']     ) * seq_length] * batch_size
																		UpperCamelCase   			=     dict(preprocessor(A_						,				return_tensors=A_     )     )
																		UpperCamelCase   			=     inputs.pop('input_ids'     )
																		return inputs
											elif isinstance(A_						,				A_     ) and preprocessor.model_input_names[0] == "pixel_values":
																		# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
																		UpperCamelCase   			=     compute_effective_axis_dimension(A_						,				fixed_dimension=OnnxConfig.default_fixed_batch     )
																		UpperCamelCase   			=     self._generate_dummy_images(A_						,				A_						,				A_						,				A_     )
																		UpperCamelCase   			=     dict(preprocessor(images=A_						,				return_tensors=A_     )     )
																		UpperCamelCase   			=     inputs.pop('pixel_values'     )
																		return inputs
											else:
																		raise ValueError(
																		    'Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor.'     )










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



					'''simple docstring'''





					def __init__(	self     :						str      ,       _lowercase     :						List[str]      ,       _lowercase     :						Dict=13      ,       _lowercase     :						List[str]=7      ,       _lowercase     :						Union[str, Any]=True      ,       _lowercase     :						int=True      ,       _lowercase     :						int=True      ,       _lowercase     :						Optional[Any]=True      ,       _lowercase     :						List[Any]=99      ,       _lowercase     :						Optional[Any]=[1, 1, 2]      ,       _lowercase     :						Optional[Any]=1      ,       _lowercase     :						List[str]=32      ,       _lowercase     :						Dict=4      ,       _lowercase     :						List[str]=8      ,       _lowercase     :						List[str]=37      ,       _lowercase     :						int="gelu_new"      ,       _lowercase     :						Optional[int]=0.1      ,       _lowercase     :						List[str]=0.1      ,       _lowercase     :						Union[str, Any]=0.0      ,       _lowercase     :						str=512      ,       _lowercase     :						Optional[Any]=3      ,       _lowercase     :						str=0.02      ,       _lowercase     :						Union[str, Any]=3      ,       _lowercase     :						Optional[Any]=4      ,       _lowercase     :						Tuple=None      ,       _lowercase     :						List[Any]=False      ,       )    ->							List[str]:
								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     __snake_case						(	self     :						str)    ->							List[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     __snake_case						(	self     :						Tuple      ,       _lowercase     :						Tuple      ,       _lowercase     :						str      ,       _lowercase     :						int      ,       _lowercase     :						Dict      ,       _lowercase     :						Optional[Any]      ,       _lowercase     :						str      ,       _lowercase     :						str      ,       )    ->							Dict:
								A_									= TFFunnelModel(config=_lowercase)
								A_									= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
								A_									= model(_lowercase)

								A_									= [input_ids, input_mask]
								A_									= model(_lowercase)

								A_									= model(_lowercase)
								self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.d_model))

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

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



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

								A_									= [input_ids, input_mask]
								A_									= model(_lowercase)

								A_									= model(_lowercase)
								self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, 2, self.d_model))

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

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



					def     __snake_case						(	self     :						int      ,       _lowercase     :						List[str]      ,       _lowercase     :						int      ,       _lowercase     :						List[str]      ,       _lowercase     :						str      ,       _lowercase     :						Any      ,       _lowercase     :						Optional[Any]      ,       _lowercase     :						Optional[Any]      ,       )    ->							List[Any]:
								A_									= TFFunnelForPreTraining(config=_lowercase)
								A_									= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
								A_									= model(_lowercase)
								self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length))



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



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



					def     __snake_case						(	self     :						Optional[int]      ,       _lowercase     :						int      ,       _lowercase     :						Optional[Any]      ,       _lowercase     :						Tuple      ,       _lowercase     :						Union[str, Any]      ,       _lowercase     :						Union[str, Any]      ,       _lowercase     :						Union[str, Any]      ,       _lowercase     :						str      ,       )    ->							List[str]:
								A_									= self.num_choices
								A_									= TFFunnelForMultipleChoice(config=_lowercase)
								A_									= tf.tile(tf.expand_dims(_lowercase      ,       1)      ,       (1, self.num_choices, 1))
								A_									= tf.tile(tf.expand_dims(_lowercase      ,       1)      ,       (1, self.num_choices, 1))
								A_									= tf.tile(tf.expand_dims(_lowercase      ,       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(_lowercase)
								self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.num_choices))



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



					def     __snake_case						(	self     :						Optional[int]      ,       _lowercase     :						str      ,       _lowercase     :						Union[str, Any]      ,       _lowercase     :						List[str]      ,       _lowercase     :						List[Any]      ,       _lowercase     :						List[str]      ,       _lowercase     :						List[Any]      ,       _lowercase     :						Union[str, Any]      ,       )    ->							Optional[int]:
								A_									= TFFunnelForQuestionAnswering(config=_lowercase)
								A_									= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
								A_									= model(_lowercase)
								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     :						str)    ->							Union[str, 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   __UpperCAmelCase				(     lowerCAmelCase			,lowerCAmelCase			,unittest.TestCase				):



					'''simple docstring'''





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



					def     __snake_case						(	self     :						Optional[int])    ->							Optional[Any]:
								A_									= TFFunnelModelTester(self)
								A_									= ConfigTester(self      ,       config_class=_lowercase)



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



					def     __snake_case						(	self     :						Union[str, Any])    ->							Union[str, Any]:
								A_									= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_lowercase)



					def     __snake_case						(	self     :						List[Any])    ->							str:
								A_									= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_pretraining(*_lowercase)



					def     __snake_case						(	self     :						Any)    ->							Optional[int]:
								A_									= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*_lowercase)



					def     __snake_case						(	self     :						List[str])    ->							Tuple:
								A_									= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*_lowercase)



					def     __snake_case						(	self     :						int)    ->							Dict:
								A_									= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*_lowercase)





@require_tf
class   __UpperCAmelCase				(     lowerCAmelCase			,unittest.TestCase				):



					'''simple docstring'''





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



					def     __snake_case						(	self     :						str)    ->							Dict:
								A_									= TFFunnelModelTester(self      ,       base=_lowercase)
								A_									= ConfigTester(self      ,       config_class=_lowercase)



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



					def     __snake_case						(	self     :						List[str])    ->							List[str]:
								A_									= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_base_model(*_lowercase)



					def     __snake_case						(	self     :						Dict)    ->							Dict:
								A_									= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*_lowercase)



					def     __snake_case						(	self     :						Union[str, Any])    ->							int:
								A_									= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_multiple_choice(*_lowercase)




