





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__    :      Any       					=logging.get_logger(__name__)

class    __A  (    nn.Module     ):
				__A  =					42
				__A  =					jnp.floataa


				def _snake_case							(		self				):
								lowerCamelCase									=self.config.text_config
								lowerCamelCase									=self.config.vision_config

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

								lowerCamelCase									=FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
								lowerCamelCase									=FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__ ,			UpperCAmelCase_				).module_class

								lowerCamelCase									=text_module(UpperCAmelCase_ ,			dtype=self.dtype				)
								lowerCamelCase									=vision_module(UpperCAmelCase_ ,			dtype=self.dtype				)

								lowerCamelCase									=nn.Dense(
								    self.projection_dim ,			dtype=self.dtype ,			kernel_init=jax.nn.initializers.normal(0.0_2				) ,			use_bias=UpperCAmelCase_ ,			)
								lowerCamelCase									=nn.Dense(
								    self.projection_dim ,			dtype=self.dtype ,			kernel_init=jax.nn.initializers.normal(0.0_2				) ,			use_bias=UpperCAmelCase_ ,			)
								lowerCamelCase									=self.param("""logit_scale""" ,			jax.nn.initializers.ones ,			[]				)


				def __call__(		self ,			UpperCAmelCase_=None ,			UpperCAmelCase_=None ,			UpperCAmelCase_=None ,			UpperCAmelCase_=None ,			UpperCAmelCase_=None ,			UpperCAmelCase_ = True ,			UpperCAmelCase_=None ,			UpperCAmelCase_=None ,			UpperCAmelCase_=None ,			):
								lowerCamelCase									=return_dict if return_dict is not None else self.config.return_dict

								lowerCamelCase									=self.vision_model(
								    pixel_values=UpperCAmelCase_ ,			deterministic=UpperCAmelCase_ ,			output_attentions=UpperCAmelCase_ ,			output_hidden_states=UpperCAmelCase_ ,			return_dict=UpperCAmelCase_ ,			)

								lowerCamelCase									=self.text_model(
								    input_ids=UpperCAmelCase_ ,			attention_mask=UpperCAmelCase_ ,			token_type_ids=UpperCAmelCase_ ,			position_ids=UpperCAmelCase_ ,			deterministic=UpperCAmelCase_ ,			output_attentions=UpperCAmelCase_ ,			output_hidden_states=UpperCAmelCase_ ,			return_dict=UpperCAmelCase_ ,			)

								lowerCamelCase									=vision_outputs[1]
								lowerCamelCase									=self.visual_projection(UpperCAmelCase_				)

								lowerCamelCase									=text_outputs[1]
								lowerCamelCase									=self.text_projection(UpperCAmelCase_				)

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

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




