






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


snake_case			 =						logging.get_logger(__name__)


class        SCREAMING_SNAKE_CASE	(	nn.Module    ):
     '''simple docstring'''


     UpperCamelCase_							:						HybridCLIPConfig
     UpperCamelCase_							:						jnp.dtype								=		jnp.floataa


     def  _A				( self				:		str					):
       SCREAMING_SNAKE_CASE				:					int		  =			self.config.text_config
       SCREAMING_SNAKE_CASE				:					Optional[int]		  =			self.config.vision_config

       SCREAMING_SNAKE_CASE				:					Tuple		  =			self.config.projection_dim
       SCREAMING_SNAKE_CASE				:					List[str]		  =			text_config.hidden_size
       SCREAMING_SNAKE_CASE				:					str		  =			vision_config.hidden_size

       SCREAMING_SNAKE_CASE				:					Optional[int]		  =			FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
       SCREAMING_SNAKE_CASE				:					Optional[Any]		  =			FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__    ,       UpperCAmelCase_					).module_class

       SCREAMING_SNAKE_CASE				:					Tuple		  =			text_module(UpperCAmelCase_    ,       dtype=self.dtype					)
       SCREAMING_SNAKE_CASE				:					str		  =			vision_module(UpperCAmelCase_    ,       dtype=self.dtype					)

       SCREAMING_SNAKE_CASE				:					Any		  =			nn.Dense(
           self.projection_dim    ,       dtype=self.dtype    ,       kernel_init=jax.nn.initializers.normal(0.02					)    ,       use_bias=UpperCAmelCase_    ,       )
       SCREAMING_SNAKE_CASE				:					List[Any]		  =			nn.Dense(
           self.projection_dim    ,       dtype=self.dtype    ,       kernel_init=jax.nn.initializers.normal(0.02					)    ,       use_bias=UpperCAmelCase_    ,       )
       SCREAMING_SNAKE_CASE				:					Dict		  =			self.param("logit_scale"    ,       jax.nn.initializers.ones    ,       []					)






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

       SCREAMING_SNAKE_CASE				:					int		  =			self.vision_model(
           pixel_values=UpperCAmelCase_    ,       deterministic=UpperCAmelCase_    ,       output_attentions=UpperCAmelCase_    ,       output_hidden_states=UpperCAmelCase_    ,       return_dict=UpperCAmelCase_    ,       )

       SCREAMING_SNAKE_CASE				:					List[Any]		  =			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_    ,       )

       SCREAMING_SNAKE_CASE				:					Any		  =			vision_outputs[1]
       SCREAMING_SNAKE_CASE				:					int		  =			self.visual_projection(UpperCAmelCase_					)

       SCREAMING_SNAKE_CASE				:					Dict		  =			text_outputs[1]
       SCREAMING_SNAKE_CASE				:					str		  =			self.text_projection(UpperCAmelCase_					)

       # normalized features
       SCREAMING_SNAKE_CASE				:					Optional[int]		  =			image_embeds / jnp.linalg.norm(UpperCAmelCase_    ,       axis=-1    ,       keepdims=UpperCAmelCase_					)
       SCREAMING_SNAKE_CASE				:					Optional[Any]		  =			text_embeds / jnp.linalg.norm(UpperCAmelCase_    ,       axis=-1    ,       keepdims=UpperCAmelCase_					)

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





