


'''simple docstring'''
import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
 from PIL import Image

 from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
 import torch

if is_tf_available():
 import tensorflow as tf

@require_vision
@require_torchvision
class 						_lowerCamelCase					(    unittest.TestCase				):




 '''simple docstring'''


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		str       =			tempfile.mkdtemp()
  __A  :		str       =			SamImageProcessor()
  __A  :		List[str]       =			SamProcessor(__lowercase						)
  processor.save_pretrained(self.tmpdirname						)


 def  snake_case__   (      self      ,      **__lowercase						):

  """simple docstring"""







  return AutoProcessor.from_pretrained(self.tmpdirname      ,      **__lowercase						).image_processor


 def  snake_case__   (      self						):

  """simple docstring"""







  shutil.rmtree(self.tmpdirname						)


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Union[str, Any]       =			[np.random.randint(255      ,      size=(3, 30, 400)      ,      dtype=np.uinta						)]

  __A  :		Any       =			[Image.fromarray(np.moveaxis(__lowercase      ,      0      ,      -1						)						) for x in image_inputs]

  return image_inputs


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		int       =			SamProcessor(image_processor=self.get_image_processor()						)
  processor.save_pretrained(self.tmpdirname						)

  __A  :		Dict       =			self.get_image_processor(do_normalize=__lowercase      ,      padding_value=1.0						)

  __A  :		Union[str, Any]       =			SamProcessor.from_pretrained(self.tmpdirname      ,      do_normalize=__lowercase      ,      padding_value=1.0						)

  self.assertEqual(processor.image_processor.to_json_string()      ,      image_processor_add_kwargs.to_json_string()						)
  self.assertIsInstance(processor.image_processor      ,      __lowercase						)


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Optional[Any]       =			self.get_image_processor()

  __A  :		Any       =			SamProcessor(image_processor=__lowercase						)

  __A  :		List[str]       =			self.prepare_image_inputs()

  __A  :		List[str]       =			image_processor(__lowercase      ,      return_tensors='np'						)
  __A  :		Any       =			processor(images=__lowercase      ,      return_tensors='np'						)

  input_feat_extract.pop('original_sizes'						)  # pop original_sizes as it is popped in the processor
  input_feat_extract.pop('reshaped_input_sizes'						)  # pop original_sizes as it is popped in the processor

  for key in input_feat_extract.keys():
   self.assertAlmostEqual(input_feat_extract[key].sum()      ,      input_processor[key].sum()      ,      delta=1E-2						)







 @require_torch
 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Optional[int]       =			self.get_image_processor()

  __A  :		List[Any]       =			SamProcessor(image_processor=__lowercase						)
  __A  :		str       =			[torch.ones((1, 3, 5, 5)						)]

  __A  :		List[Any]       =			[[1_764, 2_646]]

  __A  :		List[str]       =			[[683, 1_024]]
  __A  :		Optional[int]       =			processor.post_process_masks(__lowercase      ,      __lowercase      ,      __lowercase						)
  self.assertEqual(masks[0].shape      ,      (1, 3, 1_764, 2_646)						)

  __A  :		Union[str, Any]       =			processor.post_process_masks(
      __lowercase      ,      torch.tensor(__lowercase						)      ,      torch.tensor(__lowercase						)						)
  self.assertEqual(masks[0].shape      ,      (1, 3, 1_764, 2_646)						)

  # should also work with np
  __A  :		Optional[Any]       =			[np.ones((1, 3, 5, 5)						)]
  __A  :		List[Any]       =			processor.post_process_masks(__lowercase      ,      np.array(__lowercase						)      ,      np.array(__lowercase						)						)

  self.assertEqual(masks[0].shape      ,      (1, 3, 1_764, 2_646)						)

  __A  :		Union[str, Any]       =			[[1, 0], [0, 1]]
  with self.assertRaises(__lowercase						):
   __A  :		Dict       =			processor.post_process_masks(__lowercase      ,      np.array(__lowercase						)      ,      np.array(__lowercase						)						)


