



"""simple docstring"""

import unittest

from transformers import FunnelConfig, FunnelTokenizer, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    MODEL_FOR_PRETRAINING_MAPPING,
		    FunnelBaseModel,
		    FunnelForMaskedLM,
		    FunnelForMultipleChoice,
		    FunnelForPreTraining,
		    FunnelForQuestionAnswering,
		    FunnelForSequenceClassification,
		    FunnelForTokenClassification,
		    FunnelModel,
		)
class 						lowerCAmelCase__	:


	'''simple docstring'''







	def __init__(			self							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]=13    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]=7    ,					_SCREAMING_SNAKE_CASE							:					Any=True    ,					_SCREAMING_SNAKE_CASE							:					Dict=True    ,					_SCREAMING_SNAKE_CASE							:					Dict=True    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]=True    ,					_SCREAMING_SNAKE_CASE							:					List[Any]=99    ,					_SCREAMING_SNAKE_CASE							:					List[str]=[1, 1, 2]    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]=1    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]=32    ,					_SCREAMING_SNAKE_CASE							:					Any=4    ,					_SCREAMING_SNAKE_CASE							:					List[str]=8    ,					_SCREAMING_SNAKE_CASE							:					List[str]=37    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]="gelu_new"    ,					_SCREAMING_SNAKE_CASE							:					int=0.1    ,					_SCREAMING_SNAKE_CASE							:					Dict=0.1    ,					_SCREAMING_SNAKE_CASE							:					Dict=0.0    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]=512    ,					_SCREAMING_SNAKE_CASE							:					List[Any]=3    ,					_SCREAMING_SNAKE_CASE							:					List[str]=0.0_2    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]=3    ,					_SCREAMING_SNAKE_CASE							:					List[Any]=4    ,					_SCREAMING_SNAKE_CASE							:					List[Any]=None    ,					_SCREAMING_SNAKE_CASE							:					Tuple=False    ,					)	->  Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[str] 							=    parent
					SCREAMING_SNAKE_CASE	:       str 							=    batch_size
					SCREAMING_SNAKE_CASE	:       int 							=    seq_length
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    is_training
					SCREAMING_SNAKE_CASE	:       int 							=    use_input_mask
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    use_token_type_ids
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    use_labels
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    vocab_size
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    block_sizes
					SCREAMING_SNAKE_CASE	:       int 							=    num_decoder_layers
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    d_model
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    n_head
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    d_head
					SCREAMING_SNAKE_CASE	:       str 							=    d_inner
					SCREAMING_SNAKE_CASE	:       str 							=    hidden_act
					SCREAMING_SNAKE_CASE	:       str 							=    hidden_dropout
					SCREAMING_SNAKE_CASE	:       int 							=    attention_dropout
					SCREAMING_SNAKE_CASE	:       str 							=    activation_dropout
					SCREAMING_SNAKE_CASE	:       Tuple 							=    max_position_embeddings
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    type_vocab_size
					SCREAMING_SNAKE_CASE	:       Tuple 							=    2
					SCREAMING_SNAKE_CASE	:       List[str] 							=    num_labels
					SCREAMING_SNAKE_CASE	:       Dict 							=    num_choices
					SCREAMING_SNAKE_CASE	:       Dict 							=    scope
					SCREAMING_SNAKE_CASE	:       str 							=    initializer_std

					# Used in the tests to check the size of the first attention layer
					SCREAMING_SNAKE_CASE	:       str 							=    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	:       Tuple 							=    self.num_hidden_layers + 2





	def  _lowerCAmelCase    (			self							:					Any						)	->  Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    ids_tensor([self.batch_size, self.seq_length]    ,					self.vocab_size						)

					SCREAMING_SNAKE_CASE	:       Dict 							=    None
					if self.use_input_mask:
									SCREAMING_SNAKE_CASE	:       List[str] 							=    ids_tensor([self.batch_size, self.seq_length]    ,					vocab_size=2						)

					SCREAMING_SNAKE_CASE	:       int 							=    None
					if self.use_token_type_ids:
									SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    ids_tensor([self.batch_size, self.seq_length]    ,					self.type_vocab_size						)

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

					SCREAMING_SNAKE_CASE	:       Any 							=    self.get_config()

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





	def  _lowerCAmelCase    (			self							:					Any						)	->  Any:





					"""simple docstring"""


					return 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    ,					)





	def  _lowerCAmelCase    (			self							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					Any    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					Any    ,					)	->  Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    FunnelModel(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    model(_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE						)
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    model(_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE						)
					SCREAMING_SNAKE_CASE	:       int 							=    model(_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.last_hidden_state.shape    ,					(self.batch_size, self.seq_length, self.d_model)						)

					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    False
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    model(_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.last_hidden_state.shape    ,					(self.batch_size, self.seq_length, self.d_model)						)

					SCREAMING_SNAKE_CASE	:       Any 							=    False
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    model(_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.last_hidden_state.shape    ,					(self.batch_size, self.seq_length, self.d_model)						)





	def  _lowerCAmelCase    (			self							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Any    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					str    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					Any    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					)	->  str:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    FunnelBaseModel(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Any 							=    model(_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE						)
					SCREAMING_SNAKE_CASE	:       Dict 							=    model(_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE						)
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    model(_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.last_hidden_state.shape    ,					(self.batch_size, 2, self.d_model)						)

					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    False
					SCREAMING_SNAKE_CASE	:       List[str] 							=    model(_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.last_hidden_state.shape    ,					(self.batch_size, 3, self.d_model)						)

					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    False
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    model(_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.last_hidden_state.shape    ,					(self.batch_size, 2, self.d_model)						)





	def  _lowerCAmelCase    (			self							:					int    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					str    ,					)	->  Optional[Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    self.num_labels
					SCREAMING_SNAKE_CASE	:       Dict 							=    FunnelForPreTraining(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       int 							=    model(_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.seq_length)						)





	def  _lowerCAmelCase    (			self							:					str    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					str    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					int    ,					)	->  Optional[Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       int 							=    FunnelForMaskedLM(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    model(_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.seq_length, self.vocab_size)						)





	def  _lowerCAmelCase    (			self							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					str    ,					)	->  Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    self.num_labels
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    FunnelForSequenceClassification(_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Tuple 							=    model(_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.num_labels)						)





	def  _lowerCAmelCase    (			self							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					str    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Any    ,					_SCREAMING_SNAKE_CASE							:					Any    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					str    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    self.num_choices
					SCREAMING_SNAKE_CASE	:       Any 							=    FunnelForMultipleChoice(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       int 							=    input_ids.unsqueeze(1						).expand(-1    ,					self.num_choices    ,					-1						).contiguous()
					SCREAMING_SNAKE_CASE	:       Dict 							=    token_type_ids.unsqueeze(1						).expand(-1    ,					self.num_choices    ,					-1						).contiguous()
					SCREAMING_SNAKE_CASE	:       Tuple 							=    input_mask.unsqueeze(1						).expand(-1    ,					self.num_choices    ,					-1						).contiguous()
					SCREAMING_SNAKE_CASE	:       str 							=    model(
					    _SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE    ,					)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.num_choices)						)





	def  _lowerCAmelCase    (			self							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					int    ,					)	->  Optional[Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Dict 							=    self.num_labels
					SCREAMING_SNAKE_CASE	:       int 							=    FunnelForTokenClassification(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       str 							=    model(_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.seq_length, self.num_labels)						)





	def  _lowerCAmelCase    (			self							:					int    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					str    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					)	->  Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    FunnelForQuestionAnswering(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       int 							=    model(
					    _SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					start_positions=_SCREAMING_SNAKE_CASE    ,					end_positions=_SCREAMING_SNAKE_CASE    ,					)
					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							:					Dict						)	->  Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    self.prepare_config_and_inputs()
					(
					    (
					    SCREAMING_SNAKE_CASE		
					)							,	(
					    SCREAMING_SNAKE_CASE		
					)							,	(
					    SCREAMING_SNAKE_CASE		
					)							,	(
					    SCREAMING_SNAKE_CASE		
					)							,	(
					    SCREAMING_SNAKE_CASE		
					)							,	(
					    SCREAMING_SNAKE_CASE		
					)							,	(
					    SCREAMING_SNAKE_CASE		
					)							,	(
					    SCREAMING_SNAKE_CASE		
					)							,			
					)	:       Tuple 							=    config_and_inputs
					SCREAMING_SNAKE_CASE	:       int 							=    {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
					return config, inputs_dict




@require_torch
class 						lowerCAmelCase__	(  UpperCAmelCase_       ,       UpperCAmelCase_       ,       unittest.TestCase   ):


	'''simple docstring'''







	_SCREAMING_SNAKE_CASE  :		Tuple     	=   False
	_SCREAMING_SNAKE_CASE  :		Optional[Any]     	=   False
	_SCREAMING_SNAKE_CASE  :		Optional[Any]     	=   (
	    (
	        FunnelModel,
	        FunnelForMaskedLM,
	        FunnelForPreTraining,
	        FunnelForQuestionAnswering,
	        FunnelForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	_SCREAMING_SNAKE_CASE  :		Tuple     	=   (
	    {
	        '''feature-extraction''': (FunnelBaseModel, FunnelModel),
	        '''fill-mask''': FunnelForMaskedLM,
	        '''question-answering''': FunnelForQuestionAnswering,
	        '''text-classification''': FunnelForSequenceClassification,
	        '''token-classification''': FunnelForTokenClassification,
	        '''zero-shot''': FunnelForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)





	def  _lowerCAmelCase    (			self							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					str=False						)	->  Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    super()._prepare_for_class(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					return_labels=_SCREAMING_SNAKE_CASE						)

					if return_labels:
									if model_class in get_values(_SCREAMING_SNAKE_CASE						):
													SCREAMING_SNAKE_CASE	:       List[str] 							=    torch.zeros(
													    (self.model_tester.batch_size, self.model_tester.seq_length)    ,					dtype=torch.long    ,					device=_SCREAMING_SNAKE_CASE						)
					return inputs_dict





	def  _lowerCAmelCase    (			self							:					List[Any]						)	->  List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       int 							=    FunnelModelTester(self						)
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    ConfigTester(self    ,					config_class=_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Tuple						)	->  Tuple:





					"""simple docstring"""


					self.config_tester.run_common_tests()





	def  _lowerCAmelCase    (			self							:					List[str]						)	->  List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Optional[Any]						)	->  Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_pretraining(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					str						)	->  str:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       int 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Tuple						)	->  Dict:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Dict 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Any						)	->  Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[str] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE						)







	def  _lowerCAmelCase    (			self							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					List[str]						)	->  int:





					"""simple docstring"""


					if hasattr(_SCREAMING_SNAKE_CASE    ,					'weight'						) and module.weight is not None:
									module.weight.data.fill_(3						)
					if hasattr(_SCREAMING_SNAKE_CASE    ,					'bias'						) and module.bias is not None:
									module.bias.data.fill_(3						)

					for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
									if hasattr(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						) and getattr(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						) is not None:
													SCREAMING_SNAKE_CASE	:       List[Any] 							=    getattr(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						)
													weight.data.fill_(3						)




@require_torch
class 						lowerCAmelCase__	(  UpperCAmelCase_       ,       unittest.TestCase   ):


	'''simple docstring'''







	_SCREAMING_SNAKE_CASE  :		int     	=   False
	_SCREAMING_SNAKE_CASE  :		List[Any]     	=   False
	_SCREAMING_SNAKE_CASE  :		int     	=   (
	    (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
	)





	def  _lowerCAmelCase    (			self							:					Union[str, Any]						)	->  Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       str 							=    FunnelModelTester(self    ,					base=_SCREAMING_SNAKE_CASE						)
					SCREAMING_SNAKE_CASE	:       str 							=    ConfigTester(self    ,					config_class=_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					str						)	->  Any:





					"""simple docstring"""


					self.config_tester.run_common_tests()





	def  _lowerCAmelCase    (			self							:					Optional[Any]						)	->  str:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_base_model(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Dict						)	->  Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Tuple 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Tuple						)	->  Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[Any] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Union[str, Any]						)	->  str:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE							,	SCREAMING_SNAKE_CASE	:       Any 							=    self.model_tester.prepare_config_and_inputs_for_common()
					SCREAMING_SNAKE_CASE	:       List[str] 							=    True

					for model_class in self.all_model_classes:
									if model_class.__name__ == "FunnelBaseModel":
													continue
									SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    model_class(_SCREAMING_SNAKE_CASE						)
									model.to(_SCREAMING_SNAKE_CASE						)
									model.train()
									SCREAMING_SNAKE_CASE	:       List[str] 							=    self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					return_labels=_SCREAMING_SNAKE_CASE						)
									SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    model(**_SCREAMING_SNAKE_CASE						).loss
									loss.backward()







	def  _lowerCAmelCase    (			self							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]						)	->  List[Any]:





					"""simple docstring"""


					if hasattr(_SCREAMING_SNAKE_CASE    ,					'weight'						) and module.weight is not None:
									module.weight.data.fill_(3						)
					if hasattr(_SCREAMING_SNAKE_CASE    ,					'bias'						) and module.bias is not None:
									module.bias.data.fill_(3						)

					for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
									if hasattr(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						) and getattr(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						) is not None:
													SCREAMING_SNAKE_CASE	:       List[Any] 							=    getattr(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						)
													weight.data.fill_(3						)




@require_torch
@require_sentencepiece
@require_tokenizers
class 						lowerCAmelCase__	(  unittest.TestCase   ):


	'''simple docstring'''







	def  _lowerCAmelCase    (			self							:					Any						)	->  Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       str 							=    13
					SCREAMING_SNAKE_CASE	:       Any 							=    7
					SCREAMING_SNAKE_CASE	:       Any 							=    torch.arange(0    ,					batch_size * sequence_length						).long().reshape(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						)
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    [0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
					SCREAMING_SNAKE_CASE	:       Any 							=    torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]						)

					SCREAMING_SNAKE_CASE	:       Tuple 							=    FunnelModel.from_pretrained('sgugger/funnel-random-tiny'						)
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    model(_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE						)[0].abs()

					SCREAMING_SNAKE_CASE	:       List[Any] 							=    torch.tensor(2_344.8_352						)
					SCREAMING_SNAKE_CASE	:       Dict 							=    torch.tensor(0.8_0_5_2						)
					self.assertTrue(torch.allclose(output.sum()    ,					_SCREAMING_SNAKE_CASE    ,					atol=1E-4						)						)
					self.assertTrue(torch.allclose(output.mean()    ,					_SCREAMING_SNAKE_CASE    ,					atol=1E-4						)						)

					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    torch.tensor([[1] * 7, [1] * 4 + [0] * 3] * 6 + [[0, 1, 1, 0, 0, 1, 1]]						)
					SCREAMING_SNAKE_CASE	:       str 							=    model(_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE						)[0].abs()

					SCREAMING_SNAKE_CASE	:       Dict 							=    torch.tensor(2_343.8_425						)
					SCREAMING_SNAKE_CASE	:       int 							=    torch.tensor(0.8_0_4_9						)
					self.assertTrue(torch.allclose(output.sum()    ,					_SCREAMING_SNAKE_CASE    ,					atol=1E-4						)						)
					self.assertTrue(torch.allclose(output.mean()    ,					_SCREAMING_SNAKE_CASE    ,					atol=1E-4						)						)







	@slow
	def  _lowerCAmelCase    (			self							:					str						)	->  Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    FunnelTokenizer.from_pretrained('huggingface/funnel-small'						)
					SCREAMING_SNAKE_CASE	:       Tuple 							=    FunnelModel.from_pretrained('huggingface/funnel-small'						)
					SCREAMING_SNAKE_CASE	:       List[str] 							=    tokenizer('Hello! I am the Funnel Transformer model.'    ,					return_tensors='pt'						)
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    model(**_SCREAMING_SNAKE_CASE						)[0]

					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    torch.tensor(2_3_5.7_2_4_6						)
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    torch.tensor(0.0_2_5_6						)
					self.assertTrue(torch.allclose(output.sum()    ,					_SCREAMING_SNAKE_CASE    ,					atol=1E-4						)						)
					self.assertTrue(torch.allclose(output.mean()    ,					_SCREAMING_SNAKE_CASE    ,					atol=1E-4						)						)










"""simple docstring"""

import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import ConvNextImageProcessor
class 						lowerCAmelCase__	(  unittest.TestCase   ):


	'''simple docstring'''







	def __init__(			self							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]=7    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]=3    ,					_SCREAMING_SNAKE_CASE							:					List[str]=18    ,					_SCREAMING_SNAKE_CASE							:					str=30    ,					_SCREAMING_SNAKE_CASE							:					str=400    ,					_SCREAMING_SNAKE_CASE							:					Any=True    ,					_SCREAMING_SNAKE_CASE							:					int=None    ,					_SCREAMING_SNAKE_CASE							:					Dict=0.8_7_5    ,					_SCREAMING_SNAKE_CASE							:					List[str]=True    ,					_SCREAMING_SNAKE_CASE							:					List[Any]=[0.5, 0.5, 0.5]    ,					_SCREAMING_SNAKE_CASE							:					str=[0.5, 0.5, 0.5]    ,					)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       int 							=    size if size is not None else {'shortest_edge': 20}
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    parent
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    batch_size
					SCREAMING_SNAKE_CASE	:       Any 							=    num_channels
					SCREAMING_SNAKE_CASE	:       Any 							=    image_size
					SCREAMING_SNAKE_CASE	:       str 							=    min_resolution
					SCREAMING_SNAKE_CASE	:       Dict 							=    max_resolution
					SCREAMING_SNAKE_CASE	:       Dict 							=    do_resize
					SCREAMING_SNAKE_CASE	:       List[str] 							=    size
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    crop_pct
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    do_normalize
					SCREAMING_SNAKE_CASE	:       Dict 							=    image_mean
					SCREAMING_SNAKE_CASE	:       str 							=    image_std







	def  _lowerCAmelCase    (			self							:					Dict						)	->  Any:





					"""simple docstring"""


					return {
					    "image_mean": self.image_mean,
					    "image_std": self.image_std,
					    "do_normalize": self.do_normalize,
					    "do_resize": self.do_resize,
					    "size": self.size,
					    "crop_pct": self.crop_pct,
					}




