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				:						Tuple				,	lowerCamelCase				:						Optional[Any]				,	lowerCamelCase				:						Dict=1_3				,	lowerCamelCase				:						Tuple=7				,	lowerCamelCase				:						List[str]=True				,	lowerCamelCase				:						Union[str, Any]=True				,	lowerCamelCase				:						int=True				,	lowerCamelCase				:						Any=True				,	lowerCamelCase				:						List[Any]=9_9				,	lowerCamelCase				:						Optional[int]=[1, 1, 2]				,	lowerCamelCase				:						Any=1				,	lowerCamelCase				:						Dict=3_2				,	lowerCamelCase				:						Union[str, Any]=4				,	lowerCamelCase				:						Dict=8				,	lowerCamelCase				:						Union[str, Any]=3_7				,	lowerCamelCase				:						List[Any]="gelu_new"				,	lowerCamelCase				:						Dict=0.1				,	lowerCamelCase				:						Dict=0.1				,	lowerCamelCase				:						List[Any]=0.0				,	lowerCamelCase				:						Any=5_1_2				,	lowerCamelCase				:						List[Any]=3				,	lowerCamelCase				:						Dict=0.0_2				,	lowerCamelCase				:						int=3				,	lowerCamelCase				:						int=4				,	lowerCamelCase				:						List[str]=None				,	lowerCamelCase				:						Optional[Any]=False				,	)       ->							Optional[int]:
        _UpperCamelCase   :				Any       =     parent
        _UpperCamelCase   :				Optional[Any]       =     batch_size
        _UpperCamelCase   :				List[str]       =     seq_length
        _UpperCamelCase   :				Optional[Any]       =     is_training
        _UpperCamelCase   :				int       =     use_input_mask
        _UpperCamelCase   :				str       =     use_token_type_ids
        _UpperCamelCase   :				Tuple       =     use_labels
        _UpperCamelCase   :				Union[str, Any]       =     vocab_size
        _UpperCamelCase   :				Dict       =     block_sizes
        _UpperCamelCase   :				Dict       =     num_decoder_layers
        _UpperCamelCase   :				int       =     d_model
        _UpperCamelCase   :				Any       =     n_head
        _UpperCamelCase   :				Union[str, Any]       =     d_head
        _UpperCamelCase   :				Optional[Any]       =     d_inner
        _UpperCamelCase   :				Any       =     hidden_act
        _UpperCamelCase   :				int       =     hidden_dropout
        _UpperCamelCase   :				Union[str, Any]       =     attention_dropout
        _UpperCamelCase   :				Optional[int]       =     activation_dropout
        _UpperCamelCase   :				Optional[int]       =     max_position_embeddings
        _UpperCamelCase   :				Any       =     type_vocab_size
        _UpperCamelCase   :				int       =     2
        _UpperCamelCase   :				int       =     num_labels
        _UpperCamelCase   :				str       =     num_choices
        _UpperCamelCase   :				Optional[Any]       =     scope
        _UpperCamelCase   :				List[str]       =     initializer_std

        # Used in the tests to check the size of the first attention layer
        _UpperCamelCase   :				Tuple       =     n_head
        # Used in the tests to check the size of the first hidden state
        _UpperCamelCase   :				Union[str, Any]       =     self.d_model
        # Used in the tests to check the number of output hidden states/attentions
        _UpperCamelCase   :				List[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   :				Tuple       =     self.num_hidden_layers + 2

 def     __snake_case    (	self				:						str     )       ->							Any:
        _UpperCamelCase   :				Tuple       =     ids_tensor([self.batch_size, self.seq_length]				,	self.vocab_size     )

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

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

        _UpperCamelCase   :				str       =     None
        _UpperCamelCase   :				Optional[int]       =     None
        _UpperCamelCase   :				Dict       =     None
        if self.use_labels:
               _UpperCamelCase   :				str       =     ids_tensor([self.batch_size]				,	self.type_sequence_label_size     )
               _UpperCamelCase   :				List[Any]       =     ids_tensor([self.batch_size, self.seq_length]				,	self.num_labels     )
               _UpperCamelCase   :				int       =     ids_tensor([self.batch_size]				,	self.num_choices     )
               _UpperCamelCase   :				str       =     ids_tensor([self.batch_size, self.seq_length]				,	1     )

        _UpperCamelCase   :				Any       =     self.get_config()

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

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

        _UpperCamelCase   :				Optional[int]       =     False
        _UpperCamelCase   :				Dict       =     model(lowerCamelCase     )
        self.parent.assertEqual(result.last_hidden_state.shape				,	(self.batch_size, self.seq_length, self.d_model)     )

        _UpperCamelCase   :				str       =     False
        _UpperCamelCase   :				Dict       =     model(lowerCamelCase     )
        self.parent.assertEqual(result.last_hidden_state.shape				,	(self.batch_size, self.seq_length, self.d_model)     )

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

        _UpperCamelCase   :				Dict       =     False
        _UpperCamelCase   :				Optional[int]       =     model(lowerCamelCase     )
        self.parent.assertEqual(result.last_hidden_state.shape				,	(self.batch_size, 3, self.d_model)     )

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

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

 def     __snake_case    (	self				:						Optional[Any]				,	lowerCamelCase				:						str				,	lowerCamelCase				:						List[Any]				,	lowerCamelCase				:						List[str]				,	lowerCamelCase				:						Union[str, Any]				,	lowerCamelCase				:						Optional[Any]				,	lowerCamelCase				:						Any				,	lowerCamelCase				:						Dict				,	lowerCamelCase				:						Any				,	)       ->							Dict:
        _UpperCamelCase   :				Union[str, Any]       =     FunnelForMaskedLM(config=lowerCamelCase     )
        model.to(lowerCamelCase     )
        model.eval()
        _UpperCamelCase   :				Optional[int]       =     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     __snake_case    (	self				:						Optional[int]				,	lowerCamelCase				:						List[str]				,	lowerCamelCase				:						Optional[int]				,	lowerCamelCase				:						Union[str, Any]				,	lowerCamelCase				:						Optional[int]				,	lowerCamelCase				:						Optional[Any]				,	lowerCamelCase				:						Any				,	lowerCamelCase				:						Any				,	lowerCamelCase				:						Tuple				,	)       ->							List[Any]:
        _UpperCamelCase   :				Optional[Any]       =     self.num_labels
        _UpperCamelCase   :				str       =     FunnelForSequenceClassification(lowerCamelCase     )
        model.to(lowerCamelCase     )
        model.eval()
        _UpperCamelCase   :				Optional[Any]       =     model(lowerCamelCase				,	attention_mask=lowerCamelCase				,	token_type_ids=lowerCamelCase				,	labels=lowerCamelCase     )
        self.parent.assertEqual(result.logits.shape				,	(self.batch_size, self.num_labels)     )

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

 def     __snake_case    (	self				:						int				,	lowerCamelCase				:						int				,	lowerCamelCase				:						str				,	lowerCamelCase				:						Optional[int]				,	lowerCamelCase				:						Optional[Any]				,	lowerCamelCase				:						Optional[Any]				,	lowerCamelCase				:						List[Any]				,	lowerCamelCase				:						Optional[Any]				,	lowerCamelCase				:						int				,	)       ->							Union[str, Any]:
        _UpperCamelCase   :				Union[str, Any]       =     self.num_labels
        _UpperCamelCase   :				Optional[Any]       =     FunnelForTokenClassification(config=lowerCamelCase     )
        model.to(lowerCamelCase     )
        model.eval()
        _UpperCamelCase   :				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, self.num_labels)     )

 def     __snake_case    (	self				:						Optional[int]				,	lowerCamelCase				:						int				,	lowerCamelCase				:						int				,	lowerCamelCase				:						List[str]				,	lowerCamelCase				:						Dict				,	lowerCamelCase				:						int				,	lowerCamelCase				:						int				,	lowerCamelCase				:						int				,	lowerCamelCase				:						Optional[int]				,	)       ->							str:
        _UpperCamelCase   :				List[str]       =     FunnelForQuestionAnswering(config=lowerCamelCase     )
        model.to(lowerCamelCase     )
        model.eval()
        _UpperCamelCase   :				Union[str, Any]       =     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     __snake_case    (	self				:						Any     )       ->							Tuple:
        _UpperCamelCase   :				Any       =     self.prepare_config_and_inputs()
        (
            (
            _UpperCamelCase
        )  ,					(
            _UpperCamelCase
        )  ,					(
            _UpperCamelCase
        )  ,					(
            _UpperCamelCase
        )  ,					(
            _UpperCamelCase
        )  ,					(
            _UpperCamelCase
        )  ,					(
            _UpperCamelCase
        )  ,					(
            _UpperCamelCase
        )  ,					
        )   :				Union[str, Any]       =     config_and_inputs
        _UpperCamelCase   :				str       =     {'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  :		str 			=       False
 _lowerCamelCase  :		List[str] 			=       False
 _lowerCamelCase  :		Dict 			=       (
     (
         FunnelModel,
         FunnelForMaskedLM,
         FunnelForPreTraining,
         FunnelForQuestionAnswering,
         FunnelForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 _lowerCamelCase  :		List[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     __snake_case    (	self				:						Optional[int]				,	lowerCamelCase				:						Optional[Any]				,	lowerCamelCase				:						Optional[int]				,	lowerCamelCase				:						List[Any]=False     )       ->							Optional[int]:
        _UpperCamelCase   :				List[Any]       =     super()._prepare_for_class(lowerCamelCase				,	lowerCamelCase				,	return_labels=lowerCamelCase     )

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

 def     __snake_case    (	self				:						str     )       ->							Tuple:
        _UpperCamelCase   :				Optional[Any]       =     FunnelModelTester(self     )
        _UpperCamelCase   :				Optional[int]       =     ConfigTester(self				,	config_class=lowerCamelCase     )

 def     __snake_case    (	self				:						int     )       ->							str:
        self.config_tester.run_common_tests()

 def     __snake_case    (	self				:						str     )       ->							List[str]:
        _UpperCamelCase   :				Any       =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCamelCase     )

 def     __snake_case    (	self				:						int     )       ->							Any:
        _UpperCamelCase   :				Dict       =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_pretraining(*lowerCamelCase     )

 def     __snake_case    (	self				:						Optional[Any]     )       ->							Union[str, Any]:
        _UpperCamelCase   :				Tuple       =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase     )

 def     __snake_case    (	self				:						Any     )       ->							Optional[Any]:
        _UpperCamelCase   :				int       =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*lowerCamelCase     )

 def     __snake_case    (	self				:						str     )       ->							str:
        _UpperCamelCase   :				Any       =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*lowerCamelCase     )

 def     __snake_case    (	self				:						int				,	lowerCamelCase				:						Optional[int]     )       ->							Tuple:
        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:
                      _UpperCamelCase   :				str       =     getattr(lowerCamelCase				,	lowerCamelCase     )
                      weight.data.fill_(3     )







