






'''simple docstring'''
import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


a_					=	logging.get_logger(__name__)

a_					=	{
    'Salesforce/blip-vqa-base': 'https://huggingface.co/Salesforce/blip-vqa-base/resolve/main/config.json',
    'Salesforce/blip-vqa-capfit-large': (
        'https://huggingface.co/Salesforce/blip-vqa-base-capfit/resolve/main/config.json'
    ),
    'Salesforce/blip-image-captioning-base': (
        'https://huggingface.co/Salesforce/blip-image-captioning-base/resolve/main/config.json'
    ),
    'Salesforce/blip-image-captioning-large': (
        'https://huggingface.co/Salesforce/blip-image-captioning-large/resolve/main/config.json'
    ),
    'Salesforce/blip-itm-base-coco': 'https://huggingface.co/Salesforce/blip-itm-base-coco/resolve/main/config.json',
    'Salesforce/blip-itm-large-coco': 'https://huggingface.co/Salesforce/blip-itm-large-coco/resolve/main/config.json',
    'Salesforce/blip-itm-base-flikr': 'https://huggingface.co/Salesforce/blip-itm-base-flikr/resolve/main/config.json',
    'Salesforce/blip-itm-large-flikr': (
        'https://huggingface.co/Salesforce/blip-itm-large-flikr/resolve/main/config.json'
    ),
}






