





from argparse import ArgumentParser

from . import BaseTransformersCLICommand

def   __snake_case    (       _lowerCAmelCase			:			Any						)			->    Union[str, Any]:
   return DownloadCommand(args.model					,					args.cache_dir					,					args.force					,					args.trust_remote_code						)





class        __magic_name__	(				lowerCamelCase__				):





     """simple docstring"""



     @staticmethod
     def     SCREAMING_SNAKE_CASE		(				snake_case							:ArgumentParser	):





        '''simple docstring'''




        A_       :					Any        =      parser.add_parser("download"	)
        download_parser.add_argument(
            "--cache-dir"      ,   type=snake_case      ,   default=snake_case      ,   help="Path to location to store the models"	)
        download_parser.add_argument(
            "--force"      ,   action="store_true"      ,   help="Force the model to be download even if already in cache-dir"	)
        download_parser.add_argument(
            "--trust-remote-code"      ,   action="store_true"      ,   help="Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you've reviewed the code as it will execute on your local machine"      ,   )
        download_parser.add_argument("model"      ,   type=snake_case      ,   help="Name of the model to download"	)
        download_parser.set_defaults(func=snake_case	)



     def __init__(				self							:List[Any]      ,   snake_case							:str      ,   snake_case							:str      ,   snake_case							:bool      ,   snake_case							:bool	):





        '''simple docstring'''




        A_       :					int        =      model
        A_       :					Optional[Any]        =      cache
        A_       :					List[str]        =      force
        A_       :					Dict        =      trust_remote_code





     def     SCREAMING_SNAKE_CASE		(				self							:List[str]	):





        '''simple docstring'''




        from ..models.auto import AutoModel, AutoTokenizer

        AutoModel.from_pretrained(
            self._model      ,   cache_dir=self._cache      ,   force_download=self._force      ,   trust_remote_code=self._trust_remote_code	)
        AutoTokenizer.from_pretrained(
            self._model      ,   cache_dir=self._cache      ,   force_download=self._force      ,   trust_remote_code=self._trust_remote_code	)







import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


_lowerCAmelCase				:   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''')
_lowerCAmelCase				:   Tuple						       =						parser.parse_args()


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

_lowerCAmelCase				:   Union[str, Any]						       =						'''path-to-your-trained-model'''
_lowerCAmelCase				:   Tuple						       =						StableDiffusionPipeline.from_pretrained(model_id)
if args.dpm:
 _lowerCAmelCase				:   List[str]						       =						DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
_lowerCAmelCase				:   List[Any]						       =						pipe.to(device)

# to channels last
_lowerCAmelCase				:   Any						       =						pipe.unet.to(memory_format=torch.channels_last)
_lowerCAmelCase				:   int						       =						pipe.vae.to(memory_format=torch.channels_last)
_lowerCAmelCase				:   Optional[Any]						       =						pipe.text_encoder.to(memory_format=torch.channels_last)
if pipe.requires_safety_checker:
 _lowerCAmelCase				:   Optional[int]						       =						pipe.safety_checker.to(memory_format=torch.channels_last)

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

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

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

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