@require_torch
class       __lowerCAmelCase     (				a_,		unittest.TestCase  ):



 '''simple docstring'''
 _lowerCamelCase  :		Optional[Any] 			=       False
 _lowerCamelCase  :		List[Any] 			=       False
 _lowerCamelCase  :		List[Any] 			=       (
     (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
 )

 def     __snake_case    (	self				:						List[str]     )       ->							Optional[Any]:
        _UpperCamelCase   :				Optional[int]       =     FunnelModelTester(self				,	base=lowerCamelCase     )
        _UpperCamelCase   :				int       =     ConfigTester(self				,	config_class=lowerCamelCase     )

 def     __snake_case    (	self				:						str     )       ->							Union[str, Any]:
        self.config_tester.run_common_tests()

 def     __snake_case    (	self				:						Optional[Any]     )       ->							Optional[int]:
        _UpperCamelCase   :				Tuple       =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_base_model(*lowerCamelCase     )

 def     __snake_case    (	self				:						Dict     )       ->							Tuple:
        _UpperCamelCase   :				List[str]       =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase     )

 def     __snake_case    (	self				:						Union[str, Any]     )       ->							Optional[int]:
        _UpperCamelCase   :				List[Any]       =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase     )

 def     __snake_case    (	self				:						Optional[Any]     )       ->							List[str]:
        _UpperCamelCase  ,					_UpperCamelCase   :				List[str]       =     self.model_tester.prepare_config_and_inputs_for_common()
        _UpperCamelCase   :				Union[str, Any]       =     True

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

 def     __snake_case    (	self				:						Any				,	lowerCamelCase				:						str     )       ->							Tuple:
        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:
                      _UpperCamelCase   :				Optional[Any]       =     getattr(lowerCamelCase				,	lowerCamelCase     )
                      weight.data.fill_(3     )







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



 '''simple docstring'''

 def     __snake_case    (	self				:						Optional[int]     )       ->							Any:
        _UpperCamelCase   :				Tuple       =     1_3
        _UpperCamelCase   :				str       =     7
        _UpperCamelCase   :				List[Any]       =     torch.arange(0				,	batch_size * sequence_length     ).long().reshape(lowerCamelCase				,	lowerCamelCase     )
        _UpperCamelCase   :				Dict       =     [0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
        _UpperCamelCase   :				str       =     torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]     )

        _UpperCamelCase   :				Union[str, Any]       =     FunnelModel.from_pretrained('sgugger/funnel-random-tiny'     )
        _UpperCamelCase   :				int       =     model(lowerCamelCase				,	token_type_ids=lowerCamelCase     )[0].abs()

        _UpperCamelCase   :				Any       =     torch.tensor(2_3_4_4.8_3_5_2     )
        _UpperCamelCase   :				Optional[int]       =     torch.tensor(0.8_0_5_2     )
        self.assertTrue(torch.allclose(output.sum()				,	lowerCamelCase				,	atol=1e-4     )     )
        self.assertTrue(torch.allclose(output.mean()				,	lowerCamelCase				,	atol=1e-4     )     )

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

        _UpperCamelCase   :				List[str]       =     torch.tensor(2_3_4_3.8_4_2_5     )
        _UpperCamelCase   :				Optional[Any]       =     torch.tensor(0.8_0_4_9     )
        self.assertTrue(torch.allclose(output.sum()				,	lowerCamelCase				,	atol=1e-4     )     )
        self.assertTrue(torch.allclose(output.mean()				,	lowerCamelCase				,	atol=1e-4     )     )

 @slow
 def     __snake_case    (	self				:						int     )       ->							int:
        _UpperCamelCase   :				List[Any]       =     FunnelTokenizer.from_pretrained('huggingface/funnel-small'     )
        _UpperCamelCase   :				int       =     FunnelModel.from_pretrained('huggingface/funnel-small'     )
        _UpperCamelCase   :				Optional[int]       =     tokenizer('Hello! I am the Funnel Transformer model.'				,	return_tensors='pt'     )
        _UpperCamelCase   :				str       =     model(**lowerCamelCase     )[0]

        _UpperCamelCase   :				Optional[Any]       =     torch.tensor(2_3_5.7_2_4_6     )
        _UpperCamelCase   :				Optional[int]       =     torch.tensor(0.0_2_5_6     )
        self.assertTrue(torch.allclose(output.sum()				,	lowerCamelCase				,	atol=1e-4     )     )
        self.assertTrue(torch.allclose(output.mean()				,	lowerCamelCase				,	atol=1e-4     )     )




