





"""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


a_			=      logging.get_logger(__name__)

a_			=      {
    """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__					):





 """simple docstring"""


 _lowerCamelCase				=					'owlvit_text_model'

 def __init__(   self ,						__lowerCamelCase=4_9408 ,						__lowerCamelCase=512 ,						__lowerCamelCase=2048 ,						__lowerCamelCase=12 ,						__lowerCamelCase=8 ,						__lowerCamelCase=16 ,						__lowerCamelCase="quick_gelu" ,						__lowerCamelCase=1e-5 ,						__lowerCamelCase=0.0 ,						__lowerCamelCase=0.0_2 ,						__lowerCamelCase=1.0 ,						__lowerCamelCase=0 ,						__lowerCamelCase=4_9406 ,						__lowerCamelCase=4_9407 ,						**__lowerCamelCase ,						):



     '''simple docstring'''




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

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







 @classmethod
 def  UpperCamelCase__(   cls ,						__lowerCamelCase ,						**__lowerCamelCase  ):



     '''simple docstring'''




     cls._set_token_in_kwargs(__lowerCAmelCase  )

     __A			,	__A   :						str									=					cls.get_config_dict(__lowerCAmelCase ,						**__lowerCAmelCase  )

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






class 		__snake_case (		SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 _lowerCamelCase				=					'owlvit_vision_model'

 def __init__(   self ,						__lowerCamelCase=768 ,						__lowerCamelCase=3072 ,						__lowerCamelCase=12 ,						__lowerCamelCase=12 ,						__lowerCamelCase=3 ,						__lowerCamelCase=768 ,						__lowerCamelCase=32 ,						__lowerCamelCase="quick_gelu" ,						__lowerCamelCase=1e-5 ,						__lowerCamelCase=0.0 ,						__lowerCamelCase=0.0_2 ,						__lowerCamelCase=1.0 ,						**__lowerCamelCase ,						):



     '''simple docstring'''




     super().__init__(**__lowerCAmelCase  )

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







 @classmethod
 def  UpperCamelCase__(   cls ,						__lowerCamelCase ,						**__lowerCamelCase  ):



     '''simple docstring'''




     cls._set_token_in_kwargs(__lowerCAmelCase  )

     __A			,	__A   :						Union[str, Any]									=					cls.get_config_dict(__lowerCAmelCase ,						**__lowerCAmelCase  )

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






class 		__snake_case (		SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 _lowerCamelCase				=					'owlvit'
 _lowerCamelCase				=					True

 def __init__(   self ,						__lowerCamelCase=None ,						__lowerCamelCase=None ,						__lowerCamelCase=512 ,						__lowerCamelCase=2.6_5_9_2 ,						__lowerCamelCase=True ,						**__lowerCamelCase ,						):



     '''simple docstring'''




     super().__init__(**__lowerCAmelCase  )

     if text_config is None:
         __A   :						Dict									=					{}
         logger.info('''text_config is None. Initializing the OwlViTTextConfig with default values.'''  )

     if vision_config is None:
         __A   :						int									=					{}
         logger.info('''vision_config is None. initializing the OwlViTVisionConfig with default values.'''  )

     __A   :						Optional[Any]									=					OwlViTTextConfig(**__lowerCAmelCase  )
     __A   :						List[Any]									=					OwlViTVisionConfig(**__lowerCAmelCase  )

     __A   :						Dict									=					projection_dim
     __A   :						Tuple									=					logit_scale_init_value
     __A   :						Optional[int]									=					return_dict
     __A   :						int									=					1.0

 @classmethod
 def  UpperCamelCase__(   cls ,						__lowerCamelCase ,						**__lowerCamelCase  ):



     '''simple docstring'''




     cls._set_token_in_kwargs(__lowerCAmelCase  )

     __A			,	__A   :						Tuple									=					cls.get_config_dict(__lowerCAmelCase ,						**__lowerCAmelCase  )

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

 @classmethod
 def  UpperCamelCase__(   cls ,						__lowerCamelCase ,						__lowerCamelCase ,						**__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						int									=					{}
     __A   :						Optional[int]									=					text_config
     __A   :						List[str]									=					vision_config

     return cls.from_dict(__lowerCAmelCase ,						**__lowerCAmelCase  )







 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




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






class 		__snake_case (		SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 @property
 def  UpperCamelCase__(   self  ):



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



     '''simple docstring'''




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

 @property
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     return 1e-4

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase = -1 ,						__lowerCamelCase = -1 ,						__lowerCamelCase = None ,						):



     '''simple docstring'''




     __A   :						Optional[Any]									=					super().generate_dummy_inputs(
         processor.tokenizer ,						batch_size=__lowerCAmelCase ,						seq_length=__lowerCAmelCase ,						framework=__lowerCAmelCase  )
     __A   :						Union[str, Any]									=					super().generate_dummy_inputs(
         processor.image_processor ,						batch_size=__lowerCAmelCase ,						framework=__lowerCAmelCase  )
     return {**text_input_dict, **image_input_dict}







 @property
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     return 14











"""simple docstring"""



import json
import os
import unittest

from transformers import MgpstrTokenizer
from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin






@require_tokenizers
class 		__snake_case (		SCREAMING_SNAKE_CASE__							,							unittest.TestCase					):





 """simple docstring"""


 _lowerCamelCase				=					MgpstrTokenizer
 _lowerCamelCase				=					False
 _lowerCamelCase				=					{}
 _lowerCamelCase				=					False

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     super().setUp()

     # fmt: off
     __A   :						int									=					['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z''']
     # fmt: on
     __A   :						Dict									=					dict(zip(__lowerCamelCase ,						range(len(__lowerCamelCase  )  )  )  )

     __A   :						Optional[Any]									=					os.path.join(self.tmpdirname ,						VOCAB_FILES_NAMES['''vocab_file''']  )
     with open(self.vocab_file ,						'''w''' ,						encoding='''utf-8'''  ) as fp:
         fp.write(json.dumps(__lowerCamelCase  ) + '''\n'''  )

 def  UpperCamelCase__(   self ,						**__lowerCamelCase  ):



     '''simple docstring'''




     return MgpstrTokenizer.from_pretrained(self.tmpdirname ,						**__lowerCamelCase  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						List[str]									=					'''tester'''
     __A   :						Dict									=					'''tester'''
     return input_text, output_text

 @unittest.skip('''MGP-STR always lower cases letters.'''  )
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     pass

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						List[Any]									=					self.get_tokenizers(do_lower_case=__lowerCamelCase  )
     for tokenizer in tokenizers:
         with self.subTest(F"""{tokenizer.__class__.__name__}"""  ):
             __A   :						Union[str, Any]									=					'''[SPECIAL_TOKEN]'''

             tokenizer.add_special_tokens({'''cls_token''': special_token}  )
             __A   :						Optional[Any]									=					tokenizer.encode([special_token] ,						add_special_tokens=__lowerCamelCase  )
             self.assertEqual(len(__lowerCamelCase  ) ,						1  )

             __A   :						List[Any]									=					tokenizer.decode(__lowerCamelCase ,						skip_special_tokens=__lowerCamelCase  )
             self.assertTrue(special_token not in decoded  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Tuple									=					self.get_tokenizers()
     for tokenizer in tokenizers:
         with self.subTest(F"""{tokenizer.__class__.__name__}"""  ):
             __A			,	__A   :						str									=					self.get_input_output_texts(__lowerCamelCase  )

             __A   :						Union[str, Any]									=					tokenizer.tokenize(__lowerCamelCase  )
             __A   :						Union[str, Any]									=					tokenizer.convert_tokens_to_ids(__lowerCamelCase  )
             __A   :						Union[str, Any]									=					tokenizer.encode(__lowerCamelCase ,						add_special_tokens=__lowerCamelCase  )
             self.assertListEqual(__lowerCamelCase ,						__lowerCamelCase  )

             __A   :						Optional[Any]									=					tokenizer.convert_ids_to_tokens(__lowerCamelCase  )
             self.assertNotEqual(len(__lowerCamelCase  ) ,						0  )
             __A   :						Union[str, Any]									=					tokenizer.decode(__lowerCamelCase  )
             self.assertIsInstance(__lowerCamelCase ,						__lowerCamelCase  )

             self.assertEqual(text_a.replace(''' ''' ,						''''''  ) ,						__lowerCamelCase  )

 @unittest.skip('''MGP-STR tokenizer only handles one sequence.'''  )
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     pass







 @unittest.skip('''inputs cannot be pretokenized in MgpstrTokenizer'''  )
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     pass




