


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


     """simple docstring"""

     def 		a__						(	self:						Dict	)->    Optional[int]:
           # clean up the VRAM after each test
           super().tearDown()
           gc.collect()
           torch.cuda.empty_cache()

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

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

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

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

           lowerCamelCase					:					Optional[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]     ,		)

           lowerCamelCase					:					int        =	DDIMScheduler()
           lowerCamelCase					:					int        =	self.dummy_vqvae_and_unet
           lowerCamelCase					:					Dict        =	AudioDiffusionPipeline(
               vqvae=self.dummy_vqvae_and_unet[0]     ,		unet=dummy_vqvae_and_unet[1]     ,		mel=__a     ,		scheduler=__a	)
           lowerCamelCase					:					List[str]        =	pipe.to(__a	)
           pipe.set_progress_bar_config(disable=__a	)

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

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

           lowerCamelCase					:					List[str]        =	self.dummy_unet_condition
           lowerCamelCase					:					Union[str, Any]        =	AudioDiffusionPipeline(
               vqvae=self.dummy_vqvae_and_unet[0]     ,		unet=__a     ,		mel=__a     ,		scheduler=__a	)
           lowerCamelCase					:					Union[str, Any]        =	pipe.to(__a	)
           pipe.set_progress_bar_config(disable=__a	)

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


     """simple docstring"""

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







     def 		a__						(	self:						Optional[int]	)->    Optional[Any]:
           lowerCamelCase					:					Tuple        =	torch_device

           lowerCamelCase					:					List[str]        =	DiffusionPipeline.from_pretrained("""teticio/audio-diffusion-ddim-256"""	)
           lowerCamelCase					:					List[Any]        =	pipe.to(__a	)
           pipe.set_progress_bar_config(disable=__a	)

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

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








"""simple docstring"""


import unittest

from knapsack import greedy_knapsack as kp


class 			A__	(						unittest.TestCase):


     """simple docstring"""

     def 		a__						(	self:						Optional[int]	)->    Union[str, Any]:
           lowerCamelCase					:					Tuple        =	[10, 20, 30, 40, 50, 60]
           lowerCamelCase					:					Union[str, Any]        =	[2, 4, 6, 8, 10, 12]
           lowerCamelCase					:					Union[str, Any]        =	100
           self.assertEqual(kp.calc_profit(__a     ,		__a     ,		__a	)     ,		210	)

     def 		a__						(	self:						str	)->    str:
           self.assertRaisesRegex(__a     ,		"""max_weight must greater than zero."""	)

     def 		a__						(	self:						str	)->    List[Any]:
           self.assertRaisesRegex(__a     ,		"""Weight can not be negative."""	)

     def 		a__						(	self:						Any	)->    Dict:
           self.assertRaisesRegex(__a     ,		"""Profit can not be negative."""	)

     def 		a__						(	self:						Optional[Any]	)->    List[Any]:
           self.assertRaisesRegex(__a     ,		"""max_weight must greater than zero."""	)







     def 		a__						(	self:						Optional[Any]	)->    Tuple:
           self.assertRaisesRegex(
               __a     ,		"""The length of profit and weight must be same."""	)


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