from typing import List

from ...processing_utils import ProcessorMixin
from ...utils import is_torch_available


if is_torch_available():
 import torch




class       __lowerCAmelCase     (				a_  ):



 '''simple docstring'''
 _lowerCamelCase  :		Dict 			=       ['image_processor', 'tokenizer']
 _lowerCamelCase  :		Tuple 			=       'OneFormerImageProcessor'
 _lowerCamelCase  :		Dict 			=       ('CLIPTokenizer', 'CLIPTokenizerFast')

 def __init__(	self				:						str				,	lowerCamelCase				:						Any=None				,	lowerCamelCase				:						List[Any]=None				,	lowerCamelCase				:						int = 7_7				,	lowerCamelCase				:						int = 7_7				,	**lowerCamelCase				:						Union[str, Any]     )       ->							Any:
        if image_processor is None:
               raise ValueError('You need to specify an `image_processor`.'     )
        if tokenizer is None:
               raise ValueError('You need to specify a `tokenizer`.'     )

        _UpperCamelCase   :				int       =     max_seq_length
        _UpperCamelCase   :				Optional[Any]       =     task_seq_length

        super().__init__(lowerCamelCase				,	lowerCamelCase     )

 def     __snake_case    (	self				:						List[str]				,	lowerCamelCase				:						int=None				,	lowerCamelCase				:						Optional[int]=7_7     )       ->							Tuple:
        if text_list is None:
               raise ValueError('tokens cannot be None.'     )

        _UpperCamelCase   :				Dict       =     self.tokenizer(lowerCamelCase				,	padding='max_length'				,	max_length=lowerCamelCase				,	truncation=lowerCamelCase     )

        _UpperCamelCase  ,					_UpperCamelCase   :				List[str]       =     tokens['attention_mask'], tokens['input_ids']

        _UpperCamelCase   :				Optional[int]       =     []
        for attn_mask, input_id in zip(lowerCamelCase				,	lowerCamelCase     ):
               _UpperCamelCase   :				int       =     torch.tensor(lowerCamelCase     ) * torch.tensor(lowerCamelCase     )
               token_inputs.append(token.unsqueeze(0     )     )

        _UpperCamelCase   :				Dict       =     torch.cat(lowerCamelCase				,	dim=0     )
        return token_inputs

 def __call__(	self				:						Union[str, Any]				,	lowerCamelCase				:						int=None				,	lowerCamelCase				:						str=None				,	lowerCamelCase				:						Any=None				,	**lowerCamelCase				:						List[Any]     )       ->							List[Any]:

        if task_inputs is None:
               raise ValueError('You have to specify the task_input. Found None.'     )
        elif images is None:
               raise ValueError('You have to specify the image. Found None.'     )

        if not all(task in ['semantic', 'instance', 'panoptic'] for task in task_inputs     ):
               raise ValueError('task_inputs must be semantic, instance, or panoptic.'     )

        _UpperCamelCase   :				Optional[Any]       =     self.image_processor(lowerCamelCase				,	lowerCamelCase				,	lowerCamelCase				,	**lowerCamelCase     )

        if isinstance(lowerCamelCase				,	lowerCamelCase     ):
               _UpperCamelCase   :				int       =     [task_inputs]

        if isinstance(lowerCamelCase				,	lowerCamelCase     ) and all(isinstance(lowerCamelCase				,	lowerCamelCase     ) for task_input in task_inputs     ):
               _UpperCamelCase   :				str       =     []
               for task in task_inputs:
                      _UpperCamelCase   :				Optional[Any]       =     f"the task is {task}"
                      task_token_inputs.append(lowerCamelCase     )
               _UpperCamelCase   :				Dict       =     self._preprocess_text(lowerCamelCase				,	max_length=self.task_seq_length     )
        else:
               raise TypeError('Task Inputs should be a string or a list of strings.'     )

        if hasattr(lowerCamelCase				,	'text_inputs'     ):
               _UpperCamelCase   :				str       =     encoded_inputs.text_inputs

               _UpperCamelCase   :				Optional[Any]       =     []
               for texts in texts_list:
                      _UpperCamelCase   :				Optional[int]       =     self._preprocess_text(lowerCamelCase				,	max_length=self.max_seq_length     )
                      text_inputs.append(text_input_list.unsqueeze(0     )     )

               _UpperCamelCase   :				Optional[int]       =     torch.cat(lowerCamelCase				,	dim=0     )

        return encoded_inputs

 def     __snake_case    (	self				:						List[str]				,	lowerCamelCase				:						str=None				,	lowerCamelCase				:						str=None				,	lowerCamelCase				:						int=None				,	**lowerCamelCase				:						List[str]     )       ->							Tuple:

        if task_inputs is None:
               raise ValueError('You have to specify the task_input. Found None.'     )
        elif images is None:
               raise ValueError('You have to specify the image. Found None.'     )

        if not all(task in ['semantic', 'instance', 'panoptic'] for task in task_inputs     ):
               raise ValueError('task_inputs must be semantic, instance, or panoptic.'     )

        _UpperCamelCase   :				Any       =     self.image_processor.encode_inputs(lowerCamelCase				,	lowerCamelCase				,	lowerCamelCase				,	**lowerCamelCase     )

        if isinstance(lowerCamelCase				,	lowerCamelCase     ):
               _UpperCamelCase   :				int       =     [task_inputs]

        if isinstance(lowerCamelCase				,	lowerCamelCase     ) and all(isinstance(lowerCamelCase				,	lowerCamelCase     ) for task_input in task_inputs     ):
               _UpperCamelCase   :				int       =     []
               for task in task_inputs:
                      _UpperCamelCase   :				List[str]       =     f"the task is {task}"
                      task_token_inputs.append(lowerCamelCase     )
               _UpperCamelCase   :				int       =     self._preprocess_text(lowerCamelCase				,	max_length=self.task_seq_length     )
        else:
               raise TypeError('Task Inputs should be a string or a list of strings.'     )

        if hasattr(lowerCamelCase				,	'text_inputs'     ):
               _UpperCamelCase   :				Optional[int]       =     encoded_inputs.text_inputs

               _UpperCamelCase   :				Any       =     []
               for texts in texts_list:
                      _UpperCamelCase   :				Any       =     self._preprocess_text(lowerCamelCase				,	max_length=self.max_seq_length     )
                      text_inputs.append(text_input_list.unsqueeze(0     )     )

               _UpperCamelCase   :				Optional[Any]       =     torch.cat(lowerCamelCase				,	dim=0     )

        return encoded_inputs

 def     __snake_case    (	self				:						Dict				,	*lowerCamelCase				:						Tuple				,	**lowerCamelCase				:						Any     )       ->							str:
        return self.image_processor.post_process_semantic_segmentation(*lowerCamelCase				,	**lowerCamelCase     )

 def     __snake_case    (	self				:						Union[str, Any]				,	*lowerCamelCase				:						Optional[int]				,	**lowerCamelCase				:						Any     )       ->							Any:
        return self.image_processor.post_process_instance_segmentation(*lowerCamelCase				,	**lowerCamelCase     )

 def     __snake_case    (	self				:						Dict				,	*lowerCamelCase				:						List[str]				,	**lowerCamelCase				:						int     )       ->							List[Any]:
        return self.image_processor.post_process_panoptic_segmentation(*lowerCamelCase				,	**lowerCamelCase     )



