





'''simple docstring'''







import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import MaskaFormerConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

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


if is_torch_available():
					import torch

					from transformers import MaskaFormerForUniversalSegmentation, MaskaFormerModel

					if is_vision_available():
										from transformers import MaskaFormerImageProcessor

if is_vision_available():
					from PIL import Image


class 					__a      :

							def __init__(							self							:     Union[str, Any]       ,     __magic_name__							:     Any       ,     __magic_name__							:     str=2       ,     __magic_name__							:     str=True       ,     __magic_name__							:     int=False       ,     __magic_name__							:     Any=10       ,     __magic_name__							:     Dict=3       ,     __magic_name__							:     Tuple=32 * 8       ,     __magic_name__							:     int=32 * 8       ,     __magic_name__							:     Union[str, Any]=4       ,     __magic_name__							:     Dict=64       ,     )					->       Tuple:







													"""simple docstring"""
													UpperCAmelCase_			:      Any     						=  parent
													UpperCAmelCase_			:      Optional[int]     						=  batch_size
													UpperCAmelCase_			:      List[Any]     						=  is_training
													UpperCAmelCase_			:      List[str]     						=  use_auxiliary_loss
													UpperCAmelCase_			:      Any     						=  num_queries
													UpperCAmelCase_			:      Tuple     						=  num_channels
													UpperCAmelCase_			:      Optional[int]     						=  min_size
													UpperCAmelCase_			:      Any     						=  max_size
													UpperCAmelCase_			:      str     						=  num_labels
													UpperCAmelCase_			:      Optional[int]     						=  hidden_dim
													UpperCAmelCase_			:      Dict     						=  hidden_dim

							def     UpperCAmelCase__	(							self							:     Optional[Any]   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      Union[str, Any]     						=  floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]   ).to(
													    __magic_name__   )

													UpperCAmelCase_			:      Dict     						=  torch.ones([self.batch_size, self.min_size, self.max_size]       ,     device=__magic_name__   )

													UpperCAmelCase_			:      str     						=  (
													    torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]       ,     device=__magic_name__   ) > 0.5
													).float()
													UpperCAmelCase_			:      str     						=  (torch.rand((self.batch_size, self.num_labels)       ,     device=__magic_name__   ) > 0.5).long()

													UpperCAmelCase_			:      List[str]     						=  self.get_config()
													return config, pixel_values, pixel_mask, mask_labels, class_labels

							def     UpperCAmelCase__	(							self							:     Tuple   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      List[str]     						=  MaskaFormerConfig(
													    hidden_size=self.hidden_dim       ,     )
													UpperCAmelCase_			:      Tuple     						=  self.num_queries
													UpperCAmelCase_			:      int     						=  self.num_labels

													UpperCAmelCase_			:      str     						=  [1, 1, 1, 1]
													UpperCAmelCase_			:      int     						=  self.num_channels

													UpperCAmelCase_			:      Union[str, Any]     						=  64
													UpperCAmelCase_			:      Tuple     						=  1_28
													UpperCAmelCase_			:      str     						=  self.hidden_dim
													UpperCAmelCase_			:      Union[str, Any]     						=  self.hidden_dim
													UpperCAmelCase_			:      List[Any]     						=  self.hidden_dim
													return config

							def     UpperCAmelCase__	(							self							:     str   )					->       Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_						,       UpperCAmelCase_						,       UpperCAmelCase_						,       UpperCAmelCase_						,       UpperCAmelCase_			:      Optional[int]     						=  self.prepare_config_and_inputs()
													UpperCAmelCase_			:      int     						=  {'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask}
													return config, inputs_dict

							def     UpperCAmelCase__	(							self							:     str       ,     __magic_name__							:     int       ,     __magic_name__							:     Union[str, Any]   )					->       Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  output.encoder_hidden_states
													UpperCAmelCase_			:      str     						=  output.pixel_decoder_hidden_states
													UpperCAmelCase_			:      str     						=  output.transformer_decoder_hidden_states

													self.parent.assertTrue(len(__magic_name__   )       ,     len(config.backbone_config.depths   )   )
													self.parent.assertTrue(len(__magic_name__   )       ,     len(config.backbone_config.depths   )   )
													self.parent.assertTrue(len(__magic_name__   )       ,     config.decoder_layers   )

							def     UpperCAmelCase__	(							self							:     List[Any]       ,     __magic_name__							:     Dict       ,     __magic_name__							:     List[str]       ,     __magic_name__							:     Union[str, Any]       ,     __magic_name__							:     Optional[Any]=False   )					->       List[str]:







													"""simple docstring"""
													with torch.no_grad():
																			UpperCAmelCase_			:      Dict     						=  MaskaFormerModel(config=__magic_name__   )
																			model.to(__magic_name__   )
																			model.eval()

																			UpperCAmelCase_			:      List[str]     						=  model(pixel_values=__magic_name__       ,     pixel_mask=__magic_name__   )
																			UpperCAmelCase_			:      Dict     						=  model(__magic_name__       ,     output_hidden_states=__magic_name__   )

													self.parent.assertEqual(
													    output.transformer_decoder_last_hidden_state.shape       ,     (self.batch_size, self.num_queries, self.hidden_dim)       ,     )
													# let's ensure the other two hidden state exists
													self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None   )
													self.parent.assertTrue(output.encoder_last_hidden_state is not None   )

													if output_hidden_states:
																			self.check_output_hidden_state(__magic_name__       ,     __magic_name__   )



							def     UpperCAmelCase__	(							self							:     Tuple       ,     __magic_name__							:     Union[str, Any]       ,     __magic_name__							:     str       ,     __magic_name__							:     Union[str, Any]       ,     __magic_name__							:     Any       ,     __magic_name__							:     Tuple   )					->       Any:







													"""simple docstring"""
													UpperCAmelCase_			:      List[Any]     						=  MaskaFormerForUniversalSegmentation(config=__magic_name__   )
													model.to(__magic_name__   )
													model.eval()

													def comm_check_on_output(__magic_name__							:     Optional[Any]   ):
																			# let's still check that all the required stuff is there
																			self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None   )
																			self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None   )
																			self.parent.assertTrue(result.encoder_last_hidden_state is not None   )
																			# okay, now we need to check the logits shape
																			# due to the encoder compression, masks have a //4 spatial size
																			self.parent.assertEqual(
																			    result.masks_queries_logits.shape       ,     (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4)       ,     )
																			# + 1 for null class
																			self.parent.assertEqual(
																			    result.class_queries_logits.shape       ,     (self.batch_size, self.num_queries, self.num_labels + 1)   )

													with torch.no_grad():
																			UpperCAmelCase_			:      List[Any]     						=  model(pixel_values=__magic_name__       ,     pixel_mask=__magic_name__   )
																			UpperCAmelCase_			:      Dict     						=  model(__magic_name__   )

																			comm_check_on_output(__magic_name__   )

																			UpperCAmelCase_			:      Dict     						=  model(
																			    pixel_values=__magic_name__       ,     pixel_mask=__magic_name__       ,     mask_labels=__magic_name__       ,     class_labels=__magic_name__   )

													comm_check_on_output(__magic_name__   )

													self.parent.assertTrue(result.loss is not None   )
													self.parent.assertEqual(result.loss.shape       ,     torch.Size([1]   )   )





