


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


__UpperCamelCase  						=    logging.get_logger(__name__)







class     lowerCamelCase__		(							nn.Module ):






			"""simple docstring"""






			_UpperCamelCase						:   Optional[Any]					=						42
			_UpperCamelCase						:   Any					=						jnp.floataa




			def 			snake_case__			(							self       ):

						'''simple docstring'''


						UpperCamelCase__   =    self.config.text_config
						UpperCamelCase__   =    self.config.vision_config

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

						UpperCamelCase__   =    FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
						UpperCamelCase__   =    FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__ ,  snake_case       ).module_class

						UpperCamelCase__   =    text_module(snake_case ,  dtype=self.dtype       )
						UpperCamelCase__   =    vision_module(snake_case ,  dtype=self.dtype       )

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







			def __call__(							self ,  snake_case=None ,  snake_case=None ,  snake_case=None ,  snake_case=None ,  snake_case=None ,  snake_case = True ,  snake_case=None ,  snake_case=None ,  snake_case=None ,  ):

						'''simple docstring'''


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

						UpperCamelCase__   =    self.vision_model(
						    pixel_values=snake_case ,  deterministic=snake_case ,  output_attentions=snake_case ,  output_hidden_states=snake_case ,  return_dict=snake_case ,  )

						UpperCamelCase__   =    self.text_model(
						    input_ids=snake_case ,  attention_mask=snake_case ,  token_type_ids=snake_case ,  position_ids=snake_case ,  deterministic=snake_case ,  output_attentions=snake_case ,  output_hidden_states=snake_case ,  return_dict=snake_case ,  )

						UpperCamelCase__   =    vision_outputs[1]
						UpperCamelCase__   =    self.visual_projection(snake_case       )

						UpperCamelCase__   =    text_outputs[1]
						UpperCamelCase__   =    self.text_projection(snake_case       )

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

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







class     lowerCamelCase__		(							__A ):






			"""simple docstring"""






			_UpperCamelCase						:   Any					=						HybridCLIPConfig
			_UpperCamelCase						:   Dict					=						FlaxHybridCLIPModule




			def __init__(							self ,  snake_case ,  snake_case = None ,  snake_case = 0 ,  snake_case = jnp.floataa ,  **snake_case ,  ):

						'''simple docstring'''


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

						UpperCamelCase__   =    self.module_class(config=snake_case ,  dtype=snake_case ,  **snake_case       )
						super().__init__(snake_case ,  snake_case ,  input_shape=snake_case ,  seed=snake_case ,  dtype=snake_case       )




			def 			snake_case__			(							self ,  snake_case ,  snake_case ,  snake_case = None       ):

						'''simple docstring'''


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

						UpperCamelCase__   =    jax.random.normal(snake_case ,  input_shape[1]       )

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

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




			def __call__(							self ,  snake_case ,  snake_case ,  snake_case=None ,  snake_case=None ,  snake_case=None ,  snake_case = None ,  snake_case = None ,  snake_case = False ,  snake_case = None ,  snake_case = None ,  snake_case = None ,  ):

						'''simple docstring'''


						UpperCamelCase__   =    output_attentions if output_attentions is not None else self.config.output_attentions
						UpperCamelCase__   =    (
						    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
						)
						UpperCamelCase__   =    return_dict if return_dict is not None else self.config.return_dict

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

						if token_type_ids is None:
									UpperCamelCase__   =    jnp.zeros_like(snake_case       )

						if attention_mask is None:
									UpperCamelCase__   =    jnp.ones_like(snake_case       )

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

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




			def 			snake_case__			(							self ,  snake_case ,  snake_case=None ,  snake_case=None ,  snake_case=None ,  snake_case = None ,  snake_case = None ,  snake_case=False ,  ):

						'''simple docstring'''


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

						if token_type_ids is None:
									UpperCamelCase__   =    jnp.zeros_like(snake_case       )

						if attention_mask is None:
									UpperCamelCase__   =    jnp.ones_like(snake_case       )

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

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

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




			def 			snake_case__			(							self ,  snake_case ,  snake_case = None ,  snake_case = None ,  snake_case=False       ):

						'''simple docstring'''


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

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

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







			@classmethod
			def 			snake_case__			(							cls ,  snake_case = None ,  snake_case = None ,  *snake_case ,  **snake_case ,  ):

						'''simple docstring'''


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

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

												UpperCamelCase__   =    AutoConfig.from_pretrained(snake_case       )
												UpperCamelCase__   =    text_config

									UpperCamelCase__   =    FlaxAutoModel.from_pretrained(snake_case ,  *snake_case ,  **snake_case       )

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

												UpperCamelCase__   =    AutoConfig.from_pretrained(snake_case       )
												UpperCamelCase__   =    vision_config

									UpperCamelCase__   =    FlaxAutoModel.from_pretrained(snake_case ,  *snake_case ,  **snake_case       )

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

						# init model
						UpperCamelCase__   =    cls(snake_case ,  *snake_case ,  dtype=snake_case ,  **snake_case       )

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

						UpperCamelCase__   =    text_model.params

						return model






