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


lowerCAmelCase__	:  Tuple						=  logging.get_logger(__name__)





class 					__snake_case    (     nn.Module      ):
					__lowerCamelCase			   =  42
					__lowerCamelCase			   =  jnp.floataa




					def      __a     (  self						)	->    Optional[int]:



									'''simple docstring'''







									snake_case__   :      int   				= self.config.text_config
									snake_case__   :      List[Any]   				= self.config.vision_config

									snake_case__   :      Any   				= self.config.projection_dim
									snake_case__   :      Union[str, Any]   				= text_config.hidden_size
									snake_case__   :      Optional[int]   				= vision_config.hidden_size

									snake_case__   :      int   				= FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
									snake_case__   :      List[str]   				= FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__       ,			__UpperCamelCase						).module_class

									snake_case__   :      List[Any]   				= text_module(__UpperCamelCase       ,			dtype=self.dtype						)
									snake_case__   :      Optional[Any]   				= vision_module(__UpperCamelCase       ,			dtype=self.dtype						)

									snake_case__   :      Dict   				= nn.Dense(
									    self.projection_dim       ,			dtype=self.dtype       ,			kernel_init=jax.nn.initializers.normal(0.0_2						)       ,			use_bias=__UpperCamelCase       ,			)
									snake_case__   :      Any   				= nn.Dense(
									    self.projection_dim       ,			dtype=self.dtype       ,			kernel_init=jax.nn.initializers.normal(0.0_2						)       ,			use_bias=__UpperCamelCase       ,			)
									snake_case__   :      Optional[int]   				= self.param('logit_scale'       ,			jax.nn.initializers.ones       ,			[]						)




					def __call__(  self       ,			__UpperCamelCase=None       ,			__UpperCamelCase=None       ,			__UpperCamelCase=None       ,			__UpperCamelCase=None       ,			__UpperCamelCase=None       ,			__UpperCamelCase = True       ,			__UpperCamelCase=None       ,			__UpperCamelCase=None       ,			__UpperCamelCase=None       ,			)	->    Optional[int]:



									'''simple docstring'''







									snake_case__   :      Tuple   				= return_dict if return_dict is not None else self.config.return_dict

									snake_case__   :      Optional[int]   				= self.vision_model(
									    pixel_values=__UpperCamelCase       ,			deterministic=__UpperCamelCase       ,			output_attentions=__UpperCamelCase       ,			output_hidden_states=__UpperCamelCase       ,			return_dict=__UpperCamelCase       ,			)

									snake_case__   :      Union[str, 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       ,			)

									snake_case__   :      Dict   				= vision_outputs[1]
									snake_case__   :      Optional[int]   				= self.visual_projection(__UpperCamelCase						)

									snake_case__   :      Union[str, Any]   				= text_outputs[1]
									snake_case__   :      Union[str, Any]   				= self.text_projection(__UpperCamelCase						)

									# normalized features
									snake_case__   :      Any   				= image_embeds / jnp.linalg.norm(__UpperCamelCase       ,			axis=-1       ,			keepdims=__UpperCamelCase						)
									snake_case__   :      Dict   				= text_embeds / jnp.linalg.norm(__UpperCamelCase       ,			axis=-1       ,			keepdims=__UpperCamelCase						)

									# cosine similarity as logits
									snake_case__   :      List[str]   				= jnp.exp(self.logit_scale						)
									snake_case__   :      Union[str, Any]   				= jnp.matmul(__UpperCamelCase       ,			image_embeds.T						) * logit_scale
									snake_case__   :      Optional[int]   				= 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 					__snake_case    (     _lowerCamelCase      ):
					__lowerCamelCase			   =  HybridCLIPConfig
					__lowerCamelCase			   =  FlaxHybridCLIPModule




					def __init__(  self       ,			__UpperCamelCase       ,			__UpperCamelCase = None       ,			__UpperCamelCase = 0       ,			__UpperCamelCase = jnp.floataa       ,			**__UpperCamelCase       ,			)	->    Dict:



									'''simple docstring'''







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

									snake_case__   :      List[Any]   				= self.module_class(config=__UpperCamelCase       ,			dtype=__UpperCamelCase       ,			**__UpperCamelCase						)
									super().__init__(__UpperCamelCase       ,			__UpperCamelCase       ,			input_shape=__UpperCamelCase       ,			seed=__UpperCamelCase       ,			dtype=__UpperCamelCase						)




					def      __a     (  self       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase = None						)	->    FrozenDict:



									'''simple docstring'''







									snake_case__   :      List[Any]   				= jnp.zeros(input_shape[0]       ,			dtype='i4'						)
									snake_case__   :      Optional[int]   				= jnp.broadcast_to(jnp.arange(jnp.atleast_ad(__UpperCamelCase						).shape[-1]						)       ,			input_shape[0]						)
									snake_case__   :      int   				= jnp.ones_like(__UpperCamelCase						)
									snake_case__   :      int   				= jnp.ones_like(__UpperCamelCase						)

									snake_case__   :      List[Any]   				= jax.random.normal(__UpperCamelCase       ,			input_shape[1]						)

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

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




					def __call__(  self       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase=None       ,			__UpperCamelCase=None       ,			__UpperCamelCase=None       ,			__UpperCamelCase = None       ,			__UpperCamelCase = None       ,			__UpperCamelCase = False       ,			__UpperCamelCase = None       ,			__UpperCamelCase = None       ,			__UpperCamelCase = None       ,			)	->    List[Any]:



									'''simple docstring'''







									snake_case__   :      Any   				= output_attentions if output_attentions is not None else self.config.output_attentions
									snake_case__   :      List[Any]   				= (
									    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
									)
									snake_case__   :      Any   				= return_dict if return_dict is not None else self.config.return_dict

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

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

									if attention_mask is None:
													snake_case__   :      Union[str, Any]   				= jnp.ones_like(__UpperCamelCase						)

									# Handle any PRNG if needed
									snake_case__   :      List[str]   				= {}
									if dropout_rng is not None:
													snake_case__   :      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       ,			__UpperCamelCase       ,			__UpperCamelCase=None       ,			__UpperCamelCase=None       ,			__UpperCamelCase=None       ,			__UpperCamelCase = None       ,			__UpperCamelCase = None       ,			__UpperCamelCase=False       ,			)	->    Optional[Any]:



									'''simple docstring'''







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

									if token_type_ids is None:
													snake_case__   :      List[str]   				= jnp.zeros_like(__UpperCamelCase						)

									if attention_mask is None:
													snake_case__   :      Optional[Any]   				= jnp.ones_like(__UpperCamelCase						)

									# Handle any PRNG if needed
									snake_case__   :      str   				= {}
									if dropout_rng is not None:
													snake_case__   :      Tuple   				= dropout_rng

									def _get_features(__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase						):
													snake_case__   :      str   				= module.text_model(
													    input_ids=__UpperCamelCase       ,			attention_mask=__UpperCamelCase       ,			position_ids=__UpperCamelCase       ,			token_type_ids=__UpperCamelCase       ,			deterministic=__UpperCamelCase       ,			)
													snake_case__   :      Dict   				= text_outputs[1]
													snake_case__   :      Any   				= 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       ,			__UpperCamelCase       ,			__UpperCamelCase = None       ,			__UpperCamelCase = None       ,			__UpperCamelCase=False						)	->    Dict:



									'''simple docstring'''







									snake_case__   :      Tuple   				= {}
									if dropout_rng is not None:
													snake_case__   :      int   				= dropout_rng

									def _get_features(__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase						):
													snake_case__   :      Any   				= module.vision_model(pixel_values=__UpperCamelCase       ,			deterministic=__UpperCamelCase						)
													snake_case__   :      Tuple   				= vision_outputs[1]  # pooled_output
													snake_case__   :      Optional[Any]   				= 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       ,			__UpperCamelCase = None       ,			__UpperCamelCase = None       ,			*__UpperCamelCase       ,			**__UpperCamelCase       ,			)	->    FlaxPreTrainedModel:



									'''simple docstring'''







									snake_case__   :      Dict   				= {
									    argument[len('text_'						) :]: value for argument, value in kwargs.items() if argument.startswith('text_'						)
									}

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

																	snake_case__   :      Optional[Any]   				= AutoConfig.from_pretrained(__UpperCamelCase						)
																	snake_case__   :      List[str]   				= text_config

													snake_case__   :      Optional[int]   				= FlaxAutoModel.from_pretrained(__UpperCamelCase       ,			*__UpperCamelCase       ,			**__UpperCamelCase						)

									snake_case__   :      str   				= 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

																	snake_case__   :      Optional[int]   				= AutoConfig.from_pretrained(__UpperCamelCase						)
																	snake_case__   :      str   				= vision_config

													snake_case__   :      List[str]   				= FlaxAutoModel.from_pretrained(__UpperCamelCase       ,			*__UpperCamelCase       ,			**__UpperCamelCase						)

									# instantiate config with corresponding kwargs
									snake_case__   :      int   				= kwargs.pop('dtype'       ,			jnp.floataa						)
									snake_case__   :      List[str]   				= HybridCLIPConfig.from_text_vision_configs(text_model.config       ,			vision_model.config       ,			**__UpperCamelCase						)

									# init model
									snake_case__   :      List[Any]   				= cls(__UpperCamelCase       ,			*__UpperCamelCase       ,			dtype=__UpperCamelCase       ,			**__UpperCamelCase						)

									if vision_config.model_type == "clip":
													snake_case__   :      Any   				= vision_model.params['vision_model']
													snake_case__   :      List[Any]   				= vision_model.params['visual_projection']['kernel']
									else:
													snake_case__   :      List[str]   				= vision_model.params

									snake_case__   :      Dict   				= 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()


