





'''simple docstring'''



import random
import unittest

import torch

from diffusers import IFInpaintingPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin






@skip_mps
class 							__lowercase    ( lowerCAmelCase__	, lowerCAmelCase__	, unittest.TestCase  ):







	'''simple docstring'''

	a      :			Tuple     =    IFInpaintingPipeline
	a      :			Union[str, Any]     =    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"}
	a      :			Any     =    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
	a      :			Tuple     =    PipelineTesterMixin.required_optional_params - {"latents"}


	def   _UpperCAmelCase       (self	)							->							Union[str, Any]:







				'''simple docstring'''





				return self._get_dummy_components()


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase=0	)							->							Any:







				'''simple docstring'''





				if str(_lowerCamelCase	).startswith('''mps'''	):
							__lowercase			  =							torch.manual_seed(_lowerCamelCase	)
				else:
							__lowercase			  =							torch.Generator(device=_lowerCamelCase	).manual_seed(_lowerCamelCase	)

				__lowercase			  =							floats_tensor((1, 3, 32, 32)       ,rng=random.Random(_lowerCamelCase	)	).to(_lowerCamelCase	)
				__lowercase			  =							floats_tensor((1, 3, 32, 32)       ,rng=random.Random(_lowerCamelCase	)	).to(_lowerCamelCase	)

				__lowercase			  =							{
				    '''prompt''': '''A painting of a squirrel eating a burger''',
				    '''image''': image,
				    '''mask_image''': mask_image,
				    '''generator''': generator,
				    '''num_inference_steps''': 2,
				    '''output_type''': '''numpy''',
				}

				return inputs


	@unittest.skipIf(
	    torch_device != '''cuda''' or not is_xformers_available()       ,reason='''XFormers attention is only available with CUDA and `xformers` installed'''       ,)
	def   _UpperCAmelCase       (self	)							->							Optional[int]:







				'''simple docstring'''





				self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3	)


	def   _UpperCAmelCase       (self	)							->							Optional[Any]:







				'''simple docstring'''





				self._test_save_load_optional_components()


	@unittest.skipIf(torch_device != '''cuda'''       ,reason='''float16 requires CUDA'''	)
	def   _UpperCAmelCase       (self	)							->							Any:







				'''simple docstring'''





				super().test_save_load_floataa(expected_max_diff=1E-1	)


	def   _UpperCAmelCase       (self	)							->							Optional[int]:







				'''simple docstring'''





				self._test_attention_slicing_forward_pass(expected_max_diff=1E-2	)


	def   _UpperCAmelCase       (self	)							->							Optional[int]:







				'''simple docstring'''





				self._test_save_load_local()


	def   _UpperCAmelCase       (self	)							->							List[Any]:







				'''simple docstring'''





				self._test_inference_batch_single_identical(
				    expected_max_diff=1E-2       ,)












'''simple docstring'''



import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from .tokenization_electra import ElectraTokenizer


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

_SCREAMING_SNAKE_CASE   =      {
    '''vocab_file''': {
        '''google/electra-small-generator''': (
            '''https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt'''
        ),
        '''google/electra-base-generator''': '''https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt''',
        '''google/electra-large-generator''': (
            '''https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt'''
        ),
        '''google/electra-small-discriminator''': (
            '''https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt'''
        ),
        '''google/electra-base-discriminator''': (
            '''https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt'''
        ),
        '''google/electra-large-discriminator''': (
            '''https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt'''
        ),
    },
    '''tokenizer_file''': {
        '''google/electra-small-generator''': (
            '''https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json'''
        ),
        '''google/electra-base-generator''': (
            '''https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json'''
        ),
        '''google/electra-large-generator''': (
            '''https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json'''
        ),
        '''google/electra-small-discriminator''': (
            '''https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json'''
        ),
        '''google/electra-base-discriminator''': (
            '''https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json'''
        ),
        '''google/electra-large-discriminator''': (
            '''https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json'''
        ),
    },
}

_SCREAMING_SNAKE_CASE   =      {
    '''google/electra-small-generator''': 5_1_2,
    '''google/electra-base-generator''': 5_1_2,
    '''google/electra-large-generator''': 5_1_2,
    '''google/electra-small-discriminator''': 5_1_2,
    '''google/electra-base-discriminator''': 5_1_2,
    '''google/electra-large-discriminator''': 5_1_2,
}

_SCREAMING_SNAKE_CASE   =      {
    '''google/electra-small-generator''': {'''do_lower_case''': True},
    '''google/electra-base-generator''': {'''do_lower_case''': True},
    '''google/electra-large-generator''': {'''do_lower_case''': True},
    '''google/electra-small-discriminator''': {'''do_lower_case''': True},
    '''google/electra-base-discriminator''': {'''do_lower_case''': True},
    '''google/electra-large-discriminator''': {'''do_lower_case''': True},
}






class 							__lowercase    ( lowerCAmelCase__  ):







	'''simple docstring'''

	a      :			str     =    VOCAB_FILES_NAMES
	a      :			Dict     =    PRETRAINED_VOCAB_FILES_MAP
	a      :			int     =    PRETRAINED_INIT_CONFIGURATION
	a      :			Tuple     =    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
	a      :			List[Any]     =    ElectraTokenizer


	def __init__(self       ,_lowerCamelCase=None       ,_lowerCamelCase=None       ,_lowerCamelCase=True       ,_lowerCamelCase="[UNK]"       ,_lowerCamelCase="[SEP]"       ,_lowerCamelCase="[PAD]"       ,_lowerCamelCase="[CLS]"       ,_lowerCamelCase="[MASK]"       ,_lowerCamelCase=True       ,_lowerCamelCase=None       ,**_lowerCamelCase       ,)							->							List[Any]:







				'''simple docstring'''





				super().__init__(
				    _lowerCamelCase       ,tokenizer_file=_lowerCamelCase       ,do_lower_case=_lowerCamelCase       ,unk_token=_lowerCamelCase       ,sep_token=_lowerCamelCase       ,pad_token=_lowerCamelCase       ,cls_token=_lowerCamelCase       ,mask_token=_lowerCamelCase       ,tokenize_chinese_chars=_lowerCamelCase       ,strip_accents=_lowerCamelCase       ,**_lowerCamelCase       ,)

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

				__lowercase			  =							do_lower_case


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase=None	)							->							str:







				'''simple docstring'''





				__lowercase			  =							[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   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase = None	)							->							List[int]:







				'''simple docstring'''





				__lowercase			  =							[self.sep_token_id]
				__lowercase			  =							[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   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase = None	)							->							Tuple[str]:







				'''simple docstring'''





				__lowercase			  =							self._tokenizer.model.save(_lowerCamelCase       ,name=_lowerCamelCase	)
				return tuple(_lowerCamelCase	)





