




import json
import os
import re
import warnings
from pathlib import Path
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple, Union

import sentencepiece

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


lowercase_      :							Tuple    							=					logging.get_logger(__name__)

lowercase_      :							List[str]    							=					{
    'source_spm': 'source.spm',
    'target_spm': 'target.spm',
    'vocab': 'vocab.json',
    'target_vocab_file': 'target_vocab.json',
    'tokenizer_config_file': 'tokenizer_config.json',
}

lowercase_      :							int    							=					{
    'source_spm': {
        'Helsinki-NLP/opus-mt-en-de': 'https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/source.spm'
    },
    'target_spm': {
        'Helsinki-NLP/opus-mt-en-de': 'https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/target.spm'
    },
    'vocab': {
        'Helsinki-NLP/opus-mt-en-de': 'https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/vocab.json'
    },
    'tokenizer_config_file': {
        'Helsinki-NLP/opus-mt-en-de': (
            'https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/tokenizer_config.json'
        )
    },
}

lowercase_      :							Any    							=					{'Helsinki-NLP/opus-mt-en-de': 5_12}
lowercase_      :							int    							=					{}






class        __lowerCAmelCase					(			UpperCAmelCase__					):
			snake_case_ :					List[Any]						=	VOCAB_FILES_NAMES
			snake_case_ :					List[Any]						=	PRETRAINED_VOCAB_FILES_MAP
			snake_case_ :					List[Any]						=	PRETRAINED_INIT_CONFIGURATION
			snake_case_ :					Any						=	PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			snake_case_ :					str						=	["input_ids", "attention_mask"]
			snake_case_ :					List[str]						=	re.compile(">>.+<<"					)  # type: re.Pattern







			def __init__(			self       :     Optional[int]       ,       snake_case__       :     List[str]       ,       snake_case__       :     List[Any]       ,       snake_case__       :     str       ,       snake_case__       :     Union[str, Any]=None       ,       snake_case__       :     Dict=None       ,       snake_case__       :     Union[str, Any]=None       ,       snake_case__       :     List[str]="<unk>"       ,       snake_case__       :     List[Any]="</s>"       ,       snake_case__       :     List[Any]="<pad>"       ,       snake_case__       :     List[str]=512       ,       snake_case__       :     Optional[Dict[str, Any]] = None       ,       snake_case__       :     Optional[int]=False       ,       **snake_case__       :     Optional[int]       ,       ):




									"""simple docstring"""


									_UpperCAmelCase						=	{} if sp_model_kwargs is None else sp_model_kwargs

									super().__init__(
									    # bos_token=bos_token,  unused. Start decoding with config.decoder_start_token_id
									    source_lang=snake_case__       ,       target_lang=snake_case__       ,       unk_token=snake_case__       ,       eos_token=snake_case__       ,       pad_token=snake_case__       ,       model_max_length=snake_case__       ,       sp_model_kwargs=self.sp_model_kwargs       ,       target_vocab_file=snake_case__       ,       separate_vocabs=snake_case__       ,       **snake_case__       ,       )
									assert Path(snake_case__ ).exists(), F"""cannot find spm source {source_spm}"""

									_UpperCAmelCase						=	separate_vocabs
									_UpperCAmelCase						=	load_json(snake_case__ )
									if self.unk_token not in self.encoder:
															raise KeyError("<unk> token must be in vocab" )
									assert self.pad_token in self.encoder

									if separate_vocabs:
															_UpperCAmelCase						=	load_json(snake_case__ )
															_UpperCAmelCase						=	{v: k for k, v in self.target_encoder.items()}
															_UpperCAmelCase						=	[]
									else:
															_UpperCAmelCase						=	{v: k for k, v in self.encoder.items()}
															_UpperCAmelCase						=	[k for k in self.encoder if k.startswith(">>" ) and k.endswith("<<" )]

									_UpperCAmelCase						=	source_lang
									_UpperCAmelCase						=	target_lang
									_UpperCAmelCase						=	[source_spm, target_spm]

									# load SentencePiece model for pre-processing
									_UpperCAmelCase						=	load_spm(snake_case__       ,       self.sp_model_kwargs )
									_UpperCAmelCase						=	load_spm(snake_case__       ,       self.sp_model_kwargs )
									_UpperCAmelCase						=	self.spm_source
									_UpperCAmelCase						=	self.encoder

									# Multilingual target side: default to using first supported language code.

									self._setup_normalizer()







			def UpperCamelCase     (			self       :     int ):




									"""simple docstring"""


									try:
															from sacremoses import MosesPunctNormalizer

															_UpperCAmelCase						=	MosesPunctNormalizer(self.source_lang ).normalize
									except (ImportError, FileNotFoundError):
															warnings.warn("Recommended: pip install sacremoses." )
															_UpperCAmelCase						=	lambda snake_case__ : x







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




									"""simple docstring"""


									return self.punc_normalizer(snake_case__ ) if x else ""







			def UpperCamelCase     (			self       :     str       ,       snake_case__       :     Tuple ):




									"""simple docstring"""


									return self.current_encoder.get(snake_case__       ,       self.current_encoder[self.unk_token] )







			def UpperCamelCase     (			self       :     int       ,       snake_case__       :     str ):




									"""simple docstring"""


									_UpperCAmelCase						=	self.language_code_re.match(snake_case__ )
									_UpperCAmelCase						=	[match.group(0 )] if match else []
									return code, self.language_code_re.sub(""       ,       snake_case__ )







			def UpperCamelCase     (			self       :     Optional[int]       ,       snake_case__       :     str ):




									"""simple docstring"""


									_UpperCAmelCase				,					_UpperCAmelCase						=	self.remove_language_code(snake_case__ )
									_UpperCAmelCase						=	self.current_spm.encode(snake_case__       ,       out_type=snake_case__ )
									return code + pieces







			def UpperCamelCase     (			self       :     List[Any]       ,       snake_case__       :     int ):




									"""simple docstring"""


									return self.decoder.get(snake_case__       ,       self.unk_token )







			def UpperCamelCase     (			self       :     Union[str, Any]       ,       snake_case__       :     Union[str, Any]       ,       **snake_case__       :     Union[str, Any] ):




									"""simple docstring"""


									return super().batch_decode(snake_case__       ,       **snake_case__ )







			def UpperCamelCase     (			self       :     List[str]       ,       snake_case__       :     Tuple       ,       **snake_case__       :     List[Any] ):




									"""simple docstring"""


									return super().decode(snake_case__       ,       **snake_case__ )







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




									"""simple docstring"""


									_UpperCAmelCase						=	self.spm_source if self._decode_use_source_tokenizer else self.spm_target
									_UpperCAmelCase						=	[]
									_UpperCAmelCase						=	""
									for token in tokens:
															# make sure that special tokens are not decoded using sentencepiece model
															if token in self.all_special_tokens:
																					out_string += sp_model.decode_pieces(snake_case__ ) + token + " "
																					_UpperCAmelCase						=	[]
															else:
																					current_sub_tokens.append(snake_case__ )
									out_string += sp_model.decode_pieces(snake_case__ )
									return out_string.strip()







			def UpperCamelCase     (			self       :     Optional[Any]       ,       snake_case__       :     Union[str, Any]       ,       snake_case__       :     Any=None ):




									"""simple docstring"""


									if token_ids_a is None:
															return token_ids_a + [self.eos_token_id]
									# We don't expect to process pairs, but leave the pair logic for API consistency
									return token_ids_a + token_ids_a + [self.eos_token_id]







			def UpperCamelCase     (			self       :     Union[str, Any] ):




									"""simple docstring"""


									_UpperCAmelCase						=	self.spm_source
									_UpperCAmelCase						=	self.encoder







			def UpperCamelCase     (			self       :     Optional[Any] ):




									"""simple docstring"""


									_UpperCAmelCase						=	self.spm_target
									if self.separate_vocabs:
															_UpperCAmelCase						=	self.target_encoder







			@property
			def UpperCamelCase     (			self       :     int ):




									"""simple docstring"""


									return len(self.encoder )







			def UpperCamelCase     (			self       :     Any       ,       snake_case__       :     str       ,       snake_case__       :     Optional[str] = None ):




									"""simple docstring"""


									if not os.path.isdir(snake_case__ ):
															logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" )
															return
									_UpperCAmelCase						=	[]

									if self.separate_vocabs:
															_UpperCAmelCase						=	os.path.join(
															    snake_case__       ,       (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab"]       ,       )
															_UpperCAmelCase						=	os.path.join(
															    snake_case__       ,       (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["target_vocab_file"]       ,       )
															save_json(self.encoder       ,       snake_case__ )
															save_json(self.target_encoder       ,       snake_case__ )
															saved_files.append(snake_case__ )
															saved_files.append(snake_case__ )
									else:
															_UpperCAmelCase						=	os.path.join(
															    snake_case__       ,       (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab"] )
															save_json(self.encoder       ,       snake_case__ )
															saved_files.append(snake_case__ )

									for spm_save_filename, spm_orig_path, spm_model in zip(
									    [VOCAB_FILES_NAMES["source_spm"], VOCAB_FILES_NAMES["target_spm"]]       ,       self.spm_files       ,       [self.spm_source, self.spm_target]       ,       ):
															_UpperCAmelCase						=	os.path.join(
															    snake_case__       ,       (filename_prefix + "-" if filename_prefix else "") + spm_save_filename )
															if os.path.abspath(snake_case__ ) != os.path.abspath(snake_case__ ) and os.path.isfile(snake_case__ ):
																					copyfile(snake_case__       ,       snake_case__ )
																					saved_files.append(snake_case__ )
															elif not os.path.isfile(snake_case__ ):
																					with open(snake_case__       ,       "wb" ) as fi:
																											_UpperCAmelCase						=	spm_model.serialized_model_proto()
																											fi.write(snake_case__ )
																					saved_files.append(snake_case__ )

									return tuple(snake_case__ )







			def UpperCamelCase     (			self       :     Tuple ):




									"""simple docstring"""


									return self.get_src_vocab()







			def UpperCamelCase     (			self       :     List[Any] ):




									"""simple docstring"""


									return dict(self.encoder       ,       **self.added_tokens_encoder )







			def UpperCamelCase     (			self       :     Dict ):




									"""simple docstring"""


									return dict(self.target_encoder       ,       **self.added_tokens_decoder )







			def __getstate__(			self       :     Optional[Any] ):




									"""simple docstring"""


									_UpperCAmelCase						=	self.__dict__.copy()
									state.update(
									    {k: None for k in ["spm_source", "spm_target", "current_spm", "punc_normalizer", "target_vocab_file"]} )
									return state







			def __setstate__(			self       :     Dict       ,       snake_case__       :     Dict ):




									"""simple docstring"""


									_UpperCAmelCase						=	d

									# for backward compatibility
									if not hasattr(self       ,       "sp_model_kwargs" ):
															_UpperCAmelCase						=	{}

									_UpperCAmelCase				,					_UpperCAmelCase						=	(load_spm(snake_case__       ,       self.sp_model_kwargs ) for f in self.spm_files)
									_UpperCAmelCase						=	self.spm_source
									self._setup_normalizer()







			def UpperCamelCase     (			self       :     Tuple       ,       *snake_case__       :     int       ,       **snake_case__       :     Optional[int] ):




									"""simple docstring"""


									return 1







			def UpperCamelCase     (			self       :     str       ,       snake_case__       :     Dict ):




									"""simple docstring"""


									_UpperCAmelCase						=	set(self.all_special_ids )  # call it once instead of inside list comp
									all_special_ids.remove(self.unk_token_id )  # <unk> is only sometimes special
									return [1 if x in all_special_ids else 0 for x in seq]







			def UpperCamelCase     (			self       :     Optional[int]       ,       snake_case__       :     List       ,       snake_case__       :     Optional[List] = None       ,       snake_case__       :     bool = False ):




									"""simple docstring"""


									if already_has_special_tokens:
															return self._special_token_mask(snake_case__ )
									elif token_ids_a is None:
															return self._special_token_mask(snake_case__ ) + [1]
									else:
															return self._special_token_mask(token_ids_a + token_ids_a ) + [1]




def 				__SCREAMING_SNAKE_CASE    (     snake_case_      ,       snake_case_		):

						'''simple docstring'''


						_UpperCAmelCase						=	sentencepiece.SentencePieceProcessor(**snake_case_		)
						spm.Load(snake_case_		)
						return spm


def 				__SCREAMING_SNAKE_CASE    (     snake_case_      ,       snake_case_		):

						'''simple docstring'''


						with open(snake_case_      ,       "w"		) as f:
												json.dump(snake_case_      ,       snake_case_      ,       indent=2		)





def 				__SCREAMING_SNAKE_CASE    (     snake_case_		):

						'''simple docstring'''


						with open(snake_case_      ,       "r"		) as f:
												return json.load(snake_case_		)










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        __lowerCAmelCase					(			nn.Module					):
			snake_case_ :					HybridCLIPConfig
			snake_case_ :					jnp.dtype						=	jnp.floataa







			def UpperCamelCase     (			self       :     Union[str, Any] ):




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

									_UpperCAmelCase						=	text_module(snake_case__       ,       dtype=self.dtype )
									_UpperCAmelCase						=	vision_module(snake_case__       ,       dtype=self.dtype )

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







			def __call__(			self       :     List[Any]       ,       snake_case__       :     List[Any]=None       ,       snake_case__       :     List[str]=None       ,       snake_case__       :     Optional[Any]=None       ,       snake_case__       :     Dict=None       ,       snake_case__       :     Tuple=None       ,       snake_case__       :     bool = True       ,       snake_case__       :     Dict=None       ,       snake_case__       :     Dict=None       ,       snake_case__       :     Dict=None       ,       ):




									"""simple docstring"""


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

									_UpperCAmelCase						=	self.vision_model(
									    pixel_values=snake_case__       ,       deterministic=snake_case__       ,       output_attentions=snake_case__       ,       output_hidden_states=snake_case__       ,       return_dict=snake_case__       ,       )

									_UpperCAmelCase						=	self.text_model(
									    input_ids=snake_case__       ,       attention_mask=snake_case__       ,       token_type_ids=snake_case__       ,       position_ids=snake_case__       ,       deterministic=snake_case__       ,       output_attentions=snake_case__       ,       output_hidden_states=snake_case__       ,       return_dict=snake_case__       ,       )

									_UpperCAmelCase						=	vision_outputs[1]
									_UpperCAmelCase						=	self.visual_projection(snake_case__ )

									_UpperCAmelCase						=	text_outputs[1]
									_UpperCAmelCase						=	self.text_projection(snake_case__ )

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

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






class        __lowerCAmelCase					(			UpperCAmelCase__					):
			snake_case_ :					Union[str, Any]						=	HybridCLIPConfig
			snake_case_ :					Optional[Any]						=	FlaxHybridCLIPModule







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




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







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




									"""simple docstring"""


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

									_UpperCAmelCase						=	jax.random.normal(snake_case__       ,       input_shape[1] )

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

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







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




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

									if token_type_ids is None:
															_UpperCAmelCase						=	jnp.zeros_like(snake_case__ )

									if attention_mask is None:
															_UpperCAmelCase						=	jnp.ones_like(snake_case__ )

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







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




									"""simple docstring"""


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

									if token_type_ids is None:
															_UpperCAmelCase						=	jnp.zeros_like(snake_case__ )

									if attention_mask is None:
															_UpperCAmelCase						=	jnp.ones_like(snake_case__ )

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

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

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







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




									"""simple docstring"""


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

									def _get_features(snake_case__       :     Optional[Any]       ,       snake_case__       :     Tuple       ,       snake_case__       :     int ):
															_UpperCAmelCase						=	module.vision_model(pixel_values=snake_case__       ,       deterministic=snake_case__ )
															_UpperCAmelCase						=	vision_outputs[1]  # pooled_output
															_UpperCAmelCase						=	module.visual_projection(snake_case__ )
															return image_features

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







			@classmethod
			def UpperCamelCase     (			cls       :     Tuple       ,       snake_case__       :     str = None       ,       snake_case__       :     str = None       ,       *snake_case__       :     Any       ,       **snake_case__       :     Tuple       ,       ):




									"""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"       ,       snake_case__ )
									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(snake_case__ )
																					_UpperCAmelCase						=	text_config

															_UpperCAmelCase						=	FlaxAutoModel.from_pretrained(snake_case__       ,       *snake_case__       ,       **snake_case__ )

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

															_UpperCAmelCase						=	FlaxAutoModel.from_pretrained(snake_case__       ,       *snake_case__       ,       **snake_case__ )

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

									# init model
									_UpperCAmelCase						=	cls(snake_case__       ,       *snake_case__       ,       dtype=snake_case__       ,       **snake_case__ )

									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




