



"""simple docstring"""





from math import cos, sin, sqrt, tau

from audio_filters.iir_filter import IIRFilter
def 	__lowerCAmelCase						(		__UpperCamelCase      :			int					, __UpperCamelCase      :			int					, __UpperCamelCase      :			float = 1 / sqrt(2    )    ):


						'''simple docstring'''






						snake_case_   : int						=			tau * frequency / samplerate
						snake_case_   : Any						=			sin(__UpperCamelCase    )
						snake_case_   : str						=			cos(__UpperCamelCase    )
						snake_case_   : List[Any]						=			_sin / (2 * q_factor)

						snake_case_   : List[str]						=			(1 - _cos) / 2
						snake_case_   : str						=			1 - _cos

						snake_case_   : Union[str, Any]						=			1 + alpha
						snake_case_   : Tuple						=			-2 * _cos
						snake_case_   : int						=			1 - alpha

						snake_case_   : List[str]						=			IIRFilter(2    )
						filt.set_coefficients([aa, aa, aa]					, [ba, ba, ba]    )
						return filt
def 	__lowerCAmelCase						(		__UpperCamelCase      :			int					, __UpperCamelCase      :			int					, __UpperCamelCase      :			float = 1 / sqrt(2    )    ):


						'''simple docstring'''






						snake_case_   : Any						=			tau * frequency / samplerate
						snake_case_   : Union[str, Any]						=			sin(__UpperCamelCase    )
						snake_case_   : Tuple						=			cos(__UpperCamelCase    )
						snake_case_   : int						=			_sin / (2 * q_factor)

						snake_case_   : str						=			(1 + _cos) / 2
						snake_case_   : Union[str, Any]						=			-1 - _cos

						snake_case_   : List[Any]						=			1 + alpha
						snake_case_   : str						=			-2 * _cos
						snake_case_   : Optional[int]						=			1 - alpha

						snake_case_   : int						=			IIRFilter(2    )
						filt.set_coefficients([aa, aa, aa]					, [ba, ba, ba]    )
						return filt
def 	__lowerCAmelCase						(		__UpperCamelCase      :			int					, __UpperCamelCase      :			int					, __UpperCamelCase      :			float = 1 / sqrt(2    )    ):


						'''simple docstring'''






						snake_case_   : str						=			tau * frequency / samplerate
						snake_case_   : Union[str, Any]						=			sin(__UpperCamelCase    )
						snake_case_   : Any						=			cos(__UpperCamelCase    )
						snake_case_   : Optional[Any]						=			_sin / (2 * q_factor)

						snake_case_   : str						=			_sin / 2
						snake_case_   : List[Any]						=			0
						snake_case_   : Any						=			-ba

						snake_case_   : Optional[int]						=			1 + alpha
						snake_case_   : Union[str, Any]						=			-2 * _cos
						snake_case_   : Any						=			1 - alpha

						snake_case_   : str						=			IIRFilter(2    )
						filt.set_coefficients([aa, aa, aa]					, [ba, ba, ba]    )
						return filt
def 	__lowerCAmelCase						(		__UpperCamelCase      :			int					, __UpperCamelCase      :			int					, __UpperCamelCase      :			float = 1 / sqrt(2    )    ):


						'''simple docstring'''






						snake_case_   : int						=			tau * frequency / samplerate
						snake_case_   : str						=			sin(__UpperCamelCase    )
						snake_case_   : Optional[int]						=			cos(__UpperCamelCase    )
						snake_case_   : Any						=			_sin / (2 * q_factor)

						snake_case_   : List[str]						=			1 - alpha
						snake_case_   : Optional[Any]						=			-2 * _cos
						snake_case_   : List[Any]						=			1 + alpha

						snake_case_   : int						=			IIRFilter(2    )
						filt.set_coefficients([ba, ba, ba]					, [ba, ba, ba]    )
						return filt
def 	__lowerCAmelCase						(		__UpperCamelCase      :			int					, __UpperCamelCase      :			int					, __UpperCamelCase      :			float					, __UpperCamelCase      :			float = 1 / sqrt(2    )					, ):


						'''simple docstring'''






						snake_case_   : Union[str, Any]						=			tau * frequency / samplerate
						snake_case_   : Optional[Any]						=			sin(__UpperCamelCase    )
						snake_case_   : Tuple						=			cos(__UpperCamelCase    )
						snake_case_   : Any						=			_sin / (2 * q_factor)
						snake_case_   : Any						=			1_0 ** (gain_db / 4_0)

						snake_case_   : Optional[Any]						=			1 + alpha * big_a
						snake_case_   : Any						=			-2 * _cos
						snake_case_   : Any						=			1 - alpha * big_a
						snake_case_   : Union[str, Any]						=			1 + alpha / big_a
						snake_case_   : List[str]						=			-2 * _cos
						snake_case_   : int						=			1 - alpha / big_a

						snake_case_   : str						=			IIRFilter(2    )
						filt.set_coefficients([aa, aa, aa]					, [ba, ba, ba]    )
						return filt
