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


UpperCamelCase     					=     logging.get_logger(__name__)
class   UpperCAmelCase__				(    nn.Module			):


	"""simple docstring"""
	lowerCAmelCase__    :					HybridCLIPConfig
	lowerCAmelCase__    :					jnp.dtype						      =							jnp.floataa







	def       A    (			self )    ->	List[str]:
							a_							:			List[Any]     		=					self.config.text_config
							a_							:			str     		=					self.config.vision_config

							a_							:			Dict     		=					self.config.projection_dim
							a_							:			Union[str, Any]     		=					text_config.hidden_size
							a_							:			List[Any]     		=					vision_config.hidden_size

							a_							:			Dict     		=					FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
							a_							:			Union[str, Any]     		=					FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__       ,     _SCREAMING_SNAKE_CASE ).module_class

							a_							:			int     		=					text_module(_SCREAMING_SNAKE_CASE       ,     dtype=self.dtype )
							a_							:			Optional[Any]     		=					vision_module(_SCREAMING_SNAKE_CASE       ,     dtype=self.dtype )

							a_							:			Optional[int]     		=					nn.Dense(
							    self.projection_dim       ,     dtype=self.dtype       ,     kernel_init=jax.nn.initializers.normal(0.0_2 )       ,     use_bias=_SCREAMING_SNAKE_CASE       ,     )
							a_							:			Dict     		=					nn.Dense(
							    self.projection_dim       ,     dtype=self.dtype       ,     kernel_init=jax.nn.initializers.normal(0.0_2 )       ,     use_bias=_SCREAMING_SNAKE_CASE       ,     )
							a_							:			Optional[Any]     		=					self.param("logit_scale"       ,     jax.nn.initializers.ones       ,     [] )







	def __call__(			self       ,     _SCREAMING_SNAKE_CASE=None       ,     _SCREAMING_SNAKE_CASE=None       ,     _SCREAMING_SNAKE_CASE=None       ,     _SCREAMING_SNAKE_CASE=None       ,     _SCREAMING_SNAKE_CASE=None       ,     _SCREAMING_SNAKE_CASE = True       ,     _SCREAMING_SNAKE_CASE=None       ,     _SCREAMING_SNAKE_CASE=None       ,     _SCREAMING_SNAKE_CASE=None       ,     )    ->	Union[str, Any]:
							a_							:			str     		=					return_dict if return_dict is not None else self.config.return_dict

							a_							:			List[str]     		=					self.vision_model(
							    pixel_values=_SCREAMING_SNAKE_CASE       ,     deterministic=_SCREAMING_SNAKE_CASE       ,     output_attentions=_SCREAMING_SNAKE_CASE       ,     output_hidden_states=_SCREAMING_SNAKE_CASE       ,     return_dict=_SCREAMING_SNAKE_CASE       ,     )

							a_							:			List[Any]     		=					self.text_model(
							    input_ids=_SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     position_ids=_SCREAMING_SNAKE_CASE       ,     deterministic=_SCREAMING_SNAKE_CASE       ,     output_attentions=_SCREAMING_SNAKE_CASE       ,     output_hidden_states=_SCREAMING_SNAKE_CASE       ,     return_dict=_SCREAMING_SNAKE_CASE       ,     )

							a_							:			str     		=					vision_outputs[1]
							a_							:			List[str]     		=					self.visual_projection(_SCREAMING_SNAKE_CASE )

							a_							:			List[str]     		=					text_outputs[1]
							a_							:			int     		=					self.text_projection(_SCREAMING_SNAKE_CASE )

							# normalized features
							a_							:			Optional[Any]     		=					image_embeds / jnp.linalg.norm(_SCREAMING_SNAKE_CASE       ,     axis=-1       ,     keepdims=_SCREAMING_SNAKE_CASE )
							a_							:			Tuple     		=					text_embeds / jnp.linalg.norm(_SCREAMING_SNAKE_CASE       ,     axis=-1       ,     keepdims=_SCREAMING_SNAKE_CASE )

							# cosine similarity as logits
							a_							:			str     		=					jnp.exp(self.logit_scale )
							a_							:			Optional[Any]     		=					jnp.matmul(_SCREAMING_SNAKE_CASE       ,     image_embeds.T ) * logit_scale
							a_							:			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=_SCREAMING_SNAKE_CASE       ,     logits_per_text=_SCREAMING_SNAKE_CASE       ,     text_embeds=_SCREAMING_SNAKE_CASE       ,     image_embeds=_SCREAMING_SNAKE_CASE       ,     text_model_output=_SCREAMING_SNAKE_CASE       ,     vision_model_output=_SCREAMING_SNAKE_CASE       ,     )




