



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				:							Dict								=logging.get_logger(__name__)

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

UpperCAmelCase				:							List[Any]								={
    """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				:							Union[str, Any]								={
    """openai/clip-vit-base-patch32""": 77,
}


class  _lowercase				(a_ ):
				'''simple docstring'''







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

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

									'''simple docstring'''


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

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

									'''simple docstring'''


									UpperCamelCase_											=				self.backend_tokenizer.decode

									def new_decode_method(*snake_case__       ,	**snake_case__      ):
														UpperCamelCase_											=				orig_decode_method(*snake_case__       ,	**snake_case__      )
														UpperCamelCase_											=				text.replace(self.backend_tokenizer.model.end_of_word_suffix       ,	" "      ).strip()
														return text

									UpperCamelCase_											=				new_decode_method

				def  _lowerCamelCase   (	self       ,	snake_case__       ,	snake_case__ = 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  _lowerCamelCase   (	self       ,	snake_case__       ,	snake_case__ = 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  _lowerCamelCase   (	self       ,	snake_case__       ,	snake_case__ = None      ):

									'''simple docstring'''


									UpperCamelCase_											=				self._tokenizer.model.save(snake_case__       ,	name=snake_case__      )
									return tuple(snake_case__      )





from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
	import torch

UpperCAmelCase				:							Any								=logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
	from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):



	def 				_lowerCAmelCase      (cls):
						return cls



def 				_lowerCAmelCase      (_lowerCAmelCase				,		_lowerCAmelCase = None				,		_lowerCAmelCase = None				,		_lowerCAmelCase = None				,		_lowerCAmelCase = None				,		):
					UpperCamelCase_											=				device
					UpperCamelCase_											=				shape[0]

					UpperCamelCase_											=				layout or torch.strided
					UpperCamelCase_											=				device or torch.device("cpu")

					if generator is not None:
										UpperCamelCase_											=				generator.device.type if not isinstance(_lowerCAmelCase				,		_lowerCAmelCase) else generator[0].device.type
										if gen_device_type != device.type and gen_device_type == "cpu":
															UpperCamelCase_											=				"cpu"
															if device != "mps":
																				logger.info(
																				    f"""The passed generator was created on 'cpu' even though a tensor on {device} was expected."""
																				    f""" Tensors will be created on 'cpu' and then moved to {device}. Note that one can probably"""
																				    f""" slighly speed up this function by passing a generator that was created on the {device} device.""")
										elif gen_device_type != device.type and gen_device_type == "cuda":
															raise ValueError(f"""Cannot generate a {device} tensor from a generator of type {gen_device_type}.""")

					if isinstance(_lowerCAmelCase				,		_lowerCAmelCase):
										UpperCamelCase_											=				(1,) + shape[1:]
										UpperCamelCase_											=				[
										    torch.randn(_lowerCAmelCase				,		generator=generator[i]				,		device=_lowerCAmelCase				,		dtype=_lowerCAmelCase				,		layout=_lowerCAmelCase)
										    for i in range(_lowerCAmelCase)
										]
										UpperCamelCase_											=				torch.cat(_lowerCAmelCase				,		dim=0).to(_lowerCAmelCase)
					else:
										UpperCamelCase_											=				torch.randn(_lowerCAmelCase				,		generator=_lowerCAmelCase				,		device=_lowerCAmelCase				,		dtype=_lowerCAmelCase				,		layout=_lowerCAmelCase).to(_lowerCAmelCase)

					return latents






def 				_lowerCAmelCase      (_lowerCAmelCase):
					if is_torch_version("<"				,		"2.0.0") or not hasattr(_lowerCAmelCase				,		"_dynamo"):
										return False
					return isinstance(_lowerCAmelCase				,		torch._dynamo.eval_frame.OptimizedModule)
