






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


SCREAMING_SNAKE_CASE_				      =		logging.get_logger(__name__)




class 							lowerCAmelCase					(							nn.Module   ):







   """simple docstring"""







   _A						=   42
   _A						=   jnp.floataa



   def    __magic_name__    ( self			)    ->   Tuple:
    __a    :	Optional[Any]									=						self.config.text_config
    __a    :	Tuple									=						self.config.vision_config

    __a    :	List[Any]									=						self.config.projection_dim
    __a    :	str									=						text_config.hidden_size
    __a    :	Optional[Any]									=						vision_config.hidden_size

    __a    :	List[Any]									=						FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
    __a    :	List[Any]									=						FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__   ,   lowercase_			).module_class

    __a    :	Optional[int]									=						text_module(lowercase_   ,   dtype=self.dtype			)
    __a    :	Dict									=						vision_module(lowercase_   ,   dtype=self.dtype			)

    __a    :	int									=						nn.Dense(
        self.projection_dim   ,   dtype=self.dtype   ,   kernel_init=jax.nn.initializers.normal(0.02			)   ,   use_bias=lowercase_   ,   )
    __a    :	List[Any]									=						nn.Dense(
        self.projection_dim   ,   dtype=self.dtype   ,   kernel_init=jax.nn.initializers.normal(0.02			)   ,   use_bias=lowercase_   ,   )
    __a    :	Optional[int]									=						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   ,   )    ->   Optional[int]:
    __a    :	Dict									=						return_dict if return_dict is not None else self.config.return_dict

    __a    :	Union[str, Any]									=						self.vision_model(
        pixel_values=lowercase_   ,   deterministic=lowercase_   ,   output_attentions=lowercase_   ,   output_hidden_states=lowercase_   ,   return_dict=lowercase_   ,   )

    __a    :	str									=						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_   ,   )

    __a    :	Tuple									=						vision_outputs[1]
    __a    :	Dict									=						self.visual_projection(lowercase_			)

    __a    :	str									=						text_outputs[1]
    __a    :	List[str]									=						self.text_projection(lowercase_			)

    # normalized features
    __a    :	Tuple									=						image_embeds / jnp.linalg.norm(lowercase_   ,   axis=-1   ,   keepdims=lowercase_			)
    __a    :	Optional[int]									=						text_embeds / jnp.linalg.norm(lowercase_   ,   axis=-1   ,   keepdims=lowercase_			)

    # cosine similarity as logits
    __a    :	Dict									=						jnp.exp(self.logit_scale			)
    __a    :	Union[str, Any]									=						jnp.matmul(lowercase_   ,   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=lowercase_   ,   logits_per_text=lowercase_   ,   text_embeds=lowercase_   ,   image_embeds=lowercase_   ,   text_model_output=lowercase_   ,   vision_model_output=lowercase_   ,   )




