




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


__SCREAMING_SNAKE_CASE     : int      =logging.get_logger(__name__)






class       A      (		nn.Module		):
     """simple docstring"""

     _snake_case:						HybridCLIPConfig
     _snake_case:						jnp.dtype  =jnp.floataa




     def 			__snake_case (     self	: Tuple      )					->							List[Any]:
           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__						,	A__      ).module_class

           lowerCamelCase__   	=					text_module(A__						,	dtype=self.dtype      )
           lowerCamelCase__   	=					vision_module(A__						,	dtype=self.dtype      )

           lowerCamelCase__   	=					nn.Dense(
               self.projection_dim						,	dtype=self.dtype						,	kernel_init=jax.nn.initializers.normal(0.02      )						,	use_bias=A__						,	)
           lowerCamelCase__   	=					nn.Dense(
               self.projection_dim						,	dtype=self.dtype						,	kernel_init=jax.nn.initializers.normal(0.02      )						,	use_bias=A__						,	)
           lowerCamelCase__   	=					self.param('logit_scale'						,	jax.nn.initializers.ones						,	[]      )




     def __call__(     self	: Union[str, Any]						,	A__	: List[Any]=None						,	A__	: Any=None						,	A__	: Any=None						,	A__	: Optional[int]=None						,	A__	: str=None						,	A__	: bool = True						,	A__	: Optional[Any]=None						,	A__	: Union[str, Any]=None						,	A__	: Union[str, Any]=None						,	)					->							Optional[int]:
           lowerCamelCase__   	=					return_dict if return_dict is not None else self.config.return_dict

           lowerCamelCase__   	=					self.vision_model(
               pixel_values=A__						,	deterministic=A__						,	output_attentions=A__						,	output_hidden_states=A__						,	return_dict=A__						,	)

           lowerCamelCase__   	=					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__						,	)

           lowerCamelCase__   	=					vision_outputs[1]
           lowerCamelCase__   	=					self.visual_projection(A__      )

           lowerCamelCase__   	=					text_outputs[1]
           lowerCamelCase__   	=					self.text_projection(A__      )

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

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






class       A      (		_lowerCAmelCase		):
     """simple docstring"""

     _snake_case:						List[Any]  =HybridCLIPConfig
     _snake_case:						Optional[int]  =FlaxHybridCLIPModule




     def __init__(     self	: List[Any]						,	A__	: HybridCLIPConfig						,	A__	: Optional[Tuple] = None						,	A__	: int = 0						,	A__	: jnp.dtype = jnp.floataa						,	**A__	: List[Any]						,	)					->							Optional[int]:
           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=A__						,	dtype=A__						,	**A__      )
           super().__init__(A__						,	A__						,	input_shape=A__						,	seed=A__						,	dtype=A__      )




     def 			__snake_case (     self	: List[Any]						,	A__	: jax.random.PRNGKey						,	A__	: Tuple						,	A__	: FrozenDict = None      )					->							FrozenDict:
           # init input tensor
           lowerCamelCase__   	=					jnp.zeros(input_shape[0]						,	dtype='i4'      )
           lowerCamelCase__   	=					jnp.broadcast_to(jnp.arange(jnp.atleast_ad(A__      ).shape[-1]      )						,	input_shape[0]      )
           lowerCamelCase__   	=					jnp.ones_like(A__      )
           lowerCamelCase__   	=					jnp.ones_like(A__      )

           lowerCamelCase__   	=					jax.random.normal(A__						,	input_shape[1]      )

           lowerCamelCase__	,		lowerCamelCase__   	=					jax.random.split(A__      )
           lowerCamelCase__   	=					{'params': params_rng, 'dropout': dropout_rng}

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




     def __call__(     self	: Any						,	A__	: List[Any]						,	A__	: Any						,	A__	: Optional[int]=None						,	A__	: int=None						,	A__	: Any=None						,	A__	: dict = None						,	A__	: jax.random.PRNGKey = None						,	A__	: bool = False						,	A__	: Optional[bool] = None						,	A__	: Optional[bool] = None						,	A__	: Optional[bool] = None						,	)					->							Optional[int]:
           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(A__      ).shape[-1]      )						,	input_ids.shape      )

           if token_type_ids is None:
                 lowerCamelCase__   	=					jnp.zeros_like(A__      )

           if attention_mask is None:
                 lowerCamelCase__   	=					jnp.ones_like(A__      )

           # 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(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 			__snake_case (     self	: Any						,	A__	: Optional[Any]						,	A__	: List[str]=None						,	A__	: str=None						,	A__	: Tuple=None						,	A__	: dict = None						,	A__	: jax.random.PRNGKey = None						,	A__	: str=False						,	)					->							int:
           if position_ids is None:
                 lowerCamelCase__   	=					jnp.broadcast_to(jnp.arange(jnp.atleast_ad(A__      ).shape[-1]      )						,	input_ids.shape      )

           if token_type_ids is None:
                 lowerCamelCase__   	=					jnp.zeros_like(A__      )

           if attention_mask is None:
                 lowerCamelCase__   	=					jnp.ones_like(A__      )

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

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

           def _get_features(A__	: Any						,	A__	: List[Any]						,	A__	: str      ):
                 lowerCamelCase__   	=					module.vision_model(pixel_values=A__						,	deterministic=A__      )
                 lowerCamelCase__   	=					vision_outputs[1]  # pooled_output
                 lowerCamelCase__   	=					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 			__snake_case (     cls	: int						,	A__	: str = None						,	A__	: str = None						,	*A__	: Dict						,	**A__	: int						,	)					->							FlaxPreTrainedModel:
           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'						,	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

                       lowerCamelCase__   	=					AutoConfig.from_pretrained(A__      )
                       lowerCamelCase__   	=					text_config

                 lowerCamelCase__   	=					FlaxAutoModel.from_pretrained(A__						,	*A__						,	**A__      )

           lowerCamelCase__   	=					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

                       lowerCamelCase__   	=					AutoConfig.from_pretrained(A__      )
                       lowerCamelCase__   	=					vision_config

                 lowerCamelCase__   	=					FlaxAutoModel.from_pretrained(A__						,	*A__						,	**A__      )

           # instantiate config with corresponding kwargs
           lowerCamelCase__   	=					kwargs.pop('dtype'						,	jnp.floataa      )
           lowerCamelCase__   	=					HybridCLIPConfig.from_text_vision_configs(text_model.config						,	vision_model.config						,	**A__      )

           # init model
           lowerCamelCase__   	=					cls(A__						,	*A__						,	dtype=A__						,	**A__      )

           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





def       lowerCAmelCase_      (	lowerCamelCase:							str    )       ->      int:


      '''simple docstring'''




      lowerCamelCase__   	=					53_81
      for x in s:
            lowerCamelCase__   	=					((hash_value << 5) + hash_value) + ord(lowerCamelCase    )
      return hash_value & 0Xffff_ffff