

"""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      __UpperCamelCase					:


		def __init__(self					:						Dict	,	__SCREAMING_SNAKE_CASE					:						List[str]	,	__SCREAMING_SNAKE_CASE					:						List[Any]=1_3	,	__SCREAMING_SNAKE_CASE					:						List[str]=7	,	__SCREAMING_SNAKE_CASE					:						Optional[int]=True	,	__SCREAMING_SNAKE_CASE					:						str=True	,	__SCREAMING_SNAKE_CASE					:						Tuple=True	,	__SCREAMING_SNAKE_CASE					:						List[Any]=True	,	__SCREAMING_SNAKE_CASE					:						List[str]=9_9	,	__SCREAMING_SNAKE_CASE					:						Any=[1, 1, 2]	,	__SCREAMING_SNAKE_CASE					:						int=1	,	__SCREAMING_SNAKE_CASE					:						List[str]=3_2	,	__SCREAMING_SNAKE_CASE					:						List[str]=4	,	__SCREAMING_SNAKE_CASE					:						Union[str, Any]=8	,	__SCREAMING_SNAKE_CASE					:						List[str]=3_7	,	__SCREAMING_SNAKE_CASE					:						Tuple="gelu_new"	,	__SCREAMING_SNAKE_CASE					:						List[Any]=0.1	,	__SCREAMING_SNAKE_CASE					:						str=0.1	,	__SCREAMING_SNAKE_CASE					:						Dict=0.0	,	__SCREAMING_SNAKE_CASE					:						List[str]=5_1_2	,	__SCREAMING_SNAKE_CASE					:						Union[str, Any]=3	,	__SCREAMING_SNAKE_CASE					:						List[Any]=0.0_2	,	__SCREAMING_SNAKE_CASE					:						Optional[Any]=3	,	__SCREAMING_SNAKE_CASE					:						Optional[int]=4	,	__SCREAMING_SNAKE_CASE					:						Union[str, Any]=None	,	__SCREAMING_SNAKE_CASE					:						Tuple=False	,	):
							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    SCREAMING_SNAKE_CASE__  (self					:						Optional[Any]):
							A     =   ids_tensor([self.batch_size, self.seq_length]	,	self.vocab_size)

							A     =   None
							if self.use_input_mask:
												A     =   ids_tensor([self.batch_size, self.seq_length]	,	vocab_size=2)

							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     =   ids_tensor([self.batch_size, self.seq_length]	,	1)

							A     =   self.get_config()

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


		def    SCREAMING_SNAKE_CASE__  (self					:						Dict):
							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    SCREAMING_SNAKE_CASE__  (self					:						List[str]	,	__SCREAMING_SNAKE_CASE					:						int	,	__SCREAMING_SNAKE_CASE					:						Dict	,	__SCREAMING_SNAKE_CASE					:						Dict	,	__SCREAMING_SNAKE_CASE					:						Optional[int]	,	__SCREAMING_SNAKE_CASE					:						Optional[int]	,	__SCREAMING_SNAKE_CASE					:						Optional[int]	,	__SCREAMING_SNAKE_CASE					:						Any	,	__SCREAMING_SNAKE_CASE					:						List[str]	,	):
							A     =   FunnelModel(config=__SCREAMING_SNAKE_CASE)
							model.to(__SCREAMING_SNAKE_CASE)
							model.eval()
							A     =   model(__SCREAMING_SNAKE_CASE	,	attention_mask=__SCREAMING_SNAKE_CASE	,	token_type_ids=__SCREAMING_SNAKE_CASE)
							A     =   model(__SCREAMING_SNAKE_CASE	,	token_type_ids=__SCREAMING_SNAKE_CASE)
							A     =   model(__SCREAMING_SNAKE_CASE)
							self.parent.assertEqual(result.last_hidden_state.shape	,	(self.batch_size, self.seq_length, self.d_model))

							A     =   False
							A     =   model(__SCREAMING_SNAKE_CASE)
							self.parent.assertEqual(result.last_hidden_state.shape	,	(self.batch_size, self.seq_length, self.d_model))

							A     =   False
							A     =   model(__SCREAMING_SNAKE_CASE)
							self.parent.assertEqual(result.last_hidden_state.shape	,	(self.batch_size, self.seq_length, self.d_model))


		def    SCREAMING_SNAKE_CASE__  (self					:						Optional[Any]	,	__SCREAMING_SNAKE_CASE					:						int	,	__SCREAMING_SNAKE_CASE					:						int	,	__SCREAMING_SNAKE_CASE					:						Any	,	__SCREAMING_SNAKE_CASE					:						Dict	,	__SCREAMING_SNAKE_CASE					:						Dict	,	__SCREAMING_SNAKE_CASE					:						Dict	,	__SCREAMING_SNAKE_CASE					:						Union[str, Any]	,	__SCREAMING_SNAKE_CASE					:						List[Any]	,	):
							A     =   FunnelBaseModel(config=__SCREAMING_SNAKE_CASE)
							model.to(__SCREAMING_SNAKE_CASE)
							model.eval()
							A     =   model(__SCREAMING_SNAKE_CASE	,	attention_mask=__SCREAMING_SNAKE_CASE	,	token_type_ids=__SCREAMING_SNAKE_CASE)
							A     =   model(__SCREAMING_SNAKE_CASE	,	token_type_ids=__SCREAMING_SNAKE_CASE)
							A     =   model(__SCREAMING_SNAKE_CASE)
							self.parent.assertEqual(result.last_hidden_state.shape	,	(self.batch_size, 2, self.d_model))

							A     =   False
							A     =   model(__SCREAMING_SNAKE_CASE)
							self.parent.assertEqual(result.last_hidden_state.shape	,	(self.batch_size, 3, self.d_model))

							A     =   False
							A     =   model(__SCREAMING_SNAKE_CASE)
							self.parent.assertEqual(result.last_hidden_state.shape	,	(self.batch_size, 2, self.d_model))


		def    SCREAMING_SNAKE_CASE__  (self					:						List[Any]	,	__SCREAMING_SNAKE_CASE					:						str	,	__SCREAMING_SNAKE_CASE					:						Union[str, Any]	,	__SCREAMING_SNAKE_CASE					:						Optional[int]	,	__SCREAMING_SNAKE_CASE					:						Optional[int]	,	__SCREAMING_SNAKE_CASE					:						Tuple	,	__SCREAMING_SNAKE_CASE					:						Dict	,	__SCREAMING_SNAKE_CASE					:						List[Any]	,	__SCREAMING_SNAKE_CASE					:						Tuple	,	):
							A     =   self.num_labels
							A     =   FunnelForPreTraining(config=__SCREAMING_SNAKE_CASE)
							model.to(__SCREAMING_SNAKE_CASE)
							model.eval()
							A     =   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    SCREAMING_SNAKE_CASE__  (self					:						List[Any]	,	__SCREAMING_SNAKE_CASE					:						List[Any]	,	__SCREAMING_SNAKE_CASE					:						Tuple	,	__SCREAMING_SNAKE_CASE					:						Optional[Any]	,	__SCREAMING_SNAKE_CASE					:						Optional[Any]	,	__SCREAMING_SNAKE_CASE					:						List[str]	,	__SCREAMING_SNAKE_CASE					:						int	,	__SCREAMING_SNAKE_CASE					:						str	,	__SCREAMING_SNAKE_CASE					:						Tuple	,	):
							A     =   FunnelForMaskedLM(config=__SCREAMING_SNAKE_CASE)
							model.to(__SCREAMING_SNAKE_CASE)
							model.eval()
							A     =   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    SCREAMING_SNAKE_CASE__  (self					:						Optional[Any]	,	__SCREAMING_SNAKE_CASE					:						str	,	__SCREAMING_SNAKE_CASE					:						str	,	__SCREAMING_SNAKE_CASE					:						int	,	__SCREAMING_SNAKE_CASE					:						List[str]	,	__SCREAMING_SNAKE_CASE					:						Tuple	,	__SCREAMING_SNAKE_CASE					:						Any	,	__SCREAMING_SNAKE_CASE					:						Union[str, Any]	,	__SCREAMING_SNAKE_CASE					:						Optional[int]	,	):
							A     =   self.num_labels
							A     =   FunnelForSequenceClassification(__SCREAMING_SNAKE_CASE)
							model.to(__SCREAMING_SNAKE_CASE)
							model.eval()
							A     =   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    SCREAMING_SNAKE_CASE__  (self					:						Optional[int]	,	__SCREAMING_SNAKE_CASE					:						Any	,	__SCREAMING_SNAKE_CASE					:						Tuple	,	__SCREAMING_SNAKE_CASE					:						str	,	__SCREAMING_SNAKE_CASE					:						Dict	,	__SCREAMING_SNAKE_CASE					:						Optional[int]	,	__SCREAMING_SNAKE_CASE					:						int	,	__SCREAMING_SNAKE_CASE					:						List[Any]	,	__SCREAMING_SNAKE_CASE					:						Dict	,	):
							A     =   self.num_choices
							A     =   FunnelForMultipleChoice(config=__SCREAMING_SNAKE_CASE)
							model.to(__SCREAMING_SNAKE_CASE)
							model.eval()
							A     =   input_ids.unsqueeze(1).expand(-1	,	self.num_choices	,	-1).contiguous()
							A     =   token_type_ids.unsqueeze(1).expand(-1	,	self.num_choices	,	-1).contiguous()
							A     =   input_mask.unsqueeze(1).expand(-1	,	self.num_choices	,	-1).contiguous()
							A     =   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    SCREAMING_SNAKE_CASE__  (self					:						Union[str, Any]	,	__SCREAMING_SNAKE_CASE					:						Union[str, Any]	,	__SCREAMING_SNAKE_CASE					:						Tuple	,	__SCREAMING_SNAKE_CASE					:						Optional[Any]	,	__SCREAMING_SNAKE_CASE					:						Any	,	__SCREAMING_SNAKE_CASE					:						Optional[int]	,	__SCREAMING_SNAKE_CASE					:						int	,	__SCREAMING_SNAKE_CASE					:						Union[str, Any]	,	__SCREAMING_SNAKE_CASE					:						List[Any]	,	):
							A     =   self.num_labels
							A     =   FunnelForTokenClassification(config=__SCREAMING_SNAKE_CASE)
							model.to(__SCREAMING_SNAKE_CASE)
							model.eval()
							A     =   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    SCREAMING_SNAKE_CASE__  (self					:						Dict	,	__SCREAMING_SNAKE_CASE					:						Any	,	__SCREAMING_SNAKE_CASE					:						Optional[int]	,	__SCREAMING_SNAKE_CASE					:						Optional[Any]	,	__SCREAMING_SNAKE_CASE					:						int	,	__SCREAMING_SNAKE_CASE					:						List[str]	,	__SCREAMING_SNAKE_CASE					:						List[str]	,	__SCREAMING_SNAKE_CASE					:						int	,	__SCREAMING_SNAKE_CASE					:						Union[str, Any]	,	):
							A     =   FunnelForQuestionAnswering(config=__SCREAMING_SNAKE_CASE)
							model.to(__SCREAMING_SNAKE_CASE)
							model.eval()
							A     =   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    SCREAMING_SNAKE_CASE__  (self					:						Tuple):
							A     =   self.prepare_config_and_inputs()
							(
							    (
							    A
							)     ,       (
							    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_torch
class      __UpperCamelCase					(  _A	,						_A	,						unittest.TestCase		):
		SCREAMING_SNAKE_CASE														=       False
		SCREAMING_SNAKE_CASE														=       False
		SCREAMING_SNAKE_CASE														=       (
		    (
		        FunnelModel,
		        FunnelForMaskedLM,
		        FunnelForPreTraining,
		        FunnelForQuestionAnswering,
		        FunnelForTokenClassification,
		    )
		    if is_torch_available()
		    else ()
		)
		SCREAMING_SNAKE_CASE														=       (
		    {
		        "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    SCREAMING_SNAKE_CASE__  (self					:						Dict	,	__SCREAMING_SNAKE_CASE					:						Optional[int]	,	__SCREAMING_SNAKE_CASE					:						List[str]	,	__SCREAMING_SNAKE_CASE					:						Any=False):
							A     =   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):
																	A     =   torch.zeros(
																	    (self.model_tester.batch_size, self.model_tester.seq_length)	,	dtype=torch.long	,	device=__SCREAMING_SNAKE_CASE)
							return inputs_dict


		def    SCREAMING_SNAKE_CASE__  (self					:						int):
							A     =   FunnelModelTester(self)
							A     =   ConfigTester(self	,	config_class=__SCREAMING_SNAKE_CASE)


		def    SCREAMING_SNAKE_CASE__  (self					:						Optional[Any]):
							self.config_tester.run_common_tests()


		def    SCREAMING_SNAKE_CASE__  (self					:						Tuple):
							A     =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE)


		def    SCREAMING_SNAKE_CASE__  (self					:						List[Any]):
							A     =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*__SCREAMING_SNAKE_CASE)


		def    SCREAMING_SNAKE_CASE__  (self					:						List[str]):
							A     =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE)


		def    SCREAMING_SNAKE_CASE__  (self					:						str):
							A     =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE)


		def    SCREAMING_SNAKE_CASE__  (self					:						List[str]):
							A     =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE)


		def    SCREAMING_SNAKE_CASE__  (self					:						Optional[Any]	,	__SCREAMING_SNAKE_CASE					:						Tuple):
							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:
																	A     =   getattr(__SCREAMING_SNAKE_CASE	,	__SCREAMING_SNAKE_CASE)
																	weight.data.fill_(3)





@require_torch
class      __UpperCamelCase					(  _A	,						unittest.TestCase		):
		SCREAMING_SNAKE_CASE														=       False
		SCREAMING_SNAKE_CASE														=       False
		SCREAMING_SNAKE_CASE														=       (
		    (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
		)


		def    SCREAMING_SNAKE_CASE__  (self					:						Union[str, Any]):
							A     =   FunnelModelTester(self	,	base=__SCREAMING_SNAKE_CASE)
							A     =   ConfigTester(self	,	config_class=__SCREAMING_SNAKE_CASE)


		def    SCREAMING_SNAKE_CASE__  (self					:						str):
							self.config_tester.run_common_tests()


		def    SCREAMING_SNAKE_CASE__  (self					:						Optional[Any]):
							A     =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_base_model(*__SCREAMING_SNAKE_CASE)


		def    SCREAMING_SNAKE_CASE__  (self					:						str):
							A     =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE)


		def    SCREAMING_SNAKE_CASE__  (self					:						str):
							A     =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*__SCREAMING_SNAKE_CASE)


		def    SCREAMING_SNAKE_CASE__  (self					:						str):
							A     ,       A     =   self.model_tester.prepare_config_and_inputs_for_common()
							A     =   True

							for model_class in self.all_model_classes:
												if model_class.__name__ == "FunnelBaseModel":
																	continue
												A     =   model_class(__SCREAMING_SNAKE_CASE)
												model.to(__SCREAMING_SNAKE_CASE)
												model.train()
												A     =   self._prepare_for_class(__SCREAMING_SNAKE_CASE	,	__SCREAMING_SNAKE_CASE	,	return_labels=__SCREAMING_SNAKE_CASE)
												A     =   model(**__SCREAMING_SNAKE_CASE).loss
												loss.backward()


		def    SCREAMING_SNAKE_CASE__  (self					:						List[Any]	,	__SCREAMING_SNAKE_CASE					:						Tuple):
							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:
																	A     =   getattr(__SCREAMING_SNAKE_CASE	,	__SCREAMING_SNAKE_CASE)
																	weight.data.fill_(3)





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


		def    SCREAMING_SNAKE_CASE__  (self					:						str):
							A     =   1_3
							A     =   7
							A     =   torch.arange(0	,	batch_size * sequence_length).long().reshape(__SCREAMING_SNAKE_CASE	,	__SCREAMING_SNAKE_CASE)
							A     =   [0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
							A     =   torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths])

							A     =   FunnelModel.from_pretrained("sgugger/funnel-random-tiny")
							A     =   model(__SCREAMING_SNAKE_CASE	,	token_type_ids=__SCREAMING_SNAKE_CASE)[0].abs()

							A     =   torch.tensor(2_3_4_4.8_3_5_2)
							A     =   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))

							A     =   torch.tensor([[1] * 7, [1] * 4 + [0] * 3] * 6 + [[0, 1, 1, 0, 0, 1, 1]])
							A     =   model(__SCREAMING_SNAKE_CASE	,	attention_mask=__SCREAMING_SNAKE_CASE	,	token_type_ids=__SCREAMING_SNAKE_CASE)[0].abs()

							A     =   torch.tensor(2_3_4_3.8_4_2_5)
							A     =   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    SCREAMING_SNAKE_CASE__  (self					:						Optional[int]):
							A     =   FunnelTokenizer.from_pretrained("huggingface/funnel-small")
							A     =   FunnelModel.from_pretrained("huggingface/funnel-small")
							A     =   tokenizer("Hello! I am the Funnel Transformer model."	,	return_tensors="pt")
							A     =   model(**__SCREAMING_SNAKE_CASE)[0]

							A     =   torch.tensor(2_3_5.7_2_4_6)
							A     =   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"""