def 	__lowerCAmelCase						(		__UpperCamelCase      :			int					, __UpperCamelCase      :			int					, __UpperCamelCase      :			float					, __UpperCamelCase      :			float = 1 / sqrt(2    )					, ):


						'''simple docstring'''






						snake_case_   : List[Any]						=			tau * frequency / samplerate
						snake_case_   : List[str]						=			sin(__UpperCamelCase    )
						snake_case_   : Optional[int]						=			cos(__UpperCamelCase    )
						snake_case_   : int						=			_sin / (2 * q_factor)
						snake_case_   : int						=			1_0 ** (gain_db / 4_0)
						snake_case_   : Tuple						=			(big_a + 1) - (big_a - 1) * _cos
						snake_case_   : int						=			(big_a + 1) + (big_a - 1) * _cos
						snake_case_   : Any						=			(big_a - 1) - (big_a + 1) * _cos
						snake_case_   : Union[str, Any]						=			(big_a - 1) + (big_a + 1) * _cos
						snake_case_   : List[str]						=			2 * sqrt(__UpperCamelCase    ) * alpha

						snake_case_   : Optional[int]						=			big_a * (pmc + aaa)
						snake_case_   : Optional[Any]						=			2 * big_a * mpc
						snake_case_   : Union[str, Any]						=			big_a * (pmc - aaa)
						snake_case_   : Dict						=			ppmc + aaa
						snake_case_   : str						=			-2 * pmpc
						snake_case_   : str						=			ppmc - aaa

						snake_case_   : List[str]						=			IIRFilter(2    )
						filt.set_coefficients([aa, aa, aa]					, [ba, ba, ba]    )
						return filt






def 	__lowerCAmelCase						(		__UpperCamelCase      :			int					, __UpperCamelCase      :			int					, __UpperCamelCase      :			float					, __UpperCamelCase      :			float = 1 / sqrt(2    )					, ):


						'''simple docstring'''






						snake_case_   : Dict						=			tau * frequency / samplerate
						snake_case_   : int						=			sin(__UpperCamelCase    )
						snake_case_   : List[Any]						=			cos(__UpperCamelCase    )
						snake_case_   : int						=			_sin / (2 * q_factor)
						snake_case_   : Dict						=			1_0 ** (gain_db / 4_0)
						snake_case_   : Dict						=			(big_a + 1) - (big_a - 1) * _cos
						snake_case_   : Any						=			(big_a + 1) + (big_a - 1) * _cos
						snake_case_   : Any						=			(big_a - 1) - (big_a + 1) * _cos
						snake_case_   : List[str]						=			(big_a - 1) + (big_a + 1) * _cos
						snake_case_   : int						=			2 * sqrt(__UpperCamelCase    ) * alpha

						snake_case_   : Optional[Any]						=			big_a * (ppmc + aaa)
						snake_case_   : List[str]						=			-2 * big_a * pmpc
						snake_case_   : Any						=			big_a * (ppmc - aaa)
						snake_case_   : Optional[Any]						=			pmc + aaa
						snake_case_   : Optional[Any]						=			2 * mpc
						snake_case_   : str						=			pmc - aaa

						snake_case_   : Any						=			IIRFilter(2    )
						filt.set_coefficients([aa, aa, aa]					, [ba, ba, ba]    )
						return filt






"""simple docstring"""





import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


__lowerCAmelCase       :					Optional[int]      					=					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       :					Optional[Any]      					=					parser.parse_args()


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

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

# to channels last
__lowerCAmelCase       :					Optional[Any]      					=					pipe.unet.to(memory_format=torch.channels_last)
__lowerCAmelCase       :					List[str]      					=					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       :					Dict      					=					pipe.safety_checker.to(memory_format=torch.channels_last)

# optimize with ipex
__lowerCAmelCase       :					Tuple      					=					torch.randn(2, 4, 64, 64)
__lowerCAmelCase       :					Any      					=					torch.rand(1) * 999
__lowerCAmelCase       :					List[str]      					=					torch.randn(2, 77, 768)
__lowerCAmelCase       :					Optional[int]      					=					(sample, timestep, encoder_hidden_status)
try:
					__lowerCAmelCase       :					List[Any]      					=					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       :					Any      					=					ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True)
__lowerCAmelCase       :					int      					=					ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True)
if pipe.requires_safety_checker:
					__lowerCAmelCase       :					Union[str, Any]      					=					ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True)

# compute
__lowerCAmelCase       :					List[str]      					=					666
__lowerCAmelCase       :					Optional[int]      					=					torch.Generator(device).manual_seed(seed)
__lowerCAmelCase       :					List[Any]      					=					{'''generator''': generator}
if args.steps is not None:
					__lowerCAmelCase       :					Any      					=					args.steps

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

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

