


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


A__:   Dict    =	logging.get_logger(__name__)







class A__	(						nn.Module				):
      __UpperCamelCase	:  HybridCLIPConfig
      __UpperCamelCase	:  jnp.dtype     	=							jnp.floataa






      def __UpperCAmelCase	(			self      :str     )	-> List[str]:




             '''simple docstring'''
             _a   :			List[str]					 =self.config.text_config
             _a   :			List[Any]					 =self.config.vision_config

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

             _a   :			str					 =FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
             _a   :			Any					 =FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__					,						SCREAMING_SNAKE_CASE     ).module_class

             _a   :			Union[str, Any]					 =text_module(SCREAMING_SNAKE_CASE					,						dtype=self.dtype     )
             _a   :			Union[str, Any]					 =vision_module(SCREAMING_SNAKE_CASE					,						dtype=self.dtype     )

             _a   :			Tuple					 =nn.Dense(
                 self.projection_dim					,						dtype=self.dtype					,						kernel_init=jax.nn.initializers.normal(0.02     )					,						use_bias=SCREAMING_SNAKE_CASE					,						)
             _a   :			Dict					 =nn.Dense(
                 self.projection_dim					,						dtype=self.dtype					,						kernel_init=jax.nn.initializers.normal(0.02     )					,						use_bias=SCREAMING_SNAKE_CASE					,						)
             _a   :			Union[str, Any]					 =self.param("""logit_scale"""					,						jax.nn.initializers.ones					,						[]     )






      def __call__(			self      :Union[str, Any]					,						SCREAMING_SNAKE_CASE      :Any=None					,						SCREAMING_SNAKE_CASE      :Dict=None					,						SCREAMING_SNAKE_CASE      :int=None					,						SCREAMING_SNAKE_CASE      :List[str]=None					,						SCREAMING_SNAKE_CASE      :Optional[Any]=None					,						SCREAMING_SNAKE_CASE      :bool = True					,						SCREAMING_SNAKE_CASE      :Union[str, Any]=None					,						SCREAMING_SNAKE_CASE      :Optional[Any]=None					,						SCREAMING_SNAKE_CASE      :List[str]=None					,						)	-> Any:




             '''simple docstring'''
             _a   :			Optional[int]					 =return_dict if return_dict is not None else self.config.return_dict

             _a   :			Optional[Any]					 =self.vision_model(
                 pixel_values=SCREAMING_SNAKE_CASE					,						deterministic=SCREAMING_SNAKE_CASE					,						output_attentions=SCREAMING_SNAKE_CASE					,						output_hidden_states=SCREAMING_SNAKE_CASE					,						return_dict=SCREAMING_SNAKE_CASE					,						)

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

             _a   :			Optional[int]					 =vision_outputs[1]
             _a   :			int					 =self.visual_projection(SCREAMING_SNAKE_CASE     )

             _a   :			List[Any]					 =text_outputs[1]
             _a   :			Optional[int]					 =self.text_projection(SCREAMING_SNAKE_CASE     )

             # normalized features
             _a   :			List[str]					 =image_embeds / jnp.linalg.norm(SCREAMING_SNAKE_CASE					,						axis=-1					,						keepdims=SCREAMING_SNAKE_CASE     )
             _a   :			Any					 =text_embeds / jnp.linalg.norm(SCREAMING_SNAKE_CASE					,						axis=-1					,						keepdims=SCREAMING_SNAKE_CASE     )

             # cosine similarity as logits
             _a   :			int					 =jnp.exp(self.logit_scale     )
             _a   :			Optional[int]					 =jnp.matmul(SCREAMING_SNAKE_CASE					,						image_embeds.T     ) * logit_scale
             _a   :			Tuple					 =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=SCREAMING_SNAKE_CASE					,						logits_per_text=SCREAMING_SNAKE_CASE					,						text_embeds=SCREAMING_SNAKE_CASE					,						image_embeds=SCREAMING_SNAKE_CASE					,						text_model_output=SCREAMING_SNAKE_CASE					,						vision_model_output=SCREAMING_SNAKE_CASE					,						)







