


from typing import Optional, Tuple

import flax.linen as nn
import jax
import jax.numpy as jnp
from configuration_hybrid_clip import HybridCLIPConfig
from flax.core.frozen_dict import FrozenDict

from transformers import FLAX_MODEL_MAPPING, FlaxCLIPVisionModel
from transformers.modeling_flax_utils import FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPOutput
from transformers.utils import logging


__lowerCamelCase   	=  logging.get_logger(__name__)


class 	a__						(    nn.Module     ):
       lowerCamelCase__:  HybridCLIPConfig
       lowerCamelCase__:  jnp.dtype				     =  jnp.floataa

       def     UpperCAmelCase( self   :  str     ):
            a_     :  Dict				      =     self.config.text_config
            a_     :  List[str]				      =     self.config.vision_config

            a_     :  Tuple				      =     self.config.projection_dim
            a_     :  Any				      =     text_config.hidden_size
            a_     :  int				      =     vision_config.hidden_size

            a_     :  Any				      =     FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
            a_     :  Any				      =     FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__   ,				__SCREAMING_SNAKE_CASE     ).module_class

            a_     :  Tuple				      =     text_module(__SCREAMING_SNAKE_CASE   ,				dtype=self.dtype     )
            a_     :  str				      =     vision_module(__SCREAMING_SNAKE_CASE   ,				dtype=self.dtype     )

            a_     :  Any				      =     nn.Dense(
                self.projection_dim   ,				dtype=self.dtype   ,				kernel_init=jax.nn.initializers.normal(0.0_2     )   ,				use_bias=__SCREAMING_SNAKE_CASE   ,				)
            a_     :  Tuple				      =     nn.Dense(
                self.projection_dim   ,				dtype=self.dtype   ,				kernel_init=jax.nn.initializers.normal(0.0_2     )   ,				use_bias=__SCREAMING_SNAKE_CASE   ,				)
            a_     :  Optional[Any]				      =     self.param("""logit_scale"""   ,				jax.nn.initializers.ones   ,				[]     )

       def __call__( self   :  Dict   ,				lowerCamelCase_   :  str=None   ,				lowerCamelCase_   :  str=None   ,				lowerCamelCase_   :  Tuple=None   ,				lowerCamelCase_   :  Union[str, Any]=None   ,				lowerCamelCase_   :  Optional[int]=None   ,				lowerCamelCase_   :  Optional[Any] = True   ,				lowerCamelCase_   :  Union[str, Any]=None   ,				lowerCamelCase_   :  int=None   ,				lowerCamelCase_   :  Dict=None   ,				):
            a_     :  List[str]				      =     return_dict if return_dict is not None else self.config.return_dict

            a_     :  Any				      =     self.vision_model(
                pixel_values=__SCREAMING_SNAKE_CASE   ,				deterministic=__SCREAMING_SNAKE_CASE   ,				output_attentions=__SCREAMING_SNAKE_CASE   ,				output_hidden_states=__SCREAMING_SNAKE_CASE   ,				return_dict=__SCREAMING_SNAKE_CASE   ,				)

            a_     :  int				      =     self.text_model(
                input_ids=__SCREAMING_SNAKE_CASE   ,				attention_mask=__SCREAMING_SNAKE_CASE   ,				token_type_ids=__SCREAMING_SNAKE_CASE   ,				position_ids=__SCREAMING_SNAKE_CASE   ,				deterministic=__SCREAMING_SNAKE_CASE   ,				output_attentions=__SCREAMING_SNAKE_CASE   ,				output_hidden_states=__SCREAMING_SNAKE_CASE   ,				return_dict=__SCREAMING_SNAKE_CASE   ,				)

            a_     :  Dict				      =     vision_outputs[1]
            a_     :  Optional[Any]				      =     self.visual_projection(__SCREAMING_SNAKE_CASE     )

            a_     :  Optional[int]				      =     text_outputs[1]
            a_     :  str				      =     self.text_projection(__SCREAMING_SNAKE_CASE     )

            # normalized features
            a_     :  str				      =     image_embeds / jnp.linalg.norm(__SCREAMING_SNAKE_CASE   ,				axis=-1   ,				keepdims=__SCREAMING_SNAKE_CASE     )
            a_     :  int				      =     text_embeds / jnp.linalg.norm(__SCREAMING_SNAKE_CASE   ,				axis=-1   ,				keepdims=__SCREAMING_SNAKE_CASE     )

            # cosine similarity as logits
            a_     :  Any				      =     jnp.exp(self.logit_scale     )
            a_     :  Dict				      =     jnp.matmul(__SCREAMING_SNAKE_CASE   ,				image_embeds.T     ) * logit_scale
            a_     :  List[Any]				      =     logits_per_text.T

            if not return_dict:
                 return (logits_per_image, logits_per_text, text_embeds, image_embeds, text_outputs, vision_outputs)

            return FlaxCLIPOutput(
                logits_per_image=__SCREAMING_SNAKE_CASE   ,				logits_per_text=__SCREAMING_SNAKE_CASE   ,				text_embeds=__SCREAMING_SNAKE_CASE   ,				image_embeds=__SCREAMING_SNAKE_CASE   ,				text_model_output=__SCREAMING_SNAKE_CASE   ,				vision_model_output=__SCREAMING_SNAKE_CASE   ,				)


