


'''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__						:   Tuple				=    logging.get_logger(__name__)

A__						:   Dict				=    {
    """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   UpperCAmelCase_    (_UpperCAmelCase    ):





    """simple docstring"""






    lowerCamelCase    :   str					=     'owlvit_text_model'







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

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







    @classmethod
    def  lowercase_							(				cls     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )							->	"PretrainedConfig":
         cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase							,			__lowerCamelCase  :      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":
              __lowerCamelCase  :      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(SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )







class   UpperCAmelCase_    (_UpperCAmelCase    ):





    """simple docstring"""






    lowerCamelCase    :   Tuple					=     'owlvit_vision_model'







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

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







    @classmethod
    def  lowercase_							(				cls     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )							->	"PretrainedConfig":
         cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase							,			__lowerCamelCase  :      Optional[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":
              __lowerCamelCase  :      Tuple														=						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   UpperCAmelCase_    (_UpperCAmelCase    ):





    """simple docstring"""






    lowerCamelCase    :   Optional[int]					=     'owlvit'
    lowerCamelCase    :   Dict					=     True







    def __init__(				self     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_=5_12     ,	SCREAMING_SNAKE_CASE_=2.6_5_9_2     ,	SCREAMING_SNAKE_CASE_=True     ,	**SCREAMING_SNAKE_CASE_     ,	)							->	Optional[int]:
         super().__init__(**SCREAMING_SNAKE_CASE_      )

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

         if vision_config is None:
              __lowerCamelCase  :      Optional[Any]														=						{}
              logger.info('vision_config is None. initializing the OwlViTVisionConfig with default values.'      )

         __lowerCamelCase  :      List[str]														=						OwlViTTextConfig(**SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      List[Any]														=						OwlViTVisionConfig(**SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      Any														=						projection_dim
         __lowerCamelCase  :      List[str]														=						logit_scale_init_value
         __lowerCamelCase  :      List[Any]														=						return_dict
         __lowerCamelCase  :      str														=						1.0







    @classmethod
    def  lowercase_							(				cls     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )							->	"PretrainedConfig":
         cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase							,			__lowerCamelCase  :      str														=						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     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )							->	Any:
         __lowerCamelCase  :      Tuple														=						{}
         __lowerCamelCase  :      Optional[int]														=						text_config
         __lowerCamelCase  :      Dict														=						vision_config

         return cls.from_dict(SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )







    def  lowercase_							(				self      )							->	str:
         __lowerCamelCase  :      int														=						copy.deepcopy(self.__dict__      )
         __lowerCamelCase  :      Optional[Any]														=						self.text_config.to_dict()
         __lowerCamelCase  :      Any														=						self.vision_config.to_dict()
         __lowerCamelCase  :      str														=						self.__class__.model_type
         return output







class   UpperCAmelCase_    (_UpperCAmelCase    ):





    """simple docstring"""






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







    @property
    def  lowercase_							(				self      )							->	float:
         return 1E-4







    def  lowercase_							(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_ = -1     ,	SCREAMING_SNAKE_CASE_ = -1     ,	SCREAMING_SNAKE_CASE_ = None     ,	)							->	Mapping[str, Any]:
         __lowerCamelCase  :      Optional[int]														=						super().generate_dummy_inputs(
             processor.tokenizer     ,	batch_size=SCREAMING_SNAKE_CASE_     ,	seq_length=SCREAMING_SNAKE_CASE_     ,	framework=SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      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      )							->	int:
         return 14










'''simple docstring'''



import flax.linen as nn
import jax
import jax.numpy as jnp





class   UpperCAmelCase_    (nn.Module    ):





    """simple docstring"""






    lowerCamelCase    :   int
    lowerCamelCase    :   jnp.dtype					=     jnp.floataa







    def  lowercase_							(				self      )							->	Union[str, Any]:
         __lowerCamelCase  :      Optional[Any]														=						nn.Conv(
             self.out_channels     ,	kernel_size=(3, 3)     ,	strides=(1, 1)     ,	padding=((1, 1), (1, 1))     ,	dtype=self.dtype     ,	)







    def __call__(				self     ,	SCREAMING_SNAKE_CASE_      )							->	Any:
         __lowerCamelCase							,			__lowerCamelCase							,			__lowerCamelCase							,			__lowerCamelCase  :      Optional[int]														=						hidden_states.shape
         __lowerCamelCase  :      Dict														=						jax.image.resize(
             SCREAMING_SNAKE_CASE_     ,	shape=(batch, height * 2, width * 2, channels)     ,	method='nearest'     ,	)
         __lowerCamelCase  :      Optional[Any]														=						self.conv(SCREAMING_SNAKE_CASE_      )
         return hidden_states







class   UpperCAmelCase_    (nn.Module    ):





    """simple docstring"""






    lowerCamelCase    :   int
    lowerCamelCase    :   jnp.dtype					=     jnp.floataa







    def  lowercase_							(				self      )							->	List[str]:
         __lowerCamelCase  :      str														=						nn.Conv(
             self.out_channels     ,	kernel_size=(3, 3)     ,	strides=(2, 2)     ,	padding=((1, 1), (1, 1))     ,	dtype=self.dtype     ,	)







    def __call__(				self     ,	SCREAMING_SNAKE_CASE_      )							->	List[str]:
         # pad = ((0, 0), (0, 1), (0, 1), (0, 0))  # pad height and width dim
         # hidden_states = jnp.pad(hidden_states, pad_width=pad)
         __lowerCamelCase  :      str														=						self.conv(SCREAMING_SNAKE_CASE_      )
         return hidden_states







class   UpperCAmelCase_    (nn.Module    ):





    """simple docstring"""






    lowerCamelCase    :   int
    lowerCamelCase    :   int					=     None
    lowerCamelCase    :   float					=     0.0
    lowerCamelCase    :   bool					=     None
    lowerCamelCase    :   jnp.dtype					=     jnp.floataa







    def  lowercase_							(				self      )							->	Optional[int]:
         __lowerCamelCase  :      Optional[Any]														=						self.in_channels if self.out_channels is None else self.out_channels

         __lowerCamelCase  :      Optional[Any]														=						nn.GroupNorm(num_groups=32     ,	epsilon=1E-5      )
         __lowerCamelCase  :      Tuple														=						nn.Conv(
             SCREAMING_SNAKE_CASE_     ,	kernel_size=(3, 3)     ,	strides=(1, 1)     ,	padding=((1, 1), (1, 1))     ,	dtype=self.dtype     ,	)

         __lowerCamelCase  :      List[str]														=						nn.Dense(SCREAMING_SNAKE_CASE_     ,	dtype=self.dtype      )

         __lowerCamelCase  :      Dict														=						nn.GroupNorm(num_groups=32     ,	epsilon=1E-5      )
         __lowerCamelCase  :      int														=						nn.Dropout(self.dropout_prob      )
         __lowerCamelCase  :      Union[str, Any]														=						nn.Conv(
             SCREAMING_SNAKE_CASE_     ,	kernel_size=(3, 3)     ,	strides=(1, 1)     ,	padding=((1, 1), (1, 1))     ,	dtype=self.dtype     ,	)

         __lowerCamelCase  :      Optional[int]														=						self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut

         __lowerCamelCase  :      List[Any]														=						None
         if use_nin_shortcut:
              __lowerCamelCase  :      Any														=						nn.Conv(
                  SCREAMING_SNAKE_CASE_     ,	kernel_size=(1, 1)     ,	strides=(1, 1)     ,	padding='VALID'     ,	dtype=self.dtype     ,	)







    def __call__(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_=True      )							->	Tuple:
         __lowerCamelCase  :      List[Any]														=						hidden_states
         __lowerCamelCase  :      str														=						self.norma(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      Union[str, Any]														=						nn.swish(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      Union[str, Any]														=						self.conva(SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      str														=						self.time_emb_proj(nn.swish(SCREAMING_SNAKE_CASE_      )      )
         __lowerCamelCase  :      List[str]														=						jnp.expand_dims(jnp.expand_dims(SCREAMING_SNAKE_CASE_     ,	1      )     ,	1      )
         __lowerCamelCase  :      Optional[int]														=						hidden_states + temb

         __lowerCamelCase  :      List[Any]														=						self.norma(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      Any														=						nn.swish(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      List[Any]														=						self.dropout(SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      List[Any]														=						self.conva(SCREAMING_SNAKE_CASE_      )

         if self.conv_shortcut is not None:
              __lowerCamelCase  :      List[str]														=						self.conv_shortcut(SCREAMING_SNAKE_CASE_      )

         return hidden_states + residual






