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


snake_case    =		logging.get_logger(__name__)




class      UpperCamelCase	(       nn.Module      ):

				"""simple docstring"""



				UpperCAmelCase_				:      HybridCLIPConfig
				UpperCAmelCase_				:      jnp.dtype						  =				jnp.floataa


				def 							A					(				self			)    ->    str:
									"""simple docstring"""







									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__		,	lowercase__			).module_class

									SCREAMING_SNAKE_CASE				=	text_module(lowercase__		,	dtype=self.dtype			)
									SCREAMING_SNAKE_CASE				=	vision_module(lowercase__		,	dtype=self.dtype			)

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




				def __call__(				self		,	lowercase__=None		,	lowercase__=None		,	lowercase__=None		,	lowercase__=None		,	lowercase__=None		,	lowercase__ = True		,	lowercase__=None		,	lowercase__=None		,	lowercase__=None		,	)    ->    int:
									"""simple docstring"""







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

									SCREAMING_SNAKE_CASE				=	self.vision_model(
									    pixel_values=lowercase__		,	deterministic=lowercase__		,	output_attentions=lowercase__		,	output_hidden_states=lowercase__		,	return_dict=lowercase__		,	)

									SCREAMING_SNAKE_CASE				=	self.text_model(
									    input_ids=lowercase__		,	attention_mask=lowercase__		,	token_type_ids=lowercase__		,	position_ids=lowercase__		,	deterministic=lowercase__		,	output_attentions=lowercase__		,	output_hidden_states=lowercase__		,	return_dict=lowercase__		,	)

									SCREAMING_SNAKE_CASE				=	vision_outputs[1]
									SCREAMING_SNAKE_CASE				=	self.visual_projection(lowercase__			)

									SCREAMING_SNAKE_CASE				=	text_outputs[1]
									SCREAMING_SNAKE_CASE				=	self.text_projection(lowercase__			)

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

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






class      UpperCamelCase	(       __magic_name__      ):

				"""simple docstring"""



				UpperCAmelCase_				:      List[Any]						  =				HybridCLIPConfig
				UpperCAmelCase_				:      Any						  =				FlaxHybridCLIPModule


				def __init__(				self		,	lowercase__		,	lowercase__ = None		,	lowercase__ = 0		,	lowercase__ = jnp.floataa		,	**lowercase__		,	)    ->    List[str]:
									"""simple docstring"""







									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=lowercase__		,	dtype=lowercase__		,	**lowercase__			)
									super().__init__(lowercase__		,	lowercase__		,	input_shape=lowercase__		,	seed=lowercase__		,	dtype=lowercase__			)


				def 							A					(				self		,	lowercase__		,	lowercase__		,	lowercase__ = None			)    ->    FrozenDict:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE				=	jnp.zeros(input_shape[0]		,	dtype='i4'			)
									SCREAMING_SNAKE_CASE				=	jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowercase__			).shape[-1]			)		,	input_shape[0]			)
									SCREAMING_SNAKE_CASE				=	jnp.ones_like(lowercase__			)
									SCREAMING_SNAKE_CASE				=	jnp.ones_like(lowercase__			)

									SCREAMING_SNAKE_CASE				=	jax.random.normal(lowercase__		,	input_shape[1]			)

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

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


				def __call__(				self		,	lowercase__		,	lowercase__		,	lowercase__=None		,	lowercase__=None		,	lowercase__=None		,	lowercase__ = None		,	lowercase__ = None		,	lowercase__ = False		,	lowercase__ = None		,	lowercase__ = None		,	lowercase__ = None		,	)    ->    Dict:
									"""simple docstring"""







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

									if token_type_ids is None:
														SCREAMING_SNAKE_CASE				=	jnp.zeros_like(lowercase__			)

									if attention_mask is None:
														SCREAMING_SNAKE_CASE				=	jnp.ones_like(lowercase__			)

									# 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(lowercase__		,	dtype='i4'			)		,	jnp.array(lowercase__		,	dtype=jnp.floataa			)		,	jnp.array(lowercase__		,	dtype='i4'			)		,	jnp.array(lowercase__		,	dtype='i4'			)		,	jnp.array(lowercase__		,	dtype='i4'			)		,	not train		,	lowercase__		,	lowercase__		,	lowercase__		,	rngs=lowercase__		,	)


				def 							A					(				self		,	lowercase__		,	lowercase__=None		,	lowercase__=None		,	lowercase__=None		,	lowercase__ = None		,	lowercase__ = None		,	lowercase__=False		,	)    ->    Tuple:
									"""simple docstring"""







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

									if token_type_ids is None:
														SCREAMING_SNAKE_CASE				=	jnp.zeros_like(lowercase__			)

									if attention_mask is None:
														SCREAMING_SNAKE_CASE				=	jnp.ones_like(lowercase__			)

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

									def _get_features(lowercase__		,	lowercase__		,	lowercase__		,	lowercase__		,	lowercase__		,	lowercase__			):
														SCREAMING_SNAKE_CASE				=	module.text_model(
														    input_ids=lowercase__		,	attention_mask=lowercase__		,	position_ids=lowercase__		,	token_type_ids=lowercase__		,	deterministic=lowercase__		,	)
														SCREAMING_SNAKE_CASE				=	text_outputs[1]
														SCREAMING_SNAKE_CASE				=	module.text_projection(lowercase__			)
														return text_features

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


				def 							A					(				self		,	lowercase__		,	lowercase__ = None		,	lowercase__ = None		,	lowercase__=False			)    ->    Optional[int]:
									"""simple docstring"""







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

									def _get_features(lowercase__		,	lowercase__		,	lowercase__			):
														SCREAMING_SNAKE_CASE				=	module.vision_model(pixel_values=lowercase__		,	deterministic=lowercase__			)
														SCREAMING_SNAKE_CASE				=	vision_outputs[1]  # pooled_output
														SCREAMING_SNAKE_CASE				=	module.visual_projection(lowercase__			)
														return image_features

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




				@classmethod
				def 							A					(				cls		,	lowercase__ = None		,	lowercase__ = None		,	*lowercase__		,	**lowercase__		,	)    ->    FlaxPreTrainedModel:
									"""simple docstring"""







									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'		,	lowercase__			)
									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(lowercase__			)
																			SCREAMING_SNAKE_CASE				=	text_config

														SCREAMING_SNAKE_CASE				=	FlaxAutoModel.from_pretrained(lowercase__		,	*lowercase__		,	**lowercase__			)

									SCREAMING_SNAKE_CASE				=	kwargs_vision.pop('model'		,	lowercase__			)
									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(lowercase__			)
																			SCREAMING_SNAKE_CASE				=	vision_config

														SCREAMING_SNAKE_CASE				=	FlaxAutoModel.from_pretrained(lowercase__		,	*lowercase__		,	**lowercase__			)

									# 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		,	**lowercase__			)

									# init model
									SCREAMING_SNAKE_CASE				=	cls(lowercase__		,	*lowercase__		,	dtype=lowercase__		,	**lowercase__			)

									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


