



"""simple docstring"""






import json
import os
import tempfile

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


if is_torch_available():
    import numpy as np
    import torch

if is_vision_available():
    from PIL import Image


def 							A__ (     UpperCamelCase__       ,							UpperCamelCase__=False       ,							UpperCamelCase__=False       ,							UpperCamelCase__=False    ):


  '''simple docstring'''


  assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

  _SCREAMING_SNAKE_CASE        =			[]
  for i in range(image_processor_tester.batch_size    ):
    if equal_resolution:
      _SCREAMING_SNAKE_CASE        =			_SCREAMING_SNAKE_CASE        =			image_processor_tester.max_resolution
    else:
      # To avoid getting image width/height 0
      _SCREAMING_SNAKE_CASE        =			image_processor_tester.min_resolution
      if getattr(UpperCamelCase__       ,							'''size_divisor'''       ,							UpperCamelCase__    ):
        # If `size_divisor` is defined, the image needs to have width/size >= `size_divisor`
        _SCREAMING_SNAKE_CASE        =			max(image_processor_tester.size_divisor       ,							UpperCamelCase__    )
      _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE        =			np.random.choice(np.arange(UpperCamelCase__       ,							image_processor_tester.max_resolution    )       ,							2    )
    image_inputs.append(
        np.random.randint(255       ,							size=(image_processor_tester.num_channels, width, height)       ,							dtype=np.uinta    )    )

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

  if torchify:
    _SCREAMING_SNAKE_CASE        =			[torch.from_numpy(UpperCamelCase__    ) for image in image_inputs]

  return image_inputs


def 							A__ (     UpperCamelCase__       ,							UpperCamelCase__=10       ,							UpperCamelCase__=10       ,							UpperCamelCase__=False       ,							UpperCamelCase__=False    ):


  '''simple docstring'''


  _SCREAMING_SNAKE_CASE        =			[]
  for i in range(image_processor_tester.num_frames    ):
    video.append(np.random.randint(255       ,							size=(image_processor_tester.num_channels, width, height)       ,							dtype=np.uinta    )    )

  if not numpify and not torchify:
    # PIL expects the channel dimension as last dimension
    _SCREAMING_SNAKE_CASE        =			[Image.fromarray(np.moveaxis(UpperCamelCase__       ,							0       ,							-1    )    ) for frame in video]

  if torchify:
    _SCREAMING_SNAKE_CASE        =			[torch.from_numpy(UpperCamelCase__    ) for frame in video]

  return video


def 							A__ (     UpperCamelCase__       ,							UpperCamelCase__=False       ,							UpperCamelCase__=False       ,							UpperCamelCase__=False    ):


  '''simple docstring'''


  assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

  _SCREAMING_SNAKE_CASE        =			[]
  for i in range(image_processor_tester.batch_size    ):
    if equal_resolution:
      _SCREAMING_SNAKE_CASE        =			_SCREAMING_SNAKE_CASE        =			image_processor_tester.max_resolution
    else:
      _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE        =			np.random.choice(
          np.arange(image_processor_tester.min_resolution       ,							image_processor_tester.max_resolution    )       ,							2    )
      _SCREAMING_SNAKE_CASE        =			prepare_video(
          image_processor_tester=UpperCamelCase__       ,							width=UpperCamelCase__       ,							height=UpperCamelCase__       ,							numpify=UpperCamelCase__       ,							torchify=UpperCamelCase__       ,							)
    video_inputs.append(UpperCamelCase__    )

  return video_inputs







