


import unittest

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

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


if is_torch_available():
    import torch

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







class       _lowercase			:


       """simple docstring"""




       def __init__(       self							:      Optional[int] ,				__lowerCamelCase							:      Any ,				__lowerCamelCase							:      Any=13 ,				__lowerCamelCase							:      str=7 ,				__lowerCamelCase							:      str=True ,				__lowerCamelCase							:      int=True ,				__lowerCamelCase							:      List[Any]=True ,				__lowerCamelCase							:      List[Any]=True ,				__lowerCamelCase							:      Any=99 ,				__lowerCamelCase							:      Union[str, Any]=[1, 1, 2] ,				__lowerCamelCase							:      int=1 ,				__lowerCamelCase							:      Optional[Any]=32 ,				__lowerCamelCase							:      int=4 ,				__lowerCamelCase							:      Dict=8 ,				__lowerCamelCase							:      Any=37 ,				__lowerCamelCase							:      List[Any]="gelu_new" ,				__lowerCamelCase							:      List[Any]=0.1 ,				__lowerCamelCase							:      List[str]=0.1 ,				__lowerCamelCase							:      Union[str, Any]=0.0 ,				__lowerCamelCase							:      Optional[Any]=512 ,				__lowerCamelCase							:      int=3 ,				__lowerCamelCase							:      int=0.0_2 ,				__lowerCamelCase							:      Any=3 ,				__lowerCamelCase							:      List[str]=4 ,				__lowerCamelCase							:      List[str]=None ,				__lowerCamelCase							:      Optional[Any]=False ,				):


             '''simple docstring'''



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

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





       def 						lowerCAmelCase					(       self							:      List[Any]	):


             '''simple docstring'''



             lowerCamelCase__		:    List[Any]   =  ids_tensor([self.batch_size, self.seq_length] ,				self.vocab_size	)

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

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

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

             lowerCamelCase__		:    str   =  self.get_config()

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





       def 						lowerCAmelCase					(       self							:      Optional[Any]	):


             '''simple docstring'''



             return FunnelConfig(
                 vocab_size=self.vocab_size ,				block_sizes=self.block_sizes ,				num_decoder_layers=self.num_decoder_layers ,				d_model=self.d_model ,				n_head=self.n_head ,				d_head=self.d_head ,				d_inner=self.d_inner ,				hidden_act=self.hidden_act ,				hidden_dropout=self.hidden_dropout ,				attention_dropout=self.attention_dropout ,				activation_dropout=self.activation_dropout ,				max_position_embeddings=self.max_position_embeddings ,				type_vocab_size=self.type_vocab_size ,				initializer_std=self.initializer_std ,				)





       def 						lowerCAmelCase					(       self							:      Any ,				__lowerCamelCase							:      Union[str, Any] ,				__lowerCamelCase							:      List[Any] ,				__lowerCamelCase							:      Union[str, Any] ,				__lowerCamelCase							:      str ,				__lowerCamelCase							:      int ,				__lowerCamelCase							:      List[str] ,				__lowerCamelCase							:      int ,				__lowerCamelCase							:      Union[str, Any] ,				):


             '''simple docstring'''



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

             lowerCamelCase__		:    int   =  False
             lowerCamelCase__		:    str   =  model(__lowerCamelCase	)
             self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.d_model)	)

             lowerCamelCase__		:    Dict   =  False
             lowerCamelCase__		:    int   =  model(__lowerCamelCase	)
             self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.d_model)	)





       def 						lowerCAmelCase					(       self							:      List[str] ,				__lowerCamelCase							:      Any ,				__lowerCamelCase							:      Optional[Any] ,				__lowerCamelCase							:      Tuple ,				__lowerCamelCase							:      Dict ,				__lowerCamelCase							:      Tuple ,				__lowerCamelCase							:      str ,				__lowerCamelCase							:      List[Any] ,				__lowerCamelCase							:      Tuple ,				):


             '''simple docstring'''



             lowerCamelCase__		:    Tuple   =  FunnelBaseModel(config=__lowerCamelCase	)
             model.to(__lowerCamelCase	)
             model.eval()
             lowerCamelCase__		:    int   =  model(__lowerCamelCase ,				attention_mask=__lowerCamelCase ,				token_type_ids=__lowerCamelCase	)
             lowerCamelCase__		:    int   =  model(__lowerCamelCase ,				token_type_ids=__lowerCamelCase	)
             lowerCamelCase__		:    Union[str, Any]   =  model(__lowerCamelCase	)
             self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, 2, self.d_model)	)

             lowerCamelCase__		:    Any   =  False
             lowerCamelCase__		:    List[str]   =  model(__lowerCamelCase	)
             self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, 3, self.d_model)	)

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





       def 						lowerCAmelCase					(       self							:      Dict ,				__lowerCamelCase							:      Dict ,				__lowerCamelCase							:      Dict ,				__lowerCamelCase							:      List[str] ,				__lowerCamelCase							:      str ,				__lowerCamelCase							:      Any ,				__lowerCamelCase							:      Dict ,				__lowerCamelCase							:      int ,				__lowerCamelCase							:      List[str] ,				):


             '''simple docstring'''



             lowerCamelCase__		:    int   =  self.num_labels
             lowerCamelCase__		:    Optional[int]   =  FunnelForPreTraining(config=__lowerCamelCase	)
             model.to(__lowerCamelCase	)
             model.eval()
             lowerCamelCase__		:    List[str]   =  model(__lowerCamelCase ,				attention_mask=__lowerCamelCase ,				token_type_ids=__lowerCamelCase ,				labels=__lowerCamelCase	)
             self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length)	)





       def 						lowerCAmelCase					(       self							:      str ,				__lowerCamelCase							:      Dict ,				__lowerCamelCase							:      List[str] ,				__lowerCamelCase							:      Any ,				__lowerCamelCase							:      int ,				__lowerCamelCase							:      List[str] ,				__lowerCamelCase							:      Union[str, Any] ,				__lowerCamelCase							:      Optional[int] ,				__lowerCamelCase							:      List[str] ,				):


             '''simple docstring'''



             lowerCamelCase__		:    Union[str, Any]   =  FunnelForMaskedLM(config=__lowerCamelCase	)
             model.to(__lowerCamelCase	)
             model.eval()
             lowerCamelCase__		:    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 						lowerCAmelCase					(       self							:      Optional[int] ,				__lowerCamelCase							:      Optional[Any] ,				__lowerCamelCase							:      Optional[int] ,				__lowerCamelCase							:      List[Any] ,				__lowerCamelCase							:      Optional[int] ,				__lowerCamelCase							:      Optional[int] ,				__lowerCamelCase							:      Tuple ,				__lowerCamelCase							:      int ,				__lowerCamelCase							:      Dict ,				):


             '''simple docstring'''



             lowerCamelCase__		:    Tuple   =  self.num_labels
             lowerCamelCase__		:    Union[str, Any]   =  FunnelForSequenceClassification(__lowerCamelCase	)
             model.to(__lowerCamelCase	)
             model.eval()
             lowerCamelCase__		:    Tuple   =  model(__lowerCamelCase ,				attention_mask=__lowerCamelCase ,				token_type_ids=__lowerCamelCase ,				labels=__lowerCamelCase	)
             self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_labels)	)





       def 						lowerCAmelCase					(       self							:      Dict ,				__lowerCamelCase							:      int ,				__lowerCamelCase							:      List[str] ,				__lowerCamelCase							:      Optional[Any] ,				__lowerCamelCase							:      int ,				__lowerCamelCase							:      Optional[Any] ,				__lowerCamelCase							:      Any ,				__lowerCamelCase							:      Optional[int] ,				__lowerCamelCase							:      Dict ,				):


             '''simple docstring'''



             lowerCamelCase__		:    int   =  self.num_choices
             lowerCamelCase__		:    Union[str, Any]   =  FunnelForMultipleChoice(config=__lowerCamelCase	)
             model.to(__lowerCamelCase	)
             model.eval()
             lowerCamelCase__		:    Dict   =  input_ids.unsqueeze(1	).expand(-1 ,				self.num_choices ,				-1	).contiguous()
             lowerCamelCase__		:    str   =  token_type_ids.unsqueeze(1	).expand(-1 ,				self.num_choices ,				-1	).contiguous()
             lowerCamelCase__		:    str   =  input_mask.unsqueeze(1	).expand(-1 ,				self.num_choices ,				-1	).contiguous()
             lowerCamelCase__		:    Optional[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 						lowerCAmelCase					(       self							:      Optional[int] ,				__lowerCamelCase							:      Dict ,				__lowerCamelCase							:      Dict ,				__lowerCamelCase							:      Any ,				__lowerCamelCase							:      str ,				__lowerCamelCase							:      Optional[Any] ,				__lowerCamelCase							:      int ,				__lowerCamelCase							:      Any ,				__lowerCamelCase							:      Union[str, Any] ,				):


             '''simple docstring'''



             lowerCamelCase__		:    Tuple   =  self.num_labels
             lowerCamelCase__		:    str   =  FunnelForTokenClassification(config=__lowerCamelCase	)
             model.to(__lowerCamelCase	)
             model.eval()
             lowerCamelCase__		:    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.num_labels)	)





       def 						lowerCAmelCase					(       self							:      Dict ,				__lowerCamelCase							:      Dict ,				__lowerCamelCase							:      Optional[Any] ,				__lowerCamelCase							:      str ,				__lowerCamelCase							:      Dict ,				__lowerCamelCase							:      Optional[Any] ,				__lowerCamelCase							:      Any ,				__lowerCamelCase							:      Optional[int] ,				__lowerCamelCase							:      List[Any] ,				):


             '''simple docstring'''



             lowerCamelCase__		:    Dict   =  FunnelForQuestionAnswering(config=__lowerCamelCase	)
             model.to(__lowerCamelCase	)
             model.eval()
             lowerCamelCase__		:    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 						lowerCAmelCase					(       self							:      List[str]	):


             '''simple docstring'''



             lowerCamelCase__		:    Optional[Any]   =  self.prepare_config_and_inputs()
             (
                 (
                 lowerCamelCase__
             )   ,       (
                 lowerCamelCase__
             )   ,       (
                 lowerCamelCase__
             )   ,       (
                 lowerCamelCase__
             )   ,       (
                 lowerCamelCase__
             )   ,       (
                 lowerCamelCase__
             )   ,       (
                 lowerCamelCase__
             )   ,       (
                 lowerCamelCase__
             )   ,       
             )		:    Tuple   =  config_and_inputs
             lowerCamelCase__		:    Union[str, Any]   =  {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
             return config, inputs_dict







@require_torch
class       _lowercase			(							lowercase__							,  lowercase__							,  unittest.TestCase):


       """simple docstring"""




       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 						lowerCAmelCase					(       self							:      Tuple ,				__lowerCamelCase							:      Optional[Any] ,				__lowerCamelCase							:      Union[str, Any] ,				__lowerCamelCase							:      int=False	):


             '''simple docstring'''



             lowerCamelCase__		:    int   =  super()._prepare_for_class(__lowerCamelCase ,				__lowerCamelCase ,				return_labels=__lowerCamelCase	)

             if return_labels:
                   if model_class in get_values(__lowerCamelCase	):
                         lowerCamelCase__		:    Optional[int]   =  torch.zeros(
                             (self.model_tester.batch_size, self.model_tester.seq_length) ,				dtype=torch.long ,				device=__lowerCamelCase	)
             return inputs_dict





       def 						lowerCAmelCase					(       self							:      Optional[Any]	):


             '''simple docstring'''



             lowerCamelCase__		:    Optional[Any]   =  FunnelModelTester(self	)
             lowerCamelCase__		:    Optional[int]   =  ConfigTester(self ,				config_class=__lowerCamelCase	)





       def 						lowerCAmelCase					(       self							:      Tuple	):


             '''simple docstring'''



             self.config_tester.run_common_tests()





       def 						lowerCAmelCase					(       self							:      str	):


             '''simple docstring'''



             lowerCamelCase__		:    Any   =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*__lowerCamelCase	)





       def 						lowerCAmelCase					(       self							:      str	):


             '''simple docstring'''



             lowerCamelCase__		:    List[str]   =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_pretraining(*__lowerCamelCase	)





       def 						lowerCAmelCase					(       self							:      Union[str, Any]	):


             '''simple docstring'''



             lowerCamelCase__		:    str   =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_lm(*__lowerCamelCase	)





       def 						lowerCAmelCase					(       self							:      Any	):


             '''simple docstring'''



             lowerCamelCase__		:    List[str]   =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase	)





       def 						lowerCAmelCase					(       self							:      str	):


             '''simple docstring'''



             lowerCamelCase__		:    Any   =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase	)





       def 						lowerCAmelCase					(       self							:      str ,				__lowerCamelCase							:      Optional[Any]	):


             '''simple docstring'''



             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:
                         lowerCamelCase__		:    Any   =  getattr(__lowerCamelCase ,				__lowerCamelCase	)
                         weight.data.fill_(3	)







