
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''')








from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


SCREAMING_SNAKE_CASE		:Optional[int]						=     {
    """configuration_rembert""": ["""REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """RemBertConfig""", """RemBertOnnxConfig"""]
}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE		:str						=     ["""RemBertTokenizer"""]

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE		:Optional[Any]						=     ["""RemBertTokenizerFast"""]

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE		:Dict						=     [
     """REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
     """RemBertForCausalLM""",
     """RemBertForMaskedLM""",
     """RemBertForMultipleChoice""",
     """RemBertForQuestionAnswering""",
     """RemBertForSequenceClassification""",
     """RemBertForTokenClassification""",
     """RemBertLayer""",
     """RemBertModel""",
     """RemBertPreTrainedModel""",
     """load_tf_weights_in_rembert""",
 ]


try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE		:Optional[int]						=     [
     """TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
     """TFRemBertForCausalLM""",
     """TFRemBertForMaskedLM""",
     """TFRemBertForMultipleChoice""",
     """TFRemBertForQuestionAnswering""",
     """TFRemBertForSequenceClassification""",
     """TFRemBertForTokenClassification""",
     """TFRemBertLayer""",
     """TFRemBertModel""",
     """TFRemBertPreTrainedModel""",
 ]


if TYPE_CHECKING:
 from .configuration_rembert import REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RemBertConfig, RemBertOnnxConfig

 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_rembert import RemBertTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_rembert_fast import RemBertTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_rembert import (
      REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      RemBertForCausalLM,
      RemBertForMaskedLM,
      RemBertForMultipleChoice,
      RemBertForQuestionAnswering,
      RemBertForSequenceClassification,
      RemBertForTokenClassification,
      RemBertLayer,
      RemBertModel,
      RemBertPreTrainedModel,
      load_tf_weights_in_rembert,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_rembert import (
      TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFRemBertForCausalLM,
      TFRemBertForMaskedLM,
      TFRemBertForMultipleChoice,
      TFRemBertForQuestionAnswering,
      TFRemBertForSequenceClassification,
      TFRemBertForTokenClassification,
      TFRemBertLayer,
      TFRemBertModel,
      TFRemBertPreTrainedModel,
  )


else:
 import sys

 SCREAMING_SNAKE_CASE		:List[Any]						=     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
