




'''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': 7_7,
}

class 						lowerCAmelCase_   ( lowerCamelCase_   ):







  '''simple docstring'''







  lowerCAmelCase_				:					Optional[int]				     =						VOCAB_FILES_NAMES
  lowerCAmelCase_				:					str				     =						PRETRAINED_VOCAB_FILES_MAP
  lowerCAmelCase_				:					int				     =						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  lowerCAmelCase_				:					List[str]				     =						["""input_ids""", """attention_mask"""]
  lowerCAmelCase_				:					str				     =						CLIPTokenizer


  def __init__(						self    :	Optional[Any]						,		_UpperCAmelCase    :	Any=None						,		_UpperCAmelCase    :	Tuple=None						,		_UpperCAmelCase    :	Optional[int]=None						,		_UpperCAmelCase    :	int="<|endoftext|>"						,		_UpperCAmelCase    :	List[str]="<|startoftext|>"						,		_UpperCAmelCase    :	Dict="<|endoftext|>"						,		_UpperCAmelCase    :	List[Any]="<|endoftext|>"						,		**_UpperCAmelCase    :	int						,		):






   """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 							SCREAMING_SNAKE_CASE__			(						self    :	int ):






   """simple docstring"""






   UpperCAmelCase__										=    self.backend_tokenizer.decode

   def new_decode_method(*_UpperCAmelCase    :	Optional[Any]						,		**_UpperCAmelCase    :	Any ):
    UpperCAmelCase__										=    orig_decode_method(*_UpperCAmelCase						,		**_UpperCAmelCase )
    UpperCAmelCase__										=    text.replace(self.backend_tokenizer.model.end_of_word_suffix						,		""" """ ).strip()
    return text

   UpperCAmelCase__										=    new_decode_method


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[Any]						,		_UpperCAmelCase    :	List[int]						,		_UpperCAmelCase    :	Optional[List[int]] = None ):






   """simple docstring"""






   UpperCAmelCase__										=    [self.bos_token_id]
   UpperCAmelCase__										=    [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 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[Any]						,		_UpperCAmelCase    :	List[int]						,		_UpperCAmelCase    :	Optional[List[int]] = None ):






   """simple docstring"""






   UpperCAmelCase__										=    [self.bos_token_id]
   UpperCAmelCase__										=    [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 							SCREAMING_SNAKE_CASE__			(						self    :	List[Any]						,		_UpperCAmelCase    :	str						,		_UpperCAmelCase    :	Optional[str] = None ):






   """simple docstring"""






   UpperCAmelCase__										=    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 						lowerCAmelCase_   ( lowerCamelCase_			,  lowerCamelCase_			,  unittest.TestCase   ):







  '''simple docstring'''







  lowerCAmelCase_				:					Any				     =						IFImgaImgPipeline
  lowerCAmelCase_				:					Dict				     =						TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""width""", """height"""}
  lowerCAmelCase_				:					Union[str, Any]				     =						TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
  lowerCAmelCase_				:					Dict				     =						PipelineTesterMixin.required_optional_params - {"""latents"""}


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int] ):






   """simple docstring"""






   return self._get_dummy_components()


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Tuple						,		_UpperCAmelCase    :	Any						,		_UpperCAmelCase    :	Any=0 ):






   """simple docstring"""






   if str(_UpperCAmelCase ).startswith("""mps""" ):
    UpperCAmelCase__										=    torch.manual_seed(_UpperCAmelCase )
   else:
    UpperCAmelCase__										=    torch.Generator(device=_UpperCAmelCase ).manual_seed(_UpperCAmelCase )

   UpperCAmelCase__										=    floats_tensor((1, 3, 32, 32)						,		rng=random.Random(_UpperCAmelCase ) ).to(_UpperCAmelCase )

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

   return inputs


  def 							SCREAMING_SNAKE_CASE__			(						self    :	List[Any] ):






   """simple docstring"""






   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 							SCREAMING_SNAKE_CASE__			(						self    :	int ):






   """simple docstring"""






   self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 )


  @unittest.skipIf(torch_device != """cuda"""						,		reason="""float16 requires CUDA""" )
  def 							SCREAMING_SNAKE_CASE__			(						self    :	int ):






   """simple docstring"""






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


  @unittest.skipIf(torch_device != """cuda"""						,		reason="""float16 requires CUDA""" )
  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int] ):






   """simple docstring"""






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


  def 							SCREAMING_SNAKE_CASE__			(						self    :	List[str] ):






   """simple docstring"""






   self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 )


  def 							SCREAMING_SNAKE_CASE__			(						self    :	int ):






   """simple docstring"""






   self._test_save_load_local()



  def 							SCREAMING_SNAKE_CASE__			(						self    :	Any ):






   """simple docstring"""






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