




'''simple docstring'''



import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMParallelScheduler,
    DDPMParallelScheduler,
    StableDiffusionParadigmsPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    require_torch_gpu,
)

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()





class       lowerCAmelCase				(	__lowerCAmelCase			,				__lowerCAmelCase			,				unittest.TestCase):
       __lowercase					: List[str]	    =     StableDiffusionParadigmsPipeline
       __lowercase					: int	    =     TEXT_TO_IMAGE_PARAMS
       __lowercase					: Dict	    =     TEXT_TO_IMAGE_BATCH_PARAMS
       __lowercase					: Tuple	    =     TEXT_TO_IMAGE_IMAGE_PARAMS
       __lowercase					: List[Any]	    =     TEXT_TO_IMAGE_IMAGE_PARAMS


       def  lowerCAmelCase (				self					) ->					Optional[int]:



            '''simple docstring'''




            torch.manual_seed(0					)
            __snake_case         =					UNetaDConditionModel(
                block_out_channels=(32, 64)      ,					layers_per_block=2      ,					sample_size=32      ,					in_channels=4      ,					out_channels=4      ,					down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')      ,					up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')      ,					cross_attention_dim=32      ,					attention_head_dim=(2, 4)      ,					use_linear_projection=__SCREAMING_SNAKE_CASE      ,					)
            __snake_case         =					DDIMParallelScheduler(
                beta_start=0.00_085      ,					beta_end=0.012      ,					beta_schedule='''scaled_linear'''      ,					clip_sample=__SCREAMING_SNAKE_CASE      ,					set_alpha_to_one=__SCREAMING_SNAKE_CASE      ,					)
            torch.manual_seed(0					)
            __snake_case         =					AutoencoderKL(
                block_out_channels=[32, 64]      ,					in_channels=3      ,					out_channels=3      ,					down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']      ,					up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']      ,					latent_channels=4      ,					sample_size=128      ,					)
            torch.manual_seed(0					)
            __snake_case         =					CLIPTextConfig(
                bos_token_id=0      ,					eos_token_id=2      ,					hidden_size=32      ,					intermediate_size=37      ,					layer_norm_eps=1E-05      ,					num_attention_heads=4      ,					num_hidden_layers=5      ,					pad_token_id=1      ,					vocab_size=1000      ,					hidden_act='''gelu'''      ,					projection_dim=512      ,					)
            __snake_case         =					CLIPTextModel(__SCREAMING_SNAKE_CASE					)
            __snake_case         =					CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					)

            __snake_case         =					{
                '''unet''': unet,
                '''scheduler''': scheduler,
                '''vae''': vae,
                '''text_encoder''': text_encoder,
                '''tokenizer''': tokenizer,
                '''safety_checker''': None,
                '''feature_extractor''': None,
            }
            return components


       def  lowerCAmelCase (				self      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE=0					) ->					Optional[int]:



            '''simple docstring'''




            if str(__SCREAMING_SNAKE_CASE					).startswith('''mps'''					):
                 __snake_case         =					torch.manual_seed(__SCREAMING_SNAKE_CASE					)
            else:
                 __snake_case         =					torch.Generator(device=__SCREAMING_SNAKE_CASE					).manual_seed(__SCREAMING_SNAKE_CASE					)
            __snake_case         =					{
                '''prompt''': '''a photograph of an astronaut riding a horse''',
                '''generator''': generator,
                '''num_inference_steps''': 10,
                '''guidance_scale''': 6.0,
                '''output_type''': '''numpy''',
                '''parallel''': 3,
                '''debug''': True,
            }
            return inputs


       def  lowerCAmelCase (				self					) ->					Optional[int]:



            '''simple docstring'''




            __snake_case         =					'''cpu'''  # ensure determinism for the device-dependent torch.Generator
            __snake_case         =					self.get_dummy_components()
            __snake_case         =					StableDiffusionParadigmsPipeline(**__SCREAMING_SNAKE_CASE					)
            __snake_case         =					sd_pipe.to(__SCREAMING_SNAKE_CASE					)
            sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

            __snake_case         =					self.get_dummy_inputs(__SCREAMING_SNAKE_CASE					)
            __snake_case         =					sd_pipe(**__SCREAMING_SNAKE_CASE					).images
            __snake_case         =					image[0, -3:, -3:, -1]
            assert image.shape == (1, 64, 64, 3)

            __snake_case         =					np.array([0.4_773, 0.5_417, 0.4_723, 0.4_925, 0.5_631, 0.4_752, 0.5_240, 0.4_935, 0.5_023]					)

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


       def  lowerCAmelCase (				self					) ->					int:



            '''simple docstring'''




            __snake_case         =					'''cpu'''  # ensure determinism for the device-dependent torch.Generator
            __snake_case         =					self.get_dummy_components()
            torch.manual_seed(0					)
            __snake_case         =					DDPMParallelScheduler()
            torch.manual_seed(0					)
            __snake_case         =					StableDiffusionParadigmsPipeline(**__SCREAMING_SNAKE_CASE					)
            __snake_case         =					sd_pipe.to(__SCREAMING_SNAKE_CASE					)
            sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

            __snake_case         =					self.get_dummy_inputs(__SCREAMING_SNAKE_CASE					)
            __snake_case         =					sd_pipe(**__SCREAMING_SNAKE_CASE					).images
            __snake_case         =					image[0, -3:, -3:, -1]
            assert image.shape == (1, 64, 64, 3)

            __snake_case         =					np.array([0.3_573, 0.4_420, 0.4_960, 0.4_799, 0.3_796, 0.3_879, 0.4_819, 0.4_365, 0.4_468]					)

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


       def  lowerCAmelCase (				self					) ->					List[str]:



            '''simple docstring'''




            super().test_inference_batch_consistent(batch_sizes=[1, 2]					)


       def  lowerCAmelCase (				self					) ->					List[str]:



            '''simple docstring'''




            super().test_inference_batch_single_identical(batch_size=2      ,					expected_max_diff=3E-3					)




       def  lowerCAmelCase (				self					) ->					List[str]:



            '''simple docstring'''




            __snake_case         =					'''cpu'''  # ensure determinism for the device-dependent torch.Generator
            __snake_case         =					self.get_dummy_components()
            __snake_case         =					StableDiffusionParadigmsPipeline(**__SCREAMING_SNAKE_CASE					)
            __snake_case         =					sd_pipe.to(__SCREAMING_SNAKE_CASE					)
            sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

            __snake_case         =					self.get_dummy_inputs(__SCREAMING_SNAKE_CASE					)
            __snake_case         =					'''french fries'''
            __snake_case         =					sd_pipe(**__SCREAMING_SNAKE_CASE      ,					negative_prompt=__SCREAMING_SNAKE_CASE					)
            __snake_case         =					output.images
            __snake_case         =					image[0, -3:, -3:, -1]

            assert image.shape == (1, 64, 64, 3)

            __snake_case         =					np.array([0.4_771, 0.5_420, 0.4_683, 0.4_918, 0.5_636, 0.4_725, 0.5_230, 0.4_923, 0.5_015]					)

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





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


       def  lowerCAmelCase (				self					) ->					Optional[Any]:



            '''simple docstring'''




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


       def  lowerCAmelCase (				self      ,					__SCREAMING_SNAKE_CASE=0					) ->					str:



            '''simple docstring'''




            __snake_case         =					torch.Generator(device=__SCREAMING_SNAKE_CASE					).manual_seed(__SCREAMING_SNAKE_CASE					)
            __snake_case         =					{
                '''prompt''': '''a photograph of an astronaut riding a horse''',
                '''generator''': generator,
                '''num_inference_steps''': 10,
                '''guidance_scale''': 7.5,
                '''output_type''': '''numpy''',
                '''parallel''': 3,
                '''debug''': True,
            }
            return inputs




       def  lowerCAmelCase (				self					) ->					Union[str, Any]:



            '''simple docstring'''




            __snake_case         =					'''stabilityai/stable-diffusion-2-base'''
            __snake_case         =					DDIMParallelScheduler.from_pretrained(__SCREAMING_SNAKE_CASE      ,					subfolder='''scheduler'''					)
            __snake_case         =					StableDiffusionParadigmsPipeline.from_pretrained(__SCREAMING_SNAKE_CASE      ,					scheduler=__SCREAMING_SNAKE_CASE      ,					safety_checker=__SCREAMING_SNAKE_CASE					)
            pipe.to(__SCREAMING_SNAKE_CASE					)
            pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)
            pipe.enable_attention_slicing()

            __snake_case         =					self.get_inputs()
            __snake_case         =					pipe(**__SCREAMING_SNAKE_CASE					).images
            __snake_case         =					image[0, -3:, -3:, -1].flatten()

            assert image.shape == (1, 512, 512, 3)

            __snake_case         =					np.array([0.9_622, 0.9_602, 0.9_748, 0.9_591, 0.9_630, 0.9_691, 0.9_661, 0.9_631, 0.9_741]					)

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












