'''simple docstring'''





import unittest

import numpy as np

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

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 DPTImageProcessor






class 				UpperCAmelCase (       unittest.TestCase ):




    '''simple docstring'''







    def __init__(       self    ,							lowercase__    ,							lowercase__=7    ,							lowercase__=3    ,							lowercase__=18    ,							lowercase__=30    ,							lowercase__=400    ,							lowercase__=True    ,							lowercase__=None    ,							lowercase__=True    ,							lowercase__=[0.5, 0.5, 0.5]    ,							lowercase__=[0.5, 0.5, 0.5]    ,							)							-> List[Any]:
         SCREAMING_SNAKE_CASE					:			Tuple    	=       size if size is not None else {'height': 18, 'width': 18}
         SCREAMING_SNAKE_CASE					:			Tuple    	=       parent
         SCREAMING_SNAKE_CASE					:			List[str]    	=       batch_size
         SCREAMING_SNAKE_CASE					:			Any    	=       num_channels
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       image_size
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       min_resolution
         SCREAMING_SNAKE_CASE					:			str    	=       max_resolution
         SCREAMING_SNAKE_CASE					:			Any    	=       do_resize
         SCREAMING_SNAKE_CASE					:			int    	=       size
         SCREAMING_SNAKE_CASE					:			Any    	=       do_normalize
         SCREAMING_SNAKE_CASE					:			List[str]    	=       image_mean
         SCREAMING_SNAKE_CASE					:			Dict    	=       image_std






    def       _UpperCamelCase    (       self )							-> Optional[Any]:
         return {
             "image_mean": self.image_mean,
             "image_std": self.image_std,
             "do_normalize": self.do_normalize,
             "do_resize": self.do_resize,
             "size": self.size,
         }






@require_torch
@require_vision
class 				UpperCAmelCase (       _SCREAMING_SNAKE_CASE   ,    unittest.TestCase ):




    '''simple docstring'''







    snake_case__							:   str							=						DPTImageProcessor if is_vision_available() else None






    def       _UpperCamelCase    (       self )							-> str:
         SCREAMING_SNAKE_CASE					:			Tuple    	=       DPTImageProcessingTester(self )






    @property
    def       _UpperCamelCase    (       self )							-> int:
         return self.image_processor_tester.prepare_image_processor_dict()






    def       _UpperCamelCase    (       self )							-> str:
         SCREAMING_SNAKE_CASE					:			str    	=       self.image_processing_class(**self.image_processor_dict )
         self.assertTrue(hasattr(lowercase__    ,							'image_mean' ) )
         self.assertTrue(hasattr(lowercase__    ,							'image_std' ) )
         self.assertTrue(hasattr(lowercase__    ,							'do_normalize' ) )
         self.assertTrue(hasattr(lowercase__    ,							'do_resize' ) )
         self.assertTrue(hasattr(lowercase__    ,							'size' ) )






    def       _UpperCamelCase    (       self )							-> List[str]:
         SCREAMING_SNAKE_CASE					:			int    	=       self.image_processing_class.from_dict(self.image_processor_dict )
         self.assertEqual(image_processor.size    ,							{'height': 18, 'width': 18} )

         SCREAMING_SNAKE_CASE					:			Any    	=       self.image_processing_class.from_dict(self.image_processor_dict    ,							size=42 )
         self.assertEqual(image_processor.size    ,							{'height': 42, 'width': 42} )






    def       _UpperCamelCase    (       self )							-> Dict:
         # Initialize image_processing
         SCREAMING_SNAKE_CASE					:			Dict    	=       self.image_processing_class(**self.image_processor_dict )
         # create random PIL images
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       prepare_image_inputs(self.image_processor_tester    ,							equal_resolution=lowercase__ )
         for image in image_inputs:
              self.assertIsInstance(lowercase__    ,							Image.Image )

         # Test not batched input
         SCREAMING_SNAKE_CASE					:			List[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.size['height'],
                 self.image_processor_tester.size['width'],
             )    ,							)

         # Test batched
         SCREAMING_SNAKE_CASE					:			Dict    	=       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.size['height'],
                 self.image_processor_tester.size['width'],
             )    ,							)






    def       _UpperCamelCase    (       self )							-> Tuple:
         # Initialize image_processing
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       self.image_processing_class(**self.image_processor_dict )
         # create random numpy tensors
         SCREAMING_SNAKE_CASE					:			Any    	=       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
         SCREAMING_SNAKE_CASE					:			List[str]    	=       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.size['height'],
                 self.image_processor_tester.size['width'],
             )    ,							)

         # Test batched
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       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.size['height'],
                 self.image_processor_tester.size['width'],
             )    ,							)






    def       _UpperCamelCase    (       self )							-> Dict:
         # Initialize image_processing
         SCREAMING_SNAKE_CASE					:			Dict    	=       self.image_processing_class(**self.image_processor_dict )
         # create random PyTorch tensors
         SCREAMING_SNAKE_CASE					:			Dict    	=       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
         SCREAMING_SNAKE_CASE					:			str    	=       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.size['height'],
                 self.image_processor_tester.size['width'],
             )    ,							)

         # Test batched
         SCREAMING_SNAKE_CASE					:			Tuple    	=       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.size['height'],
                 self.image_processor_tester.size['width'],
             )    ,							)




