


"""simple docstring"""


import gc
import random
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionInstructPixaPixPipeline,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.utils import floats_tensor, load_image, 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_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()
class     _UpperCamelCase    (  lowerCAmelCase__			,lowerCAmelCase__			,lowerCAmelCase__			,unittest.TestCase						):
							'''simple docstring'''







							__UpperCAmelCase		:       Tuple							 =StableDiffusionInstructPixaPixPipeline
							__UpperCAmelCase		:       Optional[Any]							 =TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width""", """cross_attention_kwargs"""}
							__UpperCAmelCase		:       int							 =TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
							__UpperCAmelCase		:       Tuple							 =IMAGE_TO_IMAGE_IMAGE_PARAMS
							__UpperCAmelCase		:       List[str]							 =IMAGE_TO_IMAGE_IMAGE_PARAMS

							def      snake_case							(	self      ):
										torch.manual_seed(0      )
										__lowerCAmelCase							       =     UNetaDConditionModel(
										    block_out_channels=(32, 64)       ,  layers_per_block=2       ,  sample_size=32       ,  in_channels=8       ,  out_channels=4       ,  down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")       ,  up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")       ,  cross_attention_dim=32       ,  )
										__lowerCAmelCase							       =     PNDMScheduler(skip_prk_steps=__a      )
										torch.manual_seed(0      )
										__lowerCAmelCase							       =     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       ,  )
										torch.manual_seed(0      )
										__lowerCAmelCase							       =     CLIPTextConfig(
										    bos_token_id=0       ,  eos_token_id=2       ,  hidden_size=32       ,  intermediate_size=37       ,  layer_norm_eps=1e-0_5       ,  num_attention_heads=4       ,  num_hidden_layers=5       ,  pad_token_id=1       ,  vocab_size=10_00       ,  )
										__lowerCAmelCase							       =     CLIPTextModel(__a      )
										__lowerCAmelCase							       =     CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"      )

										__lowerCAmelCase							       =     {
										    "unet": unet,
										    "scheduler": scheduler,
										    "vae": vae,
										    "text_encoder": text_encoder,
										    "tokenizer": tokenizer,
										    "safety_checker": None,
										    "feature_extractor": None,
										}
										return components

							def      snake_case							(	self       ,  __a       ,  __a=0      ):
										__lowerCAmelCase							       =     floats_tensor((1, 3, 32, 32)       ,  rng=random.Random(__a      )      ).to(__a      )
										__lowerCAmelCase							       =     image.cpu().permute(0       ,  2       ,  3       ,  1      )[0]
										__lowerCAmelCase							       =     Image.fromarray(np.uinta(__a      )      ).convert("RGB"      )
										if str(__a      ).startswith("mps"      ):
													__lowerCAmelCase							       =     torch.manual_seed(__a      )
										else:
													__lowerCAmelCase							       =     torch.Generator(device=__a      ).manual_seed(__a      )
										__lowerCAmelCase							       =     {
										    "prompt": "A painting of a squirrel eating a burger",
										    "image": image,
										    "generator": generator,
										    "num_inference_steps": 2,
										    "guidance_scale": 6.0,
										    "image_guidance_scale": 1,
										    "output_type": "numpy",
										}
										return inputs

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     "cpu"  # ensure determinism for the device-dependent torch.Generator
										__lowerCAmelCase							       =     self.get_dummy_components()
										__lowerCAmelCase							       =     StableDiffusionInstructPixaPixPipeline(**__a      )
										__lowerCAmelCase							       =     sd_pipe.to(__a      )
										sd_pipe.set_progress_bar_config(disable=__a      )

										__lowerCAmelCase							       =     self.get_dummy_inputs(__a      )
										__lowerCAmelCase							       =     sd_pipe(**__a      ).images
										__lowerCAmelCase							       =     image[0, -3:, -3:, -1]
										assert image.shape == (1, 32, 32, 3)
										__lowerCAmelCase							       =     np.array([0.7_5_2_6, 0.3_7_5_0, 0.4_5_4_7, 0.6_1_1_7, 0.5_8_6_6, 0.5_0_1_6, 0.4_3_2_7, 0.5_6_4_2, 0.4_8_1_5]      )

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

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     "cpu"  # ensure determinism for the device-dependent torch.Generator
										__lowerCAmelCase							       =     self.get_dummy_components()
										__lowerCAmelCase							       =     StableDiffusionInstructPixaPixPipeline(**__a      )
										__lowerCAmelCase							       =     sd_pipe.to(__a      )
										sd_pipe.set_progress_bar_config(disable=__a      )

										__lowerCAmelCase							       =     self.get_dummy_inputs(__a      )
										__lowerCAmelCase							       =     "french fries"
										__lowerCAmelCase							       =     sd_pipe(**__a       ,  negative_prompt=__a      )
										__lowerCAmelCase							       =     output.images
										__lowerCAmelCase							       =     image[0, -3:, -3:, -1]

										assert image.shape == (1, 32, 32, 3)
										__lowerCAmelCase							       =     np.array([0.7_5_1_1, 0.3_6_4_2, 0.4_5_5_3, 0.6_2_3_6, 0.5_7_9_7, 0.5_0_1_3, 0.4_3_4_3, 0.5_6_1_1, 0.4_8_3_1]      )

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

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     "cpu"  # ensure determinism for the device-dependent torch.Generator
										__lowerCAmelCase							       =     self.get_dummy_components()
										__lowerCAmelCase							       =     StableDiffusionInstructPixaPixPipeline(**__a      )
										__lowerCAmelCase							       =     sd_pipe.to(__a      )
										sd_pipe.set_progress_bar_config(disable=__a      )

										__lowerCAmelCase							       =     self.get_dummy_inputs(__a      )
										__lowerCAmelCase							       =     [inputs["prompt"]] * 2

										__lowerCAmelCase							       =     np.array(inputs["image"]      ).astype(np.floataa      ) / 2_5_5.0
										__lowerCAmelCase							       =     torch.from_numpy(__a      ).unsqueeze(0      ).to(__a      )
										__lowerCAmelCase							       =     image / 2 + 0.5
										__lowerCAmelCase							       =     image.permute(0       ,  3       ,  1       ,  2      )
										__lowerCAmelCase							       =     image.repeat(2       ,  1       ,  1       ,  1      )

										__lowerCAmelCase							       =     sd_pipe(**__a      ).images
										__lowerCAmelCase							       =     image[-1, -3:, -3:, -1]

										assert image.shape == (2, 32, 32, 3)
										__lowerCAmelCase							       =     np.array([0.5_8_1_2, 0.5_7_4_8, 0.5_2_2_2, 0.5_9_0_8, 0.5_6_9_5, 0.7_1_7_4, 0.6_8_0_4, 0.5_5_2_3, 0.5_5_7_9]      )

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

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     "cpu"  # ensure determinism for the device-dependent torch.Generator
										__lowerCAmelCase							       =     self.get_dummy_components()
										__lowerCAmelCase							       =     EulerAncestralDiscreteScheduler(
										    beta_start=0.0_0_0_8_5       ,  beta_end=0.0_1_2       ,  beta_schedule="scaled_linear"      )
										__lowerCAmelCase							       =     StableDiffusionInstructPixaPixPipeline(**__a      )
										__lowerCAmelCase							       =     sd_pipe.to(__a      )
										sd_pipe.set_progress_bar_config(disable=__a      )

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

										__lowerCAmelCase							       =     [round(__a       ,  4      ) for x in image_slice.flatten().tolist()]
										print(",".join([str(__a      ) for x in slice]      )      )

										assert image.shape == (1, 32, 32, 3)
										__lowerCAmelCase							       =     np.array([0.7_4_1_7, 0.3_8_4_2, 0.4_7_3_2, 0.5_7_7_6, 0.5_8_9_1, 0.5_1_3_9, 0.4_0_5_2, 0.5_6_7_3, 0.4_9_8_6]      )

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

							def      snake_case							(	self      ):
										super().test_inference_batch_single_identical(expected_max_diff=3e-3      )



							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     self.get_dummy_components()
										__lowerCAmelCase							       =     StableDiffusionInstructPixaPixPipeline(**__a      )
										__lowerCAmelCase							       =     VaeImageProcessor(do_resize=__a       ,  do_normalize=__a      )
										__lowerCAmelCase							       =     pipe.to(__a      )
										pipe.set_progress_bar_config(disable=__a      )

										__lowerCAmelCase							       =     pipe(**self.get_dummy_inputs_by_type(__a       ,  input_image_type="pt"      )      )[0]

										__lowerCAmelCase							       =     components["vae"]
										__lowerCAmelCase							       =     self.get_dummy_inputs_by_type(__a       ,  input_image_type="pt"      )

										for image_param in self.image_latents_params:
													if image_param in inputs.keys():
																__lowerCAmelCase							       =     vae.encode(inputs[image_param]      ).latent_dist.mode()

										__lowerCAmelCase							       =     pipe(**__a      )[0]

										__lowerCAmelCase							       =     np.abs(out - out_latents_inputs      ).max()
										self.assertLess(__a       ,  1e-4       ,  "passing latents as image input generate different result from passing image"      )
@slow
@require_torch_gpu
class     _UpperCamelCase    (  unittest.TestCase						):
							'''simple docstring'''







							def      snake_case							(	self      ):
										super().tearDown()
										gc.collect()
										torch.cuda.empty_cache()

							def      snake_case							(	self       ,  __a=0      ):
										__lowerCAmelCase							       =     torch.manual_seed(__a      )
										__lowerCAmelCase							       =     load_image(
										    "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg"      )
										__lowerCAmelCase							       =     {
										    "prompt": "turn him into a cyborg",
										    "image": image,
										    "generator": generator,
										    "num_inference_steps": 3,
										    "guidance_scale": 7.5,
										    "image_guidance_scale": 1.0,
										    "output_type": "numpy",
										}
										return inputs

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     StableDiffusionInstructPixaPixPipeline.from_pretrained(
										    "timbrooks/instruct-pix2pix"       ,  safety_checker=__a      )
										pipe.to(__a      )
										pipe.set_progress_bar_config(disable=__a      )
										pipe.enable_attention_slicing()

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

										assert image.shape == (1, 5_12, 5_12, 3)
										__lowerCAmelCase							       =     np.array([0.5_9_0_2, 0.6_0_1_5, 0.6_0_2_7, 0.5_9_8_3, 0.6_0_9_2, 0.6_0_6_1, 0.5_7_6_5, 0.5_7_8_5, 0.5_5_5_5]      )

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

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     StableDiffusionInstructPixaPixPipeline.from_pretrained(
										    "timbrooks/instruct-pix2pix"       ,  safety_checker=__a      )
										__lowerCAmelCase							       =     LMSDiscreteScheduler.from_config(pipe.scheduler.config      )
										pipe.to(__a      )
										pipe.set_progress_bar_config(disable=__a      )
										pipe.enable_attention_slicing()

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

										assert image.shape == (1, 5_12, 5_12, 3)
										__lowerCAmelCase							       =     np.array([0.6_5_7_8, 0.6_8_1_7, 0.6_9_7_2, 0.6_7_6_1, 0.6_8_5_6, 0.6_9_1_6, 0.6_4_2_8, 0.6_5_1_6, 0.6_3_0_1]      )

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

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     StableDiffusionInstructPixaPixPipeline.from_pretrained(
										    "timbrooks/instruct-pix2pix"       ,  safety_checker=__a      )
										__lowerCAmelCase							       =     DDIMScheduler.from_config(pipe.scheduler.config      )
										pipe.to(__a      )
										pipe.set_progress_bar_config(disable=__a      )
										pipe.enable_attention_slicing()

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

										assert image.shape == (1, 5_12, 5_12, 3)
										__lowerCAmelCase							       =     np.array([0.3_8_2_8, 0.3_8_3_4, 0.3_8_1_8, 0.3_7_9_2, 0.3_8_6_5, 0.3_7_5_2, 0.3_7_9_2, 0.3_8_4_7, 0.3_7_5_3]      )

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

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     0

										def callback_fn(__a       ,  __a       ,  __a      ) -> None:
													__lowerCAmelCase							       =     True
													nonlocal number_of_steps
													number_of_steps += 1
													if step == 1:
																__lowerCAmelCase							       =     latents.detach().cpu().numpy()
																assert latents.shape == (1, 4, 64, 64)
																__lowerCAmelCase							       =     latents[0, -3:, -3:, -1]
																__lowerCAmelCase							       =     np.array([-0.2_4_6_3, -0.4_6_4_4, -0.9_7_5_6, 1.5_1_7_6, 1.4_4_1_4, 0.7_8_6_6, 0.9_8_9_7, 0.8_5_2_1, 0.7_9_8_3]      )

																assert np.abs(latents_slice.flatten() - expected_slice      ).max() < 5e-2
													elif step == 2:
																__lowerCAmelCase							       =     latents.detach().cpu().numpy()
																assert latents.shape == (1, 4, 64, 64)
																__lowerCAmelCase							       =     latents[0, -3:, -3:, -1]
																__lowerCAmelCase							       =     np.array([-0.2_6_4_4, -0.4_6_2_6, -0.9_6_5_3, 1.5_1_7_6, 1.4_5_5_1, 0.7_6_8_6, 0.9_8_0_5, 0.8_4_5_2, 0.8_1_1_5]      )

																assert np.abs(latents_slice.flatten() - expected_slice      ).max() < 5e-2

										__lowerCAmelCase							       =     False

										__lowerCAmelCase							       =     StableDiffusionInstructPixaPixPipeline.from_pretrained(
										    "timbrooks/instruct-pix2pix"       ,  safety_checker=__a       ,  torch_dtype=torch.floataa      )
										__lowerCAmelCase							       =     pipe.to(__a      )
										pipe.set_progress_bar_config(disable=__a      )
										pipe.enable_attention_slicing()

										__lowerCAmelCase							       =     self.get_inputs()
										pipe(**__a       ,  callback=__a       ,  callback_steps=1      )
										assert callback_fn.has_been_called
										assert number_of_steps == 3

							def      snake_case							(	self      ):
										torch.cuda.empty_cache()
										torch.cuda.reset_max_memory_allocated()
										torch.cuda.reset_peak_memory_stats()

										__lowerCAmelCase							       =     StableDiffusionInstructPixaPixPipeline.from_pretrained(
										    "timbrooks/instruct-pix2pix"       ,  safety_checker=__a       ,  torch_dtype=torch.floataa      )
										__lowerCAmelCase							       =     pipe.to(__a      )
										pipe.set_progress_bar_config(disable=__a      )
										pipe.enable_attention_slicing(1      )
										pipe.enable_sequential_cpu_offload()

										__lowerCAmelCase							       =     self.get_inputs()
										__lowerCAmelCase							       =     pipe(**__a      )

										__lowerCAmelCase							       =     torch.cuda.max_memory_allocated()
										# make sure that less than 2.2 GB is allocated
										assert mem_bytes < 2.2 * 10**9



							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     self.get_inputs()
										# resize to resolution that is divisible by 8 but not 16 or 32
										__lowerCAmelCase							       =     inputs["image"].resize((5_04, 5_04)      )

										__lowerCAmelCase							       =     "timbrooks/instruct-pix2pix"
										__lowerCAmelCase							       =     StableDiffusionInstructPixaPixPipeline.from_pretrained(
										    __a       ,  safety_checker=__a       ,  )
										pipe.to(__a      )
										pipe.set_progress_bar_config(disable=__a      )
										pipe.enable_attention_slicing()

										__lowerCAmelCase							       =     pipe(**__a      )
										__lowerCAmelCase							       =     output.images[0]

										__lowerCAmelCase							       =     image[2_55:2_58, 3_83:3_86, -1]

										assert image.shape == (5_04, 5_04, 3)
										__lowerCAmelCase							       =     np.array([0.2_7_2_6, 0.2_5_2_9, 0.2_6_6_4, 0.2_6_5_5, 0.2_6_4_1, 0.2_6_4_2, 0.2_5_9_1, 0.2_6_4_9, 0.2_5_9_0]      )

										assert np.abs(image_slice.flatten() - expected_slice      ).max() < 5e-3








"""simple docstring"""


import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
	from .tokenization_xlnet import XLNetTokenizer
else:
	A	:  Optional[Any]            =     None


A	:  Any            =     logging.get_logger(__name__)

A	:  Union[str, Any]            =     {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"}

A	:  List[Any]            =     {
    "vocab_file": {
        "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model",
        "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model",
    },
    "tokenizer_file": {
        "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/tokenizer.json",
        "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/tokenizer.json",
    },
}

A	:  Union[str, Any]            =     {
    "xlnet-base-cased": None,
    "xlnet-large-cased": None,
}

A	:  Optional[int]            =     "▁"

# Segments (not really needed)
A	:  str            =     0
A	:  Dict            =     1
A	:  List[str]            =     2
A	:  Tuple            =     3
A	:  Tuple            =     4
class     _UpperCamelCase    (  lowerCAmelCase__						):
							'''simple docstring'''







							__UpperCAmelCase		:       Optional[Any]							 =VOCAB_FILES_NAMES
							__UpperCAmelCase		:       Optional[int]							 =PRETRAINED_VOCAB_FILES_MAP
							__UpperCAmelCase		:       Dict							 =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							__UpperCAmelCase		:       List[Any]							 ="""left"""
							__UpperCAmelCase		:       List[str]							 =XLNetTokenizer

							def __init__(	self       ,  __a=None       ,  __a=None       ,  __a=False       ,  __a=True       ,  __a=False       ,  __a="<s>"       ,  __a="</s>"       ,  __a="<unk>"       ,  __a="<sep>"       ,  __a="<pad>"       ,  __a="<cls>"       ,  __a="<mask>"       ,  __a=["<eop>", "<eod>"]       ,  **__a       ,  ):
										# Mask token behave like a normal word, i.e. include the space before it
										__lowerCAmelCase							       =     AddedToken(__a       ,  lstrip=__a       ,  rstrip=__a      ) if isinstance(__a       ,  __a      ) else mask_token

										super().__init__(
										    vocab_file=__a       ,  tokenizer_file=__a       ,  do_lower_case=__a       ,  remove_space=__a       ,  keep_accents=__a       ,  bos_token=__a       ,  eos_token=__a       ,  unk_token=__a       ,  sep_token=__a       ,  pad_token=__a       ,  cls_token=__a       ,  mask_token=__a       ,  additional_special_tokens=__a       ,  **__a       ,  )

										__lowerCAmelCase							       =     3
										__lowerCAmelCase							       =     do_lower_case
										__lowerCAmelCase							       =     remove_space
										__lowerCAmelCase							       =     keep_accents
										__lowerCAmelCase							       =     vocab_file
										__lowerCAmelCase							       =     False if not self.vocab_file else True

							def      snake_case							(	self       ,  __a       ,  __a = None      ):
										__lowerCAmelCase							       =     [self.sep_token_id]
										__lowerCAmelCase							       =     [self.cls_token_id]
										if token_ids_a is None:
													return token_ids_a + sep + cls
										return token_ids_a + sep + token_ids_a + sep + cls

							def      snake_case							(	self       ,  __a       ,  __a = None      ):
										__lowerCAmelCase							       =     [self.sep_token_id]
										__lowerCAmelCase							       =     [2]

										if token_ids_a is None:
													return len(token_ids_a + sep      ) * [0] + cls_segment_id
										return len(token_ids_a + sep      ) * [0] + len(token_ids_a + sep      ) * [1] + cls_segment_id



							def      snake_case							(	self       ,  __a       ,  __a = None      ):
										if not self.can_save_slow_tokenizer:
													raise ValueError(
													    "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow "
													    "tokenizer."      )

										if not os.path.isdir(__a      ):
													logger.error(f"Vocabulary path ({save_directory}) should be a directory"      )
													return
										__lowerCAmelCase							       =     os.path.join(
										    __a       ,  (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]      )

										if os.path.abspath(self.vocab_file      ) != os.path.abspath(__a      ):
													copyfile(self.vocab_file       ,  __a      )

										return (out_vocab_file,)