@require_torch
class       _lowercase			(							lowercase__							,  unittest.TestCase):


       """simple docstring"""




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





       def 						lowerCAmelCase					(       self							:      Tuple	):


             '''simple docstring'''



             lowerCamelCase__		:    Dict   =  FunnelModelTester(self ,				base=__lowerCamelCase	)
             lowerCamelCase__		:    Dict   =  ConfigTester(self ,				config_class=__lowerCamelCase	)





       def 						lowerCAmelCase					(       self							:      Dict	):


             '''simple docstring'''



             self.config_tester.run_common_tests()





       def 						lowerCAmelCase					(       self							:      List[Any]	):


             '''simple docstring'''



             lowerCamelCase__		:    Dict   =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_base_model(*__lowerCamelCase	)





       def 						lowerCAmelCase					(       self							:      Tuple	):


             '''simple docstring'''



             lowerCamelCase__		:    Dict   =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase	)





       def 						lowerCAmelCase					(       self							:      Any	):


             '''simple docstring'''



             lowerCamelCase__		:    str   =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_multiple_choice(*__lowerCamelCase	)





       def 						lowerCAmelCase					(       self							:      int	):


             '''simple docstring'''



             lowerCamelCase__   ,       lowerCamelCase__		:    List[str]   =  self.model_tester.prepare_config_and_inputs_for_common()
             lowerCamelCase__		:    Any   =  True

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





       def 						lowerCAmelCase					(       self							:      Optional[Any] ,				__lowerCamelCase							:      Dict	):


             '''simple docstring'''



             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:
                         lowerCamelCase__		:    Dict   =  getattr(__lowerCamelCase ,				__lowerCamelCase	)
                         weight.data.fill_(3	)







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


       """simple docstring"""




       def 						lowerCAmelCase					(       self							:      Optional[Any]	):


             '''simple docstring'''



             lowerCamelCase__		:    Union[str, Any]   =  13
             lowerCamelCase__		:    List[str]   =  7
             lowerCamelCase__		:    List[str]   =  torch.arange(0 ,				batch_size * sequence_length	).long().reshape(__lowerCamelCase ,				__lowerCamelCase	)
             lowerCamelCase__		:    List[Any]   =  [0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
             lowerCamelCase__		:    List[str]   =  torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]	)

             lowerCamelCase__		:    Optional[int]   =  FunnelModel.from_pretrained("sgugger/funnel-random-tiny"	)
             lowerCamelCase__		:    str   =  model(__lowerCamelCase ,				token_type_ids=__lowerCamelCase	)[0].abs()

             lowerCamelCase__		:    Any   =  torch.tensor(2_3_4_4.8_3_5_2	)
             lowerCamelCase__		:    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	)	)

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

             lowerCamelCase__		:    str   =  torch.tensor(2_3_4_3.8_4_2_5	)
             lowerCamelCase__		:    Dict   =  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 						lowerCAmelCase					(       self							:      Union[str, Any]	):


             '''simple docstring'''



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

             lowerCamelCase__		:    List[str]   =  torch.tensor(2_3_5.7_2_4_6	)
             lowerCamelCase__		:    Optional[Any]   =  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 Any, Callable, Dict, List, Optional, Union

