import inspect
import unittest

from transformers import YolosConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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


if is_torch_available():
	import torch
	from torch import nn

	from transformers import YolosForObjectDetection, YolosModel
	from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import AutoImageProcessor





class UpperCAmelCase						:


				'''simple docstring'''






				def __init__(						self		,     lowercase		,     lowercase=1_3		,     lowercase=[3_0, 3_0]		,     lowercase=2		,     lowercase=3		,     lowercase=True		,     lowercase=True		,     lowercase=3_2		,     lowercase=5		,     lowercase=4		,     lowercase=3_7		,     lowercase="gelu"		,     lowercase=0.1		,     lowercase=0.1		,     lowercase=1_0		,     lowercase=0.02		,     lowercase=3		,     lowercase=None		,     lowercase=8		,     lowercase=1_0		,     ):



						"""simple docstring"""





						A_		:    Optional[int]         =				parent
						A_		:    Union[str, Any]         =				batch_size
						A_		:    Optional[Any]         =				image_size
						A_		:    List[Any]         =				patch_size
						A_		:    List[Any]         =				num_channels
						A_		:    List[Any]         =				is_training
						A_		:    Optional[int]         =				use_labels
						A_		:    List[Any]         =				hidden_size
						A_		:    List[str]         =				num_hidden_layers
						A_		:    Union[str, Any]         =				num_attention_heads
						A_		:    Any         =				intermediate_size
						A_		:    Dict         =				hidden_act
						A_		:    Tuple         =				hidden_dropout_prob
						A_		:    Tuple         =				attention_probs_dropout_prob
						A_		:    int         =				type_sequence_label_size
						A_		:    Union[str, Any]         =				initializer_range
						A_		:    int         =				num_labels
						A_		:    int         =				scope
						A_		:    List[Any]         =				n_targets
						A_		:    Optional[Any]         =				num_detection_tokens
						# we set the expected sequence length (which is used in several tests)
						# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens
						A_		:    Union[str, Any]         =				(image_size[1] // patch_size) * (image_size[0] // patch_size)
						A_		:    Any         =				num_patches + 1 + self.num_detection_tokens
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    List[Any]         =				floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]] )

						A_		:    List[Any]         =				None
						if self.use_labels:
								# labels is a list of Dict (each Dict being the labels for a given example in the batch)
								A_		:    Any         =				[]
								for i in range(self.batch_size ):
										A_		:    List[Any]         =				{}
										A_		:    Dict         =				torch.randint(
										    high=self.num_labels		,     size=(self.n_targets,)		,     device=lowercase )
										A_		:    Any         =				torch.rand(self.n_targets		,     4		,     device=lowercase )
										labels.append(lowercase )

						A_		:    Tuple         =				self.get_config()

						return config, pixel_values, labels
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						return YolosConfig(
						    image_size=self.image_size		,     patch_size=self.patch_size		,     num_channels=self.num_channels		,     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		,     is_decoder=lowercase		,     initializer_range=self.initializer_range		,     num_detection_tokens=self.num_detection_tokens		,     num_labels=self.num_labels		,     )
				def   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase ):



						"""simple docstring"""





						A_		:    Optional[int]         =				YolosModel(config=lowercase )
						model.to(lowercase )
						model.eval()
						A_		:    str         =				model(lowercase )
						self.parent.assertEqual(
						    result.last_hidden_state.shape		,     (self.batch_size, self.expected_seq_len, self.hidden_size) )
				def   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase ):



						"""simple docstring"""





						A_		:    List[Any]         =				YolosForObjectDetection(lowercase )
						model.to(lowercase )
						model.eval()

						A_		:    Union[str, Any]         =				model(pixel_values=lowercase )
						A_		:    List[Any]         =				model(lowercase )

						self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_detection_tokens, self.num_labels + 1) )
						self.parent.assertEqual(result.pred_boxes.shape		,     (self.batch_size, self.num_detection_tokens, 4) )

						A_		:    Optional[Any]         =				model(pixel_values=lowercase		,     labels=lowercase )

						self.parent.assertEqual(result.loss.shape		,     () )
						self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_detection_tokens, self.num_labels + 1) )
						self.parent.assertEqual(result.pred_boxes.shape		,     (self.batch_size, self.num_detection_tokens, 4) )







				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    str         =				self.prepare_config_and_inputs()
						A_		:    Optional[Any]         =				config_and_inputs
						A_		:    Union[str, Any]         =				{'pixel_values': pixel_values}
						return config, inputs_dict





