



"""simple docstring"""





import timeit

import numpy as np

import datasets
from datasets.arrow_writer import ArrowWriter
from datasets.features.features import _ArrayXD
def 	__lowerCAmelCase						(		__UpperCamelCase      :			Any    ):


						'''simple docstring'''






						def wrapper(*__UpperCamelCase      :			Optional[Any]					, **__UpperCamelCase      :			Dict    ):
												snake_case_   : int						=			timeit.default_timer()
												snake_case_   : List[Any]						=			func(*lowerCamelCase_					, **lowerCamelCase_    )
												snake_case_   : List[Any]						=			timeit.default_timer() - starttime
												return delta

						snake_case_   : Dict						=			func.__name__

						return wrapper
def 	__lowerCAmelCase						(		__UpperCamelCase      :			str					, __UpperCamelCase      :			Tuple=1_0_0					, __UpperCamelCase      :			Optional[Any]=None    ):


						'''simple docstring'''






						snake_case_   : Any						=			[]
						snake_case_   : Optional[Any]						=			seq_shapes or {}
						for i in range(lowerCamelCase_    ):
												snake_case_   : int						=			{}
												for col_id, (k, v) in enumerate(features.items()    ):
																		if isinstance(lowerCamelCase_					, _ArrayXD    ):
																								snake_case_   : int						=			np.random.rand(*v.shape    ).astype(v.dtype    )
																		elif isinstance(lowerCamelCase_					, datasets.Value    ):
																								if v.dtype == "string":
																														snake_case_   : Dict						=			"""The small grey turtle was surprisingly fast when challenged."""
																								else:
																														snake_case_   : str						=			np.random.randint(1_0					, size=1    ).astype(v.dtype    ).item()
																		elif isinstance(lowerCamelCase_					, datasets.Sequence    ):
																								while isinstance(lowerCamelCase_					, datasets.Sequence    ):
																														snake_case_   : Optional[Any]						=			v.feature
																								snake_case_   : Dict						=			seq_shapes[k]
																								snake_case_   : List[str]						=			np.random.rand(*lowerCamelCase_    ).astype(v.dtype    )
																		snake_case_   : Any						=			data

												dummy_data.append((i, example)    )

						return dummy_data






def 	__lowerCAmelCase						(		__UpperCamelCase      :			List[Any]					, __UpperCamelCase      :			Any					, __UpperCamelCase      :			int=1_0_0					, __UpperCamelCase      :			Any=None    ):


						'''simple docstring'''






						snake_case_   : List[str]						=			generate_examples(lowerCamelCase_					, num_examples=lowerCamelCase_					, seq_shapes=lowerCamelCase_    )

						with ArrowWriter(features=lowerCamelCase_					, path=lowerCamelCase_    ) as writer:
												for key, record in dummy_data:
																		snake_case_   : Tuple						=			features.encode_example(lowerCamelCase_    )
																		writer.write(lowerCamelCase_    )

												snake_case_   : Union[str, Any]						=			writer.finalize()

						if not num_final_examples == num_examples:
												raise ValueError(
												    F'Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}.'    )

						snake_case_   : Optional[int]						=			datasets.Dataset.from_file(filename=lowerCamelCase_					, info=datasets.DatasetInfo(features=lowerCamelCase_    )    )

						return dataset






"""simple docstring"""





import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


__lowerCAmelCase       :					Optional[int]      					=					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       :					Optional[Any]      					=					parser.parse_args()


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

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

# to channels last
__lowerCAmelCase       :					Optional[Any]      					=					pipe.unet.to(memory_format=torch.channels_last)
__lowerCAmelCase       :					List[str]      					=					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       :					Dict      					=					pipe.safety_checker.to(memory_format=torch.channels_last)

# optimize with ipex
__lowerCAmelCase       :					Tuple      					=					torch.randn(2, 4, 64, 64)
__lowerCAmelCase       :					Any      					=					torch.rand(1) * 999
__lowerCAmelCase       :					List[str]      					=					torch.randn(2, 77, 768)
__lowerCAmelCase       :					Optional[int]      					=					(sample, timestep, encoder_hidden_status)
try:
					__lowerCAmelCase       :					List[Any]      					=					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       :					Any      					=					ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True)
__lowerCAmelCase       :					int      					=					ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True)
if pipe.requires_safety_checker:
					__lowerCAmelCase       :					Union[str, Any]      					=					ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True)

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

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

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