'''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 CLIPImageProcessor





class       lowerCAmelCase				(	unittest.TestCase):


       def __init__(				self      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE=7      ,					__SCREAMING_SNAKE_CASE=3      ,					__SCREAMING_SNAKE_CASE=18      ,					__SCREAMING_SNAKE_CASE=30      ,					__SCREAMING_SNAKE_CASE=400      ,					__SCREAMING_SNAKE_CASE=True      ,					__SCREAMING_SNAKE_CASE=None      ,					__SCREAMING_SNAKE_CASE=True      ,					__SCREAMING_SNAKE_CASE=None      ,					__SCREAMING_SNAKE_CASE=True      ,					__SCREAMING_SNAKE_CASE=[0.48_145_466, 0.4_578_275, 0.40_821_073]      ,					__SCREAMING_SNAKE_CASE=[0.26_862_954, 0.26_130_258, 0.27_577_711]      ,					__SCREAMING_SNAKE_CASE=True      ,					) ->					Optional[Any]:



            '''simple docstring'''




            __snake_case         =					size if size is not None else {'''shortest_edge''': 20}
            __snake_case         =					crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
            __snake_case         =					parent
            __snake_case         =					batch_size
            __snake_case         =					num_channels
            __snake_case         =					image_size
            __snake_case         =					min_resolution
            __snake_case         =					max_resolution
            __snake_case         =					do_resize
            __snake_case         =					size
            __snake_case         =					do_center_crop
            __snake_case         =					crop_size
            __snake_case         =					do_normalize
            __snake_case         =					image_mean
            __snake_case         =					image_std
            __snake_case         =					do_convert_rgb


       def  lowerCAmelCase (				self					) ->					Optional[int]:



            '''simple docstring'''




            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  lowerCAmelCase (				self      ,					__SCREAMING_SNAKE_CASE=False      ,					__SCREAMING_SNAKE_CASE=False      ,					__SCREAMING_SNAKE_CASE=False					) ->					str:



            '''simple docstring'''




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

            if equal_resolution:
                 __snake_case         =					[]
                 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:
                 __snake_case         =					[]
                 for i in range(self.batch_size					):
                      __snake_case   ,      __snake_case         =					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
                 __snake_case         =					[Image.fromarray(np.moveaxis(__SCREAMING_SNAKE_CASE      ,					0      ,					-1					)					) for x in image_inputs]

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

            return image_inputs





@require_torch
@require_vision
class       lowerCAmelCase				(	__lowerCAmelCase			,				unittest.TestCase):
       __lowercase					: Dict	    =     CLIPImageProcessor if is_vision_available() else None


       def  lowerCAmelCase (				self					) ->					int:



            '''simple docstring'''




            __snake_case         =					CLIPImageProcessingTester(self					)


       @property
       def  lowerCAmelCase (				self					) ->					List[str]:



            '''simple docstring'''




            return self.image_processor_tester.prepare_image_processor_dict()


       def  lowerCAmelCase (				self					) ->					Optional[Any]:



            '''simple docstring'''




            __snake_case         =					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  lowerCAmelCase (				self					) ->					Optional[int]:



            '''simple docstring'''




            __snake_case         =					self.image_processing_class.from_dict(self.image_processor_dict					)
            self.assertEqual(image_processor.size      ,					{'''shortest_edge''': 20}					)
            self.assertEqual(image_processor.crop_size      ,					{'''height''': 18, '''width''': 18}					)

            __snake_case         =					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  lowerCAmelCase (				self					) ->					Dict:



            '''simple docstring'''




            pass


       def  lowerCAmelCase (				self					) ->					str:



            '''simple docstring'''




            __snake_case         =					self.image_processing_class(**self.image_processor_dict					)
            # create random PIL images
            __snake_case         =					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
            __snake_case         =					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
            __snake_case         =					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  lowerCAmelCase (				self					) ->					Dict:



            '''simple docstring'''




            __snake_case         =					self.image_processing_class(**self.image_processor_dict					)
            # create random numpy tensors
            __snake_case         =					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
            __snake_case         =					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
            __snake_case         =					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  lowerCAmelCase (				self					) ->					int:



            '''simple docstring'''




            __snake_case         =					self.image_processing_class(**self.image_processor_dict					)
            # create random PyTorch tensors
            __snake_case         =					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
            __snake_case         =					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
            __snake_case         =					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       lowerCAmelCase				(	__lowerCAmelCase			,				unittest.TestCase):
       __lowercase					: Dict	    =     CLIPImageProcessor if is_vision_available() else None


       def  lowerCAmelCase (				self					) ->					Any:



            '''simple docstring'''




            __snake_case         =					CLIPImageProcessingTester(self      ,					num_channels=4					)
            __snake_case         =					3


       @property
       def  lowerCAmelCase (				self					) ->					str:



            '''simple docstring'''




            return self.image_processor_tester.prepare_image_processor_dict()


       def  lowerCAmelCase (				self					) ->					List[Any]:



            '''simple docstring'''




            __snake_case         =					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  lowerCAmelCase (				self					) ->					Tuple:



            '''simple docstring'''




            pass




       def  lowerCAmelCase (				self					) ->					Optional[int]:



            '''simple docstring'''




            __snake_case         =					self.image_processing_class(**self.image_processor_dict					)
            # create random PIL images
            __snake_case         =					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
            __snake_case         =					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
            __snake_case         =					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'''],
                )      ,					)






