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


__SCREAMING_SNAKE_CASE       :     Optional[Any]     =  logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE       :     List[Any]     =  {
    """salesforce/blip2-opt-2.7b""": """https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json""",
}

class 	lowerCamelCase_	(snake_case__    ):
      '''simple docstring'''

      __UpperCamelCase:  int						=						"blip_2_vision_model"




      def __init__(       self						:				Optional[Any]						,					A						:				Union[str, Any]=1408						,					A						:				int=6144						,					A						:				Union[str, Any]=39						,					A						:				List[str]=16						,					A						:				List[str]=224						,					A						:				List[Any]=14						,					A						:				int="gelu"						,					A						:				Optional[Any]=0.00_001						,					A						:				str=0.0						,					A						:				List[str]=1E-10						,					A						:				Dict=True						,					**A						:				Optional[Any]						,					):
             super().__init__(**A       )

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





      @classmethod
      def      _A	(       cls						:				Tuple						,					A						:				Union[str, os.PathLike]						,					**A						:				List[str]       ):
             cls._set_token_in_kwargs(A       )

             _UpperCAmelCase      ,  _UpperCAmelCase     :  List[Any]							      =    cls.get_config_dict(A						,					**A       )

             # get the vision config dict if we are loading from Blip2Config
             if config_dict.get("model_type"       ) == "blip-2":
                    _UpperCAmelCase     :  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 	lowerCamelCase_	(snake_case__    ):
      '''simple docstring'''

      __UpperCamelCase:  int						=						"blip_2_qformer"




      def __init__(       self						:				List[str]						,					A						:				Dict=30522						,					A						:				Optional[Any]=768						,					A						:				List[str]=12						,					A						:				int=12						,					A						:				List[Any]=3072						,					A						:				List[Any]="gelu"						,					A						:				List[str]=0.1						,					A						:				int=0.1						,					A						:				Any=512						,					A						:				List[Any]=0.02						,					A						:				List[Any]=1E-12						,					A						:				Tuple=0						,					A						:				int="absolute"						,					A						:				str=2						,					A						:				List[Any]=1408						,					**A						:				Optional[int]						,					):
             super().__init__(pad_token_id=A						,					**A       )

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





      @classmethod
      def      _A	(       cls						:				Optional[Any]						,					A						:				Union[str, os.PathLike]						,					**A						:				Optional[int]       ):
             cls._set_token_in_kwargs(A       )

             _UpperCAmelCase      ,  _UpperCAmelCase     :  str							      =    cls.get_config_dict(A						,					**A       )

             # get the qformer config dict if we are loading from Blip2Config
             if config_dict.get("model_type"       ) == "blip-2":
                    _UpperCAmelCase     :  List[str]							      =    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 	lowerCamelCase_	(snake_case__    ):
      '''simple docstring'''

      __UpperCamelCase:  int						=						"blip-2"
      __UpperCamelCase:  List[Any]						=						True




      def __init__(       self						:				str						,					A						:				Optional[Any]=None						,					A						:				List[Any]=None						,					A						:				str=None						,					A						:				Optional[int]=32						,					**A						:				Union[str, Any]       ):
             super().__init__(**A       )

             if vision_config is None:
                    _UpperCAmelCase     :  Dict							      =    {}
                    logger.info("vision_config is None. initializing the Blip2VisionConfig with default values."       )

             if qformer_config is None:
                    _UpperCAmelCase     :  int							      =    {}
                    logger.info("qformer_config is None. Initializing the Blip2QFormerConfig with default values."       )

             if text_config is None:
                    _UpperCAmelCase     :  int							      =    {}
                    logger.info("text_config is None. Initializing the text config with default values (`OPTConfig`)."       )

             _UpperCAmelCase     :  Dict							      =    BlipaVisionConfig(**A       )
             _UpperCAmelCase     :  Optional[int]							      =    BlipaQFormerConfig(**A       )
             _UpperCAmelCase     :  str							      =    text_config["model_type"] if "model_type" in text_config else "opt"
             _UpperCAmelCase     :  int							      =    CONFIG_MAPPING[text_model_type](**A       )

             _UpperCAmelCase     :  Any							      =    self.text_config.tie_word_embeddings
             _UpperCAmelCase     :  Any							      =    self.text_config.is_encoder_decoder

             _UpperCAmelCase     :  List[str]							      =    num_query_tokens
             _UpperCAmelCase     :  Optional[int]							      =    self.vision_config.hidden_size
             _UpperCAmelCase     :  List[Any]							      =    self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
             _UpperCAmelCase     :  int							      =    1.0
             _UpperCAmelCase     :  Optional[Any]							      =    0.02




      @classmethod
      def      _A	(       cls						:				List[Any]						,					A						:				BlipaVisionConfig						,					A						:				BlipaQFormerConfig						,					A						:				PretrainedConfig						,					**A						:				Any						,					):
             return cls(
                 vision_config=vision_config.to_dict()						,					qformer_config=qformer_config.to_dict()						,					text_config=text_config.to_dict()						,					**A						,					)





      def      _A	(       self						:				Optional[Any]       ):
             _UpperCAmelCase     :  Optional[Any]							      =    copy.deepcopy(self.__dict__       )
             _UpperCAmelCase     :  List[Any]							      =    self.vision_config.to_dict()
             _UpperCAmelCase     :  Tuple							      =    self.qformer_config.to_dict()
             _UpperCAmelCase     :  Dict							      =    self.text_config.to_dict()
             _UpperCAmelCase     :  Tuple							      =    self.__class__.model_type
             return output


