




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							:






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






       def        SCREAMING_SNAKE_CASE     ( self:       Optional[Any]					):
         lowercase     :Tuple   						=    ids_tensor([self.batch_size, self.seq_length]							,       self.vocab_size					)

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

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

         lowercase     :str   						=    None
         lowercase     :str   						=    None
         lowercase     :Tuple   						=    None
         if self.use_labels:
           lowercase     :Tuple   						=    ids_tensor([self.batch_size]							,       self.type_sequence_label_size					)
           lowercase     :List[str]   						=    ids_tensor([self.batch_size, self.seq_length]							,       self.num_labels					)
           lowercase     :Any   						=    ids_tensor([self.batch_size]							,       self.num_choices					)
           lowercase     :Any   						=    ids_tensor([self.batch_size, self.seq_length]							,       1					)

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

         lowercase     :str   						=    False
         lowercase     :Union[str, Any]   						=    model(_lowerCAmelCase					)
         self.parent.assertEqual(result.last_hidden_state.shape							,       (self.batch_size, self.seq_length, self.d_model)					)

         lowercase     :Union[str, Any]   						=    False
         lowercase     :Optional[int]   						=    model(_lowerCAmelCase					)
         self.parent.assertEqual(result.last_hidden_state.shape							,       (self.batch_size, self.seq_length, self.d_model)					)






       def        SCREAMING_SNAKE_CASE     ( self:       Union[str, Any]							,       _lowerCAmelCase:       Optional[int]							,       _lowerCAmelCase:       str							,       _lowerCAmelCase:       List[str]							,       _lowerCAmelCase:       Tuple							,       _lowerCAmelCase:       Tuple							,       _lowerCAmelCase:       Union[str, Any]							,       _lowerCAmelCase:       Any							,       _lowerCAmelCase:       Dict							,       ):
         lowercase     :Dict   						=    FunnelBaseModel(config=_lowerCAmelCase					)
         model.to(_lowerCAmelCase					)
         model.eval()
         lowercase     :List[str]   						=    model(_lowerCAmelCase							,       attention_mask=_lowerCAmelCase							,       token_type_ids=_lowerCAmelCase					)
         lowercase     :int   						=    model(_lowerCAmelCase							,       token_type_ids=_lowerCAmelCase					)
         lowercase     :Optional[Any]   						=    model(_lowerCAmelCase					)
         self.parent.assertEqual(result.last_hidden_state.shape							,       (self.batch_size, 2, self.d_model)					)

         lowercase     :Dict   						=    False
         lowercase     :List[Any]   						=    model(_lowerCAmelCase					)
         self.parent.assertEqual(result.last_hidden_state.shape							,       (self.batch_size, 3, self.d_model)					)

         lowercase     :Tuple   						=    False
         lowercase     :Optional[Any]   						=    model(_lowerCAmelCase					)
         self.parent.assertEqual(result.last_hidden_state.shape							,       (self.batch_size, 2, self.d_model)					)






       def        SCREAMING_SNAKE_CASE     ( self:       List[Any]							,       _lowerCAmelCase:       Optional[Any]							,       _lowerCAmelCase:       List[str]							,       _lowerCAmelCase:       str							,       _lowerCAmelCase:       Tuple							,       _lowerCAmelCase:       Tuple							,       _lowerCAmelCase:       str							,       _lowerCAmelCase:       Dict							,       _lowerCAmelCase:       int							,       ):
         lowercase     :str   						=    self.num_labels
         lowercase     :List[Any]   						=    FunnelForPreTraining(config=_lowerCAmelCase					)
         model.to(_lowerCAmelCase					)
         model.eval()
         lowercase     :Dict   						=    model(_lowerCAmelCase							,       attention_mask=_lowerCAmelCase							,       token_type_ids=_lowerCAmelCase							,       labels=_lowerCAmelCase					)
         self.parent.assertEqual(result.logits.shape							,       (self.batch_size, self.seq_length)					)






       def        SCREAMING_SNAKE_CASE     ( self:       List[Any]							,       _lowerCAmelCase:       Tuple							,       _lowerCAmelCase:       Optional[int]							,       _lowerCAmelCase:       Optional[int]							,       _lowerCAmelCase:       int							,       _lowerCAmelCase:       Tuple							,       _lowerCAmelCase:       List[Any]							,       _lowerCAmelCase:       Dict							,       _lowerCAmelCase:       Tuple							,       ):
         lowercase     :List[Any]   						=    FunnelForMaskedLM(config=_lowerCAmelCase					)
         model.to(_lowerCAmelCase					)
         model.eval()
         lowercase     :Optional[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.vocab_size)					)






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






       def        SCREAMING_SNAKE_CASE     ( self:       List[Any]							,       _lowerCAmelCase:       Optional[int]							,       _lowerCAmelCase:       Any							,       _lowerCAmelCase:       int							,       _lowerCAmelCase:       int							,       _lowerCAmelCase:       List[Any]							,       _lowerCAmelCase:       Any							,       _lowerCAmelCase:       List[str]							,       _lowerCAmelCase:       Tuple							,       ):
         lowercase     :int   						=    self.num_choices
         lowercase     :Optional[Any]   						=    FunnelForMultipleChoice(config=_lowerCAmelCase					)
         model.to(_lowerCAmelCase					)
         model.eval()
         lowercase     :List[Any]   						=    input_ids.unsqueeze(1					).expand(-1							,       self.num_choices							,       -1					).contiguous()
         lowercase     :Optional[int]   						=    token_type_ids.unsqueeze(1					).expand(-1							,       self.num_choices							,       -1					).contiguous()
         lowercase     :Optional[int]   						=    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        SCREAMING_SNAKE_CASE     ( self:       Tuple							,       _lowerCAmelCase:       Dict							,       _lowerCAmelCase:       Tuple							,       _lowerCAmelCase:       List[str]							,       _lowerCAmelCase:       List[str]							,       _lowerCAmelCase:       Optional[int]							,       _lowerCAmelCase:       List[str]							,       _lowerCAmelCase:       List[Any]							,       _lowerCAmelCase:       Dict							,       ):
         lowercase     :Union[str, Any]   						=    self.num_labels
         lowercase     :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        SCREAMING_SNAKE_CASE     ( self:       List[Any]							,       _lowerCAmelCase:       Union[str, Any]							,       _lowerCAmelCase:       int							,       _lowerCAmelCase:       str							,       _lowerCAmelCase:       Any							,       _lowerCAmelCase:       List[Any]							,       _lowerCAmelCase:       Tuple							,       _lowerCAmelCase:       str							,       _lowerCAmelCase:       Tuple							,       ):
         lowercase     :Any   						=    FunnelForQuestionAnswering(config=_lowerCAmelCase					)
         model.to(_lowerCAmelCase					)
         model.eval()
         lowercase     :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        SCREAMING_SNAKE_CASE     ( self:       int					):
         lowercase     :int   						=    self.prepare_config_and_inputs()
         (
             (
             lowercase
         )						,     (
             lowercase
         )						,     (
             lowercase
         )						,     (
             lowercase
         )						,     (
             lowercase
         )						,     (
             lowercase
         )						,     (
             lowercase
         )						,     (
             lowercase
         )						,     
         )     :Optional[Any]   						=    config_and_inputs
         lowercase     :Union[str, Any]   						=    {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
         return config, inputs_dict







@require_torch
class  __lowerCAmelCase							(				lowerCAmelCase   ,			lowerCAmelCase   ,			unittest.TestCase):
       _a			       =    False
       _a			       =    False
       _a			       =    (
           (
               FunnelModel,
               FunnelForMaskedLM,
               FunnelForPreTraining,
               FunnelForQuestionAnswering,
               FunnelForTokenClassification,
           )
           if is_torch_available()
           else ()
       )
       _a			       =    (
           {
               '''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:       Optional[Any]							,       _lowerCAmelCase:       List[Any]							,       _lowerCAmelCase:       Tuple							,       _lowerCAmelCase:       Optional[Any]=False					):
         lowercase     :List[str]   						=    super()._prepare_for_class(_lowerCAmelCase							,       _lowerCAmelCase							,       return_labels=_lowerCAmelCase					)

         if return_labels:
           if model_class in get_values(_lowerCAmelCase					):
             lowercase     :Union[str, Any]   						=    torch.zeros(
                 (self.model_tester.batch_size, self.model_tester.seq_length)							,       dtype=torch.long							,       device=_lowerCAmelCase					)
         return inputs_dict






       def        SCREAMING_SNAKE_CASE     ( self:       Optional[Any]					):
         lowercase     :Optional[Any]   						=    FunnelModelTester(self					)
         lowercase     :Optional[Any]   						=    ConfigTester(self							,       config_class=_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       List[str]					):
         self.config_tester.run_common_tests()






       def        SCREAMING_SNAKE_CASE     ( self:       List[Any]					):
         lowercase     :Union[str, Any]   						=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       Dict					):
         lowercase     :Optional[int]   						=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_pretraining(*_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       int					):
         lowercase     :str   						=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       Tuple					):
         lowercase     :Union[str, Any]   						=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       Optional[Any]					):
         lowercase     :Optional[Any]   						=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       Union[str, Any]							,       _lowerCAmelCase:       Dict					):
         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     :Tuple   						=    getattr(_lowerCAmelCase							,       _lowerCAmelCase					)
             weight.data.fill_(3					)







@require_torch
class  __lowerCAmelCase							(				lowerCAmelCase   ,			unittest.TestCase):
       _a			       =    False
       _a			       =    False
       _a			       =    (
           (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
       )






       def        SCREAMING_SNAKE_CASE     ( self:       Union[str, Any]					):
         lowercase     :Dict   						=    FunnelModelTester(self							,       base=_lowerCAmelCase					)
         lowercase     :List[str]   						=    ConfigTester(self							,       config_class=_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       Any					):
         self.config_tester.run_common_tests()






       def        SCREAMING_SNAKE_CASE     ( self:       List[Any]					):
         lowercase     :Dict   						=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_base_model(*_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       str					):
         lowercase     :List[str]   						=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       List[Any]					):
         lowercase     :int   						=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       Tuple					):
         lowercase						,     lowercase     :List[str]   						=    self.model_tester.prepare_config_and_inputs_for_common()
         lowercase     :Any   						=    True

         for model_class in self.all_model_classes:
           if model_class.__name__ == "FunnelBaseModel":
             continue
           lowercase     :Tuple   						=    model_class(_lowerCAmelCase					)
           model.to(_lowerCAmelCase					)
           model.train()
           lowercase     :Optional[int]   						=    self._prepare_for_class(_lowerCAmelCase							,       _lowerCAmelCase							,       return_labels=_lowerCAmelCase					)
           lowercase     :str   						=    model(**_lowerCAmelCase					).loss
           loss.backward()






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







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






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

         lowercase     :int   						=    FunnelModel.from_pretrained("sgugger/funnel-random-tiny"					)
         lowercase     :List[str]   						=    model(_lowerCAmelCase							,       token_type_ids=_lowerCAmelCase					)[0].abs()

         lowercase     :Dict   						=    torch.tensor(23_44.83_52					)
         lowercase     :Optional[Any]   						=    torch.tensor(0.80_52					)
         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     :Dict   						=    torch.tensor(0.80_49					)
         self.assertTrue(torch.allclose(output.sum()							,       _lowerCAmelCase							,       atol=1e-4					)					)
         self.assertTrue(torch.allclose(output.mean()							,       _lowerCAmelCase							,       atol=1e-4					)					)






       @slow
       def        SCREAMING_SNAKE_CASE     ( self:       Union[str, Any]					):
         lowercase     :Tuple   						=    FunnelTokenizer.from_pretrained("huggingface/funnel-small"					)
         lowercase     :Any   						=    FunnelModel.from_pretrained("huggingface/funnel-small"					)
         lowercase     :Optional[Any]   						=    tokenizer("Hello! I am the Funnel Transformer model."							,       return_tensors="pt"					)
         lowercase     :Optional[int]   						=    model(**_lowerCAmelCase					)[0]

         lowercase     :Optional[int]   						=    torch.tensor(2_35.72_46					)
         lowercase     :str   						=    torch.tensor(0.02_56					)
         self.assertTrue(torch.allclose(output.sum()							,       _lowerCAmelCase							,       atol=1e-4					)					)
         self.assertTrue(torch.allclose(output.mean()							,       _lowerCAmelCase							,       atol=1e-4					)					)









import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
     from PIL import Image




class  __lowerCAmelCase							(				unittest.TestCase   ,			lowerCAmelCase):






       def        SCREAMING_SNAKE_CASE     ( self:       Dict					):
         lowercase     :Any   						=    load_tool("image-segmentation"					)
         self.tool.setup()
         lowercase     :int   						=    load_tool("image-segmentation"							,       remote=_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       str					):
         lowercase     :Any   						=    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"					)					) / "000000039769.png"					)
         lowercase     :Dict   						=    self.tool(_lowerCAmelCase							,       "cat"					)
         self.assertTrue(isinstance(_lowerCAmelCase							,       Image.Image					)					)






       def        SCREAMING_SNAKE_CASE     ( self:       Union[str, Any]					):
         lowercase     :Dict   						=    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"					)					) / "000000039769.png"					)
         lowercase     :List[Any]   						=    self.remote_tool(_lowerCAmelCase							,       "cat"					)
         self.assertTrue(isinstance(_lowerCAmelCase							,       Image.Image					)					)






       def        SCREAMING_SNAKE_CASE     ( self:       List[Any]					):
         lowercase     :Optional[Any]   						=    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"					)					) / "000000039769.png"					)
         lowercase     :Dict   						=    self.tool(image=_lowerCAmelCase							,       label="cat"					)
         self.assertTrue(isinstance(_lowerCAmelCase							,       Image.Image					)					)






       def        SCREAMING_SNAKE_CASE     ( self:       Tuple					):
         lowercase     :int   						=    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"					)					) / "000000039769.png"					)
         lowercase     :List[str]   						=    self.remote_tool(image=_lowerCAmelCase							,       label="cat"					)
         self.assertTrue(isinstance(_lowerCAmelCase							,       Image.Image					)					)



