
'''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 			UpperCamelCase__	(     nn.Module):
 UpperCAmelCase__     :     List[str]    					=       4_2
 UpperCAmelCase__     :     Optional[Any]    					=       jnp.floataa






 def 						lowercase_    (      self	:Optional[int]      )					->      str:







       '''simple docstring'''



       __A													=      self.config.text_config
       __A													=      self.config.vision_config

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

       __A													=      FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
       __A													=      FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__						,					_A      ).module_class

       __A													=      text_module(_A						,					dtype=self.dtype      )
       __A													=      vision_module(_A						,					dtype=self.dtype      )

       __A													=      nn.Dense(
           self.projection_dim						,					dtype=self.dtype						,					kernel_init=jax.nn.initializers.normal(0.02      )						,					use_bias=_A						,					)
       __A													=      nn.Dense(
           self.projection_dim						,					dtype=self.dtype						,					kernel_init=jax.nn.initializers.normal(0.02      )						,					use_bias=_A						,					)
       __A													=      self.param('logit_scale'						,					jax.nn.initializers.ones						,					[]      )






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







       '''simple docstring'''



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

       __A													=      self.vision_model(
           pixel_values=_A						,					deterministic=_A						,					output_attentions=_A						,					output_hidden_states=_A						,					return_dict=_A						,					)

       __A													=      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						,					)

       __A													=      vision_outputs[1]
       __A													=      self.visual_projection(_A      )

       __A													=      text_outputs[1]
       __A													=      self.text_projection(_A      )

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

       # cosine similarity as logits
       __A													=      jnp.exp(self.logit_scale      )
       __A													=      jnp.matmul(_A						,					image_embeds.T      ) * logit_scale
       __A													=      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):
 UpperCAmelCase__     :     Optional[int]    					=       HybridCLIPConfig
 UpperCAmelCase__     :     Union[str, Any]    					=       FlaxHybridCLIPModule






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







       '''simple docstring'''



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

       __A													=      self.module_class(config=_A						,					dtype=_A						,					**_A      )
       super().__init__(_A						,					_A						,					input_shape=_A						,					seed=_A						,					dtype=_A      )






 def 						lowercase_    (      self	:Union[str, Any]						,					_A	:jax.random.PRNGKey						,					_A	:Tuple						,					_A	:FrozenDict = None      )					->      Optional[int]:







       '''simple docstring'''



       __A													=      jnp.zeros(input_shape[0]						,					dtype='i4'      )
       __A													=      jnp.broadcast_to(jnp.arange(jnp.atleast_ad(_A      ).shape[-1]      )						,					input_shape[0]      )
       __A													=      jnp.ones_like(_A      )
       __A													=      jnp.ones_like(_A      )

       __A													=      jax.random.normal(_A						,					input_shape[1]      )

       __A						,   __A													=      jax.random.split(_A      )
       __A													=      {'params': params_rng, 'dropout': dropout_rng}

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






 def __call__(      self	:Tuple						,					_A	:str						,					_A	:Dict						,					_A	:int=None						,					_A	:Optional[int]=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						,					)					->      List[str]:







       '''simple docstring'''



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

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

       if token_type_ids is None:
             __A													=      jnp.zeros_like(_A      )

       if attention_mask is None:
             __A													=      jnp.ones_like(_A      )

       # Handle any PRNG if needed
       __A													=      {}
       if dropout_rng is not None:
             __A													=      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 						lowercase_    (      self	:Union[str, Any]						,					_A	:Any						,					_A	:int=None						,					_A	:int=None						,					_A	:List[Any]=None						,					_A	:dict = None						,					_A	:jax.random.PRNGKey = None						,					_A	:Tuple=False						,					)					->      int:







       '''simple docstring'''



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

       if token_type_ids is None:
             __A													=      jnp.zeros_like(_A      )

       if attention_mask is None:
             __A													=      jnp.ones_like(_A      )

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

       def _get_features(_A	:str						,					_A	:Union[str, Any]						,					_A	:Optional[int]						,					_A	:Dict						,					_A	:Any						,					_A	:Optional[int]      ):
             __A													=      module.text_model(
                 input_ids=_A						,					attention_mask=_A						,					position_ids=_A						,					token_type_ids=_A						,					deterministic=_A						,					)
             __A													=      text_outputs[1]
             __A													=      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 						lowercase_    (      self	:Union[str, Any]						,					_A	:List[str]						,					_A	:dict = None						,					_A	:jax.random.PRNGKey = None						,					_A	:Tuple=False      )					->      Tuple:







       '''simple docstring'''



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

       def _get_features(_A	:List[str]						,					_A	:Optional[int]						,					_A	:Any      ):
             __A													=      module.vision_model(pixel_values=_A						,					deterministic=_A      )
             __A													=      vision_outputs[1]  # pooled_output
             __A													=      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 						lowercase_    (      cls	:List[Any]						,					_A	:str = None						,					_A	:str = None						,					*_A	:Optional[int]						,					**_A	:int						,					)					->      Optional[int]:







       '''simple docstring'''



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

       __A													=      {
           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													=      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

                   __A													=      AutoConfig.from_pretrained(_A      )
                   __A													=      text_config

             __A													=      FlaxAutoModel.from_pretrained(_A						,					*_A						,					**_A      )

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

                   __A													=      AutoConfig.from_pretrained(_A      )
                   __A													=      vision_config

             __A													=      FlaxAutoModel.from_pretrained(_A						,					*_A						,					**_A      )

       # instantiate config with corresponding kwargs
       __A													=      kwargs.pop('dtype'						,					jnp.floataa      )
       __A													=      HybridCLIPConfig.from_text_vision_configs(text_model.config						,					vision_model.config						,					**_A      )

       # init model
       __A													=      cls(_A						,					*_A						,					dtype=_A						,					**_A      )

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

       __A													=      text_model.params

       return model









"""simple docstring"""



from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


SCREAMING_SNAKE_CASE_       :						Union[str, Any]					=    logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_       :						Union[str, Any]					=    {
    'MBZUAI/swiftformer-xs': 'https://huggingface.co/MBZUAI/swiftformer-xs/resolve/main/config.json',
}



class        a    (							_lowerCamelCase					):



    """simple docstring"""



    UpperCAmelCase    			=       "swiftformer"

    def __init__(  self:			Tuple     ,    UpperCamelCase:			int=3     ,    UpperCamelCase:			Union[str, Any]=[3, 3, 6, 4]     ,    UpperCamelCase:			List[str]=[48, 56, 1_12, 2_20]     ,    UpperCamelCase:			List[Any]=4     ,    UpperCamelCase:			List[Any]=[True, True, True, True]     ,    UpperCamelCase:			str="gelu"     ,    UpperCamelCase:			Tuple=3     ,    UpperCamelCase:			Dict=2     ,    UpperCamelCase:			int=1     ,    UpperCamelCase:			List[Any]=0.0     ,    UpperCamelCase:			Any=True     ,    UpperCamelCase:			List[Any]=1e-5     ,    UpperCamelCase:			List[Any]=1e-5     ,    **UpperCamelCase:			Any     ,    ):






         """simple docstring"""


         super().__init__(**UpperCamelCase							)
         A__				      =	num_channels
         A__				      =	depths
         A__				      =	embed_dims
         A__				      =	mlp_ratio
         A__				      =	downsamples
         A__				      =	hidden_act
         A__				      =	down_patch_size
         A__				      =	down_stride
         A__				      =	down_pad
         A__				      =	drop_path_rate
         A__				      =	use_layer_scale
         A__				      =	layer_scale_init_value
         A__				      =	batch_norm_eps



class        a    (							_lowerCamelCase					):



    """simple docstring"""



    UpperCAmelCase    			=       version.parse("1.11"					)

    @property
    def 					UpperCamelCase						(  self:			int							):






         """simple docstring"""


         return OrderedDict(
             [
                 ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
             ]							)





    @property
    def 					UpperCamelCase						(  self:			Any							):






         """simple docstring"""


         return 1e-4






