





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






       @property
       def 							__A (   self     ):
              torch.manual_seed(0     )
              _UpperCAmelCase     =     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     ):
              torch.manual_seed(0     )
              _UpperCAmelCase     =     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     ):
              torch.manual_seed(0     )
              _UpperCAmelCase     =     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')    ,  )
              _UpperCAmelCase     =     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     ):
              _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=a__    ,  unet=self.dummy_unet    ,  mel=a__    ,  scheduler=a__     )
              _UpperCAmelCase     =     pipe.to(a__     )
              pipe.set_progress_bar_config(disable=a__     )

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

              _UpperCAmelCase     =     torch.Generator(device=a__     ).manual_seed(42     )
              _UpperCAmelCase     =     pipe(generator=a__    ,  steps=4    ,  return_dict=a__     )
              _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, 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

              _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=a__    ,  scheduler=a__     )
              _UpperCAmelCase     =     pipe.to(a__     )
              pipe.set_progress_bar_config(disable=a__     )

              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=a__     ).manual_seed(42     )
              _UpperCAmelCase     =     pipe(raw_audio=a__    ,  generator=a__    ,  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([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

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

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

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

              _UpperCAmelCase     =     torch.Generator(device=a__     ).manual_seed(42     )
              _UpperCAmelCase     =     pipe(generator=a__     )
              _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([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













"""simple docstring"""






import unittest

from transformers import RoFormerTokenizer, RoFormerTokenizerFast
from transformers.testing_utils import require_rjieba, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin



@require_rjieba
@require_tokenizers
class 	lowerCAmelCase					(      snake_case			,	unittest.TestCase							):
       lowerCAmelCase__						 =  RoFormerTokenizer
       lowerCAmelCase__						 =  RoFormerTokenizerFast
       lowerCAmelCase__						 =  True
       lowerCAmelCase__						 =  True






       def 							__A (   self     ):
              super().setUp()






       def 							__A (   self    ,  **a__     ):
              return self.tokenizer_class.from_pretrained('junnyu/roformer_chinese_base'    ,  **a__     )






       def 							__A (   self    ,  **a__     ):
              return self.rust_tokenizer_class.from_pretrained('junnyu/roformer_chinese_base'    ,  **a__     )






       def 							__A (   self     ):
              _UpperCAmelCase     =     '永和服装饰品有限公司,今天天气非常好'
              _UpperCAmelCase     =     '永和 服装 饰品 有限公司 , 今 天 天 气 非常 好'
              return input_text, output_text






       def 							__A (   self     ):
              _UpperCAmelCase     =     self.get_tokenizer()
              _UpperCAmelCase    ,							_UpperCAmelCase     =     self.get_chinese_input_output_texts()
              _UpperCAmelCase     =     tokenizer.tokenize(a__     )

              self.assertListEqual(a__    ,  output_text.split()     )

              _UpperCAmelCase     =     tokens + [tokenizer.unk_token]
              _UpperCAmelCase     =     [2_29_43, 2_13_32, 3_44_31, 4_59_04, 1_17, 3_06, 12_31, 12_31, 26_53, 3_39_94, 12_66, 1_00]
              self.assertListEqual(tokenizer.convert_tokens_to_ids(a__     )    ,  a__     )






       def 							__A (   self     ):
              _UpperCAmelCase     =     self.get_rust_tokenizer()
              _UpperCAmelCase    ,							_UpperCAmelCase     =     self.get_chinese_input_output_texts()
              _UpperCAmelCase     =     tokenizer.tokenize(a__     )
              self.assertListEqual(a__    ,  output_text.split()     )
              _UpperCAmelCase     =     tokens + [tokenizer.unk_token]
              _UpperCAmelCase     =     [2_29_43, 2_13_32, 3_44_31, 4_59_04, 1_17, 3_06, 12_31, 12_31, 26_53, 3_39_94, 12_66, 1_00]
              self.assertListEqual(tokenizer.convert_tokens_to_ids(a__     )    ,  a__     )






       def 							__A (   self     ):
              pass






       def 							__A (   self     ):
              pass






       def 							__A (   self     ):
              pass






