
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


_snake_case			:       List[Any]      =						logging.get_logger(__name__)

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

_snake_case			:       List[str]      =						{
    "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"
        ),
    },
}

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





class      UpperCamelCase_			(			__a		):







						'''simple docstring'''



						UpperCamelCase			:     List[str]							      =				VOCAB_FILES_NAMES
						UpperCamelCase			:     Any							      =				PRETRAINED_VOCAB_FILES_MAP
						UpperCamelCase			:     Union[str, Any]							      =				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
						UpperCamelCase			:     Tuple							      =				['''input_ids''', '''attention_mask''']
						UpperCamelCase			:     List[str]							      =				CLIPTokenizer





						def __init__( self       :str		,						lowerCAmelCase__       :Tuple=None		,						lowerCAmelCase__       :List[Any]=None		,						lowerCAmelCase__       :List[Any]=None		,						lowerCAmelCase__       :List[Any]="<|endoftext|>"		,						lowerCAmelCase__       :Any="<|startoftext|>"		,						lowerCAmelCase__       :str="<|endoftext|>"		,						lowerCAmelCase__       :str="<|endoftext|>"		,						**lowerCAmelCase__       :Optional[int]		,						)     ->Dict:
										super().__init__(
										    lowerCAmelCase__		,						lowerCAmelCase__		,						tokenizer_file=lowerCAmelCase__		,						unk_token=lowerCAmelCase__		,						bos_token=lowerCAmelCase__		,						eos_token=lowerCAmelCase__		,						pad_token=lowerCAmelCase__		,						**lowerCAmelCase__		,						)

										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       :Any    )     ->Tuple:
										lowercase						    =			self.backend_tokenizer.decode

										def new_decode_method(*lowerCAmelCase__       :Dict		,						**lowerCAmelCase__       :Dict    ):
														lowercase						    =			orig_decode_method(*lowerCAmelCase__		,						**lowerCAmelCase__    )
														lowercase						    =			text.replace(self.backend_tokenizer.model.end_of_word_suffix		,						" "    ).strip()
														return text

										lowercase						    =			new_decode_method





						def 					SCREAMING_SNAKE_CASE( self       :Optional[Any]		,						lowerCAmelCase__       :List[int]		,						lowerCAmelCase__       :Optional[List[int]] = None    )     ->List[int]:
										lowercase						    =			[self.bos_token_id]
										lowercase						    =			[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       :int		,						lowerCAmelCase__       :List[int]		,						lowerCAmelCase__       :Optional[List[int]] = None    )     ->List[int]:
										lowercase						    =			[self.bos_token_id]
										lowercase						    =			[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       :Optional[Any]		,						lowerCAmelCase__       :str		,						lowerCAmelCase__       :Optional[str] = None    )     ->Tuple[str]:
										lowercase						    =			self._tokenizer.model.save(lowerCAmelCase__		,						name=lowerCAmelCase__    )
										return tuple(lowerCAmelCase__    )


from functools import partial
from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload

import torch
import torch.nn as nn
import torch.types

def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__	):







				'''simple docstring'''


				if not inplace:
								lowercase						    =			ma + ma
				else:
								ma += ma

				return ma

def    __snake_case       (  __magic_name__		,						__magic_name__	):







				'''simple docstring'''


				lowercase						    =			-1 * len(__magic_name__	)
				lowercase						    =			list(range(len(tensor.shape[:zero_index]	)	)	)
				return tensor.permute(first_inds + [zero_index + i for i in inds]	)

def    __snake_case       (  __magic_name__		,						__magic_name__	):







				'''simple docstring'''


				return t.reshape(t.shape[:-no_dims] + (-1,)	)

def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__		,						__magic_name__ = 1e-4	):







				'''simple docstring'''


				lowercase						    =			mask.expand(*value.shape	)
				return torch.sum(mask * value		,						dim=__magic_name__	) / (eps + torch.sum(__magic_name__		,						dim=__magic_name__	))

