



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



 '''simple docstring'''







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


      """simple docstring"""



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

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



 def  a__  (self	)   ->					List[Any]:


      """simple docstring"""



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

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

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

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

      _a     =  self.get_config()

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



 def  a__  (self	)   ->					Union[str, 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  a__  (self			,       A			,       A			,       A			,       A			,       A			,       A			,       A			,       A			,       )   ->					int:


      """simple docstring"""



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

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

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



 def  a__  (self			,       A			,       A			,       A			,       A			,       A			,       A			,       A			,       A			,       )   ->					Dict:


      """simple docstring"""



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

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

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



 def  a__  (self			,       A			,       A			,       A			,       A			,       A			,       A			,       A			,       A			,       )   ->					Optional[Any]:


      """simple docstring"""



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



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


      """simple docstring"""



      _a     =  FunnelForMaskedLM(config=A	)
      model.to(A	)
      model.eval()
      _a     =  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  a__  (self			,       A			,       A			,       A			,       A			,       A			,       A			,       A			,       A			,       )   ->					List[str]:


      """simple docstring"""



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



 def  a__  (self			,       A			,       A			,       A			,       A			,       A			,       A			,       A			,       A			,       )   ->					List[Any]:


      """simple docstring"""



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



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


      """simple docstring"""



      _a     =  self.num_labels
      _a     =  FunnelForTokenClassification(config=A	)
      model.to(A	)
      model.eval()
      _a     =  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  a__  (self			,       A			,       A			,       A			,       A			,       A			,       A			,       A			,       A			,       )   ->					List[str]:


      """simple docstring"""



      _a     =  FunnelForQuestionAnswering(config=A	)
      model.to(A	)
      model.eval()
      _a     =  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  a__  (self	)   ->					Any:


      """simple docstring"""



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






@require_torch
class __A      (  A       ,       A       ,       unittest.TestCase						):



 '''simple docstring'''







 __lowerCamelCase   :	Any										=	False
 __lowerCamelCase   :	List[Any]										=	False
 __lowerCamelCase   :	List[str]										=	(
     (
         FunnelModel,
         FunnelForMaskedLM,
         FunnelForPreTraining,
         FunnelForQuestionAnswering,
         FunnelForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 __lowerCamelCase   :	Union[str, Any]										=	(
     {
         'feature-extraction': (FunnelBaseModel, FunnelModel),
         'fill-mask': FunnelForMaskedLM,
         'question-answering': FunnelForQuestionAnswering,
         'text-classification': FunnelForSequenceClassification,
         'token-classification': FunnelForTokenClassification,
         'zero-shot': FunnelForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )



 def  a__  (self			,       A			,       A			,       A=False	)   ->					str:


      """simple docstring"""



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

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



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


      """simple docstring"""



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



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


      """simple docstring"""



      self.config_tester.run_common_tests()



 def  a__  (self	)   ->					List[str]:


      """simple docstring"""



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



 def  a__  (self	)   ->					str:


      """simple docstring"""



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



 def  a__  (self	)   ->					Any:


      """simple docstring"""



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



 def  a__  (self	)   ->					Dict:


      """simple docstring"""



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



 def  a__  (self	)   ->					List[str]:


      """simple docstring"""



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



 def  a__  (self			,       A	)   ->					Dict:


      """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:
                _a     =  getattr(A			,       A	)
                weight.data.fill_(3	)






@require_torch
class __A      (  A       ,       unittest.TestCase						):



 '''simple docstring'''







 __lowerCamelCase   :	Optional[int]										=	False
 __lowerCamelCase   :	Optional[Any]										=	False
 __lowerCamelCase   :	Tuple										=	(
     (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
 )



 def  a__  (self	)   ->					List[str]:


      """simple docstring"""



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



 def  a__  (self	)   ->					str:


      """simple docstring"""



      self.config_tester.run_common_tests()



 def  a__  (self	)   ->					int:


      """simple docstring"""



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



 def  a__  (self	)   ->					List[str]:


      """simple docstring"""



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



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


      """simple docstring"""



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



 def  a__  (self	)   ->					Dict:


      """simple docstring"""



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

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



 def  a__  (self			,       A	)   ->					List[str]:


      """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:
                _a     =  getattr(A			,       A	)
                weight.data.fill_(3	)






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



 '''simple docstring'''







 def  a__  (self	)   ->					Dict:


      """simple docstring"""



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

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

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

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

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



 @slow
 def  a__  (self	)   ->					Any:


      """simple docstring"""



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

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








'''simple docstring'''





import warnings
from collections.abc import Callable
from typing import Any

import numpy
from matplotlib import pyplot

lowercase_			=  0.25 + 0.0J
lowercase_			=  -0.4 + 0.6J
lowercase_			=  -0.1 + 0.651J
lowercase_			=  -2.0
lowercase_			=  56
lowercase_			=  2.0
lowercase_			=  666




def lowerCAmelCase  (__A  ,     __A):



     """simple docstring"""



     return numpy.exp(__A) + c_parameter




def lowerCAmelCase  (__A  ,     __A):



     """simple docstring"""



     return z_values * z_values + c_parameter




def lowerCAmelCase  (__A  ,     __A):



     """simple docstring"""



     _a     =  numpy.linspace(-window_size  ,     __A  ,     __A)
     _a     =  x.reshape((nb_pixels, 1))
     _a     =  numpy.linspace(-window_size  ,     __A  ,     __A)
     _a     =  y.reshape((1, nb_pixels))
     return x + 1.0J * y




def lowerCAmelCase  (__A  ,     __A  ,     __A  ,     __A  ,     __A = None  ,     ):



     """simple docstring"""



     _a     =  z_a.astype('''complex64''')
     for _ in range(__A):
          _a     =  eval_function(__A  ,     __A)
          if infinity is not None:
               numpy.nan_to_num(__A  ,     copy=__A  ,     nan=__A)
               _a     =  infinity
     return z_n




def lowerCAmelCase  (__A  ,     __A  ,     __A  ,     __A  ,     ):



     """simple docstring"""



     _a     =  (abs(__A)).transpose()
     _a     =  abs_z_final[::-1, :]
     pyplot.matshow(abs_z_final < escape_radius)
     pyplot.title(F'''Julia set of ${function_label}$, $c={function_params}$''')
     pyplot.show()




def lowerCAmelCase  ():



     """simple docstring"""



     warnings.filterwarnings(
         '''ignore'''  ,     category=__A  ,     message='''overflow encountered in multiply''')
     warnings.filterwarnings(
         '''ignore'''  ,     category=__A  ,     message='''invalid value encountered in multiply'''  ,     )
     warnings.filterwarnings(
         '''ignore'''  ,     category=__A  ,     message='''overflow encountered in absolute''')
     warnings.filterwarnings(
         '''ignore'''  ,     category=__A  ,     message='''overflow encountered in exp''')


if __name__ == "__main__":
  lowercase_			=  prepare_grid(window_size, nb_pixels)

  ignore_overflow_warnings()  # See file header for explanations

  lowercase_			=  24
  lowercase_			=  2 * abs(c_cauliflower) + 1
  lowercase_			=  iterate_function(
      eval_quadratic_polynomial,
      c_cauliflower,
      nb_iterations,
      z_a,
      infinity=1.1 * escape_radius,
  )
  show_results("z^2+c", c_cauliflower, escape_radius, z_final)

  lowercase_			=  64
  lowercase_			=  2 * abs(c_polynomial_a) + 1
  lowercase_			=  iterate_function(
      eval_quadratic_polynomial,
      c_polynomial_a,
      nb_iterations,
      z_a,
      infinity=1.1 * escape_radius,
  )
  show_results("z^2+c", c_polynomial_a, escape_radius, z_final)

  lowercase_			=  161
  lowercase_			=  2 * abs(c_polynomial_a) + 1
  lowercase_			=  iterate_function(
      eval_quadratic_polynomial,
      c_polynomial_a,
      nb_iterations,
      z_a,
      infinity=1.1 * escape_radius,
  )
  show_results("z^2+c", c_polynomial_a, escape_radius, z_final)

  lowercase_			=  12
  lowercase_			=  1_0000.0
  lowercase_			=  iterate_function(
      eval_exponential,
      c_exponential,
      nb_iterations,
      z_a + 2,
      infinity=1.0e10,
  )
  show_results("e^z+c", c_exponential, escape_radius, z_final)



