

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





			def 		__magic_name__    (		self					)   -> Any:




						'''simple docstring'''
						# clean up the VRAM after each test
						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()





			@property
			def 		__magic_name__    (		self					)   -> Optional[Any]:




						'''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=(128, 128)					,  down_block_types=('AttnDownBlock2D', 'DownBlock2D')					,  up_block_types=('UpBlock2D', 'AttnUpBlock2D')					,  )
						return model





			@property
			def 		__magic_name__    (		self					)   -> int:




						'''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=(128, 128)					,  down_block_types=('CrossAttnDownBlock2D', 'DownBlock2D')					,  up_block_types=('UpBlock2D', 'CrossAttnUpBlock2D')					,  cross_attention_dim=10					,  )
						return model





			@property
			def 		__magic_name__    (		self					)   -> Union[str, Any]:




						'''simple docstring'''
						torch.manual_seed(0					)
						__a        =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')					,  )
						__a        =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 		__magic_name__    (		self					)   -> str:




						'''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=__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, 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

						__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([120, 117, 110, 109, 138, 167, 138, 148, 132, 121]					)

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





			def 		__magic_name__    (		self					)   -> int:




						'''simple docstring'''
						# clean up the VRAM after each test
						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()





			def 		__magic_name__    (		self					)   -> Dict:




						'''simple docstring'''
						__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([151, 167, 154, 144, 122, 134, 121, 105, 70, 26]					)

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









from manim import *






class 	__magic_name__    (			lowerCAmelCase_      ):





			def 		__magic_name__    (		self					)   -> Any:




						'''simple docstring'''
						__a        =Rectangle(height=0.5					,  width=0.5					)
						__a        =Rectangle(height=0.25					,  width=0.25					)
						__a        =Rectangle(height=0.46					,  width=0.46					).set_stroke(width=0					)

						__a        =[mem.copy() for i in range(6					)]
						__a        =[mem.copy() for i in range(6					)]
						__a        =VGroup(*__snake_case					).arrange(__snake_case					,  buff=0					)
						__a        =VGroup(*__snake_case					).arrange(__snake_case					,  buff=0					)
						__a        =VGroup(__snake_case					,  __snake_case					).arrange(__snake_case					,  buff=0					)
						__a        =Text('CPU'					,  font_size=24					)
						__a        =Group(__snake_case					,  __snake_case					).arrange(__snake_case					,  buff=0.5					,  aligned_edge=__snake_case					)
						cpu.move_to([-2.5, -0.5, 0]					)
						self.add(__snake_case					)

						__a        =[mem.copy() for i in range(4					)]
						__a        =VGroup(*__snake_case					).arrange(__snake_case					,  buff=0					)
						__a        =Text('GPU'					,  font_size=24					)
						__a        =Group(__snake_case					,  __snake_case					).arrange(__snake_case					,  buff=0.5					,  aligned_edge=__snake_case					)
						gpu.move_to([-1, -1, 0]					)
						self.add(__snake_case					)

						__a        =[mem.copy() for i in range(6					)]
						__a        =VGroup(*__snake_case					).arrange(__snake_case					,  buff=0					)

						__a        =Text('Model'					,  font_size=24					)
						__a        =Group(__snake_case					,  __snake_case					).arrange(__snake_case					,  buff=0.5					,  aligned_edge=__snake_case					)
						model.move_to([3, -1.0, 0]					)
						self.add(__snake_case					)

						__a        =[]
						__a        =[]
						__a        =[]

						for i, rect in enumerate(__snake_case					):
									rect.set_stroke(__snake_case					)

									__a        =Rectangle(height=0.46 / 4					,  width=0.46 / 3					).set_stroke(width=0.0					).set_fill(__snake_case					,  opacity=0.7					)

									if i == 0:
												cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT					)					,  buff=0.02					,  direction=__snake_case					)
												cpu_target.set_x(cpu_target.get_x() + 0.1					)
									elif i == 3:
												cpu_target.next_to(model_cpu_arr[0]					,  direction=__snake_case					,  buff=0.0					)
									else:
												cpu_target.next_to(model_cpu_arr[i - 1]					,  direction=__snake_case					,  buff=0.0					)
									self.add(__snake_case					)
									model_cpu_arr.append(__snake_case					)

						self.add(*__snake_case					,  *__snake_case					,  *__snake_case					)

						__a        =[mem.copy() for i in range(6					)]
						__a        =VGroup(*__snake_case					).arrange(__snake_case					,  buff=0					)

						__a        =Text('Loaded Checkpoint'					,  font_size=24					)
						__a        =Group(__snake_case					,  __snake_case					).arrange(__snake_case					,  buff=0.5					,  aligned_edge=__snake_case					)
						checkpoint.move_to([3, 0.5, 0]					)

						self.add(__snake_case					)

						__a        =[]
						__a        =[]

						for i, rect in enumerate(__snake_case					):
									__a        =fill.copy().set_fill(__snake_case					,  opacity=0.7					)
									target.move_to(__snake_case					)
									ckpt_arr.append(__snake_case					)

									__a        =target.copy()
									if i < 5:
												cpu_target.move_to(cpu_left_col_base[i + 1]					)
									else:
												cpu_target.move_to(cpu_right_col_base[i - 5]					)
									ckpt_cpu_arr.append(__snake_case					)
						self.add(*__snake_case					,  *__snake_case					)

						__a        =Square(side_length=2.2					)
						key.move_to([-5, 2, 0]					)

						__a        =MarkupText(
						    f'<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'					,  font_size=18					,  )

						key_text.move_to([-5, 2.4, 0]					)

						self.add(__snake_case					,  __snake_case					)

						__a        =MarkupText(
						    f'<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'					,  font_size=18					,  )

						blue_text.next_to(__snake_case					,  DOWN * 2.4					,  aligned_edge=key_text.get_left()					)
						self.add(__snake_case					)

						__a        =MarkupText(
						    f'Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.'					,  font_size=24					,  )
						step_a.move_to([2, 2, 0]					)

						__a        =[meta_mem.copy() for i in range(6					)]
						__a        =[meta_mem.copy() for i in range(6					)]
						__a        =VGroup(*__snake_case					).arrange(__snake_case					,  buff=0					)
						__a        =VGroup(*__snake_case					).arrange(__snake_case					,  buff=0					)
						__a        =VGroup(__snake_case					,  __snake_case					).arrange(__snake_case					,  buff=0					)
						__a        =Text('Disk'					,  font_size=24					)
						__a        =Group(__snake_case					,  __snake_case					).arrange(__snake_case					,  buff=0.5					,  aligned_edge=__snake_case					)
						disk.move_to([-4.0, -1.25, 0]					)
						self.play(Write(__snake_case					,  run_time=3					)					,  Write(__snake_case					,  run_time=1					)					,  Create(__snake_case					,  run_time=1					)					)

						__a        =[]
						for i, rect in enumerate(__snake_case					):
									__a        =rect.copy()
									target.generate_target()
									target.target.move_to(disk_left_col_base[i]					).scale(0.5					)
									animations.append(MoveToTarget(__snake_case					,  run_time=1.5					)					)
						self.play(*__snake_case					)

						self.play(FadeOut(__snake_case					)					)

						__a        =MarkupText(f'Then, the checkpoint is removed from memory\nthrough garbage collection.'					,  font_size=24					)
						step_a.move_to([2, 2, 0]					)

						self.play(Write(__snake_case					,  run_time=3					)					)

						self.play(
						    FadeOut(__snake_case					,  __snake_case					,  *__snake_case					,  *__snake_case					)					,  )

						self.wait()






