





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  ,				UpperCAmelCase  ,				UpperCAmelCase=13  ,				UpperCAmelCase=7  ,				UpperCAmelCase=True  ,				UpperCAmelCase=True  ,				UpperCAmelCase=True  ,				UpperCAmelCase=True  ,				UpperCAmelCase=99  ,				UpperCAmelCase=[1, 1, 2]  ,				UpperCAmelCase=1  ,				UpperCAmelCase=32  ,				UpperCAmelCase=4  ,				UpperCAmelCase=8  ,				UpperCAmelCase=37  ,				UpperCAmelCase="gelu_new"  ,				UpperCAmelCase=0.1  ,				UpperCAmelCase=0.1  ,				UpperCAmelCase=0.0  ,				UpperCAmelCase=5_12  ,				UpperCAmelCase=3  ,				UpperCAmelCase=0.02  ,				UpperCAmelCase=3  ,				UpperCAmelCase=4  ,				UpperCAmelCase=None  ,				UpperCAmelCase=False  ,				)->					List[str]:
     __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 		SCREAMING_SNAKE_CASE__       (					self	)->					Optional[Any]:
     __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 		SCREAMING_SNAKE_CASE__       (					self	)->					Union[str, 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 		SCREAMING_SNAKE_CASE__       (					self  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				)->					Optional[int]:
     __A      					=   FunnelModel(config=lowercase_	)
     model.to(lowercase_	)
     model.eval()
     __A      					=   model(lowercase_  ,				attention_mask=lowercase_  ,				token_type_ids=lowercase_	)
     __A      					=   model(lowercase_  ,				token_type_ids=lowercase_	)
     __A      					=   model(lowercase_	)
     self.parent.assertEqual(result.last_hidden_state.shape  ,				(self.batch_size, self.seq_length, self.d_model)	)

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

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

  def 		SCREAMING_SNAKE_CASE__       (					self  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				)->					List[Any]:
     __A      					=   FunnelBaseModel(config=lowercase_	)
     model.to(lowercase_	)
     model.eval()
     __A      					=   model(lowercase_  ,				attention_mask=lowercase_  ,				token_type_ids=lowercase_	)
     __A      					=   model(lowercase_  ,				token_type_ids=lowercase_	)
     __A      					=   model(lowercase_	)
     self.parent.assertEqual(result.last_hidden_state.shape  ,				(self.batch_size, 2, self.d_model)	)

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

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

  def 		SCREAMING_SNAKE_CASE__       (					self  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				)->					List[str]:
     __A      					=   self.num_labels
     __A      					=   FunnelForPreTraining(config=lowercase_	)
     model.to(lowercase_	)
     model.eval()
     __A      					=   model(lowercase_  ,				attention_mask=lowercase_  ,				token_type_ids=lowercase_  ,				labels=lowercase_	)
     self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.seq_length)	)

  def 		SCREAMING_SNAKE_CASE__       (					self  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				)->					int:
     __A      					=   FunnelForMaskedLM(config=lowercase_	)
     model.to(lowercase_	)
     model.eval()
     __A      					=   model(lowercase_  ,				attention_mask=lowercase_  ,				token_type_ids=lowercase_  ,				labels=lowercase_	)
     self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.seq_length, self.vocab_size)	)

  def 		SCREAMING_SNAKE_CASE__       (					self  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				)->					Dict:
     __A      					=   self.num_labels
     __A      					=   FunnelForSequenceClassification(lowercase_	)
     model.to(lowercase_	)
     model.eval()
     __A      					=   model(lowercase_  ,				attention_mask=lowercase_  ,				token_type_ids=lowercase_  ,				labels=lowercase_	)
     self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.num_labels)	)

  def 		SCREAMING_SNAKE_CASE__       (					self  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				)->					str:
     __A      					=   self.num_choices
     __A      					=   FunnelForMultipleChoice(config=lowercase_	)
     model.to(lowercase_	)
     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(
         lowercase_  ,				attention_mask=lowercase_  ,				token_type_ids=lowercase_  ,				labels=lowercase_  ,				)
     self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.num_choices)	)

  def 		SCREAMING_SNAKE_CASE__       (					self  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				)->					List[str]:
     __A      					=   self.num_labels
     __A      					=   FunnelForTokenClassification(config=lowercase_	)
     model.to(lowercase_	)
     model.eval()
     __A      					=   model(lowercase_  ,				attention_mask=lowercase_  ,				token_type_ids=lowercase_  ,				labels=lowercase_	)
     self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.seq_length, self.num_labels)	)

  def 		SCREAMING_SNAKE_CASE__       (					self  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				)->					Dict:
     __A      					=   FunnelForQuestionAnswering(config=lowercase_	)
     model.to(lowercase_	)
     model.eval()
     __A      					=   model(
         lowercase_  ,				attention_mask=lowercase_  ,				token_type_ids=lowercase_  ,				start_positions=lowercase_  ,				end_positions=lowercase_  ,				)
     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 		SCREAMING_SNAKE_CASE__       (					self	)->					int:
     __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 						_lowerCAmelCase(						_a		,    _a		,    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 		SCREAMING_SNAKE_CASE__       (					self  ,				UpperCAmelCase  ,				UpperCAmelCase  ,				UpperCAmelCase=False	)->					List[Any]:
     __A      					=   super()._prepare_for_class(lowercase_  ,				lowercase_  ,				return_labels=lowercase_	)

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

  def 		SCREAMING_SNAKE_CASE__       (					self	)->					Any:
     __A      					=   FunnelModelTester(self	)
     __A      					=   ConfigTester(self  ,				config_class=lowercase_	)

  def 		SCREAMING_SNAKE_CASE__       (					self	)->					Optional[int]:
     self.config_tester.run_common_tests()

  def 		SCREAMING_SNAKE_CASE__       (					self	)->					str:
     __A      					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_	)

  def 		SCREAMING_SNAKE_CASE__       (					self	)->					str:
     __A      					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_pretraining(*lowercase_	)

  def 		SCREAMING_SNAKE_CASE__       (					self	)->					Optional[int]:
     __A      					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*lowercase_	)

  def 		SCREAMING_SNAKE_CASE__       (					self	)->					Union[str, Any]:
     __A      					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*lowercase_	)

  def 		SCREAMING_SNAKE_CASE__       (					self	)->					Dict:
     __A      					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*lowercase_	)





  def 		SCREAMING_SNAKE_CASE__       (					self  ,				UpperCAmelCase	)->					Optional[int]:







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







