

"""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_       :						Dict					=    logging.get_logger(__name__)



class        a    (							nn.Module					):



    """simple docstring"""



    UpperCAmelCase    			=       42
    UpperCAmelCase    			=       jnp.floataa

    def 					UpperCamelCase						(  self:			Optional[int]							):






         """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__     ,    UpperCamelCase							).module_class

         A__				      =	text_module(UpperCamelCase     ,    dtype=self.dtype							)
         A__				      =	vision_module(UpperCamelCase     ,    dtype=self.dtype							)

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





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






         """simple docstring"""


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

         A__				      =	self.vision_model(
             pixel_values=UpperCamelCase     ,    deterministic=UpperCamelCase     ,    output_attentions=UpperCamelCase     ,    output_hidden_states=UpperCamelCase     ,    return_dict=UpperCamelCase     ,    )

         A__				      =	self.text_model(
             input_ids=UpperCamelCase     ,    attention_mask=UpperCamelCase     ,    token_type_ids=UpperCamelCase     ,    position_ids=UpperCamelCase     ,    deterministic=UpperCamelCase     ,    output_attentions=UpperCamelCase     ,    output_hidden_states=UpperCamelCase     ,    return_dict=UpperCamelCase     ,    )

         A__				      =	vision_outputs[1]
         A__				      =	self.visual_projection(UpperCamelCase							)

         A__				      =	text_outputs[1]
         A__				      =	self.text_projection(UpperCamelCase							)

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

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



class        a    (							_lowerCamelCase					):



    """simple docstring"""



    UpperCAmelCase    			=       HybridCLIPConfig
    UpperCAmelCase    			=       FlaxHybridCLIPModule

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






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

    def 					UpperCamelCase						(  self:			Union[str, Any]     ,    UpperCamelCase:			jax.random.PRNGKey     ,    UpperCamelCase:			Tuple     ,    UpperCamelCase:			FrozenDict = None							):






         """simple docstring"""


         A__				      =	jnp.zeros(input_shape[0]     ,    dtype="""i4"""							)
         A__				      =	jnp.broadcast_to(jnp.arange(jnp.atleast_ad(UpperCamelCase							).shape[-1]							)     ,    input_shape[0]							)
         A__				      =	jnp.ones_like(UpperCamelCase							)
         A__				      =	jnp.ones_like(UpperCamelCase							)

         A__				      =	jax.random.normal(UpperCamelCase     ,    input_shape[1]							)

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

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

    def __call__(  self:			Tuple     ,    UpperCamelCase:			str     ,    UpperCamelCase:			Dict     ,    UpperCamelCase:			int=None     ,    UpperCamelCase:			Optional[int]=None     ,    UpperCamelCase:			Tuple=None     ,    UpperCamelCase:			dict = None     ,    UpperCamelCase:			jax.random.PRNGKey = None     ,    UpperCamelCase:			bool = False     ,    UpperCamelCase:			Optional[bool] = None     ,    UpperCamelCase:			Optional[bool] = None     ,    UpperCamelCase:			Optional[bool] = None     ,    ):






         """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(UpperCamelCase							).shape[-1]							)     ,    input_ids.shape							)

         if token_type_ids is None:
              A__				      =	jnp.zeros_like(UpperCamelCase							)

         if attention_mask is None:
              A__				      =	jnp.ones_like(UpperCamelCase							)

         # 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(UpperCamelCase     ,    dtype="""i4"""							)     ,    jnp.array(UpperCamelCase     ,    dtype=jnp.floataa							)     ,    jnp.array(UpperCamelCase     ,    dtype="""i4"""							)     ,    jnp.array(UpperCamelCase     ,    dtype="""i4"""							)     ,    jnp.array(UpperCamelCase     ,    dtype="""i4"""							)     ,    not train     ,    UpperCamelCase     ,    UpperCamelCase     ,    UpperCamelCase     ,    rngs=UpperCamelCase     ,    )

    def 					UpperCamelCase						(  self:			Union[str, Any]     ,    UpperCamelCase:			Any     ,    UpperCamelCase:			int=None     ,    UpperCamelCase:			int=None     ,    UpperCamelCase:			List[Any]=None     ,    UpperCamelCase:			dict = None     ,    UpperCamelCase:			jax.random.PRNGKey = None     ,    UpperCamelCase:			Tuple=False     ,    ):






         """simple docstring"""


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

         if token_type_ids is None:
              A__				      =	jnp.zeros_like(UpperCamelCase							)

         if attention_mask is None:
              A__				      =	jnp.ones_like(UpperCamelCase							)

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

         def _get_features(UpperCamelCase:			str     ,    UpperCamelCase:			Union[str, Any]     ,    UpperCamelCase:			Optional[int]     ,    UpperCamelCase:			Dict     ,    UpperCamelCase:			Any     ,    UpperCamelCase:			Optional[int]							):
              A__				      =	module.text_model(
                  input_ids=UpperCamelCase     ,    attention_mask=UpperCamelCase     ,    position_ids=UpperCamelCase     ,    token_type_ids=UpperCamelCase     ,    deterministic=UpperCamelCase     ,    )
              A__				      =	text_outputs[1]
              A__				      =	module.text_projection(UpperCamelCase							)
              return text_features

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

    def 					UpperCamelCase						(  self:			Union[str, Any]     ,    UpperCamelCase:			List[str]     ,    UpperCamelCase:			dict = None     ,    UpperCamelCase:			jax.random.PRNGKey = None     ,    UpperCamelCase:			Tuple=False							):






         """simple docstring"""


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

         def _get_features(UpperCamelCase:			List[str]     ,    UpperCamelCase:			Optional[int]     ,    UpperCamelCase:			Any							):
              A__				      =	module.vision_model(pixel_values=UpperCamelCase     ,    deterministic=UpperCamelCase							)
              A__				      =	vision_outputs[1]  # pooled_output
              A__				      =	module.visual_projection(UpperCamelCase							)
              return image_features

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





    @classmethod
    def 					UpperCamelCase						(  cls:			List[Any]     ,    UpperCamelCase:			str = None     ,    UpperCamelCase:			str = None     ,    *UpperCamelCase:			Optional[int]     ,    **UpperCamelCase:			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"""     ,    UpperCamelCase							)
         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(UpperCamelCase							)
                   A__				      =	text_config

              A__				      =	FlaxAutoModel.from_pretrained(UpperCamelCase     ,    *UpperCamelCase     ,    **UpperCamelCase							)

         A__				      =	kwargs_vision.pop("""model"""     ,    UpperCamelCase							)
         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(UpperCamelCase							)
                   A__				      =	vision_config

              A__				      =	FlaxAutoModel.from_pretrained(UpperCamelCase     ,    *UpperCamelCase     ,    **UpperCamelCase							)

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

         # init model
         A__				      =	cls(UpperCamelCase     ,    *UpperCamelCase     ,    dtype=UpperCamelCase     ,    **UpperCamelCase							)

         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 typing import List