@require_vision
@require_tf
class 						_lowerCamelCase					(    unittest.TestCase				):




 '''simple docstring'''


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		str       =			tempfile.mkdtemp()
  __A  :		Tuple       =			SamImageProcessor()
  __A  :		Dict       =			SamProcessor(__lowercase						)
  processor.save_pretrained(self.tmpdirname						)


 def  snake_case__   (      self      ,      **__lowercase						):

  """simple docstring"""







  return AutoProcessor.from_pretrained(self.tmpdirname      ,      **__lowercase						).image_processor


 def  snake_case__   (      self						):

  """simple docstring"""







  shutil.rmtree(self.tmpdirname						)


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Tuple       =			[np.random.randint(255      ,      size=(3, 30, 400)      ,      dtype=np.uinta						)]

  __A  :		Union[str, Any]       =			[Image.fromarray(np.moveaxis(__lowercase      ,      0      ,      -1						)						) for x in image_inputs]

  return image_inputs


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		str       =			SamProcessor(image_processor=self.get_image_processor()						)
  processor.save_pretrained(self.tmpdirname						)

  __A  :		List[Any]       =			self.get_image_processor(do_normalize=__lowercase      ,      padding_value=1.0						)

  __A  :		Dict       =			SamProcessor.from_pretrained(self.tmpdirname      ,      do_normalize=__lowercase      ,      padding_value=1.0						)

  self.assertEqual(processor.image_processor.to_json_string()      ,      image_processor_add_kwargs.to_json_string()						)
  self.assertIsInstance(processor.image_processor      ,      __lowercase						)


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		str       =			self.get_image_processor()

  __A  :		List[str]       =			SamProcessor(image_processor=__lowercase						)

  __A  :		Tuple       =			self.prepare_image_inputs()

  __A  :		str       =			image_processor(__lowercase      ,      return_tensors='np'						)
  __A  :		Union[str, Any]       =			processor(images=__lowercase      ,      return_tensors='np'						)

  input_feat_extract.pop('original_sizes'						)  # pop original_sizes as it is popped in the processor
  input_feat_extract.pop('reshaped_input_sizes'						)  # pop reshaped_input_sizes as it is popped in the processor

  for key in input_feat_extract.keys():
   self.assertAlmostEqual(input_feat_extract[key].sum()      ,      input_processor[key].sum()      ,      delta=1E-2						)







 @require_tf
 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Tuple       =			self.get_image_processor()

  __A  :		Any       =			SamProcessor(image_processor=__lowercase						)
  __A  :		int       =			[tf.ones((1, 3, 5, 5)						)]

  __A  :		str       =			[[1_764, 2_646]]

  __A  :		Optional[int]       =			[[683, 1_024]]
  __A  :		int       =			processor.post_process_masks(__lowercase      ,      __lowercase      ,      __lowercase      ,      return_tensors='tf'						)
  self.assertEqual(masks[0].shape      ,      (1, 3, 1_764, 2_646)						)

  __A  :		Any       =			processor.post_process_masks(
      __lowercase      ,      tf.convert_to_tensor(__lowercase						)      ,      tf.convert_to_tensor(__lowercase						)      ,      return_tensors='tf'      ,      )
  self.assertEqual(masks[0].shape      ,      (1, 3, 1_764, 2_646)						)

  # should also work with np
  __A  :		List[str]       =			[np.ones((1, 3, 5, 5)						)]
  __A  :		Dict       =			processor.post_process_masks(
      __lowercase      ,      np.array(__lowercase						)      ,      np.array(__lowercase						)      ,      return_tensors='tf'						)

  self.assertEqual(masks[0].shape      ,      (1, 3, 1_764, 2_646)						)

  __A  :		Any       =			[[1, 0], [0, 1]]
  with self.assertRaises(tf.errors.InvalidArgumentError						):
   __A  :		Optional[int]       =			processor.post_process_masks(
       __lowercase      ,      np.array(__lowercase						)      ,      np.array(__lowercase						)      ,      return_tensors='tf'						)


