

'''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 		lowerCAmelCase__     :



 """simple docstring"""



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


      """simple docstring"""




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

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


      """simple docstring"""




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

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

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

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

      __SCREAMING_SNAKE_CASE						     =   self.get_config()

      return (
          config,
          input_ids,
          token_type_ids,
          input_mask,
          sequence_labels,
          token_labels,
          choice_labels,
          fake_token_labels,
      )
 def    UpperCAmelCase__	(	self      : List[Any]    )						->				List[str]:


      """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    UpperCAmelCase__	(	self      : Union[str, Any]      ,							__SCREAMING_SNAKE_CASE      : Union[str, Any]      ,							__SCREAMING_SNAKE_CASE      : Optional[Any]      ,							__SCREAMING_SNAKE_CASE      : int      ,							__SCREAMING_SNAKE_CASE      : str      ,							__SCREAMING_SNAKE_CASE      : Union[str, Any]      ,							__SCREAMING_SNAKE_CASE      : Dict      ,							__SCREAMING_SNAKE_CASE      : Tuple      ,							__SCREAMING_SNAKE_CASE      : Optional[Any]      ,							)						->				Union[str, Any]:


      """simple docstring"""




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

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

      __SCREAMING_SNAKE_CASE						     =   False
      __SCREAMING_SNAKE_CASE						     =   model(__SCREAMING_SNAKE_CASE    )
      self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, self.seq_length, self.d_model)    )
 def    UpperCAmelCase__	(	self      : List[str]      ,							__SCREAMING_SNAKE_CASE      : int      ,							__SCREAMING_SNAKE_CASE      : Dict      ,							__SCREAMING_SNAKE_CASE      : Optional[Any]      ,							__SCREAMING_SNAKE_CASE      : Optional[Any]      ,							__SCREAMING_SNAKE_CASE      : int      ,							__SCREAMING_SNAKE_CASE      : Optional[Any]      ,							__SCREAMING_SNAKE_CASE      : str      ,							__SCREAMING_SNAKE_CASE      : Tuple      ,							)						->				Union[str, Any]:


      """simple docstring"""




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

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

      __SCREAMING_SNAKE_CASE						     =   False
      __SCREAMING_SNAKE_CASE						     =   model(__SCREAMING_SNAKE_CASE    )
      self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, 2, self.d_model)    )
 def    UpperCAmelCase__	(	self      : List[str]      ,							__SCREAMING_SNAKE_CASE      : Union[str, Any]      ,							__SCREAMING_SNAKE_CASE      : Optional[Any]      ,							__SCREAMING_SNAKE_CASE      : Optional[int]      ,							__SCREAMING_SNAKE_CASE      : Union[str, Any]      ,							__SCREAMING_SNAKE_CASE      : Union[str, Any]      ,							__SCREAMING_SNAKE_CASE      : Optional[int]      ,							__SCREAMING_SNAKE_CASE      : Tuple      ,							__SCREAMING_SNAKE_CASE      : Union[str, Any]      ,							)						->				Union[str, Any]:


      """simple docstring"""




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


      """simple docstring"""




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


      """simple docstring"""




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


      """simple docstring"""




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


      """simple docstring"""




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


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   FunnelForQuestionAnswering(config=__SCREAMING_SNAKE_CASE    )
      model.to(__SCREAMING_SNAKE_CASE    )
      model.eval()
      __SCREAMING_SNAKE_CASE						     =   model(
          __SCREAMING_SNAKE_CASE      ,							attention_mask=__SCREAMING_SNAKE_CASE      ,							token_type_ids=__SCREAMING_SNAKE_CASE      ,							start_positions=__SCREAMING_SNAKE_CASE      ,							end_positions=__SCREAMING_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    UpperCAmelCase__	(	self      : Any    )						->				int:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   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 
      )     ,     
      )						     =   config_and_inputs
      __SCREAMING_SNAKE_CASE						     =   {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
      return config, inputs_dict







@require_torch
class 		lowerCAmelCase__     (   snake_case_				,      snake_case_				,      unittest.TestCase      ):



 """simple docstring"""



 lowerCAmelCase__						      =   False
 lowerCAmelCase__						      =   False
 lowerCAmelCase__						      =   (
     (
         FunnelModel,
         FunnelForMaskedLM,
         FunnelForPreTraining,
         FunnelForQuestionAnswering,
         FunnelForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 lowerCAmelCase__						      =   (
     {
         "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    UpperCAmelCase__	(	self      : List[str]      ,							__SCREAMING_SNAKE_CASE      : Tuple      ,							__SCREAMING_SNAKE_CASE      : Tuple      ,							__SCREAMING_SNAKE_CASE      : Dict=False    )						->				int:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   super()._prepare_for_class(__SCREAMING_SNAKE_CASE      ,							__SCREAMING_SNAKE_CASE      ,							return_labels=__SCREAMING_SNAKE_CASE    )

      if return_labels:
           if model_class in get_values(__SCREAMING_SNAKE_CASE    ):
                __SCREAMING_SNAKE_CASE						     =   torch.zeros(
                    (self.model_tester.batch_size, self.model_tester.seq_length)      ,							dtype=torch.long      ,							device=__SCREAMING_SNAKE_CASE    )
      return inputs_dict
 def    UpperCAmelCase__	(	self      : int    )						->				str:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   FunnelModelTester(self    )
      __SCREAMING_SNAKE_CASE						     =   ConfigTester(self      ,							config_class=__SCREAMING_SNAKE_CASE    )
 def    UpperCAmelCase__	(	self      : str    )						->				List[str]:


      """simple docstring"""




      self.config_tester.run_common_tests()
 def    UpperCAmelCase__	(	self      : Tuple    )						->				str:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE    )
 def    UpperCAmelCase__	(	self      : List[Any]    )						->				Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_pretraining(*__SCREAMING_SNAKE_CASE    )
 def    UpperCAmelCase__	(	self      : int    )						->				Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE    )
 def    UpperCAmelCase__	(	self      : Dict    )						->				Optional[Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE    )
 def    UpperCAmelCase__	(	self      : List[Any]    )						->				List[str]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE    )


 def    UpperCAmelCase__	(	self      : Union[str, Any]      ,							__SCREAMING_SNAKE_CASE      : Any    )						->				Tuple:


      """simple docstring"""




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







@require_torch
class 		lowerCAmelCase__     (   snake_case_				,      unittest.TestCase      ):



 """simple docstring"""



 lowerCAmelCase__						      =   False
 lowerCAmelCase__						      =   False
 lowerCAmelCase__						      =   (
     (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
 )
 def    UpperCAmelCase__	(	self      : Tuple    )						->				Optional[Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   FunnelModelTester(self      ,							base=__SCREAMING_SNAKE_CASE    )
      __SCREAMING_SNAKE_CASE						     =   ConfigTester(self      ,							config_class=__SCREAMING_SNAKE_CASE    )
 def    UpperCAmelCase__	(	self      : Optional[Any]    )						->				str:


      """simple docstring"""




      self.config_tester.run_common_tests()
 def    UpperCAmelCase__	(	self      : List[str]    )						->				Any:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_base_model(*__SCREAMING_SNAKE_CASE    )
 def    UpperCAmelCase__	(	self      : Tuple    )						->				int:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE    )
 def    UpperCAmelCase__	(	self      : int    )						->				str:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_multiple_choice(*__SCREAMING_SNAKE_CASE    )
 def    UpperCAmelCase__	(	self      : Any    )						->				Any:


      """simple docstring"""




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

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


 def    UpperCAmelCase__	(	self      : Dict      ,							__SCREAMING_SNAKE_CASE      : int    )						->				Dict:


      """simple docstring"""




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







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



 """simple docstring"""



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


      """simple docstring"""




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

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

      __SCREAMING_SNAKE_CASE						     =   torch.tensor(2344.8352    )
      __SCREAMING_SNAKE_CASE						     =   torch.tensor(0.8052    )
      self.assertTrue(torch.allclose(output.sum()      ,							__SCREAMING_SNAKE_CASE      ,							atol=1E-4    )    )
      self.assertTrue(torch.allclose(output.mean()      ,							__SCREAMING_SNAKE_CASE      ,							atol=1E-4    )    )

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

      __SCREAMING_SNAKE_CASE						     =   torch.tensor(2343.8425    )
      __SCREAMING_SNAKE_CASE						     =   torch.tensor(0.8049    )
      self.assertTrue(torch.allclose(output.sum()      ,							__SCREAMING_SNAKE_CASE      ,							atol=1E-4    )    )
      self.assertTrue(torch.allclose(output.mean()      ,							__SCREAMING_SNAKE_CASE      ,							atol=1E-4    )    )


 @slow
 def    UpperCAmelCase__	(	self      : int    )						->				Tuple:


      """simple docstring"""




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

      __SCREAMING_SNAKE_CASE						     =   torch.tensor(235.7246    )
      __SCREAMING_SNAKE_CASE						     =   torch.tensor(0.0256    )
      self.assertTrue(torch.allclose(output.sum()      ,							__SCREAMING_SNAKE_CASE      ,							atol=1E-4    )    )
      self.assertTrue(torch.allclose(output.mean()      ,							__SCREAMING_SNAKE_CASE      ,							atol=1E-4    )    )









'''simple docstring'''



from __future__ import annotations

from collections.abc import Callable







def      a__ (						a__   ,					a__   ,					a__   ,					a__ = 1_00   ,					):




     """simple docstring"""


     __SCREAMING_SNAKE_CASE						     =   x_start
     __SCREAMING_SNAKE_CASE						     =   fnc(a__		)
     __SCREAMING_SNAKE_CASE						     =   0.0

     for _ in range(a__		):
          # Approximates small segments of curve as linear and solve
          # for trapezoidal area
          __SCREAMING_SNAKE_CASE						     =   (x_end - x_start) / steps + xa
          __SCREAMING_SNAKE_CASE						     =   fnc(a__		)
          area += abs(fxa + fxa		) * (xa - xa) / 2

          # Increment step
          __SCREAMING_SNAKE_CASE						     =   xa
          __SCREAMING_SNAKE_CASE						     =   fxa
     return area


if __name__ == "__main__":







  def      a__ (						a__		):




       """simple docstring"""


       return x**3

  print('f(x) = x^3')
  print('The area between the curve, x = -10, x = 10 and the x axis is:')
  UpperCAmelCase				: Optional[Any]         =  1_0
  while i <= 1_0_0_0_0_0:
    UpperCAmelCase				: Union[str, Any]         =  trapezoidal_area(f, -5, 5, i)
    print(f"""with {i} steps: {area}""")
    i *= 1_0






