

import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
	import torch

if is_vision_available():
	from PIL import Image

	from transformers import PoolFormerImageProcessor

class 					a__						(					unittest.TestCase			):


			def __init__(    self      ,      lowercase      ,      lowercase=7      ,      lowercase=3      ,      lowercase=30      ,      lowercase=400      ,      lowercase=True      ,      lowercase=None      ,      lowercase=0.9      ,      lowercase=None      ,      lowercase=True      ,      lowercase=[0.5, 0.5, 0.5]      ,      lowercase=[0.5, 0.5, 0.5]      ,      )  ->						str:
							'''simple docstring'''






							A__        =			size if size is not None else {"shortest_edge": 30}
							A__        =			crop_size if crop_size is not None else {"height": 30, "width": 30}
							A__        =			parent
							A__        =			batch_size
							A__        =			num_channels
							A__        =			min_resolution
							A__        =			max_resolution
							A__        =			do_resize_and_center_crop
							A__        =			size
							A__        =			crop_pct
							A__        =			crop_size
							A__        =			do_normalize
							A__        =			image_mean
							A__        =			image_std


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






							return {
							    "size": self.size,
							    "do_resize_and_center_crop": self.do_resize_and_center_crop,
							    "crop_pct": self.crop_pct,
							    "crop_size": self.crop_size,
							    "do_normalize": self.do_normalize,
							    "image_mean": self.image_mean,
							    "image_std": self.image_std,
							}

@require_torch
@require_vision
class 					a__						(					snake_case							,	unittest.TestCase			):
			__lowerCamelCase        =       PoolFormerImageProcessor if is_vision_available() else None


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






							A__        =			PoolFormerImageProcessingTester(self       )


			@property
			def      UpperCamelCase		(    self       )  ->						List[str]:
							'''simple docstring'''






							return self.image_processor_tester.prepare_image_processor_dict()


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






							A__        =			self.image_processing_class(**self.image_processor_dict       )
							self.assertTrue(hasattr(lowercase      ,      "do_resize_and_center_crop"       )       )
							self.assertTrue(hasattr(lowercase      ,      "size"       )       )
							self.assertTrue(hasattr(lowercase      ,      "crop_pct"       )       )
							self.assertTrue(hasattr(lowercase      ,      "do_normalize"       )       )
							self.assertTrue(hasattr(lowercase      ,      "image_mean"       )       )
							self.assertTrue(hasattr(lowercase      ,      "image_std"       )       )


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






							A__        =			self.image_processing_class.from_dict(self.image_processor_dict       )
							self.assertEqual(image_processor.size      ,      {"shortest_edge": 30}       )
							self.assertEqual(image_processor.crop_size      ,      {"height": 30, "width": 30}       )

							A__        =			self.image_processing_class.from_dict(self.image_processor_dict      ,      size=42      ,      crop_size=84       )
							self.assertEqual(image_processor.size      ,      {"shortest_edge": 42}       )
							self.assertEqual(image_processor.crop_size      ,      {"height": 84, "width": 84}       )


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






							pass


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






							A__        =			self.image_processing_class(**self.image_processor_dict       )
							# create random PIL images
							A__        =			prepare_image_inputs(self.image_processor_tester      ,      equal_resolution=lowercase       )
							for image in image_inputs:
											self.assertIsInstance(lowercase      ,      Image.Image       )

							# Test not batched input
							A__        =			image_processing(image_inputs[0]      ,      return_tensors="pt"       ).pixel_values

							self.assertEqual(
							    encoded_images.shape      ,      (
							        1,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )      ,      )

							# Test batched
							A__        =			image_processing(lowercase      ,      return_tensors="pt"       ).pixel_values
							self.assertEqual(
							    encoded_images.shape      ,      (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )      ,      )


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






							A__        =			self.image_processing_class(**self.image_processor_dict       )
							# create random numpy tensors
							A__        =			prepare_image_inputs(self.image_processor_tester      ,      equal_resolution=lowercase      ,      numpify=lowercase       )
							for image in image_inputs:
											self.assertIsInstance(lowercase      ,      np.ndarray       )

							# Test not batched input
							A__        =			image_processing(image_inputs[0]      ,      return_tensors="pt"       ).pixel_values
							self.assertEqual(
							    encoded_images.shape      ,      (
							        1,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )      ,      )

							# Test batched
							A__        =			image_processing(lowercase      ,      return_tensors="pt"       ).pixel_values
							self.assertEqual(
							    encoded_images.shape      ,      (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )      ,      )


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






							A__        =			self.image_processing_class(**self.image_processor_dict       )
							# create random PyTorch tensors
							A__        =			prepare_image_inputs(self.image_processor_tester      ,      equal_resolution=lowercase      ,      torchify=lowercase       )
							for image in image_inputs:
											self.assertIsInstance(lowercase      ,      torch.Tensor       )

							# Test not batched input
							A__        =			image_processing(image_inputs[0]      ,      return_tensors="pt"       ).pixel_values
							self.assertEqual(
							    encoded_images.shape      ,      (
							        1,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )      ,      )

							# Test batched
							A__        =			image_processing(lowercase      ,      return_tensors="pt"       ).pixel_values
							self.assertEqual(
							    encoded_images.shape      ,      (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )      ,      )




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

