



"""simple docstring"""





import gc
import unittest

import numpy as np
import torch

from diffusers import (
    AudioDiffusionPipeline,
    AutoencoderKL,
    DDIMScheduler,
    DDPMScheduler,
    DiffusionPipeline,
    Mel,
    UNetaDConditionModel,
    UNetaDModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()
class 				lowercase__       (   unittest.TestCase				):







       '''simple docstring'''


       def 					lowercase__    (							self      :  List[Any]   )			->						Any:


             '''simple docstring'''




             super().tearDown()
             gc.collect()
             torch.cuda.empty_cache()



       @property
       def 					lowercase__    (							self      :  Tuple   )			->						List[Any]:


             '''simple docstring'''




             torch.manual_seed(0   )
             UpperCAmelCase_      			=							UNetaDModel(
                 sample_size=(32, 64)		,    in_channels=1		,    out_channels=1		,    layers_per_block=2		,    block_out_channels=(128, 128)		,    down_block_types=("AttnDownBlock2D", "DownBlock2D")		,    up_block_types=("UpBlock2D", "AttnUpBlock2D")		,    )
             return model



       @property
       def 					lowercase__    (							self      :  Any   )			->						str:


             '''simple docstring'''




             torch.manual_seed(0   )
             UpperCAmelCase_      			=							UNetaDConditionModel(
                 sample_size=(64, 32)		,    in_channels=1		,    out_channels=1		,    layers_per_block=2		,    block_out_channels=(128, 128)		,    down_block_types=("CrossAttnDownBlock2D", "DownBlock2D")		,    up_block_types=("UpBlock2D", "CrossAttnUpBlock2D")		,    cross_attention_dim=10		,    )
             return model



       @property
       def 					lowercase__    (							self      :  str   )			->						Optional[int]:


             '''simple docstring'''




             torch.manual_seed(0   )
             UpperCAmelCase_      			=							AutoencoderKL(
                 sample_size=(128, 64)		,    in_channels=1		,    out_channels=1		,    latent_channels=1		,    layers_per_block=2		,    block_out_channels=(128, 128)		,    down_block_types=("DownEncoderBlock2D", "DownEncoderBlock2D")		,    up_block_types=("UpDecoderBlock2D", "UpDecoderBlock2D")		,    )
             UpperCAmelCase_      			=							UNetaDModel(
                 sample_size=(64, 32)		,    in_channels=1		,    out_channels=1		,    layers_per_block=2		,    block_out_channels=(128, 128)		,    down_block_types=("AttnDownBlock2D", "DownBlock2D")		,    up_block_types=("UpBlock2D", "AttnUpBlock2D")		,    )
             return vqvae, unet







       @slow
       def 					lowercase__    (							self      :  Optional[Any]   )			->						str:


             '''simple docstring'''




             UpperCAmelCase_      			=							"cpu"  # ensure determinism for the device-dependent torch.Generator
             UpperCAmelCase_      			=							Mel(
                 x_res=self.dummy_unet.config.sample_size[1]		,    y_res=self.dummy_unet.config.sample_size[0]		,    )

             UpperCAmelCase_      			=							DDPMScheduler()
             UpperCAmelCase_      			=							AudioDiffusionPipeline(vqvae=_UpperCAmelCase		,    unet=self.dummy_unet		,    mel=_UpperCAmelCase		,    scheduler=_UpperCAmelCase   )
             UpperCAmelCase_      			=							pipe.to(_UpperCAmelCase   )
             pipe.set_progress_bar_config(disable=_UpperCAmelCase   )

             UpperCAmelCase_      			=							torch.Generator(device=_UpperCAmelCase   ).manual_seed(42   )
             UpperCAmelCase_      			=							pipe(generator=_UpperCAmelCase		,    steps=4   )
             UpperCAmelCase_      			=							output.audios[0]
             UpperCAmelCase_      			=							output.images[0]

             UpperCAmelCase_      			=							torch.Generator(device=_UpperCAmelCase   ).manual_seed(42   )
             UpperCAmelCase_      			=							pipe(generator=_UpperCAmelCase		,    steps=4		,    return_dict=_UpperCAmelCase   )
             UpperCAmelCase_      			=							output[0][0]

             assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length)
             assert (
                 image.height == self.dummy_unet.config.sample_size[0]
                 and image.width == self.dummy_unet.config.sample_size[1]
             )
             UpperCAmelCase_      			=							np.frombuffer(image.tobytes()		,    dtype="uint8"   )[:10]
             UpperCAmelCase_      			=							np.frombuffer(image_from_tuple.tobytes()		,    dtype="uint8"   )[:10]
             UpperCAmelCase_      			=							np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127]   )

             assert np.abs(image_slice.flatten() - expected_slice   ).max() == 0
             assert np.abs(image_from_tuple_slice.flatten() - expected_slice   ).max() == 0

             UpperCAmelCase_      			=							Mel(
                 x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1]		,    y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0]		,    )

             UpperCAmelCase_      			=							DDIMScheduler()
             UpperCAmelCase_      			=							self.dummy_vqvae_and_unet
             UpperCAmelCase_      			=							AudioDiffusionPipeline(
                 vqvae=self.dummy_vqvae_and_unet[0]		,    unet=dummy_vqvae_and_unet[1]		,    mel=_UpperCAmelCase		,    scheduler=_UpperCAmelCase   )
             UpperCAmelCase_      			=							pipe.to(_UpperCAmelCase   )
             pipe.set_progress_bar_config(disable=_UpperCAmelCase   )

             np.random.seed(0   )
             UpperCAmelCase_      			=							np.random.uniform(-1		,    1		,    ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,)   )
             UpperCAmelCase_      			=							torch.Generator(device=_UpperCAmelCase   ).manual_seed(42   )
             UpperCAmelCase_      			=							pipe(raw_audio=_UpperCAmelCase		,    generator=_UpperCAmelCase		,    start_step=5		,    steps=10   )
             UpperCAmelCase_      			=							output.images[0]

             assert (
                 image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0]
                 and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1]
             )
             UpperCAmelCase_      			=							np.frombuffer(image.tobytes()		,    dtype="uint8"   )[:10]
             UpperCAmelCase_      			=							np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121]   )

             assert np.abs(image_slice.flatten() - expected_slice   ).max() == 0

             UpperCAmelCase_      			=							self.dummy_unet_condition
             UpperCAmelCase_      			=							AudioDiffusionPipeline(
                 vqvae=self.dummy_vqvae_and_unet[0]		,    unet=_UpperCAmelCase		,    mel=_UpperCAmelCase		,    scheduler=_UpperCAmelCase   )
             UpperCAmelCase_      			=							pipe.to(_UpperCAmelCase   )
             pipe.set_progress_bar_config(disable=_UpperCAmelCase   )

             np.random.seed(0   )
             UpperCAmelCase_      			=							torch.rand((1, 1, 10)   )
             UpperCAmelCase_      			=							pipe(generator=_UpperCAmelCase		,    encoding=_UpperCAmelCase   )
             UpperCAmelCase_      			=							output.images[0]
             UpperCAmelCase_      			=							np.frombuffer(image.tobytes()		,    dtype="uint8"   )[:10]
             UpperCAmelCase_      			=							np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111]   )

             assert np.abs(image_slice.flatten() - expected_slice   ).max() == 0



