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






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

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






	def 			_a(self							:   List[Any]						)   -> Optional[int]:
							_lowercase					:  Tuple    			=      ids_tensor([self.batch_size, self.seq_length]			,  self.vocab_size						)

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

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

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

							_lowercase					:  str    			=      self.get_config()

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






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

							_lowercase					:  Tuple    			=      False
							_lowercase					:  Tuple    			=      model(snake_case						)
							self.parent.assertEqual(result.last_hidden_state.shape			,  (self.batch_size, self.seq_length, self.d_model)						)

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






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

							_lowercase					:  int    			=      False
							_lowercase					:  int    			=      model(snake_case						)
							self.parent.assertEqual(result.last_hidden_state.shape			,  (self.batch_size, 3, self.d_model)						)

							_lowercase					:  Tuple    			=      False
							_lowercase					:  Dict    			=      model(snake_case						)
							self.parent.assertEqual(result.last_hidden_state.shape			,  (self.batch_size, 2, self.d_model)						)






	def 			_a(self							:   Union[str, Any]			,  snake_case							:   Tuple			,  snake_case							:   Union[str, Any]			,  snake_case							:   Union[str, Any]			,  snake_case							:   Optional[Any]			,  snake_case							:   List[str]			,  snake_case							:   List[str]			,  snake_case							:   Any			,  snake_case							:   Optional[Any]			,  )   -> str:
							_lowercase					:  str    			=      self.num_labels
							_lowercase					:  Union[str, Any]    			=      FunnelForPreTraining(config=snake_case						)
							model.to(snake_case						)
							model.eval()
							_lowercase					:  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)						)






	def 			_a(self							:   Tuple			,  snake_case							:   Any			,  snake_case							:   Any			,  snake_case							:   Union[str, Any]			,  snake_case							:   Union[str, Any]			,  snake_case							:   Optional[Any]			,  snake_case							:   Optional[int]			,  snake_case							:   List[str]			,  snake_case							:   int			,  )   -> Optional[Any]:
							_lowercase					:  Any    			=      FunnelForMaskedLM(config=snake_case						)
							model.to(snake_case						)
							model.eval()
							_lowercase					:  List[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.vocab_size)						)






	def 			_a(self							:   Any			,  snake_case							:   Optional[Any]			,  snake_case							:   Any			,  snake_case							:   Union[str, Any]			,  snake_case							:   Tuple			,  snake_case							:   Union[str, Any]			,  snake_case							:   Optional[Any]			,  snake_case							:   Optional[int]			,  snake_case							:   Optional[int]			,  )   -> str:
							_lowercase					:  Dict    			=      self.num_labels
							_lowercase					:  Dict    			=      FunnelForSequenceClassification(snake_case						)
							model.to(snake_case						)
							model.eval()
							_lowercase					:  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 			_a(self							:   Optional[Any]			,  snake_case							:   Union[str, Any]			,  snake_case							:   Tuple			,  snake_case							:   List[Any]			,  snake_case							:   List[Any]			,  snake_case							:   int			,  snake_case							:   List[Any]			,  snake_case							:   int			,  snake_case							:   Tuple			,  )   -> Union[str, Any]:
							_lowercase					:  str    			=      self.num_choices
							_lowercase					:  Optional[Any]    			=      FunnelForMultipleChoice(config=snake_case						)
							model.to(snake_case						)
							model.eval()
							_lowercase					:  Dict    			=      input_ids.unsqueeze(1						).expand(-1			,  self.num_choices			,  -1						).contiguous()
							_lowercase					:  Union[str, Any]    			=      token_type_ids.unsqueeze(1						).expand(-1			,  self.num_choices			,  -1						).contiguous()
							_lowercase					:  List[Any]    			=      input_mask.unsqueeze(1						).expand(-1			,  self.num_choices			,  -1						).contiguous()
							_lowercase					:  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_choices)						)






	def 			_a(self							:   Any			,  snake_case							:   List[Any]			,  snake_case							:   int			,  snake_case							:   Union[str, Any]			,  snake_case							:   str			,  snake_case							:   str			,  snake_case							:   List[str]			,  snake_case							:   Optional[Any]			,  snake_case							:   str			,  )   -> Optional[int]:
							_lowercase					:  List[Any]    			=      self.num_labels
							_lowercase					:  List[Any]    			=      FunnelForTokenClassification(config=snake_case						)
							model.to(snake_case						)
							model.eval()
							_lowercase					:  int    			=      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 			_a(self							:   List[str]			,  snake_case							:   int			,  snake_case							:   int			,  snake_case							:   Dict			,  snake_case							:   List[Any]			,  snake_case							:   Tuple			,  snake_case							:   int			,  snake_case							:   Tuple			,  snake_case							:   Optional[Any]			,  )   -> Union[str, Any]:
							_lowercase					:  Optional[Any]    			=      FunnelForQuestionAnswering(config=snake_case						)
							model.to(snake_case						)
							model.eval()
							_lowercase					:  str    			=      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 			_a(self							:   Dict						)   -> Optional[int]:
							_lowercase					:  List[Any]    			=      self.prepare_config_and_inputs()
							(
							    (
							    _lowercase
							)			,						(
							    _lowercase
							)			,						(
							    _lowercase
							)			,						(
							    _lowercase
							)			,						(
							    _lowercase
							)			,						(
							    _lowercase
							)			,						(
							    _lowercase
							)			,						(
							    _lowercase
							)			,						
							)					:  Union[str, Any]    			=      config_and_inputs
							_lowercase					:  int    			=      {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
							return config, inputs_dict




@require_torch
class      __lowercase	(	__snake_case      ,   __snake_case      ,   unittest.TestCase					):
	_A   			=      False
	_A   			=      False
	_A   			=      (
	    (
	        FunnelModel,
	        FunnelForMaskedLM,
	        FunnelForPreTraining,
	        FunnelForQuestionAnswering,
	        FunnelForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	_A   			=      (
	    {
	        "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							:   Any			,  snake_case							:   Dict			,  snake_case							:   Dict			,  snake_case							:   Union[str, Any]=False						)   -> List[Any]:
							_lowercase					:  Union[str, Any]    			=      super()._prepare_for_class(snake_case			,  snake_case			,  return_labels=snake_case						)

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






	def 			_a(self							:   Dict						)   -> Optional[Any]:
							_lowercase					:  str    			=      FunnelModelTester(self						)
							_lowercase					:  Optional[Any]    			=      ConfigTester(self			,  config_class=snake_case						)






	def 			_a(self							:   str						)   -> Optional[Any]:
							self.config_tester.run_common_tests()






	def 			_a(self							:   Any						)   -> Optional[Any]:
							_lowercase					:  Any    			=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*snake_case						)






	def 			_a(self							:   int						)   -> Dict:
							_lowercase					:  Union[str, Any]    			=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*snake_case						)






	def 			_a(self							:   Dict						)   -> Optional[Any]:
							_lowercase					:  Tuple    			=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*snake_case						)






	def 			_a(self							:   Dict						)   -> Dict:
							_lowercase					:  Union[str, Any]    			=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*snake_case						)






	def 			_a(self							:   Optional[Any]						)   -> Optional[int]:
							_lowercase					:  Dict    			=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*snake_case						)






	def 			_a(self							:   Union[str, Any]			,  snake_case							:   Dict						)   -> int:
							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:
																			_lowercase					:  Tuple    			=      getattr(snake_case			,  snake_case						)
																			weight.data.fill_(3						)




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






	def 			_a(self							:   int						)   -> Any:
							_lowercase					:  Any    			=      FunnelModelTester(self			,  base=snake_case						)
							_lowercase					:  Union[str, Any]    			=      ConfigTester(self			,  config_class=snake_case						)






	def 			_a(self							:   Any						)   -> str:
							self.config_tester.run_common_tests()






	def 			_a(self							:   Union[str, Any]						)   -> int:
							_lowercase					:  Any    			=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_base_model(*snake_case						)






	def 			_a(self							:   Optional[int]						)   -> Any:
							_lowercase					:  List[Any]    			=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*snake_case						)






	def 			_a(self							:   Optional[Any]						)   -> List[Any]:
							_lowercase					:  List[str]    			=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*snake_case						)






	def 			_a(self							:   Tuple						)   -> Dict:
							_lowercase			,						_lowercase					:  Union[str, Any]    			=      self.model_tester.prepare_config_and_inputs_for_common()
							_lowercase					:  Dict    			=      True

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






	def 			_a(self							:   str			,  snake_case							:   int						)   -> List[Any]:
							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:
																			_lowercase					:  Dict    			=      getattr(snake_case			,  snake_case						)
																			weight.data.fill_(3						)




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






	def 			_a(self							:   Union[str, Any]						)   -> Any:
							_lowercase					:  Dict    			=      13
							_lowercase					:  Optional[int]    			=      7
							_lowercase					:  Union[str, Any]    			=      torch.arange(0			,  batch_size * sequence_length						).long().reshape(snake_case			,  snake_case						)
							_lowercase					:  str    			=      [0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
							_lowercase					:  str    			=      torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]						)

							_lowercase					:  Tuple    			=      FunnelModel.from_pretrained("sgugger/funnel-random-tiny"						)
							_lowercase					:  Tuple    			=      model(snake_case			,  token_type_ids=snake_case						)[0].abs()

							_lowercase					:  int    			=      torch.tensor(23_44.83_52						)
							_lowercase					:  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						)						)

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

							_lowercase					:  List[Any]    			=      torch.tensor(23_43.84_25						)
							_lowercase					:  List[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 			_a(self							:   List[str]						)   -> Dict:
							_lowercase					:  Tuple    			=      FunnelTokenizer.from_pretrained("huggingface/funnel-small"						)
							_lowercase					:  Any    			=      FunnelModel.from_pretrained("huggingface/funnel-small"						)
							_lowercase					:  int    			=      tokenizer("Hello! I am the Funnel Transformer model."			,  return_tensors="pt"						)
							_lowercase					:  Optional[int]    			=      model(**snake_case						)[0]

							_lowercase					:  Union[str, Any]    			=      torch.tensor(2_35.72_46						)
							_lowercase					:  Tuple    			=      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						)						)






import os



def 		UpperCamelCase    (					)      -> Optional[int]:






						'''simple docstring'''

						_lowercase					:  Tuple    			=      0
						_lowercase					:  List[Any]    			=      0
						with open(os.path.dirname(_UpperCAmelCase     ) + "/p022_names.txt"     ) as file:
												_lowercase					:  Any    			=      str(file.readlines()[0]     )
												_lowercase					:  int    			=      name.replace("\""   , ""     ).split(","     )

						name.sort()
						for i in range(len(_UpperCAmelCase     )     ):
												for j in name[i]:
																		temp_sum += ord(_UpperCAmelCase     ) - ord("A"     ) + 1
												total_sum += (i + 1) * temp_sum
												_lowercase					:  Tuple    			=      0
						return total_sum


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




