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


lowercase_						    =					logging.get_logger(__name__)



class 		UpperCAmelCase_	(nn.Module						):
 """simple docstring"""




 UpperCamelCase_						:     HybridCLIPConfig
 UpperCamelCase_						:     jnp.dtype				   =    jnp.floataa




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



      """simple docstring"""

      UpperCAmelCase_						:    List[Any]    					=  self.config.text_config
      UpperCAmelCase_						:    Any    					=  self.config.vision_config

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

      UpperCAmelCase_						:    Optional[int]    					=  FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
      UpperCAmelCase_						:    str    					=  FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__     ,   a_ ).module_class

      UpperCAmelCase_						:    Optional[int]    					=  text_module(a_     ,   dtype=self.dtype )
      UpperCAmelCase_						:    int    					=  vision_module(a_     ,   dtype=self.dtype )

      UpperCAmelCase_						:    Optional[int]    					=  nn.Dense(
          self.projection_dim     ,   dtype=self.dtype     ,   kernel_init=jax.nn.initializers.normal(0.02 )     ,   use_bias=a_     ,   )
      UpperCAmelCase_						:    Optional[Any]    					=  nn.Dense(
          self.projection_dim     ,   dtype=self.dtype     ,   kernel_init=jax.nn.initializers.normal(0.02 )     ,   use_bias=a_     ,   )
      UpperCAmelCase_						:    Optional[Any]    					=  self.param("""logit_scale"""     ,   jax.nn.initializers.ones     ,   [] )






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



      """simple docstring"""

      UpperCAmelCase_						:    List[Any]    					=  return_dict if return_dict is not None else self.config.return_dict

      UpperCAmelCase_						:    List[str]    					=  self.vision_model(
          pixel_values=a_     ,   deterministic=a_     ,   output_attentions=a_     ,   output_hidden_states=a_     ,   return_dict=a_     ,   )

      UpperCAmelCase_						:    Union[str, Any]    					=  self.text_model(
          input_ids=a_     ,   attention_mask=a_     ,   token_type_ids=a_     ,   position_ids=a_     ,   deterministic=a_     ,   output_attentions=a_     ,   output_hidden_states=a_     ,   return_dict=a_     ,   )

      UpperCAmelCase_						:    str    					=  vision_outputs[1]
      UpperCAmelCase_						:    Optional[int]    					=  self.visual_projection(a_ )

      UpperCAmelCase_						:    str    					=  text_outputs[1]
      UpperCAmelCase_						:    Any    					=  self.text_projection(a_ )

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

      # cosine similarity as logits
      UpperCAmelCase_						:    str    					=  jnp.exp(self.logit_scale )
      UpperCAmelCase_						:    int    					=  jnp.matmul(a_     ,   image_embeds.T ) * logit_scale
      UpperCAmelCase_						:    Any    					=  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=a_     ,   logits_per_text=a_     ,   text_embeds=a_     ,   image_embeds=a_     ,   text_model_output=a_     ,   vision_model_output=a_     ,   )



