



'''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


lowercase											=      logging.get_logger(__name__)







class 						UpperCAmelCase						(		nn.Module):


 '''simple docstring'''




 __magic_name__       :			HybridCLIPConfig
 __magic_name__       :			jnp.dtype	=   jnp.floataa




 def    lowercase_	( self)			->   Union[str, Any]:



      """simple docstring"""







      a_									=self.config.text_config
      a_									=self.config.vision_config

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

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

      a_									=text_module(lowerCAmelCase_				,	dtype=self.dtype)
      a_									=vision_module(lowerCAmelCase_				,	dtype=self.dtype)

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




 def __call__( self				,	lowerCAmelCase_=None				,	lowerCAmelCase_=None				,	lowerCAmelCase_=None				,	lowerCAmelCase_=None				,	lowerCAmelCase_=None				,	lowerCAmelCase_ = True				,	lowerCAmelCase_=None				,	lowerCAmelCase_=None				,	lowerCAmelCase_=None				,	)			->   Dict:



      """simple docstring"""







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

      a_									=self.vision_model(
          pixel_values=lowerCAmelCase_				,	deterministic=lowerCAmelCase_				,	output_attentions=lowerCAmelCase_				,	output_hidden_states=lowerCAmelCase_				,	return_dict=lowerCAmelCase_				,	)

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

      a_									=vision_outputs[1]
      a_									=self.visual_projection(lowerCAmelCase_)

      a_									=text_outputs[1]
      a_									=self.text_projection(lowerCAmelCase_)

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

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







class 						UpperCAmelCase						(		__a):


 '''simple docstring'''




 __magic_name__       :			Any	=   HybridCLIPConfig
 __magic_name__       :			Optional[int]	=   FlaxHybridCLIPModule




 def __init__( self				,	lowerCAmelCase_				,	lowerCAmelCase_ = None				,	lowerCAmelCase_ = 0				,	lowerCAmelCase_ = jnp.floataa				,	**lowerCAmelCase_				,	)			->   Dict:



      """simple docstring"""







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

      a_									=self.module_class(config=lowerCAmelCase_				,	dtype=lowerCAmelCase_				,	**lowerCAmelCase_)
      super().__init__(lowerCAmelCase_				,	lowerCAmelCase_				,	input_shape=lowerCAmelCase_				,	seed=lowerCAmelCase_				,	dtype=lowerCAmelCase_)




 def    lowercase_	( self				,	lowerCAmelCase_				,	lowerCAmelCase_				,	lowerCAmelCase_ = None)			->   FrozenDict:



      """simple docstring"""







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

      a_									=jax.random.normal(lowerCAmelCase_				,	input_shape[1])

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

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




 def __call__( self				,	lowerCAmelCase_				,	lowerCAmelCase_				,	lowerCAmelCase_=None				,	lowerCAmelCase_=None				,	lowerCAmelCase_=None				,	lowerCAmelCase_ = None				,	lowerCAmelCase_ = None				,	lowerCAmelCase_ = False				,	lowerCAmelCase_ = None				,	lowerCAmelCase_ = None				,	lowerCAmelCase_ = None				,	)			->   str:



      """simple docstring"""







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

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

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

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

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

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




 def    lowercase_	( self				,	lowerCAmelCase_				,	lowerCAmelCase_=None				,	lowerCAmelCase_=None				,	lowerCAmelCase_=None				,	lowerCAmelCase_ = None				,	lowerCAmelCase_ = None				,	lowerCAmelCase_=False				,	)			->   Dict:



      """simple docstring"""







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

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

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

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

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

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




 def    lowercase_	( self				,	lowerCAmelCase_				,	lowerCAmelCase_ = None				,	lowerCAmelCase_ = None				,	lowerCAmelCase_=False)			->   Optional[int]:



      """simple docstring"""







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

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

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




 @classmethod
 def    lowercase_	( cls				,	lowerCAmelCase_ = None				,	lowerCAmelCase_ = None				,	*lowerCAmelCase_				,	**lowerCAmelCase_				,	)			->   FlaxPreTrainedModel:



      """simple docstring"""







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

      a_									={
          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_									=kwargs_text.pop("model"				,	lowerCAmelCase_)
      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_									=AutoConfig.from_pretrained(lowerCAmelCase_)
                a_									=text_config

           a_									=FlaxAutoModel.from_pretrained(lowerCAmelCase_				,	*lowerCAmelCase_				,	**lowerCAmelCase_)

      a_									=kwargs_vision.pop("model"				,	lowerCAmelCase_)
      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_									=AutoConfig.from_pretrained(lowerCAmelCase_)
                a_									=vision_config

           a_									=FlaxAutoModel.from_pretrained(lowerCAmelCase_				,	*lowerCAmelCase_				,	**lowerCAmelCase_)

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

      # init model
      a_									=cls(lowerCAmelCase_				,	*lowerCAmelCase_				,	dtype=lowerCAmelCase_				,	**lowerCAmelCase_)

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

      a_									=text_model.params

      return model







'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available


lowercase											=      {}

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

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase											=      ['''BarthezTokenizerFast''']


if TYPE_CHECKING:
  try:
    if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_barthez import BarthezTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_barthez_fast import BarthezTokenizerFast

else:
  import sys

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


