





'''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


snake_case_     :       List[str]						=  {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

snake_case_     :       Tuple						=  {
    "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"
        ),
    },
}

snake_case_     :       List[Any]						=  {
    "google/electra-small-generator": 5_12,
    "google/electra-base-generator": 5_12,
    "google/electra-large-generator": 5_12,
    "google/electra-small-discriminator": 5_12,
    "google/electra-base-discriminator": 5_12,
    "google/electra-large-discriminator": 5_12,
}

snake_case_     :       int						=  {
    "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 					__a      (lowerCamelCase     ):
							__a					:   Any									=       VOCAB_FILES_NAMES
							__a					:   int									=       PRETRAINED_VOCAB_FILES_MAP
							__a					:   Tuple									=       PRETRAINED_INIT_CONFIGURATION
							__a					:   Dict									=       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							__a					:   str									=       ElectraTokenizer

							def __init__(							self							:     Optional[Any]       ,     __magic_name__							:     Optional[int]=None       ,     __magic_name__							:     Optional[int]=None       ,     __magic_name__							:     Optional[Any]=True       ,     __magic_name__							:     List[Any]="[UNK]"       ,     __magic_name__							:     Union[str, Any]="[SEP]"       ,     __magic_name__							:     List[Any]="[PAD]"       ,     __magic_name__							:     Tuple="[CLS]"       ,     __magic_name__							:     Union[str, Any]="[MASK]"       ,     __magic_name__							:     List[Any]=True       ,     __magic_name__							:     Optional[Any]=None       ,     **__magic_name__							:     Union[str, Any]       ,     )					->       List[str]:







													"""simple docstring"""
													super().__init__(
													    __magic_name__       ,     tokenizer_file=__magic_name__       ,     do_lower_case=__magic_name__       ,     unk_token=__magic_name__       ,     sep_token=__magic_name__       ,     pad_token=__magic_name__       ,     cls_token=__magic_name__       ,     mask_token=__magic_name__       ,     tokenize_chinese_chars=__magic_name__       ,     strip_accents=__magic_name__       ,     **__magic_name__       ,     )

													UpperCAmelCase_			:      List[str]     						=  json.loads(self.backend_tokenizer.normalizer.__getstate__()   )
													if (
													    normalizer_state.get('''lowercase'''       ,     __magic_name__   ) != do_lower_case
													    or normalizer_state.get('''strip_accents'''       ,     __magic_name__   ) != strip_accents
													    or normalizer_state.get('''handle_chinese_chars'''       ,     __magic_name__   ) != tokenize_chinese_chars
													):
																			UpperCAmelCase_			:      List[Any]     						=  getattr(__magic_name__       ,     normalizer_state.pop('''type'''   )   )
																			UpperCAmelCase_			:      Any     						=  do_lower_case
																			UpperCAmelCase_			:      int     						=  strip_accents
																			UpperCAmelCase_			:      Tuple     						=  tokenize_chinese_chars
																			UpperCAmelCase_			:      Optional[Any]     						=  normalizer_class(**__magic_name__   )

													UpperCAmelCase_			:      Dict     						=  do_lower_case

							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     Dict       ,     __magic_name__							:     str=None   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      int     						=  [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							:     int       ,     __magic_name__							:     List[int]       ,     __magic_name__							:     Optional[List[int]] = None   )					->       List[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      List[Any]     						=  [self.sep_token_id]
													UpperCAmelCase_			:      Optional[int]     						=  [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							:     int       ,     __magic_name__							:     str       ,     __magic_name__							:     Optional[str] = None   )					->       Tuple[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      Tuple     						=  self._tokenizer.model.save(__magic_name__       ,     name=__magic_name__   )
													return tuple(__magic_name__   )












'''simple docstring'''







import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


snake_case_     :       List[str]						=  False


class 					__a      (unittest.TestCase     ):
							pass





@nightly
@require_torch_gpu
class 					__a      (unittest.TestCase     ):

							def     UpperCAmelCase__	(							self							:     int   )					->       str:







													"""simple docstring"""
													# clean up the VRAM after each test
													super().tearDown()
													gc.collect()
													torch.cuda.empty_cache()

							def     UpperCAmelCase__	(							self							:     List[str]   )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Tuple     						=  VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion'''       ,     torch_dtype=torch.floataa   )
													pipe.to(__magic_name__   )
													pipe.set_progress_bar_config(disable=__magic_name__   )

													UpperCAmelCase_			:      List[Any]     						=  load_image(
													    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''   )

													UpperCAmelCase_			:      Optional[int]     						=  torch.manual_seed(0   )
													UpperCAmelCase_			:      Union[str, Any]     						=  pipe.dual_guided(
													    prompt='''first prompt'''       ,     image=__magic_name__       ,     text_to_image_strength=0.7_5       ,     generator=__magic_name__       ,     guidance_scale=7.5       ,     num_inference_steps=2       ,     output_type='''numpy'''       ,     ).images

													with tempfile.TemporaryDirectory() as tmpdirname:
																			pipe.save_pretrained(__magic_name__   )
																			UpperCAmelCase_			:      Optional[int]     						=  VersatileDiffusionPipeline.from_pretrained(__magic_name__       ,     torch_dtype=torch.floataa   )
													pipe.to(__magic_name__   )
													pipe.set_progress_bar_config(disable=__magic_name__   )

													UpperCAmelCase_			:      Any     						=  generator.manual_seed(0   )
													UpperCAmelCase_			:      Dict     						=  pipe.dual_guided(
													    prompt='''first prompt'''       ,     image=__magic_name__       ,     text_to_image_strength=0.7_5       ,     generator=__magic_name__       ,     guidance_scale=7.5       ,     num_inference_steps=2       ,     output_type='''numpy'''       ,     ).images

													assert np.abs(image - new_image   ).sum() < 1E-5, "Models don't have the same forward pass"



							def     UpperCAmelCase__	(							self							:     str   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion'''       ,     torch_dtype=torch.floataa   )
													pipe.to(__magic_name__   )
													pipe.set_progress_bar_config(disable=__magic_name__   )

													UpperCAmelCase_			:      Union[str, Any]     						=  '''cyberpunk 2077'''
													UpperCAmelCase_			:      Union[str, Any]     						=  load_image(
													    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''   )
													UpperCAmelCase_			:      Tuple     						=  torch.manual_seed(0   )
													UpperCAmelCase_			:      Optional[Any]     						=  pipe.dual_guided(
													    prompt=__magic_name__       ,     image=__magic_name__       ,     text_to_image_strength=0.7_5       ,     generator=__magic_name__       ,     guidance_scale=7.5       ,     num_inference_steps=50       ,     output_type='''numpy'''       ,     ).images

													UpperCAmelCase_			:      List[str]     						=  image[0, 2_53:2_56, 2_53:2_56, -1]

													assert image.shape == (1, 5_12, 5_12, 3)
													UpperCAmelCase_			:      Union[str, Any]     						=  np.array([0.1_4_4_8, 0.1_6_1_9, 0.1_7_4_1, 0.1_0_8_6, 0.1_1_4_7, 0.1_1_2_8, 0.1_1_9_9, 0.1_1_6_5, 0.1_0_0_1]   )

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

													UpperCAmelCase_			:      Tuple     						=  '''A painting of a squirrel eating a burger '''
													UpperCAmelCase_			:      Optional[int]     						=  torch.manual_seed(0   )
													UpperCAmelCase_			:      List[Any]     						=  pipe.text_to_image(
													    prompt=__magic_name__       ,     generator=__magic_name__       ,     guidance_scale=7.5       ,     num_inference_steps=50       ,     output_type='''numpy'''   ).images

													UpperCAmelCase_			:      Tuple     						=  image[0, 2_53:2_56, 2_53:2_56, -1]

													assert image.shape == (1, 5_12, 5_12, 3)
													UpperCAmelCase_			:      Any     						=  np.array([0.3_3_6_7, 0.3_1_6_9, 0.2_6_5_6, 0.3_8_7_0, 0.4_7_9_0, 0.3_7_9_6, 0.4_0_0_9, 0.4_8_7_8, 0.4_7_7_8]   )

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

													UpperCAmelCase_			:      Tuple     						=  pipe.image_variation(__magic_name__       ,     generator=__magic_name__       ,     output_type='''numpy'''   ).images

													UpperCAmelCase_			:      Optional[Any]     						=  image[0, 2_53:2_56, 2_53:2_56, -1]

													assert image.shape == (1, 5_12, 5_12, 3)
													UpperCAmelCase_			:      List[str]     						=  np.array([0.3_0_7_6, 0.3_1_2_3, 0.3_2_8_4, 0.3_7_8_2, 0.3_7_7_0, 0.3_8_9_4, 0.4_2_9_7, 0.4_3_3_1, 0.4_4_5_6]   )

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





