

import gc
import random
import unittest

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

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLImgaImgPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import floats_tensor, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    IMAGE_TO_IMAGE_IMAGE_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()
class     A_      (					a__       ,   a__       ,   unittest.TestCase  ):


    '''simple docstring'''






    a__   							=				StableDiffusionXLImgaImgPipeline
    a__   							=				TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"height", "width"}
    a__   							=				PipelineTesterMixin.required_optional_params - {"latents"}
    a__   							=				TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
    a__   							=				IMAGE_TO_IMAGE_IMAGE_PARAMS
    a__   							=				IMAGE_TO_IMAGE_IMAGE_PARAMS


    def    lowerCAmelCase_  (self     )   ->					Dict:
        torch.manual_seed(0     )
        __UpperCAmelCase     =	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''')   ,					attention_head_dim=(2, 4)   ,					use_linear_projection=UpperCamelCase_   ,					addition_embed_type='''text_time'''   ,					addition_time_embed_dim=8   ,					transformer_layers_per_block=(1, 2)   ,					projection_class_embeddings_input_dim=80   ,					cross_attention_dim=64   ,					)
        __UpperCAmelCase     =	EulerDiscreteScheduler(
            beta_start=0.00085   ,					beta_end=0.012   ,					steps_offset=1   ,					beta_schedule='''scaled_linear'''   ,					timestep_spacing='''leading'''   ,					)
        torch.manual_seed(0     )
        __UpperCAmelCase     =	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     )
        __UpperCAmelCase     =	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=1_000   ,					hidden_act='''gelu'''   ,					projection_dim=32   ,					)
        __UpperCAmelCase     =	CLIPTextModel(UpperCamelCase_     )
        __UpperCAmelCase     =	CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''   ,					local_files_only=UpperCamelCase_     )

        __UpperCAmelCase     =	CLIPTextModelWithProjection(UpperCamelCase_     )
        __UpperCAmelCase     =	CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''   ,					local_files_only=UpperCamelCase_     )

        __UpperCAmelCase     =	{
            '''unet''': unet,
            '''scheduler''': scheduler,
            '''vae''': vae,
            '''text_encoder''': text_encoder,
            '''tokenizer''': tokenizer,
            '''text_encoder_2''': text_encoder_a,
            '''tokenizer_2''': tokenizer_a,
            # "safety_checker": None,
            # "feature_extractor": None,
        }
        return components


    def    lowerCAmelCase_  (self   ,					lowercase__   ,					lowercase__=0     )   ->					Any:
        __UpperCAmelCase     =	floats_tensor((1, 3, 32, 32)   ,					rng=random.Random(UpperCamelCase_     )     ).to(UpperCamelCase_     )
        __UpperCAmelCase     =	image / 2 + 0.5
        if str(UpperCamelCase_     ).startswith('''mps'''     ):
            __UpperCAmelCase     =	torch.manual_seed(UpperCamelCase_     )
        else:
            __UpperCAmelCase     =	torch.Generator(device=UpperCamelCase_     ).manual_seed(UpperCamelCase_     )
        __UpperCAmelCase     =	{
            '''prompt''': '''A painting of a squirrel eating a burger''',
            '''image''': image,
            '''generator''': generator,
            '''num_inference_steps''': 2,
            '''guidance_scale''': 5.0,
            '''output_type''': '''numpy''',
            '''strength''': 0.75,
        }
        return inputs


    def    lowerCAmelCase_  (self     )   ->					Optional[Any]:
        __UpperCAmelCase     =	'''cpu'''  # ensure determinism for the device-dependent torch.Generator
        __UpperCAmelCase     =	self.get_dummy_components()
        __UpperCAmelCase     =	StableDiffusionXLImgaImgPipeline(**UpperCamelCase_     )
        __UpperCAmelCase     =	sd_pipe.to(UpperCamelCase_     )
        sd_pipe.set_progress_bar_config(disable=UpperCamelCase_     )

        __UpperCAmelCase     =	self.get_dummy_inputs(UpperCamelCase_     )
        __UpperCAmelCase     =	sd_pipe(**UpperCamelCase_     ).images
        __UpperCAmelCase     =	image[0, -3:, -3:, -1]

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

        __UpperCAmelCase     =	np.array([0.4656, 0.4840, 0.4439, 0.6698, 0.5574, 0.4524, 0.5799, 0.5943, 0.5165]     )

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


    def    lowerCAmelCase_  (self     )   ->					int:
        super().test_attention_slicing_forward_pass(expected_max_diff=3E-3     )


    def    lowerCAmelCase_  (self     )   ->					Optional[Any]:
        super().test_inference_batch_single_identical(expected_max_diff=3E-3     )


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






    def    lowerCAmelCase_  (self     )   ->					List[Any]:
        __UpperCAmelCase     =	self.get_dummy_components()
        __UpperCAmelCase     =	StableDiffusionXLImgaImgPipeline(**UpperCamelCase_     )
        __UpperCAmelCase     =	sd_pipe.to(UpperCamelCase_     )
        __UpperCAmelCase     =	sd_pipe.to(UpperCamelCase_     )
        sd_pipe.set_progress_bar_config(disable=UpperCamelCase_     )

        # forward without prompt embeds
        __UpperCAmelCase     =	self.get_dummy_inputs(UpperCamelCase_     )
        __UpperCAmelCase     =	3 * ['''this is a negative prompt''']
        __UpperCAmelCase     =	negative_prompt
        __UpperCAmelCase     =	3 * [inputs['''prompt''']]

        __UpperCAmelCase     =	sd_pipe(**UpperCamelCase_     )
        __UpperCAmelCase     =	output.images[0, -3:, -3:, -1]

        # forward with prompt embeds
        __UpperCAmelCase     =	self.get_dummy_inputs(UpperCamelCase_     )
        __UpperCAmelCase     =	3 * ['''this is a negative prompt''']
        __UpperCAmelCase     =	3 * [inputs.pop('''prompt'''     )]

        (
            __UpperCAmelCase					
        )     =	sd_pipe.encode_prompt(UpperCamelCase_   ,					negative_prompt=UpperCamelCase_     )

        __UpperCAmelCase     =	sd_pipe(
            **UpperCamelCase_   ,					prompt_embeds=UpperCamelCase_   ,					negative_prompt_embeds=UpperCamelCase_   ,					pooled_prompt_embeds=UpperCamelCase_   ,					negative_pooled_prompt_embeds=UpperCamelCase_   ,					)
        __UpperCAmelCase     =	output.images[0, -3:, -3:, -1]

        # make sure that it's equal
        assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()     ).max() < 1E-4






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


    '''simple docstring'''






    def    lowerCAmelCase_  (self     )   ->					str:
        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()


    def    lowerCAmelCase_  (self   ,					lowercase__   ,					lowercase__="cpu"   ,					lowercase__=torch.floataa   ,					lowercase__=0     )   ->					Optional[int]:
        __UpperCAmelCase     =	torch.Generator(device=UpperCamelCase_     ).manual_seed(UpperCamelCase_     )
        __UpperCAmelCase     =	np.random.RandomState(UpperCamelCase_     ).standard_normal((1, 4, 64, 64)     )
        __UpperCAmelCase     =	torch.from_numpy(UpperCamelCase_     ).to(device=UpperCamelCase_   ,					dtype=UpperCamelCase_     )
        __UpperCAmelCase     =	{
            '''prompt''': '''a photograph of an astronaut riding a horse''',
            '''latents''': latents,
            '''generator''': generator,
            '''num_inference_steps''': 3,
            '''guidance_scale''': 7.5,
            '''output_type''': '''numpy''',
        }
        return inputs






    def    lowerCAmelCase_  (self     )   ->					Union[str, Any]:
        __UpperCAmelCase     =	DiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-base'''     )
        pipe.to(UpperCamelCase_     )
        pipe.set_progress_bar_config(disable=UpperCamelCase_     )

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

        assert image.shape == (1, 512, 512, 3)
        __UpperCAmelCase     =	np.array([0.49493, 0.47896, 0.40798, 0.54214, 0.53212, 0.48202, 0.47656, 0.46329, 0.48506]     )
        assert np.abs(image_slice - expected_slice     ).max() < 7E-3








