
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			:       List[Any]      =						logging.get_logger(__name__)





class      UpperCamelCase_			(			nn.Module		):







						'''simple docstring'''



						UpperCamelCase			:     HybridCLIPConfig
						UpperCamelCase			:     jnp.dtype							      =				jnp.floataa





						def 					SCREAMING_SNAKE_CASE( self       :Any    )     ->Tuple:
										lowercase						    =			self.config.text_config
										lowercase						    =			self.config.vision_config

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

										lowercase						    =			FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
										lowercase						    =			FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__		,						lowerCAmelCase__    ).module_class

										lowercase						    =			text_module(lowerCAmelCase__		,						dtype=self.dtype    )
										lowercase						    =			vision_module(lowerCAmelCase__		,						dtype=self.dtype    )

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





						def __call__( self       :Dict		,						lowerCAmelCase__       :str=None		,						lowerCAmelCase__       :List[Any]=None		,						lowerCAmelCase__       :Dict=None		,						lowerCAmelCase__       :Tuple=None		,						lowerCAmelCase__       :Optional[Any]=None		,						lowerCAmelCase__       :bool = True		,						lowerCAmelCase__       :Dict=None		,						lowerCAmelCase__       :Optional[int]=None		,						lowerCAmelCase__       :Any=None		,						)     ->Any:
										lowercase						    =			return_dict if return_dict is not None else self.config.return_dict

										lowercase						    =			self.vision_model(
										    pixel_values=lowerCAmelCase__		,						deterministic=lowerCAmelCase__		,						output_attentions=lowerCAmelCase__		,						output_hidden_states=lowerCAmelCase__		,						return_dict=lowerCAmelCase__		,						)

										lowercase						    =			self.text_model(
										    input_ids=lowerCAmelCase__		,						attention_mask=lowerCAmelCase__		,						token_type_ids=lowerCAmelCase__		,						position_ids=lowerCAmelCase__		,						deterministic=lowerCAmelCase__		,						output_attentions=lowerCAmelCase__		,						output_hidden_states=lowerCAmelCase__		,						return_dict=lowerCAmelCase__		,						)

										lowercase						    =			vision_outputs[1]
										lowercase						    =			self.visual_projection(lowerCAmelCase__    )

										lowercase						    =			text_outputs[1]
										lowercase						    =			self.text_projection(lowerCAmelCase__    )

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

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