@slow
@require_torch_gpu
class 				lowercase__       (   unittest.TestCase				):







       '''simple docstring'''


       def 					lowercase__    (							self      :  Optional[int]   )			->						Optional[Any]:


             '''simple docstring'''




             super().tearDown()
             gc.collect()
             torch.cuda.empty_cache()







       def 					lowercase__    (							self      :  Tuple   )			->						str:


             '''simple docstring'''




             UpperCAmelCase_      			=							torch_device

             UpperCAmelCase_      			=							DiffusionPipeline.from_pretrained("teticio/audio-diffusion-ddim-256"   )
             UpperCAmelCase_      			=							pipe.to(_UpperCAmelCase   )
             pipe.set_progress_bar_config(disable=_UpperCAmelCase   )

             UpperCAmelCase_      			=							torch.Generator(device=_UpperCAmelCase   ).manual_seed(42   )
             UpperCAmelCase_      			=							pipe(generator=_UpperCAmelCase   )
             UpperCAmelCase_      			=							output.audios[0]
             UpperCAmelCase_      			=							output.images[0]

             assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length)
             assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1]
             UpperCAmelCase_      			=							np.frombuffer(image.tobytes()		,    dtype="uint8"   )[:10]
             UpperCAmelCase_      			=							np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26]   )

             assert np.abs(image_slice.flatten() - expected_slice   ).max() == 0









"""simple docstring"""





