






'''simple docstring'''







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


if is_torch_available():
		import torch

if is_vision_available():
		from PIL import Image

		from transformers import ChineseCLIPImageProcessor





class 	SCREAMING_SNAKE_CASE					(					unittest.TestCase       ):






	def __init__( self      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=7  ,						__SCREAMING_SNAKE_CASE      :			str=3  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=18  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=30  ,						__SCREAMING_SNAKE_CASE      :			int=400  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=True  ,						__SCREAMING_SNAKE_CASE      :			str=None  ,						__SCREAMING_SNAKE_CASE      :			Any=True  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=None  ,						__SCREAMING_SNAKE_CASE      :			Dict=True  ,						__SCREAMING_SNAKE_CASE      :			Tuple=[0.4814_5466, 0.457_8275, 0.4082_1073]  ,						__SCREAMING_SNAKE_CASE      :			str=[0.2686_2954, 0.2613_0258, 0.2757_7711]  ,						__SCREAMING_SNAKE_CASE      :			Dict=True  ,						)			->			int:
					a_		:  Any											=      size if size is not None else {'''height''': 224, '''width''': 224}
					a_		:  List[str]											=      crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
					a_		:  Dict											=      parent
					a_		:  str											=      batch_size
					a_		:  int											=      num_channels
					a_		:  Dict											=      image_size
					a_		:  Tuple											=      min_resolution
					a_		:  int											=      max_resolution
					a_		:  List[Any]											=      do_resize
					a_		:  Union[str, Any]											=      size
					a_		:  Union[str, Any]											=      do_center_crop
					a_		:  str											=      crop_size
					a_		:  str											=      do_normalize
					a_		:  Optional[int]											=      image_mean
					a_		:  Dict											=      image_std
					a_		:  int											=      do_convert_rgb






	def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]					)			->			Optional[Any]:
					return {
					    "do_resize": self.do_resize,
					    "size": self.size,
					    "do_center_crop": self.do_center_crop,
					    "crop_size": self.crop_size,
					    "do_normalize": self.do_normalize,
					    "image_mean": self.image_mean,
					    "image_std": self.image_std,
					    "do_convert_rgb": self.do_convert_rgb,
					}







	def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=False  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=False  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=False					)			->			Any:
					assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

					if equal_resolution:
									a_		:  Tuple											=      []
									for i in range(self.batch_size					):
													image_inputs.append(
													    np.random.randint(
													        255  ,						size=(self.num_channels, self.max_resolution, self.max_resolution)  ,						dtype=np.uinta					)					)
					else:
									a_		:  Union[str, Any]											=      []
									for i in range(self.batch_size					):
													a_				,			a_		:  Tuple											=      np.random.choice(np.arange(self.min_resolution  ,						self.max_resolution					)  ,						2					)
													image_inputs.append(np.random.randint(255  ,						size=(self.num_channels, width, height)  ,						dtype=np.uinta					)					)

					if not numpify and not torchify:
									# PIL expects the channel dimension as last dimension
									a_		:  str											=      [Image.fromarray(np.moveaxis(__SCREAMING_SNAKE_CASE  ,						0  ,						-1					)					) for x in image_inputs]

					if torchify:
									a_		:  List[Any]											=      [torch.from_numpy(__SCREAMING_SNAKE_CASE					) for x in image_inputs]

					return image_inputs





