



'''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 				lowercase__      (				lowercase		,   unittest.TestCase  ):
	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       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							torch.manual_seed(0					)
							_UpperCamelCase   :						List[str]												=  UNetaDModel(
							    block_out_channels=(32, 32, 64)					,extra_in_channels=16					,sample_size=512					,sample_rate=16000					,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')					,)
							_UpperCamelCase   :						List[Any]												=  IPNDMScheduler()

							_UpperCamelCase   :						Any												=  {
							    'unet': unet,
							    'scheduler': scheduler,
							}
							return components





	def       UpperCamelCase_    (					self   :							List[Any]					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							Any=0					):







							'''simple docstring'''
							if str(lowerCamelCase__					).startswith('mps'					):
													_UpperCamelCase   :						int												=  torch.manual_seed(lowerCamelCase__					)
							else:
													_UpperCamelCase   :						List[Any]												=  torch.Generator(device=lowerCamelCase__					).manual_seed(lowerCamelCase__					)
							_UpperCamelCase   :						int												=  {
							    'batch_size': 1,
							    'generator': generator,
							    'num_inference_steps': 4,
							}
							return inputs





	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[int]												=  'cpu'  # ensure determinism for the device-dependent torch.Generator
							_UpperCamelCase   :						Optional[int]												=  self.get_dummy_components()
							_UpperCamelCase   :						int												=  DanceDiffusionPipeline(**lowerCamelCase__					)
							_UpperCamelCase   :						Union[str, Any]												=  pipe.to(lowerCamelCase__					)
							pipe.set_progress_bar_config(disable=lowerCamelCase__					)

							_UpperCamelCase   :						str												=  self.get_dummy_inputs(lowerCamelCase__					)
							_UpperCamelCase   :						Optional[Any]												=  pipe(**lowerCamelCase__					)
							_UpperCamelCase   :						List[str]												=  output.audios

							_UpperCamelCase   :						Optional[int]												=  audio[0, -3:, -3:]

							assert audio.shape == (1, 2, components["unet"].sample_size)
							_UpperCamelCase   :						Union[str, Any]												=  np.array([-0.7_2_6_5, 1.0_0_0_0, -0.8_3_8_8, 0.1_1_7_5, 0.9_4_9_8, -1.0_0_0_0]					)
							assert np.abs(audio_slice.flatten() - expected_slice					).max() < 1E-2





	@skip_mps
	def       UpperCamelCase_    (					self   :							List[Any]					):







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





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







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





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







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





	@skip_mps
	def       UpperCamelCase_    (					self   :							Any					):







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





	def       UpperCamelCase_    (					self   :							List[str]					):







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






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





	def       UpperCamelCase_    (					self   :							List[Any]					):







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





	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[int]												=  torch_device

							_UpperCamelCase   :						Any												=  DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k'					)
							_UpperCamelCase   :						List[str]												=  pipe.to(lowerCamelCase__					)
							pipe.set_progress_bar_config(disable=lowerCamelCase__					)

							_UpperCamelCase   :						int												=  torch.manual_seed(0					)
							_UpperCamelCase   :						Any												=  pipe(generator=lowerCamelCase__					,num_inference_steps=100					,audio_length_in_s=4.0_9_6					)
							_UpperCamelCase   :						List[Any]												=  output.audios

							_UpperCamelCase   :						Optional[Any]												=  audio[0, -3:, -3:]

							assert audio.shape == (1, 2, pipe.unet.sample_size)
							_UpperCamelCase   :						Optional[Any]												=  np.array([-0.0_1_9_2, -0.0_2_3_1, -0.0_3_1_8, -0.0_0_5_9, 0.0_0_0_2, -0.0_0_2_0]					)

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





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							_UpperCamelCase   :						Tuple												=  torch_device

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

							_UpperCamelCase   :						Optional[Any]												=  torch.manual_seed(0					)
							_UpperCamelCase   :						Dict												=  pipe(generator=lowerCamelCase__					,num_inference_steps=100					,audio_length_in_s=4.0_9_6					)
							_UpperCamelCase   :						Tuple												=  output.audios

							_UpperCamelCase   :						Union[str, Any]												=  audio[0, -3:, -3:]

							assert audio.shape == (1, 2, pipe.unet.sample_size)
							_UpperCamelCase   :						Dict												=  np.array([-0.0_3_6_7, -0.0_4_8_8, -0.0_7_7_1, -0.0_5_2_5, -0.0_4_4_4, -0.0_3_4_1]					)

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







'''simple docstring'''
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


snake_case_					:      Tuple    			=    {
    'configuration_layoutlmv2': ['LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LayoutLMv2Config'],
    'processing_layoutlmv2': ['LayoutLMv2Processor'],
    'tokenization_layoutlmv2': ['LayoutLMv2Tokenizer'],
}

try:
					if not is_tokenizers_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					snake_case_					:      int    			=    ['LayoutLMv2TokenizerFast']

try:
					if not is_vision_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					snake_case_					:      Union[str, Any]    			=    ['LayoutLMv2FeatureExtractor']
					snake_case_					:      str    			=    ['LayoutLMv2ImageProcessor']

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					snake_case_					:      Optional[int]    			=    [
					    'LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST',
					    'LayoutLMv2ForQuestionAnswering',
					    'LayoutLMv2ForSequenceClassification',
					    'LayoutLMv2ForTokenClassification',
					    'LayoutLMv2Layer',
					    'LayoutLMv2Model',
					    'LayoutLMv2PreTrainedModel',
					]

if TYPE_CHECKING:
					from .configuration_layoutlmva import LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig
					from .processing_layoutlmva import LayoutLMvaProcessor
					from .tokenization_layoutlmva import LayoutLMvaTokenizer

					try:
										if not is_tokenizers_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

					try:
										if not is_vision_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor, LayoutLMvaImageProcessor

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_layoutlmva import (
										    LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST,
										    LayoutLMvaForQuestionAnswering,
										    LayoutLMvaForSequenceClassification,
										    LayoutLMvaForTokenClassification,
										    LayoutLMvaLayer,
										    LayoutLMvaModel,
										    LayoutLMvaPreTrainedModel,
										)
else:
					import sys

					snake_case_					:      int    			=    _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)


