






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 							snake_case_       (    unittest.TestCase      ):

						"""simple docstring"""



						def        UpperCAmelCase__					(self:					List[str]   )    ->     Union[str, Any]:




											'''simple docstring'''



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



						@property
						def        UpperCAmelCase__					(self:					List[Any]   )    ->     Dict:




											'''simple docstring'''



											torch.manual_seed(0   )
											__a					:						List[Any]      =					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        UpperCAmelCase__					(self:					int   )    ->     str:




											'''simple docstring'''



											torch.manual_seed(0   )
											__a					:						Union[str, Any]      =					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        UpperCAmelCase__					(self:					int   )    ->     Optional[int]:




											'''simple docstring'''



											torch.manual_seed(0   )
											__a					:						List[str]      =					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") ,					)
											__a					:						Optional[int]      =					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        UpperCAmelCase__					(self:					str   )    ->     Union[str, Any]:




											'''simple docstring'''



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

											__a					:						Union[str, Any]      =					DDPMScheduler()
											__a					:						Any      =					AudioDiffusionPipeline(vqvae=__UpperCAmelCase ,					unet=self.dummy_unet ,					mel=__UpperCAmelCase ,					scheduler=__UpperCAmelCase   )
											__a					:						Optional[Any]      =					pipe.to(__UpperCAmelCase   )
											pipe.set_progress_bar_config(disable=__UpperCAmelCase   )

											__a					:						Optional[Any]      =					torch.Generator(device=__UpperCAmelCase   ).manual_seed(42   )
											__a					:						Any      =					pipe(generator=__UpperCAmelCase ,					steps=4   )
											__a					:						Union[str, Any]      =					output.audios[0]
											__a					:						Optional[int]      =					output.images[0]

											__a					:						Any      =					torch.Generator(device=__UpperCAmelCase   ).manual_seed(42   )
											__a					:						Optional[int]      =					pipe(generator=__UpperCAmelCase ,					steps=4 ,					return_dict=__UpperCAmelCase   )
											__a					:						List[str]      =					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]
											)
											__a					:						Union[str, Any]      =					np.frombuffer(image.tobytes() ,					dtype="uint8"   )[:10]
											__a					:						Optional[int]      =					np.frombuffer(image_from_tuple.tobytes() ,					dtype="uint8"   )[:10]
											__a					:						Union[str, Any]      =					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

											__a					:						List[str]      =					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] ,					)

											__a					:						str      =					DDIMScheduler()
											__a					:						int      =					self.dummy_vqvae_and_unet
											__a					:						Optional[Any]      =					AudioDiffusionPipeline(
											    vqvae=self.dummy_vqvae_and_unet[0] ,					unet=dummy_vqvae_and_unet[1] ,					mel=__UpperCAmelCase ,					scheduler=__UpperCAmelCase   )
											__a					:						Union[str, Any]      =					pipe.to(__UpperCAmelCase   )
											pipe.set_progress_bar_config(disable=__UpperCAmelCase   )

											np.random.seed(0   )
											__a					:						Union[str, Any]      =					np.random.uniform(-1 ,					1 ,					((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,)   )
											__a					:						List[Any]      =					torch.Generator(device=__UpperCAmelCase   ).manual_seed(42   )
											__a					:						Any      =					pipe(raw_audio=__UpperCAmelCase ,					generator=__UpperCAmelCase ,					start_step=5 ,					steps=10   )
											__a					:						List[Any]      =					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]
											)
											__a					:						int      =					np.frombuffer(image.tobytes() ,					dtype="uint8"   )[:10]
											__a					:						Dict      =					np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121]   )

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

											__a					:						Dict      =					self.dummy_unet_condition
											__a					:						Any      =					AudioDiffusionPipeline(
											    vqvae=self.dummy_vqvae_and_unet[0] ,					unet=__UpperCAmelCase ,					mel=__UpperCAmelCase ,					scheduler=__UpperCAmelCase   )
											__a					:						Any      =					pipe.to(__UpperCAmelCase   )
											pipe.set_progress_bar_config(disable=__UpperCAmelCase   )

											np.random.seed(0   )
											__a					:						str      =					torch.rand((1, 1, 10)   )
											__a					:						str      =					pipe(generator=__UpperCAmelCase ,					encoding=__UpperCAmelCase   )
											__a					:						List[str]      =					output.images[0]
											__a					:						int      =					np.frombuffer(image.tobytes() ,					dtype="uint8"   )[:10]
											__a					:						int      =					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 							snake_case_       (    unittest.TestCase      ):

						"""simple docstring"""



						def        UpperCAmelCase__					(self:					List[str]   )    ->     Optional[Any]:




											'''simple docstring'''



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



						def        UpperCAmelCase__					(self:					List[Any]   )    ->     Tuple:




											'''simple docstring'''



											__a					:						List[str]      =					torch_device

											__a					:						Dict      =					DiffusionPipeline.from_pretrained("teticio/audio-diffusion-ddim-256"   )
											__a					:						Any      =					pipe.to(__UpperCAmelCase   )
											pipe.set_progress_bar_config(disable=__UpperCAmelCase   )

											__a					:						str      =					torch.Generator(device=__UpperCAmelCase   ).manual_seed(42   )
											__a					:						int      =					pipe(generator=__UpperCAmelCase   )
											__a					:						List[Any]      =					output.audios[0]
											__a					:						Optional[Any]      =					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]
											__a					:						List[Any]      =					np.frombuffer(image.tobytes() ,					dtype="uint8"   )[:10]
											__a					:						int      =					np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26]   )

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