@require_torch
@require_vision
class 						lowerCAmelCase__	(  _lowerCamelCase       ,       unittest.TestCase   ):


	'''simple docstring'''







	_SCREAMING_SNAKE_CASE  :		Optional[Any]     	=   ConvNextImageProcessor if is_vision_available() else None





	def  _lowerCAmelCase    (			self							:					Any						)	->  Dict:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    ConvNextImageProcessingTester(self						)





	@property
	def  _lowerCAmelCase    (			self							:					Optional[int]						)	->  Dict:





					"""simple docstring"""


					return self.image_processor_tester.prepare_image_processor_dict()





	def  _lowerCAmelCase    (			self							:					str						)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    self.image_processing_class(**self.image_processor_dict						)
					self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE    ,					'do_resize'						)						)
					self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE    ,					'size'						)						)
					self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE    ,					'crop_pct'						)						)
					self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE    ,					'do_normalize'						)						)
					self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE    ,					'image_mean'						)						)
					self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE    ,					'image_std'						)						)





	def  _lowerCAmelCase    (			self							:					Union[str, Any]						)	->  Union[str, Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    self.image_processing_class.from_dict(self.image_processor_dict						)
					self.assertEqual(image_processor.size    ,					{'shortest_edge': 20}						)

					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    self.image_processing_class.from_dict(self.image_processor_dict    ,					size=42						)
					self.assertEqual(image_processor.size    ,					{'shortest_edge': 42}						)





	def  _lowerCAmelCase    (			self							:					int						)	->  str:





					"""simple docstring"""


					pass





	def  _lowerCAmelCase    (			self							:					Tuple						)	->  Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[str] 							=    self.image_processing_class(**self.image_processor_dict						)
					# create random PIL images
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    prepare_image_inputs(self.image_processor_tester    ,					equal_resolution=_SCREAMING_SNAKE_CASE						)
					for image in image_inputs:
									self.assertIsInstance(_SCREAMING_SNAKE_CASE    ,					Image.Image						)

					# Test not batched input
					SCREAMING_SNAKE_CASE	:       str 							=    image_processing(image_inputs[0]    ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape    ,					(
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.size['shortest_edge'],
					        self.image_processor_tester.size['shortest_edge'],
					    )    ,					)

					# Test batched
					SCREAMING_SNAKE_CASE	:       str 							=    image_processing(_SCREAMING_SNAKE_CASE    ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape    ,					(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.size['shortest_edge'],
					        self.image_processor_tester.size['shortest_edge'],
					    )    ,					)





	def  _lowerCAmelCase    (			self							:					int						)	->  str:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    self.image_processing_class(**self.image_processor_dict						)
					# create random numpy tensors
					SCREAMING_SNAKE_CASE	:       Tuple 							=    prepare_image_inputs(self.image_processor_tester    ,					equal_resolution=_SCREAMING_SNAKE_CASE    ,					numpify=_SCREAMING_SNAKE_CASE						)
					for image in image_inputs:
									self.assertIsInstance(_SCREAMING_SNAKE_CASE    ,					np.ndarray						)

					# Test not batched input
					SCREAMING_SNAKE_CASE	:       int 							=    image_processing(image_inputs[0]    ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape    ,					(
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.size['shortest_edge'],
					        self.image_processor_tester.size['shortest_edge'],
					    )    ,					)

					# Test batched
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    image_processing(_SCREAMING_SNAKE_CASE    ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape    ,					(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.size['shortest_edge'],
					        self.image_processor_tester.size['shortest_edge'],
					    )    ,					)







	def  _lowerCAmelCase    (			self							:					Optional[Any]						)	->  Dict:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       str 							=    self.image_processing_class(**self.image_processor_dict						)
					# create random PyTorch tensors
					SCREAMING_SNAKE_CASE	:       Tuple 							=    prepare_image_inputs(self.image_processor_tester    ,					equal_resolution=_SCREAMING_SNAKE_CASE    ,					torchify=_SCREAMING_SNAKE_CASE						)
					for image in image_inputs:
									self.assertIsInstance(_SCREAMING_SNAKE_CASE    ,					torch.Tensor						)

					# Test not batched input
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    image_processing(image_inputs[0]    ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape    ,					(
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.size['shortest_edge'],
					        self.image_processor_tester.size['shortest_edge'],
					    )    ,					)

					# Test batched
					SCREAMING_SNAKE_CASE	:       Any 							=    image_processing(_SCREAMING_SNAKE_CASE    ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape    ,					(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.size['shortest_edge'],
					        self.image_processor_tester.size['shortest_edge'],
					    )    ,					)





