





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__							:      List[str]         =    logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__							:      Dict         =    {
    """salesforce/blip2-opt-2.7b""": """https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json""",
}







class 	__lowerCAmelCase      (       _UpperCamelCase	):
   _UpperCamelCase			:    Optional[int]  		=					"""blip_2_vision_model"""





   def __init__(			self       ,      snake_case=1_408       ,      snake_case=6_144       ,      snake_case=39       ,      snake_case=16       ,      snake_case=224       ,      snake_case=14       ,      snake_case="gelu"       ,      snake_case=0.00_001       ,      snake_case=0.0       ,      snake_case=1E-10       ,      snake_case=True       ,      **snake_case       ,      )					->	Any:
      """simple docstring"""





      super().__init__(**snake_case   )

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





   @classmethod
   def 						_snake_case			(			cls       ,      snake_case       ,      **snake_case   )					->	"PretrainedConfig":
      """simple docstring"""





      cls._set_token_in_kwargs(snake_case   )

      a__		,  a__						:    int   		=	cls.get_config_dict(snake_case       ,      **snake_case   )

      # get the vision config dict if we are loading from Blip2Config
      if config_dict.get("model_type"   ) == "blip-2":
         a__						:    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(snake_case       ,      **snake_case   )







class 	__lowerCAmelCase      (       _UpperCamelCase	):
   _UpperCamelCase			:    Union[str, Any]  		=					"""blip_2_qformer"""





   def __init__(			self       ,      snake_case=30_522       ,      snake_case=768       ,      snake_case=12       ,      snake_case=12       ,      snake_case=3_072       ,      snake_case="gelu"       ,      snake_case=0.1       ,      snake_case=0.1       ,      snake_case=512       ,      snake_case=0.02       ,      snake_case=1E-12       ,      snake_case=0       ,      snake_case="absolute"       ,      snake_case=2       ,      snake_case=1_408       ,      **snake_case       ,      )					->	Optional[int]:
      """simple docstring"""





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

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





   @classmethod
   def 						_snake_case			(			cls       ,      snake_case       ,      **snake_case   )					->	"PretrainedConfig":
      """simple docstring"""





      cls._set_token_in_kwargs(snake_case   )

      a__		,  a__						:    Any   		=	cls.get_config_dict(snake_case       ,      **snake_case   )

      # get the qformer config dict if we are loading from Blip2Config
      if config_dict.get("model_type"   ) == "blip-2":
         a__						:    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(snake_case       ,      **snake_case   )







class 	__lowerCAmelCase      (       _UpperCamelCase	):
   _UpperCamelCase			:    Optional[int]  		=					"""blip-2"""
   _UpperCamelCase			:    int  		=					True





   def __init__(			self       ,      snake_case=None       ,      snake_case=None       ,      snake_case=None       ,      snake_case=32       ,      **snake_case   )					->	str:
      """simple docstring"""





      super().__init__(**snake_case   )

      if vision_config is None:
         a__						:    Optional[Any]   		=	{}
         logger.info("vision_config is None. initializing the Blip2VisionConfig with default values."   )

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

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

      a__						:    List[Any]   		=	BlipaVisionConfig(**snake_case   )
      a__						:    Any   		=	BlipaQFormerConfig(**snake_case   )
      a__						:    Optional[Any]   		=	text_config["model_type"] if "model_type" in text_config else "opt"
      a__						:    List[Any]   		=	CONFIG_MAPPING[text_model_type](**snake_case   )

      a__						:    List[str]   		=	self.text_config.tie_word_embeddings
      a__						:    Dict   		=	self.text_config.is_encoder_decoder

      a__						:    List[Any]   		=	num_query_tokens
      a__						:    Union[str, Any]   		=	self.vision_config.hidden_size
      a__						:    Any   		=	self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
      a__						:    List[Any]   		=	1.0
      a__						:    int   		=	0.02





   @classmethod
   def 						_snake_case			(			cls       ,      snake_case       ,      snake_case       ,      snake_case       ,      **snake_case       ,      )					->	Optional[int]:
      """simple docstring"""





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





   def 						_snake_case			(			self   )					->	str:
      """simple docstring"""





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








import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


SCREAMING_SNAKE_CASE__							:      List[str]         =    argparse.ArgumentParser("""Stable Diffusion script with intel optimization""", add_help=False)
parser.add_argument("""--dpm""", action="""store_true""", help="""Enable DPMSolver or not""")
parser.add_argument("""--steps""", default=None, type=int, help="""Num inference steps""")
SCREAMING_SNAKE_CASE__							:      Tuple         =    parser.parse_args()


SCREAMING_SNAKE_CASE__							:      int         =    """cpu"""
SCREAMING_SNAKE_CASE__							:      Optional[Any]         =    """a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings"""

SCREAMING_SNAKE_CASE__							:      List[Any]         =    """path-to-your-trained-model"""
SCREAMING_SNAKE_CASE__							:      Tuple         =    StableDiffusionPipeline.from_pretrained(model_id)
if args.dpm:
  SCREAMING_SNAKE_CASE__							:      Union[str, Any]         =    DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
SCREAMING_SNAKE_CASE__							:      Optional[int]         =    pipe.to(device)

# to channels last
SCREAMING_SNAKE_CASE__							:      int         =    pipe.unet.to(memory_format=torch.channels_last)
SCREAMING_SNAKE_CASE__							:      List[Any]         =    pipe.vae.to(memory_format=torch.channels_last)
SCREAMING_SNAKE_CASE__							:      Union[str, Any]         =    pipe.text_encoder.to(memory_format=torch.channels_last)
if pipe.requires_safety_checker:
  SCREAMING_SNAKE_CASE__							:      List[str]         =    pipe.safety_checker.to(memory_format=torch.channels_last)

# optimize with ipex
SCREAMING_SNAKE_CASE__							:      Optional[int]         =    torch.randn(2, 4, 6_4, 6_4)
SCREAMING_SNAKE_CASE__							:      Any         =    torch.rand(1) * 9_9_9
SCREAMING_SNAKE_CASE__							:      List[Any]         =    torch.randn(2, 7_7, 7_6_8)
SCREAMING_SNAKE_CASE__							:      Optional[Any]         =    (sample, timestep, encoder_hidden_status)
try:
  SCREAMING_SNAKE_CASE__							:      Any         =    ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example)
except Exception:
  SCREAMING_SNAKE_CASE__							:      Optional[Any]         =    ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True)
SCREAMING_SNAKE_CASE__							:      str         =    ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True)
SCREAMING_SNAKE_CASE__							:      Union[str, Any]         =    ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True)
if pipe.requires_safety_checker:
  SCREAMING_SNAKE_CASE__							:      Union[str, Any]         =    ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True)

# compute
SCREAMING_SNAKE_CASE__							:      Optional[Any]         =    6_6_6
SCREAMING_SNAKE_CASE__							:      Any         =    torch.Generator(device).manual_seed(seed)
SCREAMING_SNAKE_CASE__							:      int         =    {"""generator""": generator}
if args.steps is not None:
  SCREAMING_SNAKE_CASE__							:      Optional[int]         =    args.steps

with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa):
  SCREAMING_SNAKE_CASE__							:      Dict         =    pipe(prompt, **generate_kwargs).images[0]

# save image
image.save("""generated.png""")

