






import unittest

import torch

from diffusers import VQModel
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()





class       __a  (   _a	,       _a	,       unittest.TestCase	):
 _lowerCAmelCase	:	int				=			VQModel
 _lowerCAmelCase	:	Dict				=			"""sample"""







 @property
 def  __lowercase (		self :       Any							,       SCREAMING_SNAKE_CASE :       int=(32, 32)						):


      '''simple docstring'''






      UpperCamelCase__      :       List[Any]	    =  4
      UpperCamelCase__      :       Optional[Any]	    =  3

      UpperCamelCase__      :       str	    =  floats_tensor((batch_size, num_channels) + sizes						).to(__lowerCamelCase						)

      return {"sample": image}







 @property
 def  __lowercase (		self :       Dict						):


      '''simple docstring'''






      return (3, 32, 32)







 @property
 def  __lowercase (		self :       Union[str, Any]						):


      '''simple docstring'''






      return (3, 32, 32)







 def  __lowercase (		self :       Dict						):


      '''simple docstring'''






      UpperCamelCase__      :       List[str]	    =  {
          """block_out_channels""": [32, 64],
          """in_channels""": 3,
          """out_channels""": 3,
          """down_block_types""": ["""DownEncoderBlock2D""", """DownEncoderBlock2D"""],
          """up_block_types""": ["""UpDecoderBlock2D""", """UpDecoderBlock2D"""],
          """latent_channels""": 3,
      }
      UpperCamelCase__      :       Union[str, Any]	    =  self.dummy_input
      return init_dict, inputs_dict







 def  __lowercase (		self :       Any						):


      '''simple docstring'''






      pass







 def  __lowercase (		self :       str						):


      '''simple docstring'''






      pass







 def  __lowercase (		self :       Tuple						):


      '''simple docstring'''






      UpperCamelCase__      :       Dict	    =  VQModel.from_pretrained("fusing/vqgan-dummy"							,       output_loading_info=__lowerCamelCase						)
      self.assertIsNotNone(__lowerCamelCase						)
      self.assertEqual(len(loading_info["missing_keys"]						)							,       0						)

      model.to(__lowerCamelCase						)
      UpperCamelCase__      :       Any	    =  model(**self.dummy_input						)

      assert image is not None, "Make sure output is not None"







 def  __lowercase (		self :       Dict						):


      '''simple docstring'''






      UpperCamelCase__      :       Optional[Any]	    =  VQModel.from_pretrained("fusing/vqgan-dummy"						)
      model.to(__lowerCamelCase						).eval()

      torch.manual_seed(0						)
      if torch.cuda.is_available():
           torch.cuda.manual_seed_all(0						)

      UpperCamelCase__      :       Any	    =  torch.randn(1							,       model.config.in_channels							,       model.config.sample_size							,       model.config.sample_size						)
      UpperCamelCase__      :       str	    =  image.to(__lowerCamelCase						)
      with torch.no_grad():
           UpperCamelCase__      :       List[Any]	    =  model(__lowerCamelCase						).sample

      UpperCamelCase__      :       Union[str, Any]	    =  output[0, -1, -3:, -3:].flatten().cpu()
      # fmt: off
      UpperCamelCase__      :       List[Any]	    =  torch.tensor([-0.0_1_5_3, -0.4_0_4_4, -0.1_8_8_0, -0.5_1_6_1, -0.2_4_1_8, -0.4_0_7_2, -0.1_6_1_2, -0.0_6_3_3, -0.0_1_4_3]						)
      # fmt: on
      self.assertTrue(torch.allclose(__lowerCamelCase							,       __lowerCamelCase							,       atol=1e-3						)						)







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 GLPNImageProcessor





