





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   A      (unittest.TestCase   ):
			'''simple docstring'''




			def        a_	(      self       :   Any       )     ->     Optional[int]:







					"""simple docstring"""






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






			@property
			def        a_	(      self       :   Any       )     ->     int:







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






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







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






			@property
			def        a_	(      self       :   Any       )     ->     Tuple:







					"""simple docstring"""






					torch.manual_seed(0       )
					A__  				=       AutoencoderKL(
					    sample_size=(1_28, 64)							,     in_channels=1							,     out_channels=1							,     latent_channels=1							,     layers_per_block=2							,     block_out_channels=(1_28, 1_28)							,     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=(1_28, 1_28)							,     down_block_types=("""AttnDownBlock2D""", """DownBlock2D""")							,     up_block_types=("""UpBlock2D""", """AttnUpBlock2D""")							,     )
					return vqvae, unet







			@slow
			def        a_	(      self       :   Optional[int]       )     ->     int:







					"""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=__lowerCAmelCase							,     unet=self.dummy_unet							,     mel=__lowerCAmelCase							,     scheduler=__lowerCAmelCase       )
					A__  				=       pipe.to(__lowerCAmelCase       )
					pipe.set_progress_bar_config(disable=__lowerCAmelCase       )

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

					A__  				=       torch.Generator(device=__lowerCAmelCase       ).manual_seed(42       )
					A__  				=       pipe(generator=__lowerCAmelCase							,     steps=4							,     return_dict=__lowerCAmelCase       )
					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, 2_55, 2_55, 2_55, 0, 0, 77, 1_81, 12, 1_27]       )

					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=__lowerCAmelCase							,     scheduler=__lowerCAmelCase       )
					A__  				=       pipe.to(__lowerCAmelCase       )
					pipe.set_progress_bar_config(disable=__lowerCAmelCase       )

					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=__lowerCAmelCase       ).manual_seed(42       )
					A__  				=       pipe(raw_audio=__lowerCAmelCase							,     generator=__lowerCAmelCase							,     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([1_20, 1_17, 1_10, 1_09, 1_38, 1_67, 1_38, 1_48, 1_32, 1_21]       )

					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=__lowerCAmelCase							,     mel=__lowerCAmelCase							,     scheduler=__lowerCAmelCase       )
					A__  				=       pipe.to(__lowerCAmelCase       )
					pipe.set_progress_bar_config(disable=__lowerCAmelCase       )

					np.random.seed(0       )
					A__  				=       torch.rand((1, 1, 10)       )
					A__  				=       pipe(generator=__lowerCAmelCase							,     encoding=__lowerCAmelCase       )
					A__  				=       output.images[0]
					A__  				=       np.frombuffer(image.tobytes()							,     dtype="""uint8"""       )[:10]
					A__  				=       np.array([1_07, 1_03, 1_20, 1_27, 1_42, 1_22, 1_13, 1_22, 97, 1_11]       )

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


@slow
@require_torch_gpu
class   A      (unittest.TestCase   ):
			'''simple docstring'''




			def        a_	(      self       :   int       )     ->     Any:







					"""simple docstring"""






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







			def        a_	(      self       :   List[str]       )     ->     Optional[int]:







					"""simple docstring"""






					A__  				=       torch_device

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

					A__  				=       torch.Generator(device=__lowerCAmelCase       ).manual_seed(42       )
					A__  				=       pipe(generator=__lowerCAmelCase       )
					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([1_51, 1_67, 1_54, 1_44, 1_22, 1_34, 1_21, 1_05, 70, 26]       )

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










from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


A   : Tuple			=				logging.get_logger(__name__)

A   : Optional[int]			=				{
    '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/config.json''',
    '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/config.json''',
    '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/config.json''',
    '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/config.json''',
    '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json''',
    '''roberta-large-openai-detector''': '''https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json''',
}


class   A      (SCREAMING_SNAKE_CASE   ):
			'''simple docstring'''




			__lowerCamelCase     :	Union[str, Any]          =   '''roberta'''






			def __init__(      self       :   Any							,     __lowerCAmelCase       :   Tuple=5_02_65							,     __lowerCAmelCase       :   Optional[int]=7_68							,     __lowerCAmelCase       :   Union[str, Any]=12							,     __lowerCAmelCase       :   Dict=12							,     __lowerCAmelCase       :   Optional[Any]=30_72							,     __lowerCAmelCase       :   Any="gelu"							,     __lowerCAmelCase       :   Optional[Any]=0.1							,     __lowerCAmelCase       :   Optional[int]=0.1							,     __lowerCAmelCase       :   Optional[Any]=5_12							,     __lowerCAmelCase       :   int=2							,     __lowerCAmelCase       :   List[str]=0.0_2							,     __lowerCAmelCase       :   Dict=1e-12							,     __lowerCAmelCase       :   Optional[int]=1							,     __lowerCAmelCase       :   Tuple=0							,     __lowerCAmelCase       :   int=2							,     __lowerCAmelCase       :   Dict="absolute"							,     __lowerCAmelCase       :   Optional[Any]=True							,     __lowerCAmelCase       :   Union[str, Any]=None							,     **__lowerCAmelCase       :   str							,     )     ->     str:







					"""simple docstring"""






					super().__init__(pad_token_id=__lowerCAmelCase							,     bos_token_id=__lowerCAmelCase							,     eos_token_id=__lowerCAmelCase							,     **__lowerCAmelCase       )

					A__  				=       vocab_size
					A__  				=       hidden_size
					A__  				=       num_hidden_layers
					A__  				=       num_attention_heads
					A__  				=       hidden_act
					A__  				=       intermediate_size
					A__  				=       hidden_dropout_prob
					A__  				=       attention_probs_dropout_prob
					A__  				=       max_position_embeddings
					A__  				=       type_vocab_size
					A__  				=       initializer_range
					A__  				=       layer_norm_eps
					A__  				=       position_embedding_type
					A__  				=       use_cache
					A__  				=       classifier_dropout


class   A      (SCREAMING_SNAKE_CASE   ):
			'''simple docstring'''




			@property
			def        a_	(      self       :   int       )     ->     Mapping[str, Mapping[int, str]]:







					"""simple docstring"""






					if self.task == "multiple-choice":
							A__  				=       {0: """batch""", 1: """choice""", 2: """sequence"""}
					else:
							A__  				=       {0: """batch""", 1: """sequence"""}
					return OrderedDict(
					    [
					        ("""input_ids""", dynamic_axis),
					        ("""attention_mask""", dynamic_axis),
					    ]       )



