




'''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_      :  List[str]				     =  logging.get_logger(__name__)

lowerCAmelCase_      :  Tuple				     =  {
    '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      SCREAMING_SNAKE_CASE    (						snake_case_				):
      __magic_name__  :       List[Any]   							=					'''owlvit_text_model'''






      def __init__( self   :  List[Any]						,						lowercase__   :  Union[str, Any]=4_9408						,						lowercase__   :  Union[str, Any]=512						,						lowercase__   :  Union[str, Any]=2048						,						lowercase__   :  str=12						,						lowercase__   :  List[Any]=8						,						lowercase__   :  Optional[int]=16						,						lowercase__   :  Optional[Any]="quick_gelu"						,						lowercase__   :  Optional[Any]=1e-5						,						lowercase__   :  Any=0.0						,						lowercase__   :  List[str]=0.02						,						lowercase__   :  Tuple=1.0						,						lowercase__   :  Union[str, Any]=0						,						lowercase__   :  Optional[int]=4_9406						,						lowercase__   :  Union[str, Any]=4_9407						,						**lowercase__   :  List[Any]						,						):


           '''simple docstring'''

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

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






      @classmethod
      def 							lowercase_						( cls   :  List[str]						,						lowercase__   :  Union[str, os.PathLike]						,						**lowercase__   :  int       ):


           '''simple docstring'''

           cls._set_token_in_kwargs(lowercase__       )

           a_						,     a_     :							Optional[int]     						=  cls.get_config_dict(lowercase__						,						**lowercase__       )

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







class      SCREAMING_SNAKE_CASE    (						snake_case_				):
      __magic_name__  :       Union[str, Any]   							=					'''owlvit_vision_model'''






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


           '''simple docstring'''

           super().__init__(**lowercase__       )

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






      @classmethod
      def 							lowercase_						( cls   :  Any						,						lowercase__   :  Union[str, os.PathLike]						,						**lowercase__   :  List[Any]       ):


           '''simple docstring'''

           cls._set_token_in_kwargs(lowercase__       )

           a_						,     a_     :							Any     						=  cls.get_config_dict(lowercase__						,						**lowercase__       )

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







class      SCREAMING_SNAKE_CASE    (						snake_case_				):
      __magic_name__  :       List[Any]   							=					'''owlvit'''
      __magic_name__  :       Any   							=					True






      def __init__( self   :  Optional[Any]						,						lowercase__   :  List[Any]=None						,						lowercase__   :  List[Any]=None						,						lowercase__   :  Dict=512						,						lowercase__   :  List[Any]=2.6592						,						lowercase__   :  Optional[int]=True						,						**lowercase__   :  Dict						,						):


           '''simple docstring'''

           super().__init__(**lowercase__       )

           if text_config is None:
                a_     :							Optional[int]     						=  {}
                logger.info("""text_config is None. Initializing the OwlViTTextConfig with default values."""       )

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

           a_     :							Tuple     						=  OwlViTTextConfig(**lowercase__       )
           a_     :							int     						=  OwlViTVisionConfig(**lowercase__       )

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






      @classmethod
      def 							lowercase_						( cls   :  List[str]						,						lowercase__   :  Union[str, os.PathLike]						,						**lowercase__   :  Optional[int]       ):


           '''simple docstring'''

           cls._set_token_in_kwargs(lowercase__       )

           a_						,     a_     :							Any     						=  cls.get_config_dict(lowercase__						,						**lowercase__       )

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






      @classmethod
      def 							lowercase_						( cls   :  int						,						lowercase__   :  Dict						,						lowercase__   :  Dict						,						**lowercase__   :  List[Any]       ):


           '''simple docstring'''

           a_     :							Optional[Any]     						=  {}
           a_     :							Tuple     						=  text_config
           a_     :							List[Any]     						=  vision_config

           return cls.from_dict(lowercase__						,						**lowercase__       )






      def 							lowercase_						( self   :  Optional[Any]       ):


           '''simple docstring'''

           a_     :							Any     						=  copy.deepcopy(self.__dict__       )
           a_     :							Optional[Any]     						=  self.text_config.to_dict()
           a_     :							Tuple     						=  self.vision_config.to_dict()
           a_     :							List[str]     						=  self.__class__.model_type
           return output







class      SCREAMING_SNAKE_CASE    (						snake_case_				):






      @property
      def 							lowercase_						( self   :  Union[str, Any]       ):


           '''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   :  Union[str, Any]       ):


           '''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   :  str       ):


           '''simple docstring'''

           return 1e-4






      def 							lowercase_						( self   :  Optional[int]						,						lowercase__   :  "ProcessorMixin"						,						lowercase__   :  int = -1						,						lowercase__   :  int = -1						,						lowercase__   :  Optional["TensorType"] = None						,						):


           '''simple docstring'''

           a_     :							Optional[int]     						=  super().generate_dummy_inputs(
               processor.tokenizer						,						batch_size=lowercase__						,						seq_length=lowercase__						,						framework=lowercase__       )
           a_     :							Optional[Any]     						=  super().generate_dummy_inputs(
               processor.image_processor						,						batch_size=lowercase__						,						framework=lowercase__       )
           return {**text_input_dict, **image_input_dict}






      @property
      def 							lowercase_						( self   :  List[Any]       ):


           '''simple docstring'''

           return 14






'''simple docstring'''


import json
from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_roberta import RobertaTokenizer


lowerCAmelCase_      :  Optional[Any]				     =  logging.get_logger(__name__)

lowerCAmelCase_      :  int				     =  {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'}

lowerCAmelCase_      :  Union[str, Any]				     =  {
    'vocab_file': {
        'roberta-base': 'https://huggingface.co/roberta-base/resolve/main/vocab.json',
        'roberta-large': 'https://huggingface.co/roberta-large/resolve/main/vocab.json',
        'roberta-large-mnli': 'https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json',
        'distilroberta-base': 'https://huggingface.co/distilroberta-base/resolve/main/vocab.json',
        'roberta-base-openai-detector': 'https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json',
        'roberta-large-openai-detector': (
            'https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json'
        ),
    },
    'merges_file': {
        'roberta-base': 'https://huggingface.co/roberta-base/resolve/main/merges.txt',
        'roberta-large': 'https://huggingface.co/roberta-large/resolve/main/merges.txt',
        'roberta-large-mnli': 'https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt',
        'distilroberta-base': 'https://huggingface.co/distilroberta-base/resolve/main/merges.txt',
        'roberta-base-openai-detector': 'https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt',
        'roberta-large-openai-detector': (
            'https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt'
        ),
    },
    'tokenizer_file': {
        'roberta-base': 'https://huggingface.co/roberta-base/resolve/main/tokenizer.json',
        'roberta-large': 'https://huggingface.co/roberta-large/resolve/main/tokenizer.json',
        'roberta-large-mnli': 'https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json',
        'distilroberta-base': 'https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json',
        'roberta-base-openai-detector': (
            'https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json'
        ),
        'roberta-large-openai-detector': (
            'https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json'
        ),
    },
}

lowerCAmelCase_      :  Union[str, Any]				     =  {
    'roberta-base': 512,
    'roberta-large': 512,
    'roberta-large-mnli': 512,
    'distilroberta-base': 512,
    'roberta-base-openai-detector': 512,
    'roberta-large-openai-detector': 512,
}







class      SCREAMING_SNAKE_CASE    (						snake_case_				):
      __magic_name__  :       int   							=					VOCAB_FILES_NAMES
      __magic_name__  :       List[str]   							=					PRETRAINED_VOCAB_FILES_MAP
      __magic_name__  :       str   							=					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      __magic_name__  :       List[str]   							=					['''input_ids''', '''attention_mask''']
      __magic_name__  :       Any   							=					RobertaTokenizer






      def __init__( self   :  Any						,						lowercase__   :  int=None						,						lowercase__   :  Union[str, Any]=None						,						lowercase__   :  List[Any]=None						,						lowercase__   :  int="replace"						,						lowercase__   :  Dict="<s>"						,						lowercase__   :  int="</s>"						,						lowercase__   :  Optional[Any]="</s>"						,						lowercase__   :  List[Any]="<s>"						,						lowercase__   :  Optional[int]="<unk>"						,						lowercase__   :  Tuple="<pad>"						,						lowercase__   :  Optional[Any]="<mask>"						,						lowercase__   :  List[Any]=False						,						lowercase__   :  Optional[Any]=True						,						**lowercase__   :  List[str]						,						):


           '''simple docstring'''

           super().__init__(
               lowercase__						,						lowercase__						,						tokenizer_file=lowercase__						,						errors=lowercase__						,						bos_token=lowercase__						,						eos_token=lowercase__						,						sep_token=lowercase__						,						cls_token=lowercase__						,						unk_token=lowercase__						,						pad_token=lowercase__						,						mask_token=lowercase__						,						add_prefix_space=lowercase__						,						trim_offsets=lowercase__						,						**lowercase__						,						)

           a_     :							Any     						=  json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()       )
           if pre_tok_state.get("""add_prefix_space"""						,						lowercase__       ) != add_prefix_space:
                a_     :							List[Any]     						=  getattr(lowercase__						,						pre_tok_state.pop("""type"""       )       )
                a_     :							Optional[Any]     						=  add_prefix_space
                a_     :							List[str]     						=  pre_tok_class(**lowercase__       )

           a_     :							List[Any]     						=  add_prefix_space

           a_     :							Any     						=  """post_processor"""
           a_     :							Dict     						=  getattr(self.backend_tokenizer						,						lowercase__						,						lowercase__       )
           if tokenizer_component_instance:
                a_     :							Optional[Any]     						=  json.loads(tokenizer_component_instance.__getstate__()       )

                # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
                if "sep" in state:
                     a_     :							Dict     						=  tuple(state["""sep"""]       )
                if "cls" in state:
                     a_     :							List[str]     						=  tuple(state["""cls"""]       )

                a_     :							str     						=  False

                if state.get("""add_prefix_space"""						,						lowercase__       ) != add_prefix_space:
                     a_     :							Any     						=  add_prefix_space
                     a_     :							List[str]     						=  True

                if state.get("""trim_offsets"""						,						lowercase__       ) != trim_offsets:
                     a_     :							Optional[int]     						=  trim_offsets
                     a_     :							Tuple     						=  True

                if changes_to_apply:
                     a_     :							str     						=  getattr(lowercase__						,						state.pop("""type"""       )       )
                     a_     :							Union[str, Any]     						=  component_class(**lowercase__       )
                     setattr(self.backend_tokenizer						,						lowercase__						,						lowercase__       )






      @property
      def 							lowercase_						( self   :  List[str]       ):


           '''simple docstring'''

           if self._mask_token is None:
                if self.verbose:
                     logger.error("""Using mask_token, but it is not set yet."""       )
                return None
           return str(self._mask_token       )






      @mask_token.setter
      def 							lowercase_						( self   :  Union[str, Any]						,						lowercase__   :  Tuple       ):


           '''simple docstring'''

           a_     :							List[Any]     						=  AddedToken(lowercase__						,						lstrip=lowercase__						,						rstrip=lowercase__       ) if isinstance(lowercase__						,						lowercase__       ) else value
           a_     :							Union[str, Any]     						=  value






      def 							lowercase_						( self   :  Union[str, Any]						,						*lowercase__   :  Union[str, Any]						,						**lowercase__   :  Union[str, Any]       ):


           '''simple docstring'''

           a_     :							Any     						=  kwargs.get("""is_split_into_words"""						,						lowercase__       )
           assert self.add_prefix_space or not is_split_into_words, (
               F"You need to instantiate {self.__class__.__name__} with add_prefix_space=True "
               "to use it with pretokenized inputs."
           )

           return super()._batch_encode_plus(*lowercase__						,						**lowercase__       )






      def 							lowercase_						( self   :  Dict						,						*lowercase__   :  int						,						**lowercase__   :  Dict       ):


           '''simple docstring'''

           a_     :							Any     						=  kwargs.get("""is_split_into_words"""						,						lowercase__       )

           assert self.add_prefix_space or not is_split_into_words, (
               F"You need to instantiate {self.__class__.__name__} with add_prefix_space=True "
               "to use it with pretokenized inputs."
           )

           return super()._encode_plus(*lowercase__						,						**lowercase__       )






      def 							lowercase_						( self   :  Dict						,						lowercase__   :  str						,						lowercase__   :  Optional[str] = None       ):


           '''simple docstring'''

           a_     :							str     						=  self._tokenizer.model.save(lowercase__						,						name=lowercase__       )
           return tuple(lowercase__       )






      def 							lowercase_						( self   :  Dict						,						lowercase__   :  List[Any]						,						lowercase__   :  int=None       ):


           '''simple docstring'''

           a_     :							int     						=  [self.bos_token_id] + token_ids_a + [self.eos_token_id]
           if token_ids_a is None:
                return output

           return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]






      def 							lowercase_						( self   :  List[str]						,						lowercase__   :  List[int]						,						lowercase__   :  Optional[List[int]] = None       ):


           '''simple docstring'''

           a_     :							Optional[Any]     						=  [self.sep_token_id]
           a_     :							Optional[int]     						=  [self.cls_token_id]

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