






'''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:      Tuple  				=				logging.get_logger(__name__)

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






class 							__magic_name__     (	UpperCAmelCase_				):







	"""simple docstring"""



	SCREAMING_SNAKE_CASE_					:      List[Any]        =  'blip_2_vision_model'
	def __init__(							self  ,      _lowercase=1408  ,      _lowercase=6144  ,      _lowercase=39  ,      _lowercase=16  ,      _lowercase=224  ,      _lowercase=14  ,      _lowercase="gelu"  ,      _lowercase=0.0_00_01  ,      _lowercase=0.0  ,      _lowercase=1E-1_0  ,      _lowercase=True  ,      **_lowercase  ,      )						->  Optional[int]:
							super().__init__(**_lowercase       )

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







	@classmethod
	def        lowerCamelCase__		(							cls  ,      _lowercase  ,      **_lowercase       )						->  "PretrainedConfig":
							cls._set_token_in_kwargs(_lowercase       )

							lowercase_  ,      lowercase_      :   Any        =			cls.get_config_dict(_lowercase  ,      **_lowercase       )

							# get the vision config dict if we are loading from Blip2Config
							if config_dict.get('model_type'       ) == "blip-2":
													lowercase_      :   Union[str, Any]        =			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(_lowercase  ,      **_lowercase       )






class 							__magic_name__     (	UpperCAmelCase_				):







	"""simple docstring"""



	SCREAMING_SNAKE_CASE_					:      int        =  'blip_2_qformer'
	def __init__(							self  ,      _lowercase=3_0522  ,      _lowercase=768  ,      _lowercase=12  ,      _lowercase=12  ,      _lowercase=3072  ,      _lowercase="gelu"  ,      _lowercase=0.1  ,      _lowercase=0.1  ,      _lowercase=512  ,      _lowercase=0.02  ,      _lowercase=1E-1_2  ,      _lowercase=0  ,      _lowercase="absolute"  ,      _lowercase=2  ,      _lowercase=1408  ,      **_lowercase  ,      )						->  Any:
							super().__init__(pad_token_id=_lowercase  ,      **_lowercase       )

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







	@classmethod
	def        lowerCamelCase__		(							cls  ,      _lowercase  ,      **_lowercase       )						->  "PretrainedConfig":
							cls._set_token_in_kwargs(_lowercase       )

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

							# get the qformer config dict if we are loading from Blip2Config
							if config_dict.get('model_type'       ) == "blip-2":
													lowercase_      :   Union[str, Any]        =			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(_lowercase  ,      **_lowercase       )






class 							__magic_name__     (	UpperCAmelCase_				):







	"""simple docstring"""



	SCREAMING_SNAKE_CASE_					:      Optional[Any]        =  'blip-2'
	SCREAMING_SNAKE_CASE_					:      str        =  True
	def __init__(							self  ,      _lowercase=None  ,      _lowercase=None  ,      _lowercase=None  ,      _lowercase=32  ,      **_lowercase       )						->  int:
							super().__init__(**_lowercase       )

							if vision_config is None:
													lowercase_      :   Tuple        =			{}
													logger.info('vision_config is None. initializing the Blip2VisionConfig with default values.'       )

							if qformer_config is None:
													lowercase_      :   str        =			{}
													logger.info('qformer_config is None. Initializing the Blip2QFormerConfig with default values.'       )

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

							lowercase_      :   List[str]        =			BlipaVisionConfig(**_lowercase       )
							lowercase_      :   Union[str, Any]        =			BlipaQFormerConfig(**_lowercase       )
							lowercase_      :   List[Any]        =			text_config['model_type'] if 'model_type' in text_config else 'opt'
							lowercase_      :   Any        =			CONFIG_MAPPING[text_model_type](**_lowercase       )

							lowercase_      :   int        =			self.text_config.tie_word_embeddings
							lowercase_      :   int        =			self.text_config.is_encoder_decoder

							lowercase_      :   Tuple        =			num_query_tokens
							lowercase_      :   Tuple        =			self.vision_config.hidden_size
							lowercase_      :   Union[str, Any]        =			self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
							lowercase_      :   Any        =			1.0
							lowercase_      :   Union[str, Any]        =			0.02
	@classmethod
	def        lowerCamelCase__		(							cls  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      **_lowercase  ,      )						->  Optional[int]:
							return cls(
							    vision_config=vision_config.to_dict()  ,      qformer_config=qformer_config.to_dict()  ,      text_config=text_config.to_dict()  ,      **_lowercase  ,      )







	def        lowerCamelCase__		(							self       )						->  Any:
							lowercase_      :   List[Any]        =			copy.deepcopy(self.__dict__       )
							lowercase_      :   int        =			self.vision_config.to_dict()
							lowercase_      :   Any        =			self.qformer_config.to_dict()
							lowercase_      :   Optional[int]        =			self.text_config.to_dict()
							lowercase_      :   str        =			self.__class__.model_type
							return output








'''simple docstring'''

import unittest

from diffusers.pipelines.pipeline_utils import is_safetensors_compatible






class 							__magic_name__     (	unittest.TestCase				):







	"""simple docstring"""



	def        lowerCamelCase__		(							self       )						->  Optional[Any]:
							lowercase_      :   Tuple        =			[
							    'safety_checker/pytorch_model.bin',
							    'safety_checker/model.safetensors',
							    'vae/diffusion_pytorch_model.bin',
							    'vae/diffusion_pytorch_model.safetensors',
							    'text_encoder/pytorch_model.bin',
							    'text_encoder/model.safetensors',
							    'unet/diffusion_pytorch_model.bin',
							    'unet/diffusion_pytorch_model.safetensors',
							]
							self.assertTrue(is_safetensors_compatible(_lowercase       )       )
	def        lowerCamelCase__		(							self       )						->  Dict:
							lowercase_      :   Tuple        =			[
							    'unet/diffusion_pytorch_model.bin',
							    'unet/diffusion_pytorch_model.safetensors',
							]
							self.assertTrue(is_safetensors_compatible(_lowercase       )       )
	def        lowerCamelCase__		(							self       )						->  str:
							lowercase_      :   int        =			[
							    'safety_checker/pytorch_model.bin',
							    'safety_checker/model.safetensors',
							    'vae/diffusion_pytorch_model.bin',
							    'vae/diffusion_pytorch_model.safetensors',
							    'text_encoder/pytorch_model.bin',
							    'text_encoder/model.safetensors',
							    'unet/diffusion_pytorch_model.bin',
							    # Removed: 'unet/diffusion_pytorch_model.safetensors',
							]
							self.assertFalse(is_safetensors_compatible(_lowercase       )       )
	def        lowerCamelCase__		(							self       )						->  Any:
							lowercase_      :   Dict        =			[
							    'text_encoder/pytorch_model.bin',
							    'text_encoder/model.safetensors',
							]
							self.assertTrue(is_safetensors_compatible(_lowercase       )       )
	def        lowerCamelCase__		(							self       )						->  Optional[Any]:
							lowercase_      :   List[Any]        =			[
							    'safety_checker/pytorch_model.bin',
							    'safety_checker/model.safetensors',
							    'vae/diffusion_pytorch_model.bin',
							    'vae/diffusion_pytorch_model.safetensors',
							    'text_encoder/pytorch_model.bin',
							    # Removed: 'text_encoder/model.safetensors',
							    'unet/diffusion_pytorch_model.bin',
							    'unet/diffusion_pytorch_model.safetensors',
							]
							self.assertFalse(is_safetensors_compatible(_lowercase       )       )
	def        lowerCamelCase__		(							self       )						->  Optional[int]:
							lowercase_      :   str        =			[
							    'safety_checker/pytorch_model.fp16.bin',
							    'safety_checker/model.fp16.safetensors',
							    'vae/diffusion_pytorch_model.fp16.bin',
							    'vae/diffusion_pytorch_model.fp16.safetensors',
							    'text_encoder/pytorch_model.fp16.bin',
							    'text_encoder/model.fp16.safetensors',
							    'unet/diffusion_pytorch_model.fp16.bin',
							    'unet/diffusion_pytorch_model.fp16.safetensors',
							]
							lowercase_      :   Union[str, Any]        =			'fp16'
							self.assertTrue(is_safetensors_compatible(_lowercase  ,      variant=_lowercase       )       )
	def        lowerCamelCase__		(							self       )						->  int:
							lowercase_      :   Optional[int]        =			[
							    'unet/diffusion_pytorch_model.fp16.bin',
							    'unet/diffusion_pytorch_model.fp16.safetensors',
							]
							lowercase_      :   Dict        =			'fp16'
							self.assertTrue(is_safetensors_compatible(_lowercase  ,      variant=_lowercase       )       )
	def        lowerCamelCase__		(							self       )						->  int:
							# pass variant but use the non-variant filenames
							lowercase_      :   Optional[int]        =			[
							    'unet/diffusion_pytorch_model.bin',
							    'unet/diffusion_pytorch_model.safetensors',
							]
							lowercase_      :   Dict        =			'fp16'
							self.assertTrue(is_safetensors_compatible(_lowercase  ,      variant=_lowercase       )       )
	def        lowerCamelCase__		(							self       )						->  Union[str, Any]:
							lowercase_      :   int        =			[
							    'safety_checker/pytorch_model.fp16.bin',
							    'safety_checker/model.fp16.safetensors',
							    'vae/diffusion_pytorch_model.fp16.bin',
							    'vae/diffusion_pytorch_model.fp16.safetensors',
							    'text_encoder/pytorch_model.fp16.bin',
							    'text_encoder/model.fp16.safetensors',
							    'unet/diffusion_pytorch_model.fp16.bin',
							    # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors',
							]
							lowercase_      :   Dict        =			'fp16'
							self.assertFalse(is_safetensors_compatible(_lowercase  ,      variant=_lowercase       )       )
	def        lowerCamelCase__		(							self       )						->  int:
							lowercase_      :   str        =			[
							    'text_encoder/pytorch_model.fp16.bin',
							    'text_encoder/model.fp16.safetensors',
							]
							lowercase_      :   str        =			'fp16'
							self.assertTrue(is_safetensors_compatible(_lowercase  ,      variant=_lowercase       )       )
	def        lowerCamelCase__		(							self       )						->  List[str]:
							# pass variant but use the non-variant filenames
							lowercase_      :   List[Any]        =			[
							    'text_encoder/pytorch_model.bin',
							    'text_encoder/model.safetensors',
							]
							lowercase_      :   Dict        =			'fp16'
							self.assertTrue(is_safetensors_compatible(_lowercase  ,      variant=_lowercase       )       )







	def        lowerCamelCase__		(							self       )						->  List[str]:
							lowercase_      :   Union[str, Any]        =			[
							    'safety_checker/pytorch_model.fp16.bin',
							    'safety_checker/model.fp16.safetensors',
							    'vae/diffusion_pytorch_model.fp16.bin',
							    'vae/diffusion_pytorch_model.fp16.safetensors',
							    'text_encoder/pytorch_model.fp16.bin',
							    # 'text_encoder/model.fp16.safetensors',
							    'unet/diffusion_pytorch_model.fp16.bin',
							    'unet/diffusion_pytorch_model.fp16.safetensors',
							]
							lowercase_      :   Dict        =			'fp16'
							self.assertFalse(is_safetensors_compatible(_lowercase  ,      variant=_lowercase       )       )
