




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




	def __init__( self	:						int   ,     lowercase	:						Union[str, Any]   ,     lowercase	:						Optional[Any]=13   ,     lowercase	:						int=7   ,     lowercase	:						Dict=True   ,     lowercase	:						Tuple=True   ,     lowercase	:						str=True   ,     lowercase	:						Union[str, Any]=True   ,     lowercase	:						Any=99   ,     lowercase	:						Union[str, Any]=[1, 1, 2]   ,     lowercase	:						Optional[int]=1   ,     lowercase	:						Dict=32   ,     lowercase	:						Dict=4   ,     lowercase	:						int=8   ,     lowercase	:						List[Any]=37   ,     lowercase	:						Optional[int]="gelu_new"   ,     lowercase	:						Any=0.1   ,     lowercase	:						str=0.1   ,     lowercase	:						Optional[int]=0.0   ,     lowercase	:						Any=512   ,     lowercase	:						List[str]=3   ,     lowercase	:						Dict=0.02   ,     lowercase	:						List[Any]=3   ,     lowercase	:						Union[str, Any]=4   ,     lowercase	:						str=None   ,     lowercase	:						Any=False   ,     )       ->			List[Any]:
						__snake_case        =						parent
						__snake_case        =						batch_size
						__snake_case        =						seq_length
						__snake_case        =						is_training
						__snake_case        =						use_input_mask
						__snake_case        =						use_token_type_ids
						__snake_case        =						use_labels
						__snake_case        =						vocab_size
						__snake_case        =						block_sizes
						__snake_case        =						num_decoder_layers
						__snake_case        =						d_model
						__snake_case        =						n_head
						__snake_case        =						d_head
						__snake_case        =						d_inner
						__snake_case        =						hidden_act
						__snake_case        =						hidden_dropout
						__snake_case        =						attention_dropout
						__snake_case        =						activation_dropout
						__snake_case        =						max_position_embeddings
						__snake_case        =						type_vocab_size
						__snake_case        =						2
						__snake_case        =						num_labels
						__snake_case        =						num_choices
						__snake_case        =						scope
						__snake_case        =						initializer_std

						# Used in the tests to check the size of the first attention layer
						__snake_case        =						n_head
						# Used in the tests to check the size of the first hidden state
						__snake_case        =						self.d_model
						# Used in the tests to check the number of output hidden states/attentions
						__snake_case        =						sum(self.block_sizes						) + (0 if base else self.num_decoder_layers)
						# FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
						# the last hidden state of the first block (which is the first hidden state of the decoder).
						if not base:
											__snake_case        =						self.num_hidden_layers + 2




	def 				a_		( self	:						Union[str, Any]						)       ->			List[Any]:
						__snake_case        =						ids_tensor([self.batch_size, self.seq_length]   ,     self.vocab_size						)

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

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

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

						__snake_case        =						self.get_config()

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




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

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

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




	def 				a_		( self	:						Union[str, Any]   ,     lowercase	:						str   ,     lowercase	:						str   ,     lowercase	:						Optional[int]   ,     lowercase	:						Dict   ,     lowercase	:						str   ,     lowercase	:						Tuple   ,     lowercase	:						Any   ,     lowercase	:						Tuple   ,     )       ->			List[str]:
						__snake_case        =						FunnelBaseModel(config=lowercase						)
						model.to(lowercase						)
						model.eval()
						__snake_case        =						model(lowercase   ,     attention_mask=lowercase   ,     token_type_ids=lowercase						)
						__snake_case        =						model(lowercase   ,     token_type_ids=lowercase						)
						__snake_case        =						model(lowercase						)
						self.parent.assertEqual(result.last_hidden_state.shape   ,     (self.batch_size, 2, self.d_model)						)

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

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




	def 				a_		( self	:						Any   ,     lowercase	:						List[str]   ,     lowercase	:						Any   ,     lowercase	:						Optional[Any]   ,     lowercase	:						List[str]   ,     lowercase	:						Dict   ,     lowercase	:						int   ,     lowercase	:						List[Any]   ,     lowercase	:						str   ,     )       ->			Union[str, Any]:
						__snake_case        =						self.num_labels
						__snake_case        =						FunnelForPreTraining(config=lowercase						)
						model.to(lowercase						)
						model.eval()
						__snake_case        =						model(lowercase   ,     attention_mask=lowercase   ,     token_type_ids=lowercase   ,     labels=lowercase						)
						self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length)						)




	def 				a_		( self	:						Tuple   ,     lowercase	:						Dict   ,     lowercase	:						List[str]   ,     lowercase	:						Union[str, Any]   ,     lowercase	:						Any   ,     lowercase	:						Optional[Any]   ,     lowercase	:						List[Any]   ,     lowercase	:						List[str]   ,     lowercase	:						List[Any]   ,     )       ->			List[str]:
						__snake_case        =						FunnelForMaskedLM(config=lowercase						)
						model.to(lowercase						)
						model.eval()
						__snake_case        =						model(lowercase   ,     attention_mask=lowercase   ,     token_type_ids=lowercase   ,     labels=lowercase						)
						self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length, self.vocab_size)						)




	def 				a_		( self	:						int   ,     lowercase	:						Any   ,     lowercase	:						Union[str, Any]   ,     lowercase	:						List[str]   ,     lowercase	:						Union[str, Any]   ,     lowercase	:						List[str]   ,     lowercase	:						Tuple   ,     lowercase	:						Any   ,     lowercase	:						List[Any]   ,     )       ->			Dict:
						__snake_case        =						self.num_labels
						__snake_case        =						FunnelForSequenceClassification(lowercase						)
						model.to(lowercase						)
						model.eval()
						__snake_case        =						model(lowercase   ,     attention_mask=lowercase   ,     token_type_ids=lowercase   ,     labels=lowercase						)
						self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.num_labels)						)




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




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




	def 				a_		( self	:						Dict   ,     lowercase	:						Optional[int]   ,     lowercase	:						int   ,     lowercase	:						Optional[Any]   ,     lowercase	:						List[Any]   ,     lowercase	:						Tuple   ,     lowercase	:						Optional[int]   ,     lowercase	:						Dict   ,     lowercase	:						Dict   ,     )       ->			int:
						__snake_case        =						FunnelForQuestionAnswering(config=lowercase						)
						model.to(lowercase						)
						model.eval()
						__snake_case        =						model(
						    lowercase   ,     attention_mask=lowercase   ,     token_type_ids=lowercase   ,     start_positions=lowercase   ,     end_positions=lowercase   ,     )
						self.parent.assertEqual(result.start_logits.shape   ,     (self.batch_size, self.seq_length)						)
						self.parent.assertEqual(result.end_logits.shape   ,     (self.batch_size, self.seq_length)						)




	def 				a_		( self	:						Optional[int]						)       ->			Optional[int]:
						__snake_case        =						self.prepare_config_and_inputs()
						(
						    (
						    __snake_case	
						)   ,   (
						    __snake_case	
						)   ,   (
						    __snake_case	
						)   ,   (
						    __snake_case	
						)   ,   (
						    __snake_case	
						)   ,   (
						    __snake_case	
						)   ,   (
						    __snake_case	
						)   ,   (
						    __snake_case	
						)   ,   	
						)        =						config_and_inputs
						__snake_case        =						{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
						return config, inputs_dict

@require_torch
class      A__   (   lowercase_  ,						lowercase_  ,						unittest.TestCase  ):
	A_				:					List[Any]      =   False
	A_				:					int      =   False
	A_				:					Any      =   (
	    (
	        FunnelModel,
	        FunnelForMaskedLM,
	        FunnelForPreTraining,
	        FunnelForQuestionAnswering,
	        FunnelForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	A_				:					Union[str, Any]      =   (
	    {
	        """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 				a_		( self	:						Dict   ,     lowercase	:						Tuple   ,     lowercase	:						Optional[Any]   ,     lowercase	:						Optional[Any]=False						)       ->			List[str]:
						__snake_case        =						super()._prepare_for_class(lowercase   ,     lowercase   ,     return_labels=lowercase						)

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




	def 				a_		( self	:						Tuple						)       ->			Any:
						__snake_case        =						FunnelModelTester(self						)
						__snake_case        =						ConfigTester(self   ,     config_class=lowercase						)




	def 				a_		( self	:						Tuple						)       ->			Optional[int]:
						self.config_tester.run_common_tests()




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




	def 				a_		( self	:						int						)       ->			Union[str, Any]:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_pretraining(*lowercase						)




	def 				a_		( self	:						List[Any]						)       ->			Optional[Any]:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*lowercase						)




	def 				a_		( self	:						Dict						)       ->			str:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*lowercase						)




	def 				a_		( self	:						str						)       ->			str:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*lowercase						)




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






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

@require_torch
class      A__   (   lowercase_  ,						unittest.TestCase  ):
	A_				:					str      =   False
	A_				:					int      =   False
	A_				:					str      =   (
	    (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
	)




	def 				a_		( self	:						Optional[Any]						)       ->			str:
						__snake_case        =						FunnelModelTester(self   ,     base=lowercase						)
						__snake_case        =						ConfigTester(self   ,     config_class=lowercase						)




	def 				a_		( self	:						str						)       ->			int:
						self.config_tester.run_common_tests()




	def 				a_		( self	:						int						)       ->			Tuple:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_base_model(*lowercase						)




	def 				a_		( self	:						Optional[Any]						)       ->			int:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*lowercase						)




	def 				a_		( self	:						str						)       ->			List[Any]:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_multiple_choice(*lowercase						)




	def 				a_		( self	:						Tuple						)       ->			Optional[Any]:
						__snake_case   ,   __snake_case        =						self.model_tester.prepare_config_and_inputs_for_common()
						__snake_case        =						True

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




	def 				a_		( self	:						str   ,     lowercase	:						Dict						)       ->			Optional[int]:






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

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




	def 				a_		( self	:						Union[str, Any]						)       ->			List[Any]:
						__snake_case        =						13
						__snake_case        =						7
						__snake_case        =						torch.arange(0   ,     batch_size * sequence_length						).long().reshape(lowercase   ,     lowercase						)
						__snake_case        =						[0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
						__snake_case        =						torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]						)

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

						__snake_case        =						torch.tensor(2_344.8_352						)
						__snake_case        =						torch.tensor(0.80_52						)
						self.assertTrue(torch.allclose(output.sum()   ,     lowercase   ,     atol=1e-4						)						)
						self.assertTrue(torch.allclose(output.mean()   ,     lowercase   ,     atol=1e-4						)						)

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

						__snake_case        =						torch.tensor(2_343.8_425						)
						__snake_case        =						torch.tensor(0.80_49						)
						self.assertTrue(torch.allclose(output.sum()   ,     lowercase   ,     atol=1e-4						)						)
						self.assertTrue(torch.allclose(output.mean()   ,     lowercase   ,     atol=1e-4						)						)




	@slow
	def 				a_		( self	:						Any						)       ->			Any:
						__snake_case        =						FunnelTokenizer.from_pretrained("""huggingface/funnel-small"""						)
						__snake_case        =						FunnelModel.from_pretrained("""huggingface/funnel-small"""						)
						__snake_case        =						tokenizer("""Hello! I am the Funnel Transformer model."""   ,     return_tensors="""pt"""						)
						__snake_case        =						model(**lowercase						)[0]

						__snake_case        =						torch.tensor(235.7_246						)
						__snake_case        =						torch.tensor(0.02_56						)
						self.assertTrue(torch.allclose(output.sum()   ,     lowercase   ,     atol=1e-4						)						)
						self.assertTrue(torch.allclose(output.mean()   ,     lowercase   ,     atol=1e-4						)						)







import math





def 				A      (	_UpperCAmelCase							:  int       )							->					int:







			'''simple docstring'''
			return sum(int(_UpperCAmelCase       ) for digit in str(_UpperCAmelCase       )       )





def 				A      (	_UpperCAmelCase							:  int = 30       )							->					int:







			'''simple docstring'''
			_UpperCAmelCase									=  []
			for digit in range(2				,  100       ):
						for power in range(2				,  100       ):
									_UpperCAmelCase									=  int(math.pow(_UpperCAmelCase				,  _UpperCAmelCase       )       )
									if digit == digit_sum(_UpperCAmelCase       ):
												digit_to_powers.append(_UpperCAmelCase       )

			digit_to_powers.sort()
			return digit_to_powers[n - 1]


if __name__ == "__main__":
							print(solution())
