
"""simple docstring"""




import gc
import unittest

import numpy as np
import torch

from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()





class 			snake_case  (				__lowercase							,      unittest.TestCase ):
							UpperCAmelCase__      =   DanceDiffusionPipeline
							UpperCAmelCase__      =   UNCONDITIONAL_AUDIO_GENERATION_PARAMS
							UpperCAmelCase__      =   PipelineTesterMixin.required_optional_params - {
							    '''callback''',
							    '''latents''',
							    '''callback_steps''',
							    '''output_type''',
							    '''num_images_per_prompt''',
							}
							UpperCAmelCase__      =   UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
							UpperCAmelCase__      =   False
							UpperCAmelCase__      =   False

							def _lowercase       (self     ):

									"""simple docstring"""







									torch.manual_seed(0     )
									SCREAMING_SNAKE_CASE_    			=			UNetaDModel(
									    block_out_channels=(32, 32, 64)							,							extra_in_channels=16							,							sample_size=5_12							,							sample_rate=1_60_00							,							in_channels=2							,							out_channels=2							,							flip_sin_to_cos=SCREAMING_SNAKE_CASE_							,							use_timestep_embedding=SCREAMING_SNAKE_CASE_							,							time_embedding_type='''fourier'''							,							mid_block_type='''UNetMidBlock1D'''							,							down_block_types=('''DownBlock1DNoSkip''', '''DownBlock1D''', '''AttnDownBlock1D''')							,							up_block_types=('''AttnUpBlock1D''', '''UpBlock1D''', '''UpBlock1DNoSkip''')							,							)
									SCREAMING_SNAKE_CASE_    			=			IPNDMScheduler()

									SCREAMING_SNAKE_CASE_    			=			{
									    '''unet''': unet,
									    '''scheduler''': scheduler,
									}
									return components

							def _lowercase       (self							,							SCREAMING_SNAKE_CASE_							,							SCREAMING_SNAKE_CASE_=0     ):

									"""simple docstring"""







									if str(SCREAMING_SNAKE_CASE_     ).startswith('''mps'''     ):
											SCREAMING_SNAKE_CASE_    			=			torch.manual_seed(SCREAMING_SNAKE_CASE_     )
									else:
											SCREAMING_SNAKE_CASE_    			=			torch.Generator(device=SCREAMING_SNAKE_CASE_     ).manual_seed(SCREAMING_SNAKE_CASE_     )
									SCREAMING_SNAKE_CASE_    			=			{
									    '''batch_size''': 1,
									    '''generator''': generator,
									    '''num_inference_steps''': 4,
									}
									return inputs

							def _lowercase       (self     ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_    			=			'''cpu'''  # ensure determinism for the device-dependent torch.Generator
									SCREAMING_SNAKE_CASE_    			=			self.get_dummy_components()
									SCREAMING_SNAKE_CASE_    			=			DanceDiffusionPipeline(**SCREAMING_SNAKE_CASE_     )
									SCREAMING_SNAKE_CASE_    			=			pipe.to(SCREAMING_SNAKE_CASE_     )
									pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_     )

									SCREAMING_SNAKE_CASE_    			=			self.get_dummy_inputs(SCREAMING_SNAKE_CASE_     )
									SCREAMING_SNAKE_CASE_    			=			pipe(**SCREAMING_SNAKE_CASE_     )
									SCREAMING_SNAKE_CASE_    			=			output.audios

									SCREAMING_SNAKE_CASE_    			=			audio[0, -3:, -3:]

									assert audio.shape == (1, 2, components["unet"].sample_size)
									SCREAMING_SNAKE_CASE_    			=			np.array([-0.72_65, 1.00_00, -0.83_88, 0.11_75, 0.94_98, -1.00_00]     )
									assert np.abs(audio_slice.flatten() - expected_slice     ).max() < 1e-2

							@skip_mps
							def _lowercase       (self     ):

									"""simple docstring"""







									return super().test_save_load_local()

							@skip_mps
							def _lowercase       (self     ):

									"""simple docstring"""







									return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3     )

							@skip_mps
							def _lowercase       (self     ):

									"""simple docstring"""







									return super().test_save_load_optional_components()

							@skip_mps
							def _lowercase       (self     ):

									"""simple docstring"""







									return super().test_attention_slicing_forward_pass()







							def _lowercase       (self     ):

									"""simple docstring"""







									super().test_inference_batch_single_identical(expected_max_diff=3e-3     )







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

							def _lowercase       (self     ):

									"""simple docstring"""







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

							def _lowercase       (self     ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_    			=			torch_device

									SCREAMING_SNAKE_CASE_    			=			DanceDiffusionPipeline.from_pretrained('''harmonai/maestro-150k'''     )
									SCREAMING_SNAKE_CASE_    			=			pipe.to(SCREAMING_SNAKE_CASE_     )
									pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_     )

									SCREAMING_SNAKE_CASE_    			=			torch.manual_seed(0     )
									SCREAMING_SNAKE_CASE_    			=			pipe(generator=SCREAMING_SNAKE_CASE_							,							num_inference_steps=1_00							,							audio_length_in_s=4.0_96     )
									SCREAMING_SNAKE_CASE_    			=			output.audios

									SCREAMING_SNAKE_CASE_    			=			audio[0, -3:, -3:]

									assert audio.shape == (1, 2, pipe.unet.sample_size)
									SCREAMING_SNAKE_CASE_    			=			np.array([-0.01_92, -0.02_31, -0.03_18, -0.00_59, 0.00_02, -0.00_20]     )

									assert np.abs(audio_slice.flatten() - expected_slice     ).max() < 1e-2







							def _lowercase       (self     ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_    			=			torch_device

									SCREAMING_SNAKE_CASE_    			=			DanceDiffusionPipeline.from_pretrained('''harmonai/maestro-150k'''							,							torch_dtype=torch.floataa     )
									SCREAMING_SNAKE_CASE_    			=			pipe.to(SCREAMING_SNAKE_CASE_     )
									pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_     )

									SCREAMING_SNAKE_CASE_    			=			torch.manual_seed(0     )
									SCREAMING_SNAKE_CASE_    			=			pipe(generator=SCREAMING_SNAKE_CASE_							,							num_inference_steps=1_00							,							audio_length_in_s=4.0_96     )
									SCREAMING_SNAKE_CASE_    			=			output.audios

									SCREAMING_SNAKE_CASE_    			=			audio[0, -3:, -3:]

									assert audio.shape == (1, 2, pipe.unet.sample_size)
									SCREAMING_SNAKE_CASE_    			=			np.array([-0.03_67, -0.04_88, -0.07_71, -0.05_25, -0.04_44, -0.03_41]     )

									assert np.abs(audio_slice.flatten() - expected_slice     ).max() < 1e-2

