

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 					a__						(					snake_case							,	unittest.TestCase			):
			"""simple docstring"""







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


			def      UpperCamelCase		(    self       )  ->						Optional[int]:
							'''simple docstring'''






							torch.manual_seed(0       )
							A__        =			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=lowercase      ,      use_timestep_embedding=lowercase      ,      time_embedding_type="fourier"      ,      mid_block_type="UNetMidBlock1D"      ,      down_block_types=("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D")      ,      up_block_types=("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip")      ,      )
							A__        =			IPNDMScheduler()

							A__        =			{
							    "unet": unet,
							    "scheduler": scheduler,
							}
							return components


			def      UpperCamelCase		(    self      ,      lowercase      ,      lowercase=0       )  ->						Union[str, Any]:
							'''simple docstring'''






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


			def      UpperCamelCase		(    self       )  ->						Any:
							'''simple docstring'''






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

							A__        =			self.get_dummy_inputs(lowercase       )
							A__        =			pipe(**lowercase       )
							A__        =			output.audios

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

							assert audio.shape == (1, 2, components["unet"].sample_size)
							A__        =			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      UpperCamelCase		(    self       )  ->						Dict:
							'''simple docstring'''






							return super().test_save_load_local()


			@skip_mps
			def      UpperCamelCase		(    self       )  ->						int:
							'''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       )  ->						int:
							'''simple docstring'''






							return super().test_attention_slicing_forward_pass()


			def      UpperCamelCase		(    self       )  ->						str:
							'''simple docstring'''






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

@slow
@require_torch_gpu
class 					a__						(					unittest.TestCase			):
			"""simple docstring"""







			def      UpperCamelCase		(    self       )  ->						int:
							'''simple docstring'''






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


			def      UpperCamelCase		(    self       )  ->						int:
							'''simple docstring'''






							A__        =			torch_device

							A__        =			DanceDiffusionPipeline.from_pretrained("harmonai/maestro-150k"       )
							A__        =			pipe.to(lowercase       )
							pipe.set_progress_bar_config(disable=lowercase       )

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

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

							assert audio.shape == (1, 2, pipe.unet.sample_size)
							A__        =			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      UpperCamelCase		(    self       )  ->						List[Any]:
							'''simple docstring'''






							A__        =			torch_device

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

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

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

							assert audio.shape == (1, 2, pipe.unet.sample_size)
							A__        =			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




from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow


if is_tf_available():
	import numpy as np
	import tensorflow as tf

	from transformers import TFCamembertModel

@require_tf
@require_sentencepiece
@require_tokenizers
class 					a__						(					unittest.TestCase			):
			"""simple docstring"""







			@slow
			def      UpperCamelCase		(    self       )  ->						int:
							'''simple docstring'''






							A__        =			TFCamembertModel.from_pretrained("jplu/tf-camembert-base"       )

							A__        =			tf.convert_to_tensor(
							    [[5, 121, 11, 660, 16, 730, 25543, 110, 83, 6]]      ,      dtype=tf.intaa      ,      )  # J'aime le camembert !"

							A__        =			model(lowercase       )["last_hidden_state"]
							A__        =			tf.TensorShape((1, 10, 768)       )
							self.assertEqual(output.shape      ,      lowercase       )
							# compare the actual values for a slice.
							A__        =			tf.convert_to_tensor(
							    [[[-0.0254, 0.0235, 0.1027], [0.0606, -0.1811, -0.0418], [-0.1561, -0.1127, 0.2687]]]      ,      dtype=tf.floataa      ,      )
							# camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
							# camembert.eval()
							# expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach()

							self.assertTrue(np.allclose(output[:, :3, :3].numpy()      ,      expected_slice.numpy()      ,      atol=1e-4       )       )

