

'''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 					_snake_case		(__SCREAMING_SNAKE_CASE):
			__A  :	Any        ="owlvit_text_model"






			def __init__(  self			,_snake_case=4_94_08			,_snake_case=5_12			,_snake_case=20_48			,_snake_case=12			,_snake_case=8			,_snake_case=16			,_snake_case="quick_gelu"			,_snake_case=1E-5			,_snake_case=0.0			,_snake_case=0.02			,_snake_case=1.0			,_snake_case=0			,_snake_case=4_94_06			,_snake_case=4_94_07			,**_snake_case			,):
				super().__init__(pad_token_id=_snake_case			,bos_token_id=_snake_case			,eos_token_id=_snake_case			,**_snake_case     )

				UpperCAmelCase_     :      Any						=  vocab_size
				UpperCAmelCase_     :      List[Any]						=  hidden_size
				UpperCAmelCase_     :      List[str]						=  intermediate_size
				UpperCAmelCase_     :      str						=  num_hidden_layers
				UpperCAmelCase_     :      Optional[Any]						=  num_attention_heads
				UpperCAmelCase_     :      List[Any]						=  max_position_embeddings
				UpperCAmelCase_     :      Union[str, Any]						=  hidden_act
				UpperCAmelCase_     :      Dict						=  layer_norm_eps
				UpperCAmelCase_     :      Optional[int]						=  attention_dropout
				UpperCAmelCase_     :      List[Any]						=  initializer_range
				UpperCAmelCase_     :      Optional[int]						=  initializer_factor






			@classmethod
			def 		UpperCamelCase__							(  cls			,_snake_case			,**_snake_case     ):
				cls._set_token_in_kwargs(_snake_case     )

				UpperCAmelCase_					,					UpperCAmelCase_     :      Union[str, Any]						=  cls.get_config_dict(_snake_case			,**_snake_case     )

				# get the text config dict if we are loading from OwlViTConfig
				if config_dict.get("model_type"     ) == "owlvit":
					UpperCAmelCase_     :      Tuple						=  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(_snake_case			,**_snake_case     )




class 					_snake_case		(__SCREAMING_SNAKE_CASE):
			__A  :	Tuple        ="owlvit_vision_model"






			def __init__(  self			,_snake_case=7_68			,_snake_case=30_72			,_snake_case=12			,_snake_case=12			,_snake_case=3			,_snake_case=7_68			,_snake_case=32			,_snake_case="quick_gelu"			,_snake_case=1E-5			,_snake_case=0.0			,_snake_case=0.02			,_snake_case=1.0			,**_snake_case			,):
				super().__init__(**_snake_case     )

				UpperCAmelCase_     :      Any						=  hidden_size
				UpperCAmelCase_     :      Any						=  intermediate_size
				UpperCAmelCase_     :      int						=  num_hidden_layers
				UpperCAmelCase_     :      str						=  num_attention_heads
				UpperCAmelCase_     :      int						=  num_channels
				UpperCAmelCase_     :      Dict						=  image_size
				UpperCAmelCase_     :      Dict						=  patch_size
				UpperCAmelCase_     :      int						=  hidden_act
				UpperCAmelCase_     :      List[str]						=  layer_norm_eps
				UpperCAmelCase_     :      List[str]						=  attention_dropout
				UpperCAmelCase_     :      Optional[Any]						=  initializer_range
				UpperCAmelCase_     :      Optional[int]						=  initializer_factor






			@classmethod
			def 		UpperCamelCase__							(  cls			,_snake_case			,**_snake_case     ):
				cls._set_token_in_kwargs(_snake_case     )

				UpperCAmelCase_					,					UpperCAmelCase_     :      List[Any]						=  cls.get_config_dict(_snake_case			,**_snake_case     )

				# get the vision config dict if we are loading from OwlViTConfig
				if config_dict.get("model_type"     ) == "owlvit":
					UpperCAmelCase_     :      Optional[Any]						=  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(_snake_case			,**_snake_case     )




class 					_snake_case		(__SCREAMING_SNAKE_CASE):
			__A  :	Tuple        ="owlvit"
			__A  :	Optional[int]        =True






			def __init__(  self			,_snake_case=None			,_snake_case=None			,_snake_case=5_12			,_snake_case=2.6592			,_snake_case=True			,**_snake_case			,):
				super().__init__(**_snake_case     )

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

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

				UpperCAmelCase_     :      Tuple						=  OwlViTTextConfig(**_snake_case     )
				UpperCAmelCase_     :      Optional[int]						=  OwlViTVisionConfig(**_snake_case     )

				UpperCAmelCase_     :      List[Any]						=  projection_dim
				UpperCAmelCase_     :      Union[str, Any]						=  logit_scale_init_value
				UpperCAmelCase_     :      int						=  return_dict
				UpperCAmelCase_     :      Optional[Any]						=  1.0






			@classmethod
			def 		UpperCamelCase__							(  cls			,_snake_case			,**_snake_case     ):
				cls._set_token_in_kwargs(_snake_case     )

				UpperCAmelCase_					,					UpperCAmelCase_     :      List[Any]						=  cls.get_config_dict(_snake_case			,**_snake_case     )

				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(_snake_case			,**_snake_case     )






			@classmethod
			def 		UpperCamelCase__							(  cls			,_snake_case			,_snake_case			,**_snake_case     ):
				UpperCAmelCase_     :      Tuple						=  {}
				UpperCAmelCase_     :      List[Any]						=  text_config
				UpperCAmelCase_     :      int						=  vision_config

				return cls.from_dict(_snake_case			,**_snake_case     )






			def 		UpperCamelCase__							(  self     ):
				UpperCAmelCase_     :      str						=  copy.deepcopy(self.__dict__     )
				UpperCAmelCase_     :      Any						=  self.text_config.to_dict()
				UpperCAmelCase_     :      Tuple						=  self.vision_config.to_dict()
				UpperCAmelCase_     :      Union[str, Any]						=  self.__class__.model_type
				return output




class 					_snake_case		(__SCREAMING_SNAKE_CASE):






			@property
			def 		UpperCamelCase__							(  self     ):
				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 		UpperCamelCase__							(  self     ):
				return OrderedDict(
				    [
				        ("logits_per_image", {0: "batch"}),
				        ("logits_per_text", {0: "batch"}),
				        ("text_embeds", {0: "batch"}),
				        ("image_embeds", {0: "batch"}),
				    ]     )






			@property
			def 		UpperCamelCase__							(  self     ):
				return 1E-4






			def 		UpperCamelCase__							(  self			,_snake_case			,_snake_case = -1			,_snake_case = -1			,_snake_case = None			,):
				UpperCAmelCase_     :      str						=  super().generate_dummy_inputs(
				    processor.tokenizer			,batch_size=_snake_case			,seq_length=_snake_case			,framework=_snake_case     )
				UpperCAmelCase_     :      List[Any]						=  super().generate_dummy_inputs(
				    processor.image_processor			,batch_size=_snake_case			,framework=_snake_case     )
				return {**text_input_dict, **image_input_dict}






			@property
			def 		UpperCamelCase__							(  self     ):
				return 14





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

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


SCREAMING_SNAKE_CASE		:     Union[str, Any]         =	logging.get_logger(__name__)


SCREAMING_SNAKE_CASE		:     Union[str, Any]         =	"▁"

SCREAMING_SNAKE_CASE		:     Dict         =	{"vocab_file": "spiece.model"}

SCREAMING_SNAKE_CASE		:     List[Any]         =	{
    "vocab_file": {
        "google/reformer-crime-and-punishment": (
            "https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model"
        )
    }
}

SCREAMING_SNAKE_CASE		:     Any         =	{
    "google/reformer-crime-and-punishment": 524288,
}

class        UpperCamelCase						(				__a				):
						a__		:Optional[Any]      =			VOCAB_FILES_NAMES
						a__		:List[Any]      =			PRETRAINED_VOCAB_FILES_MAP
						a__		:Union[str, Any]      =			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
						a__		:List[Any]      =			['''input_ids''', '''attention_mask''']





						def __init__(self			,     __UpperCamelCase			,     __UpperCamelCase="</s>"			,     __UpperCamelCase="<unk>"			,     __UpperCamelCase=[]			,     __UpperCamelCase = None			,     **__UpperCamelCase			,     )				->				None:
												UpperCamelCase_		:							List[str]  =		{} if sp_model_kwargs is None else sp_model_kwargs

												super().__init__(
												    eos_token=__UpperCamelCase			,     unk_token=__UpperCamelCase			,     additional_special_tokens=__UpperCamelCase			,     sp_model_kwargs=self.sp_model_kwargs			,     **__UpperCamelCase			,     )

												UpperCamelCase_		:							int  =		vocab_file
												UpperCamelCase_		:							str  =		spm.SentencePieceProcessor(**self.sp_model_kwargs						)
												self.sp_model.Load(__UpperCamelCase						)





						@property
						def        A_							(self						)				->				Dict:
												return self.sp_model.get_piece_size()





						def        A_							(self						)				->				Dict[str, int]:
												UpperCamelCase_		:							str  =		{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:
												UpperCamelCase_		:							Dict  =		self.__dict__.copy()
												UpperCamelCase_		:							List[str]  =		None
												return state





						def __setstate__(self			,     __UpperCamelCase						)				->				List[Any]:
												UpperCamelCase_		:							List[Any]  =		d

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

												UpperCamelCase_		:							Dict  =		spm.SentencePieceProcessor(**self.sp_model_kwargs						)
												self.sp_model.Load(self.vocab_file						)





						def        A_							(self			,     __UpperCamelCase						)				->				List[str]:
												return self.sp_model.encode(__UpperCamelCase			,     out_type=__UpperCamelCase						)





						def        A_							(self			,     __UpperCamelCase						)				->				Union[str, Any]:
												return self.sp_model.piece_to_id(__UpperCamelCase						)





						def        A_							(self			,     __UpperCamelCase						)				->				Dict:
												if index < self.sp_model.get_piece_size():
																		UpperCamelCase_		:							Union[str, Any]  =		self.sp_model.IdToPiece(__UpperCamelCase						)
												return token





						def        A_							(self			,     __UpperCamelCase						)				->				int:
												UpperCamelCase_		:							Dict  =		[]
												UpperCamelCase_		:							Union[str, Any]  =		""""""
												for token in tokens:
																		# make sure that special tokens are not decoded using sentencepiece model
																		if token in self.all_special_tokens:
																								out_string += self.sp_model.decode(__UpperCamelCase						) + token
																								UpperCamelCase_		:							Optional[Any]  =		[]
																		else:
																								current_sub_tokens.append(__UpperCamelCase						)
												out_string += self.sp_model.decode(__UpperCamelCase						)
												return out_string.strip()





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase = None						)				->				Tuple[str]:
												if not os.path.isdir(__UpperCamelCase						):
																		logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''						)
																		return
												UpperCamelCase_		:							Tuple  =		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_		:							int  =		self.sp_model.serialized_model_proto()
																								fi.write(__UpperCamelCase						)

												return (out_vocab_file,)






