
"""simple docstring"""



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  (  UpperCAmelCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							List[Any]							=					"align_text_model"


				def __init__(					self	:			Optional[int]				,     UpperCAmelCase_	:			Optional[int]=30_522				,     UpperCAmelCase_	:			Dict=768				,     UpperCAmelCase_	:			List[str]=12				,     UpperCAmelCase_	:			List[str]=12				,     UpperCAmelCase_	:			Dict=3_072				,     UpperCAmelCase_	:			Tuple="gelu"				,     UpperCAmelCase_	:			List[Any]=0.1				,     UpperCAmelCase_	:			Optional[int]=0.1				,     UpperCAmelCase_	:			Dict=512				,     UpperCAmelCase_	:			Any=2				,     UpperCAmelCase_	:			Any=0.02				,     UpperCAmelCase_	:			List[Any]=1E-1_2				,     UpperCAmelCase_	:			Union[str, Any]=0				,     UpperCAmelCase_	:			Union[str, Any]="absolute"				,     UpperCAmelCase_	:			Tuple=True				,     **UpperCAmelCase_	:			Optional[Any]				,     )		->  Optional[int]:







											"""simple docstring"""




											super().__init__(**UpperCAmelCase_  )

											_lowerCAmelCase  	=			vocab_size
											_lowerCAmelCase  	=			hidden_size
											_lowerCAmelCase  	=			num_hidden_layers
											_lowerCAmelCase  	=			num_attention_heads
											_lowerCAmelCase  	=			hidden_act
											_lowerCAmelCase  	=			intermediate_size
											_lowerCAmelCase  	=			hidden_dropout_prob
											_lowerCAmelCase  	=			attention_probs_dropout_prob
											_lowerCAmelCase  	=			max_position_embeddings
											_lowerCAmelCase  	=			type_vocab_size
											_lowerCAmelCase  	=			initializer_range
											_lowerCAmelCase  	=			layer_norm_eps
											_lowerCAmelCase  	=			position_embedding_type
											_lowerCAmelCase  	=			use_cache
											_lowerCAmelCase  	=			pad_token_id







				@classmethod
				def 				__lowerCamelCase	(					cls	:			str				,     UpperCAmelCase_	:			Optional[Any]				,     **UpperCAmelCase_	:			Dict  )		->  Any:







											"""simple docstring"""




											cls._set_token_in_kwargs(UpperCAmelCase_  )

											_lowerCAmelCase	,		_lowerCAmelCase  	=			cls.get_config_dict(UpperCAmelCase_				,     **UpperCAmelCase_  )

											# get the text config dict if we are loading from AlignConfig
											if config_dict.get('model_type'  ) == "align":
																		_lowerCAmelCase  	=			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  (  UpperCAmelCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							Dict							=					"align_vision_model"


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







											"""simple docstring"""




											super().__init__(**UpperCAmelCase_  )

											_lowerCAmelCase  	=			num_channels
											_lowerCAmelCase  	=			image_size
											_lowerCAmelCase  	=			width_coefficient
											_lowerCAmelCase  	=			depth_coefficient
											_lowerCAmelCase  	=			depth_divisor
											_lowerCAmelCase  	=			kernel_sizes
											_lowerCAmelCase  	=			in_channels
											_lowerCAmelCase  	=			out_channels
											_lowerCAmelCase  	=			depthwise_padding
											_lowerCAmelCase  	=			strides
											_lowerCAmelCase  	=			num_block_repeats
											_lowerCAmelCase  	=			expand_ratios
											_lowerCAmelCase  	=			squeeze_expansion_ratio
											_lowerCAmelCase  	=			hidden_act
											_lowerCAmelCase  	=			hidden_dim
											_lowerCAmelCase  	=			pooling_type
											_lowerCAmelCase  	=			initializer_range
											_lowerCAmelCase  	=			batch_norm_eps
											_lowerCAmelCase  	=			batch_norm_momentum
											_lowerCAmelCase  	=			drop_connect_rate
											_lowerCAmelCase  	=			sum(UpperCAmelCase_  ) * 4







				@classmethod
				def 				__lowerCamelCase	(					cls	:			List[Any]				,     UpperCAmelCase_	:			Dict				,     **UpperCAmelCase_	:			str  )		->  Union[str, Any]:







											"""simple docstring"""




											cls._set_token_in_kwargs(UpperCAmelCase_  )

											_lowerCAmelCase	,		_lowerCAmelCase  	=			cls.get_config_dict(UpperCAmelCase_				,     **UpperCAmelCase_  )

											# get the vision config dict if we are loading from AlignConfig
											if config_dict.get('model_type'  ) == "align":
																		_lowerCAmelCase  	=			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  (  UpperCAmelCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							List[str]							=					"align"
				SCREAMING_SNAKE_CASE_:							str							=					True


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







											"""simple docstring"""




											super().__init__(**UpperCAmelCase_  )

											if text_config is None:
																		_lowerCAmelCase  	=			{}
																		logger.info('text_config is None. Initializing the AlignTextConfig with default values.'  )

											if vision_config is None:
																		_lowerCAmelCase  	=			{}
																		logger.info('vision_config is None. Initializing the AlignVisionConfig with default values.'  )

											_lowerCAmelCase  	=			AlignTextConfig(**UpperCAmelCase_  )
											_lowerCAmelCase  	=			AlignVisionConfig(**UpperCAmelCase_  )

											_lowerCAmelCase  	=			projection_dim
											_lowerCAmelCase  	=			temperature_init_value
											_lowerCAmelCase  	=			initializer_range


				@classmethod
				def 				__lowerCamelCase	(					cls	:			str				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			Any				,     **UpperCAmelCase_	:			Optional[int]  )		->  str:







											"""simple docstring"""




											return cls(text_config=text_config.to_dict()				,     vision_config=vision_config.to_dict()				,     **UpperCAmelCase_  )







				def 				__lowerCamelCase	(					self	:			List[str]  )		->  Union[str, Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			copy.deepcopy(self.__dict__  )
											_lowerCAmelCase  	=			self.text_config.to_dict()
											_lowerCAmelCase  	=			self.vision_config.to_dict()
											_lowerCAmelCase  	=			self.__class__.model_type
											return output








"""simple docstring"""



import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


_snake_case          =						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''')
_snake_case          =						parser.parse_args()


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

_snake_case          =						'''path-to-your-trained-model'''
_snake_case          =						StableDiffusionPipeline.from_pretrained(model_id)
if args.dpm:
	_snake_case          =						DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
_snake_case          =						pipe.to(device)

# to channels last
_snake_case          =						pipe.unet.to(memory_format=torch.channels_last)
_snake_case          =						pipe.vae.to(memory_format=torch.channels_last)
_snake_case          =						pipe.text_encoder.to(memory_format=torch.channels_last)
if pipe.requires_safety_checker:
	_snake_case          =						pipe.safety_checker.to(memory_format=torch.channels_last)

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

# compute
_snake_case          =						6_6_6
_snake_case          =						torch.Generator(device).manual_seed(seed)
_snake_case          =						{'''generator''': generator}
if args.steps is not None:
	_snake_case          =						args.steps

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

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