'''simple docstring'''




import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import MobileViTImageProcessor

class 	lowerCamelCase_	(unittest.TestCase    ):
      '''simple docstring'''

      def __init__(       self						:				Optional[Any]						,					A						:				Dict						,					A						:				Dict=7						,					A						:				Optional[int]=3						,					A						:				Optional[int]=18						,					A						:				Dict=30						,					A						:				List[Any]=400						,					A						:				Union[str, Any]=True						,					A						:				Tuple=None						,					A						:				List[Any]=True						,					A						:				int=None						,					A						:				Optional[int]=True						,					):
             _UpperCAmelCase     :  Optional[int]							      =    size if size is not None else {"shortest_edge": 20}
             _UpperCAmelCase     :  Optional[Any]							      =    crop_size if crop_size is not None else {"height": 18, "width": 18}
             _UpperCAmelCase     :  List[Any]							      =    parent
             _UpperCAmelCase     :  Union[str, Any]							      =    batch_size
             _UpperCAmelCase     :  Optional[Any]							      =    num_channels
             _UpperCAmelCase     :  Union[str, Any]							      =    image_size
             _UpperCAmelCase     :  int							      =    min_resolution
             _UpperCAmelCase     :  Optional[int]							      =    max_resolution
             _UpperCAmelCase     :  List[str]							      =    do_resize
             _UpperCAmelCase     :  Optional[Any]							      =    size
             _UpperCAmelCase     :  Tuple							      =    do_center_crop
             _UpperCAmelCase     :  Optional[int]							      =    crop_size
             _UpperCAmelCase     :  Optional[Any]							      =    do_flip_channel_order





      def      _A	(       self						:				Dict       ):
             return {
                 "do_resize": self.do_resize,
                 "size": self.size,
                 "do_center_crop": self.do_center_crop,
                 "crop_size": self.crop_size,
                 "do_flip_channel_order": self.do_flip_channel_order,
             }

