


'''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


_lowerCAmelCase	:		Tuple		  =						logging.get_logger(__name__)



class 			lowerCAmelCase					(					nn.Module				):
	_lowerCamelCase :       HybridCLIPConfig
	_lowerCamelCase :       jnp.dtype         =			jnp.floataa



	def 					lowercase  (       self     ):
		lowerCAmelCase       :     str   				=			self.config.text_config
		lowerCAmelCase       :     int   				=			self.config.vision_config

		lowerCAmelCase       :     str   				=			self.config.projection_dim
		lowerCAmelCase       :     Dict   				=			text_config.hidden_size
		lowerCAmelCase       :     List[str]   				=			vision_config.hidden_size

		lowerCAmelCase       :     Optional[Any]   				=			FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
		lowerCAmelCase       :     List[Any]   				=			FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__			,       snake_case__     ).module_class

		lowerCAmelCase       :     Union[str, Any]   				=			text_module(snake_case__			,       dtype=self.dtype     )
		lowerCAmelCase       :     List[Any]   				=			vision_module(snake_case__			,       dtype=self.dtype     )

		lowerCAmelCase       :     Dict   				=			nn.Dense(
		    self.projection_dim			,       dtype=self.dtype			,       kernel_init=jax.nn.initializers.normal(0.0_2     )			,       use_bias=snake_case__			,       )
		lowerCAmelCase       :     Dict   				=			nn.Dense(
		    self.projection_dim			,       dtype=self.dtype			,       kernel_init=jax.nn.initializers.normal(0.0_2     )			,       use_bias=snake_case__			,       )
		lowerCAmelCase       :     Union[str, Any]   				=			self.param('logit_scale'			,       jax.nn.initializers.ones			,       []     )



	def __call__(       self			,       snake_case__=None			,       snake_case__=None			,       snake_case__=None			,       snake_case__=None			,       snake_case__=None			,       snake_case__ = True			,       snake_case__=None			,       snake_case__=None			,       snake_case__=None			,       ):
		lowerCAmelCase       :     str   				=			return_dict if return_dict is not None else self.config.return_dict

		lowerCAmelCase       :     Union[str, Any]   				=			self.vision_model(
		    pixel_values=snake_case__			,       deterministic=snake_case__			,       output_attentions=snake_case__			,       output_hidden_states=snake_case__			,       return_dict=snake_case__			,       )

		lowerCAmelCase       :     Union[str, Any]   				=			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__			,       )

		lowerCAmelCase       :     Union[str, Any]   				=			vision_outputs[1]
		lowerCAmelCase       :     Tuple   				=			self.visual_projection(snake_case__     )

		lowerCAmelCase       :     List[str]   				=			text_outputs[1]
		lowerCAmelCase       :     List[Any]   				=			self.text_projection(snake_case__     )

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

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



	def __init__(       self			,       snake_case__			,       snake_case__ = None			,       snake_case__ = 0			,       snake_case__ = jnp.floataa			,       **snake_case__			,       ):
		if input_shape is None:
			lowerCAmelCase       :     str   				=			((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

		lowerCAmelCase       :     List[str]   				=			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 					lowercase  (       self			,       snake_case__			,       snake_case__			,       snake_case__ = None     ):
		# init input tensor
		lowerCAmelCase       :     Union[str, Any]   				=			jnp.zeros(input_shape[0]			,       dtype='i4'     )
		lowerCAmelCase       :     Union[str, Any]   				=			jnp.broadcast_to(jnp.arange(jnp.atleast_ad(snake_case__     ).shape[-1]     )			,       input_shape[0]     )
		lowerCAmelCase       :     List[str]   				=			jnp.ones_like(snake_case__     )
		lowerCAmelCase       :     Union[str, Any]   				=			jnp.ones_like(snake_case__     )

		lowerCAmelCase       :     Optional[int]   				=			jax.random.normal(snake_case__			,       input_shape[1]     )

		lowerCAmelCase				, lowerCAmelCase       :     Union[str, Any]   				=			jax.random.split(snake_case__     )
		lowerCAmelCase       :     Union[str, Any]   				=			{'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			,       snake_case__			,       snake_case__			,       snake_case__=None			,       snake_case__=None			,       snake_case__=None			,       snake_case__ = None			,       snake_case__ = None			,       snake_case__ = False			,       snake_case__ = None			,       snake_case__ = None			,       snake_case__ = None			,       ):
		lowerCAmelCase       :     Optional[int]   				=			output_attentions if output_attentions is not None else self.config.output_attentions
		lowerCAmelCase       :     str   				=			(
		    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
		)
		lowerCAmelCase       :     Tuple   				=			return_dict if return_dict is not None else self.config.return_dict

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

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

		if attention_mask is None:
			lowerCAmelCase       :     Optional[int]   				=			jnp.ones_like(snake_case__     )

		# Handle any PRNG if needed
		lowerCAmelCase       :     str   				=			{}
		if dropout_rng is not None:
			lowerCAmelCase       :     List[Any]   				=			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 					lowercase  (       self			,       snake_case__			,       snake_case__=None			,       snake_case__=None			,       snake_case__=None			,       snake_case__ = None			,       snake_case__ = None			,       snake_case__=False			,       ):
		if position_ids is None:
			lowerCAmelCase       :     Optional[int]   				=			jnp.broadcast_to(jnp.arange(jnp.atleast_ad(snake_case__     ).shape[-1]     )			,       input_ids.shape     )

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

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

		# Handle any PRNG if needed
		lowerCAmelCase       :     Tuple   				=			{}
		if dropout_rng is not None:
			lowerCAmelCase       :     List[str]   				=			dropout_rng

		def _get_features(snake_case__			,       snake_case__			,       snake_case__			,       snake_case__			,       snake_case__			,       snake_case__     ):
			lowerCAmelCase       :     List[Any]   				=			module.text_model(
			    input_ids=snake_case__			,       attention_mask=snake_case__			,       position_ids=snake_case__			,       token_type_ids=snake_case__			,       deterministic=snake_case__			,       )
			lowerCAmelCase       :     Optional[int]   				=			text_outputs[1]
			lowerCAmelCase       :     Any   				=			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 					lowercase  (       self			,       snake_case__			,       snake_case__ = None			,       snake_case__ = None			,       snake_case__=False     ):
		lowerCAmelCase       :     List[Any]   				=			{}
		if dropout_rng is not None:
			lowerCAmelCase       :     Optional[int]   				=			dropout_rng

		def _get_features(snake_case__			,       snake_case__			,       snake_case__     ):
			lowerCAmelCase       :     Optional[Any]   				=			module.vision_model(pixel_values=snake_case__			,       deterministic=snake_case__     )
			lowerCAmelCase       :     Tuple   				=			vision_outputs[1]  # pooled_output
			lowerCAmelCase       :     int   				=			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 					lowercase  (       cls			,       snake_case__ = None			,       snake_case__ = None			,       *snake_case__			,       **snake_case__			,       ):
		lowerCAmelCase       :     str   				=			{
		    argument[len('text_'     ) :]: value for argument, value in kwargs.items() if argument.startswith('text_'     )
		}

		lowerCAmelCase       :     Optional[Any]   				=			{
		    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
		lowerCAmelCase       :     List[str]   				=			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

				lowerCAmelCase       :     Optional[int]   				=			AutoConfig.from_pretrained(snake_case__     )
				lowerCAmelCase       :     List[str]   				=			text_config

			lowerCAmelCase       :     Optional[int]   				=			FlaxAutoModel.from_pretrained(snake_case__			,       *snake_case__			,       **snake_case__     )

		lowerCAmelCase       :     Dict   				=			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

				lowerCAmelCase       :     Union[str, Any]   				=			AutoConfig.from_pretrained(snake_case__     )
				lowerCAmelCase       :     List[str]   				=			vision_config

			lowerCAmelCase       :     List[str]   				=			FlaxAutoModel.from_pretrained(snake_case__			,       *snake_case__			,       **snake_case__     )

		# instantiate config with corresponding kwargs
		lowerCAmelCase       :     str   				=			kwargs.pop('dtype'			,       jnp.floataa     )
		lowerCAmelCase       :     List[str]   				=			HybridCLIPConfig.from_text_vision_configs(text_model.config			,       vision_model.config			,       **snake_case__     )

		# init model
		lowerCAmelCase       :     Dict   				=			cls(snake_case__			,       *snake_case__			,       dtype=snake_case__			,       **snake_case__     )

		if vision_config.model_type == "clip":
			lowerCAmelCase       :     str   				=			vision_model.params['vision_model']
			lowerCAmelCase       :     int   				=			vision_model.params['visual_projection']['kernel']
		else:
			lowerCAmelCase       :     Tuple   				=			vision_model.params

		lowerCAmelCase       :     Optional[Any]   				=			text_model.params

		return model




'''simple docstring'''




from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowerCAmelCase	:		List[str]		  =						logging.get_logger(__name__)

_lowerCAmelCase	:		int		  =						{
    'facebook/opt-125m': 'https://huggingface.co/facebook/opt-125m/blob/main/config.json',
    'facebook/opt-350m': 'https://huggingface.co/facebook/opt-350m/blob/main/config.json',
    'facebook/opt-1.3b': 'https://huggingface.co/facebook/opt-1.3b/blob/main/config.json',
    'facebook/opt-2.7b': 'https://huggingface.co/facebook/opt-2.7b/blob/main/config.json',
    'facebook/opt-6.7b': 'https://huggingface.co/facebook/opt-6.7b/blob/main/config.json',
    'facebook/opt-13b': 'https://huggingface.co/facebook/opt-13b/blob/main/config.json',
}



class 			lowerCAmelCase					(					a				):
	_lowerCamelCase :       Dict         =			"""opt"""
	_lowerCamelCase :       Optional[Any]         =			["""past_key_values"""]



	def __init__(       self			,       snake_case__=5_0272			,       snake_case__=768			,       snake_case__=12			,       snake_case__=3072			,       snake_case__=2048			,       snake_case__=True			,       snake_case__=False			,       snake_case__=None			,       snake_case__=0.1			,       snake_case__=0.0			,       snake_case__=12			,       snake_case__="relu"			,       snake_case__=0.0			,       snake_case__=0.0_2			,       snake_case__=True			,       snake_case__=1			,       snake_case__=2			,       snake_case__=2			,       snake_case__=True			,       snake_case__=True			,       **snake_case__			,       ):
		super().__init__(
		    pad_token_id=snake_case__			,       bos_token_id=snake_case__			,       eos_token_id=snake_case__			,       **snake_case__			,       )
		lowerCAmelCase       :     Union[str, Any]   				=			vocab_size
		lowerCAmelCase       :     List[Any]   				=			max_position_embeddings
		lowerCAmelCase       :     List[str]   				=			num_attention_heads
		lowerCAmelCase       :     Dict   				=			word_embed_proj_dim if word_embed_proj_dim is not None else hidden_size
		lowerCAmelCase       :     Optional[Any]   				=			ffn_dim
		lowerCAmelCase       :     Optional[int]   				=			hidden_size
		lowerCAmelCase       :     List[str]   				=			num_hidden_layers
		lowerCAmelCase       :     Union[str, Any]   				=			dropout
		lowerCAmelCase       :     List[str]   				=			attention_dropout
		lowerCAmelCase       :     Union[str, Any]   				=			activation_function
		lowerCAmelCase       :     Optional[Any]   				=			init_std
		lowerCAmelCase       :     List[str]   				=			layerdrop
		lowerCAmelCase       :     Dict   				=			use_cache
		lowerCAmelCase       :     List[Any]   				=			do_layer_norm_before
		# We keep these variables at `True` for backward compatibility.
		lowerCAmelCase       :     List[str]   				=			enable_bias
		lowerCAmelCase       :     Dict   				=			layer_norm_elementwise_affine

		# Note that the only purpose of `_remove_final_layer_norm` is to keep backward compatibility
		# with checkpoints that have been fine-tuned before transformers v4.20.1
		# see https://github.com/facebookresearch/metaseq/pull/164
		lowerCAmelCase       :     Tuple   				=			_remove_final_layer_norm