class   UpperCAmelCase__				(    __lowerCamelCase			):


	"""simple docstring"""
	lowerCAmelCase__    :					Optional[Any]						      =							HybridCLIPConfig
	lowerCAmelCase__    :					List[str]						      =							FlaxHybridCLIPModule







	def __init__(			self       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE = None       ,     _SCREAMING_SNAKE_CASE = 0       ,     _SCREAMING_SNAKE_CASE = jnp.floataa       ,     **_SCREAMING_SNAKE_CASE       ,     )    ->	Tuple:
							if input_shape is None:
													a_							:			str     		=					((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

							a_							:			List[Any]     		=					self.module_class(config=_SCREAMING_SNAKE_CASE       ,     dtype=_SCREAMING_SNAKE_CASE       ,     **_SCREAMING_SNAKE_CASE )
							super().__init__(_SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     input_shape=_SCREAMING_SNAKE_CASE       ,     seed=_SCREAMING_SNAKE_CASE       ,     dtype=_SCREAMING_SNAKE_CASE )







	def       A    (			self       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE = None )    ->	FrozenDict:
							# init input tensor
							a_							:			List[Any]     		=					jnp.zeros(input_shape[0]       ,     dtype="i4" )
							a_							:			Optional[Any]     		=					jnp.broadcast_to(jnp.arange(jnp.atleast_ad(_SCREAMING_SNAKE_CASE ).shape[-1] )       ,     input_shape[0] )
							a_							:			Dict     		=					jnp.ones_like(_SCREAMING_SNAKE_CASE )
							a_							:			Optional[int]     		=					jnp.ones_like(_SCREAMING_SNAKE_CASE )

							a_							:			Union[str, Any]     		=					jax.random.normal(_SCREAMING_SNAKE_CASE       ,     input_shape[1] )

							a_		,					a_							:			Tuple     		=					jax.random.split(_SCREAMING_SNAKE_CASE )
							a_							:			List[str]     		=					{"params": params_rng, "dropout": dropout_rng}

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







	def __call__(			self       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE=None       ,     _SCREAMING_SNAKE_CASE=None       ,     _SCREAMING_SNAKE_CASE=None       ,     _SCREAMING_SNAKE_CASE = None       ,     _SCREAMING_SNAKE_CASE = None       ,     _SCREAMING_SNAKE_CASE = False       ,     _SCREAMING_SNAKE_CASE = None       ,     _SCREAMING_SNAKE_CASE = None       ,     _SCREAMING_SNAKE_CASE = None       ,     )    ->	Union[str, Any]:
							a_							:			Optional[Any]     		=					output_attentions if output_attentions is not None else self.config.output_attentions
							a_							:			List[Any]     		=					(
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							a_							:			int     		=					return_dict if return_dict is not None else self.config.return_dict

							if position_ids is None:
													a_							:			Union[str, Any]     		=					jnp.broadcast_to(jnp.arange(jnp.atleast_ad(_SCREAMING_SNAKE_CASE ).shape[-1] )       ,     input_ids.shape )

							if token_type_ids is None:
													a_							:			str     		=					jnp.zeros_like(_SCREAMING_SNAKE_CASE )

							if attention_mask is None:
													a_							:			Dict     		=					jnp.ones_like(_SCREAMING_SNAKE_CASE )

							# Handle any PRNG if needed
							a_							:			Tuple     		=					{}
							if dropout_rng is not None:
													a_							:			str     		=					dropout_rng

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







	def       A    (			self       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE=None       ,     _SCREAMING_SNAKE_CASE=None       ,     _SCREAMING_SNAKE_CASE=None       ,     _SCREAMING_SNAKE_CASE = None       ,     _SCREAMING_SNAKE_CASE = None       ,     _SCREAMING_SNAKE_CASE=False       ,     )    ->	Dict:
							if position_ids is None:
													a_							:			Tuple     		=					jnp.broadcast_to(jnp.arange(jnp.atleast_ad(_SCREAMING_SNAKE_CASE ).shape[-1] )       ,     input_ids.shape )

							if token_type_ids is None:
													a_							:			Optional[int]     		=					jnp.zeros_like(_SCREAMING_SNAKE_CASE )

							if attention_mask is None:
													a_							:			Union[str, Any]     		=					jnp.ones_like(_SCREAMING_SNAKE_CASE )

							# Handle any PRNG if needed
							a_							:			Union[str, Any]     		=					{}
							if dropout_rng is not None:
													a_							:			Tuple     		=					dropout_rng

							def _get_features(_SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE ):
													a_							:			Optional[int]     		=					module.text_model(
													    input_ids=_SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     position_ids=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     deterministic=_SCREAMING_SNAKE_CASE       ,     )
													a_							:			Dict     		=					text_outputs[1]
													a_							:			int     		=					module.text_projection(_SCREAMING_SNAKE_CASE )
													return text_features

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







	def       A    (			self       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE = None       ,     _SCREAMING_SNAKE_CASE = None       ,     _SCREAMING_SNAKE_CASE=False )    ->	List[str]:
							a_							:			Any     		=					{}
							if dropout_rng is not None:
													a_							:			Any     		=					dropout_rng

							def _get_features(_SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE ):
													a_							:			Tuple     		=					module.vision_model(pixel_values=_SCREAMING_SNAKE_CASE       ,     deterministic=_SCREAMING_SNAKE_CASE )
													a_							:			List[str]     		=					vision_outputs[1]  # pooled_output
													a_							:			Union[str, Any]     		=					module.visual_projection(_SCREAMING_SNAKE_CASE )
													return image_features

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







	@classmethod
	def       A    (			cls       ,     _SCREAMING_SNAKE_CASE = None       ,     _SCREAMING_SNAKE_CASE = None       ,     *_SCREAMING_SNAKE_CASE       ,     **_SCREAMING_SNAKE_CASE       ,     )    ->	FlaxPreTrainedModel:
							a_							:			Any     		=					{
							    argument[len("text_" ) :]: value for argument, value in kwargs.items() if argument.startswith("text_" )
							}

							a_							:			Dict     		=					{
							    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_							:			str     		=					kwargs_text.pop("model"       ,     _SCREAMING_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

																			a_							:			List[Any]     		=					AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE )
																			a_							:			Tuple     		=					text_config

													a_							:			Any     		=					FlaxAutoModel.from_pretrained(_SCREAMING_SNAKE_CASE       ,     *_SCREAMING_SNAKE_CASE       ,     **_SCREAMING_SNAKE_CASE )

							a_							:			Optional[Any]     		=					kwargs_vision.pop("model"       ,     _SCREAMING_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

																			a_							:			List[Any]     		=					AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE )
																			a_							:			Tuple     		=					vision_config

													a_							:			Tuple     		=					FlaxAutoModel.from_pretrained(_SCREAMING_SNAKE_CASE       ,     *_SCREAMING_SNAKE_CASE       ,     **_SCREAMING_SNAKE_CASE )

							# instantiate config with corresponding kwargs
							a_							:			List[str]     		=					kwargs.pop("dtype"       ,     jnp.floataa )
							a_							:			Tuple     		=					HybridCLIPConfig.from_text_vision_configs(text_model.config       ,     vision_model.config       ,     **_SCREAMING_SNAKE_CASE )

							# init model
							a_							:			List[Any]     		=					cls(_SCREAMING_SNAKE_CASE       ,     *_SCREAMING_SNAKE_CASE       ,     dtype=_SCREAMING_SNAKE_CASE       ,     **_SCREAMING_SNAKE_CASE )

							if vision_config.model_type == "clip":
													a_							:			Any     		=					vision_model.params["vision_model"]
													a_							:			str     		=					vision_model.params["visual_projection"]["kernel"]
							else:
													a_							:			Dict     		=					vision_model.params

							a_							:			Any     		=					text_model.params

							return model





"""simple docstring"""






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


UpperCamelCase     					=     {
    'configuration_bark': [
        'BARK_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'BarkCoarseConfig',
        'BarkConfig',
        'BarkFineConfig',
        'BarkSemanticConfig',
    ],
    'processing_bark': ['BarkProcessor'],
}

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCamelCase     					=     [
				    'BARK_PRETRAINED_MODEL_ARCHIVE_LIST',
				    'BarkFineModel',
				    'BarkSemanticModel',
				    'BarkCoarseModel',
				    'BarkModel',
				    'BarkPreTrainedModel',
				    'BarkCausalModel',
				]

if TYPE_CHECKING:
				from .configuration_bark import (
				    BARK_PRETRAINED_CONFIG_ARCHIVE_MAP,
				    BarkCoarseConfig,
				    BarkConfig,
				    BarkFineConfig,
				    BarkSemanticConfig,
				)
				from .processing_bark import BarkProcessor

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_bark import (
								    BARK_PRETRAINED_MODEL_ARCHIVE_LIST,
								    BarkCausalModel,
								    BarkCoarseModel,
								    BarkFineModel,
								    BarkModel,
								    BarkPreTrainedModel,
								    BarkSemanticModel,
								)

else:
				import sys

				UpperCamelCase     					=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