class       __a  (   unittest.TestCase	):







 def __init__(		self :       Tuple							,       SCREAMING_SNAKE_CASE :       Optional[int]							,       SCREAMING_SNAKE_CASE :       List[Any]=7							,       SCREAMING_SNAKE_CASE :       str=3							,       SCREAMING_SNAKE_CASE :       Dict=18							,       SCREAMING_SNAKE_CASE :       str=30							,       SCREAMING_SNAKE_CASE :       Union[str, Any]=4_00							,       SCREAMING_SNAKE_CASE :       int=True							,       SCREAMING_SNAKE_CASE :       Any=32							,       SCREAMING_SNAKE_CASE :       int=True							,       ):


      '''simple docstring'''






      UpperCamelCase__      :       Optional[int]	    =  parent
      UpperCamelCase__      :       Dict	    =  batch_size
      UpperCamelCase__      :       Optional[Any]	    =  num_channels
      UpperCamelCase__      :       int	    =  image_size
      UpperCamelCase__      :       Dict	    =  min_resolution
      UpperCamelCase__      :       List[str]	    =  max_resolution
      UpperCamelCase__      :       Union[str, Any]	    =  do_resize
      UpperCamelCase__      :       Optional[Any]	    =  size_divisor
      UpperCamelCase__      :       int	    =  do_rescale







 def  __lowercase (		self :       Union[str, Any]						):


      '''simple docstring'''






      return {
          "do_resize": self.do_resize,
          "size_divisor": self.size_divisor,
          "do_rescale": self.do_rescale,
      }







@require_torch
@require_vision
class       __a  (   A__	,       unittest.TestCase	):
 _lowerCAmelCase	:	List[Any]				=			GLPNImageProcessor if is_vision_available() else None







 def  __lowercase (		self :       Optional[int]						):


      '''simple docstring'''






      UpperCamelCase__      :       Tuple	    =  GLPNImageProcessingTester(self						)







 @property
 def  __lowercase (		self :       int						):


      '''simple docstring'''






      return self.image_processor_tester.prepare_image_processor_dict()







 def  __lowercase (		self :       List[Any]						):


      '''simple docstring'''






      UpperCamelCase__      :       Optional[Any]	    =  self.image_processing_class(**self.image_processor_dict						)
      self.assertTrue(hasattr(SCREAMING_SNAKE_CASE							,       "do_resize"						)						)
      self.assertTrue(hasattr(SCREAMING_SNAKE_CASE							,       "size_divisor"						)						)
      self.assertTrue(hasattr(SCREAMING_SNAKE_CASE							,       "resample"						)						)
      self.assertTrue(hasattr(SCREAMING_SNAKE_CASE							,       "do_rescale"						)						)







 def  __lowercase (		self :       List[str]						):


      '''simple docstring'''






      pass







 def  __lowercase (		self :       Optional[Any]						):


      '''simple docstring'''






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

      # Test not batched input (GLPNImageProcessor doesn't support batching)
      UpperCamelCase__      :       List[Any]	    =  image_processing(image_inputs[0]							,       return_tensors="pt"						).pixel_values
      self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0						)
      self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0						)







 def  __lowercase (		self :       Any						):


      '''simple docstring'''






      UpperCamelCase__      :       Optional[Any]	    =  self.image_processing_class(**self.image_processor_dict						)
      # create random numpy tensors
      UpperCamelCase__      :       Optional[Any]	    =  prepare_image_inputs(self.image_processor_tester							,       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 (GLPNImageProcessor doesn't support batching)
      UpperCamelCase__      :       str	    =  image_processing(image_inputs[0]							,       return_tensors="pt"						).pixel_values
      self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0						)
      self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0						)







 def  __lowercase (		self :       Optional[Any]						):


      '''simple docstring'''






      UpperCamelCase__      :       int	    =  self.image_processing_class(**self.image_processor_dict						)
      # create random PyTorch tensors
      UpperCamelCase__      :       Tuple	    =  prepare_image_inputs(self.image_processor_tester							,       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 (GLPNImageProcessor doesn't support batching)
      UpperCamelCase__      :       Dict	    =  image_processing(image_inputs[0]							,       return_tensors="pt"						).pixel_values
      self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0						)
      self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0						)