import numpy as np
def     UpperCamelCase_(			_A				:Optional[int]			,       _A				:Dict="utf-8"			,       _A				:Optional[Any]="surrogatepass"					)->						int:
			UpperCamelCase__   =    bin(int.from_bytes(text.encode(_A			,       _A					)			,       "big"					)					)[2:]
			return bits.zfill(8 * ((len(_A					) + 7) // 8)					)
def     UpperCamelCase_(			_A				:Optional[int]			,       _A				:int="utf-8"			,       _A				:List[str]="surrogatepass"					)->						Optional[Any]:
			UpperCamelCase__   =    int(_A			,       2					)
			return n.to_bytes((n.bit_length() + 7) // 8			,       "big"					).decode(_A			,       _A					) or "\0"
def     UpperCamelCase_(			_A				:Union[str, Any]			,       _A				:Tuple					)->						Optional[int]:
			if size_par + len(_A					) <= 2**size_par - (len(_A					) - 1):
						raise ValueError("size of parity don't match with size of data"					)

			UpperCamelCase__   =    []
			UpperCamelCase__   =    []
			UpperCamelCase__   =    [bin(_A					)[2:] for x in range(1			,       size_par + len(_A					) + 1					)]

			# sorted information data for the size of the output data
			UpperCamelCase__   =    []
			# data position template + parity
			UpperCamelCase__   =    []
			# parity bit counter
			UpperCamelCase__   =    0
			# counter position of data bits
			UpperCamelCase__   =    0

			for x in range(1			,       size_par + len(_A					) + 1					):
						# Performs a template of bit positions - who should be given,
						# and who should be parity
						if qtd_bp < size_par:
									if (np.log(_A					) / np.log(2					)).is_integer():
												data_out_gab.append("P"					)
												UpperCamelCase__   =    qtd_bp + 1
									else:
												data_out_gab.append("D"					)
						else:
									data_out_gab.append("D"					)

						# Sorts the data to the new output size
						if data_out_gab[-1] == "D":
									data_ord.append(data[cont_data]					)
									cont_data += 1
						else:
									data_ord.append(_A					)

    # Calculates parity
			UpperCamelCase__   =    0  # parity bit counter
			for bp in range(1			,       size_par + 1					):
						# Bit counter one for a given parity
						UpperCamelCase__   =    0
						# counter to control the loop reading
						UpperCamelCase__   =    0
						for x in data_ord:
									if x is not None:
												try:
															UpperCamelCase__   =    (bin_pos[cont_loop])[-1 * (bp)]
												except IndexError:
															UpperCamelCase__   =    "0"
												if aux == "1" and x == "1":
															cont_bo += 1
									cont_loop += 1
						parity.append(cont_bo % 2					)

						qtd_bp += 1

			# Mount the message
			UpperCamelCase__   =    0  # parity bit counter
			for x in range(0			,       size_par + len(_A					)					):
						if data_ord[x] is None:
									data_out.append(str(parity[cont_bp]					)					)
									cont_bp += 1
						else:
									data_out.append(data_ord[x]					)

			return data_out
def     UpperCamelCase_(			_A				:int			,       _A				:str					)->						Dict:
			UpperCamelCase__   =    []
			# Parity bit counter
			UpperCamelCase__   =    0
			# Counter p data bit reading
			UpperCamelCase__   =    0
			# list of parity received
			UpperCamelCase__   =    []
			UpperCamelCase__   =    []

			for x in range(1			,       len(_A					) + 1					):
						# Performs a template of bit positions - who should be given,
						#  and who should be parity
						if qtd_bp < size_par and (np.log(_A					) / np.log(2					)).is_integer():
									data_out_gab.append("P"					)
									UpperCamelCase__   =    qtd_bp + 1
						else:
									data_out_gab.append("D"					)

						# Sorts the data to the new output size
						if data_out_gab[-1] == "D":
									data_output.append(data[cont_data]					)
						else:
									parity_received.append(data[cont_data]					)
						cont_data += 1

			# -----------calculates the parity with the data
			UpperCamelCase__   =    []
			UpperCamelCase__   =    []
			UpperCamelCase__   =    [bin(_A					)[2:] for x in range(1			,       size_par + len(_A					) + 1					)]

			#  sorted information data for the size of the output data
			UpperCamelCase__   =    []
			# Data position feedback + parity
			UpperCamelCase__   =    []
			# Parity bit counter
			UpperCamelCase__   =    0
			# Counter p data bit reading
			UpperCamelCase__   =    0

			for x in range(1			,       size_par + len(_A					) + 1					):
						# Performs a template position of bits - who should be given,
						# and who should be parity
						if qtd_bp < size_par and (np.log(_A					) / np.log(2					)).is_integer():
									data_out_gab.append("P"					)
									UpperCamelCase__   =    qtd_bp + 1
						else:
									data_out_gab.append("D"					)

						# Sorts the data to the new output size
						if data_out_gab[-1] == "D":
									data_ord.append(data_output[cont_data]					)
									cont_data += 1
						else:
									data_ord.append(_A					)

    # Calculates parity
			UpperCamelCase__   =    0  # parity bit counter
			for bp in range(1			,       size_par + 1					):
						# Bit counter one for a certain parity
						UpperCamelCase__   =    0
						# Counter to control loop reading
						UpperCamelCase__   =    0
						for x in data_ord:
									if x is not None:
												try:
															UpperCamelCase__   =    (bin_pos[cont_loop])[-1 * (bp)]
												except IndexError:
															UpperCamelCase__   =    "0"
												if aux == "1" and x == "1":
															cont_bo += 1
									cont_loop += 1
						parity.append(str(cont_bo % 2					)					)

						qtd_bp += 1

			# Mount the message
			UpperCamelCase__   =    0  # Parity bit counter
			for x in range(0			,       size_par + len(_A					)					):
						if data_ord[x] is None:
									data_out.append(str(parity[cont_bp]					)					)
									cont_bp += 1
						else:
									data_out.append(data_ord[x]					)

			UpperCamelCase__   =    parity_received == parity
			return data_output, ack