@require_torch
@require_vision
class 	lowerCamelCase_	(snake_case__							, unittest.TestCase    ):
      '''simple docstring'''

      __UpperCamelCase:  Tuple						=						MobileViTImageProcessor if is_vision_available() else None




      def      _A	(       self						:				List[Any]       ):
             _UpperCAmelCase     :  Any							      =    MobileViTImageProcessingTester(self       )




      @property
      def      _A	(       self						:				int       ):
             return self.image_processor_tester.prepare_image_processor_dict()




      def      _A	(       self						:				Tuple       ):
             _UpperCAmelCase     :  int							      =    self.image_processing_class(**self.image_processor_dict       )
             self.assertTrue(hasattr(A						,					"do_resize"       )       )
             self.assertTrue(hasattr(A						,					"size"       )       )
             self.assertTrue(hasattr(A						,					"do_center_crop"       )       )
             self.assertTrue(hasattr(A						,					"center_crop"       )       )
             self.assertTrue(hasattr(A						,					"do_flip_channel_order"       )       )




      def      _A	(       self						:				Any       ):
             _UpperCAmelCase     :  List[str]							      =    self.image_processing_class.from_dict(self.image_processor_dict       )
             self.assertEqual(image_processor.size						,					{"shortest_edge": 20}       )
             self.assertEqual(image_processor.crop_size						,					{"height": 18, "width": 18}       )

             _UpperCAmelCase     :  Dict							      =    self.image_processing_class.from_dict(self.image_processor_dict						,					size=42						,					crop_size=84       )
             self.assertEqual(image_processor.size						,					{"shortest_edge": 42}       )
             self.assertEqual(image_processor.crop_size						,					{"height": 84, "width": 84}       )




      def      _A	(       self						:				Any       ):
             pass




      def      _A	(       self						:				Dict       ):
             # Initialize image_processing
             _UpperCAmelCase     :  List[str]							      =    self.image_processing_class(**self.image_processor_dict       )
             # create random PIL images
             _UpperCAmelCase     :  Union[str, Any]							      =    prepare_image_inputs(self.image_processor_tester						,					equal_resolution=A       )
             for image in image_inputs:
                    self.assertIsInstance(A						,					Image.Image       )

             # Test not batched input
             _UpperCAmelCase     :  List[Any]							      =    image_processing(image_inputs[0]						,					return_tensors="pt"       ).pixel_values
             self.assertEqual(
                 encoded_images.shape						,					(
                     1,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size["height"],
                     self.image_processor_tester.crop_size["width"],
                 )						,					)

             # Test batched
             _UpperCAmelCase     :  Optional[Any]							      =    image_processing(A						,					return_tensors="pt"       ).pixel_values
             self.assertEqual(
                 encoded_images.shape						,					(
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size["height"],
                     self.image_processor_tester.crop_size["width"],
                 )						,					)




      def      _A	(       self						:				Union[str, Any]       ):
             # Initialize image_processing
             _UpperCAmelCase     :  Dict							      =    self.image_processing_class(**self.image_processor_dict       )
             # create random numpy tensors
             _UpperCAmelCase     :  Optional[Any]							      =    prepare_image_inputs(self.image_processor_tester						,					equal_resolution=A						,					numpify=A       )
             for image in image_inputs:
                    self.assertIsInstance(A						,					np.ndarray       )

             # Test not batched input
             _UpperCAmelCase     :  Optional[int]							      =    image_processing(image_inputs[0]						,					return_tensors="pt"       ).pixel_values
             self.assertEqual(
                 encoded_images.shape						,					(
                     1,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size["height"],
                     self.image_processor_tester.crop_size["width"],
                 )						,					)

             # Test batched
             _UpperCAmelCase     :  Optional[int]							      =    image_processing(A						,					return_tensors="pt"       ).pixel_values
             self.assertEqual(
                 encoded_images.shape						,					(
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size["height"],
                     self.image_processor_tester.crop_size["width"],
                 )						,					)





      def      _A	(       self						:				Any       ):
             # Initialize image_processing
             _UpperCAmelCase     :  Any							      =    self.image_processing_class(**self.image_processor_dict       )
             # create random PyTorch tensors
             _UpperCAmelCase     :  Optional[Any]							      =    prepare_image_inputs(self.image_processor_tester						,					equal_resolution=A						,					torchify=A       )
             for image in image_inputs:
                    self.assertIsInstance(A						,					torch.Tensor       )

             # Test not batched input
             _UpperCAmelCase     :  List[str]							      =    image_processing(image_inputs[0]						,					return_tensors="pt"       ).pixel_values
             self.assertEqual(
                 encoded_images.shape						,					(
                     1,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size["height"],
                     self.image_processor_tester.crop_size["width"],
                 )						,					)

             # Test batched
             _UpperCAmelCase     :  Any							      =    image_processing(A						,					return_tensors="pt"       ).pixel_values
             self.assertEqual(
                 encoded_images.shape						,					(
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size["height"],
                     self.image_processor_tester.crop_size["width"],
                 )						,					)

