




"""simple docstring"""

import unittest

from transformers import TrOCRConfig
from transformers.testing_utils import is_torch_available, require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch

       from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM





@require_torch
class 				lowercase_       :



 '''simple docstring'''

 def __init__( self       :       Optional[Any]    ,							_UpperCAmelCase       :       List[Any]    ,							_UpperCAmelCase       :       str=99    ,							_UpperCAmelCase       :       Dict=13    ,							_UpperCAmelCase       :       Optional[Any]=16    ,							_UpperCAmelCase       :       Optional[int]=7    ,							_UpperCAmelCase       :       str=True    ,							_UpperCAmelCase       :       List[Any]=True    ,							_UpperCAmelCase       :       Optional[int]=True    ,							_UpperCAmelCase       :       int=False    ,							_UpperCAmelCase       :       List[Any]=True    ,							_UpperCAmelCase       :       Any=2    ,							_UpperCAmelCase       :       Optional[int]=32    ,							_UpperCAmelCase       :       Tuple=4    ,							_UpperCAmelCase       :       Tuple=4    ,							_UpperCAmelCase       :       str=30    ,							_UpperCAmelCase       :       Any=0    ,							_UpperCAmelCase       :       Tuple=1    ,							_UpperCAmelCase       :       Tuple=2    ,							_UpperCAmelCase       :       Union[str, Any]=None    ,							):
       _A					   =   parent
       _A					   =   batch_size
       _A					   =   decoder_seq_length
       # For common tests
       _A					   =   self.decoder_seq_length
       _A					   =   is_training
       _A					   =   use_attention_mask
       _A					   =   use_labels

       _A					   =   vocab_size
       _A					   =   d_model
       _A					   =   d_model
       _A					   =   decoder_layers
       _A					   =   decoder_layers
       _A					   =   decoder_ffn_dim
       _A					   =   decoder_attention_heads
       _A					   =   decoder_attention_heads
       _A					   =   eos_token_id
       _A					   =   bos_token_id
       _A					   =   pad_token_id
       _A					   =   decoder_start_token_id
       _A					   =   use_cache
       _A					   =   max_position_embeddings

       _A					   =   None
       _A					   =   decoder_seq_length
       _A					   =   2
       _A					   =   1

 def 			lowerCAmelCase_							( self       :       int		):
       _A					   =   ids_tensor([self.batch_size, self.decoder_seq_length]    ,							self.vocab_size		)

       _A					   =   None
       if self.use_attention_mask:
             _A					   =   ids_tensor([self.batch_size, self.decoder_seq_length]    ,							vocab_size=2		)

       _A					   =   None
       if self.use_labels:
             _A					   =   ids_tensor([self.batch_size, self.decoder_seq_length]    ,							self.vocab_size		)

       _A					   =   TrOCRConfig(
           vocab_size=self.vocab_size    ,							d_model=self.d_model    ,							decoder_layers=self.decoder_layers    ,							decoder_ffn_dim=self.decoder_ffn_dim    ,							decoder_attention_heads=self.decoder_attention_heads    ,							eos_token_id=self.eos_token_id    ,							bos_token_id=self.bos_token_id    ,							use_cache=self.use_cache    ,							pad_token_id=self.pad_token_id    ,							decoder_start_token_id=self.decoder_start_token_id    ,							max_position_embeddings=self.max_position_embeddings    ,							)

       return (config, input_ids, attention_mask, lm_labels)

 def 			lowerCAmelCase_							( self       :       int    ,							_UpperCAmelCase       :       List[Any]    ,							_UpperCAmelCase       :       str    ,							_UpperCAmelCase       :       Optional[int]    ,							_UpperCAmelCase       :       Union[str, Any]    ,							):
       _A					   =   True
       _A					   =   TrOCRDecoder(config=_UpperCAmelCase		).to(_UpperCAmelCase		).eval()
       _A					   =   input_ids[:2]

       input_ids[input_ids == 0] += 1
       # first forward pass
       _A					   =   model(_UpperCAmelCase    ,							use_cache=_UpperCAmelCase		)
       _A					   =   model(_UpperCAmelCase		)
       _A					   =   model(_UpperCAmelCase    ,							use_cache=_UpperCAmelCase		)

       self.parent.assertTrue(len(_UpperCAmelCase		) == len(_UpperCAmelCase		)		)
       self.parent.assertTrue(len(_UpperCAmelCase		) == len(_UpperCAmelCase		) + 1		)

       _A					   =   outputs["past_key_values"]

       # create hypothetical next token and extent to next_input_ids
       _A					   =   ids_tensor((2, 1)    ,							config.vocab_size - 1		) + 1

       # append to next input_ids and
       _A					   =   torch.cat([input_ids, next_tokens]    ,							dim=-1		)

       _A					   =   model(_UpperCAmelCase		)["last_hidden_state"]
       _A					   =   model(_UpperCAmelCase    ,							past_key_values=_UpperCAmelCase		)["last_hidden_state"]

       # select random slice
       _A					   =   ids_tensor((1,)    ,							output_from_past.shape[-1]		).item()
       _A					   =   output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
       _A					   =   output_from_past[:, 0, random_slice_idx].detach()

       # test that outputs are equal for slice
       assert torch.allclose(_UpperCAmelCase    ,							_UpperCAmelCase    ,							atol=1E-3		)



 def 			lowerCAmelCase_							( self       :       List[str]		):
       _A					   =   self.prepare_config_and_inputs()
       _A					   =   config_and_inputs

       _A					   =   {"input_ids": input_ids, "attention_mask": attention_mask}
       return config, inputs_dict





