


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 				lowerCamelCase__(   self     :Optional[int]   )      ->					str:
												# clean up the VRAM after each test
												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()
					@property
					def 				lowerCamelCase__(   self     :Optional[Any]   )      ->					Optional[int]:
												torch.manual_seed(0   )
												a__    =  UNetaDModel(
												    sample_size=(32, 64)    ,in_channels=1    ,out_channels=1    ,layers_per_block=2    ,block_out_channels=(1_28, 1_28)    ,down_block_types=('AttnDownBlock2D', 'DownBlock2D')    ,up_block_types=('UpBlock2D', 'AttnUpBlock2D')    ,)
												return model
					@property
					def 				lowerCamelCase__(   self     :str   )      ->					Tuple:
												torch.manual_seed(0   )
												a__    =  UNetaDConditionModel(
												    sample_size=(64, 32)    ,in_channels=1    ,out_channels=1    ,layers_per_block=2    ,block_out_channels=(1_28, 1_28)    ,down_block_types=('CrossAttnDownBlock2D', 'DownBlock2D')    ,up_block_types=('UpBlock2D', 'CrossAttnUpBlock2D')    ,cross_attention_dim=10    ,)
												return model
					@property
					def 				lowerCamelCase__(   self     :Optional[int]   )      ->					List[str]:
												torch.manual_seed(0   )
												a__    =  AutoencoderKL(
												    sample_size=(1_28, 64)    ,in_channels=1    ,out_channels=1    ,latent_channels=1    ,layers_per_block=2    ,block_out_channels=(1_28, 1_28)    ,down_block_types=('DownEncoderBlock2D', 'DownEncoderBlock2D')    ,up_block_types=('UpDecoderBlock2D', 'UpDecoderBlock2D')    ,)
												a__    =  UNetaDModel(
												    sample_size=(64, 32)    ,in_channels=1    ,out_channels=1    ,layers_per_block=2    ,block_out_channels=(1_28, 1_28)    ,down_block_types=('AttnDownBlock2D', 'DownBlock2D')    ,up_block_types=('UpBlock2D', 'AttnUpBlock2D')    ,)
												return vqvae, unet



					@slow
					def 				lowerCamelCase__(   self     :Optional[Any]   )      ->					Optional[Any]:
												a__    =  'cpu'  # ensure determinism for the device-dependent torch.Generator
												a__    =  Mel(
												    x_res=self.dummy_unet.config.sample_size[1]    ,y_res=self.dummy_unet.config.sample_size[0]    ,)

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

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

												a__    =  torch.Generator(device=__snake_case   ).manual_seed(42   )
												a__    =  pipe(generator=__snake_case    ,steps=4    ,return_dict=__snake_case   )
												a__    =  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__    =  np.frombuffer(image.tobytes()    ,dtype='uint8'   )[:10]
												a__    =  np.frombuffer(image_from_tuple.tobytes()    ,dtype='uint8'   )[:10]
												a__    =  np.array([69, 2_55, 2_55, 2_55, 0, 0, 77, 1_81, 12, 1_27]   )

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

												a__    =  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__    =  DDIMScheduler()
												a__    =  self.dummy_vqvae_and_unet
												a__    =  AudioDiffusionPipeline(
												    vqvae=self.dummy_vqvae_and_unet[0]    ,unet=dummy_vqvae_and_unet[1]    ,mel=__snake_case    ,scheduler=__snake_case   )
												a__    =  pipe.to(__snake_case   )
												pipe.set_progress_bar_config(disable=__snake_case   )

												np.random.seed(0   )
												a__    =  np.random.uniform(-1    ,1    ,((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,)   )
												a__    =  torch.Generator(device=__snake_case   ).manual_seed(42   )
												a__    =  pipe(raw_audio=__snake_case    ,generator=__snake_case    ,start_step=5    ,steps=10   )
												a__    =  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__    =  np.frombuffer(image.tobytes()    ,dtype='uint8'   )[:10]
												a__    =  np.array([1_20, 1_17, 1_10, 1_09, 1_38, 1_67, 1_38, 1_48, 1_32, 1_21]   )

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

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

												np.random.seed(0   )
												a__    =  torch.rand((1, 1, 10)   )
												a__    =  pipe(generator=__snake_case    ,encoding=__snake_case   )
												a__    =  output.images[0]
												a__    =  np.frombuffer(image.tobytes()    ,dtype='uint8'   )[:10]
												a__    =  np.array([1_07, 1_03, 1_20, 1_27, 1_42, 1_22, 1_13, 1_22, 97, 1_11]   )

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







@slow
@require_torch_gpu
class  snake_case_     (unittest.TestCase   ):
					def 				lowerCamelCase__(   self     :Dict   )      ->					int:
												# clean up the VRAM after each test
												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()



					def 				lowerCamelCase__(   self     :List[Any]   )      ->					List[str]:
												a__    =  torch_device

												a__    =  DiffusionPipeline.from_pretrained('teticio/audio-diffusion-ddim-256'   )
												a__    =  pipe.to(__snake_case   )
												pipe.set_progress_bar_config(disable=__snake_case   )

												a__    =  torch.Generator(device=__snake_case   ).manual_seed(42   )
												a__    =  pipe(generator=__snake_case   )
												a__    =  output.audios[0]
												a__    =  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__    =  np.frombuffer(image.tobytes()    ,dtype='uint8'   )[:10]
												a__    =  np.array([1_51, 1_67, 1_54, 1_44, 1_22, 1_34, 1_21, 1_05, 70, 26]   )

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








import unittest

from knapsack import greedy_knapsack as kp







class  snake_case_     (unittest.TestCase   ):
					def 				lowerCamelCase__(   self     :Optional[Any]   )      ->					Union[str, Any]:
												a__    =  [10, 20, 30, 40, 50, 60]
												a__    =  [2, 4, 6, 8, 10, 12]
												a__    =  1_00
												self.assertEqual(kp.calc_profit(__snake_case    ,__snake_case    ,__snake_case   )    ,2_10   )
					def 				lowerCamelCase__(   self     :str   )      ->					Optional[int]:
												self.assertRaisesRegex(__snake_case    ,'max_weight must greater than zero.'   )
					def 				lowerCamelCase__(   self     :Optional[Any]   )      ->					int:
												self.assertRaisesRegex(__snake_case    ,'Weight can not be negative.'   )
					def 				lowerCamelCase__(   self     :str   )      ->					List[str]:
												self.assertRaisesRegex(__snake_case    ,'Profit can not be negative.'   )
					def 				lowerCamelCase__(   self     :str   )      ->					Optional[Any]:
												self.assertRaisesRegex(__snake_case    ,'max_weight must greater than zero.'   )



					def 				lowerCamelCase__(   self     :int   )      ->					List[Any]:
												self.assertRaisesRegex(
												    __snake_case    ,'The length of profit and weight must be same.'   )


if __name__ == "__main__":
						unittest.main()