@require_vision
@require_torchvision
class 						_lowerCamelCase					(    unittest.TestCase				):




 '''simple docstring'''


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		str       =			tempfile.mkdtemp()
  __A  :		Tuple       =			SamImageProcessor()
  __A  :		int       =			SamProcessor(__lowercase						)
  processor.save_pretrained(self.tmpdirname						)


 def  snake_case__   (      self      ,      **__lowercase						):

  """simple docstring"""







  return AutoProcessor.from_pretrained(self.tmpdirname      ,      **__lowercase						).image_processor


 def  snake_case__   (      self						):

  """simple docstring"""







  shutil.rmtree(self.tmpdirname						)


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Union[str, Any]       =			[np.random.randint(255      ,      size=(3, 30, 400)      ,      dtype=np.uinta						)]

  __A  :		List[str]       =			[Image.fromarray(np.moveaxis(__lowercase      ,      0      ,      -1						)						) for x in image_inputs]

  return image_inputs


 @is_pt_tf_cross_test
 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		int       =			self.get_image_processor()

  __A  :		List[Any]       =			SamProcessor(image_processor=__lowercase						)
  __A  :		Optional[int]       =			np.random.randint(0      ,      2      ,      size=(1, 3, 5, 5)						).astype(np.floataa						)
  __A  :		Dict       =			[tf.convert_to_tensor(__lowercase						)]
  __A  :		Optional[Any]       =			[torch.tensor(__lowercase						)]

  __A  :		int       =			[[1_764, 2_646]]

  __A  :		Tuple       =			[[683, 1_024]]
  __A  :		Optional[int]       =			processor.post_process_masks(
      __lowercase      ,      __lowercase      ,      __lowercase      ,      return_tensors='tf'						)
  __A  :		Tuple       =			processor.post_process_masks(
      __lowercase      ,      __lowercase      ,      __lowercase      ,      return_tensors='pt'						)

  self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()						)						)







 @is_pt_tf_cross_test
 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Dict       =			self.get_image_processor()

  __A  :		Optional[int]       =			SamProcessor(image_processor=__lowercase						)

  __A  :		Optional[Any]       =			self.prepare_image_inputs()

  __A  :		List[str]       =			image_processor(__lowercase      ,      return_tensors='pt'						)['pixel_values'].numpy()
  __A  :		Dict       =			processor(images=__lowercase      ,      return_tensors='pt'						)['pixel_values'].numpy()

  __A  :		Tuple       =			image_processor(__lowercase      ,      return_tensors='tf'						)['pixel_values'].numpy()
  __A  :		Union[str, Any]       =			processor(images=__lowercase      ,      return_tensors='tf'						)['pixel_values'].numpy()

  self.assertTrue(np.allclose(__lowercase      ,      __lowercase						)						)
  self.assertTrue(np.allclose(__lowercase      ,      __lowercase						)						)
  self.assertTrue(np.allclose(__lowercase      ,      __lowercase						)						)










