
"""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      :	Union[str, Any]       		=	logging.get_logger(__name__)

class 		a__						(				nn.Module						):
   __lowerCAmelCase						=    42
   __lowerCAmelCase						=    jnp.floataa
   def       __magic_name__      (      self    ):
          lowercase   :    str			=							self.config.text_config
          lowercase   :    str			=							self.config.vision_config

          lowercase   :    Optional[Any]			=							self.config.projection_dim
          lowercase   :    Any			=							text_config.hidden_size
          lowercase   :    List[str]			=							vision_config.hidden_size

          lowercase   :    List[Any]			=							FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
          lowercase   :    Dict			=							FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__     ,   _a    ).module_class

          lowercase   :    Any			=							text_module(_a     ,   dtype=self.dtype    )
          lowercase   :    Any			=							vision_module(_a     ,   dtype=self.dtype    )

          lowercase   :    int			=							nn.Dense(
              self.projection_dim     ,   dtype=self.dtype     ,   kernel_init=jax.nn.initializers.normal(0.0_2    )     ,   use_bias=_a     ,   )
          lowercase   :    int			=							nn.Dense(
              self.projection_dim     ,   dtype=self.dtype     ,   kernel_init=jax.nn.initializers.normal(0.0_2    )     ,   use_bias=_a     ,   )
          lowercase   :    List[Any]			=							self.param("logit_scale"     ,   jax.nn.initializers.ones     ,   []    )

   def __call__(      self     ,   _a=None     ,   _a=None     ,   _a=None     ,   _a=None     ,   _a=None     ,   _a = True     ,   _a=None     ,   _a=None     ,   _a=None     ,   ):
          lowercase   :    int			=							return_dict if return_dict is not None else self.config.return_dict

          lowercase   :    Tuple			=							self.vision_model(
              pixel_values=_a     ,   deterministic=_a     ,   output_attentions=_a     ,   output_hidden_states=_a     ,   return_dict=_a     ,   )

          lowercase   :    str			=							self.text_model(
              input_ids=_a     ,   attention_mask=_a     ,   token_type_ids=_a     ,   position_ids=_a     ,   deterministic=_a     ,   output_attentions=_a     ,   output_hidden_states=_a     ,   return_dict=_a     ,   )

          lowercase   :    Optional[int]			=							vision_outputs[1]
          lowercase   :    Tuple			=							self.visual_projection(_a    )

          lowercase   :    Any			=							text_outputs[1]
          lowercase   :    int			=							self.text_projection(_a    )

          # normalized features
          lowercase   :    Any			=							image_embeds / jnp.linalg.norm(_a     ,   axis=-1     ,   keepdims=_a    )
          lowercase   :    Union[str, Any]			=							text_embeds / jnp.linalg.norm(_a     ,   axis=-1     ,   keepdims=_a    )

          # cosine similarity as logits
          lowercase   :    Any			=							jnp.exp(self.logit_scale    )
          lowercase   :    List[str]			=							jnp.matmul(_a     ,   image_embeds.T    ) * logit_scale
          lowercase   :    Dict			=							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=_a     ,   logits_per_text=_a     ,   text_embeds=_a     ,   image_embeds=_a     ,   text_model_output=_a     ,   vision_model_output=_a     ,   )


class 		a__						(				a_						):
   __lowerCAmelCase						=    HybridCLIPConfig
   __lowerCAmelCase						=    FlaxHybridCLIPModule
   def __init__(      self     ,   _a     ,   _a = None     ,   _a = 0     ,   _a = jnp.floataa     ,   **_a     ,   ):
          if input_shape is None:
                 lowercase   :    str			=							((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

          lowercase   :    List[Any]			=							self.module_class(config=_a     ,   dtype=_a     ,   **_a    )
          super().__init__(_a     ,   _a     ,   input_shape=_a     ,   seed=_a     ,   dtype=_a    )
   def       __magic_name__      (      self     ,   _a     ,   _a     ,   _a = None    ):
          # init input tensor
          lowercase   :    int			=							jnp.zeros(input_shape[0]     ,   dtype="i4"    )
          lowercase   :    List[str]			=							jnp.broadcast_to(jnp.arange(jnp.atleast_ad(_a    ).shape[-1]    )     ,   input_shape[0]    )
          lowercase   :    List[Any]			=							jnp.ones_like(_a    )
          lowercase   :    Optional[Any]			=							jnp.ones_like(_a    )

          lowercase   :    List[str]			=							jax.random.normal(_a     ,   input_shape[1]    )

          lowercase		,   lowercase   :    Any			=							jax.random.split(_a    )
          lowercase   :    List[str]			=							{"params": params_rng, "dropout": dropout_rng}

          return self.module.init(_a     ,   _a     ,   _a     ,   _a     ,   _a     ,   _a    )["params"]
   def __call__(      self     ,   _a     ,   _a     ,   _a=None     ,   _a=None     ,   _a=None     ,   _a = None     ,   _a = None     ,   _a = False     ,   _a = None     ,   _a = None     ,   _a = None     ,   ):
          lowercase   :    Union[str, Any]			=							output_attentions if output_attentions is not None else self.config.output_attentions
          lowercase   :    List[str]			=							(
              output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
          )
          lowercase   :    Optional[int]			=							return_dict if return_dict is not None else self.config.return_dict

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

          if token_type_ids is None:
                 lowercase   :    str			=							jnp.zeros_like(_a    )

          if attention_mask is None:
                 lowercase   :    Any			=							jnp.ones_like(_a    )

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

          return self.module.apply(
              {"params": params or self.params}     ,   jnp.array(_a     ,   dtype="i4"    )     ,   jnp.array(_a     ,   dtype=jnp.floataa    )     ,   jnp.array(_a     ,   dtype="i4"    )     ,   jnp.array(_a     ,   dtype="i4"    )     ,   jnp.array(_a     ,   dtype="i4"    )     ,   not train     ,   _a     ,   _a     ,   _a     ,   rngs=_a     ,   )
   def       __magic_name__      (      self     ,   _a     ,   _a=None     ,   _a=None     ,   _a=None     ,   _a = None     ,   _a = None     ,   _a=False     ,   ):
          if position_ids is None:
                 lowercase   :    Union[str, Any]			=							jnp.broadcast_to(jnp.arange(jnp.atleast_ad(_a    ).shape[-1]    )     ,   input_ids.shape    )

          if token_type_ids is None:
                 lowercase   :    Any			=							jnp.zeros_like(_a    )

          if attention_mask is None:
                 lowercase   :    Tuple			=							jnp.ones_like(_a    )

          # Handle any PRNG if needed
          lowercase   :    Dict			=							{}
          if dropout_rng is not None:
                 lowercase   :    Tuple			=							dropout_rng

          def _get_features(_a     ,   _a     ,   _a     ,   _a     ,   _a     ,   _a    ):
                 lowercase   :    int			=							module.text_model(
                     input_ids=_a     ,   attention_mask=_a     ,   position_ids=_a     ,   token_type_ids=_a     ,   deterministic=_a     ,   )
                 lowercase   :    int			=							text_outputs[1]
                 lowercase   :    Optional[int]			=							module.text_projection(_a    )
                 return text_features

          return self.module.apply(
              {"params": params or self.params}     ,   jnp.array(_a     ,   dtype="i4"    )     ,   jnp.array(_a     ,   dtype="i4"    )     ,   jnp.array(_a     ,   dtype="i4"    )     ,   jnp.array(_a     ,   dtype="i4"    )     ,   not train     ,   method=_get_features     ,   rngs=_a     ,   )
   def       __magic_name__      (      self     ,   _a     ,   _a = None     ,   _a = None     ,   _a=False    ):
          lowercase   :    int			=							{}
          if dropout_rng is not None:
                 lowercase   :    Any			=							dropout_rng

          def _get_features(_a     ,   _a     ,   _a    ):
                 lowercase   :    Union[str, Any]			=							module.vision_model(pixel_values=_a     ,   deterministic=_a    )
                 lowercase   :    List[str]			=							vision_outputs[1]  # pooled_output
                 lowercase   :    List[str]			=							module.visual_projection(_a    )
                 return image_features

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

   @classmethod
   def       __magic_name__      (      cls     ,   _a = None     ,   _a = None     ,   *_a     ,   **_a     ,   ):
          lowercase   :    Any			=							{
              argument[len("text_"    ) :]: value for argument, value in kwargs.items() if argument.startswith("text_"    )
          }

          lowercase   :    Union[str, Any]			=							{
              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
          lowercase   :    List[Any]			=							kwargs_text.pop("model"     ,   _a    )
          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

                        lowercase   :    List[str]			=							AutoConfig.from_pretrained(_a    )
                        lowercase   :    Optional[int]			=							text_config

                 lowercase   :    Tuple			=							FlaxAutoModel.from_pretrained(_a     ,   *_a     ,   **_a    )

          lowercase   :    Union[str, Any]			=							kwargs_vision.pop("model"     ,   _a    )
          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

                        lowercase   :    Tuple			=							AutoConfig.from_pretrained(_a    )
                        lowercase   :    int			=							vision_config

                 lowercase   :    List[Any]			=							FlaxAutoModel.from_pretrained(_a     ,   *_a     ,   **_a    )

          # instantiate config with corresponding kwargs
          lowercase   :    List[str]			=							kwargs.pop("dtype"     ,   jnp.floataa    )
          lowercase   :    Union[str, Any]			=							HybridCLIPConfig.from_text_vision_configs(text_model.config     ,   vision_model.config     ,   **_a    )

          # init model
          lowercase   :    Optional[int]			=							cls(_a     ,   *_a     ,   dtype=_a     ,   **_a    )

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

          lowercase   :    int			=							text_model.params

          return model




"""simple docstring"""




import warnings

from ...utils import logging
from .image_processing_convnext import ConvNextImageProcessor


_A      :	List[str]       		=	logging.get_logger(__name__)

class 		a__						(				a_						):
   def __init__(      self     ,   *_a     ,   **_a    ):
          warnings.warn(
              "The class ConvNextFeatureExtractor is deprecated and will be removed in version 5 of Transformers."
              " Please use ConvNextImageProcessor instead."     ,   _a     ,   )
          super().__init__(*_a     ,   **_a    )


