

import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


UpperCAmelCase__       :   Optional[Any] 			=			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""")
UpperCAmelCase__       :   str 			=			parser.parse_args()


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

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

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

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

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

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

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








from math import sqrt

import numpy as np
from sympy import symbols

# Coefficient
# Speed of light (m/s)
_snake_case             =     2_99_79_24_58

# Symbols
_snake_case      ,       _snake_case      ,       _snake_case      ,       _snake_case             =     symbols('''ct x y z''')






def 		lowercase_(  SCREAMING_SNAKE_CASE_	):


  '''simple docstring'''

  if velocity > c:
    raise ValueError("Speed must not exceed light speed 299,792,458 [m/s]!"	)
  elif velocity < 1:
    # Usually the speed should be much higher than 1 (c order of magnitude)
    raise ValueError("Speed must be greater than or equal to 1!"	)

  return velocity / c






def 		lowercase_(  SCREAMING_SNAKE_CASE_	):


  '''simple docstring'''

  return 1 / sqrt(1 - beta(SCREAMING_SNAKE_CASE_	) ** 2	)






def 		lowercase_(  SCREAMING_SNAKE_CASE_	):


  '''simple docstring'''

  return np.array(
      [
          [gamma(SCREAMING_SNAKE_CASE_	), -gamma(SCREAMING_SNAKE_CASE_	) * beta(SCREAMING_SNAKE_CASE_	), 0, 0],
          [-gamma(SCREAMING_SNAKE_CASE_	) * beta(SCREAMING_SNAKE_CASE_	), gamma(SCREAMING_SNAKE_CASE_	), 0, 0],
          [0, 0, 1, 0],
          [0, 0, 0, 1],
      ]	)






def 		lowercase_(  SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_ = None	):


  '''simple docstring'''

  if event is None:
    lowerCamelCase :					Tuple    =				np.array([ct, x, y, z]	)  # Symbolic four vector
  else:
    event[0] *= c  # x0 is ct (speed of light * time)

  return transformation_matrix(SCREAMING_SNAKE_CASE_	) @ event


if __name__ == "__main__":
   import doctest

   doctest.testmod()

   # Example of symbolic vector:
   _snake_case             =     transform(29_97_92_45)
   print('''Example of four vector: ''')
   print(f'''ct\' = {four_vector[0]}''')
   print(f'''x\' = {four_vector[1]}''')
   print(f'''y\' = {four_vector[2]}''')
   print(f'''z\' = {four_vector[3]}''')

   # Substitute symbols with numerical values
   _snake_case             =     {ct: c, x: 1, y: 1, z: 1}
   _snake_case             =     [four_vector[i].subs(sub_dict) for i in range(4)]

   print(f'''\n{numerical_vector}''')



