

"""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       lowerCamelCase	(       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	)
        UpperCAmelCase__			:							Optional[int]							=    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	)
        UpperCAmelCase__			:							Optional[Any]							=    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	)
        UpperCAmelCase__			:							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""")							,       )
        UpperCAmelCase__			:							List[str]							=    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"""

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

        UpperCAmelCase__			:							int							=    DDPMScheduler()
        UpperCAmelCase__			:							Tuple							=    AudioDiffusionPipeline(vqvae=_lowerCamelCase							,       unet=self.dummy_unet							,       mel=_lowerCamelCase							,       scheduler=_lowerCamelCase	)
        UpperCAmelCase__			:							Optional[Any]							=    pipe.to(_lowerCamelCase	)
        pipe.set_progress_bar_config(disable=_lowerCamelCase	)

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

        UpperCAmelCase__			:							List[Any]							=    torch.Generator(device=_lowerCamelCase	).manual_seed(42	)
        UpperCAmelCase__			:							Dict							=    pipe(generator=_lowerCamelCase							,       steps=4							,       return_dict=_lowerCamelCase	)
        UpperCAmelCase__			:							Union[str, 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]
        )
        UpperCAmelCase__			:							int							=    np.frombuffer(image.tobytes()							,       dtype="""uint8"""	)[:10]
        UpperCAmelCase__			:							Tuple							=    np.frombuffer(image_from_tuple.tobytes()							,       dtype="""uint8"""	)[:10]
        UpperCAmelCase__			:							Optional[int]							=    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

        UpperCAmelCase__			:							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]							,       )

        UpperCAmelCase__			:							int							=    DDIMScheduler()
        UpperCAmelCase__			:							List[Any]							=    self.dummy_vqvae_and_unet
        UpperCAmelCase__			:							Dict							=    AudioDiffusionPipeline(
            vqvae=self.dummy_vqvae_and_unet[0]							,       unet=dummy_vqvae_and_unet[1]							,       mel=_lowerCamelCase							,       scheduler=_lowerCamelCase	)
        UpperCAmelCase__			:							Optional[int]							=    pipe.to(_lowerCamelCase	)
        pipe.set_progress_bar_config(disable=_lowerCamelCase	)

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

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

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

        np.random.seed(0	)
        UpperCAmelCase__			:							Optional[int]							=    torch.rand((1, 1, 10)	)
        UpperCAmelCase__			:							Optional[Any]							=    pipe(generator=_lowerCamelCase							,       encoding=_lowerCamelCase	)
        UpperCAmelCase__			:							Tuple							=    output.images[0]
        UpperCAmelCase__			:							str							=    np.frombuffer(image.tobytes()							,       dtype="""uint8"""	)[:10]
        UpperCAmelCase__			:							Any							=    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       lowerCamelCase	(       unittest.TestCase   ):


  '''simple docstring'''







  def  _a      (self	):


        """simple docstring"""

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




  def  _a      (self	):


        """simple docstring"""

        UpperCAmelCase__			:							Dict							=    torch_device

        UpperCAmelCase__			:							List[str]							=    DiffusionPipeline.from_pretrained("""teticio/audio-diffusion-ddim-256"""	)
        UpperCAmelCase__			:							int							=    pipe.to(_lowerCamelCase	)
        pipe.set_progress_bar_config(disable=_lowerCamelCase	)

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

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




"""simple docstring"""
from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels
from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features
from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor
from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels

