
import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


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


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

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

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

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

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

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

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




import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
 import torch

 from transformers.activations import gelu_new, gelu_python, get_activation


@require_torch
class 						__lowerCAmelCase	(	unittest.TestCase      ):



      """simple docstring"""







      def 		snake_case_			(      self		:       Optional[Any]				):
          __lowercase		:			Union[str, Any]	    =       torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]				)
          __lowercase		:			Any	    =       get_activation('''gelu'''				)
          self.assertTrue(torch.allclose(gelu_python(_snake_case				)				,       torch_builtin(_snake_case				)				)				)
          self.assertFalse(torch.allclose(gelu_python(_snake_case				)				,       gelu_new(_snake_case				)				)				)




      def 		snake_case_			(      self		:       Dict				):
          __lowercase		:			Any	    =       torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]				)
          __lowercase		:			List[str]	    =       get_activation('''gelu'''				)
          __lowercase		:			Optional[Any]	    =       get_activation('''gelu_10'''				)

          __lowercase		:			Tuple	    =       torch_builtin(_snake_case				)
          __lowercase		:			List[Any]	    =       geluaa(_snake_case				)

          __lowercase		:			Tuple	    =       torch.where(y_gelu_aa < 10.0				,       1				,       0				)

          self.assertTrue(torch.max(_snake_case				).item() == 10.0				)
          self.assertTrue(torch.allclose(y_gelu * clipped_mask				,       y_gelu_aa * clipped_mask				)				)




      def 		snake_case_			(      self		:       Any				):
          get_activation('''gelu'''				)
          get_activation('''gelu_10'''				)
          get_activation('''gelu_fast'''				)
          get_activation('''gelu_new'''				)
          get_activation('''gelu_python'''				)
          get_activation('''gelu_pytorch_tanh'''				)
          get_activation('''linear'''				)
          get_activation('''mish'''				)
          get_activation('''quick_gelu'''				)
          get_activation('''relu'''				)
          get_activation('''sigmoid'''				)
          get_activation('''silu'''				)
          get_activation('''swish'''				)
          get_activation('''tanh'''				)
          with self.assertRaises(_snake_case				):
              get_activation('''bogus'''				)
          with self.assertRaises(_snake_case				):
              get_activation(_snake_case				)




      def 		snake_case_			(      self		:       Dict				):
          __lowercase		:			Union[str, Any]	    =       get_activation('''gelu'''				)
          __lowercase		:			List[str]	    =       1
          __lowercase		:			Union[str, Any]	    =       get_activation('''gelu'''				)
          self.assertEqual(acta.a				,       1				)
          with self.assertRaises(_snake_case				):
              __lowercase		:			Tuple	    =       acta.a