class        SCREAMING_SNAKE_CASE	(	lowerCAmelCase    ):
     '''simple docstring'''


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


     def __init__( self				:		Optional[int]    ,       UpperCAmelCase_				:		HybridCLIPConfig    ,       UpperCAmelCase_				:		Optional[Tuple] = None    ,       UpperCAmelCase_				:		int = 0    ,       UpperCAmelCase_				:		jnp.dtype = jnp.floataa    ,       **UpperCAmelCase_				:		int    ,       ):
       if input_shape is None:
         SCREAMING_SNAKE_CASE				:					Optional[int]		  =			((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

       SCREAMING_SNAKE_CASE				:					List[str]		  =			self.module_class(config=UpperCAmelCase_    ,       dtype=UpperCAmelCase_    ,       **UpperCAmelCase_					)
       super().__init__(UpperCAmelCase_    ,       UpperCAmelCase_    ,       input_shape=UpperCAmelCase_    ,       seed=UpperCAmelCase_    ,       dtype=UpperCAmelCase_					)


     def  _A				( self				:		str    ,       UpperCAmelCase_				:		jax.random.PRNGKey    ,       UpperCAmelCase_				:		Tuple    ,       UpperCAmelCase_				:		FrozenDict = None					):
       # init input tensor
       SCREAMING_SNAKE_CASE				:					int		  =			jnp.zeros(input_shape[0]    ,       dtype="i4"					)
       SCREAMING_SNAKE_CASE				:					Optional[int]		  =			jnp.broadcast_to(jnp.arange(jnp.atleast_ad(UpperCAmelCase_					).shape[-1]					)    ,       input_shape[0]					)
       SCREAMING_SNAKE_CASE				:					List[Any]		  =			jnp.ones_like(UpperCAmelCase_					)
       SCREAMING_SNAKE_CASE				:					List[str]		  =			jnp.ones_like(UpperCAmelCase_					)

       SCREAMING_SNAKE_CASE				:					Tuple		  =			jax.random.normal(UpperCAmelCase_    ,       input_shape[1]					)

       SCREAMING_SNAKE_CASE  ,     SCREAMING_SNAKE_CASE				:					Tuple		  =			jax.random.split(UpperCAmelCase_					)
       SCREAMING_SNAKE_CASE				:					Tuple		  =			{"params": params_rng, "dropout": dropout_rng}

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


     def __call__( self				:		int    ,       UpperCAmelCase_				:		Dict    ,       UpperCAmelCase_				:		Dict    ,       UpperCAmelCase_				:		List[Any]=None    ,       UpperCAmelCase_				:		Tuple=None    ,       UpperCAmelCase_				:		Tuple=None    ,       UpperCAmelCase_				:		dict = None    ,       UpperCAmelCase_				:		jax.random.PRNGKey = None    ,       UpperCAmelCase_				:		bool = False    ,       UpperCAmelCase_				:		Optional[bool] = None    ,       UpperCAmelCase_				:		Optional[bool] = None    ,       UpperCAmelCase_				:		Optional[bool] = None    ,       ):
       SCREAMING_SNAKE_CASE				:					Dict		  =			output_attentions if output_attentions is not None else self.config.output_attentions
       SCREAMING_SNAKE_CASE				:					Optional[Any]		  =			(
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )
       SCREAMING_SNAKE_CASE				:					Tuple		  =			return_dict if return_dict is not None else self.config.return_dict

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

       if token_type_ids is None:
         SCREAMING_SNAKE_CASE				:					Optional[Any]		  =			jnp.zeros_like(UpperCAmelCase_					)

       if attention_mask is None:
         SCREAMING_SNAKE_CASE				:					Dict		  =			jnp.ones_like(UpperCAmelCase_					)

       # Handle any PRNG if needed
       SCREAMING_SNAKE_CASE				:					int		  =			{}
       if dropout_rng is not None:
         SCREAMING_SNAKE_CASE				:					List[Any]		  =			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  _A				( self				:		Optional[int]    ,       UpperCAmelCase_				:		Tuple    ,       UpperCAmelCase_				:		List[str]=None    ,       UpperCAmelCase_				:		Tuple=None    ,       UpperCAmelCase_				:		Optional[Any]=None    ,       UpperCAmelCase_				:		dict = None    ,       UpperCAmelCase_				:		jax.random.PRNGKey = None    ,       UpperCAmelCase_				:		Dict=False    ,       ):
       if position_ids is None:
         SCREAMING_SNAKE_CASE				:					List[Any]		  =			jnp.broadcast_to(jnp.arange(jnp.atleast_ad(UpperCAmelCase_					).shape[-1]					)    ,       input_ids.shape					)

       if token_type_ids is None:
         SCREAMING_SNAKE_CASE				:					Optional[Any]		  =			jnp.zeros_like(UpperCAmelCase_					)

       if attention_mask is None:
         SCREAMING_SNAKE_CASE				:					List[str]		  =			jnp.ones_like(UpperCAmelCase_					)

       # Handle any PRNG if needed
       SCREAMING_SNAKE_CASE				:					str		  =			{}
       if dropout_rng is not None:
         SCREAMING_SNAKE_CASE				:					Any		  =			dropout_rng

       def _get_features(UpperCAmelCase_				:		int    ,       UpperCAmelCase_				:		List[str]    ,       UpperCAmelCase_				:		Dict    ,       UpperCAmelCase_				:		Optional[Any]    ,       UpperCAmelCase_				:		Any    ,       UpperCAmelCase_				:		str					):
         SCREAMING_SNAKE_CASE				:					Any		  =			module.text_model(
             input_ids=UpperCAmelCase_    ,       attention_mask=UpperCAmelCase_    ,       position_ids=UpperCAmelCase_    ,       token_type_ids=UpperCAmelCase_    ,       deterministic=UpperCAmelCase_    ,       )
         SCREAMING_SNAKE_CASE				:					Tuple		  =			text_outputs[1]
         SCREAMING_SNAKE_CASE				:					Dict		  =			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  _A				( self				:		Union[str, Any]    ,       UpperCAmelCase_				:		Union[str, Any]    ,       UpperCAmelCase_				:		dict = None    ,       UpperCAmelCase_				:		jax.random.PRNGKey = None    ,       UpperCAmelCase_				:		Union[str, Any]=False					):
       SCREAMING_SNAKE_CASE				:					Union[str, Any]		  =			{}
       if dropout_rng is not None:
         SCREAMING_SNAKE_CASE				:					Tuple		  =			dropout_rng

       def _get_features(UpperCAmelCase_				:		str    ,       UpperCAmelCase_				:		str    ,       UpperCAmelCase_				:		Tuple					):
         SCREAMING_SNAKE_CASE				:					Optional[int]		  =			module.vision_model(pixel_values=UpperCAmelCase_    ,       deterministic=UpperCAmelCase_					)
         SCREAMING_SNAKE_CASE				:					Optional[int]		  =			vision_outputs[1]  # pooled_output
         SCREAMING_SNAKE_CASE				:					int		  =			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  _A				( cls				:		int    ,       UpperCAmelCase_				:		str = None    ,       UpperCAmelCase_				:		str = None    ,       *UpperCAmelCase_				:		Optional[Any]    ,       **UpperCAmelCase_				:		Tuple    ,       ):
       SCREAMING_SNAKE_CASE				:					str		  =			{
           argument[len("text_"					) :]: value for argument, value in kwargs.items() if argument.startswith("text_"					)
       }

       SCREAMING_SNAKE_CASE				:					str		  =			{
           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
       SCREAMING_SNAKE_CASE				:					Tuple		  =			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

           SCREAMING_SNAKE_CASE				:					Any		  =			AutoConfig.from_pretrained(UpperCAmelCase_					)
           SCREAMING_SNAKE_CASE				:					List[str]		  =			text_config

         SCREAMING_SNAKE_CASE				:					Optional[int]		  =			FlaxAutoModel.from_pretrained(UpperCAmelCase_    ,       *UpperCAmelCase_    ,       **UpperCAmelCase_					)

       SCREAMING_SNAKE_CASE				:					Optional[int]		  =			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

           SCREAMING_SNAKE_CASE				:					Tuple		  =			AutoConfig.from_pretrained(UpperCAmelCase_					)
           SCREAMING_SNAKE_CASE				:					List[str]		  =			vision_config

         SCREAMING_SNAKE_CASE				:					Optional[int]		  =			FlaxAutoModel.from_pretrained(UpperCAmelCase_    ,       *UpperCAmelCase_    ,       **UpperCAmelCase_					)

       # instantiate config with corresponding kwargs
       SCREAMING_SNAKE_CASE				:					Dict		  =			kwargs.pop("dtype"    ,       jnp.floataa					)
       SCREAMING_SNAKE_CASE				:					Any		  =			HybridCLIPConfig.from_text_vision_configs(text_model.config    ,       vision_model.config    ,       **UpperCAmelCase_					)

       # init model
       SCREAMING_SNAKE_CASE				:					List[Any]		  =			cls(UpperCAmelCase_    ,       *UpperCAmelCase_    ,       dtype=UpperCAmelCase_    ,       **UpperCAmelCase_					)

       if vision_config.model_type == "clip":
         SCREAMING_SNAKE_CASE				:					Optional[int]		  =			vision_model.params["vision_model"]
         SCREAMING_SNAKE_CASE				:					Tuple		  =			vision_model.params["visual_projection"]["kernel"]
       else:
         SCREAMING_SNAKE_CASE				:					Union[str, Any]		  =			vision_model.params

       SCREAMING_SNAKE_CASE				:					int		  =			text_model.params

       return model














import re


def      lowerCamelCase__	(   lowercase ):

  """simple docstring"""






  def docstring_decorator(lowercase ):
    SCREAMING_SNAKE_CASE				:					List[Any]		  =			fn.__doc__
    SCREAMING_SNAKE_CASE				:					Tuple		  =			func_doc.split("\n" )
    SCREAMING_SNAKE_CASE				:					Tuple		  =			0
    while i < len(lowercase ) and re.search(R"^\s*Examples?:\s*$"  ,   lines[i] ) is None:
      i += 1
    if i < len(lowercase ):
      SCREAMING_SNAKE_CASE				:					int		  =			example_docstring
      SCREAMING_SNAKE_CASE				:					Tuple		  =			"\n".join(lowercase )
    else:
      raise ValueError(
          F'''The function {fn} should have an empty \'Examples:\' in its docstring as placeholder, '''
          F'''current docstring is:\n{func_doc}''' )
    SCREAMING_SNAKE_CASE				:					int		  =			func_doc
    return fn

  return docstring_decorator






