



'''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 		lowerCamelCase__      (						A	,   unittest.TestCase     ):




					'''simple docstring'''



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




					def 						__UpperCAmelCase     (  self				:	Dict					)  ->       Tuple:



						'''simple docstring'''





						torch.manual_seed(0					)
						_lowercase					:       List[str]       =		UNetaDModel(
						    block_out_channels=(32, 32, 64)     ,  extra_in_channels=16     ,  sample_size=512     ,  sample_rate=1_6000     ,  in_channels=2     ,  out_channels=2     ,  flip_sin_to_cos=UpperCamelCase_     ,  use_timestep_embedding=UpperCamelCase_     ,  time_embedding_type='fourier'     ,  mid_block_type='UNetMidBlock1D'     ,  down_block_types=('DownBlock1DNoSkip', 'DownBlock1D', 'AttnDownBlock1D')     ,  up_block_types=('AttnUpBlock1D', 'UpBlock1D', 'UpBlock1DNoSkip')     ,  )
						_lowercase					:       List[str]       =		IPNDMScheduler()

						_lowercase					:       Tuple       =		{
						    'unet': unet,
						    'scheduler': scheduler,
						}
						return components




					def 						__UpperCAmelCase     (  self				:	Optional[Any]     ,  UpperCamelCase_				:	Any     ,  UpperCamelCase_				:	Any=0					)  ->       List[Any]:



						'''simple docstring'''





						if str(UpperCamelCase_					).startswith('mps'					):
							_lowercase					:       str       =		torch.manual_seed(UpperCamelCase_					)
						else:
							_lowercase					:       str       =		torch.Generator(device=UpperCamelCase_					).manual_seed(UpperCamelCase_					)
						_lowercase					:       Optional[int]       =		{
						    'batch_size': 1,
						    'generator': generator,
						    'num_inference_steps': 4,
						}
						return inputs




					def 						__UpperCAmelCase     (  self				:	Any					)  ->       List[Any]:



						'''simple docstring'''





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

						_lowercase					:       str       =		self.get_dummy_inputs(UpperCamelCase_					)
						_lowercase					:       Optional[Any]       =		pipe(**UpperCamelCase_					)
						_lowercase					:       Any       =		output.audios

						_lowercase					:       Dict       =		audio[0, -3:, -3:]

						assert audio.shape == (1, 2, components["unet"].sample_size)
						_lowercase					:       Union[str, Any]       =		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 						__UpperCAmelCase     (  self				:	Dict					)  ->       int:



						'''simple docstring'''





						return super().test_save_load_local()




					@skip_mps
					def 						__UpperCAmelCase     (  self				:	int					)  ->       Any:



						'''simple docstring'''





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




					@skip_mps
					def 						__UpperCAmelCase     (  self				:	str					)  ->       List[str]:



						'''simple docstring'''





						return super().test_save_load_optional_components()




					@skip_mps
					def 						__UpperCAmelCase     (  self				:	int					)  ->       str:



						'''simple docstring'''





						return super().test_attention_slicing_forward_pass()




					def 						__UpperCAmelCase     (  self				:	Any					)  ->       Union[str, Any]:



						'''simple docstring'''





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






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




					'''simple docstring'''



					def 						__UpperCAmelCase     (  self				:	int					)  ->       Dict:



						'''simple docstring'''





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




					def 						__UpperCAmelCase     (  self				:	Dict					)  ->       str:



						'''simple docstring'''





						_lowercase					:       int       =		torch_device

						_lowercase					:       Tuple       =		DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k'					)
						_lowercase					:       Dict       =		pipe.to(UpperCamelCase_					)
						pipe.set_progress_bar_config(disable=UpperCamelCase_					)

						_lowercase					:       int       =		torch.manual_seed(0					)
						_lowercase					:       Dict       =		pipe(generator=UpperCamelCase_     ,  num_inference_steps=100     ,  audio_length_in_s=4.0_96					)
						_lowercase					:       Union[str, Any]       =		output.audios

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

						assert audio.shape == (1, 2, pipe.unet.sample_size)
						_lowercase					:       List[str]       =		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 						__UpperCAmelCase     (  self				:	Dict					)  ->       Union[str, Any]:



						'''simple docstring'''





						_lowercase					:       int       =		torch_device

						_lowercase					:       int       =		DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k'     ,  torch_dtype=torch.floataa					)
						_lowercase					:       Union[str, Any]       =		pipe.to(UpperCamelCase_					)
						pipe.set_progress_bar_config(disable=UpperCamelCase_					)

						_lowercase					:       Union[str, Any]       =		torch.manual_seed(0					)
						_lowercase					:       List[str]       =		pipe(generator=UpperCamelCase_     ,  num_inference_steps=100     ,  audio_length_in_s=4.0_96					)
						_lowercase					:       Any       =		output.audios

						_lowercase					:       Any       =		audio[0, -3:, -3:]

						assert audio.shape == (1, 2, pipe.unet.sample_size)
						_lowercase					:       Dict       =		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 transformers import AutoTokenizer, is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, slow


if is_flax_available():
		import jax.numpy as jnp

		from transformers import FlaxXLMRobertaModel



@require_sentencepiece
@require_tokenizers
@require_flax
class 		lowerCamelCase__      (						unittest.TestCase     ):




					'''simple docstring'''



					@slow
					def 						__UpperCAmelCase     (  self				:	int					)  ->       Union[str, Any]:



						'''simple docstring'''





						_lowercase					:       List[Any]       =		FlaxXLMRobertaModel.from_pretrained('xlm-roberta-base'					)
						_lowercase					:       str       =		AutoTokenizer.from_pretrained('xlm-roberta-base'					)
						_lowercase					:       List[Any]       =		'The dog is cute and lives in the garden house'
						_lowercase					:       Optional[int]       =		jnp.array([tokenizer.encode(UpperCamelCase_					)]					)

						_lowercase					:       int       =		(1, 12, 768)  # batch_size, sequence_length, embedding_vector_dim
						_lowercase					:       Tuple       =		jnp.array(
						    [[-0.01_01, 0.12_18, -0.08_03, 0.08_01, 0.13_27, 0.07_76, -0.12_15, 0.23_83, 0.33_38, 0.31_06, 0.03_00, 0.02_52]]					)

						_lowercase					:       List[str]       =		model(UpperCamelCase_					)['last_hidden_state']
						self.assertEqual(output.shape     ,  UpperCamelCase_					)
						# compare the actual values for a slice of last dim
						self.assertTrue(jnp.allclose(output[:, :, -1]     ,  UpperCamelCase_     ,  atol=1E-3					)					)
