



"""simple docstring"""





import os
import unicodedata
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import SPIECE_UNDERLINE, logging


lowerCamelCase      =		logging.get_logger(__name__)

lowerCamelCase      =		{"""vocab_file""": """spiece.model"""}

lowerCamelCase      =		{
    """vocab_file""": {
        """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model""",
        """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model""",
    }
}

lowerCamelCase      =		{
    """xlnet-base-cased""": None,
    """xlnet-large-cased""": None,
}

# Segments (not really needed)
lowerCamelCase      =		0
lowerCamelCase      =		1
lowerCamelCase      =		2
lowerCamelCase      =		3
lowerCamelCase      =		4
class    lowercase__     (					SCREAMING_SNAKE_CASE   ):







       '''simple docstring'''


       UpperCamelCase		=  VOCAB_FILES_NAMES
       UpperCamelCase		=  PRETRAINED_VOCAB_FILES_MAP
       UpperCamelCase		=  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       UpperCamelCase		=  '''left'''



       def __init__(							self    :						str	,       _UpperCAmelCase    :						List[str]	,       _UpperCAmelCase    :						Tuple=False	,       _UpperCAmelCase    :						Any=True	,       _UpperCAmelCase    :						str=False	,       _UpperCAmelCase    :						List[Any]="<s>"	,       _UpperCAmelCase    :						List[str]="</s>"	,       _UpperCAmelCase    :						Optional[int]="<unk>"	,       _UpperCAmelCase    :						Union[str, Any]="<sep>"	,       _UpperCAmelCase    :						Union[str, Any]="<pad>"	,       _UpperCAmelCase    :						Any="<cls>"	,       _UpperCAmelCase    :						str="<mask>"	,       _UpperCAmelCase    :						Dict=["<eop>", "<eod>"]	,       _UpperCAmelCase    :						Optional[Dict[str, Any]] = None	,       **_UpperCAmelCase    :						List[str]	,       )				->		None:


             '''simple docstring'''




             UpperCAmelCase_    				=							AddedToken(_UpperCAmelCase	,       lstrip=_UpperCAmelCase	,       rstrip=_UpperCAmelCase					) if isinstance(_UpperCAmelCase	,       _UpperCAmelCase					) else mask_token

             UpperCAmelCase_    				=							{} if sp_model_kwargs is None else sp_model_kwargs

             super().__init__(
                 do_lower_case=_UpperCAmelCase	,       remove_space=_UpperCAmelCase	,       keep_accents=_UpperCAmelCase	,       bos_token=_UpperCAmelCase	,       eos_token=_UpperCAmelCase	,       unk_token=_UpperCAmelCase	,       sep_token=_UpperCAmelCase	,       pad_token=_UpperCAmelCase	,       cls_token=_UpperCAmelCase	,       mask_token=_UpperCAmelCase	,       additional_special_tokens=_UpperCAmelCase	,       sp_model_kwargs=self.sp_model_kwargs	,       **_UpperCAmelCase	,       )

             UpperCAmelCase_    				=							3

             UpperCAmelCase_    				=							do_lower_case
             UpperCAmelCase_    				=							remove_space
             UpperCAmelCase_    				=							keep_accents
             UpperCAmelCase_    				=							vocab_file

             UpperCAmelCase_    				=							spm.SentencePieceProcessor(**self.sp_model_kwargs					)
             self.sp_model.Load(_UpperCAmelCase					)



       @property
       def   lowercase__       (							self    :						Optional[Any]					)				->		Optional[int]:


             '''simple docstring'''




             return len(self.sp_model					)



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


             '''simple docstring'''




             UpperCAmelCase_    				=							{self.convert_ids_to_tokens(_UpperCAmelCase					): i for i in range(self.vocab_size					)}
             vocab.update(self.added_tokens_encoder					)
             return vocab



       def __getstate__(							self    :						Any					)				->		Union[str, Any]:


             '''simple docstring'''




             UpperCAmelCase_    				=							self.__dict__.copy()
             UpperCAmelCase_    				=							None
             return state



       def __setstate__(							self    :						str	,       _UpperCAmelCase    :						List[Any]					)				->		List[Any]:


             '''simple docstring'''




             UpperCAmelCase_    				=							d

             # for backward compatibility
             if not hasattr(self	,       "sp_model_kwargs"					):
                   UpperCAmelCase_    				=							{}

             UpperCAmelCase_    				=							spm.SentencePieceProcessor(**self.sp_model_kwargs					)
             self.sp_model.Load(self.vocab_file					)



       def   lowercase__       (							self    :						int	,       _UpperCAmelCase    :						Union[str, Any]					)				->		List[str]:


             '''simple docstring'''




             if self.remove_space:
                   UpperCAmelCase_    				=							" ".join(inputs.strip().split()					)
             else:
                   UpperCAmelCase_    				=							inputs
             UpperCAmelCase_    				=							outputs.replace("``"	,       "\""					).replace("''"	,       "\""					)

             if not self.keep_accents:
                   UpperCAmelCase_    				=							unicodedata.normalize("NFKD"	,       _UpperCAmelCase					)
                   UpperCAmelCase_    				=							"".join([c for c in outputs if not unicodedata.combining(_UpperCAmelCase					)]					)
             if self.do_lower_case:
                   UpperCAmelCase_    				=							outputs.lower()

             return outputs



       def   lowercase__       (							self    :						Union[str, Any]	,       _UpperCAmelCase    :						str					)				->		List[str]:


             '''simple docstring'''




             UpperCAmelCase_    				=							self.preprocess_text(_UpperCAmelCase					)
             UpperCAmelCase_    				=							self.sp_model.encode(_UpperCAmelCase	,       out_type=_UpperCAmelCase					)
             UpperCAmelCase_    				=							[]
             for piece in pieces:
                   if len(_UpperCAmelCase					) > 1 and piece[-1] == str(","					) and piece[-2].isdigit():
                         UpperCAmelCase_    				=							self.sp_model.EncodeAsPieces(piece[:-1].replace(_UpperCAmelCase	,       ""					)					)
                         if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
                               if len(cur_pieces[0]					) == 1:
                                     UpperCAmelCase_    				=							cur_pieces[1:]
                               else:
                                     UpperCAmelCase_    				=							cur_pieces[0][1:]
                         cur_pieces.append(piece[-1]					)
                         new_pieces.extend(_UpperCAmelCase					)
                   else:
                         new_pieces.append(_UpperCAmelCase					)

             return new_pieces



       def   lowercase__       (							self    :						str	,       _UpperCAmelCase    :						Union[str, Any]					)				->		int:


             '''simple docstring'''




             return self.sp_model.PieceToId(_UpperCAmelCase					)



       def   lowercase__       (							self    :						Union[str, Any]	,       _UpperCAmelCase    :						List[str]					)				->		List[Any]:


             '''simple docstring'''




             return self.sp_model.IdToPiece(_UpperCAmelCase					)



       def   lowercase__       (							self    :						str	,       _UpperCAmelCase    :						List[Any]					)				->		Union[str, Any]:


             '''simple docstring'''




             UpperCAmelCase_    				=							"".join(_UpperCAmelCase					).replace(_UpperCAmelCase	,       " "					).strip()
             return out_string



       def   lowercase__       (							self    :						List[Any]	,       _UpperCAmelCase    :						List[int]	,       _UpperCAmelCase    :						bool = False	,       _UpperCAmelCase    :						bool = None	,       _UpperCAmelCase    :						bool = True	,       **_UpperCAmelCase    :						List[str]	,       )				->		str:


             '''simple docstring'''




             UpperCAmelCase_    				=							kwargs.pop("use_source_tokenizer"	,       _UpperCAmelCase					)

             UpperCAmelCase_    				=							self.convert_ids_to_tokens(_UpperCAmelCase	,       skip_special_tokens=_UpperCAmelCase					)

             # To avoid mixing byte-level and unicode for byte-level BPT
             # we need to build string separately for added tokens and byte-level tokens
             # cf. https://github.com/huggingface/transformers/issues/1133
             UpperCAmelCase_    				=							[]
             UpperCAmelCase_    				=							[]
             for token in filtered_tokens:
                   if skip_special_tokens and token in self.all_special_ids:
                         continue
                   if token in self.added_tokens_encoder:
                         if current_sub_text:
                               sub_texts.append(self.convert_tokens_to_string(_UpperCAmelCase					)					)
                               UpperCAmelCase_    				=							[]
                         sub_texts.append(_UpperCAmelCase					)
                   else:
                         current_sub_text.append(_UpperCAmelCase					)
             if current_sub_text:
                   sub_texts.append(self.convert_tokens_to_string(_UpperCAmelCase					)					)

             # Mimic the behavior of the Rust tokenizer:
             # By default, there are no spaces between special tokens
             UpperCAmelCase_    				=							"".join(_UpperCAmelCase					)

             UpperCAmelCase_    				=							(
                 clean_up_tokenization_spaces
                 if clean_up_tokenization_spaces is not None
                 else self.clean_up_tokenization_spaces
             )
             if clean_up_tokenization_spaces:
                   UpperCAmelCase_    				=							self.clean_up_tokenization(_UpperCAmelCase					)
                   return clean_text
             else:
                   return text



       def   lowercase__       (							self    :						int	,       _UpperCAmelCase    :						List[int]	,       _UpperCAmelCase    :						Optional[List[int]] = None					)				->		List[int]:


             '''simple docstring'''




             UpperCAmelCase_    				=							[self.sep_token_id]
             UpperCAmelCase_    				=							[self.cls_token_id]
             if token_ids_a is None:
                   return token_ids_a + sep + cls
             return token_ids_a + sep + token_ids_a + sep + cls



       def   lowercase__       (							self    :						int	,       _UpperCAmelCase    :						List[int]	,       _UpperCAmelCase    :						Optional[List[int]] = None	,       _UpperCAmelCase    :						bool = False					)				->		List[int]:


             '''simple docstring'''




             if already_has_special_tokens:
                   return super().get_special_tokens_mask(
                       token_ids_a=_UpperCAmelCase	,       token_ids_a=_UpperCAmelCase	,       already_has_special_tokens=_UpperCAmelCase					)

             if token_ids_a is not None:
                   return ([0] * len(_UpperCAmelCase					)) + [1] + ([0] * len(_UpperCAmelCase					)) + [1, 1]
             return ([0] * len(_UpperCAmelCase					)) + [1, 1]



       def   lowercase__       (							self    :						Dict	,       _UpperCAmelCase    :						List[int]	,       _UpperCAmelCase    :						Optional[List[int]] = None					)				->		List[int]:


             '''simple docstring'''




             UpperCAmelCase_    				=							[self.sep_token_id]
             UpperCAmelCase_    				=							[2]

             if token_ids_a is None:
                   return len(token_ids_a + sep					) * [0] + cls_segment_id
             return len(token_ids_a + sep					) * [0] + len(token_ids_a + sep					) * [1] + cls_segment_id







       def   lowercase__       (							self    :						List[str]	,       _UpperCAmelCase    :						str	,       _UpperCAmelCase    :						Optional[str] = None					)				->		Tuple[str]:


             '''simple docstring'''




             if not os.path.isdir(_UpperCAmelCase					):
                   logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""					)
                   return
             UpperCAmelCase_    				=							os.path.join(
                 _UpperCAmelCase	,       (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]					)

             if os.path.abspath(self.vocab_file					) != os.path.abspath(_UpperCAmelCase					) and os.path.isfile(self.vocab_file					):
                   copyfile(self.vocab_file	,       _UpperCAmelCase					)
             elif not os.path.isfile(self.vocab_file					):
                   with open(_UpperCAmelCase	,       "wb"					) as fi:
                         UpperCAmelCase_    				=							self.sp_model.serialized_model_proto()
                         fi.write(_UpperCAmelCase					)

             return (out_vocab_file,)









