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  _UpperCAmelCase       (			nn.Module   ):



 '''simple docstring'''




 __A           = 42
 __A           = jnp.floataa







 def 						__UpperCAmelCase							(						self							:					Dict)		->     Optional[int]:


   """simple docstring"""







   _UpperCamelCase				=  self.config.text_config
   _UpperCamelCase				=  self.config.vision_config

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

   _UpperCamelCase				=  FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
   _UpperCamelCase				=  FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__				,      lowercase_).module_class

   _UpperCamelCase				=  text_module(lowercase_				,      dtype=self.dtype)
   _UpperCamelCase				=  vision_module(lowercase_				,      dtype=self.dtype)

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







 def __call__(						self							:					Tuple				,      lowercase_							:					List[str]=None				,      lowercase_							:					Tuple=None				,      lowercase_							:					Union[str, Any]=None				,      lowercase_							:					Optional[Any]=None				,      lowercase_							:					List[Any]=None				,      lowercase_							:					bool = True				,      lowercase_							:					Optional[Any]=None				,      lowercase_							:					Union[str, Any]=None				,      lowercase_							:					Dict=None				,      )		->     Optional[Any]:


   """simple docstring"""







   _UpperCamelCase				=  return_dict if return_dict is not None else self.config.return_dict

   _UpperCamelCase				=  self.vision_model(
       pixel_values=lowercase_				,      deterministic=lowercase_				,      output_attentions=lowercase_				,      output_hidden_states=lowercase_				,      return_dict=lowercase_				,      )

   _UpperCamelCase				=  self.text_model(
       input_ids=lowercase_				,      attention_mask=lowercase_				,      token_type_ids=lowercase_				,      position_ids=lowercase_				,      deterministic=lowercase_				,      output_attentions=lowercase_				,      output_hidden_states=lowercase_				,      return_dict=lowercase_				,      )

   _UpperCamelCase				=  vision_outputs[1]
   _UpperCamelCase				=  self.visual_projection(lowercase_)

   _UpperCamelCase				=  text_outputs[1]
   _UpperCamelCase				=  self.text_projection(lowercase_)

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

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




