




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






   def __init__(	self   :       str						,					UpperCamelCase_   :       Tuple						,					UpperCamelCase_   :       Any=13						,					UpperCamelCase_   :       List[Any]=7						,					UpperCamelCase_   :       List[Any]=True						,					UpperCamelCase_   :       Dict=True						,					UpperCamelCase_   :       Tuple=True						,					UpperCamelCase_   :       Tuple=True						,					UpperCamelCase_   :       Optional[Any]=99						,					UpperCamelCase_   :       int=[1, 1, 2]						,					UpperCamelCase_   :       Union[str, Any]=1						,					UpperCamelCase_   :       Union[str, Any]=32						,					UpperCamelCase_   :       Union[str, Any]=4						,					UpperCamelCase_   :       List[Any]=8						,					UpperCamelCase_   :       str=37						,					UpperCamelCase_   :       Union[str, Any]="gelu_new"						,					UpperCamelCase_   :       int=0.1						,					UpperCamelCase_   :       Tuple=0.1						,					UpperCamelCase_   :       int=0.0						,					UpperCamelCase_   :       Tuple=5_12						,					UpperCamelCase_   :       Tuple=3						,					UpperCamelCase_   :       Optional[Any]=0.02						,					UpperCamelCase_   :       Any=3						,					UpperCamelCase_   :       int=4						,					UpperCamelCase_   :       str=None						,					UpperCamelCase_   :       List[Any]=False						,					)->			Any:
     _lowercase: Tuple						     =    parent
     _lowercase: Dict						     =    batch_size
     _lowercase: Dict						     =    seq_length
     _lowercase: str						     =    is_training
     _lowercase: List[str]						     =    use_input_mask
     _lowercase: Union[str, Any]						     =    use_token_type_ids
     _lowercase: List[Any]						     =    use_labels
     _lowercase: Optional[Any]						     =    vocab_size
     _lowercase: Union[str, Any]						     =    block_sizes
     _lowercase: Any						     =    num_decoder_layers
     _lowercase: Any						     =    d_model
     _lowercase: Optional[int]						     =    n_head
     _lowercase: List[str]						     =    d_head
     _lowercase: Optional[int]						     =    d_inner
     _lowercase: Optional[Any]						     =    hidden_act
     _lowercase: Optional[int]						     =    hidden_dropout
     _lowercase: str						     =    attention_dropout
     _lowercase: Dict						     =    activation_dropout
     _lowercase: Optional[Any]						     =    max_position_embeddings
     _lowercase: Dict						     =    type_vocab_size
     _lowercase: Any						     =    2
     _lowercase: List[str]						     =    num_labels
     _lowercase: int						     =    num_choices
     _lowercase: Dict						     =    scope
     _lowercase: int						     =    initializer_std

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






   def  _a						(	self   :       str     )->			Optional[int]:
     _lowercase: List[str]						     =    ids_tensor([self.batch_size, self.seq_length]						,					self.vocab_size     )

     _lowercase: Optional[int]						     =    None
     if self.use_input_mask:
       _lowercase: Optional[Any]						     =    ids_tensor([self.batch_size, self.seq_length]						,					vocab_size=2     )

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

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

     _lowercase: Tuple						     =    self.get_config()

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






   def  _a						(	self   :       Dict     )->			Optional[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  _a						(	self   :       Any						,					UpperCamelCase_   :       List[Any]						,					UpperCamelCase_   :       Union[str, Any]						,					UpperCamelCase_   :       List[str]						,					UpperCamelCase_   :       Tuple						,					UpperCamelCase_   :       Optional[Any]						,					UpperCamelCase_   :       Any						,					UpperCamelCase_   :       List[Any]						,					UpperCamelCase_   :       Any						,					)->			Optional[int]:
     _lowercase: Optional[Any]						     =    FunnelModel(config=lowerCAmelCase__     )
     model.to(lowerCAmelCase__     )
     model.eval()
     _lowercase: Optional[Any]						     =    model(lowerCAmelCase__						,					attention_mask=lowerCAmelCase__						,					token_type_ids=lowerCAmelCase__     )
     _lowercase: Optional[Any]						     =    model(lowerCAmelCase__						,					token_type_ids=lowerCAmelCase__     )
     _lowercase: Union[str, Any]						     =    model(lowerCAmelCase__     )
     self.parent.assertEqual(result.last_hidden_state.shape						,					(self.batch_size, self.seq_length, self.d_model)     )

     _lowercase: List[Any]						     =    False
     _lowercase: Any						     =    model(lowerCAmelCase__     )
     self.parent.assertEqual(result.last_hidden_state.shape						,					(self.batch_size, self.seq_length, self.d_model)     )

     _lowercase: Dict						     =    False
     _lowercase: Any						     =    model(lowerCAmelCase__     )
     self.parent.assertEqual(result.last_hidden_state.shape						,					(self.batch_size, self.seq_length, self.d_model)     )






   def  _a						(	self   :       Tuple						,					UpperCamelCase_   :       Tuple						,					UpperCamelCase_   :       Any						,					UpperCamelCase_   :       Optional[Any]						,					UpperCamelCase_   :       Any						,					UpperCamelCase_   :       str						,					UpperCamelCase_   :       int						,					UpperCamelCase_   :       Any						,					UpperCamelCase_   :       str						,					)->			str:
     _lowercase: int						     =    FunnelBaseModel(config=lowerCAmelCase__     )
     model.to(lowerCAmelCase__     )
     model.eval()
     _lowercase: List[str]						     =    model(lowerCAmelCase__						,					attention_mask=lowerCAmelCase__						,					token_type_ids=lowerCAmelCase__     )
     _lowercase: Union[str, Any]						     =    model(lowerCAmelCase__						,					token_type_ids=lowerCAmelCase__     )
     _lowercase: Union[str, Any]						     =    model(lowerCAmelCase__     )
     self.parent.assertEqual(result.last_hidden_state.shape						,					(self.batch_size, 2, self.d_model)     )

     _lowercase: List[Any]						     =    False
     _lowercase: Optional[int]						     =    model(lowerCAmelCase__     )
     self.parent.assertEqual(result.last_hidden_state.shape						,					(self.batch_size, 3, self.d_model)     )

     _lowercase: Optional[int]						     =    False
     _lowercase: str						     =    model(lowerCAmelCase__     )
     self.parent.assertEqual(result.last_hidden_state.shape						,					(self.batch_size, 2, self.d_model)     )






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






   def  _a						(	self   :       List[Any]						,					UpperCamelCase_   :       Optional[Any]						,					UpperCamelCase_   :       Optional[Any]						,					UpperCamelCase_   :       List[Any]						,					UpperCamelCase_   :       Tuple						,					UpperCamelCase_   :       Dict						,					UpperCamelCase_   :       Optional[Any]						,					UpperCamelCase_   :       Dict						,					UpperCamelCase_   :       Tuple						,					)->			Tuple:
     _lowercase: Optional[Any]						     =    FunnelForMaskedLM(config=lowerCAmelCase__     )
     model.to(lowerCAmelCase__     )
     model.eval()
     _lowercase: Tuple						     =    model(lowerCAmelCase__						,					attention_mask=lowerCAmelCase__						,					token_type_ids=lowerCAmelCase__						,					labels=lowerCAmelCase__     )
     self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.seq_length, self.vocab_size)     )






   def  _a						(	self   :       Dict						,					UpperCamelCase_   :       str						,					UpperCamelCase_   :       str						,					UpperCamelCase_   :       Union[str, Any]						,					UpperCamelCase_   :       List[str]						,					UpperCamelCase_   :       str						,					UpperCamelCase_   :       Any						,					UpperCamelCase_   :       Any						,					UpperCamelCase_   :       Any						,					)->			Dict:
     _lowercase: Any						     =    self.num_labels
     _lowercase: Any						     =    FunnelForSequenceClassification(lowerCAmelCase__     )
     model.to(lowerCAmelCase__     )
     model.eval()
     _lowercase: List[str]						     =    model(lowerCAmelCase__						,					attention_mask=lowerCAmelCase__						,					token_type_ids=lowerCAmelCase__						,					labels=lowerCAmelCase__     )
     self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.num_labels)     )






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






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






   def  _a						(	self   :       Optional[Any]						,					UpperCamelCase_   :       Any						,					UpperCamelCase_   :       Dict						,					UpperCamelCase_   :       Optional[int]						,					UpperCamelCase_   :       Any						,					UpperCamelCase_   :       Any						,					UpperCamelCase_   :       List[str]						,					UpperCamelCase_   :       str						,					UpperCamelCase_   :       Union[str, Any]						,					)->			str:
     _lowercase: int						     =    FunnelForQuestionAnswering(config=lowerCAmelCase__     )
     model.to(lowerCAmelCase__     )
     model.eval()
     _lowercase: str						     =    model(
         lowerCAmelCase__						,					attention_mask=lowerCAmelCase__						,					token_type_ids=lowerCAmelCase__						,					start_positions=lowerCAmelCase__						,					end_positions=lowerCAmelCase__						,					)
     self.parent.assertEqual(result.start_logits.shape						,					(self.batch_size, self.seq_length)     )
     self.parent.assertEqual(result.end_logits.shape						,					(self.batch_size, self.seq_length)     )






   def  _a						(	self   :       Any     )->			Optional[int]:
     _lowercase: Union[str, Any]						     =    self.prepare_config_and_inputs()
     (
         _lowercase							
     ): List[str]						     =    config_and_inputs
     _lowercase: int						     =    {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
     return config, inputs_dict

@require_torch
class      UpperCAmelCase						(			UpperCamelCase__   ,					UpperCamelCase__   ,					unittest.TestCase   ):
   __SCREAMING_SNAKE_CASE		:							Optional[Any]							=					False
   __SCREAMING_SNAKE_CASE		:							int							=					False
   __SCREAMING_SNAKE_CASE		:							Optional[int]							=					(
       (
           FunnelModel,
           FunnelForMaskedLM,
           FunnelForPreTraining,
           FunnelForQuestionAnswering,
           FunnelForTokenClassification,
       )
       if is_torch_available()
       else ()
   )
   __SCREAMING_SNAKE_CASE		:							List[str]							=					(
       {
           """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   :       Dict						,					UpperCamelCase_   :       Union[str, Any]						,					UpperCamelCase_   :       Optional[Any]						,					UpperCamelCase_   :       Any=False     )->			List[str]:
     _lowercase: List[str]						     =    super()._prepare_for_class(lowerCAmelCase__						,					lowerCAmelCase__						,					return_labels=lowerCAmelCase__     )

     if return_labels:
       if model_class in get_values(lowerCAmelCase__     ):
         _lowercase: List[str]						     =    torch.zeros(
             (self.model_tester.batch_size, self.model_tester.seq_length)						,					dtype=torch.long						,					device=lowerCAmelCase__     )
     return inputs_dict






   def  _a						(	self   :       List[str]     )->			List[str]:
     _lowercase: List[Any]						     =    FunnelModelTester(self     )
     _lowercase: Optional[int]						     =    ConfigTester(self						,					config_class=lowerCAmelCase__     )






   def  _a						(	self   :       int     )->			List[str]:
     self.config_tester.run_common_tests()






   def  _a						(	self   :       Union[str, Any]     )->			Optional[int]:
     _lowercase: Optional[int]						     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowerCAmelCase__     )






   def  _a						(	self   :       List[str]     )->			List[str]:
     _lowercase: Union[str, Any]						     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase__     )






   def  _a						(	self   :       List[Any]     )->			Optional[int]:
     _lowercase: Optional[int]						     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase__     )






   def  _a						(	self   :       Union[str, Any]     )->			List[Any]:
     _lowercase: int						     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase__     )






   def  _a						(	self   :       str     )->			Tuple:
     _lowercase: List[str]						     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase__     )






   def  _a						(	self   :       Any						,					UpperCamelCase_   :       Any     )->			Optional[Any]:




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

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

@require_torch
class      UpperCAmelCase						(			UpperCamelCase__   ,					unittest.TestCase   ):
   __SCREAMING_SNAKE_CASE		:							Tuple							=					False
   __SCREAMING_SNAKE_CASE		:							List[str]							=					False
   __SCREAMING_SNAKE_CASE		:							str							=					(
       (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
   )






   def  _a						(	self   :       str     )->			List[Any]:
     _lowercase: Union[str, Any]						     =    FunnelModelTester(self						,					base=lowerCAmelCase__     )
     _lowercase: List[Any]						     =    ConfigTester(self						,					config_class=lowerCAmelCase__     )






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






   def  _a						(	self   :       List[Any]     )->			Tuple:
     _lowercase: Dict						     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_base_model(*lowerCAmelCase__     )






   def  _a						(	self   :       Union[str, Any]     )->			Any:
     _lowercase: Optional[int]						     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase__     )






   def  _a						(	self   :       Dict     )->			int:
     _lowercase: Optional[Any]						     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase__     )






   def  _a						(	self   :       int     )->			int:
     _lowercase: Any						     =    self.model_tester.prepare_config_and_inputs_for_common()
     _lowercase: List[Any]						     =    True

     for model_class in self.all_model_classes:
       if model_class.__name__ == "FunnelBaseModel":
         continue
       _lowercase: Any						     =    model_class(lowerCAmelCase__     )
       model.to(lowerCAmelCase__     )
       model.train()
       _lowercase: Union[str, Any]						     =    self._prepare_for_class(lowerCAmelCase__						,					lowerCAmelCase__						,					return_labels=lowerCAmelCase__     )
       _lowercase: Tuple						     =    model(**lowerCAmelCase__     ).loss
       loss.backward()






   def  _a						(	self   :       List[Any]						,					UpperCamelCase_   :       Dict     )->			str:




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

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

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






   def  _a						(	self   :       str     )->			Optional[Any]:
     _lowercase: Tuple						     =    13
     _lowercase: List[Any]						     =    7
     _lowercase: List[Any]						     =    torch.arange(0						,					batch_size * sequence_length     ).long().reshape(lowerCAmelCase__						,					lowerCAmelCase__     )
     _lowercase: Tuple						     =    [0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
     _lowercase: Optional[Any]						     =    torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]     )

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

     _lowercase: Tuple						     =    torch.tensor(23_44.83_52     )
     _lowercase: List[str]						     =    torch.tensor(0.8_052     )
     self.assertTrue(torch.allclose(output.sum()						,					lowerCAmelCase__						,					atol=1E-4     )     )
     self.assertTrue(torch.allclose(output.mean()						,					lowerCAmelCase__						,					atol=1E-4     )     )

     _lowercase: Any						     =    torch.tensor([[1] * 7, [1] * 4 + [0] * 3] * 6 + [[0, 1, 1, 0, 0, 1, 1]]     )
     _lowercase: Tuple						     =    model(lowerCAmelCase__						,					attention_mask=lowerCAmelCase__						,					token_type_ids=lowerCAmelCase__     )[0].abs()

     _lowercase: Union[str, Any]						     =    torch.tensor(23_43.84_25     )
     _lowercase: List[Any]						     =    torch.tensor(0.8_049     )
     self.assertTrue(torch.allclose(output.sum()						,					lowerCAmelCase__						,					atol=1E-4     )     )
     self.assertTrue(torch.allclose(output.mean()						,					lowerCAmelCase__						,					atol=1E-4     )     )






   @slow
   def  _a						(	self   :       Dict     )->			str:
     _lowercase: List[Any]						     =    FunnelTokenizer.from_pretrained('huggingface/funnel-small'     )
     _lowercase: Dict						     =    FunnelModel.from_pretrained('huggingface/funnel-small'     )
     _lowercase: str						     =    tokenizer('Hello! I am the Funnel Transformer model.'						,					return_tensors='pt'     )
     _lowercase: Tuple						     =    model(**lowerCAmelCase__     )[0]

     _lowercase: List[Any]						     =    torch.tensor(2_35.72_46     )
     _lowercase: Tuple						     =    torch.tensor(0.0_256     )
     self.assertTrue(torch.allclose(output.sum()						,					lowerCAmelCase__						,					atol=1E-4     )     )
     self.assertTrue(torch.allclose(output.mean()						,					lowerCAmelCase__						,					atol=1E-4     )     )






import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin

class      UpperCAmelCase						(			A   ):
   __SCREAMING_SNAKE_CASE		:							Optional[Any]							=					"AutoFeatureExtractor"
   __SCREAMING_SNAKE_CASE		:							Optional[Any]							=					"Speech2Text2Tokenizer"






   def __init__(	self   :       Optional[Any]						,					UpperCamelCase_   :       Any						,					UpperCamelCase_   :       List[Any]     )->			Dict:
     super().__init__(UpperCamelCase_						,					UpperCamelCase_     )
     _lowercase: Optional[int]						     =    self.feature_extractor
     _lowercase: int						     =    False






   def __call__(	self   :       List[Any]						,					*UpperCamelCase_   :       Dict						,					**UpperCamelCase_   :       Optional[int]     )->			str:
     # For backward compatibility
     if self._in_target_context_manager:
       return self.current_processor(*UpperCamelCase_						,					**UpperCamelCase_     )

     if "raw_speech" in kwargs:
       warnings.warn('Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'     )
       _lowercase: Tuple						     =    kwargs.pop('raw_speech'     )
     else:
       _lowercase: Optional[Any]						     =    kwargs.pop('audio'						,					UpperCamelCase_     )
     _lowercase: int						     =    kwargs.pop('sampling_rate'						,					UpperCamelCase_     )
     _lowercase: Dict						     =    kwargs.pop('text'						,					UpperCamelCase_     )
     if len(UpperCamelCase_     ) > 0:
       _lowercase: List[Any]						     =    args[0]
       _lowercase: List[Any]						     =    args[1:]

     if audio is None and text is None:
       raise ValueError('You need to specify either an `audio` or `text` input to process.'     )

     if audio is not None:
       _lowercase: Optional[int]						     =    self.feature_extractor(UpperCamelCase_						,					*UpperCamelCase_						,					sampling_rate=UpperCamelCase_						,					**UpperCamelCase_     )
     if text is not None:
       _lowercase: str						     =    self.tokenizer(UpperCamelCase_						,					**UpperCamelCase_     )

     if text is None:
       return inputs
     elif audio is None:
       return encodings
     else:
       _lowercase: int						     =    encodings['input_ids']
       return inputs






   def  _a						(	self   :       Optional[int]						,					*UpperCamelCase_   :       Tuple						,					**UpperCamelCase_   :       Dict     )->			List[str]:
     return self.tokenizer.batch_decode(*UpperCamelCase_						,					**UpperCamelCase_     )






   def  _a						(	self   :       Tuple						,					*UpperCamelCase_   :       List[Any]						,					**UpperCamelCase_   :       Union[str, Any]     )->			Optional[int]:
     return self.tokenizer.decode(*UpperCamelCase_						,					**UpperCamelCase_     )






   @contextmanager
   def  _a						(	self   :       Tuple     )->			Union[str, Any]:
     warnings.warn(
         '`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '
         'labels by using the argument `text` of the regular `__call__` method (either in the same call as '
         'your audio inputs, or in a separate call.'     )
     _lowercase: Any						     =    True
     _lowercase: Optional[Any]						     =    self.tokenizer
     yield
     _lowercase: Union[str, Any]						     =    self.feature_extractor
     _lowercase: str						     =    False
