





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       :   Any       )     ->     Optional[int]:







					"""simple docstring"""






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






			@property
			def        a_	(      self       :   Any       )     ->     int:







					"""simple docstring"""






					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        a_	(      self       :   Union[str, Any]       )     ->     Dict:







					"""simple docstring"""






					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        a_	(      self       :   Any       )     ->     Tuple:







					"""simple docstring"""






					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        a_	(      self       :   Optional[int]       )     ->     int:







					"""simple docstring"""






					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=__lowerCAmelCase							,     unet=self.dummy_unet							,     mel=__lowerCAmelCase							,     scheduler=__lowerCAmelCase       )
					A__  				=       pipe.to(__lowerCAmelCase       )
					pipe.set_progress_bar_config(disable=__lowerCAmelCase       )

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

					A__  				=       torch.Generator(device=__lowerCAmelCase       ).manual_seed(42       )
					A__  				=       pipe(generator=__lowerCAmelCase							,     steps=4							,     return_dict=__lowerCAmelCase       )
					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=__lowerCAmelCase							,     scheduler=__lowerCAmelCase       )
					A__  				=       pipe.to(__lowerCAmelCase       )
					pipe.set_progress_bar_config(disable=__lowerCAmelCase       )

					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=__lowerCAmelCase       ).manual_seed(42       )
					A__  				=       pipe(raw_audio=__lowerCAmelCase							,     generator=__lowerCAmelCase							,     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=__lowerCAmelCase							,     mel=__lowerCAmelCase							,     scheduler=__lowerCAmelCase       )
					A__  				=       pipe.to(__lowerCAmelCase       )
					pipe.set_progress_bar_config(disable=__lowerCAmelCase       )

					np.random.seed(0       )
					A__  				=       torch.rand((1, 1, 10)       )
					A__  				=       pipe(generator=__lowerCAmelCase							,     encoding=__lowerCAmelCase       )
					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   A      (unittest.TestCase   ):
			'''simple docstring'''




			def        a_	(      self       :   int       )     ->     Any:







					"""simple docstring"""






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







			def        a_	(      self       :   List[str]       )     ->     Optional[int]:







					"""simple docstring"""






					A__  				=       torch_device

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

					A__  				=       torch.Generator(device=__lowerCAmelCase       ).manual_seed(42       )
					A__  				=       pipe(generator=__lowerCAmelCase       )
					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 math







def        __lowerCamelCase (							__a       :int   )		->    bool:






		"""simple docstring"""

		A__  				=       math.loga(math.sqrt(4 * positive_integer + 1   ) / 2 + 1 / 2   )

		return exponent == int(__a   )







def        __lowerCamelCase (							__a       :float = 1 / 1_2_3_4_5   )		->    int:






		"""simple docstring"""

		A__  				=       0
		A__  				=       0

		A__  				=       3
		while True:
				A__  				=       (integer**2 - 1) / 4
				# if candidate is an integer, then there is a partition for k
				if partition_candidate == int(__a   ):
						A__  				=       int(__a   )
						total_partitions += 1
						if check_partition_perfect(__a   ):
								perfect_partitions += 1
				if perfect_partitions > 0:
						if perfect_partitions / total_partitions < max_proportion:
								return int(__a   )
				integer += 1


if __name__ == "__main__":
					print(F'''{solution() = }''')