class        __SCREAMING_SNAKE_CASE (     lowerCamelCase			):
 snake_case_												=							"""blip_text_model"""




 def __init__( self		:							str   , __lowercase		:							List[Any]=3_05_24   , __lowercase		:							int=7_68   , __lowercase		:							Any=7_68   , __lowercase		:							Tuple=30_72   , __lowercase		:							List[str]=7_68   , __lowercase		:							Any=12   , __lowercase		:							Optional[int]=8   , __lowercase		:							Union[str, Any]=5_12   , __lowercase		:							Tuple="gelu"   , __lowercase		:							Any=1e-12   , __lowercase		:							int=0.0   , __lowercase		:							List[str]=0.0   , __lowercase		:							Dict=0.02   , __lowercase		:							Tuple=3_05_22   , __lowercase		:							str=2   , __lowercase		:							Tuple=0   , __lowercase		:							Optional[Any]=1_02   , __lowercase		:							List[Any]=True   , __lowercase		:							Tuple=True   , **__lowercase		:							List[Any]   , )		->  Optional[Any]:
       super().__init__(
           pad_token_id=__lowercase   , bos_token_id=__lowercase   , eos_token_id=__lowercase   , sep_token_id=__lowercase   , **__lowercase   , )

       SCREAMING_SNAKE_CASE__       :			Optional[int]													=vocab_size
       SCREAMING_SNAKE_CASE__       :			str													=hidden_size
       SCREAMING_SNAKE_CASE__       :			List[str]													=encoder_hidden_size
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=intermediate_size
       SCREAMING_SNAKE_CASE__       :			List[str]													=projection_dim
       SCREAMING_SNAKE_CASE__       :			str													=hidden_dropout_prob
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=num_hidden_layers
       SCREAMING_SNAKE_CASE__       :			int													=num_attention_heads
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=max_position_embeddings
       SCREAMING_SNAKE_CASE__       :			List[Any]													=layer_norm_eps
       SCREAMING_SNAKE_CASE__       :			str													=hidden_act
       SCREAMING_SNAKE_CASE__       :			Tuple													=initializer_range
       SCREAMING_SNAKE_CASE__       :			Tuple													=attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=is_decoder
       SCREAMING_SNAKE_CASE__       :			Tuple													=use_cache




 @classmethod
 def        __magic_name__		( cls		:							List[str]   , __lowercase		:							Union[str, os.PathLike]   , **__lowercase		:							Optional[Any] )		->  "PretrainedConfig":
       cls._set_token_in_kwargs(__lowercase )

       SCREAMING_SNAKE_CASE__						,		SCREAMING_SNAKE_CASE__       :			List[Any]													=cls.get_config_dict(__lowercase   , **__lowercase )

       # get the text config dict if we are loading from BlipConfig
       if config_dict.get('''model_type''' ) == "blip":
             SCREAMING_SNAKE_CASE__       :			int													=config_dict['''text_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        __SCREAMING_SNAKE_CASE (     lowerCamelCase			):
 snake_case_												=							"""blip_vision_model"""




 def __init__( self		:							Optional[Any]   , __lowercase		:							Any=7_68   , __lowercase		:							Optional[Any]=30_72   , __lowercase		:							Union[str, Any]=5_12   , __lowercase		:							Union[str, Any]=12   , __lowercase		:							List[str]=12   , __lowercase		:							List[Any]=3_84   , __lowercase		:							Optional[Any]=16   , __lowercase		:							int="gelu"   , __lowercase		:							Tuple=1e-5   , __lowercase		:							List[str]=0.0   , __lowercase		:							Optional[int]=1e-10   , **__lowercase		:							Tuple   , )		->  Union[str, Any]:
       super().__init__(**__lowercase )

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




 @classmethod
 def        __magic_name__		( cls		:							List[str]   , __lowercase		:							Union[str, os.PathLike]   , **__lowercase		:							List[str] )		->  "PretrainedConfig":
       cls._set_token_in_kwargs(__lowercase )

       SCREAMING_SNAKE_CASE__						,		SCREAMING_SNAKE_CASE__       :			Dict													=cls.get_config_dict(__lowercase   , **__lowercase )

       # get the vision config dict if we are loading from BlipConfig
       if config_dict.get('''model_type''' ) == "blip":
             SCREAMING_SNAKE_CASE__       :			List[str]													=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        __SCREAMING_SNAKE_CASE (     lowerCamelCase			):
 snake_case_												=							"""blip"""
 snake_case_												=							True




 def __init__( self		:							int   , __lowercase		:							Dict=None   , __lowercase		:							int=None   , __lowercase		:							int=5_12   , __lowercase		:							int=2.6592   , __lowercase		:							List[Any]=2_56   , **__lowercase		:							Any   , )		->  str:
       super().__init__(**__lowercase )

       if text_config is None:
             SCREAMING_SNAKE_CASE__       :			List[Any]													={}
             logger.info('''`text_config` is `None`. Initializing the `BlipTextConfig` with default values.''' )

       if vision_config is None:
             SCREAMING_SNAKE_CASE__       :			str													={}
             logger.info('''`vision_config` is `None`. Initializing the `BlipVisionConfig` with default values.''' )

       SCREAMING_SNAKE_CASE__       :			int													=BlipTextConfig(**__lowercase )
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=BlipVisionConfig(**__lowercase )

       SCREAMING_SNAKE_CASE__       :			List[Any]													=self.vision_config.hidden_size

       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=projection_dim
       SCREAMING_SNAKE_CASE__       :			str													=logit_scale_init_value
       SCREAMING_SNAKE_CASE__       :			Tuple													=1.0
       SCREAMING_SNAKE_CASE__       :			Any													=0.02
       SCREAMING_SNAKE_CASE__       :			List[str]													=image_text_hidden_size




 @classmethod
 def        __magic_name__		( cls		:							Optional[Any]   , __lowercase		:							BlipTextConfig   , __lowercase		:							BlipVisionConfig   , **__lowercase		:							Optional[Any] )		->  Dict:
       return cls(text_config=text_config.to_dict()   , vision_config=vision_config.to_dict()   , **__lowercase )




 def        __magic_name__		( self		:							Dict )		->  List[str]:
       SCREAMING_SNAKE_CASE__       :			int													=copy.deepcopy(self.__dict__ )
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=self.text_config.to_dict()
       SCREAMING_SNAKE_CASE__       :			Any													=self.vision_config.to_dict()
       SCREAMING_SNAKE_CASE__       :			Tuple													=self.__class__.model_type
       return output







'''simple docstring'''
import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()






@slow
@require_torch_gpu
class        __SCREAMING_SNAKE_CASE (     unittest.TestCase			):




 def        __magic_name__		( self		:							List[str] )		->  Optional[Any]:
       # clean up the VRAM after each test
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()




 def        __magic_name__		( self		:							Union[str, Any] )		->  Dict:
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=StableDiffusionKDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''' )
       SCREAMING_SNAKE_CASE__       :			str													=sd_pipe.to(__lowercase )
       sd_pipe.set_progress_bar_config(disable=__lowercase )

       sd_pipe.set_scheduler('''sample_euler''' )

       SCREAMING_SNAKE_CASE__       :			List[Any]													='''A painting of a squirrel eating a burger'''
       SCREAMING_SNAKE_CASE__       :			List[str]													=torch.manual_seed(0 )
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=sd_pipe([prompt]   , generator=__lowercase   , guidance_scale=9.0   , num_inference_steps=20   , output_type='''np''' )

       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=output.images

       SCREAMING_SNAKE_CASE__       :			int													=image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       SCREAMING_SNAKE_CASE__       :			Any													=np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339] )

       assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2




 def        __magic_name__		( self		:							List[str] )		->  List[Any]:
       SCREAMING_SNAKE_CASE__       :			List[str]													=StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' )
       SCREAMING_SNAKE_CASE__       :			int													=sd_pipe.to(__lowercase )
       sd_pipe.set_progress_bar_config(disable=__lowercase )

       sd_pipe.set_scheduler('''sample_euler''' )

       SCREAMING_SNAKE_CASE__       :			Any													='''A painting of a squirrel eating a burger'''
       SCREAMING_SNAKE_CASE__       :			Dict													=torch.manual_seed(0 )
       SCREAMING_SNAKE_CASE__       :			Tuple													=sd_pipe([prompt]   , generator=__lowercase   , guidance_scale=9.0   , num_inference_steps=20   , output_type='''np''' )

       SCREAMING_SNAKE_CASE__       :			Dict													=output.images

       SCREAMING_SNAKE_CASE__       :			Dict													=image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       SCREAMING_SNAKE_CASE__       :			List[str]													=np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112] )

       assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-1




 def        __magic_name__		( self		:							List[str] )		->  List[Any]:
       SCREAMING_SNAKE_CASE__       :			Any													=StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' )
       SCREAMING_SNAKE_CASE__       :			Tuple													=sd_pipe.to(__lowercase )
       sd_pipe.set_progress_bar_config(disable=__lowercase )

       sd_pipe.set_scheduler('''sample_dpmpp_2m''' )

       SCREAMING_SNAKE_CASE__       :			Tuple													='''A painting of a squirrel eating a burger'''
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=torch.manual_seed(0 )
       SCREAMING_SNAKE_CASE__       :			List[Any]													=sd_pipe(
           [prompt]   , generator=__lowercase   , guidance_scale=7.5   , num_inference_steps=15   , output_type='''np'''   , use_karras_sigmas=__lowercase   , )

       SCREAMING_SNAKE_CASE__       :			Optional[int]													=output.images

       SCREAMING_SNAKE_CASE__       :			Dict													=image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=np.array(
           [0.11381689, 0.12112921, 0.1389457, 0.12549606, 0.1244964, 0.10831517, 0.11562866, 0.10867816, 0.10499048] )

       assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2