'''simple docstring'''





import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionTextToImagePipeline
from diffusers.utils.testing_utils import nightly, require_torch_gpu, torch_device


_lowerCAmelCase  :List[Any]  				=    False






class 				UpperCAmelCase (       unittest.TestCase ):




    '''simple docstring'''







    pass






@nightly
@require_torch_gpu
class 				UpperCAmelCase (       unittest.TestCase ):




    '''simple docstring'''







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






    def       _UpperCamelCase    (       self )							-> Tuple:
         SCREAMING_SNAKE_CASE					:			Dict    	=       VersatileDiffusionTextToImagePipeline.from_pretrained('shi-labs/versatile-diffusion' )
         # remove text_unet
         pipe.remove_unused_weights()
         pipe.to(lowercase__ )
         pipe.set_progress_bar_config(disable=lowercase__ )

         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       'A painting of a squirrel eating a burger '
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       torch.manual_seed(0 )
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       pipe(
             prompt=lowercase__    ,							generator=lowercase__    ,							guidance_scale=7.5    ,							num_inference_steps=2    ,							output_type='numpy' ).images

         with tempfile.TemporaryDirectory() as tmpdirname:
              pipe.save_pretrained(lowercase__ )
              SCREAMING_SNAKE_CASE					:			int    	=       VersatileDiffusionTextToImagePipeline.from_pretrained(lowercase__ )
         pipe.to(lowercase__ )
         pipe.set_progress_bar_config(disable=lowercase__ )

         SCREAMING_SNAKE_CASE					:			List[str]    	=       generator.manual_seed(0 )
         SCREAMING_SNAKE_CASE					:			List[str]    	=       pipe(
             prompt=lowercase__    ,							generator=lowercase__    ,							guidance_scale=7.5    ,							num_inference_steps=2    ,							output_type='numpy' ).images

         assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass"






    def       _UpperCamelCase    (       self )							-> Any:
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       VersatileDiffusionTextToImagePipeline.from_pretrained(
             'shi-labs/versatile-diffusion'    ,							torch_dtype=torch.floataa )
         pipe.to(lowercase__ )
         pipe.set_progress_bar_config(disable=lowercase__ )

         SCREAMING_SNAKE_CASE					:			int    	=       'A painting of a squirrel eating a burger '
         SCREAMING_SNAKE_CASE					:			Tuple    	=       torch.manual_seed(0 )
         SCREAMING_SNAKE_CASE					:			Tuple    	=       pipe(
             prompt=lowercase__    ,							generator=lowercase__    ,							guidance_scale=7.5    ,							num_inference_steps=50    ,							output_type='numpy' ).images

         SCREAMING_SNAKE_CASE					:			Dict    	=       image[0, 253:256, 253:256, -1]

         assert image.shape == (1, 512, 512, 3)
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       np.array([0.3_3_6_7, 0.3_1_6_9, 0.2_6_5_6, 0.3_8_7_0, 0.4_7_9_0, 0.3_7_9_6, 0.4_0_0_9, 0.4_8_7_8, 0.4_7_7_8] )

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