class  _UpperCAmelCase       (			lowerCAmelCase   ):



 '''simple docstring'''




 __A           = HybridCLIPConfig
 __A           = FlaxHybridCLIPModule







 def __init__(						self							:					List[Any]				,      lowercase_							:					HybridCLIPConfig				,      lowercase_							:					Optional[Tuple] = None				,      lowercase_							:					int = 0				,      lowercase_							:					jnp.dtype = jnp.floataa				,      **lowercase_							:					Dict				,      )		->     Dict:


   """simple docstring"""







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

   _UpperCamelCase				=  self.module_class(config=lowercase_				,      dtype=lowercase_				,      **lowercase_)
   super().__init__(lowercase_				,      lowercase_				,      input_shape=lowercase_				,      seed=lowercase_				,      dtype=lowercase_)







 def 						__UpperCAmelCase							(						self							:					Tuple				,      lowercase_							:					jax.random.PRNGKey				,      lowercase_							:					Tuple				,      lowercase_							:					FrozenDict = None)		->     FrozenDict:


   """simple docstring"""







   _UpperCamelCase				=  jnp.zeros(input_shape[0]				,      dtype="i4")
   _UpperCamelCase				=  jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowercase_).shape[-1])				,      input_shape[0])
   _UpperCamelCase				=  jnp.ones_like(lowercase_)
   _UpperCamelCase				=  jnp.ones_like(lowercase_)

   _UpperCamelCase				=  jax.random.normal(lowercase_				,      input_shape[1])

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

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







 def __call__(						self							:					Union[str, Any]				,      lowercase_							:					List[Any]				,      lowercase_							:					Optional[Any]				,      lowercase_							:					int=None				,      lowercase_							:					Any=None				,      lowercase_							:					List[Any]=None				,      lowercase_							:					dict = None				,      lowercase_							:					jax.random.PRNGKey = None				,      lowercase_							:					bool = False				,      lowercase_							:					Optional[bool] = None				,      lowercase_							:					Optional[bool] = None				,      lowercase_							:					Optional[bool] = None				,      )		->     str:


   """simple docstring"""







   _UpperCamelCase				=  output_attentions if output_attentions is not None else self.config.output_attentions
   _UpperCamelCase				=  (
       output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
   )
   _UpperCamelCase				=  return_dict if return_dict is not None else self.config.return_dict

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

   if token_type_ids is None:
     _UpperCamelCase				=  jnp.zeros_like(lowercase_)

   if attention_mask is None:
     _UpperCamelCase				=  jnp.ones_like(lowercase_)

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

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







 def 						__UpperCAmelCase							(						self							:					Union[str, Any]				,      lowercase_							:					Union[str, Any]				,      lowercase_							:					Optional[int]=None				,      lowercase_							:					int=None				,      lowercase_							:					List[str]=None				,      lowercase_							:					dict = None				,      lowercase_							:					jax.random.PRNGKey = None				,      lowercase_							:					Optional[Any]=False				,      )		->     Optional[Any]:


   """simple docstring"""







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

   if token_type_ids is None:
     _UpperCamelCase				=  jnp.zeros_like(lowercase_)

   if attention_mask is None:
     _UpperCamelCase				=  jnp.ones_like(lowercase_)

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

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

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







 def 						__UpperCAmelCase							(						self							:					Dict				,      lowercase_							:					str				,      lowercase_							:					dict = None				,      lowercase_							:					jax.random.PRNGKey = None				,      lowercase_							:					str=False)		->     Any:


   """simple docstring"""







   _UpperCamelCase				=  {}
   if dropout_rng is not None:
     _UpperCamelCase				=  dropout_rng

   def _get_features(lowercase_							:					Dict				,      lowercase_							:					Optional[Any]				,      lowercase_							:					str):
     _UpperCamelCase				=  module.vision_model(pixel_values=lowercase_				,      deterministic=lowercase_)
     _UpperCamelCase				=  vision_outputs[1]  # pooled_output
     _UpperCamelCase				=  module.visual_projection(lowercase_)
     return image_features

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







 @classmethod
 def 						__UpperCAmelCase							(						cls							:					int				,      lowercase_							:					str = None				,      lowercase_							:					str = None				,      *lowercase_							:					Optional[int]				,      **lowercase_							:					int				,      )		->     FlaxPreTrainedModel:


   """simple docstring"""







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

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

       _UpperCamelCase				=  AutoConfig.from_pretrained(lowercase_)
       _UpperCamelCase				=  text_config

     _UpperCamelCase				=  FlaxAutoModel.from_pretrained(lowercase_				,      *lowercase_				,      **lowercase_)

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

       _UpperCamelCase				=  AutoConfig.from_pretrained(lowercase_)
       _UpperCamelCase				=  vision_config

     _UpperCamelCase				=  FlaxAutoModel.from_pretrained(lowercase_				,      *lowercase_				,      **lowercase_)

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

   # init model
   _UpperCamelCase				=  cls(lowercase_				,      *lowercase_				,      dtype=lowercase_				,      **lowercase_)

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

   _UpperCamelCase				=  text_model.params

   return model





from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available


lowerCamelCase__   = {
    '''configuration_ernie_m''': ['''ERNIE_M_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ErnieMConfig'''],
}

try:
   if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   lowerCamelCase__   = ['''ErnieMTokenizer''']

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   lowerCamelCase__   = [
       '''ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST''',
       '''ErnieMForMultipleChoice''',
       '''ErnieMForQuestionAnswering''',
       '''ErnieMForSequenceClassification''',
       '''ErnieMForTokenClassification''',
       '''ErnieMModel''',
       '''ErnieMPreTrainedModel''',
       '''ErnieMForInformationExtraction''',
   ]


if TYPE_CHECKING:
   from .configuration_ernie_m import ERNIE_M_PRETRAINED_CONFIG_ARCHIVE_MAP, ErnieMConfig

   try:
      if not is_sentencepiece_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .tokenization_ernie_m import ErnieMTokenizer

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_ernie_m import (
          ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST,
          ErnieMForInformationExtraction,
          ErnieMForMultipleChoice,
          ErnieMForQuestionAnswering,
          ErnieMForSequenceClassification,
          ErnieMForTokenClassification,
          ErnieMModel,
          ErnieMPreTrainedModel,
      )


else:
   import sys

   lowerCamelCase__   = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




