




'''simple docstring'''




import flax.linen as nn
import jax.numpy as jnp

from .attention_flax import FlaxTransformeraDModel
from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD





class 					__magic_name__		(					nn.Module):
			UpperCamelCase__       = 42
			UpperCamelCase__       = 42
			UpperCamelCase__       = 0.0
			UpperCamelCase__       = 1
			UpperCamelCase__       = 1
			UpperCamelCase__       = True
			UpperCamelCase__       = False
			UpperCamelCase__       = False
			UpperCamelCase__       = False
			UpperCamelCase__       = jnp.floataa







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
										lowercase_      :    Union[str, Any]       		= []
										lowercase_      :    Union[str, Any]       		= []

										for i in range(self.num_layers					):
																	lowercase_      :    Dict       		= self.in_channels if i == 0 else self.out_channels

																	lowercase_      :    Tuple       		= FlaxResnetBlockaD(
																	    in_channels=lowercase_       ,    out_channels=self.out_channels       ,    dropout_prob=self.dropout       ,    dtype=self.dtype       ,    )
																	resnets.append(lowercase_					)

																	lowercase_      :    Any       		= FlaxTransformeraDModel(
																	    in_channels=self.out_channels       ,    n_heads=self.num_attention_heads       ,    d_head=self.out_channels // self.num_attention_heads       ,    depth=1       ,    use_linear_projection=self.use_linear_projection       ,    only_cross_attention=self.only_cross_attention       ,    use_memory_efficient_attention=self.use_memory_efficient_attention       ,    dtype=self.dtype       ,    )
																	attentions.append(lowercase_					)

										lowercase_      :    Union[str, Any]       		= resnets
										lowercase_      :    Any       		= attentions

										if self.add_downsample:
																	lowercase_      :    int       		= FlaxDownsampleaD(self.out_channels       ,    dtype=self.dtype					)







			def __call__(			self       :						Union[str, Any]       ,    lowercase_       :						List[Any]       ,    lowercase_       :						Optional[int]       ,    lowercase_       :						Dict       ,    lowercase_       :						Optional[Any]=True					):
										lowercase_      :    int       		= ()

										for resnet, attn in zip(self.resnets       ,    self.attentions					):
																	lowercase_      :    Optional[Any]       		= resnet(lowercase_       ,    lowercase_       ,    deterministic=lowercase_					)
																	lowercase_      :    Tuple       		= attn(lowercase_       ,    lowercase_       ,    deterministic=lowercase_					)
																	output_states += (hidden_states,)

										if self.add_downsample:
																	lowercase_      :    Dict       		= self.downsamplers_a(lowercase_					)
																	output_states += (hidden_states,)

										return hidden_states, output_states





class 					__magic_name__		(					nn.Module):
			UpperCamelCase__       = 42
			UpperCamelCase__       = 42
			UpperCamelCase__       = 0.0
			UpperCamelCase__       = 1
			UpperCamelCase__       = True
			UpperCamelCase__       = jnp.floataa







			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[Any]					):
										lowercase_      :    List[Any]       		= []

										for i in range(self.num_layers					):
																	lowercase_      :    Union[str, Any]       		= self.in_channels if i == 0 else self.out_channels

																	lowercase_      :    str       		= FlaxResnetBlockaD(
																	    in_channels=lowercase_       ,    out_channels=self.out_channels       ,    dropout_prob=self.dropout       ,    dtype=self.dtype       ,    )
																	resnets.append(lowercase_					)
										lowercase_      :    str       		= resnets

										if self.add_downsample:
																	lowercase_      :    Optional[int]       		= FlaxDownsampleaD(self.out_channels       ,    dtype=self.dtype					)







			def __call__(			self       :						Dict       ,    lowercase_       :						Any       ,    lowercase_       :						Optional[int]       ,    lowercase_       :						Optional[Any]=True					):
										lowercase_      :    int       		= ()

										for resnet in self.resnets:
																	lowercase_      :    str       		= resnet(lowercase_       ,    lowercase_       ,    deterministic=lowercase_					)
																	output_states += (hidden_states,)

										if self.add_downsample:
																	lowercase_      :    List[str]       		= self.downsamplers_a(lowercase_					)
																	output_states += (hidden_states,)

										return hidden_states, output_states