from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
			import tensorflow as tf

			from transformers import TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
			from PIL import Image

			from transformers import ConvNextImageProcessor

class     A_				:







				def __init__(				self				:    Optional[int]    ,     __lowerCamelCase				:    Tuple    ,     __lowerCamelCase				:    int=1_3    ,     __lowerCamelCase				:    Any=3_2    ,     __lowerCamelCase				:    Dict=3    ,     __lowerCamelCase				:    List[Any]=4    ,     __lowerCamelCase				:    Union[str, Any]=[1_0, 2_0, 3_0, 4_0]    ,     __lowerCamelCase				:    int=[2, 2, 3, 2]    ,     __lowerCamelCase				:    str=True    ,     __lowerCamelCase				:    List[str]=True    ,     __lowerCamelCase				:    List[Any]=3_7    ,     __lowerCamelCase				:    Optional[Any]="gelu"    ,     __lowerCamelCase				:    str=1_0    ,     __lowerCamelCase				:    int=0.02    ,     __lowerCamelCase				:    Optional[int]=3    ,     __lowerCamelCase				:    Any=None    ,     )					->       List[Any]:
										__magic_name__      =  parent
										__magic_name__      =  batch_size
										__magic_name__      =  image_size
										__magic_name__      =  num_channels
										__magic_name__      =  num_stages
										__magic_name__      =  hidden_sizes
										__magic_name__      =  depths
										__magic_name__      =  is_training
										__magic_name__      =  use_labels
										__magic_name__      =  intermediate_size
										__magic_name__      =  hidden_act
										__magic_name__      =  type_sequence_label_size
										__magic_name__      =  initializer_range
										__magic_name__      =  scope







				def      _snake_case       (				self				:    Optional[Any]						)					->       int:
										__magic_name__      =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

										__magic_name__      =  None
										if self.use_labels:
																__magic_name__      =  ids_tensor([self.batch_size]    ,     self.type_sequence_label_size						)

										__magic_name__      =  self.get_config()

										return config, pixel_values, labels







				def      _snake_case       (				self				:    Optional[int]						)					->       Union[str, Any]:
										return ConvNextConfig(
										    num_channels=self.num_channels    ,     hidden_sizes=self.hidden_sizes    ,     depths=self.depths    ,     num_stages=self.num_stages    ,     hidden_act=self.hidden_act    ,     is_decoder=__lowerCamelCase    ,     initializer_range=self.initializer_range    ,     )







				def      _snake_case       (				self				:    List[Any]    ,     __lowerCamelCase				:    List[str]    ,     __lowerCamelCase				:    Union[str, Any]    ,     __lowerCamelCase				:    Optional[Any]						)					->       List[str]:
										__magic_name__      =  TFConvNextModel(config=__lowerCamelCase						)
										__magic_name__      =  model(__lowerCamelCase    ,     training=__lowerCamelCase						)
										# expected last hidden states: B, C, H // 32, W // 32
										self.parent.assertEqual(
										    result.last_hidden_state.shape    ,     (self.batch_size, self.hidden_sizes[-1], self.image_size // 3_2, self.image_size // 3_2)    ,     )







				def      _snake_case       (				self				:    List[Any]    ,     __lowerCamelCase				:    Union[str, Any]    ,     __lowerCamelCase				:    Tuple    ,     __lowerCamelCase				:    List[str]						)					->       int:
										__magic_name__      =  self.type_sequence_label_size
										__magic_name__      =  TFConvNextForImageClassification(__lowerCamelCase						)
										__magic_name__      =  model(__lowerCamelCase    ,     labels=__lowerCamelCase    ,     training=__lowerCamelCase						)
										self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.type_sequence_label_size)						)







				def      _snake_case       (				self				:    List[str]						)					->       str:
										__magic_name__      =  self.prepare_config_and_inputs()
										__magic_name__   ,     __magic_name__   ,     __magic_name__      =  config_and_inputs
										__magic_name__      =  {"pixel_values": pixel_values}
										return config, inputs_dict





