






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

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

					@property
					def   lowerCamelCase__   (			self  :      List[Any]					)			->							Optional[int]:
											torch.manual_seed(0					)
											__magic_name__:	Dict							=       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   lowerCamelCase__   (			self  :      List[str]					)			->							Optional[int]:
											torch.manual_seed(0					)
											__magic_name__:	Tuple							=       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   lowerCamelCase__   (			self  :      Union[str, Any]					)			->							Optional[Any]:
											torch.manual_seed(0					)
											__magic_name__:	Any							=       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""")     ,		)
											__magic_name__:	Union[str, 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   lowerCamelCase__   (			self  :      int					)			->							Union[str, Any]:
											__magic_name__:	Tuple							=       """cpu"""  # ensure determinism for the device-dependent torch.Generator
											__magic_name__:	Dict							=       Mel(
											    x_res=self.dummy_unet.config.sample_size[1]     ,		y_res=self.dummy_unet.config.sample_size[0]     ,		)

											__magic_name__:	Optional[int]							=       DDPMScheduler()
											__magic_name__:	str							=       AudioDiffusionPipeline(vqvae=__snake_case     ,		unet=self.dummy_unet     ,		mel=__snake_case     ,		scheduler=__snake_case					)
											__magic_name__:	Any							=       pipe.to(__snake_case					)
											pipe.set_progress_bar_config(disable=__snake_case					)

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

											__magic_name__:	int							=       torch.Generator(device=__snake_case					).manual_seed(4_2					)
											__magic_name__:	List[str]							=       pipe(generator=__snake_case     ,		steps=4     ,		return_dict=__snake_case					)
											__magic_name__:	Optional[int]							=       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]
											)
											__magic_name__:	Tuple							=       np.frombuffer(image.tobytes()     ,		dtype="""uint8"""					)[:1_0]
											__magic_name__:	List[str]							=       np.frombuffer(image_from_tuple.tobytes()     ,		dtype="""uint8"""					)[:1_0]
											__magic_name__:	str							=       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

											__magic_name__:	Optional[Any]							=       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]     ,		)

											__magic_name__:	Optional[Any]							=       DDIMScheduler()
											__magic_name__:	Optional[Any]							=       self.dummy_vqvae_and_unet
											__magic_name__:	int							=       AudioDiffusionPipeline(
											    vqvae=self.dummy_vqvae_and_unet[0]     ,		unet=dummy_vqvae_and_unet[1]     ,		mel=__snake_case     ,		scheduler=__snake_case					)
											__magic_name__:	str							=       pipe.to(__snake_case					)
											pipe.set_progress_bar_config(disable=__snake_case					)

											np.random.seed(0					)
											__magic_name__:	Any							=       np.random.uniform(-1     ,		1     ,		((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,)					)
											__magic_name__:	Any							=       torch.Generator(device=__snake_case					).manual_seed(4_2					)
											__magic_name__:	Optional[Any]							=       pipe(raw_audio=__snake_case     ,		generator=__snake_case     ,		start_step=5     ,		steps=1_0					)
											__magic_name__:	List[str]							=       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]
											)
											__magic_name__:	str							=       np.frombuffer(image.tobytes()     ,		dtype="""uint8"""					)[:1_0]
											__magic_name__:	Optional[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

											__magic_name__:	int							=       self.dummy_unet_condition
											__magic_name__:	List[Any]							=       AudioDiffusionPipeline(
											    vqvae=self.dummy_vqvae_and_unet[0]     ,		unet=__snake_case     ,		mel=__snake_case     ,		scheduler=__snake_case					)
											__magic_name__:	List[Any]							=       pipe.to(__snake_case					)
											pipe.set_progress_bar_config(disable=__snake_case					)

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

					def   lowerCamelCase__   (			self  :      Any					)			->							Tuple:
											# clean up the VRAM after each test
											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()






					def   lowerCamelCase__   (			self  :      Union[str, Any]					)			->							List[str]:
											__magic_name__:	Dict							=       torch_device

											__magic_name__:	Dict							=       DiffusionPipeline.from_pretrained("""teticio/audio-diffusion-ddim-256"""					)
											__magic_name__:	List[Any]							=       pipe.to(__snake_case					)
											pipe.set_progress_bar_config(disable=__snake_case					)

											__magic_name__:	Dict							=       torch.Generator(device=__snake_case					).manual_seed(4_2					)
											__magic_name__:	str							=       pipe(generator=__snake_case					)
											__magic_name__:	str							=       output.audios[0]
											__magic_name__:	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]
											__magic_name__:	List[str]							=       np.frombuffer(image.tobytes()     ,		dtype="""uint8"""					)[:1_0]
											__magic_name__:	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






"""simple docstring"""

import os
import unittest

from transformers import LxmertTokenizer, LxmertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin






@require_tokenizers
class 			__a					(						__magic_name__       ,	unittest.TestCase       ):





       """simple docstring"""


       __UpperCamelCase					:      Optional[Any]    =						LxmertTokenizer
       __UpperCamelCase					:      Optional[Any]    =						LxmertTokenizerFast
       __UpperCamelCase					:      str    =						True
       __UpperCamelCase					:      Any    =						True






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        super().setUp()

        lowerCAmelCase__   :				Tuple 			=				[
            "[UNK]",
            "[CLS]",
            "[SEP]",
            "want",
            "##want",
            "##ed",
            "wa",
            "un",
            "runn",
            "##ing",
            ",",
            "low",
            "lowest",
        ]
        lowerCAmelCase__   :				Dict 			=				os.path.join(self.tmpdirname			,					VOCAB_FILES_NAMES["vocab_file"]   )
        with open(self.vocab_file			,					"w"			,					encoding="utf-8"   ) as vocab_writer:
         vocab_writer.write("".join([x + "\n" for x in vocab_tokens]   )   )






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case   ):





        """simple docstring"""

        lowerCAmelCase__   :				List[Any] 			=				"UNwant\u00E9d,running"
        lowerCAmelCase__   :				Union[str, Any] 			=				"unwanted, running"
        return input_text, output_text






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				str 			=				self.tokenizer_class(self.vocab_file   )

        lowerCAmelCase__   :				List[Any] 			=				tokenizer.tokenize("UNwant\u00E9d,running"   )
        self.assertListEqual(snake_case			,					["un", "##want", "##ed", ",", "runn", "##ing"]   )
        self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case   )			,					[7, 4, 5, 10, 8, 9]   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        if not self.test_rust_tokenizer:
         return

        lowerCAmelCase__   :				int 			=				self.get_tokenizer()
        lowerCAmelCase__   :				List[str] 			=				self.get_rust_tokenizer()

        lowerCAmelCase__   :				Optional[Any] 			=				"I was born in 92000, and this is falsé."

        lowerCAmelCase__   :				int 			=				tokenizer.tokenize(snake_case   )
        lowerCAmelCase__   :				int 			=				rust_tokenizer.tokenize(snake_case   )
        self.assertListEqual(snake_case			,					snake_case   )

        lowerCAmelCase__   :				str 			=				tokenizer.encode(snake_case			,					add_special_tokens=snake_case   )
        lowerCAmelCase__   :				List[str] 			=				rust_tokenizer.encode(snake_case			,					add_special_tokens=snake_case   )
        self.assertListEqual(snake_case			,					snake_case   )

        lowerCAmelCase__   :				Tuple 			=				self.get_rust_tokenizer()
        lowerCAmelCase__   :				Tuple 			=				tokenizer.encode(snake_case   )
        lowerCAmelCase__   :				List[Any] 			=				rust_tokenizer.encode(snake_case   )
        self.assertListEqual(snake_case			,					snake_case   )






