



"""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	:   Optional[Any]    ,						lowerCAmelCase	:   Tuple    ,						lowerCAmelCase	:   Any=13    ,						lowerCAmelCase	:   Union[str, Any]=7    ,						lowerCAmelCase	:   int=True    ,						lowerCAmelCase	:   Union[str, Any]=True    ,						lowerCAmelCase	:   int=True    ,						lowerCAmelCase	:   List[str]=True    ,						lowerCAmelCase	:   Union[str, Any]=99    ,						lowerCAmelCase	:   List[Any]=[1, 1, 2]    ,						lowerCAmelCase	:   Union[str, Any]=1    ,						lowerCAmelCase	:   Dict=32    ,						lowerCAmelCase	:   str=4    ,						lowerCAmelCase	:   Tuple=8    ,						lowerCAmelCase	:   Any=37    ,						lowerCAmelCase	:   List[Any]="gelu_new"    ,						lowerCAmelCase	:   Optional[int]=0.1    ,						lowerCAmelCase	:   Optional[Any]=0.1    ,						lowerCAmelCase	:   Optional[Any]=0.0    ,						lowerCAmelCase	:   Dict=512    ,						lowerCAmelCase	:   Optional[int]=3    ,						lowerCAmelCase	:   str=0.0_2    ,						lowerCAmelCase	:   List[Any]=3    ,						lowerCAmelCase	:   List[str]=4    ,						lowerCAmelCase	:   List[Any]=None    ,						lowerCAmelCase	:   str=False    ,						)       ->   Optional[Any]:


     '''simple docstring'''




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

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



  def 		lowerCamelCase__    (  self	:   Optional[int]						)       ->   Dict:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	List[str]            =ids_tensor([self.batch_size, self.seq_length]    ,						self.vocab_size						)

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

     SCREAMING_SNAKE_CASE_:	Optional[int]            =None
     if self.use_token_type_ids:
        SCREAMING_SNAKE_CASE_:	str            =ids_tensor([self.batch_size, self.seq_length]    ,						self.type_vocab_size						)

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

     SCREAMING_SNAKE_CASE_:	Dict            =self.get_config()

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



  def 		lowerCamelCase__    (  self	:   Dict						)       ->   Any:


     '''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	:   List[str]    ,						lowerCAmelCase	:   Dict    ,						lowerCAmelCase	:   Tuple    ,						lowerCAmelCase	:   List[str]    ,						lowerCAmelCase	:   List[str]    ,						lowerCAmelCase	:   int    ,						lowerCAmelCase	:   Tuple    ,						lowerCAmelCase	:   Optional[int]    ,						lowerCAmelCase	:   Dict    ,						)       ->   Any:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	Union[str, Any]            =FunnelModel(config=lowerCAmelCase						)
     model.to(lowerCAmelCase						)
     model.eval()
     SCREAMING_SNAKE_CASE_:	Optional[int]            =model(lowerCAmelCase    ,						attention_mask=lowerCAmelCase    ,						token_type_ids=lowerCAmelCase						)
     SCREAMING_SNAKE_CASE_:	List[str]            =model(lowerCAmelCase    ,						token_type_ids=lowerCAmelCase						)
     SCREAMING_SNAKE_CASE_:	List[Any]            =model(lowerCAmelCase						)
     self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.d_model)						)

     SCREAMING_SNAKE_CASE_:	Any            =False
     SCREAMING_SNAKE_CASE_:	Optional[Any]            =model(lowerCAmelCase						)
     self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.d_model)						)

     SCREAMING_SNAKE_CASE_:	Union[str, Any]            =False
     SCREAMING_SNAKE_CASE_:	Union[str, Any]            =model(lowerCAmelCase						)
     self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.d_model)						)



  def 		lowerCamelCase__    (  self	:   List[Any]    ,						lowerCAmelCase	:   Any    ,						lowerCAmelCase	:   List[str]    ,						lowerCAmelCase	:   Any    ,						lowerCAmelCase	:   int    ,						lowerCAmelCase	:   List[Any]    ,						lowerCAmelCase	:   str    ,						lowerCAmelCase	:   Any    ,						lowerCAmelCase	:   Dict    ,						)       ->   Union[str, Any]:


     '''simple docstring'''




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

     SCREAMING_SNAKE_CASE_:	Dict            =False
     SCREAMING_SNAKE_CASE_:	Optional[int]            =model(lowerCAmelCase						)
     self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, 3, self.d_model)						)

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



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	int            =self.num_labels
     SCREAMING_SNAKE_CASE_:	List[Any]            =FunnelForPreTraining(config=lowerCAmelCase						)
     model.to(lowerCAmelCase						)
     model.eval()
     SCREAMING_SNAKE_CASE_:	Optional[Any]            =model(lowerCAmelCase    ,						attention_mask=lowerCAmelCase    ,						token_type_ids=lowerCAmelCase    ,						labels=lowerCAmelCase						)
     self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length)						)



  def 		lowerCamelCase__    (  self	:   Any    ,						lowerCAmelCase	:   Dict    ,						lowerCAmelCase	:   Dict    ,						lowerCAmelCase	:   str    ,						lowerCAmelCase	:   Optional[int]    ,						lowerCAmelCase	:   Optional[int]    ,						lowerCAmelCase	:   Tuple    ,						lowerCAmelCase	:   Optional[Any]    ,						lowerCAmelCase	:   List[Any]    ,						)       ->   Optional[int]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	Optional[Any]            =FunnelForMaskedLM(config=lowerCAmelCase						)
     model.to(lowerCAmelCase						)
     model.eval()
     SCREAMING_SNAKE_CASE_:	Optional[int]            =model(lowerCAmelCase    ,						attention_mask=lowerCAmelCase    ,						token_type_ids=lowerCAmelCase    ,						labels=lowerCAmelCase						)
     self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.vocab_size)						)



  def 		lowerCamelCase__    (  self	:   Tuple    ,						lowerCAmelCase	:   Any    ,						lowerCAmelCase	:   str    ,						lowerCAmelCase	:   List[Any]    ,						lowerCAmelCase	:   str    ,						lowerCAmelCase	:   Dict    ,						lowerCAmelCase	:   Optional[int]    ,						lowerCAmelCase	:   Any    ,						lowerCAmelCase	:   Dict    ,						)       ->   List[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	int            =self.num_labels
     SCREAMING_SNAKE_CASE_:	int            =FunnelForSequenceClassification(lowerCAmelCase						)
     model.to(lowerCAmelCase						)
     model.eval()
     SCREAMING_SNAKE_CASE_:	Dict            =model(lowerCAmelCase    ,						attention_mask=lowerCAmelCase    ,						token_type_ids=lowerCAmelCase    ,						labels=lowerCAmelCase						)
     self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_labels)						)



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	Any            =self.num_choices
     SCREAMING_SNAKE_CASE_:	Tuple            =FunnelForMultipleChoice(config=lowerCAmelCase						)
     model.to(lowerCAmelCase						)
     model.eval()
     SCREAMING_SNAKE_CASE_:	str            =input_ids.unsqueeze(1						).expand(-1    ,						self.num_choices    ,						-1						).contiguous()
     SCREAMING_SNAKE_CASE_:	Any            =token_type_ids.unsqueeze(1						).expand(-1    ,						self.num_choices    ,						-1						).contiguous()
     SCREAMING_SNAKE_CASE_:	Dict            =input_mask.unsqueeze(1						).expand(-1    ,						self.num_choices    ,						-1						).contiguous()
     SCREAMING_SNAKE_CASE_:	str            =model(
         lowerCAmelCase    ,						attention_mask=lowerCAmelCase    ,						token_type_ids=lowerCAmelCase    ,						labels=lowerCAmelCase    ,						)
     self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_choices)						)



  def 		lowerCamelCase__    (  self	:   Union[str, Any]    ,						lowerCAmelCase	:   Any    ,						lowerCAmelCase	:   List[Any]    ,						lowerCAmelCase	:   Any    ,						lowerCAmelCase	:   List[Any]    ,						lowerCAmelCase	:   Dict    ,						lowerCAmelCase	:   Dict    ,						lowerCAmelCase	:   Dict    ,						lowerCAmelCase	:   List[str]    ,						)       ->   Tuple:


     '''simple docstring'''




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



  def 		lowerCamelCase__    (  self	:   Any    ,						lowerCAmelCase	:   List[Any]    ,						lowerCAmelCase	:   Dict    ,						lowerCAmelCase	:   List[str]    ,						lowerCAmelCase	:   Any    ,						lowerCAmelCase	:   Optional[Any]    ,						lowerCAmelCase	:   Union[str, Any]    ,						lowerCAmelCase	:   List[Any]    ,						lowerCAmelCase	:   Any    ,						)       ->   str:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	Any            =FunnelForQuestionAnswering(config=lowerCAmelCase						)
     model.to(lowerCAmelCase						)
     model.eval()
     SCREAMING_SNAKE_CASE_:	Optional[Any]            =model(
         lowerCAmelCase    ,						attention_mask=lowerCAmelCase    ,						token_type_ids=lowerCAmelCase    ,						start_positions=lowerCAmelCase    ,						end_positions=lowerCAmelCase    ,						)
     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	:   Tuple						)       ->   str:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	Optional[int]            =self.prepare_config_and_inputs()
     (
         (
         SCREAMING_SNAKE_CASE_
     )     ,						(
         SCREAMING_SNAKE_CASE_
     )     ,						(
         SCREAMING_SNAKE_CASE_
     )     ,						(
         SCREAMING_SNAKE_CASE_
     )     ,						(
         SCREAMING_SNAKE_CASE_
     )     ,						(
         SCREAMING_SNAKE_CASE_
     )     ,						(
         SCREAMING_SNAKE_CASE_
     )     ,						(
         SCREAMING_SNAKE_CASE_
     )     ,						
     ):	List[Any]            =config_and_inputs
     SCREAMING_SNAKE_CASE_:	List[str]            ={"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
     return config, inputs_dict







@require_torch
class    a		(					UpperCAmelCase__		, UpperCAmelCase__		, unittest.TestCase    ):
  UpperCamelCase		:			Any         =   False
  UpperCamelCase		:			str         =   False
  UpperCamelCase		:			Tuple         =   (
      (
          FunnelModel,
          FunnelForMaskedLM,
          FunnelForPreTraining,
          FunnelForQuestionAnswering,
          FunnelForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  UpperCamelCase		:			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 		lowerCamelCase__    (  self	:   Optional[Any]    ,						lowerCAmelCase	:   str    ,						lowerCAmelCase	:   Optional[int]    ,						lowerCAmelCase	:   Union[str, Any]=False						)       ->   Optional[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	Optional[Any]            =super()._prepare_for_class(lowerCAmelCase    ,						lowerCAmelCase    ,						return_labels=lowerCAmelCase						)

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



  def 		lowerCamelCase__    (  self	:   int						)       ->   int:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	str            =FunnelModelTester(self						)
     SCREAMING_SNAKE_CASE_:	int            =ConfigTester(self    ,						config_class=lowerCAmelCase						)



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


     '''simple docstring'''




     self.config_tester.run_common_tests()



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	Optional[int]            =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowerCAmelCase						)



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	Any            =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase						)



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	List[Any]            =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase						)



  def 		lowerCamelCase__    (  self	:   List[Any]						)       ->   Dict:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	Dict            =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase						)



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	Tuple            =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase						)





  def 		lowerCamelCase__    (  self	:   List[str]    ,						lowerCAmelCase	:   List[str]						)       ->   Tuple:


     '''simple docstring'''




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







@require_torch
class    a		(					UpperCAmelCase__		, unittest.TestCase    ):
  UpperCamelCase		:			List[str]         =   False
  UpperCamelCase		:			str         =   False
  UpperCamelCase		:			Tuple         =   (
      (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
  )



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	List[Any]            =FunnelModelTester(self    ,						base=lowerCAmelCase						)
     SCREAMING_SNAKE_CASE_:	Dict            =ConfigTester(self    ,						config_class=lowerCAmelCase						)



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


     '''simple docstring'''




     self.config_tester.run_common_tests()



  def 		lowerCamelCase__    (  self	:   int						)       ->   Any:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	Union[str, Any]            =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_base_model(*lowerCAmelCase						)



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	Optional[int]            =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase						)



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	Any            =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase						)



  def 		lowerCamelCase__    (  self	:   Tuple						)       ->   str:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_:	Optional[Any]            =self.model_tester.prepare_config_and_inputs_for_common()
     SCREAMING_SNAKE_CASE_:	Any            =True

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





  def 		lowerCamelCase__    (  self	:   Optional[int]    ,						lowerCAmelCase	:   str						)       ->   Union[str, Any]:


     '''simple docstring'''




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







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



  def 		lowerCamelCase__    (  self	:   Any						)       ->   int:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	Optional[Any]            =13
     SCREAMING_SNAKE_CASE_:	Any            =7
     SCREAMING_SNAKE_CASE_:	Tuple            =torch.arange(0    ,						batch_size * sequence_length						).long().reshape(lowerCAmelCase    ,						lowerCAmelCase						)
     SCREAMING_SNAKE_CASE_:	Union[str, Any]            =[0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
     SCREAMING_SNAKE_CASE_:	Optional[Any]            =torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]						)

     SCREAMING_SNAKE_CASE_:	str            =FunnelModel.from_pretrained("""sgugger/funnel-random-tiny"""						)
     SCREAMING_SNAKE_CASE_:	str            =model(lowerCAmelCase    ,						token_type_ids=lowerCAmelCase						)[0].abs()

     SCREAMING_SNAKE_CASE_:	Optional[int]            =torch.tensor(2_3_4_4.8_3_5_2						)
     SCREAMING_SNAKE_CASE_:	List[str]            =torch.tensor(0.8_0_5_2						)
     self.assertTrue(torch.allclose(output.sum()    ,						lowerCAmelCase    ,						atol=1E-4						)						)
     self.assertTrue(torch.allclose(output.mean()    ,						lowerCAmelCase    ,						atol=1E-4						)						)

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

     SCREAMING_SNAKE_CASE_:	str            =torch.tensor(2_3_4_3.8_4_2_5						)
     SCREAMING_SNAKE_CASE_:	Dict            =torch.tensor(0.8_0_4_9						)
     self.assertTrue(torch.allclose(output.sum()    ,						lowerCAmelCase    ,						atol=1E-4						)						)
     self.assertTrue(torch.allclose(output.mean()    ,						lowerCAmelCase    ,						atol=1E-4						)						)





  @slow
  def 		lowerCamelCase__    (  self	:   List[Any]						)       ->   Any:


     '''simple docstring'''




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

     SCREAMING_SNAKE_CASE_:	List[str]            =torch.tensor(2_3_5.7_2_4_6						)
     SCREAMING_SNAKE_CASE_:	Tuple            =torch.tensor(0.0_2_5_6						)
     self.assertTrue(torch.allclose(output.sum()    ,						lowerCAmelCase    ,						atol=1E-4						)						)
     self.assertTrue(torch.allclose(output.mean()    ,						lowerCAmelCase    ,						atol=1E-4						)						)








"""simple docstring"""
from collections.abc import Callable
from math import log
from os import name, system
from random import gauss, seed
from typing import TypeVar





def        __magic_name__			(							lowercase							,     lowercase							,     lowercase	):
   seed(1	)
   return [gauss(lowercase							,     lowercase	) for _ in range(lowercase	)]





def        __magic_name__			(							lowercase							,     lowercase	):
   return [k for k in range(lowercase	) for _ in range(instance_count[k]	)]





def        __magic_name__			(							lowercase							,     lowercase	):
   return sum(lowercase	) / instance_count





def        __magic_name__			(							lowercase							,     lowercase	):
   return instance_count / total_count





def        __magic_name__			(							lowercase							,     lowercase							,     lowercase	):
   SCREAMING_SNAKE_CASE_:	Optional[Any]            =[]  # An empty list to store all squared differences
   # iterate over number of elements in items
   for i in range(len(lowercase	)	):
      # for loop iterates over number of elements in inner layer of items
      for j in range(len(items[i]	)	):
         # appending squared differences to 'squared_diff' list
         squared_diff.append((items[i][j] - means[i]) ** 2	)

    # one divided by (the number of all instances - number of classes) multiplied by
    # sum of all squared differences
   SCREAMING_SNAKE_CASE_:	Union[str, Any]            =len(lowercase	)  # Number of classes in dataset
   return 1 / (total_count - n_classes) * sum(lowercase	)





def        __magic_name__			(							lowercase							,     lowercase							,     lowercase							,     lowercase	):
   SCREAMING_SNAKE_CASE_:	Optional[Any]            =[]
   # for loop iterates over number of elements in list
   for i in range(len(lowercase	)	):
      # for loop iterates over number of inner items of each element
      for j in range(len(x_items[i]	)	):
         SCREAMING_SNAKE_CASE_:	Optional[int]            =[]  # to store all discriminant values of each item as a list
         # for loop iterates over number of classes we have in our dataset
         for k in range(len(lowercase	)	):
            # appending values of discriminants for each class to 'temp' list
            temp.append(
                x_items[i][j] * (means[k] / variance)
                - (means[k] ** 2 / (2 * variance))
                + log(probabilities[k]	)	)
         # appending discriminant values of each item to 'results' list
         results.append(lowercase	)

   return [result.index(max(lowercase	)	) for result in results]





def        __magic_name__			(							lowercase							,     lowercase	):
   SCREAMING_SNAKE_CASE_:	Dict            =sum(1 for i, j in zip(lowercase							,     lowercase	) if i == j	)
   # percentage of accuracy equals to number of correct predictions divided by number
   # of all data and multiplied by 100
   return (correct / len(lowercase	)) * 100


_UpperCAmelCase       =     TypeVar("""num""")





def        __magic_name__			(							lowercase							,     lowercase							,     lowercase							,     lowercase = lambda lowercase	: True							,     lowercase = None							,     ):
   while True:
      try:
         SCREAMING_SNAKE_CASE_:	List[Any]            =input_type(input(lowercase	).strip() or default	)
         if condition(lowercase	):
            return user_input
         else:
            print(f'''{user_input}: {err_msg}'''	)
            continue
      except ValueError:
         print(
             f'''{user_input}: Incorrect input type, expected {input_type.__name__!r}'''	)





def        __magic_name__			(							):
   while True:
      print(""" Linear Discriminant Analysis """.center(50							,     """*"""	)	)
      print("""*""" * 50							,     """\n"""	)
      print("""First of all we should specify the number of classes that"""	)
      print("""we want to generate as training dataset"""	)
      # Trying to get number of classes
      SCREAMING_SNAKE_CASE_:	List[Any]            =valid_input(
          input_type=lowercase							,     condition=lambda lowercase	: x > 0							,     input_msg="""Enter the number of classes (Data Groupings): """							,     err_msg="""Number of classes should be positive!"""							,     )

      print("""-""" * 100	)

      # Trying to get the value of standard deviation
      SCREAMING_SNAKE_CASE_:	Union[str, Any]            =valid_input(
          input_type=lowercase							,     condition=lambda lowercase	: x >= 0							,     input_msg=(
              """Enter the value of standard deviation"""
              """(Default value is 1.0 for all classes): """
          )							,     err_msg="""Standard deviation should not be negative!"""							,     default="""1.0"""							,     )

      print("""-""" * 100	)

      # Trying to get number of instances in classes and theirs means to generate
      # dataset
      SCREAMING_SNAKE_CASE_:	Optional[Any]            =[]  # An empty list to store instance counts of classes in dataset
      for i in range(lowercase	):
         SCREAMING_SNAKE_CASE_:	int            =valid_input(
             input_type=lowercase							,     condition=lambda lowercase	: x > 0							,     input_msg=(f'''Enter The number of instances for class_{i+1}: ''')							,     err_msg="""Number of instances should be positive!"""							,     )
         counts.append(lowercase	)
      print("""-""" * 100	)

      # An empty list to store values of user-entered means of classes
      SCREAMING_SNAKE_CASE_:	Optional[Any]            =[]
      for a in range(lowercase	):
         SCREAMING_SNAKE_CASE_:	Tuple            =valid_input(
             input_type=lowercase							,     input_msg=(f'''Enter the value of mean for class_{a+1}: ''')							,     err_msg="""This is an invalid value."""							,     )
         user_means.append(lowercase	)
      print("""-""" * 100	)

      print("""Standard deviation: """							,     lowercase	)
      # print out the number of instances in classes in separated line
      for i, count in enumerate(lowercase							,     1	):
         print(f'''Number of instances in class_{i} is: {count}'''	)
      print("""-""" * 100	)

      # print out mean values of classes separated line
      for i, user_mean in enumerate(lowercase							,     1	):
         print(f'''Mean of class_{i} is: {user_mean}'''	)
      print("""-""" * 100	)

      # Generating training dataset drawn from gaussian distribution
      SCREAMING_SNAKE_CASE_:	Tuple            =[
          gaussian_distribution(user_means[j]							,     lowercase							,     counts[j]	)
          for j in range(lowercase	)
      ]
      print("""Generated Normal Distribution: \n"""							,     lowercase	)
      print("""-""" * 100	)

      # Generating Ys to detecting corresponding classes
      SCREAMING_SNAKE_CASE_:	int            =y_generator(lowercase							,     lowercase	)
      print("""Generated Corresponding Ys: \n"""							,     lowercase	)
      print("""-""" * 100	)

      # Calculating the value of actual mean for each class
      SCREAMING_SNAKE_CASE_:	str            =[calculate_mean(counts[k]							,     x[k]	) for k in range(lowercase	)]
      # for loop iterates over number of elements in 'actual_means' list and print
      # out them in separated line
      for i, actual_mean in enumerate(lowercase							,     1	):
         print(f'''Actual(Real) mean of class_{i} is: {actual_mean}'''	)
      print("""-""" * 100	)

      # Calculating the value of probabilities for each class
      SCREAMING_SNAKE_CASE_:	List[Any]            =[
          calculate_probabilities(counts[i]							,     sum(lowercase	)	) for i in range(lowercase	)
      ]

      # for loop iterates over number of elements in 'probabilities' list and print
      # out them in separated line
      for i, probability in enumerate(lowercase							,     1	):
         print(f'''Probability of class_{i} is: {probability}'''	)
      print("""-""" * 100	)

      # Calculating the values of variance for each class
      SCREAMING_SNAKE_CASE_:	str            =calculate_variance(lowercase							,     lowercase							,     sum(lowercase	)	)
      print("""Variance: """							,     lowercase	)
      print("""-""" * 100	)

      # Predicting Y values
      # storing predicted Y values in 'pre_indexes' variable
      SCREAMING_SNAKE_CASE_:	Dict            =predict_y_values(lowercase							,     lowercase							,     lowercase							,     lowercase	)
      print("""-""" * 100	)

      # Calculating Accuracy of the model
      print(f'''Accuracy: {accuracy(lowercase							,     lowercase	)}'''	)
      print("""-""" * 100	)
      print(""" DONE """.center(100							,     """+"""	)	)

      if input("""Press any key to restart or 'q' for quit: """	).strip().lower() == "q":
         print("""\n""" + """GoodBye!""".center(100							,     """-"""	) + """\n"""	)
         break
      system("""cls""" if name == """nt""" else """clear"""	)  # noqa: S605


if __name__ == "__main__":
   main()



