


'''simple docstring'''

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


a_  :    List[Any]      =			logging.get_logger(__name__)





class 					snake_case   (   nn.Module      ):





  """simple docstring"""




  _lowerCamelCase		   =  42
  _lowerCamelCase		   =  jnp.floataa






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.config.text_config
   lowerCamelCase_								=  self.config.vision_config

   lowerCamelCase_								=  self.config.projection_dim
   lowerCamelCase_								=  text_config.hidden_size
   lowerCamelCase_								=  vision_config.hidden_size

   lowerCamelCase_								=  FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
   lowerCamelCase_								=  FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__      ,  UpperCamelCase					).module_class

   lowerCamelCase_								=  text_module(UpperCamelCase      ,  dtype=self.dtype					)
   lowerCamelCase_								=  vision_module(UpperCamelCase      ,  dtype=self.dtype					)

   lowerCamelCase_								=  nn.Dense(
       self.projection_dim      ,  dtype=self.dtype      ,  kernel_init=jax.nn.initializers.normal(0.02					)      ,  use_bias=UpperCamelCase      ,  )
   lowerCamelCase_								=  nn.Dense(
       self.projection_dim      ,  dtype=self.dtype      ,  kernel_init=jax.nn.initializers.normal(0.02					)      ,  use_bias=UpperCamelCase      ,  )
   lowerCamelCase_								=  self.param("logit_scale"      ,  jax.nn.initializers.ones      ,  []					)







  def __call__(   self      ,  UpperCamelCase=None      ,  UpperCamelCase=None      ,  UpperCamelCase=None      ,  UpperCamelCase=None      ,  UpperCamelCase=None      ,  UpperCamelCase = True      ,  UpperCamelCase=None      ,  UpperCamelCase=None      ,  UpperCamelCase=None      ,  ):







   """simple docstring"""
   lowerCamelCase_								=  return_dict if return_dict is not None else self.config.return_dict

   lowerCamelCase_								=  self.vision_model(
       pixel_values=UpperCamelCase      ,  deterministic=UpperCamelCase      ,  output_attentions=UpperCamelCase      ,  output_hidden_states=UpperCamelCase      ,  return_dict=UpperCamelCase      ,  )

   lowerCamelCase_								=  self.text_model(
       input_ids=UpperCamelCase      ,  attention_mask=UpperCamelCase      ,  token_type_ids=UpperCamelCase      ,  position_ids=UpperCamelCase      ,  deterministic=UpperCamelCase      ,  output_attentions=UpperCamelCase      ,  output_hidden_states=UpperCamelCase      ,  return_dict=UpperCamelCase      ,  )

   lowerCamelCase_								=  vision_outputs[1]
   lowerCamelCase_								=  self.visual_projection(UpperCamelCase					)

   lowerCamelCase_								=  text_outputs[1]
   lowerCamelCase_								=  self.text_projection(UpperCamelCase					)

   # normalized features
   lowerCamelCase_								=  image_embeds / jnp.linalg.norm(UpperCamelCase      ,  axis=-1      ,  keepdims=UpperCamelCase					)
   lowerCamelCase_								=  text_embeds / jnp.linalg.norm(UpperCamelCase      ,  axis=-1      ,  keepdims=UpperCamelCase					)

   # cosine similarity as logits
   lowerCamelCase_								=  jnp.exp(self.logit_scale					)
   lowerCamelCase_								=  jnp.matmul(UpperCamelCase      ,  image_embeds.T					) * logit_scale
   lowerCamelCase_								=  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=UpperCamelCase      ,  logits_per_text=UpperCamelCase      ,  text_embeds=UpperCamelCase      ,  image_embeds=UpperCamelCase      ,  text_model_output=UpperCamelCase      ,  vision_model_output=UpperCamelCase      ,  )