class 	a__						(    _a     ):
       lowerCamelCase__:  str				     =  HybridCLIPConfig
       lowerCamelCase__:  Any				     =  FlaxHybridCLIPModule

       def __init__( self   :  Optional[int]   ,				lowerCamelCase_   :  Dict   ,				lowerCamelCase_   :  str = None   ,				lowerCamelCase_   :  List[str] = 0   ,				lowerCamelCase_   :  Any = jnp.floataa   ,				**lowerCamelCase_   :  List[str]   ,				):




            if input_shape is None:
                 a_     :  Tuple				      =     ((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

            a_     :  Dict				      =     self.module_class(config=__SCREAMING_SNAKE_CASE   ,				dtype=__SCREAMING_SNAKE_CASE   ,				**__SCREAMING_SNAKE_CASE     )
            super().__init__(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE   ,				input_shape=__SCREAMING_SNAKE_CASE   ,				seed=__SCREAMING_SNAKE_CASE   ,				dtype=__SCREAMING_SNAKE_CASE     )

       def     UpperCAmelCase( self   :  Any   ,				lowerCamelCase_   :  List[Any]   ,				lowerCamelCase_   :  Optional[Any]   ,				lowerCamelCase_   :  Union[str, Any] = None     ):
            a_     :  Optional[Any]				      =     jnp.zeros(input_shape[0]   ,				dtype="""i4"""     )
            a_     :  Tuple				      =     jnp.broadcast_to(jnp.arange(jnp.atleast_ad(__SCREAMING_SNAKE_CASE     ).shape[-1]     )   ,				input_shape[0]     )
            a_     :  str				      =     jnp.ones_like(__SCREAMING_SNAKE_CASE     )
            a_     :  Union[str, Any]				      =     jnp.ones_like(__SCREAMING_SNAKE_CASE     )

            a_     :  Optional[int]				      =     jax.random.normal(__SCREAMING_SNAKE_CASE   ,				input_shape[1]     )

            a_     :  Any				      =     jax.random.split(__SCREAMING_SNAKE_CASE     )
            a_     :  Optional[Any]				      =     {'''params''': params_rng, '''dropout''': dropout_rng}

            return self.module.init(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE     )["params"]

       def __call__( self   :  Optional[int]   ,				lowerCamelCase_   :  str   ,				lowerCamelCase_   :  Union[str, Any]   ,				lowerCamelCase_   :  int=None   ,				lowerCamelCase_   :  Tuple=None   ,				lowerCamelCase_   :  Dict=None   ,				lowerCamelCase_   :  List[Any] = None   ,				lowerCamelCase_   :  List[str] = None   ,				lowerCamelCase_   :  List[str] = False   ,				lowerCamelCase_   :  str = None   ,				lowerCamelCase_   :  Dict = None   ,				lowerCamelCase_   :  List[Any] = None   ,				):
            a_     :  List[Any]				      =     output_attentions if output_attentions is not None else self.config.output_attentions
            a_     :  str				      =     (
                output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
            )
            a_     :  int				      =     return_dict if return_dict is not None else self.config.return_dict

            if position_ids is None:
                 a_     :  Any				      =     jnp.broadcast_to(jnp.arange(jnp.atleast_ad(__SCREAMING_SNAKE_CASE     ).shape[-1]     )   ,				input_ids.shape     )

            if token_type_ids is None:
                 a_     :  Optional[int]				      =     jnp.zeros_like(__SCREAMING_SNAKE_CASE     )

            if attention_mask is None:
                 a_     :  Any				      =     jnp.ones_like(__SCREAMING_SNAKE_CASE     )

            # Handle any PRNG if needed
            a_     :  Dict				      =     {}
            if dropout_rng is not None:
                 a_     :  List[str]				      =     dropout_rng

            return self.module.apply(
                {"""params""": params or self.params}   ,				jnp.array(__SCREAMING_SNAKE_CASE   ,				dtype="""i4"""     )   ,				jnp.array(__SCREAMING_SNAKE_CASE   ,				dtype=jnp.floataa     )   ,				jnp.array(__SCREAMING_SNAKE_CASE   ,				dtype="""i4"""     )   ,				jnp.array(__SCREAMING_SNAKE_CASE   ,				dtype="""i4"""     )   ,				jnp.array(__SCREAMING_SNAKE_CASE   ,				dtype="""i4"""     )   ,				not train   ,				__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE   ,				rngs=__SCREAMING_SNAKE_CASE   ,				)

       def     UpperCAmelCase( self   :  List[Any]   ,				lowerCamelCase_   :  int   ,				lowerCamelCase_   :  Any=None   ,				lowerCamelCase_   :  Dict=None   ,				lowerCamelCase_   :  Union[str, Any]=None   ,				lowerCamelCase_   :  Optional[Any] = None   ,				lowerCamelCase_   :  str = None   ,				lowerCamelCase_   :  Optional[int]=False   ,				):




            if position_ids is None:
                 a_     :  str				      =     jnp.broadcast_to(jnp.arange(jnp.atleast_ad(__SCREAMING_SNAKE_CASE     ).shape[-1]     )   ,				input_ids.shape     )

            if token_type_ids is None:
                 a_     :  Dict				      =     jnp.zeros_like(__SCREAMING_SNAKE_CASE     )

            if attention_mask is None:
                 a_     :  List[str]				      =     jnp.ones_like(__SCREAMING_SNAKE_CASE     )

            # Handle any PRNG if needed
            a_     :  Optional[Any]				      =     {}
            if dropout_rng is not None:
                 a_     :  Optional[int]				      =     dropout_rng

            def _get_features(lowerCamelCase_   :  Optional[Any]   ,				lowerCamelCase_   :  Tuple   ,				lowerCamelCase_   :  Optional[Any]   ,				lowerCamelCase_   :  Optional[Any]   ,				lowerCamelCase_   :  Union[str, Any]   ,				lowerCamelCase_   :  Optional[int]     ):
                 a_     :  int				      =     module.text_model(
                     input_ids=__SCREAMING_SNAKE_CASE   ,				attention_mask=__SCREAMING_SNAKE_CASE   ,				position_ids=__SCREAMING_SNAKE_CASE   ,				token_type_ids=__SCREAMING_SNAKE_CASE   ,				deterministic=__SCREAMING_SNAKE_CASE   ,				)
                 a_     :  Union[str, Any]				      =     text_outputs[1]
                 a_     :  List[Any]				      =     module.text_projection(__SCREAMING_SNAKE_CASE     )
                 return text_features

            return self.module.apply(
                {"""params""": params or self.params}   ,				jnp.array(__SCREAMING_SNAKE_CASE   ,				dtype="""i4"""     )   ,				jnp.array(__SCREAMING_SNAKE_CASE   ,				dtype="""i4"""     )   ,				jnp.array(__SCREAMING_SNAKE_CASE   ,				dtype="""i4"""     )   ,				jnp.array(__SCREAMING_SNAKE_CASE   ,				dtype="""i4"""     )   ,				not train   ,				method=_get_features   ,				rngs=__SCREAMING_SNAKE_CASE   ,				)

       def     UpperCAmelCase( self   :  Any   ,				lowerCamelCase_   :  str   ,				lowerCamelCase_   :  Optional[Any] = None   ,				lowerCamelCase_   :  List[str] = None   ,				lowerCamelCase_   :  str=False     ):
            a_     :  List[str]				      =     {}
            if dropout_rng is not None:
                 a_     :  int				      =     dropout_rng

            def _get_features(lowerCamelCase_   :  List[str]   ,				lowerCamelCase_   :  Optional[int]   ,				lowerCamelCase_   :  Union[str, Any]     ):
                 a_     :  int				      =     module.vision_model(pixel_values=__SCREAMING_SNAKE_CASE   ,				deterministic=__SCREAMING_SNAKE_CASE     )
                 a_     :  int				      =     vision_outputs[1]  # pooled_output
                 a_     :  Tuple				      =     module.visual_projection(__SCREAMING_SNAKE_CASE     )
                 return image_features

            return self.module.apply(
                {"""params""": params or self.params}   ,				jnp.array(__SCREAMING_SNAKE_CASE   ,				dtype=jnp.floataa     )   ,				not train   ,				method=_get_features   ,				rngs=__SCREAMING_SNAKE_CASE   ,				)

       @classmethod
       def     UpperCAmelCase( cls   :  Optional[Any]   ,				lowerCamelCase_   :  List[Any] = None   ,				lowerCamelCase_   :  List[Any] = None   ,				*lowerCamelCase_   :  List[str]   ,				**lowerCamelCase_   :  Dict   ,				):
            a_     :  Optional[Any]				      =     {
                argument[len("""text_"""     ) :]: value for argument, value in kwargs.items() if argument.startswith("""text_"""     )
            }

            a_     :  Dict				      =     {
                argument[len("""vision_"""     ) :]: value for argument, value in kwargs.items() if argument.startswith("""vision_"""     )
            }

            # remove text, vision kwargs from kwargs
            for key in kwargs_text.keys():
                 del kwargs["text_" + key]
            for key in kwargs_vision.keys():
                 del kwargs["vision_" + key]

            # Load and initialize the text and vision model
            a_     :  Optional[Any]				      =     kwargs_text.pop("""model"""   ,				__SCREAMING_SNAKE_CASE     )
            if text_model is None:
                 assert (
                     text_model_name_or_path is not None
                 ), "If `model` is not defined as an argument, a `text_model_name_or_path` has to be defined"
                 from transformers import FlaxAutoModel

                 if "config" not in kwargs_text:
                      from transformers import AutoConfig

                      a_     :  List[str]				      =     AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE     )
                      a_     :  Tuple				      =     text_config

                 a_     :  str				      =     FlaxAutoModel.from_pretrained(__SCREAMING_SNAKE_CASE   ,				*__SCREAMING_SNAKE_CASE   ,				**__SCREAMING_SNAKE_CASE     )

            a_     :  List[Any]				      =     kwargs_vision.pop("""model"""   ,				__SCREAMING_SNAKE_CASE     )
            if vision_model is None:
                 assert (
                     vision_model_name_or_path is not None
                 ), "If `model` is not defined as an argument, a `vision_model_name_or_path` has to be defined"
                 from transformers import FlaxAutoModel

                 if "config" not in kwargs_vision:
                      from transformers import AutoConfig

                      a_     :  Any				      =     AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE     )
                      a_     :  Tuple				      =     vision_config

                 a_     :  Union[str, Any]				      =     FlaxAutoModel.from_pretrained(__SCREAMING_SNAKE_CASE   ,				*__SCREAMING_SNAKE_CASE   ,				**__SCREAMING_SNAKE_CASE     )

            # instantiate config with corresponding kwargs
            a_     :  Optional[Any]				      =     kwargs.pop("""dtype"""   ,				jnp.floataa     )
            a_     :  int				      =     HybridCLIPConfig.from_text_vision_configs(text_model.config   ,				vision_model.config   ,				**__SCREAMING_SNAKE_CASE     )

            # init model
            a_     :  List[str]				      =     cls(__SCREAMING_SNAKE_CASE   ,				*__SCREAMING_SNAKE_CASE   ,				dtype=__SCREAMING_SNAKE_CASE   ,				**__SCREAMING_SNAKE_CASE     )

            if vision_config.model_type == "clip":
                 a_     :  str				      =     vision_model.params['''vision_model''']
                 a_     :  Optional[int]				      =     vision_model.params['''visual_projection''']['''kernel''']
            else:
                 a_     :  Any				      =     vision_model.params

            a_     :  int				      =     text_model.params

            return model







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__UpperCAmelCase			:			int			      =					{"configuration_vilt": ["VILT_PRETRAINED_CONFIG_ARCHIVE_MAP", "ViltConfig"]}

