



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       :



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



						"""simple docstring"""






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

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



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



						"""simple docstring"""






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

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

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

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

						_UpperCAmelCase									=  self.get_config()

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



			def        _lowerCamelCase			(		self  :    List[str])  ->       Optional[int]:



						"""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  :    str					,						A  :    Dict					,						A  :    str					,						A  :    int					,						A  :    Union[str, Any]					,						A  :    Any					,						A  :    List[Any]					,						A  :    Any					,						A  :    Optional[int]					,						)  ->       List[str]:



						"""simple docstring"""






						_UpperCAmelCase									=  FunnelModel(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase									=  model(A					,						attention_mask=A					,						token_type_ids=A)
						_UpperCAmelCase									=  model(A					,						token_type_ids=A)
						_UpperCAmelCase									=  model(A)
						self.parent.assertEqual(result.last_hidden_state.shape					,						(self.batch_size, self.seq_length, self.d_model))

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

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



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



						"""simple docstring"""






						_UpperCAmelCase									=  FunnelBaseModel(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase									=  model(A					,						attention_mask=A					,						token_type_ids=A)
						_UpperCAmelCase									=  model(A					,						token_type_ids=A)
						_UpperCAmelCase									=  model(A)
						self.parent.assertEqual(result.last_hidden_state.shape					,						(self.batch_size, 2, self.d_model))

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

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



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



						"""simple docstring"""






						_UpperCAmelCase									=  self.num_labels
						_UpperCAmelCase									=  FunnelForPreTraining(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase									=  model(A					,						attention_mask=A					,						token_type_ids=A					,						labels=A)
						self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.seq_length))



			def        _lowerCamelCase			(		self  :    Tuple					,						A  :    Dict					,						A  :    List[str]					,						A  :    Union[str, Any]					,						A  :    Any					,						A  :    Optional[Any]					,						A  :    List[Any]					,						A  :    List[str]					,						A  :    List[Any]					,						)  ->       List[str]:



						"""simple docstring"""






						_UpperCAmelCase									=  FunnelForMaskedLM(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase									=  model(A					,						attention_mask=A					,						token_type_ids=A					,						labels=A)
						self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.seq_length, self.vocab_size))



			def        _lowerCamelCase			(		self  :    int					,						A  :    Any					,						A  :    Union[str, Any]					,						A  :    List[str]					,						A  :    Union[str, Any]					,						A  :    List[str]					,						A  :    Tuple					,						A  :    Any					,						A  :    List[Any]					,						)  ->       Dict:



						"""simple docstring"""






						_UpperCAmelCase									=  self.num_labels
						_UpperCAmelCase									=  FunnelForSequenceClassification(A)
						model.to(A)
						model.eval()
						_UpperCAmelCase									=  model(A					,						attention_mask=A					,						token_type_ids=A					,						labels=A)
						self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.num_labels))



			def        _lowerCamelCase			(		self  :    int					,						A  :    Optional[Any]					,						A  :    Any					,						A  :    Dict					,						A  :    Union[str, Any]					,						A  :    Optional[Any]					,						A  :    Tuple					,						A  :    Optional[Any]					,						A  :    int					,						)  ->       Tuple:



						"""simple docstring"""






						_UpperCAmelCase									=  self.num_choices
						_UpperCAmelCase									=  FunnelForMultipleChoice(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase									=  input_ids.unsqueeze(1).expand(-1					,						self.num_choices					,						-1).contiguous()
						_UpperCAmelCase									=  token_type_ids.unsqueeze(1).expand(-1					,						self.num_choices					,						-1).contiguous()
						_UpperCAmelCase									=  input_mask.unsqueeze(1).expand(-1					,						self.num_choices					,						-1).contiguous()
						_UpperCAmelCase									=  model(
						    A					,						attention_mask=A					,						token_type_ids=A					,						labels=A					,						)
						self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.num_choices))



			def        _lowerCamelCase			(		self  :    List[str]					,						A  :    Any					,						A  :    Any					,						A  :    Any					,						A  :    str					,						A  :    Union[str, Any]					,						A  :    str					,						A  :    str					,						A  :    int					,						)  ->       List[Any]:



						"""simple docstring"""






						_UpperCAmelCase									=  self.num_labels
						_UpperCAmelCase									=  FunnelForTokenClassification(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase									=  model(A					,						attention_mask=A					,						token_type_ids=A					,						labels=A)
						self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.seq_length, self.num_labels))



			def        _lowerCamelCase			(		self  :    Dict					,						A  :    Optional[int]					,						A  :    int					,						A  :    Optional[Any]					,						A  :    List[Any]					,						A  :    Tuple					,						A  :    Optional[int]					,						A  :    Dict					,						A  :    Dict					,						)  ->       int:



						"""simple docstring"""






						_UpperCAmelCase									=  FunnelForQuestionAnswering(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase									=  model(
						    A					,						attention_mask=A					,						token_type_ids=A					,						start_positions=A					,						end_positions=A					,						)
						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  :    Optional[int])  ->       Optional[int]:



						"""simple docstring"""






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







@require_torch
class      __lowerCAmelCase       (     A							,		A							,		unittest.TestCase		):
			UpperCamelCase     =	False
			UpperCamelCase     =	False
			UpperCamelCase     =	(
			    (
			        FunnelModel,
			        FunnelForMaskedLM,
			        FunnelForPreTraining,
			        FunnelForQuestionAnswering,
			        FunnelForTokenClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase     =	(
			    {
			        '''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  :    Dict					,						A  :    Tuple					,						A  :    Optional[Any]					,						A  :    Optional[Any]=False)  ->       List[str]:



						"""simple docstring"""






						_UpperCAmelCase									=  super()._prepare_for_class(A					,						A					,						return_labels=A)

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



			def        _lowerCamelCase			(		self  :    Tuple)  ->       Any:



						"""simple docstring"""






						_UpperCAmelCase									=  FunnelModelTester(self)
						_UpperCAmelCase									=  ConfigTester(self					,						config_class=A)



			def        _lowerCamelCase			(		self  :    Tuple)  ->       Optional[int]:



						"""simple docstring"""






						self.config_tester.run_common_tests()



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



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*A)



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



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_pretraining(*A)



			def        _lowerCamelCase			(		self  :    List[Any])  ->       Optional[Any]:



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*A)



			def        _lowerCamelCase			(		self  :    Dict)  ->       str:



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*A)



			def        _lowerCamelCase			(		self  :    str)  ->       str:



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*A)



			def        _lowerCamelCase			(		self  :    Union[str, Any]					,						A  :    Union[str, Any])  ->       int:



						"""simple docstring"""






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







@require_torch
class      __lowerCAmelCase       (     A							,		unittest.TestCase		):
			UpperCamelCase     =	False
			UpperCamelCase     =	False
			UpperCamelCase     =	(
			    (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
			)



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



						"""simple docstring"""






						_UpperCAmelCase									=  FunnelModelTester(self					,						base=A)
						_UpperCAmelCase									=  ConfigTester(self					,						config_class=A)



			def        _lowerCamelCase			(		self  :    str)  ->       int:



						"""simple docstring"""






						self.config_tester.run_common_tests()



			def        _lowerCamelCase			(		self  :    int)  ->       Tuple:



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_base_model(*A)



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



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*A)



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



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_multiple_choice(*A)



			def        _lowerCamelCase			(		self  :    Tuple)  ->       Optional[Any]:



						"""simple docstring"""






						_UpperCAmelCase			,	_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs_for_common()
						_UpperCAmelCase									=  True

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



			def        _lowerCamelCase			(		self  :    str					,						A  :    Dict)  ->       Optional[int]:



						"""simple docstring"""






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







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



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



						"""simple docstring"""






						_UpperCAmelCase									=  13
						_UpperCAmelCase									=  7
						_UpperCAmelCase									=  torch.arange(0					,						batch_size * sequence_length).long().reshape(A					,						A)
						_UpperCAmelCase									=  [0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
						_UpperCAmelCase									=  torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths])

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

						_UpperCAmelCase									=  torch.tensor(2_3_4_4.8_3_5_2)
						_UpperCAmelCase									=  torch.tensor(0.8_0_5_2)
						self.assertTrue(torch.allclose(output.sum()					,						A					,						atol=1E-4))
						self.assertTrue(torch.allclose(output.mean()					,						A					,						atol=1E-4))

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

						_UpperCAmelCase									=  torch.tensor(2_3_4_3.8_4_2_5)
						_UpperCAmelCase									=  torch.tensor(0.8_0_4_9)
						self.assertTrue(torch.allclose(output.sum()					,						A					,						atol=1E-4))
						self.assertTrue(torch.allclose(output.mean()					,						A					,						atol=1E-4))



			@slow
			def        _lowerCamelCase			(		self  :    Any)  ->       Any:



						"""simple docstring"""






						_UpperCAmelCase									=  FunnelTokenizer.from_pretrained('huggingface/funnel-small')
						_UpperCAmelCase									=  FunnelModel.from_pretrained('huggingface/funnel-small')
						_UpperCAmelCase									=  tokenizer('Hello! I am the Funnel Transformer model.'					,						return_tensors='pt')
						_UpperCAmelCase									=  model(**A)[0]

						_UpperCAmelCase									=  torch.tensor(2_3_5.7_2_4_6)
						_UpperCAmelCase									=  torch.tensor(0.0_2_5_6)
						self.assertTrue(torch.allclose(output.sum()					,						A					,						atol=1E-4))
						self.assertTrue(torch.allclose(output.mean()					,						A					,						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())
