
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 				_lowercase				(					self     :					Any       )     ->      List[str]:







    """simple docstring"""






    super().tearDown()
    gc.collect()
    torch.cuda.empty_cache()
  @property
  def 				_lowercase				(					self     :					List[str]       )     ->      Optional[Any]:







    """simple docstring"""






    torch.manual_seed(0       )
    __magic_name__								=			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 				_lowercase				(					self     :					Any       )     ->      Any:







    """simple docstring"""






    torch.manual_seed(0       )
    __magic_name__								=			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 				_lowercase				(					self     :					Dict       )     ->      Any:







    """simple docstring"""






    torch.manual_seed(0       )
    __magic_name__								=			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""")     ,				)
    __magic_name__								=			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 				_lowercase				(					self     :					Optional[Any]       )     ->      Union[str, Any]:







    """simple docstring"""






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

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

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

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

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

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

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

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

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

    np.random.seed(0       )
    __magic_name__								=			torch.rand((1, 1, 10)       )
    __magic_name__								=			pipe(generator=UpperCamelCase__     ,				encoding=UpperCamelCase__       )
    __magic_name__								=			output.images[0]
    __magic_name__								=			np.frombuffer(image.tobytes()     ,				dtype="""uint8"""       )[:10]
    __magic_name__								=			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 				_lowercase				(					self     :					int       )     ->      List[str]:







    """simple docstring"""






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





  def 				_lowercase				(					self     :					int       )     ->      str:







    """simple docstring"""






    __magic_name__								=			torch_device

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

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

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



import math

def a__       (			A_   ):






  '''simple docstring'''





  if 1 < number < 4:
    # 2 and 3 are primes
    return True
  elif number < 2 or number % 2 == 0 or number % 3 == 0:
    # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
    return False

  # All primes number are in format of 6k +/- 1
  for i in range(5,	int(math.sqrt(A_   ) + 1   ),	6   ):
    if number % i == 0 or number % (i + 2) == 0:
      return False
  return True

def a__       (			A_ = 0.1   ):






  '''simple docstring'''





  __magic_name__								=			3
  __magic_name__								=			3

  while primes / (2 * j - 1) >= ratio:
    for i in range(j * j + j + 1,	(j + 2) * (j + 2),	j + 1   ):
      primes += is_prime(A_   )
    j += 2
  return j


if __name__ == "__main__":
    import doctest

    doctest.testmod()

