





'''simple docstring'''






from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

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 (
        TFConvBertForMaskedLM,
        TFConvBertForMultipleChoice,
        TFConvBertForQuestionAnswering,
        TFConvBertForSequenceClassification,
        TFConvBertForTokenClassification,
        TFConvBertModel,
    )
class 					__snake_case  :






   '''simple docstring'''





   def __init__(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE=1_3	,		__SCREAMING_SNAKE_CASE=7	,		__SCREAMING_SNAKE_CASE=True	,		__SCREAMING_SNAKE_CASE=True	,		__SCREAMING_SNAKE_CASE=True	,		__SCREAMING_SNAKE_CASE=True	,		__SCREAMING_SNAKE_CASE=9_9	,		__SCREAMING_SNAKE_CASE=3_2	,		__SCREAMING_SNAKE_CASE=2	,		__SCREAMING_SNAKE_CASE=4	,		__SCREAMING_SNAKE_CASE=3_7	,		__SCREAMING_SNAKE_CASE="gelu"	,		__SCREAMING_SNAKE_CASE=0.1	,		__SCREAMING_SNAKE_CASE=0.1	,		__SCREAMING_SNAKE_CASE=5_1_2	,		__SCREAMING_SNAKE_CASE=1_6	,		__SCREAMING_SNAKE_CASE=2	,		__SCREAMING_SNAKE_CASE=0.02	,		__SCREAMING_SNAKE_CASE=3	,		__SCREAMING_SNAKE_CASE=4	,		__SCREAMING_SNAKE_CASE=None	,		):
    snake_case__     :					str    =  parent
    snake_case__     :					Dict    =  1_3
    snake_case__     :					Any    =  7
    snake_case__     :					Optional[int]    =  True
    snake_case__     :					Optional[Any]    =  True
    snake_case__     :					Optional[int]    =  True
    snake_case__     :					str    =  True
    snake_case__     :					Optional[Any]    =  9_9
    snake_case__     :					Dict    =  3_8_4
    snake_case__     :					Optional[Any]    =  2
    snake_case__     :					List[str]    =  4
    snake_case__     :					Union[str, Any]    =  3_7
    snake_case__     :					Optional[Any]    =  """gelu"""
    snake_case__     :					Optional[Any]    =  0.1
    snake_case__     :					Optional[Any]    =  0.1
    snake_case__     :					List[Any]    =  5_1_2
    snake_case__     :					Tuple    =  1_6
    snake_case__     :					Tuple    =  2
    snake_case__     :					List[str]    =  0.02
    snake_case__     :					Tuple    =  3
    snake_case__     :					List[str]    =  4
    snake_case__     :					Optional[Any]    =  1_2_8
    snake_case__     :					str    =  2
    snake_case__     :					Union[str, Any]    =  9
    snake_case__     :					Optional[int]    =  1
    snake_case__     :					List[Any]    =  None

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Optional[int]    =  ids_tensor([self.batch_size, self.seq_length]	,		self.vocab_size       )

    snake_case__     :					int    =  None
    if self.use_input_mask:
     snake_case__     :					Any    =  random_attention_mask([self.batch_size, self.seq_length]       )

    snake_case__     :					List[Any]    =  None
    if self.use_token_type_ids:
     snake_case__     :					Dict    =  ids_tensor([self.batch_size, self.seq_length]	,		self.type_vocab_size       )

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

    snake_case__     :					Union[str, Any]    =  ConvBertConfig(
        vocab_size=self.vocab_size	,		hidden_size=self.hidden_size	,		num_hidden_layers=self.num_hidden_layers	,		num_attention_heads=self.num_attention_heads	,		intermediate_size=self.intermediate_size	,		hidden_act=self.hidden_act	,		hidden_dropout_prob=self.hidden_dropout_prob	,		attention_probs_dropout_prob=self.attention_probs_dropout_prob	,		max_position_embeddings=self.max_position_embeddings	,		type_vocab_size=self.type_vocab_size	,		initializer_range=self.initializer_range	,		return_dict=__SCREAMING_SNAKE_CASE	,		)

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

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					int    =  TFConvBertModel(config=__SCREAMING_SNAKE_CASE       )
    snake_case__     :					Any    =  {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

    snake_case__     :					int    =  [input_ids, input_mask]
    snake_case__     :					Any    =  model(__SCREAMING_SNAKE_CASE       )

    snake_case__     :					Optional[Any]    =  model(__SCREAMING_SNAKE_CASE       )

    self.parent.assertEqual(result.last_hidden_state.shape	,		(self.batch_size, self.seq_length, self.hidden_size)       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					str    =  TFConvBertForMaskedLM(config=__SCREAMING_SNAKE_CASE       )
    snake_case__     :					str    =  {
        """input_ids""": input_ids,
        """attention_mask""": input_mask,
        """token_type_ids""": token_type_ids,
    }
    snake_case__     :					Tuple    =  model(__SCREAMING_SNAKE_CASE       )
    self.parent.assertEqual(result.logits.shape	,		(self.batch_size, self.seq_length, self.vocab_size)       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					Dict    =  self.num_labels
    snake_case__     :					int    =  TFConvBertForSequenceClassification(config=__SCREAMING_SNAKE_CASE       )
    snake_case__     :					List[str]    =  {
        """input_ids""": input_ids,
        """attention_mask""": input_mask,
        """token_type_ids""": token_type_ids,
    }

    snake_case__     :					List[Any]    =  model(__SCREAMING_SNAKE_CASE       )
    self.parent.assertEqual(result.logits.shape	,		(self.batch_size, self.num_labels)       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					str    =  self.num_choices
    snake_case__     :					List[Any]    =  TFConvBertForMultipleChoice(config=__SCREAMING_SNAKE_CASE       )
    snake_case__     :					List[str]    =  tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE	,		1       )	,		(1, self.num_choices, 1)       )
    snake_case__     :					List[Any]    =  tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE	,		1       )	,		(1, self.num_choices, 1)       )
    snake_case__     :					List[Any]    =  tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE	,		1       )	,		(1, self.num_choices, 1)       )
    snake_case__     :					Tuple    =  {
        """input_ids""": multiple_choice_inputs_ids,
        """attention_mask""": multiple_choice_input_mask,
        """token_type_ids""": multiple_choice_token_type_ids,
    }
    snake_case__     :					Optional[int]    =  model(__SCREAMING_SNAKE_CASE       )
    self.parent.assertEqual(result.logits.shape	,		(self.batch_size, self.num_choices)       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					str    =  self.num_labels
    snake_case__     :					List[Any]    =  TFConvBertForTokenClassification(config=__SCREAMING_SNAKE_CASE       )
    snake_case__     :					Dict    =  {
        """input_ids""": input_ids,
        """attention_mask""": input_mask,
        """token_type_ids""": token_type_ids,
    }
    snake_case__     :					str    =  model(__SCREAMING_SNAKE_CASE       )
    self.parent.assertEqual(result.logits.shape	,		(self.batch_size, self.seq_length, self.num_labels)       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					List[Any]    =  TFConvBertForQuestionAnswering(config=__SCREAMING_SNAKE_CASE       )
    snake_case__     :					Any    =  {
        """input_ids""": input_ids,
        """attention_mask""": input_mask,
        """token_type_ids""": token_type_ids,
    }

    snake_case__     :					Any    =  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 		__UpperCamelCase						(							self       ):
    snake_case__     :					Union[str, Any]    =  self.prepare_config_and_inputs()
    (
        (
        snake_case__
    )  ,							(
        snake_case__
    )  ,							(
        snake_case__
    )  ,							(
        snake_case__
    )  ,							(
        snake_case__
    )  ,							(
        snake_case__
    )  ,							(
        snake_case__
    )  ,							
    )     :					Union[str, Any]    =  config_and_inputs
    snake_case__     :					Any    =  {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
    return config, inputs_dict



@require_tf
class 					__snake_case  (					__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE		,				unittest.TestCase							):






   '''simple docstring'''





   lowerCamelCase__    							=      (
       (
           TFConvBertModel,
           TFConvBertForMaskedLM,
           TFConvBertForQuestionAnswering,
           TFConvBertForSequenceClassification,
           TFConvBertForTokenClassification,
           TFConvBertForMultipleChoice,
       )
       if is_tf_available()
       else ()
   )
   lowerCamelCase__    							=      (
       {
           '''feature-extraction''': TFConvBertModel,
           '''fill-mask''': TFConvBertForMaskedLM,
           '''question-answering''': TFConvBertForQuestionAnswering,
           '''text-classification''': TFConvBertForSequenceClassification,
           '''token-classification''': TFConvBertForTokenClassification,
           '''zero-shot''': TFConvBertForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   lowerCamelCase__    							=      False
   lowerCamelCase__    							=      False
   lowerCamelCase__    							=      False

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Dict    =  TFConvBertModelTester(self       )
    snake_case__     :					Dict    =  ConfigTester(self	,		config_class=__SCREAMING_SNAKE_CASE	,		hidden_size=3_7       )

   def 		__UpperCamelCase						(							self       ):
    self.config_tester.run_common_tests()

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Optional[int]    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE       )

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					List[str]    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE       )

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Dict    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_multiple_choice(*__SCREAMING_SNAKE_CASE       )

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					str    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE       )

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Dict    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE       )

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Optional[Any]    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE       )

   @slow
   def 		__UpperCamelCase						(							self       ):
    snake_case__  ,							snake_case__     :					List[Any]    =  self.model_tester.prepare_config_and_inputs_for_common()
    snake_case__     :					Union[str, Any]    =  True
    snake_case__     :					List[Any]    =  True

    if hasattr(__SCREAMING_SNAKE_CASE	,		"""use_cache"""       ):
     snake_case__     :					Optional[Any]    =  True

    snake_case__     :					str    =  getattr(self.model_tester	,		"""encoder_seq_length"""	,		self.model_tester.seq_length       )
    snake_case__     :					Dict    =  getattr(self.model_tester	,		"""key_length"""	,		__SCREAMING_SNAKE_CASE       )

    for model_class in self.all_model_classes:
     snake_case__     :					Tuple    =  self._prepare_for_class(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       )
     snake_case__     :					Dict    =  model_class(__SCREAMING_SNAKE_CASE       )
     snake_case__     :					Any    =  len(model(__SCREAMING_SNAKE_CASE       )       )

     with tempfile.TemporaryDirectory() as tmpdirname:
      model.save_pretrained(__SCREAMING_SNAKE_CASE	,		saved_model=__SCREAMING_SNAKE_CASE       )
      snake_case__     :					List[str]    =  os.path.join(__SCREAMING_SNAKE_CASE	,		"""saved_model"""	,		"""1"""       )
      snake_case__     :					List[str]    =  tf.keras.models.load_model(__SCREAMING_SNAKE_CASE       )
      snake_case__     :					List[Any]    =  model(__SCREAMING_SNAKE_CASE       )

      if self.is_encoder_decoder:
       snake_case__     :					Optional[int]    =  outputs["""encoder_hidden_states"""]
       snake_case__     :					str    =  outputs["""encoder_attentions"""]
      else:
       snake_case__     :					List[str]    =  outputs["""hidden_states"""]
       snake_case__     :					Tuple    =  outputs["""attentions"""]

      self.assertEqual(len(__SCREAMING_SNAKE_CASE       )	,		__SCREAMING_SNAKE_CASE       )

      snake_case__     :					Optional[Any]    =  getattr(
          self.model_tester	,		"""expected_num_hidden_layers"""	,		self.model_tester.num_hidden_layers + 1       )

      self.assertEqual(len(__SCREAMING_SNAKE_CASE       )	,		__SCREAMING_SNAKE_CASE       )
      self.assertListEqual(
          list(output_hidden_states[0].shape[-2:]       )	,		[self.model_tester.seq_length, self.model_tester.hidden_size]	,		)

      self.assertEqual(len(__SCREAMING_SNAKE_CASE       )	,		self.model_tester.num_hidden_layers       )
      self.assertListEqual(
          list(output_attentions[0].shape[-3:]       )	,		[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]	,		)

   @slow
   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Tuple    =  TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""       )
    self.assertIsNotNone(__SCREAMING_SNAKE_CASE       )


   def 		__UpperCamelCase						(							self       ):
    snake_case__  ,							snake_case__     :					List[str]    =  self.model_tester.prepare_config_and_inputs_for_common()
    snake_case__     :					Optional[Any]    =  True
    snake_case__     :					Dict    =  getattr(self.model_tester	,		"""decoder_seq_length"""	,		self.model_tester.seq_length       )
    snake_case__     :					Optional[Any]    =  getattr(self.model_tester	,		"""encoder_seq_length"""	,		self.model_tester.seq_length       )
    snake_case__     :					List[str]    =  getattr(self.model_tester	,		"""key_length"""	,		__SCREAMING_SNAKE_CASE       )
    snake_case__     :					Union[str, Any]    =  getattr(self.model_tester	,		"""key_length"""	,		__SCREAMING_SNAKE_CASE       )

    def check_decoder_attentions_output(__SCREAMING_SNAKE_CASE       ):
     snake_case__     :					Union[str, Any]    =  len(__SCREAMING_SNAKE_CASE       )
     self.assertEqual(out_len % 2	,		0       )
     snake_case__     :					int    =  outputs.decoder_attentions
     self.assertEqual(len(__SCREAMING_SNAKE_CASE       )	,		self.model_tester.num_hidden_layers       )
     self.assertListEqual(
         list(decoder_attentions[0].shape[-3:]       )	,		[self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]	,		)

    def check_encoder_attentions_output(__SCREAMING_SNAKE_CASE       ):
     snake_case__     :					List[str]    =  [
         t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
     ]
     self.assertEqual(len(__SCREAMING_SNAKE_CASE       )	,		self.model_tester.num_hidden_layers       )
     self.assertListEqual(
         list(attentions[0].shape[-3:]       )	,		[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]	,		)

    for model_class in self.all_model_classes:
     snake_case__     :					Optional[Any]    =  True
     snake_case__     :					Dict    =  False
     snake_case__     :					Tuple    =  model_class(__SCREAMING_SNAKE_CASE       )
     snake_case__     :					Any    =  model(self._prepare_for_class(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       )       )
     snake_case__     :					Optional[Any]    =  len(__SCREAMING_SNAKE_CASE       )
     self.assertEqual(config.output_hidden_states	,		__SCREAMING_SNAKE_CASE       )
     check_encoder_attentions_output(__SCREAMING_SNAKE_CASE       )

     if self.is_encoder_decoder:
      snake_case__     :					str    =  model_class(__SCREAMING_SNAKE_CASE       )
      snake_case__     :					List[Any]    =  model(self._prepare_for_class(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       )       )
      self.assertEqual(config.output_hidden_states	,		__SCREAMING_SNAKE_CASE       )
      check_decoder_attentions_output(__SCREAMING_SNAKE_CASE       )

     # Check that output attentions can also be changed via the config
     del inputs_dict["output_attentions"]
     snake_case__     :					str    =  True
     snake_case__     :					List[str]    =  model_class(__SCREAMING_SNAKE_CASE       )
     snake_case__     :					str    =  model(self._prepare_for_class(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       )       )
     self.assertEqual(config.output_hidden_states	,		__SCREAMING_SNAKE_CASE       )
     check_encoder_attentions_output(__SCREAMING_SNAKE_CASE       )

     # Check attention is always last and order is fine
     snake_case__     :					List[Any]    =  True
     snake_case__     :					Any    =  True
     snake_case__     :					List[Any]    =  model_class(__SCREAMING_SNAKE_CASE       )
     snake_case__     :					Union[str, Any]    =  model(self._prepare_for_class(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       )       )

     self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)	,		len(__SCREAMING_SNAKE_CASE       )       )
     self.assertEqual(model.config.output_hidden_states	,		__SCREAMING_SNAKE_CASE       )
     check_encoder_attentions_output(__SCREAMING_SNAKE_CASE       )



