
"""simple docstring"""




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       __magic_name__							:
       def __init__(  self			,					A_			,					A_=13			,					A_=[30, 30]			,					A_=2			,					A_=3			,					A_=True			,					A_=True			,					A_=32			,					A_=5			,					A_=4			,					A_=37			,					A_="gelu"			,					A_=0.1			,					A_=0.1			,					A_=10			,					A_=0.02			,					A_=3			,					A_=None			,					A_=8			,					A_=10			,					)  ->						Dict:





         """simple docstring"""




         _lowercase:      List[str]   			=  parent
         _lowercase:      List[Any]   			=  batch_size
         _lowercase:      Union[str, Any]   			=  image_size
         _lowercase:      Tuple   			=  patch_size
         _lowercase:      Union[str, Any]   			=  num_channels
         _lowercase:      str   			=  is_training
         _lowercase:      List[str]   			=  use_labels
         _lowercase:      Tuple   			=  hidden_size
         _lowercase:      str   			=  num_hidden_layers
         _lowercase:      Tuple   			=  num_attention_heads
         _lowercase:      Optional[int]   			=  intermediate_size
         _lowercase:      Any   			=  hidden_act
         _lowercase:      Optional[Any]   			=  hidden_dropout_prob
         _lowercase:      Any   			=  attention_probs_dropout_prob
         _lowercase:      Optional[int]   			=  type_sequence_label_size
         _lowercase:      List[Any]   			=  initializer_range
         _lowercase:      int   			=  num_labels
         _lowercase:      Any   			=  scope
         _lowercase:      Optional[int]   			=  n_targets
         _lowercase:      List[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
         _lowercase:      Dict   			=  (image_size[1] // patch_size) * (image_size[0] // patch_size)
         _lowercase:      Dict   			=  num_patches + 1 + self.num_detection_tokens
       def 				lowercase_				(  self							)  ->						str:





         """simple docstring"""




         _lowercase:      int   			=  floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]							)

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

         _lowercase:      Optional[int]   			=  self.get_config()

         return config, pixel_values, labels
       def 				lowercase_				(  self							)  ->						Union[str, Any]:





         """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=A_			,					initializer_range=self.initializer_range			,					num_detection_tokens=self.num_detection_tokens			,					num_labels=self.num_labels			,					)
       def 				lowercase_				(  self			,					A_			,					A_			,					A_							)  ->						Any:





         """simple docstring"""




         _lowercase:      Optional[int]   			=  YolosModel(config=A_							)
         model.to(A_							)
         model.eval()
         _lowercase:      List[Any]   			=  model(A_							)
         self.parent.assertEqual(
             result.last_hidden_state.shape			,					(self.batch_size, self.expected_seq_len, self.hidden_size)							)
       def 				lowercase_				(  self			,					A_			,					A_			,					A_							)  ->						Optional[Any]:





         """simple docstring"""




         _lowercase:      str   			=  YolosForObjectDetection(A_							)
         model.to(A_							)
         model.eval()

         _lowercase:      Dict   			=  model(pixel_values=A_							)
         _lowercase:      List[Any]   			=  model(A_							)

         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)							)

         _lowercase:      List[Any]   			=  model(pixel_values=A_			,					labels=A_							)

         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 				lowercase_				(  self							)  ->						List[Any]:





         """simple docstring"""




         _lowercase:      Optional[int]   			=  self.prepare_config_and_inputs()
         _lowercase			,					_lowercase			,					_lowercase:      Any   			=  config_and_inputs
         _lowercase:      Dict   			=  {'''pixel_values''': pixel_values}
         return config, inputs_dict


@require_torch
class       __magic_name__							(							SCREAMING_SNAKE_CASE__ ,       SCREAMING_SNAKE_CASE__ ,       unittest.TestCase			):
       UpperCamelCase_ =		(YolosModel, YolosForObjectDetection) if is_torch_available() else ()
       UpperCamelCase_ =		(
           {'''feature-extraction''': YolosModel, '''object-detection''': YolosForObjectDetection} if is_torch_available() else {}
       )

       UpperCamelCase_ =		False
       UpperCamelCase_ =		False
       UpperCamelCase_ =		False
       UpperCamelCase_ =		False
       def 				lowercase_				(  self			,					A_			,					A_			,					A_=False							)  ->						List[Any]:





         """simple docstring"""




         _lowercase:      List[str]   			=  super()._prepare_for_class(A_			,					A_			,					return_labels=A_							)

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

         return inputs_dict
       def 				lowercase_				(  self							)  ->						Optional[int]:





         """simple docstring"""




         _lowercase:      Tuple   			=  YolosModelTester(self							)
         _lowercase:      int   			=  ConfigTester(self			,					config_class=A_			,					has_text_modality=A_			,					hidden_size=37							)
       def 				lowercase_				(  self							)  ->						Union[str, Any]:





         """simple docstring"""




         self.config_tester.run_common_tests()
       def 				lowercase_				(  self							)  ->						Optional[int]:





         """simple docstring"""




         pass
       def 				lowercase_				(  self							)  ->						int:





         """simple docstring"""




         _lowercase			,					_lowercase:      Any   			=  self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           _lowercase:      Optional[Any]   			=  model_class(A_							)
           self.assertIsInstance(model.get_input_embeddings()			,					(nn.Module)							)
           _lowercase:      Optional[int]   			=  model.get_output_embeddings()
           self.assertTrue(x is None or isinstance(A_			,					nn.Linear							)							)
       def 				lowercase_				(  self							)  ->						int:





         """simple docstring"""




         _lowercase			,					_lowercase:      Optional[Any]   			=  self.model_tester.prepare_config_and_inputs_for_common()

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

           _lowercase:      int   			=  ['''pixel_values''']
           self.assertListEqual(arg_names[:1]			,					A_							)
       def 				lowercase_				(  self							)  ->						Tuple:





         """simple docstring"""




         _lowercase:      Union[str, Any]   			=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*A_							)
       def 				lowercase_				(  self							)  ->						int:





         """simple docstring"""




         _lowercase			,					_lowercase:      int   			=  self.model_tester.prepare_config_and_inputs_for_common()
         _lowercase:      Tuple   			=  True

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

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

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

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

           _lowercase:      List[Any]   			=  1
           self.assertEqual(out_len + added_hidden_states			,					len(A_							)							)

           _lowercase:      Union[str, Any]   			=  outputs.attentions

           self.assertEqual(len(A_							)			,					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 				lowercase_				(  self							)  ->						Optional[Any]:





         """simple docstring"""




         def check_hidden_states_output(A_			,					A_			,					A_							):
           _lowercase:      Union[str, Any]   			=  model_class(A_							)
           model.to(A_							)
           model.eval()

           with torch.no_grad():
             _lowercase:      int   			=  model(**self._prepare_for_class(A_			,					A_							)							)

           _lowercase:      Union[str, Any]   			=  outputs.hidden_states

           _lowercase:      Dict   			=  getattr(
               self.model_tester			,					'''expected_num_hidden_layers'''			,					self.model_tester.num_hidden_layers + 1							)
           self.assertEqual(len(A_							)			,					A_							)

           # YOLOS has a different seq_length
           _lowercase:      Union[str, Any]   			=  self.model_tester.expected_seq_len

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

         _lowercase			,					_lowercase:      str   			=  self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           _lowercase:      List[str]   			=  True
           check_hidden_states_output(A_			,					A_			,					A_							)

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

           check_hidden_states_output(A_			,					A_			,					A_							)
       def 				lowercase_				(  self							)  ->						Tuple:





         """simple docstring"""




         _lowercase:      Optional[int]   			=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_object_detection(*A_							)



       @slow
       def 				lowercase_				(  self							)  ->						Dict:





         """simple docstring"""




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




def 			_lowerCAmelCase		(		):






  """simple docstring"""



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







@require_torch
@require_vision
class       __magic_name__							(							unittest.TestCase			):
       @cached_property
       def 				lowercase_				(  self							)  ->						Union[str, Any]:





         """simple docstring"""




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



       @slow
       def 				lowercase_				(  self							)  ->						List[Any]:





         """simple docstring"""




         _lowercase:      Tuple   			=  YolosForObjectDetection.from_pretrained('''hustvl/yolos-small'''							).to(A_							)

         _lowercase:      int   			=  self.default_image_processor
         _lowercase:      Optional[int]   			=  prepare_img()
         _lowercase:      str   			=  image_processor(images=A_			,					return_tensors='''pt'''							).to(A_							)

         # forward pass
         with torch.no_grad():
           _lowercase:      Optional[int]   			=  model(inputs.pixel_values							)

         # verify outputs
         _lowercase:      Optional[Any]   			=  torch.Size((1, 100, 92)							)
         self.assertEqual(outputs.logits.shape			,					A_							)

         _lowercase:      Dict   			=  torch.tensor(
             [[-24.02_48, -10.30_24, -14.82_90], [-42.03_92, -16.82_00, -27.43_34], [-27.27_43, -11.81_54, -18.71_48]]			,					device=A_			,					)
         _lowercase:      List[Any]   			=  torch.tensor(
             [[0.25_59, 0.54_55, 0.47_06], [0.29_89, 0.72_79, 0.18_75], [0.77_32, 0.40_17, 0.44_62]]			,					device=A_							)
         self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]			,					A_			,					atol=1E-4							)							)
         self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]			,					A_			,					atol=1E-4							)							)

         # verify postprocessing
         _lowercase:      Optional[Any]   			=  image_processor.post_process_object_detection(
             A_			,					threshold=0.3			,					target_sizes=[image.size[::-1]]							)[0]
         _lowercase:      List[Any]   			=  torch.tensor([0.99_94, 0.97_90, 0.99_64, 0.99_72, 0.98_61]							).to(A_							)
         _lowercase:      Union[str, Any]   			=  [75, 75, 17, 63, 17]
         _lowercase:      int   			=  torch.tensor([3_35.06_09, 79.38_48, 3_75.42_16, 1_87.24_95]							).to(A_							)

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


"""simple docstring"""




import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from ...utils import logging
from ..auto import CONFIG_MAPPING


A__ :       List[Any]				= logging.get_logger(__name__)

A__ :       Tuple				= {
    'Salesforce/instruct-blip-flan-t5': 'https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json',
}


class       __magic_name__							(							SCREAMING_SNAKE_CASE__			):
       UpperCamelCase_ =		'''instructblip_vision_model'''
       def __init__(  self			,					A_=1408			,					A_=6144			,					A_=39			,					A_=16			,					A_=224			,					A_=14			,					A_="gelu"			,					A_=1E-6			,					A_=0.0			,					A_=1E-10			,					A_=True			,					**A_			,					)  ->						Tuple:





         """simple docstring"""




         super().__init__(**A_							)

         _lowercase:      List[str]   			=  hidden_size
         _lowercase:      Tuple   			=  intermediate_size
         _lowercase:      List[str]   			=  num_hidden_layers
         _lowercase:      Union[str, Any]   			=  num_attention_heads
         _lowercase:      str   			=  patch_size
         _lowercase:      Union[str, Any]   			=  image_size
         _lowercase:      int   			=  initializer_range
         _lowercase:      List[str]   			=  attention_dropout
         _lowercase:      Tuple   			=  layer_norm_eps
         _lowercase:      Tuple   			=  hidden_act
         _lowercase:      Optional[int]   			=  qkv_bias



       @classmethod
       def 				lowercase_				(  cls			,					A_			,					**A_							)  ->						"PretrainedConfig":





         """simple docstring"""




         cls._set_token_in_kwargs(A_							)

         _lowercase			,					_lowercase:      str   			=  cls.get_config_dict(A_			,					**A_							)

         # get the vision config dict if we are loading from InstructBlipConfig
         if config_dict.get('''model_type'''							) == "instructblip":
           _lowercase:      Dict   			=  config_dict['''vision_config''']

         if "model_type" in config_dict and hasattr(cls			,					'''model_type'''							) and config_dict["model_type"] != cls.model_type:
           logger.warning(
               f'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
               f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''							)

         return cls.from_dict(A_			,					**A_							)


class       __magic_name__							(							SCREAMING_SNAKE_CASE__			):
       UpperCamelCase_ =		'''instructblip_qformer'''
       def __init__(  self			,					A_=3_0522			,					A_=768			,					A_=12			,					A_=12			,					A_=3072			,					A_="gelu"			,					A_=0.1			,					A_=0.1			,					A_=512			,					A_=0.02			,					A_=1E-12			,					A_=0			,					A_="absolute"			,					A_=2			,					A_=1408			,					**A_			,					)  ->						Optional[int]:





         """simple docstring"""




         super().__init__(pad_token_id=A_			,					**A_							)

         _lowercase:      Optional[Any]   			=  vocab_size
         _lowercase:      Optional[int]   			=  hidden_size
         _lowercase:      Dict   			=  num_hidden_layers
         _lowercase:      Union[str, Any]   			=  num_attention_heads
         _lowercase:      List[Any]   			=  hidden_act
         _lowercase:      Union[str, Any]   			=  intermediate_size
         _lowercase:      Union[str, Any]   			=  hidden_dropout_prob
         _lowercase:      Tuple   			=  attention_probs_dropout_prob
         _lowercase:      Optional[int]   			=  max_position_embeddings
         _lowercase:      Optional[int]   			=  initializer_range
         _lowercase:      Optional[Any]   			=  layer_norm_eps
         _lowercase:      Optional[Any]   			=  position_embedding_type
         _lowercase:      Tuple   			=  cross_attention_frequency
         _lowercase:      str   			=  encoder_hidden_size



       @classmethod
       def 				lowercase_				(  cls			,					A_			,					**A_							)  ->						"PretrainedConfig":





         """simple docstring"""




         cls._set_token_in_kwargs(A_							)

         _lowercase			,					_lowercase:      Tuple   			=  cls.get_config_dict(A_			,					**A_							)

         # get the qformer config dict if we are loading from InstructBlipConfig
         if config_dict.get('''model_type'''							) == "instructblip":
           _lowercase:      Tuple   			=  config_dict['''qformer_config''']

         if "model_type" in config_dict and hasattr(cls			,					'''model_type'''							) and config_dict["model_type"] != cls.model_type:
           logger.warning(
               f'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
               f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''							)

         return cls.from_dict(A_			,					**A_							)


class       __magic_name__							(							SCREAMING_SNAKE_CASE__			):
       UpperCamelCase_ =		'''instructblip'''
       UpperCamelCase_ =		True
       def __init__(  self			,					A_=None			,					A_=None			,					A_=None			,					A_=32			,					**A_							)  ->						str:





         """simple docstring"""




         super().__init__(**A_							)

         if vision_config is None:
           _lowercase:      str   			=  {}
           logger.info('''vision_config is None. initializing the InstructBlipVisionConfig with default values.'''							)

         if qformer_config is None:
           _lowercase:      List[str]   			=  {}
           logger.info('''qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.'''							)

         if text_config is None:
           _lowercase:      Any   			=  {}
           logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).'''							)

         _lowercase:      int   			=  InstructBlipVisionConfig(**A_							)
         _lowercase:      Optional[Any]   			=  InstructBlipQFormerConfig(**A_							)
         _lowercase:      Optional[Any]   			=  text_config['''model_type'''] if '''model_type''' in text_config else '''opt'''
         _lowercase:      Optional[Any]   			=  CONFIG_MAPPING[text_model_type](**A_							)

         _lowercase:      Tuple   			=  self.text_config.tie_word_embeddings
         _lowercase:      Dict   			=  self.text_config.is_encoder_decoder

         _lowercase:      List[Any]   			=  num_query_tokens
         _lowercase:      Any   			=  self.vision_config.hidden_size
         _lowercase:      List[Any]   			=  self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
         _lowercase:      Union[str, Any]   			=  1.0
         _lowercase:      Dict   			=  0.02
       @classmethod
       def 				lowercase_				(  cls			,					A_			,					A_			,					A_			,					**A_			,					)  ->						str:





         """simple docstring"""




         return cls(
             vision_config=vision_config.to_dict()			,					qformer_config=qformer_config.to_dict()			,					text_config=text_config.to_dict()			,					**A_			,					)



       def 				lowercase_				(  self							)  ->						Union[str, Any]:





         """simple docstring"""




         _lowercase:      List[str]   			=  copy.deepcopy(self.__dict__							)
         _lowercase:      Tuple   			=  self.vision_config.to_dict()
         _lowercase:      Tuple   			=  self.qformer_config.to_dict()
         _lowercase:      int   			=  self.text_config.to_dict()
         _lowercase:      Any   			=  self.__class__.model_type
         return output
