




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







   def lowerCamelCase						(      self		:						Optional[Any]):

        """simple docstring"""


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







   @property
   def lowerCamelCase						(      self		:						Dict):

        """simple docstring"""


        torch.manual_seed(0)
        UpperCAmelCase_          =							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 lowerCamelCase						(      self		:						Optional[int]):

        """simple docstring"""


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

        """simple docstring"""


        torch.manual_seed(0)
        UpperCAmelCase_          =							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_          =							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 lowerCamelCase						(      self		:						str):

        """simple docstring"""


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

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

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

        UpperCAmelCase_          =							torch.Generator(device=_snake_case).manual_seed(42)
        UpperCAmelCase_          =							pipe(generator=_snake_case					,						steps=4					,						return_dict=_snake_case)
        UpperCAmelCase_          =							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_          =							np.frombuffer(image.tobytes()					,						dtype='''uint8''')[:10]
        UpperCAmelCase_          =							np.frombuffer(image_from_tuple.tobytes()					,						dtype='''uint8''')[:10]
        UpperCAmelCase_          =							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_          =							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_          =							DDIMScheduler()
        UpperCAmelCase_          =							self.dummy_vqvae_and_unet
        UpperCAmelCase_          =							AudioDiffusionPipeline(
            vqvae=self.dummy_vqvae_and_unet[0]					,						unet=dummy_vqvae_and_unet[1]					,						mel=_snake_case					,						scheduler=_snake_case)
        UpperCAmelCase_          =							pipe.to(_snake_case)
        pipe.set_progress_bar_config(disable=_snake_case)

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

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

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

        np.random.seed(0)
        UpperCAmelCase_          =							torch.rand((1, 1, 10))
        UpperCAmelCase_          =							pipe(generator=_snake_case					,						encoding=_snake_case)
        UpperCAmelCase_          =							output.images[0]
        UpperCAmelCase_          =							np.frombuffer(image.tobytes()					,						dtype='''uint8''')[:10]
        UpperCAmelCase_          =							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 __snake_case			(			unittest.TestCase		):







   def lowerCamelCase						(      self		:						Optional[Any]):

        """simple docstring"""


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







   def lowerCamelCase						(      self		:						Tuple):

        """simple docstring"""


        UpperCAmelCase_          =							torch_device

        UpperCAmelCase_          =							DiffusionPipeline.from_pretrained('''teticio/audio-diffusion-ddim-256''')
        UpperCAmelCase_          =							pipe.to(_snake_case)
        pipe.set_progress_bar_config(disable=_snake_case)

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

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












import os
import unittest

from transformers.models.phobert.tokenization_phobert import VOCAB_FILES_NAMES, PhobertTokenizer

from ...test_tokenization_common import TokenizerTesterMixin







class __snake_case			(			a ,  unittest.TestCase		):
   UpperCAmelCase__  :    Any          =  PhobertTokenizer
   UpperCAmelCase__  :    List[str]          =  False







   def lowerCamelCase						(      self		:						str):

        """simple docstring"""


        super().setUp()

        # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
        UpperCAmelCase_          =							['''T@@''', '''i''', '''I''', '''R@@''', '''r''', '''e@@''']
        UpperCAmelCase_          =							dict(zip(_snake_case					,						range(len(_snake_case))))
        UpperCAmelCase_          =							['''#version: 0.2''', '''l à</w>''']
        UpperCAmelCase_          =							{'''unk_token''': '''<unk>'''}

        UpperCAmelCase_          =							os.path.join(self.tmpdirname					,						VOCAB_FILES_NAMES['''vocab_file'''])
        UpperCAmelCase_          =							os.path.join(self.tmpdirname					,						VOCAB_FILES_NAMES['''merges_file'''])

        with open(self.vocab_file					,						'''w'''					,						encoding='''utf-8''') as fp:
             for token in vocab_tokens:
                  fp.write(F"""{token} {vocab_tokens[token]}\n""")
        with open(self.merges_file					,						'''w'''					,						encoding='''utf-8''') as fp:
             fp.write('''\n'''.join(_snake_case))







   def lowerCamelCase						(      self		:						int					,						**_snake_case		:						Any):

        """simple docstring"""


        kwargs.update(self.special_tokens_map)
        return PhobertTokenizer.from_pretrained(self.tmpdirname					,						**_snake_case)







   def lowerCamelCase						(      self		:						Optional[Any]					,						_snake_case		:						Union[str, Any]):

        """simple docstring"""


        UpperCAmelCase_          =							'''Tôi là VinAI Research'''
        UpperCAmelCase_          =							'''T<unk> i <unk> <unk> <unk> <unk> <unk> <unk> I Re<unk> e<unk> <unk> <unk> <unk>'''
        return input_text, output_text







   def lowerCamelCase						(      self		:						List[str]):

        """simple docstring"""


        UpperCAmelCase_          =							PhobertTokenizer(self.vocab_file					,						self.merges_file					,						**self.special_tokens_map)
        UpperCAmelCase_          =							'''Tôi là VinAI Research'''
        UpperCAmelCase_          =							'''T@@ ô@@ i l@@ à V@@ i@@ n@@ A@@ I R@@ e@@ s@@ e@@ a@@ r@@ c@@ h'''.split()
        UpperCAmelCase_          =							tokenizer.tokenize(_snake_case)
        print(_snake_case)
        self.assertListEqual(_snake_case					,						_snake_case)

        UpperCAmelCase_          =							tokens + [tokenizer.unk_token]

        UpperCAmelCase_          =							[4, 3, 5, 3, 3, 3, 3, 3, 3, 6, 7, 9, 3, 9, 3, 3, 3, 3, 3]
        self.assertListEqual(tokenizer.convert_tokens_to_ids(_snake_case)					,						_snake_case)






