


"""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						:    Union[str, Any]				      =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










"""simple docstring"""




import operator as op


lowerCAmelCase							:			Dict     							=							"""scaler.pt"""
lowerCAmelCase							:			Tuple     							=							"""pytorch_model"""
lowerCAmelCase							:			Union[str, Any]     							=							"""random_states"""
lowerCAmelCase							:			Union[str, Any]     							=							"""optimizer"""
lowerCAmelCase							:			Dict     							=							"""scheduler"""
lowerCAmelCase							:			int     							=							"""pytorch_model.bin"""
lowerCAmelCase							:			str     							=							"""pytorch_model.bin.index.json"""
lowerCAmelCase							:			Union[str, Any]     							=							"""model.safetensors"""
lowerCAmelCase							:			List[Any]     							=							"""model.safetensors.index.json"""
lowerCAmelCase							:			List[Any]     							=							"""1.10.2"""
lowerCAmelCase							:			Any     							=							"""py38"""
lowerCAmelCase							:			Optional[int]     							=							"""4.17.0"""
lowerCAmelCase							:			str     							=							["""ml.p3.16xlarge""", """ml.p3dn.24xlarge""", """ml.p4dn.24xlarge"""]
lowerCAmelCase							:			Tuple     							=							["""FULL_SHARD""", """SHARD_GRAD_OP""", """NO_SHARD""", """HYBRID_SHARD""", """HYBRID_SHARD_ZERO2"""]
lowerCAmelCase							:			List[Any]     							=							["""TRANSFORMER_BASED_WRAP""", """SIZE_BASED_WRAP""", """NO_WRAP"""]
lowerCAmelCase							:			List[str]     							=							["""BACKWARD_PRE""", """BACKWARD_POST""", """NO_PREFETCH"""]
lowerCAmelCase							:			List[str]     							=							["""FULL_STATE_DICT""", """LOCAL_STATE_DICT""", """SHARDED_STATE_DICT"""]
lowerCAmelCase							:			Any     							=							"""2.0.1"""
lowerCAmelCase							:			List[Any]     							=							["""pdsh""", """standard""", """openmpi""", """mvapich"""]
lowerCAmelCase							:			Union[str, Any]     							=							["""default""", """reduce-overhead""", """max-autotune"""]

lowerCAmelCase							:			Optional[int]     							=							{""">""": op.gt, """>=""": op.ge, """==""": op.eq, """!=""": op.ne, """<=""": op.le, """<""": op.lt}

# These are the args for `torch.distributed.launch` for pytorch < 1.9
lowerCAmelCase							:			Union[str, Any]     							=							[
    """nnodes""",
    """nproc_per_node""",
    """rdzv_backend""",
    """rdzv_endpoint""",
    """rdzv_id""",
    """rdzv_conf""",
    """standalone""",
    """max_restarts""",
    """monitor_interval""",
    """start_method""",
    """role""",
    """module""",
    """m""",
    """no_python""",
    """run_path""",
    """log_dir""",
    """r""",
    """redirects""",
    """t""",
    """tee""",
    """node_rank""",
    """master_addr""",
    """master_port""",
]

lowerCAmelCase							:			List[str]     							=							["""DEEPSPEED""", """MULTI_GPU""", """FSDP""", """MEGATRON_LM"""]
lowerCAmelCase							:			Optional[Any]     							=							["""DEEPSPEED""", """MULTI_XPU""", """FSDP"""]



