

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






class _UpperCamelCase							(							nn.Module				):
			'''simple docstring'''







			lowerCamelCase__          =42
			lowerCamelCase__          =jnp.floataa
			def 							__UpperCamelCase	(     self							:	Optional[Any]		)							->    Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE				: Tuple			       =    self.config.text_config
				SCREAMING_SNAKE_CASE				: Dict			       =    self.config.vision_config

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

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

				SCREAMING_SNAKE_CASE				: Optional[Any]			       =    text_module(a , dtype=self.dtype		)
				SCREAMING_SNAKE_CASE				: Dict			       =    vision_module(a , dtype=self.dtype		)

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






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

				"""simple docstring"""

				SCREAMING_SNAKE_CASE				: Dict			       =    return_dict if return_dict is not None else self.config.return_dict

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

				SCREAMING_SNAKE_CASE				: List[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 , )

				SCREAMING_SNAKE_CASE				: Tuple			       =    vision_outputs[1]
				SCREAMING_SNAKE_CASE				: str			       =    self.visual_projection(a		)

				SCREAMING_SNAKE_CASE				: Union[str, Any]			       =    text_outputs[1]
				SCREAMING_SNAKE_CASE				: Union[str, Any]			       =    self.text_projection(a		)

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

				# cosine similarity as logits
				SCREAMING_SNAKE_CASE				: Any			       =    jnp.exp(self.logit_scale		)
				SCREAMING_SNAKE_CASE				: Union[str, Any]			       =    jnp.matmul(a , image_embeds.T		) * logit_scale
				SCREAMING_SNAKE_CASE				: Dict			       =    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							(							__A				):
			'''simple docstring'''







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

				"""simple docstring"""

				if input_shape is None:
					SCREAMING_SNAKE_CASE				: Union[str, Any]			       =    ((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

				SCREAMING_SNAKE_CASE				: Optional[Any]			       =    self.module_class(config=a , dtype=a , **a		)
				super().__init__(a , a , input_shape=a , seed=a , dtype=a		)
			def 							__UpperCamelCase	(     self							:	List[str] , a							:	jax.random.PRNGKey , a							:	Tuple , a							:	FrozenDict = None		)							->    FrozenDict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE				: List[str]			       =    jnp.zeros(input_shape[0] , dtype="i4"		)
				SCREAMING_SNAKE_CASE				: List[Any]			       =    jnp.broadcast_to(jnp.arange(jnp.atleast_ad(a		).shape[-1]		) , input_shape[0]		)
				SCREAMING_SNAKE_CASE				: Any			       =    jnp.ones_like(a		)
				SCREAMING_SNAKE_CASE				: List[str]			       =    jnp.ones_like(a		)

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

				SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE				: int			       =    jax.random.split(a		)
				SCREAMING_SNAKE_CASE				: Any			       =    {"params": params_rng, "dropout": dropout_rng}

				return self.module.init(a , a , a , a , a , a		)["params"]
			def __call__(     self							:	str , a							:	Any , a							:	str , a							:	Optional[int]=None , a							:	int=None , a							:	Union[str, Any]=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"""

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

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

				if token_type_ids is None:
					SCREAMING_SNAKE_CASE				: List[Any]			       =    jnp.zeros_like(a		)

				if attention_mask is None:
					SCREAMING_SNAKE_CASE				: str			       =    jnp.ones_like(a		)

				# Handle any PRNG if needed
				SCREAMING_SNAKE_CASE				: str			       =    {}
				if dropout_rng is not None:
					SCREAMING_SNAKE_CASE				: 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 							__UpperCamelCase	(     self							:	Tuple , a							:	Any , a							:	Optional[Any]=None , a							:	Dict=None , a							:	Optional[Any]=None , a							:	dict = None , a							:	jax.random.PRNGKey = None , a							:	List[Any]=False , )							->    Any:

				"""simple docstring"""

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

				if token_type_ids is None:
					SCREAMING_SNAKE_CASE				: Any			       =    jnp.zeros_like(a		)

				if attention_mask is None:
					SCREAMING_SNAKE_CASE				: str			       =    jnp.ones_like(a		)

				# Handle any PRNG if needed
				SCREAMING_SNAKE_CASE				: int			       =    {}
				if dropout_rng is not None:
					SCREAMING_SNAKE_CASE				: str			       =    dropout_rng

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

				"""simple docstring"""

				SCREAMING_SNAKE_CASE				: List[str]			       =    {}
				if dropout_rng is not None:
					SCREAMING_SNAKE_CASE				: Union[str, Any]			       =    dropout_rng

				def _get_features(a							:	int , a							:	str , a							:	str		):
					SCREAMING_SNAKE_CASE				: str			       =    module.vision_model(pixel_values=a , deterministic=a		)
					SCREAMING_SNAKE_CASE				: int			       =    vision_outputs[1]  # pooled_output
					SCREAMING_SNAKE_CASE				: Dict			       =    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 							__UpperCamelCase	(     cls							:	Tuple , a							:	str = None , a							:	str = None , *a							:	List[Any] , **a							:	str , )							->    FlaxPreTrainedModel:

				"""simple docstring"""

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

				SCREAMING_SNAKE_CASE				: Tuple			       =    {
				    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
				SCREAMING_SNAKE_CASE				: 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

						SCREAMING_SNAKE_CASE				: Tuple			       =    AutoConfig.from_pretrained(a		)
						SCREAMING_SNAKE_CASE				: Union[str, Any]			       =    text_config

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

				SCREAMING_SNAKE_CASE				: List[str]			       =    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

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

					SCREAMING_SNAKE_CASE				: Optional[int]			       =    FlaxAutoModel.from_pretrained(a , *a , **a		)

				# instantiate config with corresponding kwargs
				SCREAMING_SNAKE_CASE				: int			       =    kwargs.pop("dtype" , jnp.floataa		)
				SCREAMING_SNAKE_CASE				: str			       =    HybridCLIPConfig.from_text_vision_configs(text_model.config , vision_model.config , **a		)

				# init model
				SCREAMING_SNAKE_CASE				: str			       =    cls(a , *a , dtype=a , **a		)

				if vision_config.model_type == "clip":
					SCREAMING_SNAKE_CASE				: int			       =    vision_model.params["vision_model"]
					SCREAMING_SNAKE_CASE				: List[Any]			       =    vision_model.params["visual_projection"]["kernel"]
				else:
					SCREAMING_SNAKE_CASE				: int			       =    vision_model.params

				SCREAMING_SNAKE_CASE				: Optional[Any]			       =    text_model.params

				return model


from typing import List

from tokenizers import NormalizedString, PreTokenizedString, normalizers






class _UpperCamelCase							:
			'''simple docstring'''







			def __init__(     self							:	Optional[int] , a							:	Any		)							->    None:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE				: List[str]			       =    vocab
				SCREAMING_SNAKE_CASE				: str			       =    normalizers.BertNormalizer(
				    clean_text=a , handle_chinese_chars=a , strip_accents=a , lowercase=a , )
				try:
					import rjieba
				except ImportError:
					raise ImportError(
					    "You need to install rjieba to use RoFormerTokenizer. "
					    "See https://pypi.org/project/rjieba/ for installation."		)
				SCREAMING_SNAKE_CASE				: Union[str, Any]			       =    rjieba
			def 							__UpperCamelCase	(     self							:	Optional[int] , a							:	int , a							:	NormalizedString		)							->    List[NormalizedString]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE				: Dict			       =    []

				# this code slice normalized_string is too slow (6s) but test_alignement_methods can pass
				for token, start, end in self.jieba.tokenize(str(a		) , hmm=a		):
					if token in self.vocab:
						splits.append(normalized_string[start:end]		)
					else:
						SCREAMING_SNAKE_CASE				: Any			       =    self.normalizers.normalize_str(a		).split()
						for token in token_list:
							if token:
								SCREAMING_SNAKE_CASE				: Optional[int]			       =    start + len(a		)
								splits.append(normalized_string[start:end]		)
								SCREAMING_SNAKE_CASE				: List[Any]			       =    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							:	Optional[int] , a							:	PreTokenizedString		)							->    Tuple:

				"""simple docstring"""

				pretok.split(self.jieba_split		)