"""simple docstring"""





import copy
import os
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Union


if TYPE_CHECKING:
  from ...processing_utils import ProcessorMixin
  from ...utils import TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCamelCase      =		logging.get_logger(__name__)

lowerCamelCase      =		{
    """google/owlvit-base-patch32""": """https://huggingface.co/google/owlvit-base-patch32/resolve/main/config.json""",
    """google/owlvit-base-patch16""": """https://huggingface.co/google/owlvit-base-patch16/resolve/main/config.json""",
    """google/owlvit-large-patch14""": """https://huggingface.co/google/owlvit-large-patch14/resolve/main/config.json""",
}
class    lowercase__     (					SCREAMING_SNAKE_CASE   ):







       '''simple docstring'''


       UpperCamelCase		=  '''owlvit_text_model'''



       def __init__(							self    :						List[Any]	,       _UpperCAmelCase    :						str=49408	,       _UpperCAmelCase    :						str=512	,       _UpperCAmelCase    :						Optional[Any]=2048	,       _UpperCAmelCase    :						Optional[int]=12	,       _UpperCAmelCase    :						Tuple=8	,       _UpperCAmelCase    :						List[str]=16	,       _UpperCAmelCase    :						List[str]="quick_gelu"	,       _UpperCAmelCase    :						Dict=1e-5	,       _UpperCAmelCase    :						Union[str, Any]=0.0	,       _UpperCAmelCase    :						List[Any]=0.02	,       _UpperCAmelCase    :						Optional[int]=1.0	,       _UpperCAmelCase    :						Dict=0	,       _UpperCAmelCase    :						Dict=49406	,       _UpperCAmelCase    :						Union[str, Any]=49407	,       **_UpperCAmelCase    :						List[str]	,       )				->		List[str]:


             '''simple docstring'''




             super().__init__(pad_token_id=_UpperCAmelCase	,       bos_token_id=_UpperCAmelCase	,       eos_token_id=_UpperCAmelCase	,       **_UpperCAmelCase					)

             UpperCAmelCase_    				=							vocab_size
             UpperCAmelCase_    				=							hidden_size
             UpperCAmelCase_    				=							intermediate_size
             UpperCAmelCase_    				=							num_hidden_layers
             UpperCAmelCase_    				=							num_attention_heads
             UpperCAmelCase_    				=							max_position_embeddings
             UpperCAmelCase_    				=							hidden_act
             UpperCAmelCase_    				=							layer_norm_eps
             UpperCAmelCase_    				=							attention_dropout
             UpperCAmelCase_    				=							initializer_range
             UpperCAmelCase_    				=							initializer_factor







       @classmethod
       def   lowercase__       (							cls    :						int	,       _UpperCAmelCase    :						Union[str, os.PathLike]	,       **_UpperCAmelCase    :						List[str]					)				->		"PretrainedConfig":


             '''simple docstring'''




             cls._set_token_in_kwargs(_UpperCAmelCase					)

             UpperCAmelCase_				,					UpperCAmelCase_    				=							cls.get_config_dict(_UpperCAmelCase	,       **_UpperCAmelCase					)

             # get the text config dict if we are loading from OwlViTConfig
             if config_dict.get("model_type"					) == "owlvit":
                   UpperCAmelCase_    				=							config_dict["text_config"]

             if "model_type" in config_dict and hasattr(cls	,       "model_type"					) and config_dict["model_type"] != cls.model_type:
                   logger.warning(
                       F"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """
                       F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors."""					)

             return cls.from_dict(_UpperCAmelCase	,       **_UpperCAmelCase					)



class    lowercase__     (					SCREAMING_SNAKE_CASE   ):







       '''simple docstring'''


       UpperCamelCase		=  '''owlvit_vision_model'''



       def __init__(							self    :						str	,       _UpperCAmelCase    :						List[str]=768	,       _UpperCAmelCase    :						Optional[Any]=3072	,       _UpperCAmelCase    :						Tuple=12	,       _UpperCAmelCase    :						Optional[Any]=12	,       _UpperCAmelCase    :						List[Any]=3	,       _UpperCAmelCase    :						List[str]=768	,       _UpperCAmelCase    :						int=32	,       _UpperCAmelCase    :						Dict="quick_gelu"	,       _UpperCAmelCase    :						Optional[Any]=1e-5	,       _UpperCAmelCase    :						Optional[int]=0.0	,       _UpperCAmelCase    :						List[Any]=0.02	,       _UpperCAmelCase    :						List[str]=1.0	,       **_UpperCAmelCase    :						List[str]	,       )				->		Dict:


             '''simple docstring'''




             super().__init__(**_UpperCAmelCase					)

             UpperCAmelCase_    				=							hidden_size
             UpperCAmelCase_    				=							intermediate_size
             UpperCAmelCase_    				=							num_hidden_layers
             UpperCAmelCase_    				=							num_attention_heads
             UpperCAmelCase_    				=							num_channels
             UpperCAmelCase_    				=							image_size
             UpperCAmelCase_    				=							patch_size
             UpperCAmelCase_    				=							hidden_act
             UpperCAmelCase_    				=							layer_norm_eps
             UpperCAmelCase_    				=							attention_dropout
             UpperCAmelCase_    				=							initializer_range
             UpperCAmelCase_    				=							initializer_factor







       @classmethod
       def   lowercase__       (							cls    :						Any	,       _UpperCAmelCase    :						Union[str, os.PathLike]	,       **_UpperCAmelCase    :						Union[str, Any]					)				->		"PretrainedConfig":


             '''simple docstring'''




             cls._set_token_in_kwargs(_UpperCAmelCase					)

             UpperCAmelCase_				,					UpperCAmelCase_    				=							cls.get_config_dict(_UpperCAmelCase	,       **_UpperCAmelCase					)

             # get the vision config dict if we are loading from OwlViTConfig
             if config_dict.get("model_type"					) == "owlvit":
                   UpperCAmelCase_    				=							config_dict["vision_config"]

             if "model_type" in config_dict and hasattr(cls	,       "model_type"					) and config_dict["model_type"] != cls.model_type:
                   logger.warning(
                       F"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """
                       F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors."""					)

             return cls.from_dict(_UpperCAmelCase	,       **_UpperCAmelCase					)



class    lowercase__     (					SCREAMING_SNAKE_CASE   ):







       '''simple docstring'''


       UpperCamelCase		=  '''owlvit'''
       UpperCamelCase		=  True



       def __init__(							self    :						Tuple	,       _UpperCAmelCase    :						Any=None	,       _UpperCAmelCase    :						Dict=None	,       _UpperCAmelCase    :						Tuple=512	,       _UpperCAmelCase    :						Any=2.6592	,       _UpperCAmelCase    :						Union[str, Any]=True	,       **_UpperCAmelCase    :						Union[str, Any]	,       )				->		Tuple:


             '''simple docstring'''




             super().__init__(**_UpperCAmelCase					)

             if text_config is None:
                   UpperCAmelCase_    				=							{}
                   logger.info("text_config is None. Initializing the OwlViTTextConfig with default values."					)

             if vision_config is None:
                   UpperCAmelCase_    				=							{}
                   logger.info("vision_config is None. initializing the OwlViTVisionConfig with default values."					)

             UpperCAmelCase_    				=							OwlViTTextConfig(**_UpperCAmelCase					)
             UpperCAmelCase_    				=							OwlViTVisionConfig(**_UpperCAmelCase					)

             UpperCAmelCase_    				=							projection_dim
             UpperCAmelCase_    				=							logit_scale_init_value
             UpperCAmelCase_    				=							return_dict
             UpperCAmelCase_    				=							1.0



       @classmethod
       def   lowercase__       (							cls    :						Dict	,       _UpperCAmelCase    :						Union[str, os.PathLike]	,       **_UpperCAmelCase    :						Tuple					)				->		"PretrainedConfig":


             '''simple docstring'''




             cls._set_token_in_kwargs(_UpperCAmelCase					)

             UpperCAmelCase_				,					UpperCAmelCase_    				=							cls.get_config_dict(_UpperCAmelCase	,       **_UpperCAmelCase					)

             if "model_type" in config_dict and hasattr(cls	,       "model_type"					) and config_dict["model_type"] != cls.model_type:
                   logger.warning(
                       F"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """
                       F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors."""					)

             return cls.from_dict(_UpperCAmelCase	,       **_UpperCAmelCase					)



       @classmethod
       def   lowercase__       (							cls    :						List[str]	,       _UpperCAmelCase    :						Dict	,       _UpperCAmelCase    :						Dict	,       **_UpperCAmelCase    :						Any					)				->		Union[str, Any]:


             '''simple docstring'''




             UpperCAmelCase_    				=							{}
             UpperCAmelCase_    				=							text_config
             UpperCAmelCase_    				=							vision_config

             return cls.from_dict(_UpperCAmelCase	,       **_UpperCAmelCase					)







       def   lowercase__       (							self    :						str					)				->		List[str]:


             '''simple docstring'''




             UpperCAmelCase_    				=							copy.deepcopy(self.__dict__					)
             UpperCAmelCase_    				=							self.text_config.to_dict()
             UpperCAmelCase_    				=							self.vision_config.to_dict()
             UpperCAmelCase_    				=							self.__class__.model_type
             return output



class    lowercase__     (					SCREAMING_SNAKE_CASE   ):







       '''simple docstring'''


       @property
       def   lowercase__       (							self    :						Union[str, Any]					)				->		Mapping[str, Mapping[int, str]]:


             '''simple docstring'''




             return OrderedDict(
                 [
                     ("input_ids", {0: "batch", 1: "sequence"}),
                     ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
                     ("attention_mask", {0: "batch", 1: "sequence"}),
                 ]					)



       @property
       def   lowercase__       (							self    :						Tuple					)				->		Mapping[str, Mapping[int, str]]:


             '''simple docstring'''




             return OrderedDict(
                 [
                     ("logits_per_image", {0: "batch"}),
                     ("logits_per_text", {0: "batch"}),
                     ("text_embeds", {0: "batch"}),
                     ("image_embeds", {0: "batch"}),
                 ]					)



       @property
       def   lowercase__       (							self    :						Any					)				->		float:


             '''simple docstring'''




             return 1e-4



       def   lowercase__       (							self    :						List[str]	,       _UpperCAmelCase    :						"ProcessorMixin"	,       _UpperCAmelCase    :						int = -1	,       _UpperCAmelCase    :						int = -1	,       _UpperCAmelCase    :						Optional["TensorType"] = None	,       )				->		Mapping[str, Any]:


             '''simple docstring'''




             UpperCAmelCase_    				=							super().generate_dummy_inputs(
                 processor.tokenizer	,       batch_size=_UpperCAmelCase	,       seq_length=_UpperCAmelCase	,       framework=_UpperCAmelCase					)
             UpperCAmelCase_    				=							super().generate_dummy_inputs(
                 processor.image_processor	,       batch_size=_UpperCAmelCase	,       framework=_UpperCAmelCase					)
             return {**text_input_dict, **image_input_dict}







       @property
       def   lowercase__       (							self    :						Dict					)				->		int:


             '''simple docstring'''




             return 14