from ..utils import DummyObject, requires_backends
class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Tuple		,    *_UpperCAmelCase      :  str		,    **_UpperCAmelCase      :  Any   )			->						List[str]:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  List[Any]		,    *_UpperCAmelCase      :  Dict		,    **_UpperCAmelCase      :  Tuple   )			->						List[Any]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  List[Any]		,    *_UpperCAmelCase      :  List[Any]		,    **_UpperCAmelCase      :  str   )			->						int:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Union[str, Any]		,    *_UpperCAmelCase      :  Any		,    **_UpperCAmelCase      :  Tuple   )			->						str:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  str		,    *_UpperCAmelCase      :  Optional[int]		,    **_UpperCAmelCase      :  int   )			->						Any:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  List[str]		,    *_UpperCAmelCase      :  Optional[Any]		,    **_UpperCAmelCase      :  Optional[int]   )			->						Dict:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Union[str, Any]		,    *_UpperCAmelCase      :  Dict		,    **_UpperCAmelCase      :  Dict   )			->						str:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  Optional[int]		,    *_UpperCAmelCase      :  int		,    **_UpperCAmelCase      :  List[Any]   )			->						List[str]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  Dict		,    *_UpperCAmelCase      :  Dict		,    **_UpperCAmelCase      :  Union[str, Any]   )			->						int:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Any		,    *_UpperCAmelCase      :  str		,    **_UpperCAmelCase      :  Tuple   )			->						Optional[int]:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  str		,    *_UpperCAmelCase      :  Optional[Any]		,    **_UpperCAmelCase      :  List[str]   )			->						str:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  int		,    *_UpperCAmelCase      :  Any		,    **_UpperCAmelCase      :  int   )			->						Dict:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  str		,    *_UpperCAmelCase      :  int		,    **_UpperCAmelCase      :  Dict   )			->						Tuple:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  Optional[Any]		,    *_UpperCAmelCase      :  List[str]		,    **_UpperCAmelCase      :  List[Any]   )			->						List[Any]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  List[Any]		,    *_UpperCAmelCase      :  int		,    **_UpperCAmelCase      :  int   )			->						Optional[Any]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Dict		,    *_UpperCAmelCase      :  Any		,    **_UpperCAmelCase      :  Optional[Any]   )			->						int:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  List[Any]		,    *_UpperCAmelCase      :  str		,    **_UpperCAmelCase      :  List[str]   )			->						Any:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  List[str]		,    *_UpperCAmelCase      :  str		,    **_UpperCAmelCase      :  Optional[Any]   )			->						Tuple:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Union[str, Any]		,    *_UpperCAmelCase      :  Optional[int]		,    **_UpperCAmelCase      :  Tuple   )			->						str:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  str		,    *_UpperCAmelCase      :  Union[str, Any]		,    **_UpperCAmelCase      :  Union[str, Any]   )			->						List[str]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  List[Any]		,    *_UpperCAmelCase      :  Optional[int]		,    **_UpperCAmelCase      :  Optional[Any]   )			->						str:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Optional[Any]		,    *_UpperCAmelCase      :  str		,    **_UpperCAmelCase      :  int   )			->						Union[str, Any]:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  Union[str, Any]		,    *_UpperCAmelCase      :  Optional[Any]		,    **_UpperCAmelCase      :  List[Any]   )			->						str:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  List[Any]		,    *_UpperCAmelCase      :  Dict		,    **_UpperCAmelCase      :  str   )			->						List[str]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  List[str]		,    *_UpperCAmelCase      :  Optional[int]		,    **_UpperCAmelCase      :  Optional[int]   )			->						Tuple:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  Union[str, Any]		,    *_UpperCAmelCase      :  Optional[int]		,    **_UpperCAmelCase      :  str   )			->						Union[str, Any]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  Optional[int]		,    *_UpperCAmelCase      :  Any		,    **_UpperCAmelCase      :  List[str]   )			->						Optional[int]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Optional[Any]		,    *_UpperCAmelCase      :  Union[str, Any]		,    **_UpperCAmelCase      :  Optional[Any]   )			->						Tuple:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  Optional[int]		,    *_UpperCAmelCase      :  Union[str, Any]		,    **_UpperCAmelCase      :  Dict   )			->						Any:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  List[str]		,    *_UpperCAmelCase      :  List[str]		,    **_UpperCAmelCase      :  Optional[Any]   )			->						List[str]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Tuple		,    *_UpperCAmelCase      :  List[str]		,    **_UpperCAmelCase      :  Optional[int]   )			->						Tuple:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  Any		,    *_UpperCAmelCase      :  Tuple		,    **_UpperCAmelCase      :  Optional[int]   )			->						Any:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  List[Any]		,    *_UpperCAmelCase      :  int		,    **_UpperCAmelCase      :  int   )			->						int:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  Tuple		,    *_UpperCAmelCase      :  int		,    **_UpperCAmelCase      :  Any   )			->						List[Any]:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  str		,    *_UpperCAmelCase      :  int		,    **_UpperCAmelCase      :  str   )			->						Any:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  int		,    *_UpperCAmelCase      :  Dict		,    **_UpperCAmelCase      :  List[Any]   )			->						Dict:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )



class 				lowercase__       (   metaclass=SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''flax''']



       def __init__(							self      :  int		,    *_UpperCAmelCase      :  Any		,    **_UpperCAmelCase      :  int   )			->						Union[str, Any]:


             '''simple docstring'''




             requires_backends(self		,    ["flax"]   )



       @classmethod
       def 					lowercase__    (							cls      :  Optional[int]		,    *_UpperCAmelCase      :  Dict		,    **_UpperCAmelCase      :  Dict   )			->						Dict:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )







       @classmethod
       def 					lowercase__    (							cls      :  str		,    *_UpperCAmelCase      :  Dict		,    **_UpperCAmelCase      :  Any   )			->						List[str]:


             '''simple docstring'''




             requires_backends(cls		,    ["flax"]   )




