


from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    get_resize_output_image_size,
    normalize,
    pad,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging
from ...utils.import_utils import is_vision_available


_UpperCAmelCase      :	Optional[Any]  =logging.get_logger(__name__)


if is_vision_available():
						import PIL







class 					snake_case__(				UpperCAmelCase__				):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__   :	List[Any]       =				["""pixel_values"""]




						def __init__(					self ,    __lowercase = True ,    __lowercase = None ,    __lowercase = PILImageResampling.BILINEAR ,    __lowercase = True ,    __lowercase = False ,    __lowercase = True ,    __lowercase = True ,    __lowercase = 1 / 2_5_5 ,    __lowercase = True ,    __lowercase = None ,    __lowercase = None ,    **__lowercase ,    )       ->     None:
										super().__init__(**__lowercase				)

										lowerCAmelCase_			:   Dict     					=					size if size is not None else {'''height''': 2_5_6_0, '''width''': 1_9_2_0}
										if isinstance(__lowercase ,    (tuple, list)				):
														# The previous feature extractor size parameter was in (width, height) format
														lowerCAmelCase_			:   str     					=					size[::-1]
										lowerCAmelCase_			:   int     					=					get_size_dict(__lowercase				)

										lowerCAmelCase_			:   Dict     					=					do_resize
										lowerCAmelCase_			:   List[Any]     					=					size
										lowerCAmelCase_			:   Dict     					=					resample
										lowerCAmelCase_			:   List[Any]     					=					do_thumbnail
										lowerCAmelCase_			:   Union[str, Any]     					=					do_align_long_axis
										lowerCAmelCase_			:   List[str]     					=					do_pad
										lowerCAmelCase_			:   Optional[int]     					=					do_rescale
										lowerCAmelCase_			:   Optional[Any]     					=					rescale_factor
										lowerCAmelCase_			:   Dict     					=					do_normalize
										lowerCAmelCase_			:   Any     					=					image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
										lowerCAmelCase_			:   Optional[Any]     					=					image_std if image_std is not None else IMAGENET_STANDARD_STD




						def     lowercase_			(					self ,    __lowercase ,    __lowercase ,    __lowercase = None				)       ->     np.ndarray:
										lowerCAmelCase_					,	lowerCAmelCase_			:   Optional[int]     					=					get_image_size(__lowercase				)
										lowerCAmelCase_					,	lowerCAmelCase_			:   Any     					=					size['''height'''], size['''width''']

										if (output_width < output_height and input_width > input_height) or (
										    output_width > output_height and input_width < input_height
										):
														lowerCAmelCase_			:   List[str]     					=					np.rotaa(__lowercase ,    3				)

										if data_format is not None:
														lowerCAmelCase_			:   str     					=					to_channel_dimension_format(__lowercase ,    __lowercase				)

										return image




						def     lowercase_			(					self ,    __lowercase ,    __lowercase ,    __lowercase = False ,    __lowercase = None ,    )       ->     np.ndarray:
										lowerCAmelCase_					,	lowerCAmelCase_			:   Union[str, Any]     					=					size['''height'''], size['''width''']
										lowerCAmelCase_					,	lowerCAmelCase_			:   Any     					=					get_image_size(__lowercase				)

										lowerCAmelCase_			:   Optional[int]     					=					output_width - input_width
										lowerCAmelCase_			:   str     					=					output_height - input_height

										if random_padding:
														lowerCAmelCase_			:   List[str]     					=					np.random.randint(low=0 ,    high=delta_height + 1				)
														lowerCAmelCase_			:   Optional[int]     					=					np.random.randint(low=0 ,    high=delta_width + 1				)
										else:
														lowerCAmelCase_			:   Optional[Any]     					=					delta_height // 2
														lowerCAmelCase_			:   str     					=					delta_width // 2

										lowerCAmelCase_			:   str     					=					delta_height - pad_top
										lowerCAmelCase_			:   Union[str, Any]     					=					delta_width - pad_left

										lowerCAmelCase_			:   List[str]     					=					((pad_top, pad_bottom), (pad_left, pad_right))
										return pad(__lowercase ,    __lowercase ,    data_format=__lowercase				)




						def     lowercase_			(					self ,    *__lowercase ,    **__lowercase				)       ->     Optional[Any]:
										logger.info('''pad is deprecated and will be removed in version 4.27. Please use pad_image instead.'''				)
										return self.pad_image(*__lowercase ,    **__lowercase				)




						def     lowercase_			(					self ,    __lowercase ,    __lowercase ,    __lowercase = PILImageResampling.BICUBIC ,    __lowercase = None ,    **__lowercase ,    )       ->     np.ndarray:
										lowerCAmelCase_					,	lowerCAmelCase_			:   Optional[Any]     					=					get_image_size(__lowercase				)
										lowerCAmelCase_					,	lowerCAmelCase_			:   Any     					=					size['''height'''], size['''width''']

										# We always resize to the smallest of either the input or output size.
										lowerCAmelCase_			:   Optional[Any]     					=					min(__lowercase ,    __lowercase				)
										lowerCAmelCase_			:   Tuple     					=					min(__lowercase ,    __lowercase				)

										if height == input_height and width == input_width:
														return image

										if input_height > input_width:
														lowerCAmelCase_			:   Optional[int]     					=					int(input_width * height / input_height				)
										elif input_width > input_height:
														lowerCAmelCase_			:   str     					=					int(input_height * width / input_width				)

										return resize(
										    __lowercase ,    size=(height, width) ,    resample=__lowercase ,    reducing_gap=2.0 ,    data_format=__lowercase ,    **__lowercase				)




						def     lowercase_			(					self ,    __lowercase ,    __lowercase ,    __lowercase = PILImageResampling.BICUBIC ,    __lowercase = None ,    **__lowercase ,    )       ->     np.ndarray:
										lowerCAmelCase_			:   Optional[Any]     					=					get_size_dict(__lowercase				)
										lowerCAmelCase_			:   str     					=					min(size['''height'''] ,    size['''width''']				)
										lowerCAmelCase_			:   int     					=					get_resize_output_image_size(__lowercase ,    size=__lowercase ,    default_to_square=__lowercase				)
										lowerCAmelCase_			:   List[str]     					=					resize(__lowercase ,    size=__lowercase ,    resample=__lowercase ,    data_format=__lowercase ,    **__lowercase				)
										return resized_image




						def     lowercase_			(					self ,    __lowercase ,    __lowercase ,    __lowercase = None ,    **__lowercase ,    )       ->     str:
										return rescale(__lowercase ,    scale=__lowercase ,    data_format=__lowercase ,    **__lowercase				)




						def     lowercase_			(					self ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase = None ,    **__lowercase ,    )       ->     np.ndarray:
										return normalize(__lowercase ,    mean=__lowercase ,    std=__lowercase ,    data_format=__lowercase ,    **__lowercase				)




						def     lowercase_			(					self ,    __lowercase ,    __lowercase = None ,    __lowercase = None ,    __lowercase = None ,    __lowercase = None ,    __lowercase = None ,    __lowercase = None ,    __lowercase = False ,    __lowercase = None ,    __lowercase = None ,    __lowercase = None ,    __lowercase = None ,    __lowercase = None ,    __lowercase = None ,    __lowercase = ChannelDimension.FIRST ,    **__lowercase ,    )       ->     PIL.Image.Image:
										lowerCAmelCase_			:   Tuple     					=					do_resize if do_resize is not None else self.do_resize
										lowerCAmelCase_			:   List[str]     					=					size if size is not None else self.size
										if isinstance(__lowercase ,    (tuple, list)				):
														# Previous feature extractor had size in (width, height) format
														lowerCAmelCase_			:   int     					=					size[::-1]
										lowerCAmelCase_			:   int     					=					get_size_dict(__lowercase				)
										lowerCAmelCase_			:   Optional[Any]     					=					resample if resample is not None else self.resample
										lowerCAmelCase_			:   Optional[int]     					=					do_thumbnail if do_thumbnail is not None else self.do_thumbnail
										lowerCAmelCase_			:   List[str]     					=					do_align_long_axis if do_align_long_axis is not None else self.do_align_long_axis
										lowerCAmelCase_			:   Optional[Any]     					=					do_pad if do_pad is not None else self.do_pad
										lowerCAmelCase_			:   Optional[int]     					=					do_rescale if do_rescale is not None else self.do_rescale
										lowerCAmelCase_			:   Any     					=					rescale_factor if rescale_factor is not None else self.rescale_factor
										lowerCAmelCase_			:   Optional[Any]     					=					do_normalize if do_normalize is not None else self.do_normalize
										lowerCAmelCase_			:   Tuple     					=					image_mean if image_mean is not None else self.image_mean
										lowerCAmelCase_			:   str     					=					image_std if image_std is not None else self.image_std

										lowerCAmelCase_			:   str     					=					make_list_of_images(__lowercase				)

										if not valid_images(__lowercase				):
														raise ValueError(
														    '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '''
														    '''torch.Tensor, tf.Tensor or jax.ndarray.'''				)

										if do_resize and size is None:
														raise ValueError('''Size must be specified if do_resize is True.'''				)

										if do_rescale and rescale_factor is None:
														raise ValueError('''Rescale factor must be specified if do_rescale is True.'''				)

										if do_pad and size is None:
														raise ValueError('''Size must be specified if do_pad is True.'''				)

										if do_normalize and (image_mean is None or image_std is None):
														raise ValueError('''Image mean and std must be specified if do_normalize is True.'''				)

										# All transformations expect numpy arrays.
										lowerCAmelCase_			:   List[str]     					=					[to_numpy_array(__lowercase				) for image in images]

										if do_align_long_axis:
														lowerCAmelCase_			:   Dict     					=					[self.align_long_axis(__lowercase ,    size=__lowercase				) for image in images]

										if do_resize:
														lowerCAmelCase_			:   Optional[Any]     					=					[self.resize(image=__lowercase ,    size=__lowercase ,    resample=__lowercase				) for image in images]

										if do_thumbnail:
														lowerCAmelCase_			:   Union[str, Any]     					=					[self.thumbnail(image=__lowercase ,    size=__lowercase				) for image in images]

										if do_pad:
														lowerCAmelCase_			:   Union[str, Any]     					=					[self.pad_image(image=__lowercase ,    size=__lowercase ,    random_padding=__lowercase				) for image in images]

										if do_rescale:
														lowerCAmelCase_			:   Optional[int]     					=					[self.rescale(image=__lowercase ,    scale=__lowercase				) for image in images]

										if do_normalize:
														lowerCAmelCase_			:   str     					=					[self.normalize(image=__lowercase ,    mean=__lowercase ,    std=__lowercase				) for image in images]

										lowerCAmelCase_			:   Optional[int]     					=					[to_channel_dimension_format(__lowercase ,    __lowercase				) for image in images]

										lowerCAmelCase_			:   Optional[int]     					=					{'''pixel_values''': images}
										return BatchFeature(data=__lowercase ,    tensor_type=__lowercase				)



