





'''simple docstring'''


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) * 9_99
_SCREAMING_SNAKE_CASE	 =							torch.randn(2, 77, 7_68)
_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	 =							6_66
_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")









'''simple docstring'''


import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
       from transformers import TvltImageProcessor

if is_speech_available():
       from transformers import TvltFeatureExtractor

from transformers import TvltProcessor

@require_torch
class    lowerCAmelCase_		(		unittest.TestCase		):




 def 							_snake_case   (	self		)    ->							Dict:
     _lowerCAmelCase				=      "ZinengTang/tvlt-base"
     _lowerCAmelCase				=      tempfile.mkdtemp()




 def 							_snake_case   (	self			,     **_lowerCAmelCase		)    ->							Any:
     return TvltImageProcessor.from_pretrained(self.checkpoint			,     **_lowerCAmelCase		)




 def 							_snake_case   (	self			,     **_lowerCAmelCase		)    ->							str:
     return TvltFeatureExtractor.from_pretrained(self.checkpoint			,     **_lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							Optional[Any]:
     shutil.rmtree(self.tmpdirname		)




 def 							_snake_case   (	self		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)
     processor.save_pretrained(self.tmpdirname		)
     _lowerCAmelCase				=      TvltProcessor.from_pretrained(self.tmpdirname		)

     self.assertIsInstance(processor.feature_extractor			,     _lowerCAmelCase		)
     self.assertIsInstance(processor.image_processor			,     _lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							Any:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     _lowerCAmelCase				=      np.ones([12000]		)

     _lowerCAmelCase				=      feature_extractor(_lowerCAmelCase			,     return_tensors="np"		)
     _lowerCAmelCase				=      processor(audio=_lowerCAmelCase			,     return_tensors="np"		)

     for key in audio_dict.keys():
         self.assertAlmostEqual(audio_dict[key].sum()			,     input_processor[key].sum()			,     delta=1E-2		)




 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     _lowerCAmelCase				=      np.ones([3, 224, 224]		)

     _lowerCAmelCase				=      image_processor(_lowerCAmelCase			,     return_tensors="np"		)
     _lowerCAmelCase				=      processor(images=_lowerCAmelCase			,     return_tensors="np"		)

     for key in image_dict.keys():
         self.assertAlmostEqual(image_dict[key].sum()			,     input_processor[key].sum()			,     delta=1E-2		)




 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     _lowerCAmelCase				=      np.ones([12000]		)
     _lowerCAmelCase				=      np.ones([3, 224, 224]		)

     _lowerCAmelCase				=      processor(audio=_lowerCAmelCase			,     images=_lowerCAmelCase		)

     self.assertListEqual(list(inputs.keys()		)			,     ["audio_values", "audio_mask", "pixel_values", "pixel_mask"]		)

     # test if it raises when no input is passed
     with pytest.raises(_lowerCAmelCase		):
         processor()





 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     self.assertListEqual(
         processor.model_input_names			,     image_processor.model_input_names + feature_extractor.model_input_names			,     msg="`processor` and `image_processor`+`feature_extractor` model input names do not match"			,     )


