
'''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 						__UpperCAmelCase      (					_lowerCamelCase						):
							__lowercase      		=       42


class 						__UpperCAmelCase      (					_lowerCamelCase						,   _lowerCamelCase						):


							@register_to_config
							def __init__(       self    ,   lowerCAmelCase_ = 16    ,   lowerCAmelCase_ = 88    ,   lowerCAmelCase_ = None    ,   lowerCAmelCase_ = None    ,   lowerCAmelCase_ = 1    ,   lowerCAmelCase_ = 0.0    ,   lowerCAmelCase_ = 32    ,   lowerCAmelCase_ = None    ,   lowerCAmelCase_ = False    ,   lowerCAmelCase_ = None    ,   lowerCAmelCase_ = "geglu"    ,   lowerCAmelCase_ = True    ,   lowerCAmelCase_ = True    ,   ):





								"""simple docstring"""






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

								_snake_case   						=		in_channels

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

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

								_snake_case   						=		nn.Linear(lowerCAmelCase_    ,   lowerCAmelCase_  )




							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_=None    ,   lowerCAmelCase_=None    ,   lowerCAmelCase_=None    ,   lowerCAmelCase_=1    ,   lowerCAmelCase_=None    ,   lowerCAmelCase_ = True    ,   ):





								"""simple docstring"""






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

								_snake_case   						=		hidden_states

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

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

								_snake_case   						=		self.proj_in(lowerCAmelCase_  )

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

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

								_snake_case   						=		hidden_states + residual

								if not return_dict:
									return (output,)

								return TransformerTemporalModelOutput(sample=lowerCAmelCase_  )



'''simple docstring'''







import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


lowercase    :							Optional[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")
lowercase    :							Tuple      =					parser.parse_args()


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

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

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

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

# compute
lowercase    :							List[str]      =					666
lowercase    :							Tuple      =					torch.Generator(device).manual_seed(seed)
lowercase    :							Union[str, Any]      =					{"generator": generator}
if args.steps is not None:
					lowercase    :							Dict      =					args.steps

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

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

