


"""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_											=     logging.get_logger(__name__)
class snake_case   (	nn.Module):
		__UpperCamelCase							     =      42
		__UpperCamelCase							     =      jnp.floataa







		def 			a_			(       self   :							Any		)							->    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__     , a__		).module_class

						_A       	=      text_module(a__     , dtype=self.dtype		)
						_A       	=      vision_module(a__     , dtype=self.dtype		)

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







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





						'''simple docstring'''







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

						_A       	=      self.vision_model(
						    pixel_values=a__     , deterministic=a__     , output_attentions=a__     , output_hidden_states=a__     , return_dict=a__     , )

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

						_A       	=      vision_outputs[1]
						_A       	=      self.visual_projection(a__		)

						_A       	=      text_outputs[1]
						_A       	=      self.text_projection(a__		)

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

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





class snake_case   (	_UpperCamelCase):
		__UpperCamelCase							     =      HybridCLIPConfig
		__UpperCamelCase							     =      FlaxHybridCLIPModule







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







		def 			a_			(       self   :							Any     , a__   :							jax.random.PRNGKey     , a__   :							Tuple     , a__   :							FrozenDict = None		)							->    FrozenDict:





						'''simple docstring'''







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

						_A       	=      jax.random.normal(a__     , input_shape[1]		)

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

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







		def __call__(       self   :							str     , a__   :							Any     , a__   :							int     , a__   :							List[Any]=None     , a__   :							Dict=None     , a__   :							List[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     , )							->    int:





						'''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(a__		).shape[-1]		)     , input_ids.shape		)

						if token_type_ids is None:
										_A       	=      jnp.zeros_like(a__		)

						if attention_mask is None:
										_A       	=      jnp.ones_like(a__		)

						# 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(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 			a_			(       self   :							int     , a__   :							int     , a__   :							Dict=None     , a__   :							str=None     , a__   :							List[str]=None     , a__   :							dict = None     , a__   :							jax.random.PRNGKey = None     , a__   :							List[Any]=False     , )							->    Optional[Any]:





						'''simple docstring'''







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

						if token_type_ids is None:
										_A       	=      jnp.zeros_like(a__		)

						if attention_mask is None:
										_A       	=      jnp.ones_like(a__		)

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

						def _get_features(a__   :							Dict     , a__   :							Union[str, Any]     , a__   :							str     , a__   :							int     , a__   :							Optional[int]     , a__   :							Dict		):
										_A       	=      module.text_model(
										    input_ids=a__     , attention_mask=a__     , position_ids=a__     , token_type_ids=a__     , deterministic=a__     , )
										_A       	=      text_outputs[1]
										_A       	=      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 			a_			(       self   :							int     , a__   :							List[Any]     , a__   :							dict = None     , a__   :							jax.random.PRNGKey = None     , a__   :							List[Any]=False		)							->    int:





						'''simple docstring'''







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

						def _get_features(a__   :							str     , a__   :							int     , a__   :							str		):
										_A       	=      module.vision_model(pixel_values=a__     , deterministic=a__		)
										_A       	=      vision_outputs[1]  # pooled_output
										_A       	=      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 			a_			(       cls   :							Tuple     , a__   :							str = None     , a__   :							str = None     , *a__   :							List[Any]     , **a__   :							str     , )							->    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"     , 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

														_A       	=      AutoConfig.from_pretrained(a__		)
														_A       	=      text_config

										_A       	=      FlaxAutoModel.from_pretrained(a__     , *a__     , **a__		)

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

														_A       	=      AutoConfig.from_pretrained(a__		)
														_A       	=      vision_config

										_A       	=      FlaxAutoModel.from_pretrained(a__     , *a__     , **a__		)

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

						# init model
						_A       	=      cls(a__     , *a__     , dtype=a__     , **a__		)

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


def   a__			(					__lowercase  ,      __lowercase )						->						Tuple:
				_A       	=      1
				while b > 0:
								if b & 1:
												res *= a

								a *= a
								b >>= 1

				return res






def   a__			(					__lowercase  ,      __lowercase  ,      __lowercase )						->						Optional[Any]:
				_A       	=      1
				while b > 0:
								if b & 1:
												_A       	=      ((res % c) * (a % c)) % c

								a *= a
								b >>= 1

				return res