@require_torch
class UpperCAmelCase						( __A ,	__A ,	unittest.TestCase  ):


				'''simple docstring'''






				lowerCamelCase_    = (YolosModel, YolosForObjectDetection) if is_torch_available() else ()
				lowerCamelCase_    = (
				    {'''feature-extraction''': YolosModel, '''object-detection''': YolosForObjectDetection} if is_torch_available() else {}
				)

				lowerCamelCase_    = False
				lowerCamelCase_    = False
				lowerCamelCase_    = False
				lowerCamelCase_    = False
				def   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase=False ):



						"""simple docstring"""





						A_		:    str         =				super()._prepare_for_class(lowercase		,     lowercase		,     return_labels=lowercase )

						if return_labels:
								if model_class.__name__ == "YolosForObjectDetection":
										A_		:    Optional[int]         =				[]
										for i in range(self.model_tester.batch_size ):
												A_		:    List[Any]         =				{}
												A_		:    List[Any]         =				torch.ones(
												    size=(self.model_tester.n_targets,)		,     device=lowercase		,     dtype=torch.long )
												A_		:    Tuple         =				torch.ones(
												    self.model_tester.n_targets		,     4		,     device=lowercase		,     dtype=torch.float )
												labels.append(lowercase )
										A_		:    Optional[Any]         =				labels

						return inputs_dict
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    Tuple         =				YolosModelTester(self )
						A_		:    Any         =				ConfigTester(self		,     config_class=lowercase		,     has_text_modality=lowercase		,     hidden_size=3_7 )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





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



						"""simple docstring"""





						pass
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    List[Any]         =				self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
								A_		:    Tuple         =				model_class(lowercase )
								self.assertIsInstance(model.get_input_embeddings()		,     (nn.Module) )
								A_		:    Optional[Any]         =				model.get_output_embeddings()
								self.assertTrue(x is None or isinstance(lowercase		,     nn.Linear ) )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    List[Any]         =				self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
								A_		:    int         =				model_class(lowercase )
								A_		:    Optional[Any]         =				inspect.signature(model.forward )
								# signature.parameters is an OrderedDict => so arg_names order is deterministic
								A_		:    int         =				[*signature.parameters.keys()]

								A_		:    Any         =				['pixel_values']
								self.assertListEqual(arg_names[:1]		,     lowercase )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    List[Any]         =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowercase )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    str         =				self.model_tester.prepare_config_and_inputs_for_common()
						A_		:    Dict         =				True

						# in YOLOS, the seq_len is different
						A_		:    str         =				self.model_tester.expected_seq_len
						for model_class in self.all_model_classes:
								A_		:    Tuple         =				True
								A_		:    List[Any]         =				False
								A_		:    Any         =				True
								A_		:    Union[str, Any]         =				model_class(lowercase )
								model.to(lowercase )
								model.eval()
								with torch.no_grad():
										A_		:    List[str]         =				model(**self._prepare_for_class(lowercase		,     lowercase ) )
								A_		:    Optional[Any]         =				outputs.attentions
								self.assertEqual(len(lowercase )		,     self.model_tester.num_hidden_layers )

								# check that output_attentions also work using config
								del inputs_dict["output_attentions"]
								A_		:    Tuple         =				True
								A_		:    Optional[int]         =				model_class(lowercase )
								model.to(lowercase )
								model.eval()
								with torch.no_grad():
										A_		:    Any         =				model(**self._prepare_for_class(lowercase		,     lowercase ) )
								A_		:    List[Any]         =				outputs.attentions
								self.assertEqual(len(lowercase )		,     self.model_tester.num_hidden_layers )

								self.assertListEqual(
								    list(attentions[0].shape[-3:] )		,     [self.model_tester.num_attention_heads, seq_len, seq_len]		,     )
								A_		:    List[Any]         =				len(lowercase )

								# Check attention is always last and order is fine
								A_		:    List[Any]         =				True
								A_		:    List[str]         =				True
								A_		:    int         =				model_class(lowercase )
								model.to(lowercase )
								model.eval()
								with torch.no_grad():
										A_		:    Tuple         =				model(**self._prepare_for_class(lowercase		,     lowercase ) )

								A_		:    Optional[int]         =				1
								self.assertEqual(out_len + added_hidden_states		,     len(lowercase ) )

								A_		:    Tuple         =				outputs.attentions

								self.assertEqual(len(lowercase )		,     self.model_tester.num_hidden_layers )
								self.assertListEqual(
								    list(self_attentions[0].shape[-3:] )		,     [self.model_tester.num_attention_heads, seq_len, seq_len]		,     )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						def check_hidden_states_output(lowercase		,     lowercase		,     lowercase ):
								A_		:    List[str]         =				model_class(lowercase )
								model.to(lowercase )
								model.eval()

								with torch.no_grad():
										A_		:    List[Any]         =				model(**self._prepare_for_class(lowercase		,     lowercase ) )

								A_		:    Dict         =				outputs.hidden_states

								A_		:    List[str]         =				getattr(
								    self.model_tester		,     'expected_num_hidden_layers'		,     self.model_tester.num_hidden_layers + 1 )
								self.assertEqual(len(lowercase )		,     lowercase )

								# YOLOS has a different seq_length
								A_		:    Optional[Any]         =				self.model_tester.expected_seq_len

								self.assertListEqual(
								    list(hidden_states[0].shape[-2:] )		,     [seq_length, self.model_tester.hidden_size]		,     )

						A_		:    Union[str, Any]         =				self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
								A_		:    Union[str, Any]         =				True
								check_hidden_states_output(lowercase		,     lowercase		,     lowercase )

								# check that output_hidden_states also work using config
								del inputs_dict["output_hidden_states"]
								A_		:    Any         =				True

								check_hidden_states_output(lowercase		,     lowercase		,     lowercase )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    int         =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_object_detection(*lowercase )







				@slow
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								A_		:    Union[str, Any]         =				YolosModel.from_pretrained(lowercase )
								self.assertIsNotNone(lowercase )