import PIL.Image
import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionImgaImgPipeline,
    StableDiffusionInpaintPipelineLegacy,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.configuration_utils import FrozenDict
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
from diffusers.utils import deprecate, logging


A				:   str		= logging.get_logger(__name__)  # pylint: disable=invalid-name







class       _lowercase			(							lowercase__):


       """simple docstring"""




       A__ 						=      ["safety_checker", "feature_extractor"]





       def __init__(       self							:      Optional[Any] ,				__lowerCamelCase							:      AutoencoderKL ,				__lowerCamelCase							:      CLIPTextModel ,				__lowerCamelCase							:      CLIPTokenizer ,				__lowerCamelCase							:      UNetaDConditionModel ,				__lowerCamelCase							:      Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] ,				__lowerCamelCase							:      StableDiffusionSafetyChecker ,				__lowerCamelCase							:      CLIPImageProcessor ,				__lowerCamelCase							:      bool = True ,				):


             '''simple docstring'''



             super().__init__()
             if hasattr(scheduler.config ,				"steps_offset"	) and scheduler.config.steps_offset != 1:
                   lowerCamelCase__		:    List[str]   =  (
                       f"The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`"
                       f" should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure "
                       "to update the config accordingly as leaving `steps_offset` might led to incorrect results"
                       " in future versions. If you have downloaded this checkpoint from the Hugging Face Hub,"
                       " it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`"
                       " file"
                   )
                   deprecate("steps_offset!=1" ,				"1.0.0" ,				__lowerCamelCase ,				standard_warn=__lowerCamelCase	)
                   lowerCamelCase__		:    List[str]   =  dict(scheduler.config	)
                   lowerCamelCase__		:    Optional[Any]   =  1
                   lowerCamelCase__		:    Union[str, Any]   =  FrozenDict(__lowerCamelCase	)

             self.register_modules(
                 vae=__lowerCamelCase ,				text_encoder=__lowerCamelCase ,				tokenizer=__lowerCamelCase ,				unet=__lowerCamelCase ,				scheduler=__lowerCamelCase ,				safety_checker=__lowerCamelCase ,				feature_extractor=__lowerCamelCase ,				)
             self.register_to_config(requires_safety_checker=__lowerCamelCase	)





       @property
       def 						lowerCAmelCase					(       self							:      List[Any]	):


             '''simple docstring'''



             return {k: getattr(self ,				__lowerCamelCase	) for k in self.config.keys() if not k.startswith("_"	)}





       def 						lowerCAmelCase					(       self							:      List[Any] ,				__lowerCamelCase							:      Optional[Union[str, int]] = "auto"	):


             '''simple docstring'''



             if slice_size == "auto":
                   # half the attention head size is usually a good trade-off between
                   # speed and memory
                   lowerCamelCase__		:    Tuple   =  self.unet.config.attention_head_dim // 2
             self.unet.set_attention_slice(__lowerCamelCase	)





       def 						lowerCAmelCase					(       self							:      Optional[Any]	):


             '''simple docstring'''



             self.enable_attention_slicing(__lowerCamelCase	)





       @torch.no_grad()
       def 						lowerCAmelCase					(       self							:      Any ,				__lowerCamelCase							:      Union[str, List[str]] ,				__lowerCamelCase							:      Union[torch.FloatTensor, PIL.Image.Image] ,				__lowerCamelCase							:      Union[torch.FloatTensor, PIL.Image.Image] ,				__lowerCamelCase							:      float = 0.8 ,				__lowerCamelCase							:      Optional[int] = 50 ,				__lowerCamelCase							:      Optional[float] = 7.5 ,				__lowerCamelCase							:      Optional[Union[str, List[str]]] = None ,				__lowerCamelCase							:      Optional[int] = 1 ,				__lowerCamelCase							:      Optional[float] = 0.0 ,				__lowerCamelCase							:      Optional[torch.Generator] = None ,				__lowerCamelCase							:      Optional[str] = "pil" ,				__lowerCamelCase							:      bool = True ,				__lowerCamelCase							:      Optional[Callable[[int, int, torch.FloatTensor], None]] = None ,				__lowerCamelCase							:      int = 1 ,				):


             '''simple docstring'''



             return StableDiffusionInpaintPipelineLegacy(**self.components	)(
                 prompt=__lowerCamelCase ,				image=__lowerCamelCase ,				mask_image=__lowerCamelCase ,				strength=__lowerCamelCase ,				num_inference_steps=__lowerCamelCase ,				guidance_scale=__lowerCamelCase ,				negative_prompt=__lowerCamelCase ,				num_images_per_prompt=__lowerCamelCase ,				eta=__lowerCamelCase ,				generator=__lowerCamelCase ,				output_type=__lowerCamelCase ,				return_dict=__lowerCamelCase ,				callback=__lowerCamelCase ,				)





       @torch.no_grad()
       def 						lowerCAmelCase					(       self							:      Optional[int] ,				__lowerCamelCase							:      Union[str, List[str]] ,				__lowerCamelCase							:      Union[torch.FloatTensor, PIL.Image.Image] ,				__lowerCamelCase							:      float = 0.8 ,				__lowerCamelCase							:      Optional[int] = 50 ,				__lowerCamelCase							:      Optional[float] = 7.5 ,				__lowerCamelCase							:      Optional[Union[str, List[str]]] = None ,				__lowerCamelCase							:      Optional[int] = 1 ,				__lowerCamelCase							:      Optional[float] = 0.0 ,				__lowerCamelCase							:      Optional[torch.Generator] = None ,				__lowerCamelCase							:      Optional[str] = "pil" ,				__lowerCamelCase							:      bool = True ,				__lowerCamelCase							:      Optional[Callable[[int, int, torch.FloatTensor], None]] = None ,				__lowerCamelCase							:      int = 1 ,				**__lowerCamelCase							:      List[str] ,				):


             '''simple docstring'''



             return StableDiffusionImgaImgPipeline(**self.components	)(
                 prompt=__lowerCamelCase ,				image=__lowerCamelCase ,				strength=__lowerCamelCase ,				num_inference_steps=__lowerCamelCase ,				guidance_scale=__lowerCamelCase ,				negative_prompt=__lowerCamelCase ,				num_images_per_prompt=__lowerCamelCase ,				eta=__lowerCamelCase ,				generator=__lowerCamelCase ,				output_type=__lowerCamelCase ,				return_dict=__lowerCamelCase ,				callback=__lowerCamelCase ,				callback_steps=__lowerCamelCase ,				)





       @torch.no_grad()
       def 						lowerCAmelCase					(       self							:      List[Any] ,				__lowerCamelCase							:      Union[str, List[str]] ,				__lowerCamelCase							:      int = 512 ,				__lowerCamelCase							:      int = 512 ,				__lowerCamelCase							:      int = 50 ,				__lowerCamelCase							:      float = 7.5 ,				__lowerCamelCase							:      Optional[Union[str, List[str]]] = None ,				__lowerCamelCase							:      Optional[int] = 1 ,				__lowerCamelCase							:      float = 0.0 ,				__lowerCamelCase							:      Optional[torch.Generator] = None ,				__lowerCamelCase							:      Optional[torch.FloatTensor] = None ,				__lowerCamelCase							:      Optional[str] = "pil" ,				__lowerCamelCase							:      bool = True ,				__lowerCamelCase							:      Optional[Callable[[int, int, torch.FloatTensor], None]] = None ,				__lowerCamelCase							:      int = 1 ,				):


             '''simple docstring'''



             return StableDiffusionPipeline(**self.components	)(
                 prompt=__lowerCamelCase ,				height=__lowerCamelCase ,				width=__lowerCamelCase ,				num_inference_steps=__lowerCamelCase ,				guidance_scale=__lowerCamelCase ,				negative_prompt=__lowerCamelCase ,				num_images_per_prompt=__lowerCamelCase ,				eta=__lowerCamelCase ,				generator=__lowerCamelCase ,				latents=__lowerCamelCase ,				output_type=__lowerCamelCase ,				return_dict=__lowerCamelCase ,				callback=__lowerCamelCase ,				callback_steps=__lowerCamelCase ,				)


