






import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


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


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

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

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

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

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

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

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








from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch



@require_torch
@require_tf
class        A__(a_							):
						"""simple docstring"""






						def 			UpperCamelCase__					(    self    )     -> List[str]:
									a_						:						List[Any] 							= SMALL_MODEL_IDENTIFIER
									a_						:						Optional[int] 							= """pt"""
									a_						:						Union[str, Any] 							= """tf"""





						def 			UpperCamelCase__					(    self  ,      _lowercase    )     -> str:
									a_						:						Union[str, Any] 							= AutoModel.from_pretrained(self.test_model    )
									model_pt.save_pretrained(_lowercase    )





						def 			UpperCamelCase__					(    self  ,      _lowercase    )     -> List[str]:
									a_						:						int 							= TFAutoModel.from_pretrained(self.test_model  ,      from_pt=_lowercase    )
									model_tf.save_pretrained(_lowercase    )





						def 			UpperCamelCase__					(    self    )     -> Optional[Any]:
									a_						:						str 							= """mock_framework"""

									# Framework provided - return whatever the user provides
									a_						:						Optional[int] 							= FeaturesManager.determine_framework(self.test_model  ,      _lowercase    )
									self.assertEqual(_lowercase  ,      _lowercase    )

									# Local checkpoint and framework provided - return provided framework
									# PyTorch checkpoint
									with TemporaryDirectory() as local_pt_ckpt:
												self._setup_pt_ckpt(_lowercase    )
												a_						:						Optional[int] 							= FeaturesManager.determine_framework(_lowercase  ,      _lowercase    )
												self.assertEqual(_lowercase  ,      _lowercase    )

									# TensorFlow checkpoint
									with TemporaryDirectory() as local_tf_ckpt:
												self._setup_tf_ckpt(_lowercase    )
												a_						:						List[str] 							= FeaturesManager.determine_framework(_lowercase  ,      _lowercase    )
												self.assertEqual(_lowercase  ,      _lowercase    )





						def 			UpperCamelCase__					(    self    )     -> Union[str, Any]:
									# PyTorch checkpoint
									with TemporaryDirectory() as local_pt_ckpt:
												self._setup_pt_ckpt(_lowercase    )
												a_						:						Union[str, Any] 							= FeaturesManager.determine_framework(_lowercase    )
												self.assertEqual(_lowercase  ,      self.framework_pt    )

									# TensorFlow checkpoint
									with TemporaryDirectory() as local_tf_ckpt:
												self._setup_tf_ckpt(_lowercase    )
												a_						:						Union[str, Any] 							= FeaturesManager.determine_framework(_lowercase    )
												self.assertEqual(_lowercase  ,      self.framework_tf    )

									# Invalid local checkpoint
									with TemporaryDirectory() as local_invalid_ckpt:
												with self.assertRaises(_lowercase    ):
															a_						:						Union[str, Any] 							= FeaturesManager.determine_framework(_lowercase    )





						def 			UpperCamelCase__					(    self    )     -> List[Any]:
									a_						:						int 							= MagicMock(return_value=_lowercase    )
									with patch("""transformers.onnx.features.is_tf_available"""  ,      _lowercase    ):
												a_						:						Union[str, Any] 							= FeaturesManager.determine_framework(self.test_model    )
												self.assertEqual(_lowercase  ,      self.framework_pt    )

									# PyTorch not in environment -> use TensorFlow
									a_						:						Dict 							= MagicMock(return_value=_lowercase    )
									with patch("""transformers.onnx.features.is_torch_available"""  ,      _lowercase    ):
												a_						:						Union[str, Any] 							= FeaturesManager.determine_framework(self.test_model    )
												self.assertEqual(_lowercase  ,      self.framework_tf    )

									# Both in environment -> use PyTorch
									a_						:						Optional[Any] 							= MagicMock(return_value=_lowercase    )
									a_						:						List[Any] 							= MagicMock(return_value=_lowercase    )
									with patch("""transformers.onnx.features.is_tf_available"""  ,      _lowercase    ), patch(
									    """transformers.onnx.features.is_torch_available"""  ,      _lowercase    ):
												a_						:						Any 							= FeaturesManager.determine_framework(self.test_model    )
												self.assertEqual(_lowercase  ,      self.framework_pt    )

									# Both not in environment -> raise error
									a_						:						List[str] 							= MagicMock(return_value=_lowercase    )
									a_						:						Optional[Any] 							= MagicMock(return_value=_lowercase    )
									with patch("""transformers.onnx.features.is_tf_available"""  ,      _lowercase    ), patch(
									    """transformers.onnx.features.is_torch_available"""  ,      _lowercase    ):
												with self.assertRaises(_lowercase    ):
															a_						:						Dict 							= FeaturesManager.determine_framework(self.test_model    )


