

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  UpperCamelCase_      (	nn.Module      ):


  """simple docstring"""
  SCREAMING_SNAKE_CASE__					:			int					    =   42
  SCREAMING_SNAKE_CASE__					:			Union[str, Any]					    =   jnp.floataa



  def   _UpperCamelCase     (						self     :					Optional[int]						)       ->			Optional[int]:
      SCREAMING_SNAKE_CASE__         =    self.config.text_config
      SCREAMING_SNAKE_CASE__         =    self.config.vision_config

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

      SCREAMING_SNAKE_CASE__         =    FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
      SCREAMING_SNAKE_CASE__         =    FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__  ,	__UpperCamelCase						).module_class

      SCREAMING_SNAKE_CASE__         =    text_module(__UpperCamelCase  ,	dtype=self.dtype						)
      SCREAMING_SNAKE_CASE__         =    vision_module(__UpperCamelCase  ,	dtype=self.dtype						)

      SCREAMING_SNAKE_CASE__         =    nn.Dense(
          self.projection_dim  ,	dtype=self.dtype  ,	kernel_init=jax.nn.initializers.normal(0.02						)  ,	use_bias=__UpperCamelCase  ,	)
      SCREAMING_SNAKE_CASE__         =    nn.Dense(
          self.projection_dim  ,	dtype=self.dtype  ,	kernel_init=jax.nn.initializers.normal(0.02						)  ,	use_bias=__UpperCamelCase  ,	)
      SCREAMING_SNAKE_CASE__         =    self.param("logit_scale"  ,	jax.nn.initializers.ones  ,	[]						)







  def __call__(						self     :					Optional[Any]  ,	A_     :					int=None  ,	A_     :					Optional[Any]=None  ,	A_     :					Union[str, Any]=None  ,	A_     :					Any=None  ,	A_     :					Optional[Any]=None  ,	A_     :					Optional[int] = True  ,	A_     :					Tuple=None  ,	A_     :					Optional[Any]=None  ,	A_     :					int=None  ,	)       ->			Optional[int]:
      SCREAMING_SNAKE_CASE__         =    return_dict if return_dict is not None else self.config.return_dict

      SCREAMING_SNAKE_CASE__         =    self.vision_model(
          pixel_values=__UpperCamelCase  ,	deterministic=__UpperCamelCase  ,	output_attentions=__UpperCamelCase  ,	output_hidden_states=__UpperCamelCase  ,	return_dict=__UpperCamelCase  ,	)

      SCREAMING_SNAKE_CASE__         =    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__         =    vision_outputs[1]
      SCREAMING_SNAKE_CASE__         =    self.visual_projection(__UpperCamelCase						)

      SCREAMING_SNAKE_CASE__         =    text_outputs[1]
      SCREAMING_SNAKE_CASE__         =    self.text_projection(__UpperCamelCase						)

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

      # cosine similarity as logits
      SCREAMING_SNAKE_CASE__         =    jnp.exp(self.logit_scale						)
      SCREAMING_SNAKE_CASE__         =    jnp.matmul(__UpperCamelCase  ,	image_embeds.T						) * logit_scale
      SCREAMING_SNAKE_CASE__         =    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  UpperCamelCase_      (	_lowerCamelCase      ):


  """simple docstring"""
  SCREAMING_SNAKE_CASE__					:			int					    =   HybridCLIPConfig
  SCREAMING_SNAKE_CASE__					:			Tuple					    =   FlaxHybridCLIPModule



  def __init__(						self     :					int  ,	A_     :					str  ,	A_     :					str = None  ,	A_     :					Any = 0  ,	A_     :					Optional[Any] = jnp.floataa  ,	**A_     :					str  ,	)       ->			Dict:







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

      SCREAMING_SNAKE_CASE__         =    self.module_class(config=__UpperCamelCase  ,	dtype=__UpperCamelCase  ,	**__UpperCamelCase						)
      super().__init__(__UpperCamelCase  ,	__UpperCamelCase  ,	input_shape=__UpperCamelCase  ,	seed=__UpperCamelCase  ,	dtype=__UpperCamelCase						)



  def   _UpperCamelCase     (						self     :					str  ,	A_     :					Optional[int]  ,	A_     :					int  ,	A_     :					Union[str, Any] = None						)       ->			FrozenDict:
      SCREAMING_SNAKE_CASE__         =    jnp.zeros(input_shape[0]  ,	dtype="i4"						)
      SCREAMING_SNAKE_CASE__         =    jnp.broadcast_to(jnp.arange(jnp.atleast_ad(__UpperCamelCase						).shape[-1]						)  ,	input_shape[0]						)
      SCREAMING_SNAKE_CASE__         =    jnp.ones_like(__UpperCamelCase						)
      SCREAMING_SNAKE_CASE__         =    jnp.ones_like(__UpperCamelCase						)

      SCREAMING_SNAKE_CASE__         =    jax.random.normal(__UpperCamelCase  ,	input_shape[1]						)

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

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



  def __call__(						self     :					int  ,	A_     :					Dict  ,	A_     :					Union[str, Any]  ,	A_     :					Union[str, Any]=None  ,	A_     :					Tuple=None  ,	A_     :					Any=None  ,	A_     :					List[Any] = None  ,	A_     :					Optional[int] = None  ,	A_     :					List[str] = False  ,	A_     :					Union[str, Any] = None  ,	A_     :					str = None  ,	A_     :					List[Any] = None  ,	)       ->			List[Any]:
      SCREAMING_SNAKE_CASE__         =    output_attentions if output_attentions is not None else self.config.output_attentions
      SCREAMING_SNAKE_CASE__         =    (
          output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
      )
      SCREAMING_SNAKE_CASE__         =    return_dict if return_dict is not None else self.config.return_dict

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

      if token_type_ids is None:
          SCREAMING_SNAKE_CASE__         =    jnp.zeros_like(__UpperCamelCase						)

      if attention_mask is None:
          SCREAMING_SNAKE_CASE__         =    jnp.ones_like(__UpperCamelCase						)

      # Handle any PRNG if needed
      SCREAMING_SNAKE_CASE__         =    {}
      if dropout_rng is not None:
          SCREAMING_SNAKE_CASE__         =    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   _UpperCamelCase     (						self     :					List[Any]  ,	A_     :					str  ,	A_     :					List[str]=None  ,	A_     :					Optional[Any]=None  ,	A_     :					Optional[Any]=None  ,	A_     :					List[str] = None  ,	A_     :					int = None  ,	A_     :					List[str]=False  ,	)       ->			Optional[Any]:







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

      if token_type_ids is None:
          SCREAMING_SNAKE_CASE__         =    jnp.zeros_like(__UpperCamelCase						)

      if attention_mask is None:
          SCREAMING_SNAKE_CASE__         =    jnp.ones_like(__UpperCamelCase						)

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

      def _get_features(A_     :					List[Any]  ,	A_     :					Any  ,	A_     :					Optional[int]  ,	A_     :					Optional[Any]  ,	A_     :					Dict  ,	A_     :					List[str]						):
          SCREAMING_SNAKE_CASE__         =    module.text_model(
              input_ids=__UpperCamelCase  ,	attention_mask=__UpperCamelCase  ,	position_ids=__UpperCamelCase  ,	token_type_ids=__UpperCamelCase  ,	deterministic=__UpperCamelCase  ,	)
          SCREAMING_SNAKE_CASE__         =    text_outputs[1]
          SCREAMING_SNAKE_CASE__         =    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   _UpperCamelCase     (						self     :					Optional[int]  ,	A_     :					int  ,	A_     :					str = None  ,	A_     :					Optional[int] = None  ,	A_     :					List[Any]=False						)       ->			Dict:
      SCREAMING_SNAKE_CASE__         =    {}
      if dropout_rng is not None:
          SCREAMING_SNAKE_CASE__         =    dropout_rng

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

      SCREAMING_SNAKE_CASE__         =    {
          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__         =    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__         =    AutoConfig.from_pretrained(__UpperCamelCase						)
              SCREAMING_SNAKE_CASE__         =    text_config

          SCREAMING_SNAKE_CASE__         =    FlaxAutoModel.from_pretrained(__UpperCamelCase  ,	*__UpperCamelCase  ,	**__UpperCamelCase						)

      SCREAMING_SNAKE_CASE__         =    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__         =    AutoConfig.from_pretrained(__UpperCamelCase						)
              SCREAMING_SNAKE_CASE__         =    vision_config

          SCREAMING_SNAKE_CASE__         =    FlaxAutoModel.from_pretrained(__UpperCamelCase  ,	*__UpperCamelCase  ,	**__UpperCamelCase						)

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

      # init model
      SCREAMING_SNAKE_CASE__         =    cls(__UpperCamelCase  ,	*__UpperCamelCase  ,	dtype=__UpperCamelCase  ,	**__UpperCamelCase						)

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

      SCREAMING_SNAKE_CASE__         =    text_model.params

      return model



def 					UpperCamelCase__		(							A__     ,					A__       )  ->   Any:
				if len(str(A__       )       ) == 1 or len(str(A__       )       ) == 1:
								return a * b

				snake_case__   :      Union[str, Any]   				= max(len(str(A__       )       )     ,					len(str(A__       )       )       )
				snake_case__   :      str   				= ma // 2

				snake_case__				,    snake_case__   :      Tuple   				= divmod(A__     ,					10**ma       )
				snake_case__				,    snake_case__   :      str   				= divmod(A__     ,					10**ma       )

				snake_case__   :      Optional[int]   				= karatsuba(A__     ,					A__       )
				snake_case__   :      List[Any]   				= karatsuba((aa + aa)     ,					(ba + ba)       )
				snake_case__   :      Tuple   				= karatsuba(A__     ,					A__       )

				return (z * 10 ** (2 * ma)) + ((y - z - x) * 10 ** (ma)) + (x)
def 					UpperCamelCase__		(							)  ->   Optional[int]:
				print(karatsuba(1_5463     ,					2_3489       )       )


if __name__ == "__main__":
				main()