"""simple docstring"""





import unittest
from typing import Dict, List, Optional, Union

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 BridgeTowerImageProcessor



class 						snake_case_(				unittest.TestCase	):






     def __init__(		self     :    Union[str, Any]    , UpperCamelCase_     :    Optional[Any]    , UpperCamelCase_     :    bool = True    , UpperCamelCase_     :    Dict[str, int] = None    , UpperCamelCase_     :    int = 3_2    , UpperCamelCase_     :    bool = True    , UpperCamelCase_     :    Union[int, float] = 1 / 2_5_5    , UpperCamelCase_     :    bool = True    , UpperCamelCase_     :    bool = True    , UpperCamelCase_     :    Optional[Union[float, List[float]]] = [0.48_145_466, 0.4_578_275, 0.40_821_073]    , UpperCamelCase_     :    Optional[Union[float, List[float]]] = [0.26_862_954, 0.26_130_258, 0.27_577_711]    , UpperCamelCase_     :    bool = True    , UpperCamelCase_     :    Optional[int]=7    , UpperCamelCase_     :    int=3_0    , UpperCamelCase_     :    str=4_0_0    , UpperCamelCase_     :    List[Any]=3    , ):
           lowerCAmelCase    :		Union[str, Any]				=      parent
           lowerCAmelCase    :		Union[str, Any]				=      do_resize
           lowerCAmelCase    :		List[str]				=      size if size is not None else {'''shortest_edge''': 2_8_8}
           lowerCAmelCase    :		int				=      size_divisor
           lowerCAmelCase    :		List[str]				=      do_rescale
           lowerCAmelCase    :		Optional[Any]				=      rescale_factor
           lowerCAmelCase    :		Dict				=      do_normalize
           lowerCAmelCase    :		Any				=      do_center_crop
           lowerCAmelCase    :		Union[str, Any]				=      image_mean
           lowerCAmelCase    :		Optional[Any]				=      image_std
           lowerCAmelCase    :		Union[str, Any]				=      do_pad
           lowerCAmelCase    :		Union[str, Any]				=      batch_size
           lowerCAmelCase    :		Any				=      num_channels
           lowerCAmelCase    :		Union[str, Any]				=      min_resolution
           lowerCAmelCase    :		int				=      max_resolution






     def    lowerCamelCase__   (		self     :    Dict ):
           return {
               "image_mean": self.image_mean,
               "image_std": self.image_std,
               "do_normalize": self.do_normalize,
               "do_resize": self.do_resize,
               "size": self.size,
               "size_divisor": self.size_divisor,
           }






     def    lowerCamelCase__   (		self     :    Any    , UpperCamelCase_     :    int    , UpperCamelCase_     :    List[str]=False ):
           if not batched:
                 lowerCAmelCase    :		Dict				=      self.size['''shortest_edge''']
                 lowerCAmelCase    :		Dict				=      image_inputs[0]
                 if isinstance(UpperCamelCase_    , Image.Image ):
                       lowerCAmelCase,					lowerCAmelCase    :		Optional[int]				=      image.size
                 else:
                       lowerCAmelCase,					lowerCAmelCase    :		List[Any]				=      image.shape[1], image.shape[2]
                 lowerCAmelCase    :		Union[str, Any]				=      size / min(UpperCamelCase_    , UpperCamelCase_ )
                 if h < w:
                       lowerCAmelCase,					lowerCAmelCase    :		Dict				=      size, scale * w
                 else:
                       lowerCAmelCase,					lowerCAmelCase    :		Optional[int]				=      scale * h, size

                 lowerCAmelCase    :		List[Any]				=      int((1_3_3_3 / 8_0_0) * size )
                 if max(UpperCamelCase_    , UpperCamelCase_ ) > max_size:
                       lowerCAmelCase    :		int				=      max_size / max(UpperCamelCase_    , UpperCamelCase_ )
                       lowerCAmelCase    :		str				=      newh * scale
                       lowerCAmelCase    :		Tuple				=      neww * scale

                 lowerCAmelCase,					lowerCAmelCase    :		List[str]				=      int(newh + 0.5 ), int(neww + 0.5 )
                 lowerCAmelCase,					lowerCAmelCase    :		Tuple				=      (
                     newh // self.size_divisor * self.size_divisor,
                     neww // self.size_divisor * self.size_divisor,
                 )

           else:
                 lowerCAmelCase    :		Optional[int]				=      []
                 for image in image_inputs:
                       lowerCAmelCase,					lowerCAmelCase    :		List[str]				=      self.get_expected_values([image] )
                       expected_values.append((expected_height, expected_width) )
                 lowerCAmelCase    :		Union[str, Any]				=      max(UpperCamelCase_    , key=lambda UpperCamelCase_ : item[0] )[0]
                 lowerCAmelCase    :		Union[str, Any]				=      max(UpperCamelCase_    , key=lambda UpperCamelCase_ : item[1] )[1]

           return expected_height, expected_width



