





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







		def __init__(     self			,    __snake_case			,    __snake_case=1_3			,    __snake_case=7			,    __snake_case=True			,    __snake_case=True			,    __snake_case=True			,    __snake_case=True			,    __snake_case=9_9			,    __snake_case=[1, 1, 2]			,    __snake_case=1			,    __snake_case=3_2			,    __snake_case=4			,    __snake_case=8			,    __snake_case=3_7			,    __snake_case="gelu_new"			,    __snake_case=0.1			,    __snake_case=0.1			,    __snake_case=0.0			,    __snake_case=5_1_2			,    __snake_case=3			,    __snake_case=0.02			,    __snake_case=3			,    __snake_case=4			,    __snake_case=None			,    __snake_case=False			,    )   ->   Optional[Any]:


							'''simple docstring'''

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

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







		def 							lowercase							(     self					)   ->   Optional[Any]:


							'''simple docstring'''

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

							_snake_case							:       Optional[Any]												= None
							if self.use_input_mask:
												_snake_case							:       int												= ids_tensor([self.batch_size, self.seq_length]			,    vocab_size=2					)

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

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

							_snake_case							:       str												= self.get_config()

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







		def 							lowercase							(     self					)   ->   Tuple:


							'''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 							lowercase							(     self			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    )   ->   str:


							'''simple docstring'''

							_snake_case							:       List[str]												= FunnelModel(config=__snake_case					)
							model.to(__snake_case					)
							model.eval()
							_snake_case							:       str												= model(__snake_case			,    attention_mask=__snake_case			,    token_type_ids=__snake_case					)
							_snake_case							:       List[str]												= model(__snake_case			,    token_type_ids=__snake_case					)
							_snake_case							:       Tuple												= model(__snake_case					)
							self.parent.assertEqual(result.last_hidden_state.shape			,    (self.batch_size, self.seq_length, self.d_model)					)

							_snake_case							:       Dict												= False
							_snake_case							:       int												= model(__snake_case					)
							self.parent.assertEqual(result.last_hidden_state.shape			,    (self.batch_size, self.seq_length, self.d_model)					)

							_snake_case							:       Optional[Any]												= False
							_snake_case							:       List[str]												= model(__snake_case					)
							self.parent.assertEqual(result.last_hidden_state.shape			,    (self.batch_size, self.seq_length, self.d_model)					)







		def 							lowercase							(     self			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    )   ->   List[str]:


							'''simple docstring'''

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

							_snake_case							:       Dict												= False
							_snake_case							:       List[str]												= model(__snake_case					)
							self.parent.assertEqual(result.last_hidden_state.shape			,    (self.batch_size, 3, self.d_model)					)

							_snake_case							:       Optional[Any]												= False
							_snake_case							:       Any												= model(__snake_case					)
							self.parent.assertEqual(result.last_hidden_state.shape			,    (self.batch_size, 2, self.d_model)					)







		def 							lowercase							(     self			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    )   ->   Optional[Any]:


							'''simple docstring'''

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







		def 							lowercase							(     self			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    )   ->   Dict:


							'''simple docstring'''

							_snake_case							:       Dict												= FunnelForMaskedLM(config=__snake_case					)
							model.to(__snake_case					)
							model.eval()
							_snake_case							:       Dict												= model(__snake_case			,    attention_mask=__snake_case			,    token_type_ids=__snake_case			,    labels=__snake_case					)
							self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.seq_length, self.vocab_size)					)







		def 							lowercase							(     self			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    )   ->   Tuple:


							'''simple docstring'''

							_snake_case							:       Dict												= self.num_labels
							_snake_case							:       Tuple												= FunnelForSequenceClassification(__snake_case					)
							model.to(__snake_case					)
							model.eval()
							_snake_case							:       Any												= model(__snake_case			,    attention_mask=__snake_case			,    token_type_ids=__snake_case			,    labels=__snake_case					)
							self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.num_labels)					)







		def 							lowercase							(     self			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    )   ->   Tuple:


							'''simple docstring'''

							_snake_case							:       Optional[int]												= self.num_choices
							_snake_case							:       Any												= FunnelForMultipleChoice(config=__snake_case					)
							model.to(__snake_case					)
							model.eval()
							_snake_case							:       Union[str, Any]												= input_ids.unsqueeze(1					).expand(-1			,    self.num_choices			,    -1					).contiguous()
							_snake_case							:       List[str]												= token_type_ids.unsqueeze(1					).expand(-1			,    self.num_choices			,    -1					).contiguous()
							_snake_case							:       Any												= input_mask.unsqueeze(1					).expand(-1			,    self.num_choices			,    -1					).contiguous()
							_snake_case							:       str												= model(
							    __snake_case			,    attention_mask=__snake_case			,    token_type_ids=__snake_case			,    labels=__snake_case			,    )
							self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.num_choices)					)







		def 							lowercase							(     self			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    )   ->   List[str]:


							'''simple docstring'''

							_snake_case							:       Any												= self.num_labels
							_snake_case							:       List[Any]												= FunnelForTokenClassification(config=__snake_case					)
							model.to(__snake_case					)
							model.eval()
							_snake_case							:       Union[str, Any]												= model(__snake_case			,    attention_mask=__snake_case			,    token_type_ids=__snake_case			,    labels=__snake_case					)
							self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.seq_length, self.num_labels)					)







		def 							lowercase							(     self			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    __snake_case			,    )   ->   Optional[int]:


							'''simple docstring'''

							_snake_case							:       Any												= FunnelForQuestionAnswering(config=__snake_case					)
							model.to(__snake_case					)
							model.eval()
							_snake_case							:       Optional[int]												= model(
							    __snake_case			,    attention_mask=__snake_case			,    token_type_ids=__snake_case			,    start_positions=__snake_case			,    end_positions=__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 							lowercase							(     self					)   ->   Tuple:


							'''simple docstring'''

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






@require_torch
class 							_a(     __A						, __A						, unittest.TestCase     ):
		lowerCamelCase__  :Dict 					=		False
		lowerCamelCase__  :str 					=		False
		lowerCamelCase__  :Any 					=		(
		    (
		        FunnelModel,
		        FunnelForMaskedLM,
		        FunnelForPreTraining,
		        FunnelForQuestionAnswering,
		        FunnelForTokenClassification,
		    )
		    if is_torch_available()
		    else ()
		)
		lowerCamelCase__  :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 							lowercase							(     self			,    __snake_case			,    __snake_case			,    __snake_case=False					)   ->   Union[str, Any]:


							'''simple docstring'''

							_snake_case							:       int												= super()._prepare_for_class(__snake_case			,    __snake_case			,    return_labels=__snake_case					)

							if return_labels:
												if model_class in get_values(__snake_case					):
																	_snake_case							:       Any												= torch.zeros(
																	    (self.model_tester.batch_size, self.model_tester.seq_length)			,    dtype=torch.long			,    device=__snake_case					)
							return inputs_dict







		def 							lowercase							(     self					)   ->   Tuple:


							'''simple docstring'''

							_snake_case							:       List[Any]												= FunnelModelTester(self					)
							_snake_case							:       str												= ConfigTester(self			,    config_class=__snake_case					)







		def 							lowercase							(     self					)   ->   int:


							'''simple docstring'''

							self.config_tester.run_common_tests()







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


							'''simple docstring'''

							_snake_case							:       Union[str, Any]												= self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__snake_case					)







		def 							lowercase							(     self					)   ->   Tuple:


							'''simple docstring'''

							_snake_case							:       Optional[int]												= self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*__snake_case					)







		def 							lowercase							(     self					)   ->   Any:


							'''simple docstring'''

							_snake_case							:       Any												= self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*__snake_case					)







		def 							lowercase							(     self					)   ->   Optional[Any]:


							'''simple docstring'''

							_snake_case							:       Dict												= self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*__snake_case					)







		def 							lowercase							(     self					)   ->   Optional[int]:


							'''simple docstring'''

							_snake_case							:       Tuple												= self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*__snake_case					)







		def 							lowercase							(     self			,    __snake_case					)   ->   str:


							'''simple docstring'''

							if hasattr(__snake_case			,    "weight"					) and module.weight is not None:
												module.weight.data.fill_(3					)
							if hasattr(__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(__snake_case			,    __snake_case					) and getattr(__snake_case			,    __snake_case					) is not None:
																	_snake_case							:       Optional[Any]												= getattr(__snake_case			,    __snake_case					)
																	weight.data.fill_(3					)






@require_torch
class 							_a(     __A						, unittest.TestCase     ):
		lowerCamelCase__  :Optional[Any] 					=		False
		lowerCamelCase__  :List[str] 					=		False
		lowerCamelCase__  :Optional[int] 					=		(
		    (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
		)







		def 							lowercase							(     self					)   ->   Optional[int]:


							'''simple docstring'''

							_snake_case							:       Dict												= FunnelModelTester(self			,    base=__snake_case					)
							_snake_case							:       Dict												= ConfigTester(self			,    config_class=__snake_case					)







		def 							lowercase							(     self					)   ->   int:


							'''simple docstring'''

							self.config_tester.run_common_tests()







		def 							lowercase							(     self					)   ->   int:


							'''simple docstring'''

							_snake_case							:       int												= self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_base_model(*__snake_case					)







		def 							lowercase							(     self					)   ->   Dict:


							'''simple docstring'''

							_snake_case							:       List[Any]												= self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*__snake_case					)







		def 							lowercase							(     self					)   ->   int:


							'''simple docstring'''

							_snake_case							:       Optional[Any]												= self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*__snake_case					)







		def 							lowercase							(     self					)   ->   List[Any]:


							'''simple docstring'''

							_snake_case							,			_snake_case							:       Optional[int]												= self.model_tester.prepare_config_and_inputs_for_common()
							_snake_case							:       int												= True

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







		def 							lowercase							(     self			,    __snake_case					)   ->   Optional[Any]:


							'''simple docstring'''

							if hasattr(__snake_case			,    "weight"					) and module.weight is not None:
												module.weight.data.fill_(3					)
							if hasattr(__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(__snake_case			,    __snake_case					) and getattr(__snake_case			,    __snake_case					) is not None:
																	_snake_case							:       List[str]												= getattr(__snake_case			,    __snake_case					)
																	weight.data.fill_(3					)






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







		def 							lowercase							(     self					)   ->   int:


							'''simple docstring'''

							_snake_case							:       int												= 1_3
							_snake_case							:       Dict												= 7
							_snake_case							:       int												= torch.arange(0			,    batch_size * sequence_length					).long().reshape(__snake_case			,    __snake_case					)
							_snake_case							:       Union[str, Any]												= [0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
							_snake_case							:       List[str]												= torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]					)

							_snake_case							:       Optional[int]												= FunnelModel.from_pretrained("sgugger/funnel-random-tiny"					)
							_snake_case							:       int												= model(__snake_case			,    token_type_ids=__snake_case					)[0].abs()

							_snake_case							:       str												= torch.tensor(23_44.83_52					)
							_snake_case							:       List[Any]												= torch.tensor(0.80_52					)
							self.assertTrue(torch.allclose(output.sum()			,    __snake_case			,    atol=1E-4					)					)
							self.assertTrue(torch.allclose(output.mean()			,    __snake_case			,    atol=1E-4					)					)

							_snake_case							:       Any												= torch.tensor([[1] * 7, [1] * 4 + [0] * 3] * 6 + [[0, 1, 1, 0, 0, 1, 1]]					)
							_snake_case							:       Optional[int]												= model(__snake_case			,    attention_mask=__snake_case			,    token_type_ids=__snake_case					)[0].abs()

							_snake_case							:       Optional[int]												= torch.tensor(23_43.84_25					)
							_snake_case							:       Optional[Any]												= torch.tensor(0.80_49					)
							self.assertTrue(torch.allclose(output.sum()			,    __snake_case			,    atol=1E-4					)					)
							self.assertTrue(torch.allclose(output.mean()			,    __snake_case			,    atol=1E-4					)					)







		@slow
		def 							lowercase							(     self					)   ->   int:


							'''simple docstring'''

							_snake_case							:       Dict												= FunnelTokenizer.from_pretrained("huggingface/funnel-small"					)
							_snake_case							:       Tuple												= FunnelModel.from_pretrained("huggingface/funnel-small"					)
							_snake_case							:       Dict												= tokenizer("Hello! I am the Funnel Transformer model."			,    return_tensors="pt"					)
							_snake_case							:       Optional[Any]												= model(**__snake_case					)[0]

							_snake_case							:       Any												= torch.tensor(2_35.72_46					)
							_snake_case							:       Any												= torch.tensor(0.02_56					)
							self.assertTrue(torch.allclose(output.sum()			,    __snake_case			,    atol=1E-4					)					)
							self.assertTrue(torch.allclose(output.mean()			,    __snake_case			,    atol=1E-4					)					)






from __future__ import annotations

from collections.abc import Generator







def  A					(				UpperCAmelCase					):
					if not isinstance(UpperCAmelCase	,	UpperCAmelCase					) or n < 1:
										raise Exception("Sequence only defined for positive integers"					)

					yield n
					while n != 1:
										if n % 2 == 0:
															n //= 2
										else:
															_snake_case							:       Any												= 3 * n + 1
										yield n







def  A					(				):
					_snake_case							:       Dict												= 43
					_snake_case							:       List[str]												= tuple(collatz_sequence(UpperCAmelCase					)					)
					print(UpperCAmelCase					)
					print(F"""Collatz sequence from {n} took {len(UpperCAmelCase					)} steps."""					)


if __name__ == "__main__":
							main()