'''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 						_lowerCamelCase					(    unittest.TestCase				):




 '''simple docstring'''


 def  snake_case__   (      self						):

  """simple docstring"""







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


 @property
 def  snake_case__   (      self						):

  """simple docstring"""







  torch.manual_seed(0						)
  __A  :		Optional[int]       =			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  snake_case__   (      self						):

  """simple docstring"""







  torch.manual_seed(0						)
  __A  :		Dict       =			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  snake_case__   (      self						):

  """simple docstring"""







  torch.manual_seed(0						)
  __A  :		Union[str, 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  :		Union[str, 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  snake_case__   (      self						):

  """simple docstring"""







  __A  :		str       =			'cpu'  # ensure determinism for the device-dependent torch.Generator
  __A  :		int       =			Mel(
      x_res=self.dummy_unet.config.sample_size[1]      ,      y_res=self.dummy_unet.config.sample_size[0]      ,      )

  __A  :		Tuple       =			DDPMScheduler()
  __A  :		Optional[int]       =			AudioDiffusionPipeline(vqvae=__lowercase      ,      unet=self.dummy_unet      ,      mel=__lowercase      ,      scheduler=__lowercase						)
  __A  :		Dict       =			pipe.to(__lowercase						)
  pipe.set_progress_bar_config(disable=__lowercase						)

  __A  :		Tuple       =			torch.Generator(device=__lowercase						).manual_seed(42						)
  __A  :		Tuple       =			pipe(generator=__lowercase      ,      steps=4						)
  __A  :		Union[str, Any]       =			output.audios[0]
  __A  :		Dict       =			output.images[0]

  __A  :		Optional[Any]       =			torch.Generator(device=__lowercase						).manual_seed(42						)
  __A  :		Optional[int]       =			pipe(generator=__lowercase      ,      steps=4      ,      return_dict=__lowercase						)
  __A  :		int       =			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  :		Optional[int]       =			np.frombuffer(image.tobytes()      ,      dtype='uint8'						)[:10]
  __A  :		List[Any]       =			np.frombuffer(image_from_tuple.tobytes()      ,      dtype='uint8'						)[:10]
  __A  :		Optional[Any]       =			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  :		Optional[int]       =			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  :		Optional[Any]       =			DDIMScheduler()
  __A  :		List[Any]       =			self.dummy_vqvae_and_unet
  __A  :		int       =			AudioDiffusionPipeline(
      vqvae=self.dummy_vqvae_and_unet[0]      ,      unet=dummy_vqvae_and_unet[1]      ,      mel=__lowercase      ,      scheduler=__lowercase						)
  __A  :		Dict       =			pipe.to(__lowercase						)
  pipe.set_progress_bar_config(disable=__lowercase						)

  np.random.seed(0						)
  __A  :		Dict       =			np.random.uniform(-1      ,      1      ,      ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,)						)
  __A  :		Dict       =			torch.Generator(device=__lowercase						).manual_seed(42						)
  __A  :		List[Any]       =			pipe(raw_audio=__lowercase      ,      generator=__lowercase      ,      start_step=5      ,      steps=10						)
  __A  :		Tuple       =			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  :		int       =			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  :		Any       =			self.dummy_unet_condition
  __A  :		Optional[int]       =			AudioDiffusionPipeline(
      vqvae=self.dummy_vqvae_and_unet[0]      ,      unet=__lowercase      ,      mel=__lowercase      ,      scheduler=__lowercase						)
  __A  :		Union[str, Any]       =			pipe.to(__lowercase						)
  pipe.set_progress_bar_config(disable=__lowercase						)

  np.random.seed(0						)
  __A  :		Dict       =			torch.rand((1, 1, 10)						)
  __A  :		Any       =			pipe(generator=__lowercase      ,      encoding=__lowercase						)
  __A  :		Union[str, Any]       =			output.images[0]
  __A  :		List[Any]       =			np.frombuffer(image.tobytes()      ,      dtype='uint8'						)[:10]
  __A  :		int       =			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 						_lowerCamelCase					(    unittest.TestCase				):




 '''simple docstring'''


 def  snake_case__   (      self						):

  """simple docstring"""







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







 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Union[str, Any]       =			torch_device

  __A  :		Union[str, Any]       =			DiffusionPipeline.from_pretrained('teticio/audio-diffusion-ddim-256'						)
  __A  :		Dict       =			pipe.to(__lowercase						)
  pipe.set_progress_bar_config(disable=__lowercase						)

  __A  :		Any       =			torch.Generator(device=__lowercase						).manual_seed(42						)
  __A  :		Optional[Any]       =			pipe(generator=__lowercase						)
  __A  :		Union[str, Any]       =			output.audios[0]
  __A  :		List[Any]       =			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  :		Union[str, Any]       =			np.frombuffer(image.tobytes()      ,      dtype='uint8'						)[:10]
  __A  :		Optional[Any]       =			np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26]						)

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