@require_torch
@require_vision
class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_	,   unittest.TestCase       ):
	snake_case__  =				ChineseCLIPImageProcessor if is_vision_available() else None






	def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			int:
					a_		:  int											=      ChineseCLIPImageProcessingTester(self  ,						do_center_crop=__SCREAMING_SNAKE_CASE					)






	@property
	def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			int:
					return self.image_processor_tester.prepare_image_processor_dict()






	def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			Optional[Any]:
					a_		:  Any											=      self.image_processing_class(**self.image_processor_dict					)
					self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,						'''do_resize'''					)					)
					self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,						'''size'''					)					)
					self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,						'''do_center_crop'''					)					)
					self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,						'''center_crop'''					)					)
					self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,						'''do_normalize'''					)					)
					self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,						'''image_mean'''					)					)
					self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,						'''image_std'''					)					)
					self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,						'''do_convert_rgb'''					)					)






	def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			List[Any]:
					a_		:  Union[str, Any]											=      self.image_processing_class.from_dict(self.image_processor_dict					)
					self.assertEqual(image_processor.size  ,						{'''height''': 224, '''width''': 224}					)
					self.assertEqual(image_processor.crop_size  ,						{'''height''': 18, '''width''': 18}					)

					a_		:  Tuple											=      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 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			Dict:
					pass






	def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			Union[str, Any]:
					a_		:  Any											=      self.image_processing_class(**self.image_processor_dict					)
					# create random PIL images
					a_		:  Optional[Any]											=      self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE					)
					for image in image_inputs:
									self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						Image.Image					)

					# Test not batched input
					a_		:  Union[str, Any]											=      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_		:  Optional[Any]											=      image_processing(__SCREAMING_SNAKE_CASE  ,						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 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			List[str]:
					a_		:  List[str]											=      self.image_processing_class(**self.image_processor_dict					)
					# create random numpy tensors
					a_		:  int											=      self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE  ,						numpify=__SCREAMING_SNAKE_CASE					)
					for image in image_inputs:
									self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						np.ndarray					)

					# Test not batched input
					a_		:  Dict											=      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_		:  Optional[Any]											=      image_processing(__SCREAMING_SNAKE_CASE  ,						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 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			int:
					a_		:  Dict											=      self.image_processing_class(**self.image_processor_dict					)
					# create random PyTorch tensors
					a_		:  Any											=      self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE  ,						torchify=__SCREAMING_SNAKE_CASE					)
					for image in image_inputs:
									self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						torch.Tensor					)

					# Test not batched input
					a_		:  Optional[Any]											=      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_		:  str											=      image_processing(__SCREAMING_SNAKE_CASE  ,						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'''],
					    )  ,						)





@require_torch
@require_vision
class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_	,   unittest.TestCase       ):
	snake_case__  =				ChineseCLIPImageProcessor if is_vision_available() else None






	def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]					)			->			Any:
					a_		:  Dict											=      ChineseCLIPImageProcessingTester(self  ,						num_channels=4  ,						do_center_crop=__SCREAMING_SNAKE_CASE					)
					a_		:  str											=      3






	@property
	def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]					)			->			List[Any]:
					return self.image_processor_tester.prepare_image_processor_dict()






	def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			int:
					a_		:  Union[str, Any]											=      self.image_processing_class(**self.image_processor_dict					)
					self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,						'''do_resize'''					)					)
					self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,						'''size'''					)					)
					self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,						'''do_center_crop'''					)					)
					self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,						'''center_crop'''					)					)
					self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,						'''do_normalize'''					)					)
					self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,						'''image_mean'''					)					)
					self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,						'''image_std'''					)					)
					self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,						'''do_convert_rgb'''					)					)






	def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			List[Any]:
					pass







	def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			Tuple:
					a_		:  Optional[Any]											=      self.image_processing_class(**self.image_processor_dict					)
					# create random PIL images
					a_		:  List[str]											=      self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE					)
					for image in image_inputs:
									self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						Image.Image					)

					# Test not batched input
					a_		:  List[str]											=      image_processing(image_inputs[0]  ,						return_tensors='''pt'''					).pixel_values
					self.assertEqual(
					    encoded_images.shape  ,						(
					        1,
					        self.expected_encoded_image_num_channels,
					        self.image_processor_tester.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )  ,						)

					# Test batched
					a_		:  Tuple											=      image_processing(__SCREAMING_SNAKE_CASE  ,						return_tensors='''pt'''					).pixel_values
					self.assertEqual(
					    encoded_images.shape  ,						(
					        self.image_processor_tester.batch_size,
					        self.expected_encoded_image_num_channels,
					        self.image_processor_tester.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )  ,						)












'''simple docstring'''







import gc
import unittest

import numpy as np
import torch

from diffusers import (
    AudioDiffusionPipeline,
    AutoencoderKL,
    DDIMScheduler,
    DDPMScheduler,
    DiffusionPipeline,
    Mel,
    UNetaDConditionModel,
    UNetaDModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()





class 	SCREAMING_SNAKE_CASE					(					unittest.TestCase       ):






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			List[Any]:
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()






			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			List[Any]:
							torch.manual_seed(0					)
							a_		:  Tuple											=      UNetaDModel(
							    sample_size=(32, 64)  ,						in_channels=1  ,						out_channels=1  ,						layers_per_block=2  ,						block_out_channels=(128, 128)  ,						down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''')  ,						up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''')  ,						)
							return model






			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			List[Any]:
							torch.manual_seed(0					)
							a_		:  Any											=      UNetaDConditionModel(
							    sample_size=(64, 32)  ,						in_channels=1  ,						out_channels=1  ,						layers_per_block=2  ,						block_out_channels=(128, 128)  ,						down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''')  ,						up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''')  ,						cross_attention_dim=10  ,						)
							return model






			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			List[str]:
							torch.manual_seed(0					)
							a_		:  List[Any]											=      AutoencoderKL(
							    sample_size=(128, 64)  ,						in_channels=1  ,						out_channels=1  ,						latent_channels=1  ,						layers_per_block=2  ,						block_out_channels=(128, 128)  ,						down_block_types=('''DownEncoderBlock2D''', '''DownEncoderBlock2D''')  ,						up_block_types=('''UpDecoderBlock2D''', '''UpDecoderBlock2D''')  ,						)
							a_		:  List[Any]											=      UNetaDModel(
							    sample_size=(64, 32)  ,						in_channels=1  ,						out_channels=1  ,						layers_per_block=2  ,						block_out_channels=(128, 128)  ,						down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''')  ,						up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''')  ,						)
							return vqvae, unet







			@slow
			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			Union[str, Any]:
							a_		:  Dict											=      '''cpu'''  # ensure determinism for the device-dependent torch.Generator
							a_		:  Union[str, Any]											=      Mel(
							    x_res=self.dummy_unet.config.sample_size[1]  ,						y_res=self.dummy_unet.config.sample_size[0]  ,						)

							a_		:  Any											=      DDPMScheduler()
							a_		:  str											=      AudioDiffusionPipeline(vqvae=__SCREAMING_SNAKE_CASE  ,						unet=self.dummy_unet  ,						mel=__SCREAMING_SNAKE_CASE  ,						scheduler=__SCREAMING_SNAKE_CASE					)
							a_		:  List[Any]											=      pipe.to(__SCREAMING_SNAKE_CASE					)
							pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

							a_		:  Optional[int]											=      torch.Generator(device=__SCREAMING_SNAKE_CASE					).manual_seed(42					)
							a_		:  List[str]											=      pipe(generator=__SCREAMING_SNAKE_CASE  ,						steps=4					)
							a_		:  List[Any]											=      output.audios[0]
							a_		:  Dict											=      output.images[0]

							a_		:  Dict											=      torch.Generator(device=__SCREAMING_SNAKE_CASE					).manual_seed(42					)
							a_		:  Optional[Any]											=      pipe(generator=__SCREAMING_SNAKE_CASE  ,						steps=4  ,						return_dict=__SCREAMING_SNAKE_CASE					)
							a_		:  Union[str, Any]											=      output[0][0]

							assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length)
							assert (
							    image.height == self.dummy_unet.config.sample_size[0]
							    and image.width == self.dummy_unet.config.sample_size[1]
							)
							a_		:  Dict											=      np.frombuffer(image.tobytes()  ,						dtype='''uint8'''					)[:10]
							a_		:  str											=      np.frombuffer(image_from_tuple.tobytes()  ,						dtype='''uint8'''					)[:10]
							a_		:  List[str]											=      np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127]					)

							assert np.abs(image_slice.flatten() - expected_slice					).max() == 0
							assert np.abs(image_from_tuple_slice.flatten() - expected_slice					).max() == 0

							a_		:  str											=      Mel(
							    x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1]  ,						y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0]  ,						)

							a_		:  int											=      DDIMScheduler()
							a_		:  Dict											=      self.dummy_vqvae_and_unet
							a_		:  List[str]											=      AudioDiffusionPipeline(
							    vqvae=self.dummy_vqvae_and_unet[0]  ,						unet=dummy_vqvae_and_unet[1]  ,						mel=__SCREAMING_SNAKE_CASE  ,						scheduler=__SCREAMING_SNAKE_CASE					)
							a_		:  Any											=      pipe.to(__SCREAMING_SNAKE_CASE					)
							pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

							np.random.seed(0					)
							a_		:  List[str]											=      np.random.uniform(-1  ,						1  ,						((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,)					)
							a_		:  int											=      torch.Generator(device=__SCREAMING_SNAKE_CASE					).manual_seed(42					)
							a_		:  int											=      pipe(raw_audio=__SCREAMING_SNAKE_CASE  ,						generator=__SCREAMING_SNAKE_CASE  ,						start_step=5  ,						steps=10					)
							a_		:  List[str]											=      output.images[0]

							assert (
							    image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0]
							    and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1]
							)
							a_		:  Optional[Any]											=      np.frombuffer(image.tobytes()  ,						dtype='''uint8'''					)[:10]
							a_		:  List[str]											=      np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121]					)

							assert np.abs(image_slice.flatten() - expected_slice					).max() == 0

							a_		:  List[str]											=      self.dummy_unet_condition
							a_		:  Dict											=      AudioDiffusionPipeline(
							    vqvae=self.dummy_vqvae_and_unet[0]  ,						unet=__SCREAMING_SNAKE_CASE  ,						mel=__SCREAMING_SNAKE_CASE  ,						scheduler=__SCREAMING_SNAKE_CASE					)
							a_		:  int											=      pipe.to(__SCREAMING_SNAKE_CASE					)
							pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

							np.random.seed(0					)
							a_		:  Any											=      torch.rand((1, 1, 10)					)
							a_		:  Tuple											=      pipe(generator=__SCREAMING_SNAKE_CASE  ,						encoding=__SCREAMING_SNAKE_CASE					)
							a_		:  Union[str, Any]											=      output.images[0]
							a_		:  Dict											=      np.frombuffer(image.tobytes()  ,						dtype='''uint8'''					)[:10]
							a_		:  List[str]											=      np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111]					)

							assert np.abs(image_slice.flatten() - expected_slice					).max() == 0





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






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			Any:
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()







			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			Any:
							a_		:  Any											=      torch_device

							a_		:  Optional[int]											=      DiffusionPipeline.from_pretrained('''teticio/audio-diffusion-ddim-256'''					)
							a_		:  Dict											=      pipe.to(__SCREAMING_SNAKE_CASE					)
							pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

							a_		:  str											=      torch.Generator(device=__SCREAMING_SNAKE_CASE					).manual_seed(42					)
							a_		:  List[str]											=      pipe(generator=__SCREAMING_SNAKE_CASE					)
							a_		:  Union[str, Any]											=      output.audios[0]
							a_		:  Tuple											=      output.images[0]

							assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length)
							assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1]
							a_		:  str											=      np.frombuffer(image.tobytes()  ,						dtype='''uint8'''					)[:10]
							a_		:  Tuple											=      np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26]					)

							assert np.abs(image_slice.flatten() - expected_slice					).max() == 0




