






"""simple docstring"""





from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_clip import CLIPTokenizer


UpperCamelCase					=     logging.get_logger(__name__)

UpperCamelCase					=     {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""}

UpperCamelCase					=     {
    """vocab_file""": {
        """openai/clip-vit-base-patch32""": """https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/vocab.json""",
    },
    """merges_file""": {
        """openai/clip-vit-base-patch32""": """https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/merges.txt""",
    },
    """tokenizer_file""": {
        """openai/clip-vit-base-patch32""": (
            """https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/tokenizer.json"""
        ),
    },
}

UpperCamelCase					=     {
    """openai/clip-vit-base-patch32""": 77,
}







class 						_a  (		lowercase_       ):





   '''simple docstring'''






   UpperCamelCase__     	=							VOCAB_FILES_NAMES
   UpperCamelCase__     	=							PRETRAINED_VOCAB_FILES_MAP
   UpperCamelCase__     	=							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   UpperCamelCase__     	=							["""input_ids""", """attention_mask"""]
   UpperCamelCase__     	=							CLIPTokenizer





   def __init__(       self					, UpperCAmelCase_=None					, UpperCAmelCase_=None					, UpperCAmelCase_=None					, UpperCAmelCase_="<|endoftext|>"					, UpperCAmelCase_="<|startoftext|>"					, UpperCAmelCase_="<|endoftext|>"					, UpperCAmelCase_="<|endoftext|>"					, **UpperCAmelCase_					, )			->   Dict:




          '''simple docstring'''




          super().__init__(
              UpperCAmelCase_					, UpperCAmelCase_					, tokenizer_file=UpperCAmelCase_					, unk_token=UpperCAmelCase_					, bos_token=UpperCAmelCase_					, eos_token=UpperCAmelCase_					, pad_token=UpperCAmelCase_					, **UpperCAmelCase_					, )

          if not isinstance(self.backend_tokenizer.pre_tokenizer					, pre_tokenizers.Sequence):
                 raise ValueError(
                     "The `backend_tokenizer` provided does not match the expected format. The CLIP tokenizer has been"
                     " heavily modified from transformers version 4.17.0. You need to convert the tokenizer you are using"
                     " to be compatible with this version.The easiest way to do so is"
                     " `CLIPTokenizerFast.from_pretrained(\"path_to_local_folder_or_hub_repo, from_slow=True)`. If you want"
                     " to use your existing tokenizer, you will have to revert to a version prior to 4.17.0 of"
                     " transformers.")

          self._wrap_decode_method_backend_tokenizer()





   def __lowercase							(       self)			->   Tuple:




          '''simple docstring'''




          lowercase__:							Union[str, Any]         =     self.backend_tokenizer.decode

          def new_decode_method(*UpperCAmelCase_					, **UpperCAmelCase_):
                 lowercase__:							Union[str, Any]         =     orig_decode_method(*UpperCAmelCase_					, **UpperCAmelCase_)
                 lowercase__:							Optional[Any]         =     text.replace(self.backend_tokenizer.model.end_of_word_suffix					, " ").strip()
                 return text

          lowercase__:							List[str]         =     new_decode_method





   def __lowercase							(       self					, UpperCAmelCase_					, UpperCAmelCase_ = None)			->   List[int]:




          '''simple docstring'''




          lowercase__:							List[str]         =     [self.bos_token_id]
          lowercase__:							Dict         =     [self.eos_token_id]

          if token_ids_a is None:
                 return bos_token + token_ids_a + eos_token
          return bos_token + token_ids_a + eos_token + eos_token + token_ids_a + eos_token





   def __lowercase							(       self					, UpperCAmelCase_					, UpperCAmelCase_ = None)			->   List[int]:




          '''simple docstring'''




          lowercase__:							str         =     [self.bos_token_id]
          lowercase__:							Any         =     [self.eos_token_id]

          if token_ids_a is None:
                 return len(bos_token + token_ids_a + eos_token) * [0]
          return len(bos_token + token_ids_a + eos_token + eos_token + token_ids_a + eos_token) * [0]





   def __lowercase							(       self					, UpperCAmelCase_					, UpperCAmelCase_ = None)			->   Tuple[str]:




          '''simple docstring'''




          lowercase__:							str         =     self._tokenizer.model.save(UpperCAmelCase_					, name=UpperCAmelCase_)
          return tuple(UpperCAmelCase_)










'''simple docstring'''

import random
import unittest

import torch

from diffusers import IFImgaImgPipeline
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_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin





@skip_mps
class     __SCREAMING_SNAKE_CASE       (   lowerCamelCase__			,      lowerCamelCase__			,      unittest.TestCase			):
     UpperCAmelCase			       =					IFImgaImgPipeline
     UpperCAmelCase			       =					TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''width''', '''height'''}
     UpperCAmelCase			       =					TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
     UpperCAmelCase			       =					PipelineTesterMixin.required_optional_params - {'''latents'''}






     def 			a_       (      self		)							->     Optional[Any]:
          return self._get_dummy_components()






     def 			a_       (      self  ,				__UpperCamelCase  ,				__UpperCamelCase=0		)							->     Any:
          if str(__UpperCamelCase		).startswith("mps"		):
               _a            =		torch.manual_seed(__UpperCamelCase		)
          else:
               _a            =		torch.Generator(device=__UpperCamelCase		).manual_seed(__UpperCamelCase		)

          _a            =		floats_tensor((1, 3, 32, 32)  ,				rng=random.Random(__UpperCamelCase		)		).to(__UpperCamelCase		)

          _a            =		{
              "prompt": "A painting of a squirrel eating a burger",
              "image": image,
              "generator": generator,
              "num_inference_steps": 2,
              "output_type": "numpy",
          }

          return inputs






     def 			a_       (      self		)							->     Any:
          self._test_save_load_optional_components()






     @unittest.skipIf(
         torch_device != "cuda" or not is_xformers_available()  ,				reason="XFormers attention is only available with CUDA and `xformers` installed"  ,				)
     def 			a_       (      self		)							->     Dict:
          self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3		)






     @unittest.skipIf(torch_device != "cuda"  ,				reason="float16 requires CUDA"		)
     def 			a_       (      self		)							->     List[Any]:
          # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
          super().test_save_load_floataa(expected_max_diff=1e-1		)






     @unittest.skipIf(torch_device != "cuda"  ,				reason="float16 requires CUDA"		)
     def 			a_       (      self		)							->     int:
          super().test_floataa_inference(expected_max_diff=1e-1		)






     def 			a_       (      self		)							->     Dict:
          self._test_attention_slicing_forward_pass(expected_max_diff=1e-2		)






     def 			a_       (      self		)							->     Optional[Any]:
          self._test_save_load_local()






     def 			a_       (      self		)							->     Union[str, Any]:
          self._test_inference_batch_single_identical(
              expected_max_diff=1e-2  ,				)