"""simple docstring"""




import unittest

from datasets import load_dataset

from transformers import BloomTokenizerFast
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin





@require_tokenizers
class 			snake_case  (				__lowercase							,      unittest.TestCase ):
							UpperCAmelCase__      =   None
							UpperCAmelCase__      =   BloomTokenizerFast
							UpperCAmelCase__      =   BloomTokenizerFast
							UpperCAmelCase__      =   True
							UpperCAmelCase__      =   False
							UpperCAmelCase__      =   '''tokenizer_file'''
							UpperCAmelCase__      =   {'''bos_token''': '''<s>''', '''eos_token''': '''</s>''', '''unk_token''': '''<unk>''', '''pad_token''': '''<pad>'''}

							def _lowercase       (self     ):

									"""simple docstring"""







									super().setUp()
									SCREAMING_SNAKE_CASE_    			=			BloomTokenizerFast.from_pretrained('''bigscience/tokenizer'''     )
									tokenizer.save_pretrained(self.tmpdirname     )

							def _lowercase       (self							,							**SCREAMING_SNAKE_CASE_     ):

									"""simple docstring"""







									kwargs.update(self.special_tokens_map     )
									return BloomTokenizerFast.from_pretrained(self.tmpdirname							,							**SCREAMING_SNAKE_CASE_     )

							def _lowercase       (self     ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_    			=			self.get_rust_tokenizer()

									SCREAMING_SNAKE_CASE_    			=			['''The quick brown fox</s>''', '''jumps over the lazy dog</s>''']
									SCREAMING_SNAKE_CASE_    			=			[[21_75, 2_37_14, 7_31_73, 14_42_52, 2], [77, 13_26_19, 34_78, 3_68, 10_95_86, 3_54_33, 2]]

									SCREAMING_SNAKE_CASE_    			=			tokenizer.batch_encode_plus(SCREAMING_SNAKE_CASE_     )['''input_ids''']
									self.assertListEqual(SCREAMING_SNAKE_CASE_							,							SCREAMING_SNAKE_CASE_     )

									SCREAMING_SNAKE_CASE_    			=			tokenizer.batch_decode(SCREAMING_SNAKE_CASE_     )
									self.assertListEqual(SCREAMING_SNAKE_CASE_							,							SCREAMING_SNAKE_CASE_     )

							def _lowercase       (self							,							SCREAMING_SNAKE_CASE_=6     ):

									"""simple docstring"""







									for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
											with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})'     ):
													SCREAMING_SNAKE_CASE_    			=			self.rust_tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE_							,							**SCREAMING_SNAKE_CASE_     )
													# tokenizer_r.pad_token = None # Hotfixing padding = None
													# Simple input
													SCREAMING_SNAKE_CASE_    			=			'''This is a simple input'''
													SCREAMING_SNAKE_CASE_    			=			['''This is a simple input 1''', '''This is a simple input 2''']
													SCREAMING_SNAKE_CASE_    			=			('''This is a simple input''', '''This is a pair''')
													SCREAMING_SNAKE_CASE_    			=			[
													    ('''This is a simple input 1''', '''This is a simple input 2'''),
													    ('''This is a simple pair 1''', '''This is a simple pair 2'''),
													]

													# Simple input tests
													try:
															tokenizer_r.encode(SCREAMING_SNAKE_CASE_							,							max_length=SCREAMING_SNAKE_CASE_     )
															tokenizer_r.encode_plus(SCREAMING_SNAKE_CASE_							,							max_length=SCREAMING_SNAKE_CASE_     )

															tokenizer_r.batch_encode_plus(SCREAMING_SNAKE_CASE_							,							max_length=SCREAMING_SNAKE_CASE_     )
															tokenizer_r.encode(SCREAMING_SNAKE_CASE_							,							max_length=SCREAMING_SNAKE_CASE_     )
															tokenizer_r.batch_encode_plus(SCREAMING_SNAKE_CASE_							,							max_length=SCREAMING_SNAKE_CASE_     )
													except ValueError:
															self.fail('''Bloom Tokenizer should be able to deal with padding'''     )

													SCREAMING_SNAKE_CASE_    			=			None  # Hotfixing padding = None
													self.assertRaises(SCREAMING_SNAKE_CASE_							,							tokenizer_r.encode							,							SCREAMING_SNAKE_CASE_							,							max_length=SCREAMING_SNAKE_CASE_							,							padding='''max_length'''     )

													# Simple input
													self.assertRaises(SCREAMING_SNAKE_CASE_							,							tokenizer_r.encode_plus							,							SCREAMING_SNAKE_CASE_							,							max_length=SCREAMING_SNAKE_CASE_							,							padding='''max_length'''     )

													# Simple input
													self.assertRaises(
													    SCREAMING_SNAKE_CASE_							,							tokenizer_r.batch_encode_plus							,							SCREAMING_SNAKE_CASE_							,							max_length=SCREAMING_SNAKE_CASE_							,							padding='''max_length'''							,							)

													# Pair input
													self.assertRaises(SCREAMING_SNAKE_CASE_							,							tokenizer_r.encode							,							SCREAMING_SNAKE_CASE_							,							max_length=SCREAMING_SNAKE_CASE_							,							padding='''max_length'''     )

													# Pair input
													self.assertRaises(SCREAMING_SNAKE_CASE_							,							tokenizer_r.encode_plus							,							SCREAMING_SNAKE_CASE_							,							max_length=SCREAMING_SNAKE_CASE_							,							padding='''max_length'''     )

													# Pair input
													self.assertRaises(
													    SCREAMING_SNAKE_CASE_							,							tokenizer_r.batch_encode_plus							,							SCREAMING_SNAKE_CASE_							,							max_length=SCREAMING_SNAKE_CASE_							,							padding='''max_length'''							,							)

							def _lowercase       (self     ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_    			=			self.get_rust_tokenizer()
									SCREAMING_SNAKE_CASE_    			=			load_dataset('''xnli'''							,							'''all_languages'''							,							split='''test'''							,							streaming=SCREAMING_SNAKE_CASE_     )

									SCREAMING_SNAKE_CASE_    			=			next(iter(SCREAMING_SNAKE_CASE_     )     )['''premise''']  # pick up one data
									SCREAMING_SNAKE_CASE_    			=			list(sample_data.values()     )

									SCREAMING_SNAKE_CASE_    			=			list(map(tokenizer.encode							,							SCREAMING_SNAKE_CASE_     )     )
									SCREAMING_SNAKE_CASE_    			=			[tokenizer.decode(SCREAMING_SNAKE_CASE_							,							clean_up_tokenization_spaces=SCREAMING_SNAKE_CASE_     ) for x in output_tokens]
									self.assertListEqual(SCREAMING_SNAKE_CASE_							,							SCREAMING_SNAKE_CASE_     )







							def _lowercase       (self     ):

									"""simple docstring"""







									self.assertGreaterEqual(len(self.tokenizer_class.pretrained_vocab_files_map     )							,							1     )
									self.assertGreaterEqual(len(list(self.tokenizer_class.pretrained_vocab_files_map.values()     )[0]     )							,							1     )