@require_torch
class 					__a      (lowerCamelCase       ,		lowerCamelCase       ,		unittest.TestCase     ):
							__a					:   Union[str, Any]									=       (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
							__a					:   Union[str, Any]									=       {"feature-extraction": MaskaFormerModel} if is_torch_available() else {}

							__a					:   List[str]									=       False
							__a					:   Dict									=       False
							__a					:   List[str]									=       False
							__a					:   Any									=       False

							def     UpperCAmelCase__	(							self							:     List[Any]   )					->       Tuple:







													"""simple docstring"""
													UpperCAmelCase_			:      List[str]     						=  MaskaFormerModelTester(self   )
													UpperCAmelCase_			:      str     						=  ConfigTester(self       ,     config_class=__magic_name__       ,     has_text_modality=__magic_name__   )

							def     UpperCAmelCase__	(							self							:     List[str]   )					->       str:







													"""simple docstring"""
													self.config_tester.run_common_tests()

							def     UpperCAmelCase__	(							self							:     Tuple   )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_						,       UpperCAmelCase_			:      List[Any]     						=  self.model_tester.prepare_config_and_inputs_for_common()
													self.model_tester.create_and_check_maskaformer_model(__magic_name__       ,     **__magic_name__       ,     output_hidden_states=__magic_name__   )

							def     UpperCAmelCase__	(							self							:     str   )					->       Any:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[int]     						=  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*__magic_name__   )

							@unittest.skip(reason='''Mask2Former does not use inputs_embeds'''   )
							def     UpperCAmelCase__	(							self							:     Any   )					->       Union[str, Any]:







													"""simple docstring"""
													pass

							@unittest.skip(reason='''Mask2Former does not have a get_input_embeddings method'''   )
							def     UpperCAmelCase__	(							self							:     List[str]   )					->       Union[str, Any]:







													"""simple docstring"""
													pass

							@unittest.skip(reason='''Mask2Former is not a generative model'''   )
							def     UpperCAmelCase__	(							self							:     Union[str, Any]   )					->       Optional[Any]:







													"""simple docstring"""
													pass

							@unittest.skip(reason='''Mask2Former does not use token embeddings'''   )
							def     UpperCAmelCase__	(							self							:     List[Any]   )					->       Optional[Any]:







													"""simple docstring"""
													pass

							@require_torch_multi_gpu
							@unittest.skip(
							    reason='''Mask2Former has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''   )
							def     UpperCAmelCase__	(							self							:     Optional[int]   )					->       str:







													"""simple docstring"""
													pass

							@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''   )
							def     UpperCAmelCase__	(							self							:     str   )					->       Any:







													"""simple docstring"""
													pass

							def     UpperCAmelCase__	(							self							:     Optional[Any]   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_						,       UpperCAmelCase_			:      Tuple     						=  self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																			UpperCAmelCase_			:      Union[str, Any]     						=  model_class(__magic_name__   )
																			UpperCAmelCase_			:      str     						=  inspect.signature(model.forward   )
																			# signature.parameters is an OrderedDict => so arg_names order is deterministic
																			UpperCAmelCase_			:      List[Any]     						=  [*signature.parameters.keys()]

																			UpperCAmelCase_			:      str     						=  ['''pixel_values''']
																			self.assertListEqual(arg_names[:1]       ,     __magic_name__   )

							@slow
							def     UpperCAmelCase__	(							self							:     List[Any]   )					->       Union[str, Any]:







													"""simple docstring"""
													for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
																			UpperCAmelCase_			:      str     						=  MaskaFormerModel.from_pretrained(__magic_name__   )
																			self.assertIsNotNone(__magic_name__   )

							def     UpperCAmelCase__	(							self							:     List[Any]   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  (self.model_tester.min_size,) * 2
													UpperCAmelCase_			:      Tuple     						=  {
													    '''pixel_values''': torch.randn((2, 3, *size)       ,     device=__magic_name__   ),
													    '''mask_labels''': torch.randn((2, 10, *size)       ,     device=__magic_name__   ),
													    '''class_labels''': torch.zeros(2       ,     10       ,     device=__magic_name__   ).long(),
													}
													UpperCAmelCase_			:      Optional[int]     						=  self.model_tester.get_config()

													UpperCAmelCase_			:      Any     						=  MaskaFormerForUniversalSegmentation(__magic_name__   ).to(__magic_name__   )
													UpperCAmelCase_			:      List[str]     						=  model(**__magic_name__   )
													self.assertTrue(outputs.loss is not None   )

							def     UpperCAmelCase__	(							self							:     Optional[Any]   )					->       Dict:







													"""simple docstring"""
													UpperCAmelCase_						,       UpperCAmelCase_			:      List[Any]     						=  self.model_tester.prepare_config_and_inputs_for_common()
													self.model_tester.create_and_check_maskaformer_model(__magic_name__       ,     **__magic_name__       ,     output_hidden_states=__magic_name__   )

							def     UpperCAmelCase__	(							self							:     str   )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_						,       UpperCAmelCase_			:      Dict     						=  self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																			UpperCAmelCase_			:      Tuple     						=  model_class(__magic_name__   ).to(__magic_name__   )
																			UpperCAmelCase_			:      Tuple     						=  model(**__magic_name__       ,     output_attentions=__magic_name__   )
																			self.assertTrue(outputs.attentions is not None   )

							def     UpperCAmelCase__	(							self							:     int   )					->       int:







													"""simple docstring"""
													if not self.model_tester.is_training:
																			return

													UpperCAmelCase_			:      int     						=  self.all_model_classes[1]
													UpperCAmelCase_						,       UpperCAmelCase_						,       UpperCAmelCase_						,       UpperCAmelCase_						,       UpperCAmelCase_			:      Optional[int]     						=  self.model_tester.prepare_config_and_inputs()

													UpperCAmelCase_			:      List[str]     						=  model_class(__magic_name__   )
													model.to(__magic_name__   )
													model.train()

													UpperCAmelCase_			:      Optional[Any]     						=  model(__magic_name__       ,     mask_labels=__magic_name__       ,     class_labels=__magic_name__   ).loss
													loss.backward()



							def     UpperCAmelCase__	(							self							:     List[str]   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      List[Any]     						=  self.all_model_classes[1]
													UpperCAmelCase_						,       UpperCAmelCase_						,       UpperCAmelCase_						,       UpperCAmelCase_						,       UpperCAmelCase_			:      List[Any]     						=  self.model_tester.prepare_config_and_inputs()
													UpperCAmelCase_			:      int     						=  True
													UpperCAmelCase_			:      List[Any]     						=  True

													UpperCAmelCase_			:      Union[str, Any]     						=  model_class(__magic_name__   ).to(__magic_name__   )
													model.train()

													UpperCAmelCase_			:      List[Any]     						=  model(__magic_name__       ,     mask_labels=__magic_name__       ,     class_labels=__magic_name__   )

													UpperCAmelCase_			:      List[str]     						=  outputs.encoder_hidden_states[0]
													encoder_hidden_states.retain_grad()

													UpperCAmelCase_			:      str     						=  outputs.pixel_decoder_hidden_states[0]
													pixel_decoder_hidden_states.retain_grad()

													UpperCAmelCase_			:      Optional[int]     						=  outputs.transformer_decoder_hidden_states[0]
													transformer_decoder_hidden_states.retain_grad()

													UpperCAmelCase_			:      str     						=  outputs.attentions[0]
													attentions.retain_grad()

													outputs.loss.backward(retain_graph=__magic_name__   )

													self.assertIsNotNone(encoder_hidden_states.grad   )
													self.assertIsNotNone(pixel_decoder_hidden_states.grad   )
													self.assertIsNotNone(transformer_decoder_hidden_states.grad   )
													self.assertIsNotNone(attentions.grad   )





snake_case_     :       Optional[Any]						=  1E-4





def        lowerCamelCase_      (  ) ->	Any:
						UpperCAmelCase_			:      Tuple     						=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''   )
						return image


@require_vision
@slow
class 					__a      (unittest.TestCase     ):

							@cached_property
							def     UpperCAmelCase__	(							self							:     Optional[Any]   )					->       List[str]:







													"""simple docstring"""
													return "facebook/mask2former-swin-small-coco-instance"

							@cached_property
							def     UpperCAmelCase__	(							self							:     Union[str, Any]   )					->       List[str]:







													"""simple docstring"""
													return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints   ) if is_vision_available() else None

							def     UpperCAmelCase__	(							self							:     int   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  MaskaFormerModel.from_pretrained(self.model_checkpoints   ).to(__magic_name__   )
													UpperCAmelCase_			:      Any     						=  self.default_image_processor
													UpperCAmelCase_			:      int     						=  prepare_img()
													UpperCAmelCase_			:      str     						=  image_processor(__magic_name__       ,     return_tensors='''pt'''   ).to(__magic_name__   )
													UpperCAmelCase_			:      Union[str, Any]     						=  inputs['''pixel_values'''].shape
													# check size is divisible by 32
													self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0   )
													# check size
													self.assertEqual(__magic_name__       ,     (1, 3, 3_84, 3_84)   )

													with torch.no_grad():
																			UpperCAmelCase_			:      int     						=  model(**__magic_name__   )

													UpperCAmelCase_			:      int     						=  torch.tensor(
													    [[-0.2_7_9_0, -1.0_7_1_7, -1.1_6_6_8], [-0.5_1_2_8, -0.3_1_2_8, -0.4_9_8_7], [-0.5_8_3_2, 0.1_9_7_1, -0.0_1_9_7]]   ).to(__magic_name__   )
													self.assertTrue(
													    torch.allclose(
													        outputs.encoder_last_hidden_state[0, 0, :3, :3]       ,     __magic_name__       ,     atol=__magic_name__   )   )

													UpperCAmelCase_			:      Dict     						=  torch.tensor(
													    [[0.8_9_7_3, 1.1_8_4_7, 1.1_7_7_6], [1.1_9_3_4, 1.5_0_4_0, 1.5_1_2_8], [1.1_1_5_3, 1.4_4_8_6, 1.4_9_5_1]]   ).to(__magic_name__   )
													self.assertTrue(
													    torch.allclose(
													        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]       ,     __magic_name__       ,     atol=__magic_name__   )   )

													UpperCAmelCase_			:      int     						=  torch.tensor(
													    [[2.1_1_5_2, 1.7_0_0_0, -0.8_6_0_3], [1.5_8_0_8, 1.8_0_0_4, -0.9_3_5_3], [1.6_0_4_3, 1.7_4_9_5, -0.5_9_9_9]]   ).to(__magic_name__   )
													self.assertTrue(
													    torch.allclose(
													        outputs.transformer_decoder_last_hidden_state[0, :3, :3]       ,     __magic_name__       ,     atol=__magic_name__   )   )

							def     UpperCAmelCase__	(							self							:     Optional[int]   )					->       Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      List[str]     						=  MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints   ).to(__magic_name__   ).eval()
													UpperCAmelCase_			:      int     						=  self.default_image_processor
													UpperCAmelCase_			:      Dict     						=  prepare_img()
													UpperCAmelCase_			:      Optional[int]     						=  image_processor(__magic_name__       ,     return_tensors='''pt'''   ).to(__magic_name__   )
													UpperCAmelCase_			:      List[str]     						=  inputs['''pixel_values'''].shape
													# check size is divisible by 32
													self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0   )
													# check size
													self.assertEqual(__magic_name__       ,     (1, 3, 3_84, 3_84)   )

													with torch.no_grad():
																			UpperCAmelCase_			:      Any     						=  model(**__magic_name__   )
													# masks_queries_logits
													UpperCAmelCase_			:      Dict     						=  outputs.masks_queries_logits
													self.assertEqual(
													    masks_queries_logits.shape       ,     (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)   )
													UpperCAmelCase_			:      Union[str, Any]     						=  [
													    [-8.7_8_3_9, -9.0_0_5_6, -8.8_1_2_1],
													    [-7.4_1_0_4, -7.0_3_1_3, -6.5_4_0_1],
													    [-6.6_1_0_5, -6.3_4_2_7, -6.4_6_7_5],
													]
													UpperCAmelCase_			:      List[str]     						=  torch.tensor(__magic_name__   ).to(__magic_name__   )
													self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]       ,     __magic_name__       ,     atol=__magic_name__   )   )
													# class_queries_logits
													UpperCAmelCase_			:      Optional[int]     						=  outputs.class_queries_logits
													self.assertEqual(class_queries_logits.shape       ,     (1, model.config.num_queries, model.config.num_labels + 1)   )
													UpperCAmelCase_			:      Optional[Any]     						=  torch.tensor(
													    [
													        [1.8_3_2_4, -8.0_8_3_5, -4.1_9_2_2],
													        [0.8_4_5_0, -9.0_0_5_0, -3.6_0_5_3],
													        [0.3_0_4_5, -7.7_2_9_3, -3.0_2_7_5],
													    ]   ).to(__magic_name__   )
													self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]       ,     __magic_name__       ,     atol=__magic_name__   )   )



							def     UpperCAmelCase__	(							self							:     List[Any]   )					->       Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints   ).to(__magic_name__   ).eval()
													UpperCAmelCase_			:      str     						=  self.default_image_processor

													UpperCAmelCase_			:      str     						=  image_processor(
													    [np.zeros((3, 8_00, 13_33)   ), np.zeros((3, 8_00, 13_33)   )]       ,     segmentation_maps=[np.zeros((3_84, 3_84)   ).astype(np.floataa   ), np.zeros((3_84, 3_84)   ).astype(np.floataa   )]       ,     return_tensors='''pt'''       ,     )

													UpperCAmelCase_			:      List[str]     						=  inputs['''pixel_values'''].to(__magic_name__   )
													UpperCAmelCase_			:      Union[str, Any]     						=  [el.to(__magic_name__   ) for el in inputs['''mask_labels''']]
													UpperCAmelCase_			:      List[Any]     						=  [el.to(__magic_name__   ) for el in inputs['''class_labels''']]

													with torch.no_grad():
																			UpperCAmelCase_			:      List[str]     						=  model(**__magic_name__   )

													self.assertTrue(outputs.loss is not None   )












'''simple docstring'''







# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

import torch

from ..models.auto import AutoModelForVisualQuestionAnswering, AutoProcessor
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
					from PIL import Image


class 					__a      (lowerCamelCase     ):
							__a					:   int									=       "dandelin/vilt-b32-finetuned-vqa"
							__a					:   Any									=       (
							    "This is a tool that answers a question about an image. It takes an input named `image` which should be the "
							    "image containing the information, as well as a `question` which should be the question in English. It "
							    "returns a text that is the answer to the question."
							)
							__a					:   Any									=       "image_qa"
							__a					:   str									=       AutoProcessor
							__a					:   Any									=       AutoModelForVisualQuestionAnswering

							__a					:   List[Any]									=       ["image", "text"]
							__a					:   int									=       ["text"]

							def __init__(							self							:     Tuple       ,     *__magic_name__							:     Any       ,     **__magic_name__							:     Any   )					->       Tuple:







													"""simple docstring"""
													requires_backends(self       ,     ['''vision''']   )
													super().__init__(*__magic_name__       ,     **__magic_name__   )

							def     UpperCAmelCase__	(							self							:     Union[str, Any]       ,     __magic_name__							:     "Image"       ,     __magic_name__							:     str   )					->       Tuple:







													"""simple docstring"""
													return self.pre_processor(__magic_name__       ,     __magic_name__       ,     return_tensors='''pt'''   )

							def     UpperCAmelCase__	(							self							:     Any       ,     __magic_name__							:     List[str]   )					->       Optional[Any]:







													"""simple docstring"""
													with torch.no_grad():
																			return self.model(**__magic_name__   ).logits



							def     UpperCAmelCase__	(							self							:     int       ,     __magic_name__							:     int   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  outputs.argmax(-1   ).item()
													return self.model.config.idalabel[idx]