from tokenizers import NormalizedString, PreTokenizedString, normalizers



class        a    :



    """simple docstring"""



    def __init__(  self:			List[Any]     ,    UpperCamelCase:			str							):






         """simple docstring"""


         A__				      =	vocab
         A__				      =	normalizers.BertNormalizer(
             clean_text=UpperCamelCase     ,    handle_chinese_chars=UpperCamelCase     ,    strip_accents=UpperCamelCase     ,    lowercase=UpperCamelCase     ,    )
         try:
              import rjieba
         except ImportError:
              raise ImportError(
                  """You need to install rjieba to use RoFormerTokenizer. """
                  """See https://pypi.org/project/rjieba/ for installation."""							)
         A__				      =	rjieba

    def 					UpperCamelCase						(  self:			Optional[Any]     ,    UpperCamelCase:			int     ,    UpperCamelCase:			NormalizedString							):






         """simple docstring"""


         A__				      =	[]

         # this code slice normalized_string is too slow (6s) but test_alignement_methods can pass
         for token, start, end in self.jieba.tokenize(str(UpperCamelCase							)     ,    hmm=UpperCamelCase							):
              if token in self.vocab:
                   splits.append(normalized_string[start:end]							)
              else:
                   A__				      =	self.normalizers.normalize_str(UpperCamelCase							).split()
                   for token in token_list:
                        if token:
                             A__				      =	start + len(UpperCamelCase							)
                             splits.append(normalized_string[start:end]							)
                             A__				      =	end

        # this code test_alignement_methods can't pass but fast (300ms)
        # for token in self.jieba.cut(str(normalized_string), False):
        #     if token in self.vocab:
        #         splits.append(NormalizedString(token))
        #     else:
        #         token_list = self.normalizers.normalize_str(token).split()
        #         for token in token_list:
        #             if token:
        #                 splits.append(NormalizedString(token))

         return splits





    def 					UpperCamelCase						(  self:			Tuple     ,    UpperCamelCase:			PreTokenizedString							):






         """simple docstring"""


         pretok.split(self.jieba_split							)






