






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


_UpperCamelCase     			=  logging.get_logger(__name__)







class 					lowerCamelCase__					(       nn.Module ):
    SCREAMING_SNAKE_CASE		=			42
    SCREAMING_SNAKE_CASE		=			jnp.floataa






    def 				_UpperCamelCase							( self					):
          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__	,lowercase__					).module_class

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

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






    def __call__( self	,A=None	,A=None	,A=None	,A=None	,A=None	,A = True	,A=None	,A=None	,A=None	,):
          UpperCAmelCase							=       return_dict if return_dict is not None else self.config.return_dict

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

          UpperCAmelCase							=       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__	,)

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

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

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

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







class 					lowerCamelCase__					(       snake_case ):
    SCREAMING_SNAKE_CASE		=			HybridCLIPConfig
    SCREAMING_SNAKE_CASE		=			FlaxHybridCLIPModule






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







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






    def 				_UpperCamelCase							( self	,A	,A	,A = None					):
          UpperCAmelCase							=       jnp.zeros(input_shape[0]	,dtype="""i4"""					)
          UpperCAmelCase							=       jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowercase__					).shape[-1]					)	,input_shape[0]					)
          UpperCAmelCase							=       jnp.ones_like(lowercase__					)
          UpperCAmelCase							=       jnp.ones_like(lowercase__					)

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

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

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






    def __call__( self	,A	,A	,A=None	,A=None	,A=None	,A = None	,A = None	,A = False	,A = None	,A = None	,A = None	,):
          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(lowercase__					).shape[-1]					)	,input_ids.shape					)

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

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

          # 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(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 				_UpperCamelCase							( self	,A	,A=None	,A=None	,A=None	,A = None	,A = None	,A=False	,):







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

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

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

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

          def _get_features(A	,A	,A	,A	,A	,A					):
                UpperCAmelCase							=       module.text_model(
                    input_ids=lowercase__	,attention_mask=lowercase__	,position_ids=lowercase__	,token_type_ids=lowercase__	,deterministic=lowercase__	,)
                UpperCAmelCase							=       text_outputs[1]
                UpperCAmelCase							=       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 				_UpperCamelCase							( self	,A	,A = None	,A = None	,A=False					):
          UpperCAmelCase							=       {}
          if dropout_rng is not None:
                UpperCAmelCase							=       dropout_rng

          def _get_features(A	,A	,A					):
                UpperCAmelCase							=       module.vision_model(pixel_values=lowercase__	,deterministic=lowercase__					)
                UpperCAmelCase							=       vision_outputs[1]  # pooled_output
                UpperCAmelCase							=       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 				_UpperCamelCase							( cls	,A = None	,A = None	,*A	,**A	,):
          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"""	,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

                      UpperCAmelCase							=       AutoConfig.from_pretrained(lowercase__					)
                      UpperCAmelCase							=       text_config

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

          UpperCAmelCase							=       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

                      UpperCAmelCase							=       AutoConfig.from_pretrained(lowercase__					)
                      UpperCAmelCase							=       vision_config

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

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

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

          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





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