try:
  if not is_vision_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCAmelCase			:			Optional[Any]			      =					["ViltFeatureExtractor"]
  __UpperCAmelCase			:			Union[str, Any]			      =					["ViltImageProcessor"]
  __UpperCAmelCase			:			Dict			      =					["ViltProcessor"]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCAmelCase			:			List[Any]			      =					[
      "VILT_PRETRAINED_MODEL_ARCHIVE_LIST",
      "ViltForImageAndTextRetrieval",
      "ViltForImagesAndTextClassification",
      "ViltForTokenClassification",
      "ViltForMaskedLM",
      "ViltForQuestionAnswering",
      "ViltLayer",
      "ViltModel",
      "ViltPreTrainedModel",
  ]


if TYPE_CHECKING:
  from .configuration_vilt import VILT_PRETRAINED_CONFIG_ARCHIVE_MAP, ViltConfig

  try:
    if not is_vision_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .feature_extraction_vilt import ViltFeatureExtractor
    from .image_processing_vilt import ViltImageProcessor
    from .processing_vilt import ViltProcessor

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_vilt import (
        VILT_PRETRAINED_MODEL_ARCHIVE_LIST,
        ViltForImageAndTextRetrieval,
        ViltForImagesAndTextClassification,
        ViltForMaskedLM,
        ViltForQuestionAnswering,
        ViltForTokenClassification,
        ViltLayer,
        ViltModel,
        ViltPreTrainedModel,
    )


else:
  import sys

  __UpperCAmelCase			:			Any			      =					_LazyModule(__name__, globals()["__file__"], _import_structure)

