






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       _UpperCAmelCase					(       A__		,unittest.TestCase	):

				"""simple docstring"""




				lowercase__											=       DanceDiffusionPipeline
				lowercase__											=       UNCONDITIONAL_AUDIO_GENERATION_PARAMS
				lowercase__											=       PipelineTesterMixin.required_optional_params - {
				    """callback""",
				    """latents""",
				    """callback_steps""",
				    """output_type""",
				    """num_images_per_prompt""",
				}
				lowercase__											=       UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
				lowercase__											=       False
				lowercase__											=       False




				def 				lowercase__   (     self						:    str  ):

								'''simple docstring'''
								torch.manual_seed(0  )
								lowercase__    =			UNetaDModel(
								    block_out_channels=(32, 32, 64),  extra_in_channels=16,  sample_size=512,  sample_rate=16_000,  in_channels=2,  out_channels=2,  flip_sin_to_cos=lowerCamelCase,  use_timestep_embedding=lowerCamelCase,  time_embedding_type='''fourier''',  mid_block_type='''UNetMidBlock1D''',  down_block_types=('''DownBlock1DNoSkip''', '''DownBlock1D''', '''AttnDownBlock1D'''),  up_block_types=('''AttnUpBlock1D''', '''UpBlock1D''', '''UpBlock1DNoSkip'''),  )
								lowercase__    =			IPNDMScheduler()

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




				def 				lowercase__   (     self						:    str,  lowerCamelCase						:    Optional[Any],  lowerCamelCase						:    int=0  ):

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




				def 				lowercase__   (     self						:    List[str]  ):

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

								lowercase__    =			self.get_dummy_inputs(lowerCamelCase  )
								lowercase__    =			pipe(**lowerCamelCase  )
								lowercase__    =			output.audios

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

								assert audio.shape == (1, 2, components["unet"].sample_size)
								lowercase__    =			np.array([-0.7265, 1.0000, -0.8388, 0.1175, 0.9498, -1.0000]  )
								assert np.abs(audio_slice.flatten() - expected_slice  ).max() < 1E-2




				@skip_mps
				def 				lowercase__   (     self						:    Optional[Any]  ):

								'''simple docstring'''
								return super().test_save_load_local()




				@skip_mps
				def 				lowercase__   (     self						:    Tuple  ):

								'''simple docstring'''
								return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3  )




				@skip_mps
				def 				lowercase__   (     self						:    Union[str, Any]  ):

								'''simple docstring'''
								return super().test_save_load_optional_components()




				@skip_mps
				def 				lowercase__   (     self						:    Any  ):

								'''simple docstring'''
								return super().test_attention_slicing_forward_pass()




				def 				lowercase__   (     self						:    Union[str, Any]  ):

								'''simple docstring'''
								super().test_inference_batch_single_identical(expected_max_diff=3E-3  )



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

				"""simple docstring"""




				def 				lowercase__   (     self						:    Tuple  ):

								'''simple docstring'''
								# clean up the VRAM after each test
								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()




				def 				lowercase__   (     self						:    Tuple  ):

								'''simple docstring'''
								lowercase__    =			torch_device

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

								lowercase__    =			torch.manual_seed(0  )
								lowercase__    =			pipe(generator=lowerCamelCase,  num_inference_steps=100,  audio_length_in_s=4.096  )
								lowercase__    =			output.audios

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

								assert audio.shape == (1, 2, pipe.unet.sample_size)
								lowercase__    =			np.array([-0.0192, -0.0231, -0.0318, -0.0059, 0.0002, -0.0020]  )

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




				def 				lowercase__   (     self						:    int  ):

								'''simple docstring'''
								lowercase__    =			torch_device

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

								lowercase__    =			torch.manual_seed(0  )
								lowercase__    =			pipe(generator=lowerCamelCase,  num_inference_steps=100,  audio_length_in_s=4.096  )
								lowercase__    =			output.audios

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

								assert audio.shape == (1, 2, pipe.unet.sample_size)
								lowercase__    =			np.array([-0.0367, -0.0488, -0.0771, -0.0525, -0.0444, -0.0341]  )

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












import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_distilbert import DistilBertTokenizer


A__	:			List[Any]			   = logging.get_logger(__name__)

A__	:			str			   = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

A__	:			int			   = {
    'vocab_file': {
        'distilbert-base-uncased': 'https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt',
        'distilbert-base-uncased-distilled-squad': (
            'https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt'
        ),
        'distilbert-base-cased': 'https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt',
        'distilbert-base-cased-distilled-squad': (
            'https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt'
        ),
        'distilbert-base-german-cased': 'https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt',
        'distilbert-base-multilingual-cased': (
            'https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt'
        ),
    },
    'tokenizer_file': {
        'distilbert-base-uncased': 'https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json',
        'distilbert-base-uncased-distilled-squad': (
            'https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json'
        ),
        'distilbert-base-cased': 'https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json',
        'distilbert-base-cased-distilled-squad': (
            'https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json'
        ),
        'distilbert-base-german-cased': (
            'https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json'
        ),
        'distilbert-base-multilingual-cased': (
            'https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json'
        ),
    },
}

