



import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


__snake_case		:List[str]   	=  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		:Any   	=  parser.parse_args()


__snake_case		:Optional[int]   	=  '''cpu'''
__snake_case		:List[str]   	=  '''a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings'''

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

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

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

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

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

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










from unittest import TestCase

from datasets import Sequence, Value
from datasets.arrow_dataset import Dataset

class 					_A (			__UpperCAmelCase					):



       def 	_lowerCamelCase    (	self							:  int):


           '''simple docstring'''

           return [
               {"col_1": 3, "col_2": "a"},
               {"col_1": 2, "col_2": "b"},
               {"col_1": 1, "col_2": "c"},
               {"col_1": 0, "col_2": "d"},
           ]



       def 	_lowerCamelCase    (	self							:  Tuple):


           '''simple docstring'''

           __a		 =       {'''col_1''': [3, 2, 1, 0], '''col_2''': ['''a''', '''b''', '''c''', '''d''']}
           return Dataset.from_dict(__SCREAMING_SNAKE_CASE)



       def 	_lowerCamelCase    (	self							:  Any):


           '''simple docstring'''

           __a		 =       self._create_example_records()
           __a		 =       Dataset.from_list(__SCREAMING_SNAKE_CASE)
           self.assertListEqual(dset.column_names  ,	['''col_1''', '''col_2'''])
           for i, r in enumerate(__SCREAMING_SNAKE_CASE):
               self.assertDictEqual(__SCREAMING_SNAKE_CASE  ,	example_records[i])



       def 	_lowerCamelCase    (	self							:  Optional[Any]):


           '''simple docstring'''

           __a		 =       self._create_example_records()
           __a		 =       Dataset.from_list(__SCREAMING_SNAKE_CASE)
           __a		 =       Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]})
           self.assertEqual(dset.info  ,	dset_from_dict.info)



       def 	_lowerCamelCase    (	self							:  int):  # checks what happens with missing columns


           '''simple docstring'''

           __a		 =       [{'''col_1''': 1}, {'''col_2''': '''x'''}]
           __a		 =       Dataset.from_list(__SCREAMING_SNAKE_CASE)
           self.assertDictEqual(dset[0]  ,	{'''col_1''': 1})
           self.assertDictEqual(dset[1]  ,	{'''col_1''': None})  # NB: first record is used for columns



       def 	_lowerCamelCase    (	self							:  Optional[Any]):  # checks if the type can be inferred from the second record


           '''simple docstring'''

           __a		 =       [{'''col_1''': []}, {'''col_1''': [1, 2]}]
           __a		 =       Dataset.from_list(__SCREAMING_SNAKE_CASE)
           self.assertEqual(dset.info.features['''col_1''']  ,	Sequence(Value('''int64''')))





       def 	_lowerCamelCase    (	self							:  List[Any]):


           '''simple docstring'''

           __a		 =       Dataset.from_list([])
           self.assertEqual(len(__SCREAMING_SNAKE_CASE)  ,	0)
           self.assertListEqual(dset.column_names  ,	[])





