






import copy
import os
from typing import TYPE_CHECKING, List, Union


if TYPE_CHECKING:
   pass

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


snake_case     					=		logging.get_logger(__name__)

snake_case     					=		{
    """kakaobrain/align-base""": """https://huggingface.co/kakaobrain/align-base/resolve/main/config.json""",
}


class 			SCREAMING_SNAKE_CASE      (      lowerCAmelCase       ):
     '''simple docstring'''


     UpperCamelCase_				:		int								=						'''align_text_model'''


     def __init__(					self							:						Dict       ,			UpperCAmelCase_							:						List[str]=3_0522       ,			UpperCAmelCase_							:						Dict=768       ,			UpperCAmelCase_							:						str=12       ,			UpperCAmelCase_							:						Optional[Any]=12       ,			UpperCAmelCase_							:						List[str]=3072       ,			UpperCAmelCase_							:						Dict="gelu"       ,			UpperCAmelCase_							:						int=0.1       ,			UpperCAmelCase_							:						Any=0.1       ,			UpperCAmelCase_							:						Dict=512       ,			UpperCAmelCase_							:						str=2       ,			UpperCAmelCase_							:						Dict=0.02       ,			UpperCAmelCase_							:						Optional[int]=1E-12       ,			UpperCAmelCase_							:						List[str]=0       ,			UpperCAmelCase_							:						List[Any]="absolute"       ,			UpperCAmelCase_							:						List[Any]=True       ,			**UpperCAmelCase_							:						Optional[int]       ,			):
       super().__init__(**UpperCAmelCase_   )

       SCREAMING_SNAKE_CASE							:   str						    =     vocab_size
       SCREAMING_SNAKE_CASE							:   int						    =     hidden_size
       SCREAMING_SNAKE_CASE							:   Dict						    =     num_hidden_layers
       SCREAMING_SNAKE_CASE							:   List[str]						    =     num_attention_heads
       SCREAMING_SNAKE_CASE							:   str						    =     hidden_act
       SCREAMING_SNAKE_CASE							:   Dict						    =     intermediate_size
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     hidden_dropout_prob
       SCREAMING_SNAKE_CASE							:   str						    =     attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE							:   str						    =     max_position_embeddings
       SCREAMING_SNAKE_CASE							:   Dict						    =     type_vocab_size
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     initializer_range
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     layer_norm_eps
       SCREAMING_SNAKE_CASE							:   str						    =     position_embedding_type
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     use_cache
       SCREAMING_SNAKE_CASE							:   List[str]						    =     pad_token_id






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

       SCREAMING_SNAKE_CASE    ,	SCREAMING_SNAKE_CASE							:   List[str]						    =     cls.get_config_dict(UpperCAmelCase_       ,			**UpperCAmelCase_   )

       # get the text config dict if we are loading from AlignConfig
       if config_dict.get("model_type"   ) == "align":
         SCREAMING_SNAKE_CASE							:   Optional[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(UpperCAmelCase_       ,			**UpperCAmelCase_   )





class 			SCREAMING_SNAKE_CASE      (      lowerCAmelCase       ):
     '''simple docstring'''


     UpperCamelCase_				:		int								=						'''align_vision_model'''


     def __init__(					self							:						Union[str, Any]       ,			UpperCAmelCase_							:						int = 3       ,			UpperCAmelCase_							:						int = 600       ,			UpperCAmelCase_							:						float = 2.0       ,			UpperCAmelCase_							:						float = 3.1       ,			UpperCAmelCase_							:						int = 8       ,			UpperCAmelCase_							:						List[int] = [3, 3, 5, 3, 5, 5, 3]       ,			UpperCAmelCase_							:						List[int] = [32, 16, 24, 40, 80, 112, 192]       ,			UpperCAmelCase_							:						List[int] = [16, 24, 40, 80, 112, 192, 320]       ,			UpperCAmelCase_							:						List[int] = []       ,			UpperCAmelCase_							:						List[int] = [1, 2, 2, 2, 1, 2, 1]       ,			UpperCAmelCase_							:						List[int] = [1, 2, 2, 3, 3, 4, 1]       ,			UpperCAmelCase_							:						List[int] = [1, 6, 6, 6, 6, 6, 6]       ,			UpperCAmelCase_							:						float = 0.25       ,			UpperCAmelCase_							:						str = "swish"       ,			UpperCAmelCase_							:						int = 2560       ,			UpperCAmelCase_							:						str = "mean"       ,			UpperCAmelCase_							:						float = 0.02       ,			UpperCAmelCase_							:						float = 0.001       ,			UpperCAmelCase_							:						float = 0.99       ,			UpperCAmelCase_							:						float = 0.2       ,			**UpperCAmelCase_							:						List[str]       ,			):
       super().__init__(**UpperCAmelCase_   )

       SCREAMING_SNAKE_CASE							:   Any						    =     num_channels
       SCREAMING_SNAKE_CASE							:   Dict						    =     image_size
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     width_coefficient
       SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     depth_coefficient
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     depth_divisor
       SCREAMING_SNAKE_CASE							:   Tuple						    =     kernel_sizes
       SCREAMING_SNAKE_CASE							:   int						    =     in_channels
       SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     out_channels
       SCREAMING_SNAKE_CASE							:   str						    =     depthwise_padding
       SCREAMING_SNAKE_CASE							:   Dict						    =     strides
       SCREAMING_SNAKE_CASE							:   List[str]						    =     num_block_repeats
       SCREAMING_SNAKE_CASE							:   List[str]						    =     expand_ratios
       SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     squeeze_expansion_ratio
       SCREAMING_SNAKE_CASE							:   List[str]						    =     hidden_act
       SCREAMING_SNAKE_CASE							:   Any						    =     hidden_dim
       SCREAMING_SNAKE_CASE							:   Dict						    =     pooling_type
       SCREAMING_SNAKE_CASE							:   int						    =     initializer_range
       SCREAMING_SNAKE_CASE							:   Tuple						    =     batch_norm_eps
       SCREAMING_SNAKE_CASE							:   int						    =     batch_norm_momentum
       SCREAMING_SNAKE_CASE							:   Any						    =     drop_connect_rate
       SCREAMING_SNAKE_CASE							:   Optional[Any]						    =     sum(UpperCAmelCase_   ) * 4






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

       SCREAMING_SNAKE_CASE    ,	SCREAMING_SNAKE_CASE							:   str						    =     cls.get_config_dict(UpperCAmelCase_       ,			**UpperCAmelCase_   )

       # get the vision config dict if we are loading from AlignConfig
       if config_dict.get("model_type"   ) == "align":
         SCREAMING_SNAKE_CASE							:   List[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(UpperCAmelCase_       ,			**UpperCAmelCase_   )





class 			SCREAMING_SNAKE_CASE      (      lowerCAmelCase       ):
     '''simple docstring'''


     UpperCamelCase_				:		Dict								=						'''align'''
     UpperCamelCase_				:		Optional[int]								=						True


     def __init__(					self							:						str       ,			UpperCAmelCase_							:						Optional[Any]=None       ,			UpperCAmelCase_							:						str=None       ,			UpperCAmelCase_							:						Optional[int]=640       ,			UpperCAmelCase_							:						List[Any]=1.0       ,			UpperCAmelCase_							:						str=0.02       ,			**UpperCAmelCase_							:						Optional[Any]       ,			):
       super().__init__(**UpperCAmelCase_   )

       if text_config is None:
         SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     {}
         logger.info("text_config is None. Initializing the AlignTextConfig with default values."   )

       if vision_config is None:
         SCREAMING_SNAKE_CASE							:   Tuple						    =     {}
         logger.info("vision_config is None. Initializing the AlignVisionConfig with default values."   )

       SCREAMING_SNAKE_CASE							:   List[str]						    =     AlignTextConfig(**UpperCAmelCase_   )
       SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     AlignVisionConfig(**UpperCAmelCase_   )

       SCREAMING_SNAKE_CASE							:   List[str]						    =     projection_dim
       SCREAMING_SNAKE_CASE							:   Tuple						    =     temperature_init_value
       SCREAMING_SNAKE_CASE							:   str						    =     initializer_range


     @classmethod
     def 					_A							(					cls							:						Any       ,			UpperCAmelCase_							:						AlignTextConfig       ,			UpperCAmelCase_							:						AlignVisionConfig       ,			**UpperCAmelCase_							:						List[str]   ):
       return cls(text_config=text_config.to_dict()       ,			vision_config=vision_config.to_dict()       ,			**UpperCAmelCase_   )






     def 					_A							(					self							:						List[str]   ):
       SCREAMING_SNAKE_CASE							:   Dict						    =     copy.deepcopy(self.__dict__   )
       SCREAMING_SNAKE_CASE							:   List[str]						    =     self.text_config.to_dict()
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     self.vision_config.to_dict()
       SCREAMING_SNAKE_CASE							:   str						    =     self.__class__.model_type
       return output













import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


_lowerCAmelCase				:   Any						       =						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''')
_lowerCAmelCase				:   Tuple						       =						parser.parse_args()


_lowerCAmelCase				:   Any						       =						'''cpu'''
_lowerCAmelCase				:   Dict						       =						'''a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings'''

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

# to channels last
_lowerCAmelCase				:   Any						       =						pipe.unet.to(memory_format=torch.channels_last)
_lowerCAmelCase				:   int						       =						pipe.vae.to(memory_format=torch.channels_last)
_lowerCAmelCase				:   Optional[Any]						       =						pipe.text_encoder.to(memory_format=torch.channels_last)
if pipe.requires_safety_checker:
 _lowerCAmelCase				:   Optional[int]						       =						pipe.safety_checker.to(memory_format=torch.channels_last)

# optimize with ipex
_lowerCAmelCase				:   Tuple						       =						torch.randn(2, 4, 64, 64)
_lowerCAmelCase				:   List[str]						       =						torch.rand(1) * 999
_lowerCAmelCase				:   List[Any]						       =						torch.randn(2, 77, 768)
_lowerCAmelCase				:   int						       =						(sample, timestep, encoder_hidden_status)
try:
 _lowerCAmelCase				:   Tuple						       =						ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example)
except Exception:
 _lowerCAmelCase				:   Optional[Any]						       =						ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True)
_lowerCAmelCase				:   Dict						       =						ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True)
_lowerCAmelCase				:   Dict						       =						ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True)
if pipe.requires_safety_checker:
 _lowerCAmelCase				:   Tuple						       =						ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True)

# compute
_lowerCAmelCase				:   Dict						       =						666
_lowerCAmelCase				:   Tuple						       =						torch.Generator(device).manual_seed(seed)
_lowerCAmelCase				:   Tuple						       =						{'''generator''': generator}
if args.steps is not None:
 _lowerCAmelCase				:   Tuple						       =						args.steps

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

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