class    __A  (    _UpperCAmelCase     ):
				__A  =					HybridCLIPConfig
				__A  =					FlaxHybridCLIPModule


				def __init__(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ = None ,			UpperCAmelCase_ = 0 ,			UpperCAmelCase_ = jnp.floataa ,			**UpperCAmelCase_ ,			):




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

								lowerCamelCase									=self.module_class(config=UpperCAmelCase_ ,			dtype=UpperCAmelCase_ ,			**UpperCAmelCase_				)
								super().__init__(UpperCAmelCase_ ,			UpperCAmelCase_ ,			input_shape=UpperCAmelCase_ ,			seed=UpperCAmelCase_ ,			dtype=UpperCAmelCase_				)


				def _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ = None				):
								lowerCamelCase									=jnp.zeros(input_shape[0] ,			dtype="""i4"""				)
								lowerCamelCase									=jnp.broadcast_to(jnp.arange(jnp.atleast_ad(UpperCAmelCase_				).shape[-1]				) ,			input_shape[0]				)
								lowerCamelCase									=jnp.ones_like(UpperCAmelCase_				)
								lowerCamelCase									=jnp.ones_like(UpperCAmelCase_				)

								lowerCamelCase									=jax.random.normal(UpperCAmelCase_ ,			input_shape[1]				)

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

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


				def __call__(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_=None ,			UpperCAmelCase_=None ,			UpperCAmelCase_=None ,			UpperCAmelCase_ = None ,			UpperCAmelCase_ = None ,			UpperCAmelCase_ = False ,			UpperCAmelCase_ = None ,			UpperCAmelCase_ = None ,			UpperCAmelCase_ = None ,			):
								lowerCamelCase									=output_attentions if output_attentions is not None else self.config.output_attentions
								lowerCamelCase									=(
								    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
								)
								lowerCamelCase									=return_dict if return_dict is not None else self.config.return_dict

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

								if token_type_ids is None:
												lowerCamelCase									=jnp.zeros_like(UpperCAmelCase_				)

								if attention_mask is None:
												lowerCamelCase									=jnp.ones_like(UpperCAmelCase_				)

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

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


				def _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_=None ,			UpperCAmelCase_=None ,			UpperCAmelCase_=None ,			UpperCAmelCase_ = None ,			UpperCAmelCase_ = None ,			UpperCAmelCase_=False ,			):




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

								if token_type_ids is None:
												lowerCamelCase									=jnp.zeros_like(UpperCAmelCase_				)

								if attention_mask is None:
												lowerCamelCase									=jnp.ones_like(UpperCAmelCase_				)

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

								def _get_features(UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_				):
												lowerCamelCase									=module.text_model(
												    input_ids=UpperCAmelCase_ ,			attention_mask=UpperCAmelCase_ ,			position_ids=UpperCAmelCase_ ,			token_type_ids=UpperCAmelCase_ ,			deterministic=UpperCAmelCase_ ,			)
												lowerCamelCase									=text_outputs[1]
												lowerCamelCase									=module.text_projection(UpperCAmelCase_				)
												return text_features

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


				def _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ = None ,			UpperCAmelCase_ = None ,			UpperCAmelCase_=False				):
								lowerCamelCase									={}
								if dropout_rng is not None:
												lowerCamelCase									=dropout_rng

								def _get_features(UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_				):
												lowerCamelCase									=module.vision_model(pixel_values=UpperCAmelCase_ ,			deterministic=UpperCAmelCase_				)
												lowerCamelCase									=vision_outputs[1]  # pooled_output
												lowerCamelCase									=module.visual_projection(UpperCAmelCase_				)
												return image_features

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


				@classmethod
				def _snake_case							(		cls ,			UpperCAmelCase_ = None ,			UpperCAmelCase_ = None ,			*UpperCAmelCase_ ,			**UpperCAmelCase_ ,			):
								lowerCamelCase									={
								    argument[len("""text_"""				) :]: value for argument, value in kwargs.items() if argument.startswith("""text_"""				)
								}

								lowerCamelCase									={
								    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									=kwargs_text.pop("""model""" ,			UpperCAmelCase_				)
								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									=AutoConfig.from_pretrained(UpperCAmelCase_				)
																lowerCamelCase									=text_config

												lowerCamelCase									=FlaxAutoModel.from_pretrained(UpperCAmelCase_ ,			*UpperCAmelCase_ ,			**UpperCAmelCase_				)

								lowerCamelCase									=kwargs_vision.pop("""model""" ,			UpperCAmelCase_				)
								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									=AutoConfig.from_pretrained(UpperCAmelCase_				)
																lowerCamelCase									=vision_config

												lowerCamelCase									=FlaxAutoModel.from_pretrained(UpperCAmelCase_ ,			*UpperCAmelCase_ ,			**UpperCAmelCase_				)

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

								# init model
								lowerCamelCase									=cls(UpperCAmelCase_ ,			*UpperCAmelCase_ ,			dtype=UpperCAmelCase_ ,			**UpperCAmelCase_				)

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

								lowerCamelCase									=text_model.params

								return model













from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
		if not (is_transformers_available() and is_torch_available() and is_transformers_version('''>=''', '''4.27.0''')):
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		from ...utils.dummy_torch_and_transformers_objects import (
		    AudioLDMPipeline,
		)
else:
		from .pipeline_audioldm import AudioLDMPipeline

