

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, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel
from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow

from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()





class 	snake_case_   (		__A	,       __A	,       __A	,       unittest.TestCase					):







			'''simple docstring'''

			SCREAMING_SNAKE_CASE     :   Tuple									=				StableDiffusionInpaintPipeline
			SCREAMING_SNAKE_CASE     :   Optional[int]									=				TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
			SCREAMING_SNAKE_CASE     :   Optional[Any]									=				TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
			SCREAMING_SNAKE_CASE     :   Dict									=				frozenset(
			    []					)  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
			SCREAMING_SNAKE_CASE     :   List[str]									=				frozenset([]					)







			def 				snake_case__(     self :				int							)      ->Optional[Any]:
									torch.manual_seed(0							)
									snake_case_   =	UNetaDConditionModel(
									    block_out_channels=(3_2, 6_4)							,   layers_per_block=2							,   sample_size=3_2							,   in_channels=9							,   out_channels=4							,   down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')							,   up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')							,   cross_attention_dim=3_2							,   attention_head_dim=(2, 4)							,   use_linear_projection=_UpperCamelCase							,   )
									snake_case_   =	PNDMScheduler(skip_prk_steps=_UpperCamelCase							)
									torch.manual_seed(0							)
									snake_case_   =	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							)
									snake_case_   =	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=5_1_2							,   )
									snake_case_   =	CLIPTextModel(_UpperCamelCase							)
									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 				snake_case__(     self :				int							,   _UpperCamelCase :				List[Any]							,   _UpperCamelCase :				Optional[Any]=0							)      ->List[str]:
									# TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched
									snake_case_   =	floats_tensor((1, 3, 3_2, 3_2)							,   rng=random.Random(_UpperCamelCase							)							).to(_UpperCamelCase							)
									snake_case_   =	image.cpu().permute(0							,   2							,   3							,   1							)[0]
									snake_case_   =	Image.fromarray(np.uinta(_UpperCamelCase							)							).convert('''RGB'''							).resize((6_4, 6_4)							)
									snake_case_   =	Image.fromarray(np.uinta(image + 4							)							).convert('''RGB'''							).resize((6_4, 6_4)							)
									if str(_UpperCamelCase							).startswith('''mps'''							):
															snake_case_   =	torch.manual_seed(_UpperCamelCase							)
									else:
															snake_case_   =	torch.Generator(device=_UpperCamelCase							).manual_seed(_UpperCamelCase							)
									snake_case_   =	{
									    '''prompt''': '''A painting of a squirrel eating a burger''',
									    '''image''': init_image,
									    '''mask_image''': mask_image,
									    '''generator''': generator,
									    '''num_inference_steps''': 2,
									    '''guidance_scale''': 6.0,
									    '''output_type''': '''numpy''',
									}
									return inputs







			def 				snake_case__(     self :				Union[str, Any]							)      ->Optional[Any]:
									snake_case_   =	'''cpu'''  # ensure determinism for the device-dependent torch.Generator
									snake_case_   =	self.get_dummy_components()
									snake_case_   =	StableDiffusionInpaintPipeline(**_UpperCamelCase							)
									snake_case_   =	sd_pipe.to(_UpperCamelCase							)
									sd_pipe.set_progress_bar_config(disable=_UpperCamelCase							)

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

									assert image.shape == (1, 6_4, 6_4, 3)
									snake_case_   =	np.array([0.4727, 0.5735, 0.3941, 0.5446, 0.5926, 0.4394, 0.5062, 0.4654, 0.4476]							)

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







			def 				snake_case__(     self :				str							)      ->int:
									super().test_inference_batch_single_identical(expected_max_diff=3e-3							)






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







			'''simple docstring'''

			def 				snake_case__(     self :				Optional[int]							)      ->Union[str, Any]:
									# clean up the VRAM after each test
									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()







			def 				snake_case__(     self :				List[str]							)      ->Optional[Any]:
									snake_case_   =	load_image(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
									    '''/sd2-inpaint/init_image.png'''							)
									snake_case_   =	load_image(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png'''							)
									snake_case_   =	load_numpy(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint'''
									    '''/yellow_cat_sitting_on_a_park_bench.npy'''							)

									snake_case_   =	'''stabilityai/stable-diffusion-2-inpainting'''
									snake_case_   =	StableDiffusionInpaintPipeline.from_pretrained(_UpperCamelCase							,   safety_checker=_UpperCamelCase							)
									pipe.to(_UpperCamelCase							)
									pipe.set_progress_bar_config(disable=_UpperCamelCase							)
									pipe.enable_attention_slicing()

									snake_case_   =	'''Face of a yellow cat, high resolution, sitting on a park bench'''

									snake_case_   =	torch.manual_seed(0							)
									snake_case_   =	pipe(
									    prompt=_UpperCamelCase							,   image=_UpperCamelCase							,   mask_image=_UpperCamelCase							,   generator=_UpperCamelCase							,   output_type='''np'''							,   )
									snake_case_   =	output.images[0]

									assert image.shape == (5_1_2, 5_1_2, 3)
									assert np.abs(expected_image - image							).max() < 9e-3







			def 				snake_case__(     self :				str							)      ->Optional[int]:
									snake_case_   =	load_image(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
									    '''/sd2-inpaint/init_image.png'''							)
									snake_case_   =	load_image(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png'''							)
									snake_case_   =	load_numpy(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint'''
									    '''/yellow_cat_sitting_on_a_park_bench_fp16.npy'''							)

									snake_case_   =	'''stabilityai/stable-diffusion-2-inpainting'''
									snake_case_   =	StableDiffusionInpaintPipeline.from_pretrained(
									    _UpperCamelCase							,   torch_dtype=torch.floataa							,   safety_checker=_UpperCamelCase							,   )
									pipe.to(_UpperCamelCase							)
									pipe.set_progress_bar_config(disable=_UpperCamelCase							)
									pipe.enable_attention_slicing()

									snake_case_   =	'''Face of a yellow cat, high resolution, sitting on a park bench'''

									snake_case_   =	torch.manual_seed(0							)
									snake_case_   =	pipe(
									    prompt=_UpperCamelCase							,   image=_UpperCamelCase							,   mask_image=_UpperCamelCase							,   generator=_UpperCamelCase							,   output_type='''np'''							,   )
									snake_case_   =	output.images[0]

									assert image.shape == (5_1_2, 5_1_2, 3)
									assert np.abs(expected_image - image							).max() < 5e-1







			def 				snake_case__(     self :				Union[str, Any]							)      ->int:
									torch.cuda.empty_cache()
									torch.cuda.reset_max_memory_allocated()
									torch.cuda.reset_peak_memory_stats()

									snake_case_   =	load_image(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
									    '''/sd2-inpaint/init_image.png'''							)
									snake_case_   =	load_image(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png'''							)

									snake_case_   =	'''stabilityai/stable-diffusion-2-inpainting'''
									snake_case_   =	PNDMScheduler.from_pretrained(_UpperCamelCase							,   subfolder='''scheduler'''							)
									snake_case_   =	StableDiffusionInpaintPipeline.from_pretrained(
									    _UpperCamelCase							,   safety_checker=_UpperCamelCase							,   scheduler=_UpperCamelCase							,   torch_dtype=torch.floataa							,   )
									pipe.to(_UpperCamelCase							)
									pipe.set_progress_bar_config(disable=_UpperCamelCase							)
									pipe.enable_attention_slicing(1							)
									pipe.enable_sequential_cpu_offload()

									snake_case_   =	'''Face of a yellow cat, high resolution, sitting on a park bench'''

									snake_case_   =	torch.manual_seed(0							)
									snake_case_   =	pipe(
									    prompt=_UpperCamelCase							,   image=_UpperCamelCase							,   mask_image=_UpperCamelCase							,   generator=_UpperCamelCase							,   num_inference_steps=2							,   output_type='''np'''							,   )

									snake_case_   =	torch.cuda.max_memory_allocated()
									# make sure that less than 2.65 GB is allocated
									assert mem_bytes < 2.65 * 1_0**9


import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from .tokenization_lxmert import LxmertTokenizer


lowerCAmelCase_       	=	{'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''}

lowerCAmelCase_       	=	{
    '''vocab_file''': {
        '''unc-nlp/lxmert-base-uncased''': '''https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/vocab.txt''',
    },
    '''tokenizer_file''': {
        '''unc-nlp/lxmert-base-uncased''': (
            '''https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/tokenizer.json'''
        ),
    },
}

lowerCAmelCase_       	=	{
    '''unc-nlp/lxmert-base-uncased''': 5_12,
}

lowerCAmelCase_       	=	{
    '''unc-nlp/lxmert-base-uncased''': {'''do_lower_case''': True},
}





class 	snake_case_   (		__A					):







			'''simple docstring'''

			SCREAMING_SNAKE_CASE     :   Optional[Any]									=				VOCAB_FILES_NAMES
			SCREAMING_SNAKE_CASE     :   Any									=				PRETRAINED_VOCAB_FILES_MAP
			SCREAMING_SNAKE_CASE     :   List[Any]									=				PRETRAINED_INIT_CONFIGURATION
			SCREAMING_SNAKE_CASE     :   Optional[int]									=				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			SCREAMING_SNAKE_CASE     :   Any									=				LxmertTokenizer







			def __init__(     self :				Union[str, Any]							,   _UpperCamelCase :				int=None							,   _UpperCamelCase :				Optional[Any]=None							,   _UpperCamelCase :				Dict=True							,   _UpperCamelCase :				Any="[UNK]"							,   _UpperCamelCase :				Tuple="[SEP]"							,   _UpperCamelCase :				List[Any]="[PAD]"							,   _UpperCamelCase :				Union[str, Any]="[CLS]"							,   _UpperCamelCase :				str="[MASK]"							,   _UpperCamelCase :				List[str]=True							,   _UpperCamelCase :				List[str]=None							,   **_UpperCamelCase :				List[str]							,   )      ->Any:
									super().__init__(
									    _UpperCamelCase							,   tokenizer_file=_UpperCamelCase							,   do_lower_case=_UpperCamelCase							,   unk_token=_UpperCamelCase							,   sep_token=_UpperCamelCase							,   pad_token=_UpperCamelCase							,   cls_token=_UpperCamelCase							,   mask_token=_UpperCamelCase							,   tokenize_chinese_chars=_UpperCamelCase							,   strip_accents=_UpperCamelCase							,   **_UpperCamelCase							,   )

									snake_case_   =	json.loads(self.backend_tokenizer.normalizer.__getstate__()							)
									if (
									    normalizer_state.get('''lowercase'''							,   _UpperCamelCase							) != do_lower_case
									    or normalizer_state.get('''strip_accents'''							,   _UpperCamelCase							) != strip_accents
									    or normalizer_state.get('''handle_chinese_chars'''							,   _UpperCamelCase							) != tokenize_chinese_chars
									):
															snake_case_   =	getattr(_UpperCamelCase							,   normalizer_state.pop('''type'''							)							)
															snake_case_   =	do_lower_case
															snake_case_   =	strip_accents
															snake_case_   =	tokenize_chinese_chars
															snake_case_   =	normalizer_class(**_UpperCamelCase							)

									snake_case_   =	do_lower_case







			def 				snake_case__(     self :				Optional[int]							,   _UpperCamelCase :				List[Any]							,   _UpperCamelCase :				List[str]=None							)      ->List[Any]:
									snake_case_   =	[self.cls_token_id] + token_ids_a + [self.sep_token_id]

									if token_ids_a:
															output += token_ids_a + [self.sep_token_id]

									return output







			def 				snake_case__(     self :				int							,   _UpperCamelCase :				List[int]							,   _UpperCamelCase :				Optional[List[int]] = None							)      ->List[int]:
									snake_case_   =	[self.sep_token_id]
									snake_case_   =	[self.cls_token_id]
									if token_ids_a is None:
															return len(cls + token_ids_a + sep							) * [0]
									return len(cls + token_ids_a + sep							) * [0] + len(token_ids_a + sep							) * [1]







			def 				snake_case__(     self :				Any							,   _UpperCamelCase :				str							,   _UpperCamelCase :				Optional[str] = None							)      ->Tuple[str]:
									snake_case_   =	self._tokenizer.model.save(_UpperCamelCase							,   name=_UpperCamelCase							)
									return tuple(_UpperCamelCase							)