


'''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, 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 						UpperCamelCase__       (			SCREAMING_SNAKE_CASE_     ,       SCREAMING_SNAKE_CASE_     ,       SCREAMING_SNAKE_CASE_     ,       unittest.TestCase						):



     """simple docstring"""






     __magic_name__           =     StableDiffusionInpaintPipeline
     __magic_name__           =     TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
     __magic_name__           =     TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
     __magic_name__           =     frozenset(
         []						)  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
     __magic_name__           =     frozenset([]						)







     def a						(			self ):

            '''simple docstring'''

            torch.manual_seed(0 )
            _lowerCAmelCase     :				Optional[int]       =							UNetaDConditionModel(
                block_out_channels=(32, 64)   ,						layers_per_block=2   ,						sample_size=32   ,						in_channels=9   ,						out_channels=4   ,						down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')   ,						up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')   ,						cross_attention_dim=32   ,						attention_head_dim=(2, 4)   ,						use_linear_projection=snake_case__   ,						)
            _lowerCAmelCase     :				Optional[Any]       =							PNDMScheduler(skip_prk_steps=snake_case__ )
            torch.manual_seed(0 )
            _lowerCAmelCase     :				str       =							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 )
            _lowerCAmelCase     :				Optional[int]       =							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=512   ,						)
            _lowerCAmelCase     :				Tuple       =							CLIPTextModel(snake_case__ )
            _lowerCAmelCase     :				List[Any]       =							CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' )

            _lowerCAmelCase     :				List[Any]       =							{
                'unet': unet,
                'scheduler': scheduler,
                'vae': vae,
                'text_encoder': text_encoder,
                'tokenizer': tokenizer,
                'safety_checker': None,
                'feature_extractor': None,
            }
            return components







     def a						(			self   ,						snake_case__   ,						snake_case__=0 ):

            '''simple docstring'''

            _lowerCAmelCase     :				int       =							floats_tensor((1, 3, 32, 32)   ,						rng=random.Random(snake_case__ ) ).to(snake_case__ )
            _lowerCAmelCase     :				Optional[Any]       =							image.cpu().permute(0   ,						2   ,						3   ,						1 )[0]
            _lowerCAmelCase     :				int       =							Image.fromarray(np.uinta(snake_case__ ) ).convert('RGB' ).resize((64, 64) )
            _lowerCAmelCase     :				Optional[int]       =							Image.fromarray(np.uinta(image + 4 ) ).convert('RGB' ).resize((64, 64) )
            if str(snake_case__ ).startswith('mps' ):
                   _lowerCAmelCase     :				str       =							torch.manual_seed(snake_case__ )
            else:
                   _lowerCAmelCase     :				Union[str, Any]       =							torch.Generator(device=snake_case__ ).manual_seed(snake_case__ )
            _lowerCAmelCase     :				Union[str, Any]       =							{
                '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 a						(			self ):

            '''simple docstring'''

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

            _lowerCAmelCase     :				str       =							self.get_dummy_inputs(snake_case__ )
            _lowerCAmelCase     :				int       =							sd_pipe(**snake_case__ ).images
            _lowerCAmelCase     :				str       =							image[0, -3:, -3:, -1]

            assert image.shape == (1, 64, 64, 3)
            _lowerCAmelCase     :				Union[str, Any]       =							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 a						(			self ):

            '''simple docstring'''

            super().test_inference_batch_single_identical(expected_max_diff=3E-3 )
@slow
@require_torch_gpu
class 						UpperCamelCase__       (			unittest.TestCase						):



     """simple docstring"""






     def a						(			self ):

            '''simple docstring'''

            super().tearDown()
            gc.collect()
            torch.cuda.empty_cache()







     def a						(			self ):

            '''simple docstring'''

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

            _lowerCAmelCase     :				List[str]       =							'stabilityai/stable-diffusion-2-inpainting'
            _lowerCAmelCase     :				int       =							StableDiffusionInpaintPipeline.from_pretrained(snake_case__   ,						safety_checker=snake_case__ )
            pipe.to(snake_case__ )
            pipe.set_progress_bar_config(disable=snake_case__ )
            pipe.enable_attention_slicing()

            _lowerCAmelCase     :				List[str]       =							'Face of a yellow cat, high resolution, sitting on a park bench'

            _lowerCAmelCase     :				Tuple       =							torch.manual_seed(0 )
            _lowerCAmelCase     :				Tuple       =							pipe(
                prompt=snake_case__   ,						image=snake_case__   ,						mask_image=snake_case__   ,						generator=snake_case__   ,						output_type='np'   ,						)
            _lowerCAmelCase     :				Dict       =							output.images[0]

            assert image.shape == (512, 512, 3)
            assert np.abs(expected_image - image ).max() < 9E-3







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Union[str, Any]       =							load_image(
                'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
                '/sd2-inpaint/init_image.png' )
            _lowerCAmelCase     :				Any       =							load_image(
                'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png' )
            _lowerCAmelCase     :				str       =							load_numpy(
                'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint'
                '/yellow_cat_sitting_on_a_park_bench_fp16.npy' )

            _lowerCAmelCase     :				Optional[int]       =							'stabilityai/stable-diffusion-2-inpainting'
            _lowerCAmelCase     :				Optional[int]       =							StableDiffusionInpaintPipeline.from_pretrained(
                snake_case__   ,						torch_dtype=torch.floataa   ,						safety_checker=snake_case__   ,						)
            pipe.to(snake_case__ )
            pipe.set_progress_bar_config(disable=snake_case__ )
            pipe.enable_attention_slicing()

            _lowerCAmelCase     :				Union[str, Any]       =							'Face of a yellow cat, high resolution, sitting on a park bench'

            _lowerCAmelCase     :				int       =							torch.manual_seed(0 )
            _lowerCAmelCase     :				int       =							pipe(
                prompt=snake_case__   ,						image=snake_case__   ,						mask_image=snake_case__   ,						generator=snake_case__   ,						output_type='np'   ,						)
            _lowerCAmelCase     :				Any       =							output.images[0]

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







     def a						(			self ):

            '''simple docstring'''

            torch.cuda.empty_cache()
            torch.cuda.reset_max_memory_allocated()
            torch.cuda.reset_peak_memory_stats()

            _lowerCAmelCase     :				Optional[int]       =							load_image(
                'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
                '/sd2-inpaint/init_image.png' )
            _lowerCAmelCase     :				Tuple       =							load_image(
                'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png' )

            _lowerCAmelCase     :				List[Any]       =							'stabilityai/stable-diffusion-2-inpainting'
            _lowerCAmelCase     :				int       =							PNDMScheduler.from_pretrained(snake_case__   ,						subfolder='scheduler' )
            _lowerCAmelCase     :				Tuple       =							StableDiffusionInpaintPipeline.from_pretrained(
                snake_case__   ,						safety_checker=snake_case__   ,						scheduler=snake_case__   ,						torch_dtype=torch.floataa   ,						)
            pipe.to(snake_case__ )
            pipe.set_progress_bar_config(disable=snake_case__ )
            pipe.enable_attention_slicing(1 )
            pipe.enable_sequential_cpu_offload()

            _lowerCAmelCase     :				Tuple       =							'Face of a yellow cat, high resolution, sitting on a park bench'

            _lowerCAmelCase     :				Dict       =							torch.manual_seed(0 )
            _lowerCAmelCase     :				List[Any]       =							pipe(
                prompt=snake_case__   ,						image=snake_case__   ,						mask_image=snake_case__   ,						generator=snake_case__   ,						num_inference_steps=2   ,						output_type='np'   ,						)

            _lowerCAmelCase     :				Optional[int]       =							torch.cuda.max_memory_allocated()
            # make sure that less than 2.65 GB is allocated
            assert mem_bytes < 2.65 * 10**9





'''simple docstring'''







import os
import shutil
import tempfile
from unittest import TestCase
from unittest.mock import patch

import numpy as np
from datasets import Dataset

from transformers.models.realm.configuration_realm import RealmConfig
from transformers.models.realm.retrieval_realm import _REALM_BLOCK_RECORDS_FILENAME, RealmRetriever
from transformers.models.realm.tokenization_realm import VOCAB_FILES_NAMES, RealmTokenizer
class 						UpperCamelCase__       (			SCREAMING_SNAKE_CASE_						):



     """simple docstring"""






     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Dict       =							tempfile.mkdtemp()
            _lowerCAmelCase     :				List[str]       =							5

            # Realm tok
            _lowerCAmelCase     :				str       =							[
                '[UNK]',
                '[CLS]',
                '[SEP]',
                '[PAD]',
                '[MASK]',
                'test',
                'question',
                'this',
                'is',
                'the',
                'first',
                'second',
                'third',
                'fourth',
                'fifth',
                'record',
                'want',
                '##want',
                '##ed',
                'wa',
                'un',
                'runn',
                '##ing',
                ',',
                'low',
                'lowest',
            ]
            _lowerCAmelCase     :				Union[str, Any]       =							os.path.join(self.tmpdirname   ,						'realm_tokenizer' )
            os.makedirs(snake_case__   ,						exist_ok=snake_case__ )
            _lowerCAmelCase     :				Dict       =							os.path.join(snake_case__   ,						VOCAB_FILES_NAMES['vocab_file'] )
            with open(self.vocab_file   ,						'w'   ,						encoding='utf-8' ) as vocab_writer:
                   vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) )

            _lowerCAmelCase     :				List[str]       =							os.path.join(self.tmpdirname   ,						'realm_block_records' )
            os.makedirs(snake_case__   ,						exist_ok=snake_case__ )







     def a						(			self ):

            '''simple docstring'''

            return RealmTokenizer.from_pretrained(os.path.join(self.tmpdirname   ,						'realm_tokenizer' ) )







     def a						(			self ):

            '''simple docstring'''

            shutil.rmtree(self.tmpdirname )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Dict       =							RealmConfig(num_block_records=self.num_block_records )
            return config







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				List[Any]       =							Dataset.from_dict(
                {
                    'id': ['0', '1'],
                    'question': ['foo', 'bar'],
                    'answers': [['Foo', 'Bar'], ['Bar']],
                } )
            return dataset







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				str       =							np.array(
                [
                    b'This is the first record',
                    b'This is the second record',
                    b'This is the third record',
                    b'This is the fourth record',
                    b'This is the fifth record',
                    b'This is a longer longer longer record',
                ]   ,						dtype=snake_case__   ,						)
            return block_records







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Tuple       =							RealmRetriever(
                block_records=self.get_dummy_block_records()   ,						tokenizer=self.get_tokenizer()   ,						)
            return retriever







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Optional[int]       =							self.get_config()
            _lowerCAmelCase     :				List[Any]       =							self.get_dummy_retriever()
            _lowerCAmelCase     :				Any       =							retriever.tokenizer

            _lowerCAmelCase     :				Tuple       =							np.array([0, 3]   ,						dtype='long' )
            _lowerCAmelCase     :				Dict       =							tokenizer(['Test question'] ).input_ids
            _lowerCAmelCase     :				int       =							tokenizer(
                ['the fourth']   ,						add_special_tokens=snake_case__   ,						return_token_type_ids=snake_case__   ,						return_attention_mask=snake_case__   ,						).input_ids
            _lowerCAmelCase     :				Tuple       =							config.reader_seq_len

            _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase     :				Union[str, Any]       =							retriever(
                snake_case__   ,						snake_case__   ,						answer_ids=snake_case__   ,						max_length=snake_case__   ,						return_tensors='np' )

            self.assertEqual(len(snake_case__ )   ,						2 )
            self.assertEqual(len(snake_case__ )   ,						2 )
            self.assertEqual(len(snake_case__ )   ,						2 )
            self.assertEqual(concat_inputs.input_ids.shape   ,						(2, 10) )
            self.assertEqual(concat_inputs.attention_mask.shape   ,						(2, 10) )
            self.assertEqual(concat_inputs.token_type_ids.shape   ,						(2, 10) )
            self.assertEqual(concat_inputs.special_tokens_mask.shape   ,						(2, 10) )
            self.assertEqual(
                tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[0] )   ,						['[CLS]', 'test', 'question', '[SEP]', 'this', 'is', 'the', 'first', 'record', '[SEP]']   ,						)
            self.assertEqual(
                tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[1] )   ,						['[CLS]', 'test', 'question', '[SEP]', 'this', 'is', 'the', 'fourth', 'record', '[SEP]']   ,						)







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				int       =							self.get_config()
            _lowerCAmelCase     :				int       =							self.get_dummy_retriever()
            _lowerCAmelCase     :				List[str]       =							retriever.tokenizer

            _lowerCAmelCase     :				List[Any]       =							np.array([0, 3, 5]   ,						dtype='long' )
            _lowerCAmelCase     :				str       =							tokenizer(['Test question'] ).input_ids
            _lowerCAmelCase     :				Union[str, Any]       =							tokenizer(
                ['the fourth', 'longer longer']   ,						add_special_tokens=snake_case__   ,						return_token_type_ids=snake_case__   ,						return_attention_mask=snake_case__   ,						).input_ids
            _lowerCAmelCase     :				Optional[int]       =							config.reader_seq_len

            _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase     :				List[str]       =							retriever(
                snake_case__   ,						snake_case__   ,						answer_ids=snake_case__   ,						max_length=snake_case__   ,						return_tensors='np' )

            self.assertEqual([False, True, True]   ,						snake_case__ )
            self.assertEqual([[-1, -1, -1], [6, -1, -1], [6, 7, 8]]   ,						snake_case__ )
            self.assertEqual([[-1, -1, -1], [7, -1, -1], [7, 8, 9]]   ,						snake_case__ )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				List[str]       =							self.get_dummy_retriever()
            retriever.save_pretrained(os.path.join(self.tmpdirname   ,						'realm_block_records' ) )

            # Test local path
            _lowerCAmelCase     :				Union[str, Any]       =							retriever.from_pretrained(os.path.join(self.tmpdirname   ,						'realm_block_records' ) )
            self.assertEqual(retriever.block_records[0]   ,						b'This is the first record' )

            # Test mocked remote path
            with patch('transformers.models.realm.retrieval_realm.hf_hub_download' ) as mock_hf_hub_download:
                   _lowerCAmelCase     :				Any       =							os.path.join(
                       os.path.join(self.tmpdirname   ,						'realm_block_records' )   ,						_REALM_BLOCK_RECORDS_FILENAME )
                   _lowerCAmelCase     :				str       =							RealmRetriever.from_pretrained('google/realm-cc-news-pretrained-openqa' )

            self.assertEqual(retriever.block_records[0]   ,						b'This is the first record' )