class 		UpperCAmelCase_	(lowerCamelCase_						):
 """simple docstring"""




 UpperCamelCase_						:     str				   =    HybridCLIPConfig
 UpperCamelCase_						:     str				   =    FlaxHybridCLIPModule




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



      """simple docstring"""

      if input_shape is None:
           UpperCAmelCase_						:    Optional[Any]    					=  ((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

      UpperCAmelCase_						:    Optional[Any]    					=  self.module_class(config=a_     ,   dtype=a_     ,   **a_ )
      super().__init__(a_     ,   a_     ,   input_shape=a_     ,   seed=a_     ,   dtype=a_ )




 def   a      ( self       : int     ,   a_       : jax.random.PRNGKey     ,   a_       : Tuple     ,   a_       : FrozenDict = None )->					FrozenDict:



      """simple docstring"""

      UpperCAmelCase_						:    str    					=  jnp.zeros(input_shape[0]     ,   dtype="""i4""" )
      UpperCAmelCase_						:    Any    					=  jnp.broadcast_to(jnp.arange(jnp.atleast_ad(a_ ).shape[-1] )     ,   input_shape[0] )
      UpperCAmelCase_						:    Dict    					=  jnp.ones_like(a_ )
      UpperCAmelCase_						:    int    					=  jnp.ones_like(a_ )

      UpperCAmelCase_						:    str    					=  jax.random.normal(a_     ,   input_shape[1] )

      UpperCAmelCase_					,UpperCAmelCase_						:    Dict    					=  jax.random.split(a_ )
      UpperCAmelCase_						:    Any    					=  {"""params""": params_rng, """dropout""": dropout_rng}

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




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



      """simple docstring"""

      UpperCAmelCase_						:    Optional[Any]    					=  output_attentions if output_attentions is not None else self.config.output_attentions
      UpperCAmelCase_						:    Dict    					=  (
          output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
      )
      UpperCAmelCase_						:    Optional[int]    					=  return_dict if return_dict is not None else self.config.return_dict

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

      if token_type_ids is None:
           UpperCAmelCase_						:    Dict    					=  jnp.zeros_like(a_ )

      if attention_mask is None:
           UpperCAmelCase_						:    int    					=  jnp.ones_like(a_ )

      # Handle any PRNG if needed
      UpperCAmelCase_						:    Optional[Any]    					=  {}
      if dropout_rng is not None:
           UpperCAmelCase_						:    Optional[Any]    					=  dropout_rng

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




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



      """simple docstring"""

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

      if token_type_ids is None:
           UpperCAmelCase_						:    Dict    					=  jnp.zeros_like(a_ )

      if attention_mask is None:
           UpperCAmelCase_						:    List[str]    					=  jnp.ones_like(a_ )

      # Handle any PRNG if needed
      UpperCAmelCase_						:    List[Any]    					=  {}
      if dropout_rng is not None:
           UpperCAmelCase_						:    Optional[int]    					=  dropout_rng

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

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




 def   a      ( self       : Optional[Any]     ,   a_       : Dict     ,   a_       : dict = None     ,   a_       : jax.random.PRNGKey = None     ,   a_       : Tuple=False )->					List[Any]:



      """simple docstring"""

      UpperCAmelCase_						:    str    					=  {}
      if dropout_rng is not None:
           UpperCAmelCase_						:    List[Any]    					=  dropout_rng

      def _get_features(a_       : Optional[Any]     ,   a_       : Tuple     ,   a_       : str ):
           UpperCAmelCase_						:    Union[str, Any]    					=  module.vision_model(pixel_values=a_     ,   deterministic=a_ )
           UpperCAmelCase_						:    Tuple    					=  vision_outputs[1]  # pooled_output
           UpperCAmelCase_						:    List[Any]    					=  module.visual_projection(a_ )
           return image_features

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






 @classmethod
 def   a      ( cls       : Any     ,   a_       : str = None     ,   a_       : str = None     ,   *a_       : int     ,   **a_       : Dict     ,   )->					FlaxPreTrainedModel:



      """simple docstring"""

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

      UpperCAmelCase_						:    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
      UpperCAmelCase_						:    Optional[int]    					=  kwargs_text.pop("""model"""     ,   a_ )
      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_						:    Any    					=  AutoConfig.from_pretrained(a_ )
                UpperCAmelCase_						:    Any    					=  text_config

           UpperCAmelCase_						:    str    					=  FlaxAutoModel.from_pretrained(a_     ,   *a_     ,   **a_ )

      UpperCAmelCase_						:    Optional[Any]    					=  kwargs_vision.pop("""model"""     ,   a_ )
      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_						:    int    					=  AutoConfig.from_pretrained(a_ )
                UpperCAmelCase_						:    Union[str, Any]    					=  vision_config

           UpperCAmelCase_						:    str    					=  FlaxAutoModel.from_pretrained(a_     ,   *a_     ,   **a_ )

      # instantiate config with corresponding kwargs
      UpperCAmelCase_						:    Optional[Any]    					=  kwargs.pop("""dtype"""     ,   jnp.floataa )
      UpperCAmelCase_						:    Optional[Any]    					=  HybridCLIPConfig.from_text_vision_configs(text_model.config     ,   vision_model.config     ,   **a_ )

      # init model
      UpperCAmelCase_						:    Optional[Any]    					=  cls(a_     ,   *a_     ,   dtype=a_     ,   **a_ )

      if vision_config.model_type == "clip":
           UpperCAmelCase_						:    List[str]    					=  vision_model.params["""vision_model"""]
           UpperCAmelCase_						:    Union[str, Any]    					=  vision_model.params["""visual_projection"""]["""kernel"""]
      else:
           UpperCAmelCase_						:    Tuple    					=  vision_model.params

      UpperCAmelCase_						:    List[str]    					=  text_model.params

      return model



"""simple docstring"""







def 			A_			(					lowercase = 99_8001      )     ->							int:


     """simple docstring"""







     for number in range(n - 1						, 9999						, -1      ):
          UpperCAmelCase_						:    Union[str, Any]    					=  str(lowercase      )

          # checks whether 'str_number' is a palindrome.
          if str_number == str_number[::-1]:
               UpperCAmelCase_						:    List[str]    					=  999

               # if 'number' is a product of two 3-digit numbers
               # then number is the answer otherwise fetch next number.
               while divisor != 99:
                    if (number % divisor == 0) and (len(str(number // divisor      )      ) == 3.0):
                         return number
                    divisor -= 1
     raise ValueError("""That number is larger than our acceptable range."""      )


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

