






"""simple docstring"""







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    __UpperCAmelCase(       SCREAMING_SNAKE_CASE__  ,  SCREAMING_SNAKE_CASE__  ,  unittest.TestCase							):




						"""simple docstring"""




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




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												torch.manual_seed(0      )
												lowercase__    :				int=	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=snake_case__	,  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	,  )
												lowercase__    :				Tuple=	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      )
												lowercase__    :				Any=	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      )
												lowercase__    :				Dict=	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=32	,  )
												lowercase__    :				List[Any]=	CLIPTextModel(snake_case__      )
												lowercase__    :				Union[str, Any]=	CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"	,  local_files_only=snake_case__      )

												lowercase__    :				Optional[Any]=	CLIPTextModelWithProjection(snake_case__      )
												lowercase__    :				int=	CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"	,  local_files_only=snake_case__      )

												lowercase__    :				Tuple=	{
												    "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 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__=0      ):
												'''simple docstring'''
												lowercase__    :				Dict=	floats_tensor((1, 3, 32, 32)	,  rng=random.Random(snake_case__      )      ).to(snake_case__      )
												lowercase__    :				Union[str, Any]=	image / 2 + 0.5
												if str(snake_case__      ).startswith("mps"      ):
																		lowercase__    :				Optional[Any]=	torch.manual_seed(snake_case__      )
												else:
																		lowercase__    :				Tuple=	torch.Generator(device=snake_case__      ).manual_seed(snake_case__      )
												lowercase__    :				int=	{
												    "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 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Tuple=	"cpu"  # ensure determinism for the device-dependent torch.Generator
												lowercase__    :				Tuple=	self.get_dummy_components()
												lowercase__    :				Optional[Any]=	StableDiffusionXLImgaImgPipeline(**snake_case__      )
												lowercase__    :				Union[str, Any]=	sd_pipe.to(snake_case__      )
												sd_pipe.set_progress_bar_config(disable=snake_case__      )

												lowercase__    :				Dict=	self.get_dummy_inputs(snake_case__      )
												lowercase__    :				List[Any]=	sd_pipe(**snake_case__      ).images
												lowercase__    :				Optional[Any]=	image[0, -3:, -3:, -1]

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

												lowercase__    :				Tuple=	np.array([0.46_56, 0.48_40, 0.44_39, 0.66_98, 0.55_74, 0.45_24, 0.57_99, 0.59_43, 0.51_65]      )

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




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												super().test_attention_slicing_forward_pass(expected_max_diff=3e-3      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												super().test_inference_batch_single_identical(expected_max_diff=3e-3      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												pass




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Optional[int]=	self.get_dummy_components()
												lowercase__    :				Optional[int]=	StableDiffusionXLImgaImgPipeline(**snake_case__      )
												lowercase__    :				List[Any]=	sd_pipe.to(snake_case__      )
												lowercase__    :				Dict=	sd_pipe.to(snake_case__      )
												sd_pipe.set_progress_bar_config(disable=snake_case__      )

												# forward without prompt embeds
												lowercase__    :				Union[str, Any]=	self.get_dummy_inputs(snake_case__      )
												lowercase__    :				Tuple=	3 * ["this is a negative prompt"]
												lowercase__    :				Tuple=	negative_prompt
												lowercase__    :				Optional[int]=	3 * [inputs["prompt"]]

												lowercase__    :				List[Any]=	sd_pipe(**snake_case__      )
												lowercase__    :				Tuple=	output.images[0, -3:, -3:, -1]

												# forward with prompt embeds
												lowercase__    :				Optional[int]=	self.get_dummy_inputs(snake_case__      )
												lowercase__    :				Dict=	3 * ["this is a negative prompt"]
												lowercase__    :				Optional[int]=	3 * [inputs.pop("prompt"      )]

												(
												    (
												    lowercase__
												),     (
												    lowercase__
												),     (
												    lowercase__
												),     (
												    lowercase__
												),     
												)    :				Optional[int]=	sd_pipe.encode_prompt(snake_case__	,  negative_prompt=snake_case__      )

												lowercase__    :				Optional[Any]=	sd_pipe(
												    **snake_case__	,  prompt_embeds=snake_case__	,  negative_prompt_embeds=snake_case__	,  pooled_prompt_embeds=snake_case__	,  negative_pooled_prompt_embeds=snake_case__	,  )
												lowercase__    :				Union[str, Any]=	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    __UpperCAmelCase(       unittest.TestCase							):




						"""simple docstring"""




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()




						def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__="cpu"	,  snake_case__=torch.floataa	,  snake_case__=0      ):
												'''simple docstring'''
												lowercase__    :				Optional[Any]=	torch.Generator(device=snake_case__      ).manual_seed(snake_case__      )
												lowercase__    :				Any=	np.random.RandomState(snake_case__      ).standard_normal((1, 4, 64, 64)      )
												lowercase__    :				Optional[Any]=	torch.from_numpy(snake_case__      ).to(device=snake_case__	,  dtype=snake_case__      )
												lowercase__    :				Union[str, 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 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Optional[Any]=	DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-base"      )
												pipe.to(snake_case__      )
												pipe.set_progress_bar_config(disable=snake_case__      )

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

												assert image.shape == (1, 512, 512, 3)
												lowercase__    :				Any=	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














"""simple docstring"""







import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
			from PIL import Image

			from transformers import (
			    AutoProcessor,
			    BertTokenizerFast,
			    BlipImageProcessor,
			    GPTaTokenizer,
			    InstructBlipProcessor,
			    PreTrainedTokenizerFast,
			)


@require_vision
class    __UpperCAmelCase(       unittest.TestCase							):




						"""simple docstring"""




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				List[str]=	tempfile.mkdtemp()

												lowercase__    :				Union[str, Any]=	BlipImageProcessor()
												lowercase__    :				Any=	GPTaTokenizer.from_pretrained("hf-internal-testing/tiny-random-GPT2Model"      )
												lowercase__    :				Any=	BertTokenizerFast.from_pretrained("hf-internal-testing/tiny-random-bert"      )

												lowercase__    :				str=	InstructBlipProcessor(snake_case__	,  snake_case__	,  snake_case__      )

												processor.save_pretrained(self.tmpdirname      )




						def 		UpperCAmelCase_	(       self	,  **snake_case__      ):
												'''simple docstring'''
												return AutoProcessor.from_pretrained(self.tmpdirname	,  **snake_case__      ).tokenizer




						def 		UpperCAmelCase_	(       self	,  **snake_case__      ):
												'''simple docstring'''
												return AutoProcessor.from_pretrained(self.tmpdirname	,  **snake_case__      ).image_processor




						def 		UpperCAmelCase_	(       self	,  **snake_case__      ):
												'''simple docstring'''
												return AutoProcessor.from_pretrained(self.tmpdirname	,  **snake_case__      ).qformer_tokenizer




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												shutil.rmtree(self.tmpdirname      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Union[str, Any]=	[np.random.randint(255	,  size=(3, 30, 400)	,  dtype=np.uinta      )]

												lowercase__    :				Union[str, Any]=	[Image.fromarray(np.moveaxis(snake_case__	,  0	,  -1      )      ) for x in image_inputs]

												return image_inputs




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				str=	InstructBlipProcessor(
												    tokenizer=self.get_tokenizer()	,  image_processor=self.get_image_processor()	,  qformer_tokenizer=self.get_qformer_tokenizer()	,  )
												processor.save_pretrained(self.tmpdirname      )

												lowercase__    :				Optional[Any]=	self.get_tokenizer(bos_token="(BOS)"	,  eos_token="(EOS)"      )
												lowercase__    :				Optional[Any]=	self.get_image_processor(do_normalize=snake_case__	,  padding_value=1.0      )

												lowercase__    :				Optional[Any]=	InstructBlipProcessor.from_pretrained(
												    self.tmpdirname	,  bos_token="(BOS)"	,  eos_token="(EOS)"	,  do_normalize=snake_case__	,  padding_value=1.0      )

												self.assertEqual(processor.tokenizer.get_vocab()	,  tokenizer_add_kwargs.get_vocab()      )
												self.assertIsInstance(processor.tokenizer	,  snake_case__      )

												self.assertEqual(processor.image_processor.to_json_string()	,  image_processor_add_kwargs.to_json_string()      )
												self.assertIsInstance(processor.image_processor	,  snake_case__      )
												self.assertIsInstance(processor.qformer_tokenizer	,  snake_case__      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Tuple=	self.get_image_processor()
												lowercase__    :				Optional[int]=	self.get_tokenizer()
												lowercase__    :				Any=	self.get_qformer_tokenizer()

												lowercase__    :				Any=	InstructBlipProcessor(
												    tokenizer=snake_case__	,  image_processor=snake_case__	,  qformer_tokenizer=snake_case__      )

												lowercase__    :				Tuple=	self.prepare_image_inputs()

												lowercase__    :				int=	image_processor(snake_case__	,  return_tensors="np"      )
												lowercase__    :				List[str]=	processor(images=snake_case__	,  return_tensors="np"      )

												for key in input_feat_extract.keys():
																		self.assertAlmostEqual(input_feat_extract[key].sum()	,  input_processor[key].sum()	,  delta=1e-2      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				List[Any]=	self.get_image_processor()
												lowercase__    :				List[str]=	self.get_tokenizer()
												lowercase__    :				Optional[Any]=	self.get_qformer_tokenizer()

												lowercase__    :				Optional[Any]=	InstructBlipProcessor(
												    tokenizer=snake_case__	,  image_processor=snake_case__	,  qformer_tokenizer=snake_case__      )

												lowercase__    :				Optional[Any]=	"lower newer"

												lowercase__    :				Any=	processor(text=snake_case__      )

												lowercase__    :				Union[str, Any]=	tokenizer(snake_case__	,  return_token_type_ids=snake_case__      )
												lowercase__    :				List[Any]=	qformer_tokenizer(snake_case__	,  return_token_type_ids=snake_case__      )

												for key in encoded_tokens.keys():
																		self.assertListEqual(encoded_tokens[key]	,  encoded_processor[key]      )

												for key in encoded_tokens_qformer.keys():
																		self.assertListEqual(encoded_tokens_qformer[key]	,  encoded_processor["qformer_" + key]      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				int=	self.get_image_processor()
												lowercase__    :				Optional[int]=	self.get_tokenizer()
												lowercase__    :				Any=	self.get_qformer_tokenizer()

												lowercase__    :				Optional[int]=	InstructBlipProcessor(
												    tokenizer=snake_case__	,  image_processor=snake_case__	,  qformer_tokenizer=snake_case__      )

												lowercase__    :				List[str]=	"lower newer"
												lowercase__    :				Optional[int]=	self.prepare_image_inputs()

												lowercase__    :				Dict=	processor(text=snake_case__	,  images=snake_case__      )

												self.assertListEqual(
												    list(inputs.keys()      )	,  ["input_ids", "attention_mask", "qformer_input_ids", "qformer_attention_mask", "pixel_values"]	,  )

												# test if it raises when no input is passed
												with pytest.raises(snake_case__      ):
																		processor()




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Optional[Any]=	self.get_image_processor()
												lowercase__    :				int=	self.get_tokenizer()
												lowercase__    :				Optional[int]=	self.get_qformer_tokenizer()

												lowercase__    :				Any=	InstructBlipProcessor(
												    tokenizer=snake_case__	,  image_processor=snake_case__	,  qformer_tokenizer=snake_case__      )

												lowercase__    :				Dict=	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

												lowercase__    :				Optional[Any]=	processor.batch_decode(snake_case__      )
												lowercase__    :				List[Any]=	tokenizer.batch_decode(snake_case__      )

												self.assertListEqual(snake_case__	,  snake_case__      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				str=	self.get_image_processor()
												lowercase__    :				Optional[int]=	self.get_tokenizer()
												lowercase__    :				Optional[Any]=	self.get_qformer_tokenizer()

												lowercase__    :				int=	InstructBlipProcessor(
												    tokenizer=snake_case__	,  image_processor=snake_case__	,  qformer_tokenizer=snake_case__      )

												lowercase__    :				Union[str, Any]=	"lower newer"
												lowercase__    :				int=	self.prepare_image_inputs()

												lowercase__    :				Dict=	processor(text=snake_case__	,  images=snake_case__      )

												self.assertListEqual(
												    list(inputs.keys()      )	,  ["input_ids", "attention_mask", "qformer_input_ids", "qformer_attention_mask", "pixel_values"]	,  )






