

'''simple docstring'''



from __future__ import annotations

import unittest

from transformers import FunnelConfig, is_tf_available
from transformers.testing_utils import require_tf

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers import (
         TFFunnelBaseModel,
         TFFunnelForMaskedLM,
         TFFunnelForMultipleChoice,
         TFFunnelForPreTraining,
         TFFunnelForQuestionAnswering,
         TFFunnelForSequenceClassification,
         TFFunnelForTokenClassification,
         TFFunnelModel,
     )



class 				_snake_case					:

  def __init__(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE=13      , _SCREAMING_SNAKE_CASE=7      , _SCREAMING_SNAKE_CASE=True      , _SCREAMING_SNAKE_CASE=True      , _SCREAMING_SNAKE_CASE=True      , _SCREAMING_SNAKE_CASE=True      , _SCREAMING_SNAKE_CASE=99      , _SCREAMING_SNAKE_CASE=[1, 1, 2]      , _SCREAMING_SNAKE_CASE=1      , _SCREAMING_SNAKE_CASE=32      , _SCREAMING_SNAKE_CASE=4      , _SCREAMING_SNAKE_CASE=8      , _SCREAMING_SNAKE_CASE=37      , _SCREAMING_SNAKE_CASE="gelu_new"      , _SCREAMING_SNAKE_CASE=0.1      , _SCREAMING_SNAKE_CASE=0.1      , _SCREAMING_SNAKE_CASE=0.0      , _SCREAMING_SNAKE_CASE=5_12      , _SCREAMING_SNAKE_CASE=3      , _SCREAMING_SNAKE_CASE=0.02      , _SCREAMING_SNAKE_CASE=3      , _SCREAMING_SNAKE_CASE=4      , _SCREAMING_SNAKE_CASE=None      , _SCREAMING_SNAKE_CASE=False      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							parent
      lowerCAmelCase 						=							batch_size
      lowerCAmelCase 						=							seq_length
      lowerCAmelCase 						=							is_training
      lowerCAmelCase 						=							use_input_mask
      lowerCAmelCase 						=							use_token_type_ids
      lowerCAmelCase 						=							use_labels
      lowerCAmelCase 						=							vocab_size
      lowerCAmelCase 						=							block_sizes
      lowerCAmelCase 						=							num_decoder_layers
      lowerCAmelCase 						=							d_model
      lowerCAmelCase 						=							n_head
      lowerCAmelCase 						=							d_head
      lowerCAmelCase 						=							d_inner
      lowerCAmelCase 						=							hidden_act
      lowerCAmelCase 						=							hidden_dropout
      lowerCAmelCase 						=							attention_dropout
      lowerCAmelCase 						=							activation_dropout
      lowerCAmelCase 						=							max_position_embeddings
      lowerCAmelCase 						=							type_vocab_size
      lowerCAmelCase 						=							2
      lowerCAmelCase 						=							num_labels
      lowerCAmelCase 						=							num_choices
      lowerCAmelCase 						=							scope
      lowerCAmelCase 						=							initializer_std

      # Used in the tests to check the size of the first attention layer
      lowerCAmelCase 						=							n_head
      # Used in the tests to check the size of the first hidden state
      lowerCAmelCase 						=							self.d_model
      # Used in the tests to check the number of output hidden states/attentions
      lowerCAmelCase 						=							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 						=							self.num_hidden_layers + 2

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							ids_tensor([self.batch_size, self.seq_length]      , self.vocab_size	)

      lowerCAmelCase 						=							None
      if self.use_input_mask:
          lowerCAmelCase 						=							random_attention_mask([self.batch_size, self.seq_length]	)

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

      lowerCAmelCase 						=							None
      lowerCAmelCase 						=							None
      lowerCAmelCase 						=							None
      if self.use_labels:
          lowerCAmelCase 						=							ids_tensor([self.batch_size]      , self.type_sequence_label_size	)
          lowerCAmelCase 						=							ids_tensor([self.batch_size, self.seq_length]      , self.num_labels	)
          lowerCAmelCase 						=							ids_tensor([self.batch_size]      , self.num_choices	)

      lowerCAmelCase 						=							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      , )

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

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							TFFunnelModel(config=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)

      lowerCAmelCase 						=							[input_ids, input_mask]
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)

      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)
      self.parent.assertEqual(result.last_hidden_state.shape      , (self.batch_size, self.seq_length, self.d_model)	)

      lowerCAmelCase 						=							False
      lowerCAmelCase 						=							TFFunnelModel(config=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)
      self.parent.assertEqual(result.last_hidden_state.shape      , (self.batch_size, self.seq_length, self.d_model)	)

      lowerCAmelCase 						=							False
      lowerCAmelCase 						=							TFFunnelModel(config=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)
      self.parent.assertEqual(result.last_hidden_state.shape      , (self.batch_size, self.seq_length, self.d_model)	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							TFFunnelBaseModel(config=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)

      lowerCAmelCase 						=							[input_ids, input_mask]
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)

      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)
      self.parent.assertEqual(result.last_hidden_state.shape      , (self.batch_size, 2, self.d_model)	)

      lowerCAmelCase 						=							False
      lowerCAmelCase 						=							TFFunnelBaseModel(config=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)
      self.parent.assertEqual(result.last_hidden_state.shape      , (self.batch_size, 3, self.d_model)	)

      lowerCAmelCase 						=							False
      lowerCAmelCase 						=							TFFunnelBaseModel(config=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)
      self.parent.assertEqual(result.last_hidden_state.shape      , (self.batch_size, 2, self.d_model)	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							TFFunnelForPreTraining(config=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)
      self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.seq_length)	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							TFFunnelForMaskedLM(config=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)
      self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.seq_length, self.vocab_size)	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							self.num_labels
      lowerCAmelCase 						=							TFFunnelForSequenceClassification(config=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)
      self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.num_labels)	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							self.num_choices
      lowerCAmelCase 						=							TFFunnelForMultipleChoice(config=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE      , 1	)      , (1, self.num_choices, 1)	)
      lowerCAmelCase 						=							tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE      , 1	)      , (1, self.num_choices, 1)	)
      lowerCAmelCase 						=							tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE      , 1	)      , (1, self.num_choices, 1)	)
      lowerCAmelCase 						=							{
          'input_ids': multiple_choice_inputs_ids,
          'attention_mask': multiple_choice_input_mask,
          'token_type_ids': multiple_choice_token_type_ids,
      }
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)
      self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.num_choices)	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							self.num_labels
      lowerCAmelCase 						=							TFFunnelForTokenClassification(config=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)
      self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.seq_length, self.num_labels)	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							TFFunnelForQuestionAnswering(config=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)
      self.parent.assertEqual(result.start_logits.shape      , (self.batch_size, self.seq_length)	)
      self.parent.assertEqual(result.end_logits.shape      , (self.batch_size, self.seq_length)	)


  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.prepare_config_and_inputs()
      (
          (
          lowerCAmelCase
      )						,   (
          lowerCAmelCase
      )						,   (
          lowerCAmelCase
      )						,   (
          lowerCAmelCase
      )						,   (
          lowerCAmelCase
      )						,   (
          lowerCAmelCase
      )						,   (
          lowerCAmelCase
      )						,   
      ) 						=							config_and_inputs
      lowerCAmelCase 						=							{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
      return config, inputs_dict



@require_tf
class 				_snake_case					(   a_     , a_     , unittest.TestCase			):
  SCREAMING_SNAKE_CASE			:       int				=     (
      (
          TFFunnelModel,
          TFFunnelForMaskedLM,
          TFFunnelForPreTraining,
          TFFunnelForQuestionAnswering,
          TFFunnelForTokenClassification,
      )
      if is_tf_available()
      else ()
  )
  SCREAMING_SNAKE_CASE			:       Optional[int]				=     (
      {
          '''feature-extraction''': (TFFunnelBaseModel, TFFunnelModel),
          '''fill-mask''': TFFunnelForMaskedLM,
          '''question-answering''': TFFunnelForQuestionAnswering,
          '''text-classification''': TFFunnelForSequenceClassification,
          '''token-classification''': TFFunnelForTokenClassification,
          '''zero-shot''': TFFunnelForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  SCREAMING_SNAKE_CASE			:       Tuple				=     False
  SCREAMING_SNAKE_CASE			:       List[Any]				=     False

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							TFFunnelModelTester(self	)
      lowerCAmelCase 						=							ConfigTester(self      , config_class=_SCREAMING_SNAKE_CASE	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      self.config_tester.run_common_tests()

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_pretraining(*_SCREAMING_SNAKE_CASE	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE	)


  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE	)



@require_tf
class 				_snake_case					(   a_     , unittest.TestCase			):
  SCREAMING_SNAKE_CASE			:       List[Any]				=     (
      (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
  )
  SCREAMING_SNAKE_CASE			:       List[str]				=     False
  SCREAMING_SNAKE_CASE			:       Optional[Any]				=     False

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							TFFunnelModelTester(self      , base=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							ConfigTester(self      , config_class=_SCREAMING_SNAKE_CASE	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      self.config_tester.run_common_tests()

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_base_model(*_SCREAMING_SNAKE_CASE	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE	)


  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_multiple_choice(*_SCREAMING_SNAKE_CASE	)








'''simple docstring'''



from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
     from PIL import Image

     from ..image_utils import load_image

if is_tf_available():
     import tensorflow as tf

     from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
     from ..tf_utils import stable_softmax

if is_torch_available():
     from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING

_UpperCamelCase						:  Optional[int]							       =				logging.get_logger(__name__)



@add_end_docstrings(a_			)
class 				_snake_case					(   a_			):

  def __init__(				self      , *_SCREAMING_SNAKE_CASE      , **_SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      super().__init__(*_SCREAMING_SNAKE_CASE      , **_SCREAMING_SNAKE_CASE	)
      requires_backends(self      , 'vision'	)
      self.check_model_type(
          TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
          if self.framework == 'tf'
          else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE=None	):





      '''simple docstring'''

      lowerCAmelCase 						=							{}
      if top_k is not None:
          lowerCAmelCase 						=							top_k
      return {}, {}, postprocess_params

  def __call__(				self      , _SCREAMING_SNAKE_CASE      , **_SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      return super().__call__(_SCREAMING_SNAKE_CASE      , **_SCREAMING_SNAKE_CASE	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      lowerCAmelCase 						=							load_image(_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							self.image_processor(images=_SCREAMING_SNAKE_CASE      , return_tensors=self.framework	)
      return model_inputs

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model(**_SCREAMING_SNAKE_CASE	)
      return model_outputs


  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE=5	):





      '''simple docstring'''

      if top_k > self.model.config.num_labels:
          lowerCAmelCase 						=							self.model.config.num_labels

      if self.framework == "pt":
          lowerCAmelCase 						=							model_outputs.logits.softmax(-1	)[0]
          lowerCAmelCase						,   lowerCAmelCase 						=							probs.topk(_SCREAMING_SNAKE_CASE	)
      elif self.framework == "tf":
          lowerCAmelCase 						=							stable_softmax(model_outputs.logits      , axis=-1	)[0]
          lowerCAmelCase 						=							tf.math.top_k(_SCREAMING_SNAKE_CASE      , k=_SCREAMING_SNAKE_CASE	)
          lowerCAmelCase						,   lowerCAmelCase 						=							topk.values.numpy(), topk.indices.numpy()
      else:
          raise ValueError(F'Unsupported framework: {self.framework}'	)

      lowerCAmelCase 						=							scores.tolist()
      lowerCAmelCase 						=							ids.tolist()
      return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	)]





