


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							):




 def        SCREAMING_SNAKE_CASE__				(		self    ):
       # clean up the VRAM after each test
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()




 @property
 def        SCREAMING_SNAKE_CASE__				(		self    ):
       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        SCREAMING_SNAKE_CASE__				(		self    ):
       torch.manual_seed(0    )
       a						:Dict 							=       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        SCREAMING_SNAKE_CASE__				(		self    ):
       torch.manual_seed(0    )
       a						:Tuple 							=       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						:List[str] 							=       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        SCREAMING_SNAKE_CASE__				(		self    ):
       a						:str 							=       '''cpu'''  # ensure determinism for the device-dependent torch.Generator
       a						:int 							=       Mel(
           x_res=self.dummy_unet.config.sample_size[1]					,  y_res=self.dummy_unet.config.sample_size[0]					,  )

       a						:Tuple 							=       DDPMScheduler()
       a						:Union[str, Any] 							=       AudioDiffusionPipeline(vqvae=_lowerCamelCase					,  unet=self.dummy_unet					,  mel=_lowerCamelCase					,  scheduler=_lowerCamelCase    )
       a						:List[Any] 							=       pipe.to(_lowerCamelCase    )
       pipe.set_progress_bar_config(disable=_lowerCamelCase    )

       a						:Dict 							=       torch.Generator(device=_lowerCamelCase    ).manual_seed(42    )
       a						:Dict 							=       pipe(generator=_lowerCamelCase					,  steps=4    )
       a						:List[Any] 							=       output.audios[0]
       a						:Dict 							=       output.images[0]

       a						:Optional[int] 							=       torch.Generator(device=_lowerCamelCase    ).manual_seed(42    )
       a						:Tuple 							=       pipe(generator=_lowerCamelCase					,  steps=4					,  return_dict=_lowerCamelCase    )
       a						:List[Any] 							=       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						:Optional[int] 							=       np.frombuffer(image.tobytes()					,  dtype='''uint8'''    )[:10]
       a						:Optional[Any] 							=       np.frombuffer(image_from_tuple.tobytes()					,  dtype='''uint8'''    )[:10]
       a						:Optional[int] 							=       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[Any] 							=       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						:List[str] 							=       DDIMScheduler()
       a						:int 							=       self.dummy_vqvae_and_unet
       a						:Tuple 							=       AudioDiffusionPipeline(
           vqvae=self.dummy_vqvae_and_unet[0]					,  unet=dummy_vqvae_and_unet[1]					,  mel=_lowerCamelCase					,  scheduler=_lowerCamelCase    )
       a						:Union[str, Any] 							=       pipe.to(_lowerCamelCase    )
       pipe.set_progress_bar_config(disable=_lowerCamelCase    )

       np.random.seed(0    )
       a						:str 							=       np.random.uniform(-1					,  1					,  ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,)    )
       a						:Tuple 							=       torch.Generator(device=_lowerCamelCase    ).manual_seed(42    )
       a						:int 							=       pipe(raw_audio=_lowerCamelCase					,  generator=_lowerCamelCase					,  start_step=5					,  steps=10    )
       a						:int 							=       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						:Any 							=       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						:List[str] 							=       self.dummy_unet_condition
       a						:Any 							=       AudioDiffusionPipeline(
           vqvae=self.dummy_vqvae_and_unet[0]					,  unet=_lowerCamelCase					,  mel=_lowerCamelCase					,  scheduler=_lowerCamelCase    )
       a						:Optional[int] 							=       pipe.to(_lowerCamelCase    )
       pipe.set_progress_bar_config(disable=_lowerCamelCase    )

       np.random.seed(0    )
       a						:Dict 							=       torch.rand((1, 1, 10)    )
       a						:Any 							=       pipe(generator=_lowerCamelCase					,  encoding=_lowerCamelCase    )
       a						:Union[str, Any] 							=       output.images[0]
       a						:Optional[int] 							=       np.frombuffer(image.tobytes()					,  dtype='''uint8'''    )[:10]
       a						:Optional[Any] 							=       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							):




 def        SCREAMING_SNAKE_CASE__				(		self    ):
       # clean up the VRAM after each test
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()




 def        SCREAMING_SNAKE_CASE__				(		self    ):
       a						:Optional[int] 							=       torch_device

       a						:Tuple 							=       DiffusionPipeline.from_pretrained('''teticio/audio-diffusion-ddim-256'''    )
       a						:Any 							=       pipe.to(_lowerCamelCase    )
       pipe.set_progress_bar_config(disable=_lowerCamelCase    )

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

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









import math







class 		_snake_case  :




 def __init__(		self					,  _lowerCamelCase=0    ):  # a graph with Node 0,1,...,N-1
       a						:Optional[int] 							=       n
       a						:Union[str, Any] 							=       [
           [math.inf for j in range(0					,  _lowerCamelCase    )] for i in range(0					,  _lowerCamelCase    )
       ]  # adjacency matrix for weight
       a						:List[Any] 							=       [
           [math.inf for j in range(0					,  _lowerCamelCase    )] for i in range(0					,  _lowerCamelCase    )
       ]  # dp[i][j] stores minimum distance from i to j




 def        SCREAMING_SNAKE_CASE__				(		self					,  _lowerCamelCase					,  _lowerCamelCase					,  _lowerCamelCase    ):
       a						:Tuple 							=       w




 def        SCREAMING_SNAKE_CASE__				(		self    ):
       for k in range(0					,  self.n    ):
             for i in range(0					,  self.n    ):
                   for j in range(0					,  self.n    ):
                         a						:Union[str, Any] 							=       min(self.dp[i][j]					,  self.dp[i][k] + self.dp[k][j]    )




 def        SCREAMING_SNAKE_CASE__				(		self					,  _lowerCamelCase					,  _lowerCamelCase    ):
       return self.dp[u][v]


if __name__ == "__main__":
     snake_case				:    str       				=     Graph(5)
     graph.add_edge(0, 2, 9)
     graph.add_edge(0, 4, 10)
     graph.add_edge(1, 3, 5)
     graph.add_edge(2, 3, 7)
     graph.add_edge(3, 0, 10)
     graph.add_edge(3, 1, 2)
     graph.add_edge(3, 2, 1)
     graph.add_edge(3, 4, 6)
     graph.add_edge(4, 1, 3)
     graph.add_edge(4, 2, 4)
     graph.add_edge(4, 3, 9)
     graph.floyd_warshall()
     graph.show_min(1, 4)
     graph.show_min(0, 3)