@require_torch
@require_vision
class 						snake_case_(				a__							,    unittest.TestCase	):
     __UpperCamelCase     =    BridgeTowerImageProcessor if is_vision_available() else None






     def    lowerCamelCase__   (		self     :    Optional[int] ):
           lowerCAmelCase    :		Optional[int]				=      BridgeTowerImageProcessingTester(self )






     @property
     def    lowerCamelCase__   (		self     :    List[str] ):
           return self.image_processor_tester.prepare_image_processor_dict()






     def    lowerCamelCase__   (		self     :    List[str] ):
           lowerCAmelCase    :		Optional[Any]				=      self.image_processing_class(**self.image_processor_dict )
           self.assertTrue(hasattr(UpperCamelCase_    , '''image_mean''' ) )
           self.assertTrue(hasattr(UpperCamelCase_    , '''image_std''' ) )
           self.assertTrue(hasattr(UpperCamelCase_    , '''do_normalize''' ) )
           self.assertTrue(hasattr(UpperCamelCase_    , '''do_resize''' ) )
           self.assertTrue(hasattr(UpperCamelCase_    , '''size''' ) )
           self.assertTrue(hasattr(UpperCamelCase_    , '''size_divisor''' ) )






     def    lowerCamelCase__   (		self     :    int ):
           pass






     def    lowerCamelCase__   (		self     :    Optional[Any] ):
           # Initialize image processor
           lowerCAmelCase    :		str				=      self.image_processing_class(**self.image_processor_dict )
           # create random PIL images
           lowerCAmelCase    :		Optional[int]				=      prepare_image_inputs(self.image_processor_tester    , equal_resolution=UpperCamelCase_ )
           for image in image_inputs:
                 self.assertIsInstance(UpperCamelCase_    , Image.Image )

           # Test not batched input
           lowerCAmelCase    :		Optional[int]				=      image_processing(image_inputs[0]    , return_tensors='''pt''' ).pixel_values

           lowerCAmelCase,					lowerCAmelCase    :		List[Any]				=      self.image_processor_tester.get_expected_values(UpperCamelCase_ )
           self.assertEqual(
               encoded_images.shape    , (1, self.image_processor_tester.num_channels, expected_height, expected_width)    , )

           # Test batched
           lowerCAmelCase    :		Dict				=      image_processing(UpperCamelCase_    , return_tensors='''pt''' ).pixel_values

           lowerCAmelCase,					lowerCAmelCase    :		int				=      self.image_processor_tester.get_expected_values(UpperCamelCase_    , batched=UpperCamelCase_ )
           self.assertEqual(
               encoded_images.shape    , (
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )    , )






     def    lowerCamelCase__   (		self     :    Optional[Any] ):
           # Initialize image processor
           lowerCAmelCase    :		Tuple				=      self.image_processing_class(**self.image_processor_dict )
           # create random numpy tensors
           lowerCAmelCase    :		List[Any]				=      prepare_image_inputs(self.image_processor_tester    , equal_resolution=UpperCamelCase_    , numpify=UpperCamelCase_ )
           for image in image_inputs:
                 self.assertIsInstance(UpperCamelCase_    , np.ndarray )

           # Test not batched input
           lowerCAmelCase    :		Any				=      image_processing(image_inputs[0]    , return_tensors='''pt''' ).pixel_values

           lowerCAmelCase,					lowerCAmelCase    :		Optional[Any]				=      self.image_processor_tester.get_expected_values(UpperCamelCase_ )
           self.assertEqual(
               encoded_images.shape    , (1, self.image_processor_tester.num_channels, expected_height, expected_width)    , )

           # Test batched
           lowerCAmelCase    :		Tuple				=      image_processing(UpperCamelCase_    , return_tensors='''pt''' ).pixel_values

           lowerCAmelCase,					lowerCAmelCase    :		str				=      self.image_processor_tester.get_expected_values(UpperCamelCase_    , batched=UpperCamelCase_ )
           self.assertEqual(
               encoded_images.shape    , (
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )    , )






     def    lowerCamelCase__   (		self     :    Optional[int] ):
           # Initialize image processor
           lowerCAmelCase    :		Union[str, Any]				=      self.image_processing_class(**self.image_processor_dict )
           # create random PyTorch tensors
           lowerCAmelCase    :		List[str]				=      prepare_image_inputs(self.image_processor_tester    , equal_resolution=UpperCamelCase_    , torchify=UpperCamelCase_ )
           for image in image_inputs:
                 self.assertIsInstance(UpperCamelCase_    , torch.Tensor )

           # Test not batched input
           lowerCAmelCase    :		Any				=      image_processing(image_inputs[0]    , return_tensors='''pt''' ).pixel_values

           lowerCAmelCase,					lowerCAmelCase    :		Tuple				=      self.image_processor_tester.get_expected_values(UpperCamelCase_ )
           self.assertEqual(
               encoded_images.shape    , (1, self.image_processor_tester.num_channels, expected_height, expected_width)    , )

           # Test batched
           lowerCAmelCase    :		str				=      image_processing(UpperCamelCase_    , return_tensors='''pt''' ).pixel_values

           lowerCAmelCase,					lowerCAmelCase    :		str				=      self.image_processor_tester.get_expected_values(UpperCamelCase_    , batched=UpperCamelCase_ )
           self.assertEqual(
               encoded_images.shape    , (
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )    , )