"""simple docstring"""




# Lint as: python3

import hashlib
from typing import Union

def   UpperCamelCase_  (		SCREAMING_SNAKE_CASE_			):
					if isinstance(SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_			):
										# Data already in bytes, returns as it as
										return hash_data
					elif isinstance(SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_			):
										# We keep the data as it as for it ot be later encoded to UTF-8
										# However replace `\\` with `/` for Windows compatibility
										SCREAMING_SNAKE_CASE				=	hash_data.replace('\\',     '/'			)
					elif isinstance(SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_			):
										SCREAMING_SNAKE_CASE				=	str(SCREAMING_SNAKE_CASE_			)
					else:
										# If data is not of the required type, raise error
										raise InvalidKeyError(SCREAMING_SNAKE_CASE_			)

					return hash_data.encode('utf-8'			)




class      UpperCamelCase	(       __magic_name__      ):

				"""simple docstring"""



				def __init__(				self		,	lowercase__			)    ->    Dict:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE				=	'\nFAILURE TO GENERATE DATASET: Invalid key type detected'
									SCREAMING_SNAKE_CASE				=	f'''\nFound Key {hash_data} of type {type(lowercase__			)}'''
									SCREAMING_SNAKE_CASE				=	'\nKeys should be either str, int or bytes type'
									super().__init__(f'''{self.prefix}{self.err_msg}{self.suffix}'''			)






class      UpperCamelCase	(       __magic_name__      ):

				"""simple docstring"""



				def __init__(				self		,	lowercase__		,	lowercase__		,	lowercase__=""			)    ->    Tuple:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE				=	key
									SCREAMING_SNAKE_CASE				=	duplicate_key_indices
									SCREAMING_SNAKE_CASE				=	fix_msg
									SCREAMING_SNAKE_CASE				=	'Found multiple examples generated with the same key'
									if len(lowercase__			) <= 20:
														SCREAMING_SNAKE_CASE				=	f'''\nThe examples at index {', '.join(lowercase__			)} have the key {key}'''
									else:
														SCREAMING_SNAKE_CASE				=	f'''\nThe examples at index {', '.join(duplicate_key_indices[:20]			)}... ({len(lowercase__			) - 20} more) have the key {key}'''
									SCREAMING_SNAKE_CASE				=	'\n' + fix_msg if fix_msg else ''
									super().__init__(f'''{self.prefix}{self.err_msg}{self.suffix}'''			)






class      UpperCamelCase	:

				"""simple docstring"""



				def __init__(				self		,	lowercase__			)    ->    Union[str, Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE				=	hashlib.mda(_as_bytes(lowercase__			)			)




				def 							A					(				self		,	lowercase__			)    ->    int:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE				=	self._split_mda.copy()
									SCREAMING_SNAKE_CASE				=	_as_bytes(lowercase__			)
									mda.update(lowercase__			)
									# Convert to integer with hexadecimal conversion
									return int(mda.hexdigest()		,	16			)