class    __snake_case:
    _A       = None




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.image_processing_class(**self.image_processor_dict			)
      _SCREAMING_SNAKE_CASE        =			json.loads(image_processor.to_json_string()			)
      for key, value in self.image_processor_dict.items():
        self.assertEqual(obj[key]    ,							A_			)




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.image_processing_class(**self.image_processor_dict			)

      with tempfile.TemporaryDirectory() as tmpdirname:
        _SCREAMING_SNAKE_CASE        =			os.path.join(A_    ,							'''image_processor.json'''			)
        image_processor_first.to_json_file(A_			)
        _SCREAMING_SNAKE_CASE        =			self.image_processing_class.from_json_file(A_			)

      self.assertEqual(image_processor_second.to_dict()    ,							image_processor_first.to_dict()			)




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.image_processing_class(**self.image_processor_dict			)

      with tempfile.TemporaryDirectory() as tmpdirname:
        _SCREAMING_SNAKE_CASE        =			image_processor_first.save_pretrained(A_			)[0]
        check_json_file_has_correct_format(A_			)
        _SCREAMING_SNAKE_CASE        =			self.image_processing_class.from_pretrained(A_			)

      self.assertEqual(image_processor_second.to_dict()    ,							image_processor_first.to_dict()			)




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.image_processing_class()
      self.assertIsNotNone(A_			)




    @require_torch
    @require_vision
    def   A   (   self			):





      '''simple docstring'''



      if self.test_cast_dtype is not None:
        # Initialize image_processor
        _SCREAMING_SNAKE_CASE        =			self.image_processing_class(**self.image_processor_dict			)

        # create random PyTorch tensors
        _SCREAMING_SNAKE_CASE        =			prepare_image_inputs(self.image_processor_tester    ,							equal_resolution=A_    ,							torchify=A_			)

        _SCREAMING_SNAKE_CASE        =			image_processor(A_    ,							return_tensors='''pt'''			)
        # for layoutLM compatiblity
        self.assertEqual(encoding.pixel_values.device    ,							torch.device('''cpu'''			)			)
        self.assertEqual(encoding.pixel_values.dtype    ,							torch.floataa			)

        _SCREAMING_SNAKE_CASE        =			image_processor(A_    ,							return_tensors='''pt'''			).to(torch.floataa			)
        self.assertEqual(encoding.pixel_values.device    ,							torch.device('''cpu'''			)			)
        self.assertEqual(encoding.pixel_values.dtype    ,							torch.floataa			)

        _SCREAMING_SNAKE_CASE        =			image_processor(A_    ,							return_tensors='''pt'''			).to('''cpu'''    ,							torch.bfloataa			)
        self.assertEqual(encoding.pixel_values.device    ,							torch.device('''cpu'''			)			)
        self.assertEqual(encoding.pixel_values.dtype    ,							torch.bfloataa			)

        with self.assertRaises(A_			):
          _SCREAMING_SNAKE_CASE        =			image_processor(A_    ,							return_tensors='''pt'''			).to(torch.bfloataa    ,							'''cpu'''			)

        # Try with text + image feature
        _SCREAMING_SNAKE_CASE        =			image_processor(A_    ,							return_tensors='''pt'''			)
        encoding.update({'''input_ids''': torch.LongTensor([[1, 2, 3], [4, 5, 6]]			)}			)
        _SCREAMING_SNAKE_CASE        =			encoding.to(torch.floataa			)

        self.assertEqual(encoding.pixel_values.device    ,							torch.device('''cpu'''			)			)
        self.assertEqual(encoding.pixel_values.dtype    ,							torch.floataa			)
        self.assertEqual(encoding.input_ids.dtype    ,							torch.long			)





"""simple docstring"""






import unittest

import numpy as np
import torch

from diffusers import DDPMPipeline, DDPMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device


enable_full_determinism()




