


import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


lowercase_	   =     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_	   =     parser.parse_args()


lowercase_	   =     'cpu'
lowercase_	   =     'a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings'

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

# to channels last
lowercase_	   =     pipe.unet.to(memory_format=torch.channels_last)
lowercase_	   =     pipe.vae.to(memory_format=torch.channels_last)
lowercase_	   =     pipe.text_encoder.to(memory_format=torch.channels_last)
if pipe.requires_safety_checker:
       lowercase_	   =     pipe.safety_checker.to(memory_format=torch.channels_last)

# optimize with ipex
lowercase_	   =     torch.randn(2, 4, 6_4, 6_4)
lowercase_	   =     torch.rand(1) * 9_9_9
lowercase_	   =     torch.randn(2, 7_7, 7_6_8)
lowercase_	   =     (sample, timestep, encoder_hidden_status)
try:
       lowercase_	   =     ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example)
except Exception:
       lowercase_	   =     ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True)
lowercase_	   =     ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True)
lowercase_	   =     ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True)
if pipe.requires_safety_checker:
       lowercase_	   =     ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True)

# compute
lowercase_	   =     6_6_6
lowercase_	   =     torch.Generator(device).manual_seed(seed)
lowercase_	   =     {'generator': generator}
if args.steps is not None:
       lowercase_	   =     args.steps

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

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





import json
import os
import tempfile

import transformers

import datasets
from utils import generate_example_dataset, get_duration


lowercase_	   =     5_0_0_0_0_0

lowercase_   ,lowercase_	   =     os.path.split(__file__)
lowercase_	   =     os.path.join(RESULTS_BASEPATH, 'results', RESULTS_FILENAME.replace('.py', '.json'))
@get_duration
def 				UpperCamelCase__					(  SCREAMING_SNAKE_CASE__      ,	**SCREAMING_SNAKE_CASE__						):
 __lowerCamelCase		:							Optional[int]		 =						dataset.map(**SCREAMING_SNAKE_CASE__						)
@get_duration
def 				UpperCamelCase__					(  SCREAMING_SNAKE_CASE__      ,	**SCREAMING_SNAKE_CASE__						):
 __lowerCamelCase		:							Tuple		 =						dataset.filter(**SCREAMING_SNAKE_CASE__						)
def 				UpperCamelCase__					(  ):
 __lowerCamelCase		:							str		 =						{'num examples': SPEED_TEST_N_EXAMPLES}
 with tempfile.TemporaryDirectory() as tmp_dir:
  __lowerCamelCase		:							Any		 =						datasets.Features({'text': datasets.Value('string'						), 'numbers': datasets.Value('float32'						)}						)
  __lowerCamelCase		:							Any		 =						generate_example_dataset(
      os.path.join(SCREAMING_SNAKE_CASE__      ,	'dataset.arrow'						)      ,	SCREAMING_SNAKE_CASE__      ,	num_examples=SCREAMING_SNAKE_CASE__						)

  __lowerCamelCase		:							List[Any]		 =						transformers.AutoTokenizer.from_pretrained('bert-base-cased'      ,	use_fast=SCREAMING_SNAKE_CASE__						)

  def tokenize(SCREAMING_SNAKE_CASE__						):
   return tokenizer(examples['text']						)

  __lowerCamelCase		:							str		 =						map(SCREAMING_SNAKE_CASE__						)

  __lowerCamelCase		:							int		 =						map(SCREAMING_SNAKE_CASE__      ,	batched=SCREAMING_SNAKE_CASE__						)

  __lowerCamelCase		:							Optional[int]		 =						map(SCREAMING_SNAKE_CASE__      ,	function=lambda SCREAMING_SNAKE_CASE__						: None      ,	batched=SCREAMING_SNAKE_CASE__						)

  with dataset.formatted_as(type='numpy'						):
   __lowerCamelCase		:							Union[str, Any]		 =						map(SCREAMING_SNAKE_CASE__      ,	function=lambda SCREAMING_SNAKE_CASE__						: None      ,	batched=SCREAMING_SNAKE_CASE__						)

  with dataset.formatted_as(type='pandas'						):
   __lowerCamelCase		:							Any		 =						map(SCREAMING_SNAKE_CASE__      ,	function=lambda SCREAMING_SNAKE_CASE__						: None      ,	batched=SCREAMING_SNAKE_CASE__						)

  with dataset.formatted_as(type='torch'      ,	columns='numbers'						):
   __lowerCamelCase		:							List[str]		 =						map(SCREAMING_SNAKE_CASE__      ,	function=lambda SCREAMING_SNAKE_CASE__						: None      ,	batched=SCREAMING_SNAKE_CASE__						)

  with dataset.formatted_as(type='tensorflow'      ,	columns='numbers'						):
   __lowerCamelCase		:							List[Any]		 =						map(SCREAMING_SNAKE_CASE__      ,	function=lambda SCREAMING_SNAKE_CASE__						: None      ,	batched=SCREAMING_SNAKE_CASE__						)

  __lowerCamelCase		:							Optional[Any]		 =						map(SCREAMING_SNAKE_CASE__      ,	function=SCREAMING_SNAKE_CASE__      ,	batched=SCREAMING_SNAKE_CASE__						)

  __lowerCamelCase		:							Optional[int]		 =						filter(SCREAMING_SNAKE_CASE__						)

  # Activate later when tokenizer support batched inputs
  # with dataset.formatted_as(type='numpy'):
  #     times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True)

 with open(SCREAMING_SNAKE_CASE__      ,	'wb'						) as f:
  f.write(json.dumps(SCREAMING_SNAKE_CASE__						).encode('utf-8'						)						)


if __name__ == "__main__":  # useful to run the profiler
       benchmark_map_filter()