def    __snake_case       (  __magic_name__		,						__magic_name__ = 2.3_1_2_5		,						__magic_name__ = 2_1.6_8_7_5		,						__magic_name__ = 64	):







				'''simple docstring'''


				lowercase						    =			torch.linspace(__magic_name__		,						__magic_name__		,						no_bins - 1		,						device=pts.device	)
				lowercase						    =			torch.sqrt(torch.sum((pts.unsqueeze(-2	) - pts.unsqueeze(-3	)) ** 2		,						dim=-1	)	)
				return torch.bucketize(__magic_name__		,						__magic_name__	)

def    __snake_case       (  __magic_name__		,						__magic_name__	):







				'''simple docstring'''


				lowercase						    =			dicts[0]
				lowercase						    =			{}
				for k, v in first.items():
								lowercase						    =			[d[k] for d in dicts]
								if isinstance(__magic_name__		,						__magic_name__	):
												lowercase						    =			dict_multimap(__magic_name__		,						__magic_name__	)
								else:
												lowercase						    =			fn(__magic_name__	)

				return new_dict

def    __snake_case       (  __magic_name__		,						__magic_name__	):







				'''simple docstring'''


				lowercase						    =			v_bins.view(((1,) * len(x.shape	)) + (len(__magic_name__	),)	)
				lowercase						    =			x[..., None] - reshaped_bins
				lowercase						    =			torch.argmin(torch.abs(__magic_name__	)		,						dim=-1	)
				return nn.functional.one_hot(__magic_name__		,						num_classes=len(__magic_name__	)	).float()

def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__ = 0		,						__magic_name__ = 0	):







				'''simple docstring'''


				lowercase						    =			[]
				for i, s in enumerate(data.shape[:no_batch_dims]	):
								lowercase						    =			torch.arange(__magic_name__	)
								lowercase						    =			r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape	) - i - 1)))	)
								ranges.append(__magic_name__	)

				lowercase						    =			[slice(__magic_name__	) for _ in range(len(data.shape	) - no_batch_dims	)]
				lowercase						    =			inds
				ranges.extend(__magic_name__	)
				# Matt note: Editing this to get around the behaviour of using a list as an array index changing
				# in recent Numpy versions
				return data[tuple(__magic_name__	)]


_snake_case			:       List[Any]      =						TypeVar("T")

def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__	):







				'''simple docstring'''


				lowercase						    =			{}
				for k, v in dic.items():
								if isinstance(__magic_name__		,						__magic_name__	):
												lowercase						    =			dict_map(__magic_name__		,						__magic_name__		,						__magic_name__	)
								else:
												lowercase						    =			tree_map(__magic_name__		,						__magic_name__		,						__magic_name__	)

				return new_dict

@overload
def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__	):







				'''simple docstring'''


				...

@overload
def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__	):







				'''simple docstring'''


				...

@overload
def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__	):







				'''simple docstring'''


				...

@overload
def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__	):







				'''simple docstring'''


				...

def    __snake_case       (  __magic_name__		,						__magic_name__		,						__magic_name__	):







				'''simple docstring'''


				if isinstance(__magic_name__		,						__magic_name__	):
								return dict_map(__magic_name__		,						__magic_name__		,						__magic_name__	)
				elif isinstance(__magic_name__		,						__magic_name__	):
								return [tree_map(__magic_name__		,						__magic_name__		,						__magic_name__	) for x in tree]
				elif isinstance(__magic_name__		,						__magic_name__	):
								return tuple(tree_map(__magic_name__		,						__magic_name__		,						__magic_name__	) for x in tree	)
				elif isinstance(__magic_name__		,						__magic_name__	):
								return fn(__magic_name__	)
				else:
								print(type(__magic_name__	)	)
								raise ValueError("Not supported"	)




_snake_case			:       Union[str, Any]      =						partial(tree_map, leaf_type=torch.Tensor)
