


'''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			:			List[str]	   =     logging.get_logger(__name__)






class        UpperCamelCase__(   nn.Module       ):
				__magic_name__				:      HybridCLIPConfig
				__magic_name__				:      jnp.dtype  		=       jnp.floataa




				def      a__(		self			: List[str]      )->     Tuple:

										"""simple docstring"""




										UpperCAmelCase     				=	self.config.text_config
										UpperCAmelCase     				=	self.config.vision_config

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

										UpperCAmelCase     				=	FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
										UpperCAmelCase     				=	FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__      ,    __a      ).module_class

										UpperCAmelCase     				=	text_module(__a      ,    dtype=self.dtype      )
										UpperCAmelCase     				=	vision_module(__a      ,    dtype=self.dtype      )

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





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

										"""simple docstring"""




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

										UpperCAmelCase     				=	self.vision_model(
										    pixel_values=__a      ,    deterministic=__a      ,    output_attentions=__a      ,    output_hidden_states=__a      ,    return_dict=__a      ,    )

										UpperCAmelCase     				=	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     				=	vision_outputs[1]
										UpperCAmelCase     				=	self.visual_projection(__a      )

										UpperCAmelCase     				=	text_outputs[1]
										UpperCAmelCase     				=	self.text_projection(__a      )

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

										# cosine similarity as logits
										UpperCAmelCase     				=	jnp.exp(self.logit_scale      )
										UpperCAmelCase     				=	jnp.matmul(__a      ,    image_embeds.T      ) * logit_scale
										UpperCAmelCase     				=	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__(   _UpperCamelCase       ):
				__magic_name__				:      Tuple  		=       HybridCLIPConfig
				__magic_name__				:      List[Any]  		=       FlaxHybridCLIPModule




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

										"""simple docstring"""




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

										UpperCAmelCase     				=	self.module_class(config=__a      ,    dtype=__a      ,    **__a      )
										super().__init__(__a      ,    __a      ,    input_shape=__a      ,    seed=__a      ,    dtype=__a      )




				def      a__(		self			: Optional[Any]      ,    lowerCAmelCase			: jax.random.PRNGKey      ,    lowerCAmelCase			: Tuple      ,    lowerCAmelCase			: FrozenDict = None      )->     FrozenDict:

										"""simple docstring"""




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

										UpperCAmelCase     				=	jax.random.normal(__a      ,    input_shape[1]      )

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

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




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

										"""simple docstring"""




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

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

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

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

										# Handle any PRNG if needed
										UpperCAmelCase     				=	{}
										if dropout_rng is not None:
																UpperCAmelCase     				=	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			: Dict      ,    lowerCAmelCase			: Any      ,    lowerCAmelCase			: List[str]=None      ,    lowerCAmelCase			: Union[str, Any]=None      ,    lowerCAmelCase			: Any=None      ,    lowerCAmelCase			: dict = None      ,    lowerCAmelCase			: jax.random.PRNGKey = None      ,    lowerCAmelCase			: List[str]=False      ,    )->     str:

										"""simple docstring"""




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

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

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

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

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

										"""simple docstring"""




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

										def _get_features(lowerCAmelCase			: int      ,    lowerCAmelCase			: List[str]      ,    lowerCAmelCase			: Optional[Any]      ):
																UpperCAmelCase     				=	module.vision_model(pixel_values=__a      ,    deterministic=__a      )
																UpperCAmelCase     				=	vision_outputs[1]  # pooled_output
																UpperCAmelCase     				=	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			: List[str]      ,    lowerCAmelCase			: str = None      ,    lowerCAmelCase			: str = None      ,    *lowerCAmelCase			: List[Any]      ,    **lowerCAmelCase			: Tuple      ,    )->     FlaxPreTrainedModel:

										"""simple docstring"""




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

										UpperCAmelCase     				=	{
										    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     				=	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     				=	AutoConfig.from_pretrained(__a      )
																						UpperCAmelCase     				=	text_config

																UpperCAmelCase     				=	FlaxAutoModel.from_pretrained(__a      ,    *__a      ,    **__a      )

										UpperCAmelCase     				=	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     				=	AutoConfig.from_pretrained(__a      )
																						UpperCAmelCase     				=	vision_config

																UpperCAmelCase     				=	FlaxAutoModel.from_pretrained(__a      ,    *__a      ,    **__a      )

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

										# init model
										UpperCAmelCase     				=	cls(__a      ,    *__a      ,    dtype=__a      ,    **__a      )

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

										UpperCAmelCase     				=	text_model.params

										return model












"""simple docstring"""


import os
from shutil import copyfile
from typing import List, Optional, Tuple

from tokenizers import processors

from ...tokenization_utils import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
  from .tokenization_mbartaa import MBartaaTokenizer
else:
  lowerCamelCase__      =      None


lowerCamelCase__      =      logging.get_logger(__name__)

lowerCamelCase__      =      {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"}

lowerCamelCase__      =      {
    "vocab_file": {
        "facebook/mbart-large-50-one-to-many-mmt": (
            "https://huggingface.co/facebook/mbart-large-50-one-to-many-mmt/resolve/main/sentencepiece.bpe.model"
        ),
    },
    "tokenizer_file": {
        "facebook/mbart-large-50-one-to-many-mmt": (
            "https://huggingface.co/facebook/mbart-large-50-one-to-many-mmt/resolve/main/tokenizer.json"
        ),
    },
}

lowerCamelCase__      =      {
    "facebook/mbart-large-50-one-to-many-mmt": 1024,
}

# fmt: off
lowerCamelCase__      =      ["ar_AR", "cs_CZ", "de_DE", "en_XX", "es_XX", "et_EE", "fi_FI", "fr_XX", "gu_IN", "hi_IN", "it_IT", "ja_XX", "kk_KZ", "ko_KR", "lt_LT", "lv_LV", "my_MM", "ne_NP", "nl_XX", "ro_RO", "ru_RU", "si_LK", "tr_TR", "vi_VN", "zh_CN", "af_ZA", "az_AZ", "bn_IN", "fa_IR", "he_IL", "hr_HR", "id_ID", "ka_GE", "km_KH", "mk_MK", "ml_IN", "mn_MN", "mr_IN", "pl_PL", "ps_AF", "pt_XX", "sv_SE", "sw_KE", "ta_IN", "te_IN", "th_TH", "tl_XX", "uk_UA", "ur_PK", "xh_ZA", "gl_ES", "sl_SI"]



class    __SCREAMING_SNAKE_CASE			(  _UpperCamelCase      ):







  '''simple docstring'''


  SCREAMING_SNAKE_CASE__							:Tuple		   =    VOCAB_FILES_NAMES
  SCREAMING_SNAKE_CASE__							:List[Any]		   =    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  SCREAMING_SNAKE_CASE__							:Union[str, Any]		   =    PRETRAINED_VOCAB_FILES_MAP
  SCREAMING_SNAKE_CASE__							:int		   =    ["input_ids", "attention_mask"]
  SCREAMING_SNAKE_CASE__							:Dict		   =    MBartaaTokenizer

  SCREAMING_SNAKE_CASE__							:List[int]		   =    []
  SCREAMING_SNAKE_CASE__							:List[int]		   =    []

  def __init__(						self      :				str    ,      __a      :				int=None    ,      __a      :				List[str]=None    ,      __a      :				Optional[Any]=None    ,      __a      :				List[str]=None    ,      __a      :				Tuple="</s>"    ,      __a      :				Union[str, Any]="</s>"    ,      __a      :				str="<s>"    ,      __a      :				Any="<unk>"    ,      __a      :				List[str]="<pad>"    ,      __a      :				Optional[Any]="<mask>"    ,      **__a      :				Any    ,      )						-> Union[str, Any]:
        # Mask token behave like a normal word, i.e. include the space before it
        _UpperCamelCase					:		Dict  =				AddedToken(__a    ,      lstrip=__a    ,      rstrip=__a		) if isinstance(__a    ,      __a		) else mask_token

        _UpperCamelCase					:		str  =				kwargs.get("additional_special_tokens"    ,      []		)
        kwargs["additional_special_tokens"] += [
            code for code in FAIRSEQ_LANGUAGE_CODES if code not in kwargs["additional_special_tokens"]
        ]

        super().__init__(
            __a    ,      src_lang=__a    ,      tgt_lang=__a    ,      tokenizer_file=__a    ,      eos_token=__a    ,      sep_token=__a    ,      cls_token=__a    ,      unk_token=__a    ,      pad_token=__a    ,      mask_token=__a    ,      **__a    ,      )

        _UpperCamelCase					:		Optional[int]  =				vocab_file
        _UpperCamelCase					:		int  =				False if not self.vocab_file else True

        _UpperCamelCase					:		int  =				{
            lang_code: self.convert_tokens_to_ids(__a		) for lang_code in FAIRSEQ_LANGUAGE_CODES
        }

        _UpperCamelCase					:		List[str]  =				src_lang if src_lang is not None else "en_XX"
        _UpperCamelCase					:		Dict  =				tgt_lang
        _UpperCamelCase					:		int  =				self.lang_code_to_id[self._src_lang]
        self.set_src_lang_special_tokens(self._src_lang		)

  @property
  def       __SCREAMING_SNAKE_CASE (						self      :				Optional[int]		)						-> str:
        return self._src_lang

  @src_lang.setter
  def       __SCREAMING_SNAKE_CASE (						self      :				Union[str, Any]    ,      __a      :				str		)						-> None:
        _UpperCamelCase					:		int  =				new_src_lang
        self.set_src_lang_special_tokens(self._src_lang		)

  def       __SCREAMING_SNAKE_CASE (						self      :				Optional[int]    ,      __a      :				List[int]    ,      __a      :				Optional[List[int]] = None		)						-> List[int]:
        if token_ids_a is None:
              return self.prefix_tokens + token_ids_a + self.suffix_tokens
        # We don't expect to process pairs, but leave the pair logic for API consistency
        return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens

  def       __SCREAMING_SNAKE_CASE (						self      :				Any    ,      __a      :				List[str]    ,      __a      :				str = "en_XX"    ,      __a      :				Optional[List[str]] = None    ,      __a      :				str = "ro_RO"    ,      **__a      :				Tuple    ,      )						-> BatchEncoding:
        _UpperCamelCase					:		List[Any]  =				src_lang
        _UpperCamelCase					:		Union[str, Any]  =				tgt_lang
        return super().prepare_seqaseq_batch(__a    ,      __a    ,      **__a		)

  def       __SCREAMING_SNAKE_CASE (						self      :				List[str]		)						-> int:
        return self.set_src_lang_special_tokens(self.src_lang		)

  def       __SCREAMING_SNAKE_CASE (						self      :				Tuple		)						-> Tuple:
        return self.set_tgt_lang_special_tokens(self.tgt_lang		)

  def       __SCREAMING_SNAKE_CASE (						self      :				List[str]    ,      __a      :				str		)						-> None:
        _UpperCamelCase					:		List[Any]  =				self.convert_tokens_to_ids(__a		)
        _UpperCamelCase					:		Union[str, Any]  =				[self.cur_lang_code_id]
        _UpperCamelCase					:		List[Any]  =				[self.eos_token_id]

        _UpperCamelCase					:		Union[str, Any]  =				self.convert_ids_to_tokens(self.prefix_tokens		)
        _UpperCamelCase					:		Union[str, Any]  =				self.convert_ids_to_tokens(self.suffix_tokens		)

        _UpperCamelCase					:		Optional[Any]  =				processors.TemplateProcessing(
            single=prefix_tokens_str + ["$A"] + suffix_tokens_str    ,      pair=prefix_tokens_str + ["$A", "$B"] + suffix_tokens_str    ,      special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str    ,      self.prefix_tokens + self.suffix_tokens		)		)    ,      )

  def       __SCREAMING_SNAKE_CASE (						self      :				Dict    ,      __a      :				str		)						-> None:
        _UpperCamelCase					:		Tuple  =				self.convert_tokens_to_ids(__a		)
        _UpperCamelCase					:		Union[str, Any]  =				[self.cur_lang_code_id]
        _UpperCamelCase					:		Optional[Any]  =				[self.eos_token_id]

        _UpperCamelCase					:		int  =				self.convert_ids_to_tokens(self.prefix_tokens		)
        _UpperCamelCase					:		Any  =				self.convert_ids_to_tokens(self.suffix_tokens		)

        _UpperCamelCase					:		Tuple  =				processors.TemplateProcessing(
            single=prefix_tokens_str + ["$A"] + suffix_tokens_str    ,      pair=prefix_tokens_str + ["$A", "$B"] + suffix_tokens_str    ,      special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str    ,      self.prefix_tokens + self.suffix_tokens		)		)    ,      )

  def       __SCREAMING_SNAKE_CASE (						self      :				Dict    ,      __a      :				Optional[int]    ,      __a      :				str    ,      __a      :				Optional[str]    ,      __a      :				Optional[str]    ,      **__a      :				int		)						-> List[str]:
        if src_lang is None or tgt_lang is None:
              raise ValueError("Translation requires a `src_lang` and a `tgt_lang` for this model"		)
        _UpperCamelCase					:		List[str]  =				src_lang
        _UpperCamelCase					:		List[str]  =				self(__a    ,      add_special_tokens=__a    ,      return_tensors=__a    ,      **__a		)
        _UpperCamelCase					:		str  =				self.convert_tokens_to_ids(__a		)
        _UpperCamelCase					:		str  =				tgt_lang_id
        return inputs


  def       __SCREAMING_SNAKE_CASE (						self      :				Union[str, Any]    ,      __a      :				str    ,      __a      :				Optional[str] = None		)						-> Tuple[str]:
        if not self.can_save_slow_tokenizer:
              raise ValueError(
                  "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow "
                  "tokenizer."		)

        if not os.path.isdir(__a		):
              logger.error(F'''Vocabulary path ({save_directory}) should be a directory'''		)
              return
        _UpperCamelCase					:		int  =				os.path.join(
            __a    ,      (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]		)

        if os.path.abspath(self.vocab_file		) != os.path.abspath(__a		):
              copyfile(self.vocab_file    ,      __a		)

        return (out_vocab_file,)