def     UpperCamelCase						(	):
		'''simple docstring'''




		A_		:    int         =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
		return image





@require_torch
@require_vision
class UpperCAmelCase						( unittest.TestCase  ):


				'''simple docstring'''






				@cached_property
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						return AutoImageProcessor.from_pretrained('hustvl/yolos-small' ) if is_vision_available() else None







				@slow
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    int         =				YolosForObjectDetection.from_pretrained('hustvl/yolos-small' ).to(lowercase )

						A_		:    Dict         =				self.default_image_processor
						A_		:    int         =				prepare_img()
						A_		:    Union[str, Any]         =				image_processor(images=lowercase		,     return_tensors='pt' ).to(lowercase )

						# forward pass
						with torch.no_grad():
								A_		:    Any         =				model(inputs.pixel_values )

						# verify outputs
						A_		:    Any         =				torch.Size((1, 1_0_0, 9_2) )
						self.assertEqual(outputs.logits.shape		,     lowercase )

						A_		:    int         =				torch.tensor(
						    [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]]		,     device=lowercase		,     )
						A_		:    List[str]         =				torch.tensor(
						    [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]]		,     device=lowercase )
						self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]		,     lowercase		,     atol=1E-4 ) )
						self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]		,     lowercase		,     atol=1E-4 ) )

						# verify postprocessing
						A_		:    List[str]         =				image_processor.post_process_object_detection(
						    lowercase		,     threshold=0.3		,     target_sizes=[image.size[::-1]] )[0]
						A_		:    Dict         =				torch.tensor([0.9994, 0.9790, 0.9964, 0.9972, 0.9861] ).to(lowercase )
						A_		:    Optional[int]         =				[7_5, 7_5, 1_7, 6_3, 1_7]
						A_		:    List[str]         =				torch.tensor([335.0609, 79.3848, 375.4216, 187.2495] ).to(lowercase )

						self.assertEqual(len(results['scores'] )		,     5 )
						self.assertTrue(torch.allclose(results['scores']		,     lowercase		,     atol=1E-4 ) )
						self.assertSequenceEqual(results['labels'].tolist()		,     lowercase )
						self.assertTrue(torch.allclose(results['boxes'][0, :]		,     lowercase ) )







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_UpperCAmelCase            =		{
    """configuration_instructblip""": [
        """INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """InstructBlipConfig""",
        """InstructBlipQFormerConfig""",
        """InstructBlipVisionConfig""",
    ],
    """processing_instructblip""": ["""InstructBlipProcessor"""],
}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_UpperCAmelCase            =		[
	    """INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """InstructBlipQFormerModel""",
	    """InstructBlipPreTrainedModel""",
	    """InstructBlipForConditionalGeneration""",
	    """InstructBlipVisionModel""",
	]

if TYPE_CHECKING:
	from .configuration_instructblip import (
	    INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    InstructBlipConfig,
	    InstructBlipQFormerConfig,
	    InstructBlipVisionConfig,
	)
	from .processing_instructblip import InstructBlipProcessor

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_instructblip import (
		    INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
		    InstructBlipForConditionalGeneration,
		    InstructBlipPreTrainedModel,
		    InstructBlipQFormerModel,
		    InstructBlipVisionModel,
		)

else:
	import sys

	_UpperCAmelCase            =		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)