import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLPipeline,
    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 					snake_case__(				UpperCAmelCase__,    UpperCAmelCase__,    unittest.TestCase				):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__   :	str       =				StableDiffusionXLPipeline
						SCREAMING_SNAKE_CASE__   :	Dict       =				TEXT_TO_IMAGE_PARAMS
						SCREAMING_SNAKE_CASE__   :	Optional[int]       =				TEXT_TO_IMAGE_BATCH_PARAMS
						SCREAMING_SNAKE_CASE__   :	Optional[Any]       =				TEXT_TO_IMAGE_IMAGE_PARAMS
						SCREAMING_SNAKE_CASE__   :	int       =				TEXT_TO_IMAGE_IMAGE_PARAMS




						def     lowercase_			(					self				)       ->     int:
										torch.manual_seed(0				)
										lowerCAmelCase_			:   Tuple     					=					UNetaDConditionModel(
										    block_out_channels=(3_2, 6_4) ,    layers_per_block=2 ,    sample_size=3_2 ,    in_channels=4 ,    out_channels=4 ,    down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') ,    up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') ,    attention_head_dim=(2, 4) ,    use_linear_projection=__lowercase ,    addition_embed_type='''text_time''' ,    addition_time_embed_dim=8 ,    transformer_layers_per_block=(1, 2) ,    projection_class_embeddings_input_dim=8_0 ,    cross_attention_dim=6_4 ,    )
										lowerCAmelCase_			:   Any     					=					EulerDiscreteScheduler(
										    beta_start=0.0_00_85 ,    beta_end=0.0_12 ,    steps_offset=1 ,    beta_schedule='''scaled_linear''' ,    timestep_spacing='''leading''' ,    )
										torch.manual_seed(0				)
										lowerCAmelCase_			:   Any     					=					AutoencoderKL(
										    block_out_channels=[3_2, 6_4] ,    in_channels=3 ,    out_channels=3 ,    down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] ,    up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] ,    latent_channels=4 ,    sample_size=1_2_8 ,    )
										torch.manual_seed(0				)
										lowerCAmelCase_			:   Dict     					=					CLIPTextConfig(
										    bos_token_id=0 ,    eos_token_id=2 ,    hidden_size=3_2 ,    intermediate_size=3_7 ,    layer_norm_eps=1e-05 ,    num_attention_heads=4 ,    num_hidden_layers=5 ,    pad_token_id=1 ,    vocab_size=1_0_0_0 ,    hidden_act='''gelu''' ,    projection_dim=3_2 ,    )
										lowerCAmelCase_			:   str     					=					CLIPTextModel(__lowercase				)
										lowerCAmelCase_			:   List[str]     					=					CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ,    local_files_only=__lowercase				)

										lowerCAmelCase_			:   Tuple     					=					CLIPTextModelWithProjection(__lowercase				)
										lowerCAmelCase_			:   Optional[Any]     					=					CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ,    local_files_only=__lowercase				)

										lowerCAmelCase_			:   int     					=					{
										    '''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     lowercase_			(					self ,    __lowercase ,    __lowercase=0				)       ->     Dict:
										if str(__lowercase				).startswith('''mps'''				):
														lowerCAmelCase_			:   Dict     					=					torch.manual_seed(__lowercase				)
										else:
														lowerCAmelCase_			:   Dict     					=					torch.Generator(device=__lowercase				).manual_seed(__lowercase				)
										lowerCAmelCase_			:   Dict     					=					{
										    '''prompt''': '''A painting of a squirrel eating a burger''',
										    '''generator''': generator,
										    '''num_inference_steps''': 2,
										    '''guidance_scale''': 5.0,
										    '''output_type''': '''numpy''',
										}
										return inputs




						def     lowercase_			(					self				)       ->     Optional[Any]:
										lowerCAmelCase_			:   Union[str, Any]     					=					'''cpu'''  # ensure determinism for the device-dependent torch.Generator
										lowerCAmelCase_			:   Any     					=					self.get_dummy_components()
										lowerCAmelCase_			:   Optional[int]     					=					StableDiffusionXLPipeline(**__lowercase				)
										lowerCAmelCase_			:   Optional[int]     					=					sd_pipe.to(__lowercase				)
										sd_pipe.set_progress_bar_config(disable=__lowercase				)

										lowerCAmelCase_			:   Dict     					=					self.get_dummy_inputs(__lowercase				)
										lowerCAmelCase_			:   str     					=					sd_pipe(**__lowercase				).images
										lowerCAmelCase_			:   Any     					=					image[0, -3:, -3:, -1]

										assert image.shape == (1, 6_4, 6_4, 3)
										lowerCAmelCase_			:   str     					=					np.array([0.58_73, 0.61_28, 0.47_97, 0.51_22, 0.56_74, 0.46_39, 0.52_27, 0.51_49, 0.47_47]				)

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




						def     lowercase_			(					self				)       ->     List[Any]:
										lowerCAmelCase_			:   Any     					=					self.get_dummy_components()
										lowerCAmelCase_			:   List[Any]     					=					StableDiffusionXLPipeline(**__lowercase				)
										lowerCAmelCase_			:   List[Any]     					=					sd_pipe.to(__lowercase				)
										lowerCAmelCase_			:   int     					=					sd_pipe.to(__lowercase				)
										sd_pipe.set_progress_bar_config(disable=__lowercase				)

										# forward without prompt embeds
										lowerCAmelCase_			:   Optional[Any]     					=					self.get_dummy_inputs(__lowercase				)
										lowerCAmelCase_			:   List[str]     					=					3 * ['''this is a negative prompt''']
										lowerCAmelCase_			:   Union[str, Any]     					=					negative_prompt
										lowerCAmelCase_			:   Union[str, Any]     					=					3 * [inputs['''prompt''']]

										lowerCAmelCase_			:   str     					=					sd_pipe(**__lowercase				)
										lowerCAmelCase_			:   Any     					=					output.images[0, -3:, -3:, -1]

										# forward with prompt embeds
										lowerCAmelCase_			:   Tuple     					=					self.get_dummy_inputs(__lowercase				)
										lowerCAmelCase_			:   Dict     					=					3 * ['''this is a negative prompt''']
										lowerCAmelCase_			:   Dict     					=					3 * [inputs.pop('''prompt'''				)]

										(
										    (
										    lowerCAmelCase_
										)					,	(
										    lowerCAmelCase_
										)					,	(
										    lowerCAmelCase_
										)					,	(
										    lowerCAmelCase_
										)					,	
										)			:   str     					=					sd_pipe.encode_prompt(__lowercase ,    negative_prompt=__lowercase				)

										lowerCAmelCase_			:   List[Any]     					=					sd_pipe(
										    **__lowercase ,    prompt_embeds=__lowercase ,    negative_prompt_embeds=__lowercase ,    pooled_prompt_embeds=__lowercase ,    negative_pooled_prompt_embeds=__lowercase ,    )
										lowerCAmelCase_			:   int     					=					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




						def     lowercase_			(					self				)       ->     List[str]:
										super().test_attention_slicing_forward_pass(expected_max_diff=3e-3				)




						def     lowercase_			(					self				)       ->     Tuple:
										super().test_inference_batch_single_identical(expected_max_diff=3e-3				)







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







						'''simple docstring'''




						def     lowercase_			(					self				)       ->     int:
										super().tearDown()
										gc.collect()
										torch.cuda.empty_cache()




						def     lowercase_			(					self ,    __lowercase ,    __lowercase="cpu" ,    __lowercase=torch.floataa ,    __lowercase=0				)       ->     Dict:
										lowerCAmelCase_			:   Any     					=					torch.Generator(device=__lowercase				).manual_seed(__lowercase				)
										lowerCAmelCase_			:   str     					=					np.random.RandomState(__lowercase				).standard_normal((1, 4, 6_4, 6_4)				)
										lowerCAmelCase_			:   List[str]     					=					torch.from_numpy(__lowercase				).to(device=__lowercase ,    dtype=__lowercase				)
										lowerCAmelCase_			:   Optional[Any]     					=					{
										    '''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     lowercase_			(					self				)       ->     List[str]:
										lowerCAmelCase_			:   Optional[int]     					=					DiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-base'''				)
										pipe.to(__lowercase				)
										pipe.set_progress_bar_config(disable=__lowercase				)

										lowerCAmelCase_			:   Any     					=					self.get_inputs(__lowercase				)
										lowerCAmelCase_			:   List[str]     					=					pipe(**__lowercase				).images
										lowerCAmelCase_			:   Dict     					=					image[0, -3:, -3:, -1].flatten()

										assert image.shape == (1, 5_1_2, 5_1_2, 3)
										lowerCAmelCase_			:   Optional[int]     					=					np.array([0.4_94_93, 0.4_78_96, 0.4_07_98, 0.5_42_14, 0.5_32_12, 0.4_82_02, 0.4_76_56, 0.4_63_29, 0.4_85_06]				)
										assert np.abs(image_slice - expected_slice				).max() < 7e-3