






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							):
         # clean up the VRAM after each test
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()





       @property
       def       _a   ( self							):
         torch.manual_seed(0							)
         UpperCamelCase_: int   				=    UNetaDModel(
             sample_size=(3_2, 6_4)  ,      in_channels=1  ,      out_channels=1  ,      layers_per_block=2  ,      block_out_channels=(1_2_8, 1_2_8)  ,      down_block_types=('AttnDownBlock2D', 'DownBlock2D')  ,      up_block_types=('UpBlock2D', 'AttnUpBlock2D')  ,      )
         return model





       @property
       def       _a   ( self							):
         torch.manual_seed(0							)
         UpperCamelCase_: List[Any]   				=    UNetaDConditionModel(
             sample_size=(6_4, 3_2)  ,      in_channels=1  ,      out_channels=1  ,      layers_per_block=2  ,      block_out_channels=(1_2_8, 1_2_8)  ,      down_block_types=('CrossAttnDownBlock2D', 'DownBlock2D')  ,      up_block_types=('UpBlock2D', 'CrossAttnUpBlock2D')  ,      cross_attention_dim=1_0  ,      )
         return model





       @property
       def       _a   ( self							):
         torch.manual_seed(0							)
         UpperCamelCase_: Tuple   				=    AutoencoderKL(
             sample_size=(1_2_8, 6_4)  ,      in_channels=1  ,      out_channels=1  ,      latent_channels=1  ,      layers_per_block=2  ,      block_out_channels=(1_2_8, 1_2_8)  ,      down_block_types=('DownEncoderBlock2D', 'DownEncoderBlock2D')  ,      up_block_types=('UpDecoderBlock2D', 'UpDecoderBlock2D')  ,      )
         UpperCamelCase_: Any   				=    UNetaDModel(
             sample_size=(6_4, 3_2)  ,      in_channels=1  ,      out_channels=1  ,      layers_per_block=2  ,      block_out_channels=(1_2_8, 1_2_8)  ,      down_block_types=('AttnDownBlock2D', 'DownBlock2D')  ,      up_block_types=('UpBlock2D', 'AttnUpBlock2D')  ,      )
         return vqvae, unet





       @slow
       def       _a   ( self							):
         UpperCamelCase_: List[Any]   				=    'cpu'  # ensure determinism for the device-dependent torch.Generator
         UpperCamelCase_: List[Any]   				=    Mel(
             x_res=self.dummy_unet.config.sample_size[1]  ,      y_res=self.dummy_unet.config.sample_size[0]  ,      )

         UpperCamelCase_: Any   				=    DDPMScheduler()
         UpperCamelCase_: List[str]   				=    AudioDiffusionPipeline(vqvae=_lowerCamelCase  ,      unet=self.dummy_unet  ,      mel=_lowerCamelCase  ,      scheduler=_lowerCamelCase							)
         UpperCamelCase_: Dict   				=    pipe.to(_lowerCamelCase							)
         pipe.set_progress_bar_config(disable=_lowerCamelCase							)

         UpperCamelCase_: int   				=    torch.Generator(device=_lowerCamelCase							).manual_seed(4_2							)
         UpperCamelCase_: int   				=    pipe(generator=_lowerCamelCase  ,      steps=4							)
         UpperCamelCase_: str   				=    output.audios[0]
         UpperCamelCase_: str   				=    output.images[0]

         UpperCamelCase_: Optional[int]   				=    torch.Generator(device=_lowerCamelCase							).manual_seed(4_2							)
         UpperCamelCase_: Tuple   				=    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_: str   				=    np.frombuffer(image.tobytes()  ,      dtype='uint8'							)[:1_0]
         UpperCamelCase_: Optional[int]   				=    np.frombuffer(image_from_tuple.tobytes()  ,      dtype='uint8'							)[:1_0]
         UpperCamelCase_: Optional[int]   				=    np.array([6_9, 2_5_5, 2_5_5, 2_5_5, 0, 0, 7_7, 1_8_1, 1_2, 1_2_7]							)

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

         UpperCamelCase_: int   				=    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_: Any   				=    DDIMScheduler()
         UpperCamelCase_: Optional[int]   				=    self.dummy_vqvae_and_unet
         UpperCamelCase_: List[str]   				=    AudioDiffusionPipeline(
             vqvae=self.dummy_vqvae_and_unet[0]  ,      unet=dummy_vqvae_and_unet[1]  ,      mel=_lowerCamelCase  ,      scheduler=_lowerCamelCase							)
         UpperCamelCase_: str   				=    pipe.to(_lowerCamelCase							)
         pipe.set_progress_bar_config(disable=_lowerCamelCase							)

         np.random.seed(0							)
         UpperCamelCase_: Any   				=    np.random.uniform(-1  ,      1  ,      ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,)							)
         UpperCamelCase_: Tuple   				=    torch.Generator(device=_lowerCamelCase							).manual_seed(4_2							)
         UpperCamelCase_: Dict   				=    pipe(raw_audio=_lowerCamelCase  ,      generator=_lowerCamelCase  ,      start_step=5  ,      steps=1_0							)
         UpperCamelCase_: 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]
         )
         UpperCamelCase_: int   				=    np.frombuffer(image.tobytes()  ,      dtype='uint8'							)[:1_0]
         UpperCamelCase_: Any   				=    np.array([1_2_0, 1_1_7, 1_1_0, 1_0_9, 1_3_8, 1_6_7, 1_3_8, 1_4_8, 1_3_2, 1_2_1]							)

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

         UpperCamelCase_: Tuple   				=    self.dummy_unet_condition
         UpperCamelCase_: Union[str, Any]   				=    AudioDiffusionPipeline(
             vqvae=self.dummy_vqvae_and_unet[0]  ,      unet=_lowerCamelCase  ,      mel=_lowerCamelCase  ,      scheduler=_lowerCamelCase							)
         UpperCamelCase_: Dict   				=    pipe.to(_lowerCamelCase							)
         pipe.set_progress_bar_config(disable=_lowerCamelCase							)

         np.random.seed(0							)
         UpperCamelCase_: Dict   				=    torch.rand((1, 1, 1_0)							)
         UpperCamelCase_: List[str]   				=    pipe(generator=_lowerCamelCase  ,      encoding=_lowerCamelCase							)
         UpperCamelCase_: List[str]   				=    output.images[0]
         UpperCamelCase_: Any   				=    np.frombuffer(image.tobytes()  ,      dtype='uint8'							)[:1_0]
         UpperCamelCase_: Tuple   				=    np.array([1_0_7, 1_0_3, 1_2_0, 1_2_7, 1_4_2, 1_2_2, 1_1_3, 1_2_2, 9_7, 1_1_1]							)

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







