



"""simple docstring"""






import inspect
import unittest

from transformers import ViTHybridConfig
from transformers.testing_utils import require_accelerate, 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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch
   from torch import nn

   from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel
   from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image






class        __snake_case       :




  def __init__( self     :						Dict						,					__lowerCAmelCase     :						List[str]						,					__lowerCAmelCase     :						Any=1_3						,					__lowerCAmelCase     :						Dict=6_4						,					__lowerCAmelCase     :						Tuple=2						,					__lowerCAmelCase     :						int=3						,					__lowerCAmelCase     :						str=True						,					__lowerCAmelCase     :						Dict=True						,					__lowerCAmelCase     :						Union[str, Any]=3_2						,					__lowerCAmelCase     :						Dict=5						,					__lowerCAmelCase     :						List[Any]=4						,					__lowerCAmelCase     :						Tuple=3_7						,					__lowerCAmelCase     :						Tuple="gelu"						,					__lowerCAmelCase     :						Tuple=0.1						,					__lowerCAmelCase     :						Optional[Any]=0.1						,					__lowerCAmelCase     :						Optional[int]=1_0						,					__lowerCAmelCase     :						List[Any]=0.02						,					__lowerCAmelCase     :						Union[str, Any]=[1, 1_6, 4, 4]						,					__lowerCAmelCase     :						List[Any]=None						,					):






         """simple docstring"""






         _lowerCamelCase						:				Optional[int]    			= parent
         _lowerCamelCase						:				Optional[Any]    			= batch_size
         _lowerCamelCase						:				Dict    			= image_size
         _lowerCamelCase						:				List[str]    			= patch_size
         _lowerCamelCase						:				Any    			= num_channels
         _lowerCamelCase						:				Union[str, Any]    			= is_training
         _lowerCamelCase						:				Optional[int]    			= use_labels
         _lowerCamelCase						:				Optional[Any]    			= hidden_size
         _lowerCamelCase						:				List[Any]    			= num_hidden_layers
         _lowerCamelCase						:				List[Any]    			= num_attention_heads
         _lowerCamelCase						:				Optional[Any]    			= intermediate_size
         _lowerCamelCase						:				Any    			= hidden_act
         _lowerCamelCase						:				int    			= hidden_dropout_prob
         _lowerCamelCase						:				str    			= attention_probs_dropout_prob
         _lowerCamelCase						:				Tuple    			= type_sequence_label_size
         _lowerCamelCase						:				int    			= initializer_range
         _lowerCamelCase						:				Any    			= scope
         _lowerCamelCase						:				Tuple    			= backbone_featmap_shape

         # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
         # the number of patches is based on the feature map of the backbone, which by default uses an output stride
         # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size
         _lowerCamelCase						:				Optional[int]    			= (self.image_size // 3_2) ** 2
         _lowerCamelCase						:				Dict    			= num_patches + 1




  def 						SCREAMING_SNAKE_CASE				( self     :						Dict		):






         """simple docstring"""






         _lowerCamelCase						:				str    			= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

         _lowerCamelCase						:				Union[str, Any]    			= None
         if self.use_labels:
                _lowerCamelCase						:				Any    			= ids_tensor([self.batch_size]						,					self.type_sequence_label_size		)

         _lowerCamelCase						:				List[Any]    			= self.get_config()

         return config, pixel_values, labels




  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase						:				List[Any]    			= {
             '''global_padding''': '''same''',
             '''layer_type''': '''bottleneck''',
             '''depths''': [3, 4, 9],
             '''out_features''': ['''stage1''', '''stage2''', '''stage3'''],
             '''embedding_dynamic_padding''': True,
             '''hidden_sizes''': [4, 8, 1_6, 3_2],
             '''num_groups''': 2,
         }

         return ViTHybridConfig(
             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=__lowerCAmelCase						,					initializer_range=self.initializer_range						,					backbone_featmap_shape=self.backbone_featmap_shape						,					backbone_config=__lowerCAmelCase						,					)




  def 						SCREAMING_SNAKE_CASE				( self     :						Dict						,					__lowerCAmelCase     :						Optional[int]						,					__lowerCAmelCase     :						str						,					__lowerCAmelCase     :						Dict		):






         """simple docstring"""






         _lowerCamelCase						:				Union[str, Any]    			= ViTHybridModel(config=__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()
         _lowerCamelCase						:				Optional[int]    			= model(__lowerCAmelCase		)
         self.parent.assertEqual(result.last_hidden_state.shape						,					(self.batch_size, self.seq_length, self.hidden_size)		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Union[str, Any]						,					__lowerCAmelCase     :						Any						,					__lowerCAmelCase     :						Optional[Any]						,					__lowerCAmelCase     :						Tuple		):






         """simple docstring"""






         _lowerCamelCase						:				Union[str, Any]    			= self.type_sequence_label_size
         _lowerCamelCase						:				List[str]    			= ViTHybridForImageClassification(__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()
         _lowerCamelCase						:				str    			= model(__lowerCAmelCase						,					labels=__lowerCAmelCase		)
         self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.type_sequence_label_size)		)




  def 						SCREAMING_SNAKE_CASE				( self     :						int		):






         """simple docstring"""






         _lowerCamelCase						:				Tuple    			= self.prepare_config_and_inputs()
         _lowerCamelCase			,    _lowerCamelCase			,    _lowerCamelCase						:				Tuple    			= config_and_inputs
         _lowerCamelCase						:				List[str]    			= {'''pixel_values''': pixel_values}
         return config, inputs_dict






@require_torch
class        __snake_case       (	_lowercase  ,     _lowercase  ,     unittest.TestCase):
  snake_case__				:   Dict  			=						(ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
  snake_case__				:   Union[str, Any]  			=						(
      {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification}
      if is_torch_available()
      else {}
  )
  snake_case__				:   List[str]  			=						False
  snake_case__				:   List[str]  			=						False
  snake_case__				:   Dict  			=						False




  def 						SCREAMING_SNAKE_CASE				( self     :						str		):






         """simple docstring"""






         _lowerCamelCase						:				int    			= ViTHybridModelTester(self		)
         _lowerCamelCase						:				List[str]    			= ConfigTester(self						,					config_class=__lowerCAmelCase						,					has_text_modality=__lowerCAmelCase						,					hidden_size=3_7		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Any		):






         """simple docstring"""






         self.config_tester.run_common_tests()




  @unittest.skip(reason='''ViT does not use inputs_embeds'''		)
  def 						SCREAMING_SNAKE_CASE				( self     :						int		):






         """simple docstring"""






         pass




  def 						SCREAMING_SNAKE_CASE				( self     :						str		):






         """simple docstring"""






         _lowerCamelCase			,    _lowerCamelCase						:				Dict    			= self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
                _lowerCamelCase						:				str    			= model_class(__lowerCAmelCase		)
                self.assertIsInstance(model.get_input_embeddings()						,					(nn.Module)		)
                _lowerCamelCase						:				List[Any]    			= model.get_output_embeddings()
                self.assertTrue(x is None or isinstance(__lowerCAmelCase						,					nn.Linear		)		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase			,    _lowerCamelCase						:				Tuple    			= self.model_tester.prepare_config_and_inputs_for_common()

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

                _lowerCamelCase						:				List[str]    			= ['''pixel_values''']
                self.assertListEqual(arg_names[:1]						,					__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE				( self     :						List[str]		):






         """simple docstring"""






         _lowerCamelCase						:				Union[str, Any]    			= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE				( self     :						str		):






         """simple docstring"""






         _lowerCamelCase						:				int    			= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Any		):






         """simple docstring"""






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

         _lowerCamelCase						:				Dict    			= _config_zero_init(__lowerCAmelCase		)
         for model_class in self.all_model_classes:
                _lowerCamelCase						:				Optional[Any]    			= model_class(config=__lowerCAmelCase		)
                # Skip the check for the backbone
                for name, module in model.named_modules():
                       if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
                              _lowerCamelCase						:				List[str]    			= [f'''{name}.{key}''' for key in module.state_dict().keys()]
                              break

                for name, param in model.named_parameters():
                       if param.requires_grad:
                              if name in backbone_params:
                                     continue
                              self.assertIn(
                                  ((param.data.mean() * 1E9).round() / 1E9).item()						,					[0.0, 1.0]						,					msg=f'''Parameter {name} of model {model_class} seems not properly initialized'''						,					)




  @slow
  def 						SCREAMING_SNAKE_CASE				( self     :						List[Any]		):






         """simple docstring"""






         for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                _lowerCamelCase						:				Union[str, Any]    			= ViTHybridModel.from_pretrained(__lowerCAmelCase		)
                self.assertIsNotNone(__lowerCAmelCase		)






def        snake_case_  (      ):


       '''simple docstring'''





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







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




  @cached_property
  def 						SCREAMING_SNAKE_CASE				( self     :						Tuple		):






         """simple docstring"""






         return (
             ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)
             if is_vision_available()
             else None
         )




  @slow
  def 						SCREAMING_SNAKE_CASE				( self     :						Union[str, Any]		):






         """simple docstring"""






         _lowerCamelCase						:				List[str]    			= ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]		).to(
             __lowerCAmelCase		)

         _lowerCamelCase						:				Dict    			= self.default_image_processor
         _lowerCamelCase						:				Optional[Any]    			= prepare_img()
         _lowerCamelCase						:				Dict    			= image_processor(images=__lowerCAmelCase						,					return_tensors='''pt'''		).to(__lowerCAmelCase		)

         # forward pass
         with torch.no_grad():
                _lowerCamelCase						:				str    			= model(**__lowerCAmelCase		)

         # verify the logits
         _lowerCamelCase						:				Union[str, Any]    			= torch.Size((1, 1_0_0_0)		)
         self.assertEqual(outputs.logits.shape						,					__lowerCAmelCase		)

         _lowerCamelCase						:				str    			= torch.tensor([-1.90_90, -0.49_93, -0.23_89]		).to(__lowerCAmelCase		)

         self.assertTrue(torch.allclose(outputs.logits[0, :3]						,					__lowerCAmelCase						,					atol=1E-4		)		)




  @slow
  @require_accelerate
  def 						SCREAMING_SNAKE_CASE				( self     :						List[str]		):






         """simple docstring"""






         _lowerCamelCase						:				str    			= ViTHybridImageProcessor.from_pretrained('''google/vit-hybrid-base-bit-384'''		)
         _lowerCamelCase						:				Any    			= ViTHybridForImageClassification.from_pretrained('''google/vit-hybrid-base-bit-384'''						,					device_map='''auto'''		)

         _lowerCamelCase						:				Any    			= prepare_img()

         _lowerCamelCase						:				Dict    			= image_processor(images=__lowerCAmelCase						,					return_tensors='''pt'''		)
         _lowerCamelCase						:				List[Any]    			= model(**__lowerCAmelCase		)
         _lowerCamelCase						:				Optional[Any]    			= outputs.logits
         # model predicts one of the 1000 ImageNet classes
         _lowerCamelCase						:				List[Any]    			= logits.argmax(-1		).item()

         self.assertTrue(model.config.idalabel[predicted_class_idx]						,					'''tabby, tabby cat'''		)




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_UpperCAmelCase					: Union[str, Any]      =       {
    "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					: List[Any]      =       [
					    "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					: Union[str, Any]      =       _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