class      UpperCamelCase_			(			__a		):







						'''simple docstring'''



						UpperCamelCase			:     List[str]							      =				HybridCLIPConfig
						UpperCamelCase			:     Optional[int]							      =				FlaxHybridCLIPModule





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

										lowercase						    =			self.module_class(config=lowerCAmelCase__		,						dtype=lowerCAmelCase__		,						**lowerCAmelCase__    )
										super().__init__(lowerCAmelCase__		,						lowerCAmelCase__		,						input_shape=lowerCAmelCase__		,						seed=lowerCAmelCase__		,						dtype=lowerCAmelCase__    )





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

										lowercase						    =			jax.random.normal(lowerCAmelCase__		,						input_shape[1]    )

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

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





						def __call__( self       :int		,						lowerCAmelCase__       :List[str]		,						lowerCAmelCase__       :List[Any]		,						lowerCAmelCase__       :Dict=None		,						lowerCAmelCase__       :Optional[Any]=None		,						lowerCAmelCase__       :Optional[Any]=None		,						lowerCAmelCase__       :dict = None		,						lowerCAmelCase__       :jax.random.PRNGKey = None		,						lowerCAmelCase__       :bool = False		,						lowerCAmelCase__       :Optional[bool] = None		,						lowerCAmelCase__       :Optional[bool] = None		,						lowerCAmelCase__       :Optional[bool] = None		,						)     ->str:
										lowercase						    =			output_attentions if output_attentions is not None else self.config.output_attentions
										lowercase						    =			(
										    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
										)
										lowercase						    =			return_dict if return_dict is not None else self.config.return_dict

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

										if token_type_ids is None:
														lowercase						    =			jnp.zeros_like(lowerCAmelCase__    )

										if attention_mask is None:
														lowercase						    =			jnp.ones_like(lowerCAmelCase__    )

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

										return self.module.apply(
										    {"params": params or self.params}		,						jnp.array(lowerCAmelCase__		,						dtype="i4"    )		,						jnp.array(lowerCAmelCase__		,						dtype=jnp.floataa    )		,						jnp.array(lowerCAmelCase__		,						dtype="i4"    )		,						jnp.array(lowerCAmelCase__		,						dtype="i4"    )		,						jnp.array(lowerCAmelCase__		,						dtype="i4"    )		,						not train		,						lowerCAmelCase__		,						lowerCAmelCase__		,						lowerCAmelCase__		,						rngs=lowerCAmelCase__		,						)





						def 					SCREAMING_SNAKE_CASE( self       :List[str]		,						lowerCAmelCase__       :Optional[Any]		,						lowerCAmelCase__       :Any=None		,						lowerCAmelCase__       :Optional[Any]=None		,						lowerCAmelCase__       :List[Any]=None		,						lowerCAmelCase__       :dict = None		,						lowerCAmelCase__       :jax.random.PRNGKey = None		,						lowerCAmelCase__       :Union[str, Any]=False		,						)     ->str:
										if position_ids is None:
														lowercase						    =			jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowerCAmelCase__    ).shape[-1]    )		,						input_ids.shape    )

										if token_type_ids is None:
														lowercase						    =			jnp.zeros_like(lowerCAmelCase__    )

										if attention_mask is None:
														lowercase						    =			jnp.ones_like(lowerCAmelCase__    )

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

										def _get_features(lowerCAmelCase__       :Dict		,						lowerCAmelCase__       :Union[str, Any]		,						lowerCAmelCase__       :int		,						lowerCAmelCase__       :Dict		,						lowerCAmelCase__       :int		,						lowerCAmelCase__       :Any    ):
														lowercase						    =			module.text_model(
														    input_ids=lowerCAmelCase__		,						attention_mask=lowerCAmelCase__		,						position_ids=lowerCAmelCase__		,						token_type_ids=lowerCAmelCase__		,						deterministic=lowerCAmelCase__		,						)
														lowercase						    =			text_outputs[1]
														lowercase						    =			module.text_projection(lowerCAmelCase__    )
														return text_features

										return self.module.apply(
										    {"params": params or self.params}		,						jnp.array(lowerCAmelCase__		,						dtype="i4"    )		,						jnp.array(lowerCAmelCase__		,						dtype="i4"    )		,						jnp.array(lowerCAmelCase__		,						dtype="i4"    )		,						jnp.array(lowerCAmelCase__		,						dtype="i4"    )		,						not train		,						method=_get_features		,						rngs=lowerCAmelCase__		,						)





						def 					SCREAMING_SNAKE_CASE( self       :int		,						lowerCAmelCase__       :Any		,						lowerCAmelCase__       :dict = None		,						lowerCAmelCase__       :jax.random.PRNGKey = None		,						lowerCAmelCase__       :Union[str, Any]=False    )     ->str:
										lowercase						    =			{}
										if dropout_rng is not None:
														lowercase						    =			dropout_rng

										def _get_features(lowerCAmelCase__       :Optional[int]		,						lowerCAmelCase__       :str		,						lowerCAmelCase__       :str    ):
														lowercase						    =			module.vision_model(pixel_values=lowerCAmelCase__		,						deterministic=lowerCAmelCase__    )
														lowercase						    =			vision_outputs[1]  # pooled_output
														lowercase						    =			module.visual_projection(lowerCAmelCase__    )
														return image_features

										return self.module.apply(
										    {"params": params or self.params}		,						jnp.array(lowerCAmelCase__		,						dtype=jnp.floataa    )		,						not train		,						method=_get_features		,						rngs=lowerCAmelCase__		,						)





						@classmethod
						def 					SCREAMING_SNAKE_CASE( cls       :Optional[int]		,						lowerCAmelCase__       :str = None		,						lowerCAmelCase__       :str = None		,						*lowerCAmelCase__       :Optional[Any]		,						**lowerCAmelCase__       :Any		,						)     ->FlaxPreTrainedModel:
										lowercase						    =			{
										    argument[len("text_"    ) :]: value for argument, value in kwargs.items() if argument.startswith("text_"    )
										}

										lowercase						    =			{
										    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
										lowercase						    =			kwargs_text.pop("model"		,						lowerCAmelCase__    )
										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

																		lowercase						    =			AutoConfig.from_pretrained(lowerCAmelCase__    )
																		lowercase						    =			text_config

														lowercase						    =			FlaxAutoModel.from_pretrained(lowerCAmelCase__		,						*lowerCAmelCase__		,						**lowerCAmelCase__    )

										lowercase						    =			kwargs_vision.pop("model"		,						lowerCAmelCase__    )
										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

																		lowercase						    =			AutoConfig.from_pretrained(lowerCAmelCase__    )
																		lowercase						    =			vision_config

														lowercase						    =			FlaxAutoModel.from_pretrained(lowerCAmelCase__		,						*lowerCAmelCase__		,						**lowerCAmelCase__    )

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

										# init model
										lowercase						    =			cls(lowerCAmelCase__		,						*lowerCAmelCase__		,						dtype=lowerCAmelCase__		,						**lowerCAmelCase__    )

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

										lowercase						    =			text_model.params

										return model


import sys
from collections.abc import Mapping

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter





class      UpperCamelCase_			(			TensorFormatter[Mapping, np.ndarray, Mapping]		):







						'''simple docstring'''



						def __init__( self       :int		,						lowerCAmelCase__       :Any=None		,						**lowerCAmelCase__       :Union[str, Any]    )     ->int:
										super().__init__(features=lowerCAmelCase__    )
										lowercase						    =			np_array_kwargs





						def 					SCREAMING_SNAKE_CASE( self       :Union[str, Any]		,						lowerCAmelCase__       :str    )     ->Optional[Any]:
										if isinstance(lowerCAmelCase__		,						lowerCAmelCase__    ):
														if column and all(
														    isinstance(lowerCAmelCase__		,						np.ndarray    ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column    ):
																		return np.stack(lowerCAmelCase__    )
														else:
																		# don't use np.array(column, dtype=object)
																		# since it fails in certain cases
																		# see https://stackoverflow.com/q/51005699
																		lowercase						    =			np.empty(len(lowerCAmelCase__    )		,						dtype=lowerCAmelCase__    )
																		lowercase						    =			column
																		return out
										return column





						def 					SCREAMING_SNAKE_CASE( self       :str		,						lowerCAmelCase__       :List[str]    )     ->Union[str, Any]:
										if isinstance(lowerCAmelCase__		,						(str, bytes, type(lowerCAmelCase__    ))    ):
														return value
										elif isinstance(lowerCAmelCase__		,						(np.character, np.ndarray)    ) and np.issubdtype(value.dtype		,						np.character    ):
														return value
										elif isinstance(lowerCAmelCase__		,						np.number    ):
														return value

										lowercase						    =			{}

										if isinstance(lowerCAmelCase__		,						np.ndarray    ) and np.issubdtype(value.dtype		,						np.integer    ):
														lowercase						    =			{"dtype": np.intaa}
										elif isinstance(lowerCAmelCase__		,						np.ndarray    ) and np.issubdtype(value.dtype		,						np.floating    ):
														lowercase						    =			{"dtype": np.floataa}
										elif config.PIL_AVAILABLE and "PIL" in sys.modules:
														import PIL.Image

														if isinstance(lowerCAmelCase__		,						PIL.Image.Image    ):
																		return np.asarray(lowerCAmelCase__		,						**self.np_array_kwargs    )

										return np.asarray(lowerCAmelCase__		,						**{**default_dtype, **self.np_array_kwargs}    )





						def 					SCREAMING_SNAKE_CASE( self       :int		,						lowerCAmelCase__       :Dict    )     ->int:
										# support for torch, tf, jax etc.
										if config.TORCH_AVAILABLE and "torch" in sys.modules:
														import torch

														if isinstance(lowerCAmelCase__		,						torch.Tensor    ):
																		return self._tensorize(data_struct.detach().cpu().numpy()[()]    )
										if hasattr(lowerCAmelCase__		,						"__array__"    ) and not isinstance(lowerCAmelCase__		,						(np.ndarray, np.character, np.number)    ):
														lowercase						    =			data_struct.__array__()
										# support for nested types like struct of list of struct
										if isinstance(lowerCAmelCase__		,						np.ndarray    ):
														if data_struct.dtype == object:
																		return self._consolidate([self.recursive_tensorize(lowerCAmelCase__    ) for substruct in data_struct]    )
										if isinstance(lowerCAmelCase__		,						(list, tuple)    ):
														return self._consolidate([self.recursive_tensorize(lowerCAmelCase__    ) for substruct in data_struct]    )
										return self._tensorize(lowerCAmelCase__    )





						def 					SCREAMING_SNAKE_CASE( self       :int		,						lowerCAmelCase__       :dict    )     ->str:
										return map_nested(self._recursive_tensorize		,						lowerCAmelCase__		,						map_list=lowerCAmelCase__    )





						def 					SCREAMING_SNAKE_CASE( self       :Tuple		,						lowerCAmelCase__       :pa.Table    )     ->Mapping:
										lowercase						    =			self.numpy_arrow_extractor().extract_row(lowerCAmelCase__    )
										lowercase						    =			self.python_features_decoder.decode_row(lowerCAmelCase__    )
										return self.recursive_tensorize(lowerCAmelCase__    )





						def 					SCREAMING_SNAKE_CASE( self       :str		,						lowerCAmelCase__       :pa.Table    )     ->np.ndarray:
										lowercase						    =			self.numpy_arrow_extractor().extract_column(lowerCAmelCase__    )
										lowercase						    =			self.python_features_decoder.decode_column(lowerCAmelCase__		,						pa_table.column_names[0]    )
										lowercase						    =			self.recursive_tensorize(lowerCAmelCase__    )
										lowercase						    =			self._consolidate(lowerCAmelCase__    )
										return column





						def 					SCREAMING_SNAKE_CASE( self       :List[Any]		,						lowerCAmelCase__       :pa.Table    )     ->Mapping:
										lowercase						    =			self.numpy_arrow_extractor().extract_batch(lowerCAmelCase__    )
										lowercase						    =			self.python_features_decoder.decode_batch(lowerCAmelCase__    )
										lowercase						    =			self.recursive_tensorize(lowerCAmelCase__    )
										for column_name in batch:
														lowercase						    =			self._consolidate(batch[column_name]    )
										return batch