@require_torch
class 				lowercase_       (     A__			,	A__			,	A__			,	unittest.TestCase							):



 '''simple docstring'''
 UpperCAmelCase    :     str        =	(TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
 UpperCAmelCase    :     Union[str, Any]        =	(TrOCRForCausalLM,) if is_torch_available() else ()
 UpperCAmelCase    :     Union[str, Any]        =	{'''text-generation''': TrOCRForCausalLM} if is_torch_available() else {}
 UpperCAmelCase    :     str        =	True
 UpperCAmelCase    :     Optional[int]        =	False

 def 			lowerCAmelCase_							( self       :       List[str]		):
       _A					   =   TrOCRStandaloneDecoderModelTester(self    ,							is_training=_UpperCAmelCase		)
       _A					   =   ConfigTester(self    ,							config_class=_UpperCAmelCase		)

 def 			lowerCAmelCase_							( self       :       List[str]		):
       pass

 def 			lowerCAmelCase_							( self       :       Any		):
       pass

 def 			lowerCAmelCase_							( self       :       Tuple		):
       pass

 def 			lowerCAmelCase_							( self       :       Any		):
       self.config_tester.run_common_tests()

 def 			lowerCAmelCase_							( self       :       Optional[int]		):
       _A					   =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_decoder_model_past(*_UpperCAmelCase		)

 def 			lowerCAmelCase_							( self       :       Dict		):
       return



 @unittest.skip('The model doesn\'t support left padding'		)  # and it's not used enough to be worth fixing :)
 def 			lowerCAmelCase_							( self       :       List[str]		):
       pass









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	:							Optional[Any]				=logging.get_logger(__name__)

lowerCamelCase	:							Optional[int]				={
    '''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 			__a  (    A__       ):
 _lowerCAmelCase       :       Optional[int]        = '''owlvit_text_model'''







 def __init__(		self					:   Any							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=4_94_08							,   SCREAMING_SNAKE_CASE					:   List[str]=5_12							,   SCREAMING_SNAKE_CASE					:   List[Any]=20_48							,   SCREAMING_SNAKE_CASE					:   Any=12							,   SCREAMING_SNAKE_CASE					:   Any=8							,   SCREAMING_SNAKE_CASE					:   Dict=16							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]="quick_gelu"							,   SCREAMING_SNAKE_CASE					:   List[str]=1e-5							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=0.0							,   SCREAMING_SNAKE_CASE					:   Any=0.0_2							,   SCREAMING_SNAKE_CASE					:   int=1.0							,   SCREAMING_SNAKE_CASE					:   Any=0							,   SCREAMING_SNAKE_CASE					:   int=4_94_06							,   SCREAMING_SNAKE_CASE					:   List[str]=4_94_07							,   **SCREAMING_SNAKE_CASE					:   Optional[Any]							,   ):


      '''simple docstring'''






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

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







 @classmethod
 def 					__lowercase					(		cls					:   Optional[int]							,   SCREAMING_SNAKE_CASE					:   Union[str, os.PathLike]							,   **SCREAMING_SNAKE_CASE					:   Optional[Any]						):


      '''simple docstring'''






      cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE						)

      UpperCamelCase__				,     UpperCamelCase__			:   Any       				=  cls.get_config_dict(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)

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







class 			__a  (    A__       ):
 _lowerCAmelCase       :       str        = '''owlvit_vision_model'''







 def __init__(		self					:   Optional[int]							,   SCREAMING_SNAKE_CASE					:   str=7_68							,   SCREAMING_SNAKE_CASE					:   Dict=30_72							,   SCREAMING_SNAKE_CASE					:   int=12							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=12							,   SCREAMING_SNAKE_CASE					:   Dict=3							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=7_68							,   SCREAMING_SNAKE_CASE					:   Optional[int]=32							,   SCREAMING_SNAKE_CASE					:   Dict="quick_gelu"							,   SCREAMING_SNAKE_CASE					:   Optional[Any]=1e-5							,   SCREAMING_SNAKE_CASE					:   List[str]=0.0							,   SCREAMING_SNAKE_CASE					:   Dict=0.0_2							,   SCREAMING_SNAKE_CASE					:   Optional[int]=1.0							,   **SCREAMING_SNAKE_CASE					:   Tuple							,   ):


      '''simple docstring'''






      super().__init__(**SCREAMING_SNAKE_CASE						)

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







 @classmethod
 def 					__lowercase					(		cls					:   Dict							,   SCREAMING_SNAKE_CASE					:   Union[str, os.PathLike]							,   **SCREAMING_SNAKE_CASE					:   List[Any]						):


      '''simple docstring'''






      cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE						)

      UpperCamelCase__				,     UpperCamelCase__			:   List[Any]       				=  cls.get_config_dict(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)

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







class 			__a  (    A__       ):
 _lowerCAmelCase       :       str        = '''owlvit'''
 _lowerCAmelCase       :       Tuple        = True







 def __init__(		self					:   Any							,   SCREAMING_SNAKE_CASE					:   Optional[Any]=None							,   SCREAMING_SNAKE_CASE					:   int=None							,   SCREAMING_SNAKE_CASE					:   str=5_12							,   SCREAMING_SNAKE_CASE					:   Any=2.6_5_9_2							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=True							,   **SCREAMING_SNAKE_CASE					:   List[Any]							,   ):


      '''simple docstring'''






      super().__init__(**SCREAMING_SNAKE_CASE						)

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

      if vision_config is None:
           UpperCamelCase__			:   List[str]       				=  {}
           logger.info("vision_config is None. initializing the OwlViTVisionConfig with default values."						)

      UpperCamelCase__			:   Dict       				=  OwlViTTextConfig(**SCREAMING_SNAKE_CASE						)
      UpperCamelCase__			:   Dict       				=  OwlViTVisionConfig(**SCREAMING_SNAKE_CASE						)

      UpperCamelCase__			:   Dict       				=  projection_dim
      UpperCamelCase__			:   Union[str, Any]       				=  logit_scale_init_value
      UpperCamelCase__			:   int       				=  return_dict
      UpperCamelCase__			:   Tuple       				=  1.0







 @classmethod
 def 					__lowercase					(		cls					:   Any							,   SCREAMING_SNAKE_CASE					:   Union[str, os.PathLike]							,   **SCREAMING_SNAKE_CASE					:   Dict						):


      '''simple docstring'''






      cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE						)

      UpperCamelCase__				,     UpperCamelCase__			:   Dict       				=  cls.get_config_dict(SCREAMING_SNAKE_CASE							,   **SCREAMING_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(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)







 @classmethod
 def 					__lowercase					(		cls					:   int							,   SCREAMING_SNAKE_CASE					:   Dict							,   SCREAMING_SNAKE_CASE					:   Dict							,   **SCREAMING_SNAKE_CASE					:   Optional[int]						):


      '''simple docstring'''






      UpperCamelCase__			:   List[Any]       				=  {}
      UpperCamelCase__			:   Union[str, Any]       				=  text_config
      UpperCamelCase__			:   Optional[int]       				=  vision_config

      return cls.from_dict(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)







 def 					__lowercase					(		self					:   Tuple						):


      '''simple docstring'''






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







class 			__a  (    A__       ):







 @property
 def 					__lowercase					(		self					:   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					:   Optional[int]						):


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


      '''simple docstring'''






      return 1e-4







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


      '''simple docstring'''






      UpperCamelCase__			:   Optional[int]       				=  super().generate_dummy_inputs(
          processor.tokenizer							,   batch_size=SCREAMING_SNAKE_CASE							,   seq_length=SCREAMING_SNAKE_CASE							,   framework=SCREAMING_SNAKE_CASE						)
      UpperCamelCase__			:   List[str]       				=  super().generate_dummy_inputs(
          processor.image_processor							,   batch_size=SCREAMING_SNAKE_CASE							,   framework=SCREAMING_SNAKE_CASE						)
      return {**text_input_dict, **image_input_dict}







 @property
 def 					__lowercase					(		self					:   Tuple						):


      '''simple docstring'''






      return 14