


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



     """simple docstring"""






     def a	( self   ):

            '''simple docstring'''

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







     @property
     def a	( self   ):

            '''simple docstring'''

            torch.manual_seed(0   )
            _lowerCAmelCase    :							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 a	( self   ):

            '''simple docstring'''

            torch.manual_seed(0   )
            _lowerCAmelCase    :							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 a	( self   ):

            '''simple docstring'''

            torch.manual_seed(0   )
            _lowerCAmelCase    :							int   			=       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    :							int   			=       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   ):

            '''simple docstring'''

            _lowerCAmelCase    :							Optional[int]   			=       'cpu'  # ensure determinism for the device-dependent torch.Generator
            _lowerCAmelCase    :							int   			=       Mel(
                x_res=self.dummy_unet.config.sample_size[1]      ,	y_res=self.dummy_unet.config.sample_size[0]      ,	)

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

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

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

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

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

            _lowerCAmelCase    :							int   			=       self.dummy_unet_condition
            _lowerCAmelCase    :							List[Any]   			=       AudioDiffusionPipeline(
                vqvae=self.dummy_vqvae_and_unet[0]      ,	unet=snake_case__      ,	mel=snake_case__      ,	scheduler=snake_case__   )
            _lowerCAmelCase    :							Optional[Any]   			=       pipe.to(snake_case__   )
            pipe.set_progress_bar_config(disable=snake_case__   )

            np.random.seed(0   )
            _lowerCAmelCase    :							List[Any]   			=       torch.rand((1, 1, 10)   )
            _lowerCAmelCase    :							Tuple   			=       pipe(generator=snake_case__      ,	encoding=snake_case__   )
            _lowerCAmelCase    :							Optional[Any]   			=       output.images[0]
            _lowerCAmelCase    :							Dict   			=       np.frombuffer(image.tobytes()      ,	dtype='uint8'   )[:10]
            _lowerCAmelCase    :							Optional[int]   			=       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 	UpperCamelCase__					( unittest.TestCase	):



     """simple docstring"""






     def a	( self   ):

            '''simple docstring'''

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







     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							List[str]   			=       torch_device

            _lowerCAmelCase    :							Optional[Any]   			=       DiffusionPipeline.from_pretrained('teticio/audio-diffusion-ddim-256'   )
            _lowerCAmelCase    :							str   			=       pipe.to(snake_case__   )
            pipe.set_progress_bar_config(disable=snake_case__   )

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

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





'''simple docstring'''







lowerCAmelCase    :     Optional[int]					=						[sum(int(c, 10) ** 2 for c in i.__str__()) for i in range(10_00_00)]






def 			lowercase				(_A					):







       """simple docstring"""







       _lowerCAmelCase    :							str   			=       0
       while number:
              # Increased Speed Slightly by checking every 5 digits together.
              sum_of_digits_squared += DIGITS_SQUARED[number % 1_0_0_0_0_0]
              number //= 1_0_0_0_0_0

       return sum_of_digits_squared


# There are 2 Chains made,
# One ends with 89 with the chain member 58 being the one which when declared first,
# there will be the least number of iterations for all the members to be checked.

# The other one ends with 1 and has only one element 1.

# So 58 and 1 are chosen to be declared at the starting.

# Changed dictionary to an array to quicken the solution
lowerCAmelCase    :     list[bool | None]					=						[None] * 10_00_00_00
lowerCAmelCase    :     List[str]					=						True
lowerCAmelCase    :     Union[str, Any]					=						False






def 			lowercase				(_A					):







       """simple docstring"""







       if CHAINS[number - 1] is not None:
              return CHAINS[number - 1]  # type: ignore

       _lowerCAmelCase    :							Any   			=       chain(next_number(_A					)					)
       _lowerCAmelCase    :							List[str]   			=       number_chain

       while number < 1_0_0_0_0_0_0_0:
              _lowerCAmelCase    :							Tuple   			=       number_chain
              number *= 1_0

       return number_chain






def 			lowercase				(_A = 1_0_0_0_0_0_0_0					):







       """simple docstring"""







       for i in range(1   ,						_A					):
              if CHAINS[i] is None:
                     chain(i + 1					)

       return CHAINS[:number].count(_A					)


if __name__ == "__main__":
       import doctest

       doctest.testmod()
       print(F'''{solution() = }''')