@require_tf
class     A_				(   snake_case_       ,				snake_case_       ,				unittest.TestCase	):
				UpperCAmelCase__										=					(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
				UpperCAmelCase__										=					(
				    {'''feature-extraction''': TFConvNextModel, '''image-classification''': TFConvNextForImageClassification}
				    if is_tf_available()
				    else {}
				)

				UpperCAmelCase__										=					False
				UpperCAmelCase__										=					False
				UpperCAmelCase__										=					False
				UpperCAmelCase__										=					False
				UpperCAmelCase__										=					False







				def      _snake_case       (				self				:    str						)					->       str:
										__magic_name__      =  TFConvNextModelTester(self						)
										__magic_name__      =  ConfigTester(
										    self    ,     config_class=__lowerCamelCase    ,     has_text_modality=__lowerCamelCase    ,     hidden_size=3_7    ,     )







				@unittest.skip(reason="ConvNext does not use inputs_embeds"						)
				def      _snake_case       (				self				:    Dict						)					->       Dict:
										pass







				@unittest.skipIf(
				    not is_tf_available() or len(tf.config.list_physical_devices("GPU"						)						) == 0    ,     reason="TF does not support backprop for grouped convolutions on CPU."    ,     )
				@slow
				def      _snake_case       (				self				:    List[str]						)					->       Tuple:
										super().test_keras_fit()







				@unittest.skip(reason="ConvNext does not support input and output embeddings"						)
				def      _snake_case       (				self				:    str						)					->       Tuple:
										pass







				def      _snake_case       (				self				:    Union[str, Any]						)					->       Optional[int]:
										__magic_name__   ,     __magic_name__      =  self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																__magic_name__      =  model_class(__lowerCamelCase						)
																__magic_name__      =  inspect.signature(model.call						)
																# signature.parameters is an OrderedDict => so arg_names order is deterministic
																__magic_name__      =  [*signature.parameters.keys()]

																__magic_name__      =  ["pixel_values"]
																self.assertListEqual(arg_names[:1]    ,     __lowerCamelCase						)







				def      _snake_case       (				self				:    Dict						)					->       Optional[int]:
										__magic_name__      =  self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__lowerCamelCase						)







				@unittest.skipIf(
				    not is_tf_available() or len(tf.config.list_physical_devices("GPU"						)						) == 0    ,     reason="TF does not support backprop for grouped convolutions on CPU."    ,     )
				def      _snake_case       (				self				:    Optional[int]						)					->       Tuple:
										super().test_dataset_conversion()







				def      _snake_case       (				self				:    int						)					->       List[str]:
										def check_hidden_states_output(__lowerCamelCase				:    Union[str, Any]    ,     __lowerCamelCase				:    Dict    ,     __lowerCamelCase				:    List[str]						):
																__magic_name__      =  model_class(__lowerCamelCase						)

																__magic_name__      =  model(**self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase						)						)
																__magic_name__      =  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																__magic_name__      =  self.model_tester.num_stages
																self.assertEqual(len(__lowerCamelCase						)    ,     expected_num_stages + 1						)

																# ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
																self.assertListEqual(
																    list(hidden_states[0].shape[-2:]						)    ,     [self.model_tester.image_size // 4, self.model_tester.image_size // 4]    ,     )

										__magic_name__   ,     __magic_name__      =  self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																__magic_name__      =  True
																check_hidden_states_output(__lowerCamelCase    ,     __lowerCamelCase    ,     __lowerCamelCase						)

																# check that output_hidden_states also work using config
																del inputs_dict["output_hidden_states"]
																__magic_name__      =  True

																check_hidden_states_output(__lowerCamelCase    ,     __lowerCamelCase    ,     __lowerCamelCase						)







				def      _snake_case       (				self				:    List[str]						)					->       Dict:
										__magic_name__   ,     __magic_name__      =  self.model_tester.prepare_config_and_inputs_for_common()

										def check_equivalence(__lowerCamelCase				:    Dict    ,     __lowerCamelCase				:    Any    ,     __lowerCamelCase				:    Union[str, Any]    ,     __lowerCamelCase				:    Any={}						):
																__magic_name__      =  model(__lowerCamelCase    ,     return_dict=__lowerCamelCase    ,     **__lowerCamelCase						)
																__magic_name__      =  model(__lowerCamelCase    ,     return_dict=__lowerCamelCase    ,     **__lowerCamelCase						).to_tuple()

																def recursive_check(__lowerCamelCase				:    Union[str, Any]    ,     __lowerCamelCase				:    Union[str, Any]						):
																						if isinstance(__lowerCamelCase    ,     (List, Tuple)						):
																												for tuple_iterable_value, dict_iterable_value in zip(__lowerCamelCase    ,     __lowerCamelCase						):
																																		recursive_check(__lowerCamelCase    ,     __lowerCamelCase						)
																						elif tuple_object is None:
																												return
																						else:
																												self.assertTrue(
																												    all(tf.equal(__lowerCamelCase    ,     __lowerCamelCase						)						)    ,     msg=(
																												        "Tuple and dict output are not equal. Difference:"
																												        f''' {tf.math.reduce_max(tf.abs(tuple_object - dict_object						)						)}'''
																												    )    ,     )

																						recursive_check(__lowerCamelCase    ,     __lowerCamelCase						)

										for model_class in self.all_model_classes:
																__magic_name__      =  model_class(__lowerCamelCase						)

																__magic_name__      =  self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase						)
																__magic_name__      =  self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase						)
																check_equivalence(__lowerCamelCase    ,     __lowerCamelCase    ,     __lowerCamelCase						)

																__magic_name__      =  self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase    ,     return_labels=__lowerCamelCase						)
																__magic_name__      =  self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase    ,     return_labels=__lowerCamelCase						)
																check_equivalence(__lowerCamelCase    ,     __lowerCamelCase    ,     __lowerCamelCase						)

																__magic_name__      =  self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase						)
																__magic_name__      =  self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase						)
																check_equivalence(__lowerCamelCase    ,     __lowerCamelCase    ,     __lowerCamelCase    ,     {"output_hidden_states": True}						)

																__magic_name__      =  self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase    ,     return_labels=__lowerCamelCase						)
																__magic_name__      =  self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase    ,     return_labels=__lowerCamelCase						)
																check_equivalence(__lowerCamelCase    ,     __lowerCamelCase    ,     __lowerCamelCase    ,     {"output_hidden_states": True}						)







				def      _snake_case       (				self				:    Union[str, Any]						)					->       Dict:
										__magic_name__      =  self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase						)







				@slow
				def      _snake_case       (				self				:    List[str]						)					->       Tuple:
										__magic_name__      =  TFConvNextModel.from_pretrained("facebook/convnext-tiny-224"						)
										self.assertIsNotNone(__lowerCamelCase						)






def _lowerCAmelCase						(	):

						'''simple docstring'''






						__magic_name__      =  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
						return image


@require_tf
@require_vision
class     A_				(   unittest.TestCase	):







				@cached_property
				def      _snake_case       (				self				:    Tuple						)					->       Any:
										return ConvNextImageProcessor.from_pretrained("facebook/convnext-tiny-224"						) if is_vision_available() else None







				@slow
				def      _snake_case       (				self				:    Any						)					->       str:
										__magic_name__      =  TFConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224"						)

										__magic_name__      =  self.default_image_processor
										__magic_name__      =  prepare_img()
										__magic_name__      =  image_processor(images=__lowerCamelCase    ,     return_tensors="tf"						)

										# forward pass
										__magic_name__      =  model(**__lowerCamelCase						)

										# verify the logits
										__magic_name__      =  tf.TensorShape((1, 1_0_0_0)						)
										self.assertEqual(outputs.logits.shape    ,     __lowerCamelCase						)

										__magic_name__      =  tf.constant([-0.0260, -0.4739, 0.1911]						)

										tf.debugging.assert_near(outputs.logits[0, :3]    ,     __lowerCamelCase    ,     atol=1e-4						)