class 					__magic_name__		(					nn.Module):
			UpperCamelCase__       = 42
			UpperCamelCase__       = 42
			UpperCamelCase__       = 42
			UpperCamelCase__       = 0.0
			UpperCamelCase__       = 1
			UpperCamelCase__       = 1
			UpperCamelCase__       = True
			UpperCamelCase__       = False
			UpperCamelCase__       = False
			UpperCamelCase__       = False
			UpperCamelCase__       = jnp.floataa







			def 			SCREAMING_SNAKE_CASE_  (			self       :						int					):
										lowercase_      :    Tuple       		= []
										lowercase_      :    str       		= []

										for i in range(self.num_layers					):
																	lowercase_      :    List[str]       		= self.in_channels if (i == self.num_layers - 1) else self.out_channels
																	lowercase_      :    Union[str, Any]       		= self.prev_output_channel if i == 0 else self.out_channels

																	lowercase_      :    int       		= FlaxResnetBlockaD(
																	    in_channels=resnet_in_channels + res_skip_channels       ,    out_channels=self.out_channels       ,    dropout_prob=self.dropout       ,    dtype=self.dtype       ,    )
																	resnets.append(lowercase_					)

																	lowercase_      :    str       		= FlaxTransformeraDModel(
																	    in_channels=self.out_channels       ,    n_heads=self.num_attention_heads       ,    d_head=self.out_channels // self.num_attention_heads       ,    depth=1       ,    use_linear_projection=self.use_linear_projection       ,    only_cross_attention=self.only_cross_attention       ,    use_memory_efficient_attention=self.use_memory_efficient_attention       ,    dtype=self.dtype       ,    )
																	attentions.append(lowercase_					)

										lowercase_      :    List[Any]       		= resnets
										lowercase_      :    Dict       		= attentions

										if self.add_upsample:
																	lowercase_      :    str       		= FlaxUpsampleaD(self.out_channels       ,    dtype=self.dtype					)







			def __call__(			self       :						Union[str, Any]       ,    lowercase_       :						Tuple       ,    lowercase_       :						Union[str, Any]       ,    lowercase_       :						Optional[Any]       ,    lowercase_       :						Tuple       ,    lowercase_       :						Any=True					):
										for resnet, attn in zip(self.resnets       ,    self.attentions					):
																	# pop res hidden states
																	lowercase_      :    Dict       		= res_hidden_states_tuple[-1]
																	lowercase_      :    List[Any]       		= res_hidden_states_tuple[:-1]
																	lowercase_      :    int       		= jnp.concatenate((hidden_states, res_hidden_states)       ,    axis=-1					)

																	lowercase_      :    Tuple       		= resnet(lowercase_       ,    lowercase_       ,    deterministic=lowercase_					)
																	lowercase_      :    Union[str, Any]       		= attn(lowercase_       ,    lowercase_       ,    deterministic=lowercase_					)

										if self.add_upsample:
																	lowercase_      :    Union[str, Any]       		= self.upsamplers_a(lowercase_					)

										return hidden_states





class 					__magic_name__		(					nn.Module):
			UpperCamelCase__       = 42
			UpperCamelCase__       = 42
			UpperCamelCase__       = 42
			UpperCamelCase__       = 0.0
			UpperCamelCase__       = 1
			UpperCamelCase__       = True
			UpperCamelCase__       = jnp.floataa







			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[Any]					):
										lowercase_      :    Optional[int]       		= []

										for i in range(self.num_layers					):
																	lowercase_      :    List[str]       		= self.in_channels if (i == self.num_layers - 1) else self.out_channels
																	lowercase_      :    List[Any]       		= self.prev_output_channel if i == 0 else self.out_channels

																	lowercase_      :    Union[str, Any]       		= FlaxResnetBlockaD(
																	    in_channels=resnet_in_channels + res_skip_channels       ,    out_channels=self.out_channels       ,    dropout_prob=self.dropout       ,    dtype=self.dtype       ,    )
																	resnets.append(lowercase_					)

										lowercase_      :    Dict       		= resnets

										if self.add_upsample:
																	lowercase_      :    List[Any]       		= FlaxUpsampleaD(self.out_channels       ,    dtype=self.dtype					)







			def __call__(			self       :						List[str]       ,    lowercase_       :						Optional[Any]       ,    lowercase_       :						List[str]       ,    lowercase_       :						str       ,    lowercase_       :						Dict=True					):
										for resnet in self.resnets:
																	# pop res hidden states
																	lowercase_      :    str       		= res_hidden_states_tuple[-1]
																	lowercase_      :    Any       		= res_hidden_states_tuple[:-1]
																	lowercase_      :    Union[str, Any]       		= jnp.concatenate((hidden_states, res_hidden_states)       ,    axis=-1					)

																	lowercase_      :    Union[str, Any]       		= resnet(lowercase_       ,    lowercase_       ,    deterministic=lowercase_					)

										if self.add_upsample:
																	lowercase_      :    Optional[Any]       		= self.upsamplers_a(lowercase_					)

										return hidden_states





