
"""simple docstring"""



from dataclasses import dataclass
from typing import Optional

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .modeling_utils import ModelMixin
@dataclass
class        _SCREAMING_SNAKE_CASE  (  UpperCAmelCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							torch.FloatTensor






class        _SCREAMING_SNAKE_CASE  (  UpperCAmelCase      ,				UpperCAmelCase							):






				'''simple docstring'''



				@register_to_config
				def __init__(					self	:			List[Any]				,     UpperCAmelCase_	:			int = 16				,     UpperCAmelCase_	:			int = 88				,     UpperCAmelCase_	:			Optional[int] = None				,     UpperCAmelCase_	:			Optional[int] = None				,     UpperCAmelCase_	:			int = 1				,     UpperCAmelCase_	:			float = 0.0				,     UpperCAmelCase_	:			int = 32				,     UpperCAmelCase_	:			Optional[int] = None				,     UpperCAmelCase_	:			bool = False				,     UpperCAmelCase_	:			Optional[int] = None				,     UpperCAmelCase_	:			str = "geglu"				,     UpperCAmelCase_	:			bool = True				,     UpperCAmelCase_	:			bool = True				,     )		->  Union[str, Any]:







											"""simple docstring"""




											super().__init__()
											_lowerCAmelCase  	=			num_attention_heads
											_lowerCAmelCase  	=			attention_head_dim
											_lowerCAmelCase  	=			num_attention_heads * attention_head_dim

											_lowerCAmelCase  	=			in_channels

											_lowerCAmelCase  	=			torch.nn.GroupNorm(num_groups=UpperCAmelCase_				,     num_channels=UpperCAmelCase_				,     eps=1E-6				,     affine=UpperCAmelCase_  )
											_lowerCAmelCase  	=			nn.Linear(UpperCAmelCase_				,     UpperCAmelCase_  )

											# 3. Define transformers blocks
											_lowerCAmelCase  	=			nn.ModuleList(
											    [
											        BasicTransformerBlock(
											            UpperCAmelCase_				,     UpperCAmelCase_				,     UpperCAmelCase_				,     dropout=UpperCAmelCase_				,     cross_attention_dim=UpperCAmelCase_				,     activation_fn=UpperCAmelCase_				,     attention_bias=UpperCAmelCase_				,     double_self_attention=UpperCAmelCase_				,     norm_elementwise_affine=UpperCAmelCase_				,     )
											        for d in range(UpperCAmelCase_  )
											    ]  )

											_lowerCAmelCase  	=			nn.Linear(UpperCAmelCase_				,     UpperCAmelCase_  )







				def 				__lowerCamelCase	(					self	:			Optional[int]				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			str=None				,     UpperCAmelCase_	:			int=None				,     UpperCAmelCase_	:			Any=None				,     UpperCAmelCase_	:			Union[str, Any]=1				,     UpperCAmelCase_	:			Any=None				,     UpperCAmelCase_	:			bool = True				,     )		->  Optional[int]:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase  	=			hidden_states.shape
											_lowerCAmelCase  	=			batch_frames // num_frames

											_lowerCAmelCase  	=			hidden_states

											_lowerCAmelCase  	=			hidden_states[None, :].reshape(UpperCAmelCase_				,     UpperCAmelCase_				,     UpperCAmelCase_				,     UpperCAmelCase_				,     UpperCAmelCase_  )
											_lowerCAmelCase  	=			hidden_states.permute(0				,     2				,     1				,     3				,     4  )

											_lowerCAmelCase  	=			self.norm(UpperCAmelCase_  )
											_lowerCAmelCase  	=			hidden_states.permute(0				,     3				,     4				,     2				,     1  ).reshape(batch_size * height * width				,     UpperCAmelCase_				,     UpperCAmelCase_  )

											_lowerCAmelCase  	=			self.proj_in(UpperCAmelCase_  )

											# 2. Blocks
											for block in self.transformer_blocks:
																		_lowerCAmelCase  	=			block(
																		    UpperCAmelCase_				,     encoder_hidden_states=UpperCAmelCase_				,     timestep=UpperCAmelCase_				,     cross_attention_kwargs=UpperCAmelCase_				,     class_labels=UpperCAmelCase_				,     )

											# 3. Output
											_lowerCAmelCase  	=			self.proj_out(UpperCAmelCase_  )
											_lowerCAmelCase  	=			(
											    hidden_states[None, None, :]
											    .reshape(UpperCAmelCase_				,     UpperCAmelCase_				,     UpperCAmelCase_				,     UpperCAmelCase_				,     UpperCAmelCase_  )
											    .permute(0				,     3				,     4				,     1				,     2  )
											    .contiguous()
											)
											_lowerCAmelCase  	=			hidden_states.reshape(UpperCAmelCase_				,     UpperCAmelCase_				,     UpperCAmelCase_				,     UpperCAmelCase_  )

											_lowerCAmelCase  	=			hidden_states + residual

											if not return_dict:
																		return (output,)

											return TransformerTemporalModelOutput(sample=UpperCAmelCase_  )








"""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''')