@slow
@require_torch_gpu
class     _lowerCAmelCase(	unittest.TestCase ):





       """simple docstring"""







       def       _a   ( self							):
         # clean up the VRAM after each test
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()





       def       _a   ( self							):
         UpperCamelCase_: Dict   				=    torch_device

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

         UpperCamelCase_: int   				=    torch.Generator(device=_lowerCamelCase							).manual_seed(4_2							)
         UpperCamelCase_: int   				=    pipe(generator=_lowerCamelCase							)
         UpperCamelCase_: Optional[Any]   				=    output.audios[0]
         UpperCamelCase_: Tuple   				=    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_: Any   				=    np.frombuffer(image.tobytes()  ,      dtype='uint8'							)[:1_0]
         UpperCamelCase_: Union[str, Any]   				=    np.array([1_5_1, 1_6_7, 1_5_4, 1_4_4, 1_2_2, 1_3_4, 1_2_1, 1_0_5, 7_0, 2_6]							)

         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_		:		List[Any]		     =			logging.get_logger(__name__)

A_		:		Optional[Any]		     =			{
    'YituTech/conv-bert-base': 'https://huggingface.co/YituTech/conv-bert-base/resolve/main/config.json',
    'YituTech/conv-bert-medium-small': (
        'https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/config.json'
    ),
    'YituTech/conv-bert-small': 'https://huggingface.co/YituTech/conv-bert-small/resolve/main/config.json',
    # See all ConvBERT models at https://huggingface.co/models?filter=convbert
}




class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       a    :		str									='''convbert'''





       def __init__( self  ,      _lowerCamelCase=3_0_5_2_2  ,      _lowerCamelCase=7_6_8  ,      _lowerCamelCase=1_2  ,      _lowerCamelCase=1_2  ,      _lowerCamelCase=3_0_7_2  ,      _lowerCamelCase="gelu"  ,      _lowerCamelCase=0.1  ,      _lowerCamelCase=0.1  ,      _lowerCamelCase=5_1_2  ,      _lowerCamelCase=2  ,      _lowerCamelCase=0.0_2  ,      _lowerCamelCase=1e-12  ,      _lowerCamelCase=1  ,      _lowerCamelCase=0  ,      _lowerCamelCase=2  ,      _lowerCamelCase=7_6_8  ,      _lowerCamelCase=2  ,      _lowerCamelCase=9  ,      _lowerCamelCase=1  ,      _lowerCamelCase=None  ,      **_lowerCamelCase  ,      ):
         super().__init__(
             pad_token_id=_lowerCamelCase  ,      bos_token_id=_lowerCamelCase  ,      eos_token_id=_lowerCamelCase  ,      **_lowerCamelCase  ,      )

         UpperCamelCase_: Dict   				=    vocab_size
         UpperCamelCase_: Tuple   				=    hidden_size
         UpperCamelCase_: Optional[int]   				=    num_hidden_layers
         UpperCamelCase_: Optional[int]   				=    num_attention_heads
         UpperCamelCase_: Optional[Any]   				=    intermediate_size
         UpperCamelCase_: Tuple   				=    hidden_act
         UpperCamelCase_: Any   				=    hidden_dropout_prob
         UpperCamelCase_: Any   				=    attention_probs_dropout_prob
         UpperCamelCase_: List[Any]   				=    max_position_embeddings
         UpperCamelCase_: List[Any]   				=    type_vocab_size
         UpperCamelCase_: Optional[int]   				=    initializer_range
         UpperCamelCase_: Tuple   				=    layer_norm_eps
         UpperCamelCase_: List[str]   				=    embedding_size
         UpperCamelCase_: int   				=    head_ratio
         UpperCamelCase_: Dict   				=    conv_kernel_size
         UpperCamelCase_: List[Any]   				=    num_groups
         UpperCamelCase_: Dict   				=    classifier_dropout







class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       @property
       def       _a   ( self							):
         if self.task == "multiple-choice":
           UpperCamelCase_: Dict   				=    {0: 'batch', 1: 'choice', 2: 'sequence'}
         else:
           UpperCamelCase_: Dict   				=    {0: 'batch', 1: 'sequence'}
         return OrderedDict(
             [
                 ('input_ids', dynamic_axis),
                 ('attention_mask', dynamic_axis),
                 ('token_type_ids', dynamic_axis),
             ]							)