class    __snake_case(					unittest.TestCase       ):




    @property
    def   A   (   self			):





      '''simple docstring'''



      torch.manual_seed(0			)
      _SCREAMING_SNAKE_CASE        =			UNetaDModel(
          block_out_channels=(32, 64)    ,							layers_per_block=2    ,							sample_size=32    ,							in_channels=3    ,							out_channels=3    ,							down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')    ,							up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')    ,							)
      return model




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			'''cpu'''
      _SCREAMING_SNAKE_CASE        =			self.dummy_uncond_unet
      _SCREAMING_SNAKE_CASE        =			DDPMScheduler()

      _SCREAMING_SNAKE_CASE        =			DDPMPipeline(unet=A_    ,							scheduler=A_			)
      ddpm.to(A_			)
      ddpm.set_progress_bar_config(disable=A_			)

      _SCREAMING_SNAKE_CASE        =			torch.Generator(device=A_			).manual_seed(0			)
      _SCREAMING_SNAKE_CASE        =			ddpm(generator=A_    ,							num_inference_steps=2    ,							output_type='''numpy'''			).images

      _SCREAMING_SNAKE_CASE        =			torch.Generator(device=A_			).manual_seed(0			)
      _SCREAMING_SNAKE_CASE        =			ddpm(generator=A_    ,							num_inference_steps=2    ,							output_type='''numpy'''    ,							return_dict=A_			)[0]

      _SCREAMING_SNAKE_CASE        =			image[0, -3:, -3:, -1]
      _SCREAMING_SNAKE_CASE        =			image_from_tuple[0, -3:, -3:, -1]

      assert image.shape == (1, 32, 32, 3)
      _SCREAMING_SNAKE_CASE        =			np.array(
          [9.956e-01, 5.785e-01, 4.675e-01, 9.930e-01, 0.0, 1.000, 1.199e-03, 2.648e-04, 5.101e-04]			)

      assert np.abs(image_slice.flatten() - expected_slice			).max() < 1e-2
      assert np.abs(image_from_tuple_slice.flatten() - expected_slice			).max() < 1e-2




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.dummy_uncond_unet
      _SCREAMING_SNAKE_CASE        =			DDPMScheduler(prediction_type='''sample'''			)

      _SCREAMING_SNAKE_CASE        =			DDPMPipeline(unet=A_    ,							scheduler=A_			)
      ddpm.to(A_			)
      ddpm.set_progress_bar_config(disable=A_			)

      _SCREAMING_SNAKE_CASE        =			torch.manual_seed(0			)
      _SCREAMING_SNAKE_CASE        =			ddpm(generator=A_    ,							num_inference_steps=2    ,							output_type='''numpy'''			).images

      _SCREAMING_SNAKE_CASE        =			torch.manual_seed(0			)
      _SCREAMING_SNAKE_CASE        =			ddpm(generator=A_    ,							num_inference_steps=2    ,							output_type='''numpy'''			)[0]

      _SCREAMING_SNAKE_CASE        =			image[0, -3:, -3:, -1]
      _SCREAMING_SNAKE_CASE        =			image_eps[0, -3:, -3:, -1]

      assert image.shape == (1, 32, 32, 3)
      _SCREAMING_SNAKE_CASE        =			1e-2 if torch_device != '''mps''' else 3e-2
      assert np.abs(image_slice.flatten() - image_eps_slice.flatten()			).max() < tolerance




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




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			'''google/ddpm-cifar10-32'''

      _SCREAMING_SNAKE_CASE        =			UNetaDModel.from_pretrained(A_			)
      _SCREAMING_SNAKE_CASE        =			DDPMScheduler.from_pretrained(A_			)

      _SCREAMING_SNAKE_CASE        =			DDPMPipeline(unet=A_    ,							scheduler=A_			)
      ddpm.to(A_			)
      ddpm.set_progress_bar_config(disable=A_			)

      _SCREAMING_SNAKE_CASE        =			torch.manual_seed(0			)
      _SCREAMING_SNAKE_CASE        =			ddpm(generator=A_    ,							output_type='''numpy'''			).images

      _SCREAMING_SNAKE_CASE        =			image[0, -3:, -3:, -1]

      assert image.shape == (1, 32, 32, 3)
      _SCREAMING_SNAKE_CASE        =			np.array([0.4_200, 0.3_588, 0.1_939, 0.3_847, 0.3_382, 0.2_647, 0.4_155, 0.3_582, 0.3_385]			)
      assert np.abs(image_slice.flatten() - expected_slice			).max() < 1e-2
