

'''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     a			:






											"""simple docstring"""




											@staticmethod
											def       __magic_name__							(							*snake_case_					:      Any			,			**snake_case_					:      Dict  ):



													'''simple docstring'''



													pass




@is_pipeline_test
@require_torch
@require_vision
class     a			(				unittest.TestCase       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING







						def       __magic_name__							(							self					:      Dict			,			snake_case_					:      Optional[int]			,			snake_case_					:      Any			,			snake_case_					:      Optional[int]  ):



								'''simple docstring'''



								snake_case__						:       Optional[Any]       						=  pipeline('''visual-question-answering'''			,			model='''hf-internal-testing/tiny-vilt-random-vqa'''  )
								snake_case__						:       List[str]       						=  [
								    {
								        '''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       __magic_name__							(							self					:      Optional[Any]			,			snake_case_					:      Optional[int]			,			snake_case_					:      Optional[int]  ):



								'''simple docstring'''



								snake_case__						:       Tuple       						=  vqa_pipeline(snake_case_			,			top_k=1  )
								self.assertEqual(
								    snake_case_			,			[
								        [{'''score''': ANY(snake_case_  ), '''answer''': ANY(snake_case_  )}],
								        [{'''score''': ANY(snake_case_  ), '''answer''': ANY(snake_case_  )}],
								    ]			,			)







						@require_torch
						def       __magic_name__							(							self					:      Optional[Any]  ):



								'''simple docstring'''



								snake_case__						:       Any       						=  pipeline('''visual-question-answering'''			,			model='''hf-internal-testing/tiny-vilt-random-vqa'''  )
								snake_case__						:       Tuple       						=  '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
								snake_case__						:       Tuple       						=  '''How many cats are there?'''

								snake_case__						:       str       						=  vqa_pipeline(image=snake_case_			,			question='''How many cats are there?'''			,			top_k=2  )
								self.assertEqual(
								    snake_case_			,			[{'''score''': ANY(snake_case_  ), '''answer''': ANY(snake_case_  )}, {'''score''': ANY(snake_case_  ), '''answer''': ANY(snake_case_  )}]  )

								snake_case__						:       List[Any]       						=  vqa_pipeline({'''image''': image, '''question''': question}			,			top_k=2  )
								self.assertEqual(
								    snake_case_			,			[{'''score''': ANY(snake_case_  ), '''answer''': ANY(snake_case_  )}, {'''score''': ANY(snake_case_  ), '''answer''': ANY(snake_case_  )}]  )







						@slow
						@require_torch
						def       __magic_name__							(							self					:      Any  ):



								'''simple docstring'''



								snake_case__						:       Dict       						=  pipeline('''visual-question-answering'''			,			model='''dandelin/vilt-b32-finetuned-vqa'''  )
								snake_case__						:       Tuple       						=  '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
								snake_case__						:       Dict       						=  '''How many cats are there?'''

								snake_case__						:       int       						=  vqa_pipeline(image=snake_case_			,			question=snake_case_			,			top_k=2  )
								self.assertEqual(
								    nested_simplify(snake_case_			,			decimals=4  )			,			[{'''score''': 0.8_7_9_9, '''answer''': '''2'''}, {'''score''': 0.2_9_6, '''answer''': '''1'''}]  )

								snake_case__						:       Union[str, Any]       						=  vqa_pipeline({'''image''': image, '''question''': question}			,			top_k=2  )
								self.assertEqual(
								    nested_simplify(snake_case_			,			decimals=4  )			,			[{'''score''': 0.8_7_9_9, '''answer''': '''2'''}, {'''score''': 0.2_9_6, '''answer''': '''1'''}]  )

								snake_case__						:       Any       						=  vqa_pipeline(
								    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]			,			top_k=2  )
								self.assertEqual(
								    nested_simplify(snake_case_			,			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       __magic_name__							(							self					:      int  ):



								'''simple docstring'''



								pass






'''simple docstring'''


from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase__						:       Dict										=						logging.get_logger(__name__)

lowerCAmelCase__						:       Tuple										=						{"""ctrl""": """https://huggingface.co/ctrl/resolve/main/config.json"""}




class     a			(				SCREAMING_SNAKE_CASE       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    """ctrl"""
						__UpperCAmelCase			     =    ["""past_key_values"""]
						__UpperCAmelCase			     =    {
						    """max_position_embeddings""": """n_positions""",
						    """hidden_size""": """n_embd""",
						    """num_attention_heads""": """n_head""",
						    """num_hidden_layers""": """n_layer""",
						}







						def __init__(							self					:      Tuple			,			snake_case_					:      Dict=2_4_6_5_3_4			,			snake_case_					:      Optional[int]=2_5_6			,			snake_case_					:      Dict=1_2_8_0			,			snake_case_					:      Union[str, Any]=8_1_9_2			,			snake_case_					:      Any=4_8			,			snake_case_					:      List[Any]=1_6			,			snake_case_					:      Optional[Any]=0.1			,			snake_case_					:      Union[str, Any]=0.1			,			snake_case_					:      Optional[Any]=1e-6			,			snake_case_					:      List[Any]=0.0_2			,			snake_case_					:      Dict=True			,			**snake_case_					:      List[Any]			,			):



								'''simple docstring'''



								snake_case__						:       Any       						=  vocab_size
								snake_case__						:       int       						=  n_positions
								snake_case__						:       Optional[int]       						=  n_embd
								snake_case__						:       str       						=  n_layer
								snake_case__						:       Any       						=  n_head
								snake_case__						:       str       						=  dff
								snake_case__						:       Any       						=  resid_pdrop
								snake_case__						:       Tuple       						=  embd_pdrop
								snake_case__						:       List[str]       						=  layer_norm_epsilon
								snake_case__						:       int       						=  initializer_range

								snake_case__						:       Optional[int]       						=  use_cache

								super().__init__(**snake_case_  )