@require_tf
class 					__snake_case  (					unittest.TestCase							):






   '''simple docstring'''





   @slow
   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					List[str]    =  TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""       )
    snake_case__     :					Any    =  tf.constant([[0, 1, 2, 3, 4, 5]]       )
    snake_case__     :					int    =  model(__SCREAMING_SNAKE_CASE       )[0]

    snake_case__     :					Any    =  [1, 6, 7_6_8]
    self.assertEqual(output.shape	,		__SCREAMING_SNAKE_CASE       )

    snake_case__     :					Any    =  tf.constant(
        [
            [
                [-0.0347_5493, -0.468_6034, -0.3063_8832],
                [0.2263_7248, -0.2698_8646, -0.742_3424],
                [0.1032_4868, -0.4501_3508, -0.5828_0784],
            ]
        ]       )
    tf.debugging.assert_near(output[:, :3, :3]	,		__SCREAMING_SNAKE_CASE	,		atol=1e-4       )








"""simple docstring"""


import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
	from PIL import Image
else:
	class     _UpperCamelCase    :
								'''simple docstring'''







								@staticmethod
								def      snake_case							(	*__a       ,  **__a      ):
											pass
@is_pipeline_test
@require_torch
@require_vision
class     _UpperCamelCase    (  unittest.TestCase						):
							'''simple docstring'''







							__UpperCAmelCase		:       str							 =MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     pipeline("visual-question-answering"       ,  model="hf-internal-testing/tiny-vilt-random-vqa"      )
										__lowerCAmelCase							       =     [
										    {
										        "image": Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      ),
										        "question": "How many cats are there?",
										    },
										    {
										        "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
										        "question": "How many cats are there?",
										    },
										]
										return vqa_pipeline, examples

							def      snake_case							(	self       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     vqa_pipeline(__a       ,  top_k=1      )
										self.assertEqual(
										    __a       ,  [
										        [{"score": ANY(__a      ), "answer": ANY(__a      )}],
										        [{"score": ANY(__a      ), "answer": ANY(__a      )}],
										    ]       ,  )

							@require_torch
							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     pipeline("visual-question-answering"       ,  model="hf-internal-testing/tiny-vilt-random-vqa"      )
										__lowerCAmelCase							       =     "./tests/fixtures/tests_samples/COCO/000000039769.png"
										__lowerCAmelCase							       =     "How many cats are there?"

										__lowerCAmelCase							       =     vqa_pipeline(image=__a       ,  question="How many cats are there?"       ,  top_k=2      )
										self.assertEqual(
										    __a       ,  [{"score": ANY(__a      ), "answer": ANY(__a      )}, {"score": ANY(__a      ), "answer": ANY(__a      )}]      )

										__lowerCAmelCase							       =     vqa_pipeline({"image": image, "question": question}       ,  top_k=2      )
										self.assertEqual(
										    __a       ,  [{"score": ANY(__a      ), "answer": ANY(__a      )}, {"score": ANY(__a      ), "answer": ANY(__a      )}]      )

							@slow
							@require_torch
							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     pipeline("visual-question-answering"       ,  model="dandelin/vilt-b32-finetuned-vqa"      )
										__lowerCAmelCase							       =     "./tests/fixtures/tests_samples/COCO/000000039769.png"
										__lowerCAmelCase							       =     "How many cats are there?"

										__lowerCAmelCase							       =     vqa_pipeline(image=__a       ,  question=__a       ,  top_k=2      )
										self.assertEqual(
										    nested_simplify(__a       ,  decimals=4      )       ,  [{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}]      )

										__lowerCAmelCase							       =     vqa_pipeline({"image": image, "question": question}       ,  top_k=2      )
										self.assertEqual(
										    nested_simplify(__a       ,  decimals=4      )       ,  [{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}]      )

										__lowerCAmelCase							       =     vqa_pipeline(
										    [{"image": image, "question": question}, {"image": image, "question": question}]       ,  top_k=2      )
										self.assertEqual(
										    nested_simplify(__a       ,  decimals=4      )       ,  [[{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}]] * 2       ,  )



							@require_tf
							@unittest.skip("Visual question answering not implemented in TF"      )
							def      snake_case							(	self      ):
										pass