class 					snake_case   (   lowercase      ):





  """simple docstring"""




  _lowerCamelCase		   =  HybridCLIPConfig
  _lowerCamelCase		   =  FlaxHybridCLIPModule






  def __init__(   self      ,  UpperCamelCase      ,  UpperCamelCase = None      ,  UpperCamelCase = 0      ,  UpperCamelCase = jnp.floataa      ,  **UpperCamelCase      ,  ):







   """simple docstring"""
   if input_shape is None:
    lowerCamelCase_								=  ((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

   lowerCamelCase_								=  self.module_class(config=UpperCamelCase      ,  dtype=UpperCamelCase      ,  **UpperCamelCase					)
   super().__init__(UpperCamelCase      ,  UpperCamelCase      ,  input_shape=UpperCamelCase      ,  seed=UpperCamelCase      ,  dtype=UpperCamelCase					)






  def   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase = None					):







   """simple docstring"""
   # init input tensor
   lowerCamelCase_								=  jnp.zeros(input_shape[0]      ,  dtype="i4"					)
   lowerCamelCase_								=  jnp.broadcast_to(jnp.arange(jnp.atleast_ad(UpperCamelCase					).shape[-1]					)      ,  input_shape[0]					)
   lowerCamelCase_								=  jnp.ones_like(UpperCamelCase					)
   lowerCamelCase_								=  jnp.ones_like(UpperCamelCase					)

   lowerCamelCase_								=  jax.random.normal(UpperCamelCase      ,  input_shape[1]					)

   lowerCamelCase_			,lowerCamelCase_								=  jax.random.split(UpperCamelCase					)
   lowerCamelCase_								=  {"params": params_rng, "dropout": dropout_rng}

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






  def __call__(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase=None      ,  UpperCamelCase=None      ,  UpperCamelCase=None      ,  UpperCamelCase = None      ,  UpperCamelCase = None      ,  UpperCamelCase = False      ,  UpperCamelCase = None      ,  UpperCamelCase = None      ,  UpperCamelCase = None      ,  ):







   """simple docstring"""
   lowerCamelCase_								=  output_attentions if output_attentions is not None else self.config.output_attentions
   lowerCamelCase_								=  (
       output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
   )
   lowerCamelCase_								=  return_dict if return_dict is not None else self.config.return_dict

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

   if token_type_ids is None:
    lowerCamelCase_								=  jnp.zeros_like(UpperCamelCase					)

   if attention_mask is None:
    lowerCamelCase_								=  jnp.ones_like(UpperCamelCase					)

   # Handle any PRNG if needed
   lowerCamelCase_								=  {}
   if dropout_rng is not None:
    lowerCamelCase_								=  dropout_rng

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






  def   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase=None      ,  UpperCamelCase=None      ,  UpperCamelCase=None      ,  UpperCamelCase = None      ,  UpperCamelCase = None      ,  UpperCamelCase=False      ,  ):







   """simple docstring"""
   if position_ids is None:
    lowerCamelCase_								=  jnp.broadcast_to(jnp.arange(jnp.atleast_ad(UpperCamelCase					).shape[-1]					)      ,  input_ids.shape					)

   if token_type_ids is None:
    lowerCamelCase_								=  jnp.zeros_like(UpperCamelCase					)

   if attention_mask is None:
    lowerCamelCase_								=  jnp.ones_like(UpperCamelCase					)

   # Handle any PRNG if needed
   lowerCamelCase_								=  {}
   if dropout_rng is not None:
    lowerCamelCase_								=  dropout_rng

   def _get_features(UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase					):
    lowerCamelCase_								=  module.text_model(
        input_ids=UpperCamelCase      ,  attention_mask=UpperCamelCase      ,  position_ids=UpperCamelCase      ,  token_type_ids=UpperCamelCase      ,  deterministic=UpperCamelCase      ,  )
    lowerCamelCase_								=  text_outputs[1]
    lowerCamelCase_								=  module.text_projection(UpperCamelCase					)
    return text_features

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






  def   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase = None      ,  UpperCamelCase = None      ,  UpperCamelCase=False					):







   """simple docstring"""
   lowerCamelCase_								=  {}
   if dropout_rng is not None:
    lowerCamelCase_								=  dropout_rng

   def _get_features(UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase					):
    lowerCamelCase_								=  module.vision_model(pixel_values=UpperCamelCase      ,  deterministic=UpperCamelCase					)
    lowerCamelCase_								=  vision_outputs[1]  # pooled_output
    lowerCamelCase_								=  module.visual_projection(UpperCamelCase					)
    return image_features

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







  @classmethod
  def   snake_case		(   cls      ,  UpperCamelCase = None      ,  UpperCamelCase = None      ,  *UpperCamelCase      ,  **UpperCamelCase      ,  ):







   """simple docstring"""
   lowerCamelCase_								=  {
       argument[len("text_"					) :]: value for argument, value in kwargs.items() if argument.startswith("text_"					)
   }

   lowerCamelCase_								=  {
       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
   lowerCamelCase_								=  kwargs_text.pop("model"      ,  UpperCamelCase					)
   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

     lowerCamelCase_								=  AutoConfig.from_pretrained(UpperCamelCase					)
     lowerCamelCase_								=  text_config

    lowerCamelCase_								=  FlaxAutoModel.from_pretrained(UpperCamelCase      ,  *UpperCamelCase      ,  **UpperCamelCase					)

   lowerCamelCase_								=  kwargs_vision.pop("model"      ,  UpperCamelCase					)
   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

     lowerCamelCase_								=  AutoConfig.from_pretrained(UpperCamelCase					)
     lowerCamelCase_								=  vision_config

    lowerCamelCase_								=  FlaxAutoModel.from_pretrained(UpperCamelCase      ,  *UpperCamelCase      ,  **UpperCamelCase					)

   # instantiate config with corresponding kwargs
   lowerCamelCase_								=  kwargs.pop("dtype"      ,  jnp.floataa					)
   lowerCamelCase_								=  HybridCLIPConfig.from_text_vision_configs(text_model.config      ,  vision_model.config      ,  **UpperCamelCase					)

   # init model
   lowerCamelCase_								=  cls(UpperCamelCase      ,  *UpperCamelCase      ,  dtype=UpperCamelCase      ,  **UpperCamelCase					)

   if vision_config.model_type == "clip":
    lowerCamelCase_								=  vision_model.params["vision_model"]
    lowerCamelCase_								=  vision_model.params["visual_projection"]["kernel"]
   else:
    lowerCamelCase_								=  vision_model.params

   lowerCamelCase_								=  text_model.params

   return model







'''simple docstring'''

from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


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

a_  :    Tuple      =			{
    """google/electra-small-generator""": """https://huggingface.co/google/electra-small-generator/resolve/main/config.json""",
    """google/electra-base-generator""": """https://huggingface.co/google/electra-base-generator/resolve/main/config.json""",
    """google/electra-large-generator""": """https://huggingface.co/google/electra-large-generator/resolve/main/config.json""",
    """google/electra-small-discriminator""": (
        """https://huggingface.co/google/electra-small-discriminator/resolve/main/config.json"""
    ),
    """google/electra-base-discriminator""": (
        """https://huggingface.co/google/electra-base-discriminator/resolve/main/config.json"""
    ),
    """google/electra-large-discriminator""": (
        """https://huggingface.co/google/electra-large-discriminator/resolve/main/config.json"""
    ),
}





class 					snake_case   (   lowercase      ):





  """simple docstring"""




  _lowerCamelCase		   =  "electra"






  def __init__(   self      ,  UpperCamelCase=3_0522      ,  UpperCamelCase=128      ,  UpperCamelCase=256      ,  UpperCamelCase=12      ,  UpperCamelCase=4      ,  UpperCamelCase=1024      ,  UpperCamelCase="gelu"      ,  UpperCamelCase=0.1      ,  UpperCamelCase=0.1      ,  UpperCamelCase=512      ,  UpperCamelCase=2      ,  UpperCamelCase=0.02      ,  UpperCamelCase=1e-12      ,  UpperCamelCase="first"      ,  UpperCamelCase=True      ,  UpperCamelCase="gelu"      ,  UpperCamelCase=0.1      ,  UpperCamelCase=0      ,  UpperCamelCase="absolute"      ,  UpperCamelCase=True      ,  UpperCamelCase=None      ,  **UpperCamelCase      ,  ):







   """simple docstring"""
   super().__init__(pad_token_id=UpperCamelCase      ,  **UpperCamelCase					)

   lowerCamelCase_								=  vocab_size
   lowerCamelCase_								=  embedding_size
   lowerCamelCase_								=  hidden_size
   lowerCamelCase_								=  num_hidden_layers
   lowerCamelCase_								=  num_attention_heads
   lowerCamelCase_								=  intermediate_size
   lowerCamelCase_								=  hidden_act
   lowerCamelCase_								=  hidden_dropout_prob
   lowerCamelCase_								=  attention_probs_dropout_prob
   lowerCamelCase_								=  max_position_embeddings
   lowerCamelCase_								=  type_vocab_size
   lowerCamelCase_								=  initializer_range
   lowerCamelCase_								=  layer_norm_eps

   lowerCamelCase_								=  summary_type
   lowerCamelCase_								=  summary_use_proj
   lowerCamelCase_								=  summary_activation
   lowerCamelCase_								=  summary_last_dropout
   lowerCamelCase_								=  position_embedding_type
   lowerCamelCase_								=  use_cache
   lowerCamelCase_								=  classifier_dropout





class 					snake_case   (   lowercase      ):





  """simple docstring"""




  @property
  def   snake_case		(   self					):







   """simple docstring"""
   if self.task == "multiple-choice":
    lowerCamelCase_								=  {0: "batch", 1: "choice", 2: "sequence"}
   else:
    lowerCamelCase_								=  {0: "batch", 1: "sequence"}
   return OrderedDict(
       [
           ("input_ids", dynamic_axis),
           ("attention_mask", dynamic_axis),
           ("token_type_ids", dynamic_axis),
       ]					)



