




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				:	int			,      __lowercase				:	List[str]			,      __lowercase				:	Dict=13			,      __lowercase				:	Any=7			,      __lowercase				:	List[str]=True			,      __lowercase				:	Union[str, Any]=True			,      __lowercase				:	List[Any]=True			,      __lowercase				:	Any=True			,      __lowercase				:	Tuple=99			,      __lowercase				:	int=[1, 1, 2]			,      __lowercase				:	List[str]=1			,      __lowercase				:	Optional[int]=32			,      __lowercase				:	List[str]=4			,      __lowercase				:	Tuple=8			,      __lowercase				:	Dict=37			,      __lowercase				:	Tuple="gelu_new"			,      __lowercase				:	Optional[Any]=0.1			,      __lowercase				:	Union[str, Any]=0.1			,      __lowercase				:	int=0.0			,      __lowercase				:	Any=5_12			,      __lowercase				:	List[str]=3			,      __lowercase				:	Optional[int]=0.02			,      __lowercase				:	Union[str, Any]=3			,      __lowercase				:	Optional[Any]=4			,      __lowercase				:	int=None			,      __lowercase				:	List[str]=False			,      ):






							"""simple docstring"""






							snake_case_						       =	parent
							snake_case_						       =	batch_size
							snake_case_						       =	seq_length
							snake_case_						       =	is_training
							snake_case_						       =	use_input_mask
							snake_case_						       =	use_token_type_ids
							snake_case_						       =	use_labels
							snake_case_						       =	vocab_size
							snake_case_						       =	block_sizes
							snake_case_						       =	num_decoder_layers
							snake_case_						       =	d_model
							snake_case_						       =	n_head
							snake_case_						       =	d_head
							snake_case_						       =	d_inner
							snake_case_						       =	hidden_act
							snake_case_						       =	hidden_dropout
							snake_case_						       =	attention_dropout
							snake_case_						       =	activation_dropout
							snake_case_						       =	max_position_embeddings
							snake_case_						       =	type_vocab_size
							snake_case_						       =	2
							snake_case_						       =	num_labels
							snake_case_						       =	num_choices
							snake_case_						       =	scope
							snake_case_						       =	initializer_std

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






						def 			snake_case__		(		self				:	int		):






							"""simple docstring"""






							snake_case_						       =	ids_tensor([self.batch_size, self.seq_length]			,      self.vocab_size		)

							snake_case_						       =	None
							if self.use_input_mask:
								snake_case_						       =	random_attention_mask([self.batch_size, self.seq_length]		)

							snake_case_						       =	None
							if self.use_token_type_ids:
								snake_case_						       =	ids_tensor([self.batch_size, self.seq_length]			,      self.type_vocab_size		)

							snake_case_						       =	None
							snake_case_						       =	None
							snake_case_						       =	None
							if self.use_labels:
								snake_case_						       =	ids_tensor([self.batch_size]			,      self.type_sequence_label_size		)
								snake_case_						       =	ids_tensor([self.batch_size, self.seq_length]			,      self.num_labels		)
								snake_case_						       =	ids_tensor([self.batch_size]			,      self.num_choices		)

							snake_case_						       =	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				:	List[Any]			,      __lowercase				:	Optional[Any]			,      __lowercase				:	Tuple			,      __lowercase				:	List[Any]			,      __lowercase				:	Optional[int]			,      __lowercase				:	str			,      __lowercase				:	Tuple			,      ):






							"""simple docstring"""






							snake_case_						       =	TFFunnelModel(config=UpperCamelCase__		)
							snake_case_						       =	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							snake_case_						       =	model(UpperCamelCase__		)

							snake_case_						       =	[input_ids, input_mask]
							snake_case_						       =	model(UpperCamelCase__		)

							snake_case_						       =	model(UpperCamelCase__		)
							self.parent.assertEqual(result.last_hidden_state.shape			,      (self.batch_size, self.seq_length, self.d_model)		)

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

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






						def 			snake_case__		(		self				:	str			,      __lowercase				:	str			,      __lowercase				:	Union[str, Any]			,      __lowercase				:	List[str]			,      __lowercase				:	Any			,      __lowercase				:	Dict			,      __lowercase				:	Union[str, Any]			,      __lowercase				:	List[Any]			,      ):






							"""simple docstring"""






							snake_case_						       =	TFFunnelBaseModel(config=UpperCamelCase__		)
							snake_case_						       =	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							snake_case_						       =	model(UpperCamelCase__		)

							snake_case_						       =	[input_ids, input_mask]
							snake_case_						       =	model(UpperCamelCase__		)

							snake_case_						       =	model(UpperCamelCase__		)
							self.parent.assertEqual(result.last_hidden_state.shape			,      (self.batch_size, 2, self.d_model)		)

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

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






						def 			snake_case__		(		self				:	List[str]			,      __lowercase				:	str			,      __lowercase				:	Dict			,      __lowercase				:	int			,      __lowercase				:	Tuple			,      __lowercase				:	Any			,      __lowercase				:	List[Any]			,      __lowercase				:	str			,      ):






							"""simple docstring"""






							snake_case_						       =	TFFunnelForPreTraining(config=UpperCamelCase__		)
							snake_case_						       =	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							snake_case_						       =	model(UpperCamelCase__		)
							self.parent.assertEqual(result.logits.shape			,      (self.batch_size, self.seq_length)		)






						def 			snake_case__		(		self				:	Tuple			,      __lowercase				:	int			,      __lowercase				:	Any			,      __lowercase				:	Tuple			,      __lowercase				:	List[str]			,      __lowercase				:	Tuple			,      __lowercase				:	Optional[Any]			,      __lowercase				:	Optional[int]			,      ):






							"""simple docstring"""






							snake_case_						       =	TFFunnelForMaskedLM(config=UpperCamelCase__		)
							snake_case_						       =	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							snake_case_						       =	model(UpperCamelCase__		)
							self.parent.assertEqual(result.logits.shape			,      (self.batch_size, self.seq_length, self.vocab_size)		)






						def 			snake_case__		(		self				:	Dict			,      __lowercase				:	Union[str, Any]			,      __lowercase				:	Optional[int]			,      __lowercase				:	Any			,      __lowercase				:	Dict			,      __lowercase				:	Dict			,      __lowercase				:	int			,      __lowercase				:	Dict			,      ):






							"""simple docstring"""






							snake_case_						       =	self.num_labels
							snake_case_						       =	TFFunnelForSequenceClassification(config=UpperCamelCase__		)
							snake_case_						       =	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							snake_case_						       =	model(UpperCamelCase__		)
							self.parent.assertEqual(result.logits.shape			,      (self.batch_size, self.num_labels)		)






						def 			snake_case__		(		self				:	Union[str, Any]			,      __lowercase				:	Union[str, Any]			,      __lowercase				:	int			,      __lowercase				:	Tuple			,      __lowercase				:	Optional[Any]			,      __lowercase				:	Optional[Any]			,      __lowercase				:	Tuple			,      __lowercase				:	str			,      ):






							"""simple docstring"""






							snake_case_						       =	self.num_choices
							snake_case_						       =	TFFunnelForMultipleChoice(config=UpperCamelCase__		)
							snake_case_						       =	tf.tile(tf.expand_dims(UpperCamelCase__			,      1		)			,      (1, self.num_choices, 1)		)
							snake_case_						       =	tf.tile(tf.expand_dims(UpperCamelCase__			,      1		)			,      (1, self.num_choices, 1)		)
							snake_case_						       =	tf.tile(tf.expand_dims(UpperCamelCase__			,      1		)			,      (1, self.num_choices, 1)		)
							snake_case_						       =	{
							    "input_ids": multiple_choice_inputs_ids,
							    "attention_mask": multiple_choice_input_mask,
							    "token_type_ids": multiple_choice_token_type_ids,
							}
							snake_case_						       =	model(UpperCamelCase__		)
							self.parent.assertEqual(result.logits.shape			,      (self.batch_size, self.num_choices)		)






						def 			snake_case__		(		self				:	Any			,      __lowercase				:	Any			,      __lowercase				:	Union[str, Any]			,      __lowercase				:	int			,      __lowercase				:	Tuple			,      __lowercase				:	Dict			,      __lowercase				:	Optional[int]			,      __lowercase				:	Any			,      ):






							"""simple docstring"""






							snake_case_						       =	self.num_labels
							snake_case_						       =	TFFunnelForTokenClassification(config=UpperCamelCase__		)
							snake_case_						       =	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							snake_case_						       =	model(UpperCamelCase__		)
							self.parent.assertEqual(result.logits.shape			,      (self.batch_size, self.seq_length, self.num_labels)		)






						def 			snake_case__		(		self				:	List[Any]			,      __lowercase				:	List[Any]			,      __lowercase				:	Tuple			,      __lowercase				:	Dict			,      __lowercase				:	List[str]			,      __lowercase				:	Union[str, Any]			,      __lowercase				:	Optional[Any]			,      __lowercase				:	Dict			,      ):






							"""simple docstring"""






							snake_case_						       =	TFFunnelForQuestionAnswering(config=UpperCamelCase__		)
							snake_case_						       =	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							snake_case_						       =	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 			snake_case__		(		self				:	int		):






							"""simple docstring"""






							snake_case_						       =	self.prepare_config_and_inputs()
							(
							    (
							    snake_case_      
							)       , (
							    snake_case_      
							)       , (
							    snake_case_      
							)       , (
							    snake_case_      
							)       , (
							    snake_case_      
							)       , (
							    snake_case_      
							)       , (
							    snake_case_      
							)       ,       
							)						       =	config_and_inputs
							snake_case_						       =	{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
							return config, inputs_dict







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







						'''simple docstring'''





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






						def 			snake_case__		(		self				:	Dict		):






							"""simple docstring"""






							snake_case_						       =	TFFunnelModelTester(self		)
							snake_case_						       =	ConfigTester(self			,      config_class=UpperCamelCase__		)






						def 			snake_case__		(		self				:	Dict		):






							"""simple docstring"""






							self.config_tester.run_common_tests()






						def 			snake_case__		(		self				:	str		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*UpperCamelCase__		)






						def 			snake_case__		(		self				:	int		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*UpperCamelCase__		)






						def 			snake_case__		(		self				:	Union[str, Any]		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase__		)






						def 			snake_case__		(		self				:	str		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__		)






						def 			snake_case__		(		self				:	Optional[int]		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__		)







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







						'''simple docstring'''





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






						def 			snake_case__		(		self				:	str		):






							"""simple docstring"""






							snake_case_						       =	TFFunnelModelTester(self			,      base=UpperCamelCase__		)
							snake_case_						       =	ConfigTester(self			,      config_class=UpperCamelCase__		)






						def 			snake_case__		(		self				:	Dict		):






							"""simple docstring"""






							self.config_tester.run_common_tests()






						def 			snake_case__		(		self				:	Any		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_base_model(*UpperCamelCase__		)






						def 			snake_case__		(		self				:	str		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__		)






						def 			snake_case__		(		self				:	Any		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase__		)



'''simple docstring'''







from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


__lowerCamelCase									=     logging.get_logger(__name__)

__lowerCamelCase									=     {
    '''microsoft/resnet-50''': '''https://huggingface.co/microsoft/resnet-50/blob/main/config.json''',
}


class        A__   (   _snake_case    ,    _snake_case      ):
    lowercase						       =		"resnet"
    lowercase						       =		["basic", "bottleneck"]



    def __init__(       self		,					UpperCamelCase__=3		,					UpperCamelCase__=64		,					UpperCamelCase__=[256, 512, 1024, 2048]		,					UpperCamelCase__=[3, 4, 6, 3]		,					UpperCamelCase__="bottleneck"		,					UpperCamelCase__="relu"		,					UpperCamelCase__=False		,					UpperCamelCase__=None		,					UpperCamelCase__=None		,					**UpperCamelCase__		,					)    ->   List[str]:





           '''simple docstring'''
           super().__init__(**UpperCamelCase__							)
           if layer_type not in self.layer_types:
                  raise ValueError(f'''layer_type={layer_type} is not one of {",".join(self.layer_types							)}'''							)
           A_						       =   num_channels
           A_						       =   embedding_size
           A_						       =   hidden_sizes
           A_						       =   depths
           A_						       =   layer_type
           A_						       =   hidden_act
           A_						       =   downsample_in_first_stage
           A_						       =   ["""stem"""] + [f'''stage{idx}''' for idx in range(1		,					len(UpperCamelCase__							) + 1							)]
           A_       ,      A_						       =   get_aligned_output_features_output_indices(
               out_features=UpperCamelCase__		,					out_indices=UpperCamelCase__		,					stage_names=self.stage_names							)


class        A__   (   _snake_case      ):
    lowercase						       =		version.parse("1.11"      )



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





           '''simple docstring'''
           return OrderedDict(
               [
                   ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
               ]							)







    @property
    def        snake_case_      (       self							)    ->   float:





           '''simple docstring'''
           return 1e-3