class 							lowerCAmelCase					(							__UpperCAmelCase   ):







   """simple docstring"""







   _A						=   HybridCLIPConfig
   _A						=   FlaxHybridCLIPModule



   def __init__( self   ,   _A   ,   _A = None   ,   _A = 0   ,   _A = jnp.floataa   ,   **_A   ,   )    ->   str:

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

    __a    :	int									=						self.module_class(config=lowercase_   ,   dtype=lowercase_   ,   **lowercase_			)
    super().__init__(lowercase_   ,   lowercase_   ,   input_shape=lowercase_   ,   seed=lowercase_   ,   dtype=lowercase_			)



   def    __magic_name__    ( self   ,   _A   ,   _A   ,   _A = None			)    ->   FrozenDict:
    __a    :	Union[str, Any]									=						jnp.zeros(input_shape[0]   ,   dtype='i4'			)
    __a    :	int									=						jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowercase_			).shape[-1]			)   ,   input_shape[0]			)
    __a    :	Union[str, Any]									=						jnp.ones_like(lowercase_			)
    __a    :	Tuple									=						jnp.ones_like(lowercase_			)

    __a    :	Any									=						jax.random.normal(lowercase_   ,   input_shape[1]			)

    __a    :	Optional[int]									=						jax.random.split(lowercase_			)
    __a    :	str									=						{'''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   ,   )    ->   Union[str, Any]:
    __a    :	Union[str, Any]									=						output_attentions if output_attentions is not None else self.config.output_attentions
    __a    :	int									=						(
        output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    )
    __a    :	Union[str, Any]									=						return_dict if return_dict is not None else self.config.return_dict

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

    if token_type_ids is None:
     __a    :	int									=						jnp.zeros_like(lowercase_			)

    if attention_mask is None:
     __a    :	Any									=						jnp.ones_like(lowercase_			)

    # Handle any PRNG if needed
    __a    :	List[str]									=						{}
    if dropout_rng is not None:
     __a    :	Union[str, Any]									=						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    __magic_name__    ( self   ,   _A   ,   _A=None   ,   _A=None   ,   _A=None   ,   _A = None   ,   _A = None   ,   _A=False   ,   )    ->   Union[str, Any]:

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

    if token_type_ids is None:
     __a    :	List[str]									=						jnp.zeros_like(lowercase_			)

    if attention_mask is None:
     __a    :	int									=						jnp.ones_like(lowercase_			)

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

    def _get_features(_A   ,   _A   ,   _A   ,   _A   ,   _A   ,   _A			):
     __a    :	Optional[Any]									=						module.text_model(
         input_ids=lowercase_   ,   attention_mask=lowercase_   ,   position_ids=lowercase_   ,   token_type_ids=lowercase_   ,   deterministic=lowercase_   ,   )
     __a    :	Optional[int]									=						text_outputs[1]
     __a    :	Optional[Any]									=						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    __magic_name__    ( self   ,   _A   ,   _A = None   ,   _A = None   ,   _A=False			)    ->   List[Any]:
    __a    :	Union[str, Any]									=						{}
    if dropout_rng is not None:
     __a    :	Dict									=						dropout_rng

    def _get_features(_A   ,   _A   ,   _A			):
     __a    :	Optional[Any]									=						module.vision_model(pixel_values=lowercase_   ,   deterministic=lowercase_			)
     __a    :	Tuple									=						vision_outputs[1]  # pooled_output
     __a    :	Optional[Any]									=						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    __magic_name__    ( cls   ,   _A = None   ,   _A = None   ,   *_A   ,   **_A   ,   )    ->   FlaxPreTrainedModel:
    __a    :	int									=						{
        argument[len('text_'			) :]: value for argument, value in kwargs.items() if argument.startswith('text_'			)
    }

    __a    :	str									=						{
        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    :	Optional[Any]									=						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

      __a    :	List[Any]									=						AutoConfig.from_pretrained(lowercase_			)
      __a    :	Union[str, Any]									=						text_config

     __a    :	Optional[int]									=						FlaxAutoModel.from_pretrained(lowercase_   ,   *lowercase_   ,   **lowercase_			)

    __a    :	List[Any]									=						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

      __a    :	Optional[int]									=						AutoConfig.from_pretrained(lowercase_			)
      __a    :	Dict									=						vision_config

     __a    :	Dict									=						FlaxAutoModel.from_pretrained(lowercase_   ,   *lowercase_   ,   **lowercase_			)

    # instantiate config with corresponding kwargs
    __a    :	Any									=						kwargs.pop('dtype'   ,   jnp.floataa			)
    __a    :	int									=						HybridCLIPConfig.from_text_vision_configs(text_model.config   ,   vision_model.config   ,   **lowercase_			)

    # init model
    __a    :	int									=						cls(lowercase_   ,   *lowercase_   ,   dtype=lowercase_   ,   **lowercase_			)

    if vision_config.model_type == "clip":
     __a    :	Union[str, Any]									=						vision_model.params['''vision_model''']
     __a    :	Union[str, Any]									=						vision_model.params['''visual_projection''']['''kernel''']
    else:
     __a    :	str									=						vision_model.params

    __a    :	Optional[Any]									=						text_model.params

    return model












'''simple docstring'''


def   lowerCAmelCase__			(					SCREAMING_SNAKE_CASE__ = 30		):
 __a    :	Tuple									=						exponent + 2
 __a    :	str									=						(1 + 5**0.5) / 2
 __a    :	Any									=						(phi**fibonacci_index - (phi - 1) ** fibonacci_index) / 5**0.5

 return int(SCREAMING_SNAKE_CASE__		)


if __name__ == "__main__":
  print(F"{solution() = }")




