

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

    def UpperCamelCase      (      self			:   str     ):

     """simple docstring"""






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

    @property
    def UpperCamelCase      (      self			:   Union[str, Any]     ):

     """simple docstring"""






     torch.manual_seed(0     )
     SCREAMING_SNAKE_CASE		    =      UNetaDModel(
         sample_size=(3_2, 6_4)				,     in_channels=1				,     out_channels=1				,     layers_per_block=2				,     block_out_channels=(1_2_8, 1_2_8)				,     down_block_types=('AttnDownBlock2D', 'DownBlock2D')				,     up_block_types=('UpBlock2D', 'AttnUpBlock2D')				,     )
     return model

    @property
    def UpperCamelCase      (      self			:   Union[str, Any]     ):

     """simple docstring"""






     torch.manual_seed(0     )
     SCREAMING_SNAKE_CASE		    =      UNetaDConditionModel(
         sample_size=(6_4, 3_2)				,     in_channels=1				,     out_channels=1				,     layers_per_block=2				,     block_out_channels=(1_2_8, 1_2_8)				,     down_block_types=('CrossAttnDownBlock2D', 'DownBlock2D')				,     up_block_types=('UpBlock2D', 'CrossAttnUpBlock2D')				,     cross_attention_dim=1_0				,     )
     return model

    @property
    def UpperCamelCase      (      self			:   Any     ):

     """simple docstring"""






     torch.manual_seed(0     )
     SCREAMING_SNAKE_CASE		    =      AutoencoderKL(
         sample_size=(1_2_8, 6_4)				,     in_channels=1				,     out_channels=1				,     latent_channels=1				,     layers_per_block=2				,     block_out_channels=(1_2_8, 1_2_8)				,     down_block_types=('DownEncoderBlock2D', 'DownEncoderBlock2D')				,     up_block_types=('UpDecoderBlock2D', 'UpDecoderBlock2D')				,     )
     SCREAMING_SNAKE_CASE		    =      UNetaDModel(
         sample_size=(6_4, 3_2)				,     in_channels=1				,     out_channels=1				,     layers_per_block=2				,     block_out_channels=(1_2_8, 1_2_8)				,     down_block_types=('AttnDownBlock2D', 'DownBlock2D')				,     up_block_types=('UpBlock2D', 'AttnUpBlock2D')				,     )
     return vqvae, unet




    @slow
    def UpperCamelCase      (      self			:   Any     ):

     """simple docstring"""






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

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

     SCREAMING_SNAKE_CASE		    =      torch.Generator(device=_lowercase     ).manual_seed(4_2     )
     SCREAMING_SNAKE_CASE		    =      pipe(generator=_lowercase				,     steps=4     )
     SCREAMING_SNAKE_CASE		    =      output.audios[0]
     SCREAMING_SNAKE_CASE		    =      output.images[0]

     SCREAMING_SNAKE_CASE		    =      torch.Generator(device=_lowercase     ).manual_seed(4_2     )
     SCREAMING_SNAKE_CASE		    =      pipe(generator=_lowercase				,     steps=4				,     return_dict=_lowercase     )
     SCREAMING_SNAKE_CASE		    =      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]
     )
     SCREAMING_SNAKE_CASE		    =      np.frombuffer(image.tobytes()				,     dtype='uint8'     )[:1_0]
     SCREAMING_SNAKE_CASE		    =      np.frombuffer(image_from_tuple.tobytes()				,     dtype='uint8'     )[:1_0]
     SCREAMING_SNAKE_CASE		    =      np.array([6_9, 2_5_5, 2_5_5, 2_5_5, 0, 0, 7_7, 1_8_1, 1_2, 1_2_7]     )

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

     SCREAMING_SNAKE_CASE		    =      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]				,     )

     SCREAMING_SNAKE_CASE		    =      DDIMScheduler()
     SCREAMING_SNAKE_CASE		    =      self.dummy_vqvae_and_unet
     SCREAMING_SNAKE_CASE		    =      AudioDiffusionPipeline(
         vqvae=self.dummy_vqvae_and_unet[0]				,     unet=dummy_vqvae_and_unet[1]				,     mel=_lowercase				,     scheduler=_lowercase     )
     SCREAMING_SNAKE_CASE		    =      pipe.to(_lowercase     )
     pipe.set_progress_bar_config(disable=_lowercase     )

     np.random.seed(0     )
     SCREAMING_SNAKE_CASE		    =      np.random.uniform(-1				,     1				,     ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,)     )
     SCREAMING_SNAKE_CASE		    =      torch.Generator(device=_lowercase     ).manual_seed(4_2     )
     SCREAMING_SNAKE_CASE		    =      pipe(raw_audio=_lowercase				,     generator=_lowercase				,     start_step=5				,     steps=1_0     )
     SCREAMING_SNAKE_CASE		    =      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]
     )
     SCREAMING_SNAKE_CASE		    =      np.frombuffer(image.tobytes()				,     dtype='uint8'     )[:1_0]
     SCREAMING_SNAKE_CASE		    =      np.array([1_2_0, 1_1_7, 1_1_0, 1_0_9, 1_3_8, 1_6_7, 1_3_8, 1_4_8, 1_3_2, 1_2_1]     )

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

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

     np.random.seed(0     )
     SCREAMING_SNAKE_CASE		    =      torch.rand((1, 1, 1_0)     )
     SCREAMING_SNAKE_CASE		    =      pipe(generator=_lowercase				,     encoding=_lowercase     )
     SCREAMING_SNAKE_CASE		    =      output.images[0]
     SCREAMING_SNAKE_CASE		    =      np.frombuffer(image.tobytes()				,     dtype='uint8'     )[:1_0]
     SCREAMING_SNAKE_CASE		    =      np.array([1_0_7, 1_0_3, 1_2_0, 1_2_7, 1_4_2, 1_2_2, 1_1_3, 1_2_2, 9_7, 1_1_1]     )

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




@slow
@require_torch_gpu
class   UpperCamelCase			(  unittest.TestCase ):

    def UpperCamelCase      (      self			:   Tuple     ):

     """simple docstring"""






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




    def UpperCamelCase      (      self			:   int     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      torch_device

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

     SCREAMING_SNAKE_CASE		    =      torch.Generator(device=_lowercase     ).manual_seed(4_2     )
     SCREAMING_SNAKE_CASE		    =      pipe(generator=_lowercase     )
     SCREAMING_SNAKE_CASE		    =      output.audios[0]
     SCREAMING_SNAKE_CASE		    =      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]
     SCREAMING_SNAKE_CASE		    =      np.frombuffer(image.tobytes()				,     dtype='uint8'     )[:1_0]
     SCREAMING_SNAKE_CASE		    =      np.array([1_5_1, 1_6_7, 1_5_4, 1_4_4, 1_2_2, 1_3_4, 1_2_1, 1_0_5, 7_0, 2_6]     )

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




from __future__ import annotations


def 					_UpperCAmelCase				(						a__  ,	a__):

			'''simple docstring'''



			a_						:						List[str] 							= 0
			a_						:						str 							= len(a__) - 1

			while i < j:
						if nums[i] + nums[j] == target:
									return [i, j]
						elif nums[i] + nums[j] < target:
									a_						:						List[Any] 							= i + 1
						else:
									a_						:						Optional[Any] 							= j - 1

			return []


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	print(F"""{two_pointer([2, 7, 11, 15], 9) = }""")