A__	:			Optional[Any]			   = {
    'distilbert-base-uncased': 5_12,
    'distilbert-base-uncased-distilled-squad': 5_12,
    'distilbert-base-cased': 5_12,
    'distilbert-base-cased-distilled-squad': 5_12,
    'distilbert-base-german-cased': 5_12,
    'distilbert-base-multilingual-cased': 5_12,
}


A__	:			List[str]			   = {
    'distilbert-base-uncased': {'do_lower_case': True},
    'distilbert-base-uncased-distilled-squad': {'do_lower_case': True},
    'distilbert-base-cased': {'do_lower_case': False},
    'distilbert-base-cased-distilled-squad': {'do_lower_case': False},
    'distilbert-base-german-cased': {'do_lower_case': False},
    'distilbert-base-multilingual-cased': {'do_lower_case': False},
}
class       _UpperCAmelCase					(       A__	):

				"""simple docstring"""




				lowercase__											=       VOCAB_FILES_NAMES
				lowercase__											=       PRETRAINED_VOCAB_FILES_MAP
				lowercase__											=       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				lowercase__											=       PRETRAINED_INIT_CONFIGURATION
				lowercase__											=       ["""input_ids""", """attention_mask"""]
				lowercase__											=       DistilBertTokenizer




				def __init__(     self						:    List[Any],  lowerCamelCase						:    List[Any]=None,  lowerCamelCase						:    Dict=None,  lowerCamelCase						:    str=True,  lowerCamelCase						:    Optional[int]="[UNK]",  lowerCamelCase						:    Optional[Any]="[SEP]",  lowerCamelCase						:    List[Any]="[PAD]",  lowerCamelCase						:    Any="[CLS]",  lowerCamelCase						:    Union[str, Any]="[MASK]",  lowerCamelCase						:    str=True,  lowerCamelCase						:    int=None,  **lowerCamelCase						:    Union[str, Any],  ):

								'''simple docstring'''
								super().__init__(
								    lowerCamelCase,  tokenizer_file=lowerCamelCase,  do_lower_case=lowerCamelCase,  unk_token=lowerCamelCase,  sep_token=lowerCamelCase,  pad_token=lowerCamelCase,  cls_token=lowerCamelCase,  mask_token=lowerCamelCase,  tokenize_chinese_chars=lowerCamelCase,  strip_accents=lowerCamelCase,  **lowerCamelCase,  )

								lowercase__    =			json.loads(self.backend_tokenizer.normalizer.__getstate__()  )
								if (
								    normalizer_state.get('''lowercase''',  lowerCamelCase  ) != do_lower_case
								    or normalizer_state.get('''strip_accents''',  lowerCamelCase  ) != strip_accents
								    or normalizer_state.get('''handle_chinese_chars''',  lowerCamelCase  ) != tokenize_chinese_chars
								):
												lowercase__    =			getattr(lowerCamelCase,  normalizer_state.pop('''type'''  )  )
												lowercase__    =			do_lower_case
												lowercase__    =			strip_accents
												lowercase__    =			tokenize_chinese_chars
												lowercase__    =			normalizer_class(**lowerCamelCase  )

								lowercase__    =			do_lower_case




				def 				lowercase__   (     self						:    str,  lowerCamelCase						:    Optional[Any],  lowerCamelCase						:    List[Any]=None  ):

								'''simple docstring'''
								lowercase__    =			[self.cls_token_id] + token_ids_a + [self.sep_token_id]

								if token_ids_a:
												output += token_ids_a + [self.sep_token_id]

								return output




				def 				lowercase__   (     self						:    Union[str, Any],  lowerCamelCase						:    List[int],  lowerCamelCase						:    Optional[List[int]] = None  ):

								'''simple docstring'''
								lowercase__    =			[self.sep_token_id]
								lowercase__    =			[self.cls_token_id]
								if token_ids_a is None:
												return len(cls + token_ids_a + sep  ) * [0]
								return len(cls + token_ids_a + sep  ) * [0] + len(token_ids_a + sep  ) * [1]




				def 				lowercase__   (     self						:    str,  lowerCamelCase						:    str,  lowerCamelCase						:    Optional[str] = None  ):

								'''simple docstring'''
								lowercase__    =			self._tokenizer.model.save(lowerCamelCase,  name=lowerCamelCase  )
								return tuple(lowerCamelCase  )