class A__	(						UpperCAmelCase__				):
      __UpperCamelCase	:  Optional[Any]     	=							HybridCLIPConfig
      __UpperCamelCase	:  Optional[int]     	=							FlaxHybridCLIPModule






      def __init__(			self      :Tuple					,						SCREAMING_SNAKE_CASE      :HybridCLIPConfig					,						SCREAMING_SNAKE_CASE      :Optional[Tuple] = None					,						SCREAMING_SNAKE_CASE      :int = 0					,						SCREAMING_SNAKE_CASE      :jnp.dtype = jnp.floataa					,						**SCREAMING_SNAKE_CASE      :Union[str, Any]					,						)	-> Optional[int]:




             '''simple docstring'''
             if input_shape is None:
                    _a   :			int					 =((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

             _a   :			Tuple					 =self.module_class(config=SCREAMING_SNAKE_CASE					,						dtype=SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )
             super().__init__(SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE					,						input_shape=SCREAMING_SNAKE_CASE					,						seed=SCREAMING_SNAKE_CASE					,						dtype=SCREAMING_SNAKE_CASE     )






      def __UpperCAmelCase	(			self      :List[str]					,						SCREAMING_SNAKE_CASE      :jax.random.PRNGKey					,						SCREAMING_SNAKE_CASE      :Tuple					,						SCREAMING_SNAKE_CASE      :FrozenDict = None     )	-> FrozenDict:




             '''simple docstring'''
             # init input tensor
             _a   :			str					 =jnp.zeros(input_shape[0]					,						dtype="""i4"""     )
             _a   :			Dict					 =jnp.broadcast_to(jnp.arange(jnp.atleast_ad(SCREAMING_SNAKE_CASE     ).shape[-1]     )					,						input_shape[0]     )
             _a   :			Union[str, Any]					 =jnp.ones_like(SCREAMING_SNAKE_CASE     )
             _a   :			Optional[int]					 =jnp.ones_like(SCREAMING_SNAKE_CASE     )

             _a   :			List[str]					 =jax.random.normal(SCREAMING_SNAKE_CASE					,						input_shape[1]     )

             _a ,   _a   :			Optional[Any]					 =jax.random.split(SCREAMING_SNAKE_CASE     )
             _a   :			Optional[int]					 ={"""params""": params_rng, """dropout""": dropout_rng}

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






      def __call__(			self      :Union[str, Any]					,						SCREAMING_SNAKE_CASE      :Dict					,						SCREAMING_SNAKE_CASE      :Union[str, Any]					,						SCREAMING_SNAKE_CASE      :Optional[int]=None					,						SCREAMING_SNAKE_CASE      :Dict=None					,						SCREAMING_SNAKE_CASE      :List[str]=None					,						SCREAMING_SNAKE_CASE      :dict = None					,						SCREAMING_SNAKE_CASE      :jax.random.PRNGKey = None					,						SCREAMING_SNAKE_CASE      :bool = False					,						SCREAMING_SNAKE_CASE      :Optional[bool] = None					,						SCREAMING_SNAKE_CASE      :Optional[bool] = None					,						SCREAMING_SNAKE_CASE      :Optional[bool] = None					,						)	-> Dict:




             '''simple docstring'''
             _a   :			List[Any]					 =output_attentions if output_attentions is not None else self.config.output_attentions
             _a   :			List[str]					 =(
                 output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
             )
             _a   :			int					 =return_dict if return_dict is not None else self.config.return_dict

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

             if token_type_ids is None:
                    _a   :			Union[str, Any]					 =jnp.zeros_like(SCREAMING_SNAKE_CASE     )

             if attention_mask is None:
                    _a   :			Optional[int]					 =jnp.ones_like(SCREAMING_SNAKE_CASE     )

             # Handle any PRNG if needed
             _a   :			int					 ={}
             if dropout_rng is not None:
                    _a   :			List[str]					 =dropout_rng

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






      def __UpperCAmelCase	(			self      :Tuple					,						SCREAMING_SNAKE_CASE      :Tuple					,						SCREAMING_SNAKE_CASE      :str=None					,						SCREAMING_SNAKE_CASE      :Optional[int]=None					,						SCREAMING_SNAKE_CASE      :Optional[int]=None					,						SCREAMING_SNAKE_CASE      :dict = None					,						SCREAMING_SNAKE_CASE      :jax.random.PRNGKey = None					,						SCREAMING_SNAKE_CASE      :int=False					,						)	-> Any:




             '''simple docstring'''
             if position_ids is None:
                    _a   :			Any					 =jnp.broadcast_to(jnp.arange(jnp.atleast_ad(SCREAMING_SNAKE_CASE     ).shape[-1]     )					,						input_ids.shape     )

             if token_type_ids is None:
                    _a   :			Optional[int]					 =jnp.zeros_like(SCREAMING_SNAKE_CASE     )

             if attention_mask is None:
                    _a   :			List[Any]					 =jnp.ones_like(SCREAMING_SNAKE_CASE     )

             # Handle any PRNG if needed
             _a   :			List[str]					 ={}
             if dropout_rng is not None:
                    _a   :			List[Any]					 =dropout_rng

             def _get_features(SCREAMING_SNAKE_CASE      :Union[str, Any]					,						SCREAMING_SNAKE_CASE      :List[Any]					,						SCREAMING_SNAKE_CASE      :Tuple					,						SCREAMING_SNAKE_CASE      :List[Any]					,						SCREAMING_SNAKE_CASE      :Optional[int]					,						SCREAMING_SNAKE_CASE      :Union[str, Any]     ):
                    _a   :			Any					 =module.text_model(
                        input_ids=SCREAMING_SNAKE_CASE					,						attention_mask=SCREAMING_SNAKE_CASE					,						position_ids=SCREAMING_SNAKE_CASE					,						token_type_ids=SCREAMING_SNAKE_CASE					,						deterministic=SCREAMING_SNAKE_CASE					,						)
                    _a   :			str					 =text_outputs[1]
                    _a   :			Tuple					 =module.text_projection(SCREAMING_SNAKE_CASE     )
                    return text_features

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






      def __UpperCAmelCase	(			self      :Any					,						SCREAMING_SNAKE_CASE      :Union[str, Any]					,						SCREAMING_SNAKE_CASE      :dict = None					,						SCREAMING_SNAKE_CASE      :jax.random.PRNGKey = None					,						SCREAMING_SNAKE_CASE      :str=False     )	-> Optional[Any]:




             '''simple docstring'''
             _a   :			int					 ={}
             if dropout_rng is not None:
                    _a   :			Tuple					 =dropout_rng

             def _get_features(SCREAMING_SNAKE_CASE      :str					,						SCREAMING_SNAKE_CASE      :Dict					,						SCREAMING_SNAKE_CASE      :Optional[Any]     ):
                    _a   :			Union[str, Any]					 =module.vision_model(pixel_values=SCREAMING_SNAKE_CASE					,						deterministic=SCREAMING_SNAKE_CASE     )
                    _a   :			Optional[int]					 =vision_outputs[1]  # pooled_output
                    _a   :			Dict					 =module.visual_projection(SCREAMING_SNAKE_CASE     )
                    return image_features

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






      @classmethod
      def __UpperCAmelCase	(			cls      :str					,						SCREAMING_SNAKE_CASE      :str = None					,						SCREAMING_SNAKE_CASE      :str = None					,						*SCREAMING_SNAKE_CASE      :str					,						**SCREAMING_SNAKE_CASE      :Optional[Any]					,						)	-> FlaxPreTrainedModel:




             '''simple docstring'''
             _a   :			int					 ={
                 argument[len("""text_"""     ) :]: value for argument, value in kwargs.items() if argument.startswith("""text_"""     )
             }

             _a   :			int					 ={
                 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
             _a   :			Union[str, Any]					 =kwargs_text.pop("""model"""					,						SCREAMING_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

                           _a   :			Optional[int]					 =AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE     )
                           _a   :			Union[str, Any]					 =text_config

                    _a   :			str					 =FlaxAutoModel.from_pretrained(SCREAMING_SNAKE_CASE					,						*SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )

             _a   :			Dict					 =kwargs_vision.pop("""model"""					,						SCREAMING_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

                           _a   :			Optional[int]					 =AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE     )
                           _a   :			Any					 =vision_config

                    _a   :			Optional[Any]					 =FlaxAutoModel.from_pretrained(SCREAMING_SNAKE_CASE					,						*SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )

             # instantiate config with corresponding kwargs
             _a   :			Union[str, Any]					 =kwargs.pop("""dtype"""					,						jnp.floataa     )
             _a   :			str					 =HybridCLIPConfig.from_text_vision_configs(text_model.config					,						vision_model.config					,						**SCREAMING_SNAKE_CASE     )

             # init model
             _a   :			Tuple					 =cls(SCREAMING_SNAKE_CASE					,						*SCREAMING_SNAKE_CASE					,						dtype=SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )

             if vision_config.model_type == "clip":
                    _a   :			int					 =vision_model.params["""vision_model"""]
                    _a   :			Optional[Any]					 =vision_model.params["""visual_projection"""]["""kernel"""]
             else:
                    _a   :			str					 =vision_model.params

             _a   :			Union[str, Any]					 =text_model.params

             return model






'''simple docstring'''







from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding







class A__	(						UpperCAmelCase__				):
      __UpperCamelCase	:  Optional[Any]     	=							["image_processor", "tokenizer"]
      __UpperCamelCase	:  Optional[Any]     	=							"EfficientNetImageProcessor"
      __UpperCamelCase	:  Union[str, Any]     	=							("BertTokenizer", "BertTokenizerFast")






      def __init__(			self      :Union[str, Any]					,						SCREAMING_SNAKE_CASE      :int					,						SCREAMING_SNAKE_CASE      :Optional[int]     )	-> List[str]:




             '''simple docstring'''
             super().__init__(SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE     )






      def __call__(			self      :str					,						SCREAMING_SNAKE_CASE      :List[Any]=None					,						SCREAMING_SNAKE_CASE      :Dict=None					,						SCREAMING_SNAKE_CASE      :Union[str, Any]="max_length"					,						SCREAMING_SNAKE_CASE      :Any=6_4					,						SCREAMING_SNAKE_CASE      :List[str]=None					,						**SCREAMING_SNAKE_CASE      :Union[str, Any]     )	-> Optional[int]:




             '''simple docstring'''
             if text is None and images is None:
                    raise ValueError("""You have to specify either text or images. Both cannot be none."""     )

             if text is not None:
                    _a   :			Dict					 =self.tokenizer(
                        SCREAMING_SNAKE_CASE					,						padding=SCREAMING_SNAKE_CASE					,						max_length=SCREAMING_SNAKE_CASE					,						return_tensors=SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )

             if images is not None:
                    _a   :			Any					 =self.image_processor(SCREAMING_SNAKE_CASE					,						return_tensors=SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )

             if text is not None and images is not None:
                    _a   :			Dict					 =image_features.pixel_values
                    return encoding
             elif text is not None:
                    return encoding
             else:
                    return BatchEncoding(data=dict(**SCREAMING_SNAKE_CASE     )					,						tensor_type=SCREAMING_SNAKE_CASE     )






      def __UpperCAmelCase	(			self      :Any					,						*SCREAMING_SNAKE_CASE      :Optional[int]					,						**SCREAMING_SNAKE_CASE      :str     )	-> Dict:




             '''simple docstring'''
             return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )






      def __UpperCAmelCase	(			self      :int					,						*SCREAMING_SNAKE_CASE      :List[str]					,						**SCREAMING_SNAKE_CASE      :Union[str, Any]     )	-> Union[str, Any]:




             '''simple docstring'''
             return self.tokenizer.decode(*SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )






      @property
      def __UpperCAmelCase	(			self      :Tuple     )	-> Tuple:




             '''simple docstring'''
             _a   :			int					 =self.tokenizer.model_input_names
             _a   :			Union[str, Any]					 =self.image_processor.model_input_names
             return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names     )     )