@require_torch
class 						_lowerCAmelCase(						_a		,    unittest.TestCase):

  """simple docstring"""







  lowerCamelCase__			     =    False
  lowerCamelCase__			     =    False
  lowerCamelCase__			     =    (
      (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
  )

  def 		SCREAMING_SNAKE_CASE__       (					self	)->					Dict:
     __A      					=   FunnelModelTester(self  ,				base=lowercase_	)
     __A      					=   ConfigTester(self  ,				config_class=lowercase_	)

  def 		SCREAMING_SNAKE_CASE__       (					self	)->					int:
     self.config_tester.run_common_tests()

  def 		SCREAMING_SNAKE_CASE__       (					self	)->					Union[str, Any]:
     __A      					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_base_model(*lowercase_	)

  def 		SCREAMING_SNAKE_CASE__       (					self	)->					Optional[int]:
     __A      					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*lowercase_	)

  def 		SCREAMING_SNAKE_CASE__       (					self	)->					Optional[Any]:
     __A      					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*lowercase_	)

  def 		SCREAMING_SNAKE_CASE__       (					self	)->					Dict:
     __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(lowercase_	)
        model.to(lowercase_	)
        model.train()
        __A      					=   self._prepare_for_class(lowercase_  ,				lowercase_  ,				return_labels=lowercase_	)
        __A      					=   model(**lowercase_	).loss
        loss.backward()





  def 		SCREAMING_SNAKE_CASE__       (					self  ,				UpperCAmelCase	)->					Optional[Any]:







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







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

  """simple docstring"""







  def 		SCREAMING_SNAKE_CASE__       (					self	)->					Optional[int]:
     __A      					=   13
     __A      					=   7
     __A      					=   torch.arange(0  ,				batch_size * sequence_length	).long().reshape(lowercase_  ,				lowercase_	)
     __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(lowercase_  ,				token_type_ids=lowercase_	)[0].abs()

     __A      					=   torch.tensor(23_44.83_52	)
     __A      					=   torch.tensor(0.8052	)
     self.assertTrue(torch.allclose(output.sum()  ,				lowercase_  ,				atol=1e-4	)	)
     self.assertTrue(torch.allclose(output.mean()  ,				lowercase_  ,				atol=1e-4	)	)

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

     __A      					=   torch.tensor(23_43.84_25	)
     __A      					=   torch.tensor(0.8049	)
     self.assertTrue(torch.allclose(output.sum()  ,				lowercase_  ,				atol=1e-4	)	)
     self.assertTrue(torch.allclose(output.mean()  ,				lowercase_  ,				atol=1e-4	)	)





  @slow
  def 		SCREAMING_SNAKE_CASE__       (					self	)->					List[Any]:
     __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(**lowercase_	)[0]

     __A      					=   torch.tensor(2_35.72_46	)
     __A      					=   torch.tensor(0.0256	)
     self.assertTrue(torch.allclose(output.sum()  ,				lowercase_  ,				atol=1e-4	)	)
     self.assertTrue(torch.allclose(output.mean()  ,				lowercase_  ,				atol=1e-4	)	)


from itertools import combinations, product


def 							lowerCAmelCase__    (       a__  ,			a__  ,			a__  ,			a__      )	->bool:




  '''simple docstring'''

  if xa == ya == 0 or xa == ya == 0:
    return False
  _UpperCamelCase				=  xa * xa + ya * ya
  _UpperCamelCase				=  xa * xa + ya * ya
  _UpperCamelCase				=  (xa - xa) * (xa - xa) + (ya - ya) * (ya - ya)
  return (
      a_square + b_square == c_square
      or a_square + c_square == b_square
      or b_square + c_square == a_square
  )


def 							lowerCAmelCase__    (       a__ = 50      )	->int:




  '''simple docstring'''

  return sum(
      1
      for pta, pta in combinations(product(range(limit + 1      )  ,			repeat=2      )  ,			2      )
      if is_right(*a__  ,			*a__      )      )


if __name__ == "__main__":
   print(F"{solution() = }")