from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase__      			=						logging.get_logger(__name__)

UpperCAmelCase__      			=						{
    '''vinvino02/glpn-kitti''': '''https://huggingface.co/vinvino02/glpn-kitti/resolve/main/config.json''',
    # See all GLPN models at https://huggingface.co/models?filter=glpn
}




class 							snake_case_       (    __UpperCamelCase      ):

						"""simple docstring"""



						snake_case__        =      """glpn"""



						def __init__(self:					Tuple ,					__UpperCAmelCase:					Tuple=3 ,					__UpperCAmelCase:					Dict=4 ,					__UpperCAmelCase:					Any=[2, 2, 2, 2] ,					__UpperCAmelCase:					Optional[int]=[8, 4, 2, 1] ,					__UpperCAmelCase:					Dict=[32, 64, 160, 256] ,					__UpperCAmelCase:					List[str]=[7, 3, 3, 3] ,					__UpperCAmelCase:					Dict=[4, 2, 2, 2] ,					__UpperCAmelCase:					Optional[int]=[1, 2, 5, 8] ,					__UpperCAmelCase:					Dict=[4, 4, 4, 4] ,					__UpperCAmelCase:					List[str]="gelu" ,					__UpperCAmelCase:					str=0.0 ,					__UpperCAmelCase:					List[Any]=0.0 ,					__UpperCAmelCase:					Dict=0.02 ,					__UpperCAmelCase:					List[str]=0.1 ,					__UpperCAmelCase:					Union[str, Any]=1E-6 ,					__UpperCAmelCase:					Dict=64 ,					__UpperCAmelCase:					Dict=10 ,					__UpperCAmelCase:					Union[str, Any]=-1 ,					**__UpperCAmelCase:					Dict ,					)    ->     str:




											'''simple docstring'''



											super().__init__(**__UpperCAmelCase   )

											__a					:						Optional[Any]      =					num_channels
											__a					:						Tuple      =					num_encoder_blocks
											__a					:						int      =					depths
											__a					:						Union[str, Any]      =					sr_ratios
											__a					:						str      =					hidden_sizes
											__a					:						List[Any]      =					patch_sizes
											__a					:						Optional[Any]      =					strides
											__a					:						Any      =					mlp_ratios
											__a					:						Any      =					num_attention_heads
											__a					:						Any      =					hidden_act
											__a					:						List[Any]      =					hidden_dropout_prob
											__a					:						int      =					attention_probs_dropout_prob
											__a					:						List[str]      =					initializer_range
											__a					:						Optional[int]      =					drop_path_rate
											__a					:						Any      =					layer_norm_eps
											__a					:						List[Any]      =					decoder_hidden_size
											__a					:						Any      =					max_depth
											__a					:						int      =					head_in_index