class 					__magic_name__		(					nn.Module):
			UpperCamelCase__       = 42
			UpperCamelCase__       = 0.0
			UpperCamelCase__       = 1
			UpperCamelCase__       = 1
			UpperCamelCase__       = False
			UpperCamelCase__       = False
			UpperCamelCase__       = jnp.floataa







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Tuple					):
										# there is always at least one resnet
										lowercase_      :    int       		= [
										    FlaxResnetBlockaD(
										        in_channels=self.in_channels       ,    out_channels=self.in_channels       ,    dropout_prob=self.dropout       ,    dtype=self.dtype       ,    )
										]

										lowercase_      :    str       		= []

										for _ in range(self.num_layers					):
																	lowercase_      :    Optional[int]       		= FlaxTransformeraDModel(
																	    in_channels=self.in_channels       ,    n_heads=self.num_attention_heads       ,    d_head=self.in_channels // self.num_attention_heads       ,    depth=1       ,    use_linear_projection=self.use_linear_projection       ,    use_memory_efficient_attention=self.use_memory_efficient_attention       ,    dtype=self.dtype       ,    )
																	attentions.append(lowercase_					)

																	lowercase_      :    Optional[Any]       		= FlaxResnetBlockaD(
																	    in_channels=self.in_channels       ,    out_channels=self.in_channels       ,    dropout_prob=self.dropout       ,    dtype=self.dtype       ,    )
																	resnets.append(lowercase_					)

										lowercase_      :    str       		= resnets
										lowercase_      :    int       		= attentions







			def __call__(			self       :						List[Any]       ,    lowercase_       :						Optional[int]       ,    lowercase_       :						Tuple       ,    lowercase_       :						Dict       ,    lowercase_       :						List[Any]=True					):
										lowercase_      :    Dict       		= self.resnets[0](lowercase_       ,    lowercase_					)
										for attn, resnet in zip(self.attentions       ,    self.resnets[1:]					):
																	lowercase_      :    Optional[Any]       		= attn(lowercase_       ,    lowercase_       ,    deterministic=lowercase_					)
																	lowercase_      :    Any       		= resnet(lowercase_       ,    lowercase_       ,    deterministic=lowercase_					)

										return hidden_states






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


_lowercase		:				Dict	       =		"cpu"
_lowercase		:				str	       =		"a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings"

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

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

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

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

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

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





