






import unittest

from transformers import FunnelConfig, FunnelTokenizer, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import (
       MODEL_FOR_PRETRAINING_MAPPING,
       FunnelBaseModel,
       FunnelForMaskedLM,
       FunnelForMultipleChoice,
       FunnelForPreTraining,
       FunnelForQuestionAnswering,
       FunnelForSequenceClassification,
       FunnelForTokenClassification,
       FunnelModel,
   )





class 	_lowercase		:

 def __init__(				self			:							Optional[int]     ,   snake_case			:							Dict     ,   snake_case			:							Union[str, Any]=1_3     ,   snake_case			:							Tuple=7     ,   snake_case			:							List[str]=True     ,   snake_case			:							Optional[Any]=True     ,   snake_case			:							Dict=True     ,   snake_case			:							List[Any]=True     ,   snake_case			:							int=9_9     ,   snake_case			:							Union[str, Any]=[1, 1, 2]     ,   snake_case			:							Dict=1     ,   snake_case			:							List[Any]=3_2     ,   snake_case			:							str=4     ,   snake_case			:							str=8     ,   snake_case			:							List[str]=3_7     ,   snake_case			:							List[str]="gelu_new"     ,   snake_case			:							List[str]=0.1     ,   snake_case			:							int=0.1     ,   snake_case			:							Optional[Any]=0.0     ,   snake_case			:							List[Any]=5_1_2     ,   snake_case			:							Optional[int]=3     ,   snake_case			:							Optional[int]=0.02     ,   snake_case			:							Any=3     ,   snake_case			:							Dict=4     ,   snake_case			:							int=None     ,   snake_case			:							List[str]=False     ,   )     ->		List[Any]:

  """simple docstring"""





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

  # Used in the tests to check the size of the first attention layer
  UpperCamelCase_      :		str				=      n_head
  # Used in the tests to check the size of the first hidden state
  UpperCamelCase_      :		int				=      self.d_model
  # Used in the tests to check the number of output hidden states/attentions
  UpperCamelCase_      :		Any				=      sum(self.block_sizes    ) + (0 if base else self.num_decoder_layers)
  # FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
  # the last hidden state of the first block (which is the first hidden state of the decoder).
  if not base:
   UpperCamelCase_      :		Optional[int]				=      self.num_hidden_layers + 2

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

  """simple docstring"""





  UpperCamelCase_      :		Optional[Any]				=      ids_tensor([self.batch_size, self.seq_length]     ,   self.vocab_size    )

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

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

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

  UpperCamelCase_      :		List[Any]				=      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]    )     ->		Optional[int]:

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

  """simple docstring"""





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

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

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

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

  """simple docstring"""





  UpperCamelCase_      :		Any				=      FunnelBaseModel(config=snake_case    )
  model.to(snake_case    )
  model.eval()
  UpperCamelCase_      :		List[str]				=      model(snake_case     ,   attention_mask=snake_case     ,   token_type_ids=snake_case    )
  UpperCamelCase_      :		Dict				=      model(snake_case     ,   token_type_ids=snake_case    )
  UpperCamelCase_      :		List[str]				=      model(snake_case    )
  self.parent.assertEqual(result.last_hidden_state.shape     ,   (self.batch_size, 2, self.d_model)    )

  UpperCamelCase_      :		Optional[int]				=      False
  UpperCamelCase_      :		Union[str, Any]				=      model(snake_case    )
  self.parent.assertEqual(result.last_hidden_state.shape     ,   (self.batch_size, 3, self.d_model)    )

  UpperCamelCase_      :		Any				=      False
  UpperCamelCase_      :		Optional[int]				=      model(snake_case    )
  self.parent.assertEqual(result.last_hidden_state.shape     ,   (self.batch_size, 2, self.d_model)    )

 def 		SCREAMING_SNAKE_CASE__     (				self			:							List[str]     ,   snake_case			:							Any     ,   snake_case			:							Dict     ,   snake_case			:							Any     ,   snake_case			:							Dict     ,   snake_case			:							str     ,   snake_case			:							Any     ,   snake_case			:							Optional[int]     ,   snake_case			:							List[Any]     ,   )     ->		List[str]:

  """simple docstring"""





  UpperCamelCase_      :		str				=      self.num_labels
  UpperCamelCase_      :		Tuple				=      FunnelForPreTraining(config=snake_case    )
  model.to(snake_case    )
  model.eval()
  UpperCamelCase_      :		str				=      model(snake_case     ,   attention_mask=snake_case     ,   token_type_ids=snake_case     ,   labels=snake_case    )
  self.parent.assertEqual(result.logits.shape     ,   (self.batch_size, self.seq_length)    )

 def 		SCREAMING_SNAKE_CASE__     (				self			:							int     ,   snake_case			:							str     ,   snake_case			:							int     ,   snake_case			:							Tuple     ,   snake_case			:							Tuple     ,   snake_case			:							Optional[int]     ,   snake_case			:							Tuple     ,   snake_case			:							List[str]     ,   snake_case			:							List[Any]     ,   )     ->		Any:

  """simple docstring"""





  UpperCamelCase_      :		List[str]				=      FunnelForMaskedLM(config=snake_case    )
  model.to(snake_case    )
  model.eval()
  UpperCamelCase_      :		Optional[Any]				=      model(snake_case     ,   attention_mask=snake_case     ,   token_type_ids=snake_case     ,   labels=snake_case    )
  self.parent.assertEqual(result.logits.shape     ,   (self.batch_size, self.seq_length, self.vocab_size)    )

 def 		SCREAMING_SNAKE_CASE__     (				self			:							Optional[int]     ,   snake_case			:							Any     ,   snake_case			:							Tuple     ,   snake_case			:							Any     ,   snake_case			:							Union[str, Any]     ,   snake_case			:							str     ,   snake_case			:							Optional[Any]     ,   snake_case			:							int     ,   snake_case			:							Tuple     ,   )     ->		List[Any]:

  """simple docstring"""





  UpperCamelCase_      :		Optional[int]				=      self.num_labels
  UpperCamelCase_      :		Union[str, Any]				=      FunnelForSequenceClassification(snake_case    )
  model.to(snake_case    )
  model.eval()
  UpperCamelCase_      :		str				=      model(snake_case     ,   attention_mask=snake_case     ,   token_type_ids=snake_case     ,   labels=snake_case    )
  self.parent.assertEqual(result.logits.shape     ,   (self.batch_size, self.num_labels)    )

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

  """simple docstring"""





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

 def 		SCREAMING_SNAKE_CASE__     (				self			:							Union[str, Any]     ,   snake_case			:							int     ,   snake_case			:							List[str]     ,   snake_case			:							Optional[Any]     ,   snake_case			:							str     ,   snake_case			:							Union[str, Any]     ,   snake_case			:							str     ,   snake_case			:							Tuple     ,   snake_case			:							Union[str, Any]     ,   )     ->		str:

  """simple docstring"""





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

 def 		SCREAMING_SNAKE_CASE__     (				self			:							List[Any]     ,   snake_case			:							Tuple     ,   snake_case			:							str     ,   snake_case			:							Tuple     ,   snake_case			:							List[str]     ,   snake_case			:							List[str]     ,   snake_case			:							List[str]     ,   snake_case			:							Union[str, Any]     ,   snake_case			:							Union[str, Any]     ,   )     ->		str:

  """simple docstring"""





  UpperCamelCase_      :		int				=      FunnelForQuestionAnswering(config=snake_case    )
  model.to(snake_case    )
  model.eval()
  UpperCamelCase_      :		Tuple				=      model(
      snake_case     ,   attention_mask=snake_case     ,   token_type_ids=snake_case     ,   start_positions=snake_case     ,   end_positions=snake_case     ,   )
  self.parent.assertEqual(result.start_logits.shape     ,   (self.batch_size, self.seq_length)    )
  self.parent.assertEqual(result.end_logits.shape     ,   (self.batch_size, self.seq_length)    )


 def 		SCREAMING_SNAKE_CASE__     (				self			:							Tuple    )     ->		Any:

  """simple docstring"""





  UpperCamelCase_      :		Tuple				=      self.prepare_config_and_inputs()
  (
      UpperCamelCase_   
  )      :		Any				=      config_and_inputs
  UpperCamelCase_      :		List[Any]				=      {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
  return config, inputs_dict





@require_torch
class 	_lowercase		(      snake_case_		,   snake_case_		,   unittest.TestCase ):
 lowercase         =     False
 lowercase         =     False
 lowercase         =     (
     (
         FunnelModel,
         FunnelForMaskedLM,
         FunnelForPreTraining,
         FunnelForQuestionAnswering,
         FunnelForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 lowercase         =     (
     {
         "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			:							Tuple     ,   snake_case			:							str     ,   snake_case			:							Optional[int]     ,   snake_case			:							Optional[Any]=False    )     ->		Union[str, Any]:

  """simple docstring"""





  UpperCamelCase_      :		Optional[Any]				=      super()._prepare_for_class(snake_case     ,   snake_case     ,   return_labels=snake_case    )

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

 def 		SCREAMING_SNAKE_CASE__     (				self			:							Any    )     ->		Any:

  """simple docstring"""





  UpperCamelCase_      :		List[str]				=      FunnelModelTester(self    )
  UpperCamelCase_      :		Tuple				=      ConfigTester(self     ,   config_class=snake_case    )

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

  """simple docstring"""





  self.config_tester.run_common_tests()

 def 		SCREAMING_SNAKE_CASE__     (				self			:							int    )     ->		Dict:

  """simple docstring"""





  UpperCamelCase_      :		str				=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_model(*snake_case    )

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

  """simple docstring"""





  UpperCamelCase_      :		Tuple				=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_pretraining(*snake_case    )

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

  """simple docstring"""





  UpperCamelCase_      :		int				=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_masked_lm(*snake_case    )

 def 		SCREAMING_SNAKE_CASE__     (				self			:							List[str]    )     ->		Dict:

  """simple docstring"""





  UpperCamelCase_      :		Tuple				=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_token_classification(*snake_case    )

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

  """simple docstring"""





  UpperCamelCase_      :		int				=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_question_answering(*snake_case    )


 def 		SCREAMING_SNAKE_CASE__     (				self			:							List[Any]     ,   snake_case			:							str    )     ->		Any:

  """simple docstring"""





  if hasattr(snake_case     ,   'weight'    ) and module.weight is not None:
   module.weight.data.fill_(3    )
  if hasattr(snake_case     ,   'bias'    ) and module.bias is not None:
   module.bias.data.fill_(3    )

  for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
   if hasattr(snake_case     ,   snake_case    ) and getattr(snake_case     ,   snake_case    ) is not None:
    UpperCamelCase_      :		Optional[int]				=      getattr(snake_case     ,   snake_case    )
    weight.data.fill_(3    )





@require_torch
class 	_lowercase		(      snake_case_		,   unittest.TestCase ):
 lowercase         =     False
 lowercase         =     False
 lowercase         =     (
     (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
 )

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

  """simple docstring"""





  UpperCamelCase_      :		List[Any]				=      FunnelModelTester(self     ,   base=snake_case    )
  UpperCamelCase_      :		Tuple				=      ConfigTester(self     ,   config_class=snake_case    )

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

  """simple docstring"""





  self.config_tester.run_common_tests()

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

  """simple docstring"""





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

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

  """simple docstring"""





  UpperCamelCase_      :		Optional[Any]				=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_sequence_classification(*snake_case    )

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

  """simple docstring"""





  UpperCamelCase_      :		Any				=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_multiple_choice(*snake_case    )

 def 		SCREAMING_SNAKE_CASE__     (				self			:							Tuple    )     ->		Tuple:

  """simple docstring"""





  UpperCamelCase_      :		Any				=      self.model_tester.prepare_config_and_inputs_for_common()
  UpperCamelCase_      :		Dict				=      True

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


 def 		SCREAMING_SNAKE_CASE__     (				self			:							Union[str, Any]     ,   snake_case			:							Tuple    )     ->		Tuple:

  """simple docstring"""





  if hasattr(snake_case     ,   'weight'    ) and module.weight is not None:
   module.weight.data.fill_(3    )
  if hasattr(snake_case     ,   'bias'    ) and module.bias is not None:
   module.bias.data.fill_(3    )

  for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
   if hasattr(snake_case     ,   snake_case    ) and getattr(snake_case     ,   snake_case    ) is not None:
    UpperCamelCase_      :		List[str]				=      getattr(snake_case     ,   snake_case    )
    weight.data.fill_(3    )





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

 def 		SCREAMING_SNAKE_CASE__     (				self			:							List[str]    )     ->		int:

  """simple docstring"""





  UpperCamelCase_      :		Optional[Any]				=      1_3
  UpperCamelCase_      :		Optional[Any]				=      7
  UpperCamelCase_      :		int				=      torch.arange(0     ,   batch_size * sequence_length    ).long().reshape(snake_case     ,   snake_case    )
  UpperCamelCase_      :		str				=      [0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
  UpperCamelCase_      :		Optional[Any]				=      torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]    )

  UpperCamelCase_      :		int				=      FunnelModel.from_pretrained('sgugger/funnel-random-tiny'    )
  UpperCamelCase_      :		Any				=      model(snake_case     ,   token_type_ids=snake_case    )[0].abs()

  UpperCamelCase_      :		Tuple				=      torch.tensor(2_3_4_4.8_3_5_2    )
  UpperCamelCase_      :		Tuple				=      torch.tensor(0.8052    )
  self.assertTrue(torch.allclose(output.sum()     ,   snake_case     ,   atol=1e-4    )    )
  self.assertTrue(torch.allclose(output.mean()     ,   snake_case     ,   atol=1e-4    )    )

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

  UpperCamelCase_      :		Union[str, Any]				=      torch.tensor(2_3_4_3.8_4_2_5    )
  UpperCamelCase_      :		str				=      torch.tensor(0.8049    )
  self.assertTrue(torch.allclose(output.sum()     ,   snake_case     ,   atol=1e-4    )    )
  self.assertTrue(torch.allclose(output.mean()     ,   snake_case     ,   atol=1e-4    )    )


 @slow
 def 		SCREAMING_SNAKE_CASE__     (				self			:							str    )     ->		str:

  """simple docstring"""





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

  UpperCamelCase_      :		Tuple				=      torch.tensor(235.7246    )
  UpperCamelCase_      :		str				=      torch.tensor(0.0256    )
  self.assertTrue(torch.allclose(output.sum()     ,   snake_case     ,   atol=1e-4    )    )
  self.assertTrue(torch.allclose(output.mean()     ,   snake_case     ,   atol=1e-4    )    )




import json
from typing import TYPE_CHECKING, List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_deberta import DebertaTokenizer


if TYPE_CHECKING:
   from transformers.pipelines.conversational import Conversation


a_					      = logging.get_logger(__name__)

a_					      = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'}

a_					      = {
    'vocab_file': {
        'microsoft/deberta-base': 'https://huggingface.co/microsoft/deberta-base/resolve/main/vocab.json',
        'microsoft/deberta-large': 'https://huggingface.co/microsoft/deberta-large/resolve/main/vocab.json',
        'microsoft/deberta-xlarge': 'https://huggingface.co/microsoft/deberta-xlarge/resolve/main/vocab.json',
        'microsoft/deberta-base-mnli': 'https://huggingface.co/microsoft/deberta-base-mnli/resolve/main/vocab.json',
        'microsoft/deberta-large-mnli': 'https://huggingface.co/microsoft/deberta-large-mnli/resolve/main/vocab.json',
        'microsoft/deberta-xlarge-mnli': (
            'https://huggingface.co/microsoft/deberta-xlarge-mnli/resolve/main/vocab.json'
        ),
    },
    'merges_file': {
        'microsoft/deberta-base': 'https://huggingface.co/microsoft/deberta-base/resolve/main/merges.txt',
        'microsoft/deberta-large': 'https://huggingface.co/microsoft/deberta-large/resolve/main/merges.txt',
        'microsoft/deberta-xlarge': 'https://huggingface.co/microsoft/deberta-xlarge/resolve/main/merges.txt',
        'microsoft/deberta-base-mnli': 'https://huggingface.co/microsoft/deberta-base-mnli/resolve/main/merges.txt',
        'microsoft/deberta-large-mnli': 'https://huggingface.co/microsoft/deberta-large-mnli/resolve/main/merges.txt',
        'microsoft/deberta-xlarge-mnli': (
            'https://huggingface.co/microsoft/deberta-xlarge-mnli/resolve/main/merges.txt'
        ),
    },
}

a_					      = {
    'microsoft/deberta-base': 512,
    'microsoft/deberta-large': 512,
    'microsoft/deberta-xlarge': 512,
    'microsoft/deberta-base-mnli': 512,
    'microsoft/deberta-large-mnli': 512,
    'microsoft/deberta-xlarge-mnli': 512,
}

a_					      = {
    'microsoft/deberta-base': {'do_lower_case': False},
    'microsoft/deberta-large': {'do_lower_case': False},
}





class 	_lowercase		(      snake_case_ ):
 lowercase         =     VOCAB_FILES_NAMES
 lowercase         =     PRETRAINED_VOCAB_FILES_MAP
 lowercase         =     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 lowercase         =     ['input_ids', 'attention_mask', 'token_type_ids']
 lowercase         =     DebertaTokenizer

 def __init__(				self			:							Optional[int]     ,   snake_case			:							Any=None     ,   snake_case			:							Union[str, Any]=None     ,   snake_case			:							Optional[Any]=None     ,   snake_case			:							List[str]="replace"     ,   snake_case			:							int="[CLS]"     ,   snake_case			:							Any="[SEP]"     ,   snake_case			:							int="[SEP]"     ,   snake_case			:							Union[str, Any]="[CLS]"     ,   snake_case			:							List[Any]="[UNK]"     ,   snake_case			:							List[str]="[PAD]"     ,   snake_case			:							Tuple="[MASK]"     ,   snake_case			:							int=False     ,   **snake_case			:							List[str]     ,   )     ->		Any:

  """simple docstring"""





  super().__init__(
      snake_case     ,   snake_case     ,   tokenizer_file=snake_case     ,   errors=snake_case     ,   bos_token=snake_case     ,   eos_token=snake_case     ,   unk_token=snake_case     ,   sep_token=snake_case     ,   cls_token=snake_case     ,   pad_token=snake_case     ,   mask_token=snake_case     ,   add_prefix_space=snake_case     ,   **snake_case     ,   )
  UpperCamelCase_      :		str				=      kwargs.pop('add_bos_token'     ,   snake_case    )

  UpperCamelCase_      :		Tuple				=      json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()    )
  if pre_tok_state.get('add_prefix_space'     ,   snake_case    ) != add_prefix_space:
   UpperCamelCase_      :		str				=      getattr(snake_case     ,   pre_tok_state.pop('type'    )    )
   UpperCamelCase_      :		List[Any]				=      add_prefix_space
   UpperCamelCase_      :		List[str]				=      pre_tok_class(**snake_case    )

  UpperCamelCase_      :		int				=      add_prefix_space

 @property
 def 		SCREAMING_SNAKE_CASE__     (				self			:							List[str]    )     ->		str:

  """simple docstring"""





  if self._mask_token is None:
   if self.verbose:
    logger.error('Using mask_token, but it is not set yet.'    )
   return None
  return str(self._mask_token    )

 @mask_token.setter
 def 		SCREAMING_SNAKE_CASE__     (				self			:							Any     ,   snake_case			:							Optional[int]    )     ->		str:

  """simple docstring"""





  UpperCamelCase_      :		List[Any]				=      AddedToken(snake_case     ,   lstrip=snake_case     ,   rstrip=snake_case    ) if isinstance(snake_case     ,   snake_case    ) else value
  UpperCamelCase_      :		Optional[int]				=      value

 def 		SCREAMING_SNAKE_CASE__     (				self			:							str     ,   snake_case			:							List[int]     ,   snake_case			:							Optional[List[int]] = None    )     ->		List[int]:

  """simple docstring"""





  if token_ids_a is None:
   return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
  UpperCamelCase_      :		Dict				=      [self.cls_token_id]
  UpperCamelCase_      :		Tuple				=      [self.sep_token_id]
  return cls + token_ids_a + sep + token_ids_a + sep

 def 		SCREAMING_SNAKE_CASE__     (				self			:							str     ,   snake_case			:							List[int]     ,   snake_case			:							Optional[List[int]] = None    )     ->		List[int]:

  """simple docstring"""





  UpperCamelCase_      :		Optional[int]				=      [self.sep_token_id]
  UpperCamelCase_      :		List[str]				=      [self.cls_token_id]

  if token_ids_a is None:
   return len(cls + token_ids_a + sep    ) * [0]
  return len(cls + token_ids_a + sep    ) * [0] + len(token_ids_a + sep    ) * [1]

 def 		SCREAMING_SNAKE_CASE__     (				self			:							Optional[int]     ,   *snake_case			:							Any     ,   **snake_case			:							Union[str, Any]    )     ->		BatchEncoding:

  """simple docstring"""





  UpperCamelCase_      :		Tuple				=      kwargs.get('is_split_into_words'     ,   snake_case    )
  assert self.add_prefix_space or not is_split_into_words, (
      f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True "
      "to use it with pretokenized inputs."
  )

  return super()._batch_encode_plus(*snake_case     ,   **snake_case    )

 def 		SCREAMING_SNAKE_CASE__     (				self			:							int     ,   *snake_case			:							List[str]     ,   **snake_case			:							Union[str, Any]    )     ->		BatchEncoding:

  """simple docstring"""





  UpperCamelCase_      :		List[Any]				=      kwargs.get('is_split_into_words'     ,   snake_case    )

  assert self.add_prefix_space or not is_split_into_words, (
      f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True "
      "to use it with pretokenized inputs."
  )

  return super()._encode_plus(*snake_case     ,   **snake_case    )

 def 		SCREAMING_SNAKE_CASE__     (				self			:							Optional[Any]     ,   snake_case			:							str     ,   snake_case			:							Optional[str] = None    )     ->		Tuple[str]:

  """simple docstring"""





  UpperCamelCase_      :		Dict				=      self._tokenizer.model.save(snake_case     ,   name=snake_case    )
  return tuple(snake_case    )


 def 		SCREAMING_SNAKE_CASE__     (				self			:							Any     ,   snake_case			:							"Conversation"    )     ->		List[int]:

  """simple docstring"""





  UpperCamelCase_      :		Tuple				=      []
  for is_user, text in conversation.iter_texts():
   input_ids.extend(self.encode(snake_case     ,   add_special_tokens=snake_case    ) + [self.eos_token_id]    )

  if len(snake_case    ) > self.model_max_length:
   UpperCamelCase_      :		Optional[int]				=      input_ids[-self.model_max_length :]
  return input_ids



