



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

class 	_lowerCAmelCase     (    nn.Module       ):






		'''simple docstring'''
		snake_case_							= 42
		snake_case_							= jnp.floataa
		def 						__lowercase      (						self							:       str		)      ->			Optional[Any]:







									'''simple docstring'''





									_lowercase							:		Optional[int]				=   self.config.text_config
									_lowercase							:		int				=   self.config.vision_config

									_lowercase							:		Any				=   self.config.projection_dim
									_lowercase							:		str				=   text_config.hidden_size
									_lowercase							:		Optional[Any]				=   vision_config.hidden_size

									_lowercase							:		List[str]				=   FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
									_lowercase							:		Dict				=   FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__      ,		UpperCamelCase_		).module_class

									_lowercase							:		Dict				=   text_module(UpperCamelCase_      ,		dtype=self.dtype		)
									_lowercase							:		List[Any]				=   vision_module(UpperCamelCase_      ,		dtype=self.dtype		)

									_lowercase							:		str				=   nn.Dense(
									    self.projection_dim      ,		dtype=self.dtype      ,		kernel_init=jax.nn.initializers.normal(0.02		)      ,		use_bias=UpperCamelCase_      ,		)
									_lowercase							:		Dict				=   nn.Dense(
									    self.projection_dim      ,		dtype=self.dtype      ,		kernel_init=jax.nn.initializers.normal(0.02		)      ,		use_bias=UpperCamelCase_      ,		)
									_lowercase							:		List[str]				=   self.param('''logit_scale'''      ,		jax.nn.initializers.ones      ,		[]		)





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







									'''simple docstring'''





									_lowercase							:		Dict				=   return_dict if return_dict is not None else self.config.return_dict

									_lowercase							:		int				=   self.vision_model(
									    pixel_values=UpperCamelCase_      ,		deterministic=UpperCamelCase_      ,		output_attentions=UpperCamelCase_      ,		output_hidden_states=UpperCamelCase_      ,		return_dict=UpperCamelCase_      ,		)

									_lowercase							:		Dict				=   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_      ,		)

									_lowercase							:		Any				=   vision_outputs[1]
									_lowercase							:		Tuple				=   self.visual_projection(UpperCamelCase_		)

									_lowercase							:		Any				=   text_outputs[1]
									_lowercase							:		Tuple				=   self.text_projection(UpperCamelCase_		)

									# normalized features
									_lowercase							:		Union[str, Any]				=   image_embeds / jnp.linalg.norm(UpperCamelCase_      ,		axis=-1      ,		keepdims=UpperCamelCase_		)
									_lowercase							:		int				=   text_embeds / jnp.linalg.norm(UpperCamelCase_      ,		axis=-1      ,		keepdims=UpperCamelCase_		)

									# cosine similarity as logits
									_lowercase							:		List[Any]				=   jnp.exp(self.logit_scale		)
									_lowercase							:		Any				=   jnp.matmul(UpperCamelCase_      ,		image_embeds.T		) * logit_scale
									_lowercase							:		str				=   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 	_lowerCAmelCase     (    __A       ):






		'''simple docstring'''
		snake_case_							= HybridCLIPConfig
		snake_case_							= FlaxHybridCLIPModule
		def __init__(						self							:       str      ,		UpperCamelCase_							:       HybridCLIPConfig      ,		UpperCamelCase_							:       Optional[Tuple] = None      ,		UpperCamelCase_							:       int = 0      ,		UpperCamelCase_							:       jnp.dtype = jnp.floataa      ,		**UpperCamelCase_							:       str      ,		)      ->			str:







									'''simple docstring'''





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

									_lowercase							:		Optional[int]				=   self.module_class(config=UpperCamelCase_      ,		dtype=UpperCamelCase_      ,		**UpperCamelCase_		)
									super().__init__(UpperCamelCase_      ,		UpperCamelCase_      ,		input_shape=UpperCamelCase_      ,		seed=UpperCamelCase_      ,		dtype=UpperCamelCase_		)
		def 						__lowercase      (						self							:       Optional[int]      ,		UpperCamelCase_							:       jax.random.PRNGKey      ,		UpperCamelCase_							:       Tuple      ,		UpperCamelCase_							:       FrozenDict = None		)      ->			FrozenDict:







									'''simple docstring'''





									_lowercase							:		Any				=   jnp.zeros(input_shape[0]      ,		dtype='''i4'''		)
									_lowercase							:		Tuple				=   jnp.broadcast_to(jnp.arange(jnp.atleast_ad(UpperCamelCase_		).shape[-1]		)      ,		input_shape[0]		)
									_lowercase							:		int				=   jnp.ones_like(UpperCamelCase_		)
									_lowercase							:		Optional[Any]				=   jnp.ones_like(UpperCamelCase_		)

									_lowercase							:		List[str]				=   jax.random.normal(UpperCamelCase_      ,		input_shape[1]		)

									_lowercase			, _lowercase							:		List[str]				=   jax.random.split(UpperCamelCase_		)
									_lowercase							:		Tuple				=   {'''params''': params_rng, '''dropout''': dropout_rng}

									return self.module.init(UpperCamelCase_      ,		UpperCamelCase_      ,		UpperCamelCase_      ,		UpperCamelCase_      ,		UpperCamelCase_      ,		UpperCamelCase_		)["params"]
		def __call__(						self							:       List[str]      ,		UpperCamelCase_							:       str      ,		UpperCamelCase_							:       Tuple      ,		UpperCamelCase_							:       Optional[int]=None      ,		UpperCamelCase_							:       List[Any]=None      ,		UpperCamelCase_							:       int=None      ,		UpperCamelCase_							:       dict = None      ,		UpperCamelCase_							:       jax.random.PRNGKey = None      ,		UpperCamelCase_							:       bool = False      ,		UpperCamelCase_							:       Optional[bool] = None      ,		UpperCamelCase_							:       Optional[bool] = None      ,		UpperCamelCase_							:       Optional[bool] = None      ,		)      ->			List[Any]:







									'''simple docstring'''





									_lowercase							:		int				=   output_attentions if output_attentions is not None else self.config.output_attentions
									_lowercase							:		int				=   (
									    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
									)
									_lowercase							:		Tuple				=   return_dict if return_dict is not None else self.config.return_dict

									if position_ids is None:
																_lowercase							:		Optional[Any]				=   jnp.broadcast_to(jnp.arange(jnp.atleast_ad(UpperCamelCase_		).shape[-1]		)      ,		input_ids.shape		)

									if token_type_ids is None:
																_lowercase							:		List[str]				=   jnp.zeros_like(UpperCamelCase_		)

									if attention_mask is None:
																_lowercase							:		Optional[Any]				=   jnp.ones_like(UpperCamelCase_		)

									# Handle any PRNG if needed
									_lowercase							:		Optional[Any]				=   {}
									if dropout_rng is not None:
																_lowercase							:		Optional[Any]				=   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 						__lowercase      (						self							:       Any      ,		UpperCamelCase_							:       int      ,		UpperCamelCase_							:       Any=None      ,		UpperCamelCase_							:       List[str]=None      ,		UpperCamelCase_							:       Dict=None      ,		UpperCamelCase_							:       dict = None      ,		UpperCamelCase_							:       jax.random.PRNGKey = None      ,		UpperCamelCase_							:       int=False      ,		)      ->			Dict:







									'''simple docstring'''





									if position_ids is None:
																_lowercase							:		Optional[int]				=   jnp.broadcast_to(jnp.arange(jnp.atleast_ad(UpperCamelCase_		).shape[-1]		)      ,		input_ids.shape		)

									if token_type_ids is None:
																_lowercase							:		Union[str, Any]				=   jnp.zeros_like(UpperCamelCase_		)

									if attention_mask is None:
																_lowercase							:		Any				=   jnp.ones_like(UpperCamelCase_		)

									# Handle any PRNG if needed
									_lowercase							:		List[Any]				=   {}
									if dropout_rng is not None:
																_lowercase							:		List[Any]				=   dropout_rng

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







									'''simple docstring'''





									_lowercase							:		Optional[int]				=   {}
									if dropout_rng is not None:
																_lowercase							:		int				=   dropout_rng

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







									'''simple docstring'''





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

									_lowercase							:		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
									_lowercase							:		int				=   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

																							_lowercase							:		Any				=   AutoConfig.from_pretrained(UpperCamelCase_		)
																							_lowercase							:		Optional[Any]				=   text_config

																_lowercase							:		Any				=   FlaxAutoModel.from_pretrained(UpperCamelCase_      ,		*UpperCamelCase_      ,		**UpperCamelCase_		)

									_lowercase							:		List[str]				=   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

																							_lowercase							:		Any				=   AutoConfig.from_pretrained(UpperCamelCase_		)
																							_lowercase							:		Optional[Any]				=   vision_config

																_lowercase							:		str				=   FlaxAutoModel.from_pretrained(UpperCamelCase_      ,		*UpperCamelCase_      ,		**UpperCamelCase_		)

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

									# init model
									_lowercase							:		Tuple				=   cls(UpperCamelCase_      ,		*UpperCamelCase_      ,		dtype=UpperCamelCase_      ,		**UpperCamelCase_		)

									if vision_config.model_type == "clip":
																_lowercase							:		List[Any]				=   vision_model.params['''vision_model''']
																_lowercase							:		int				=   vision_model.params['''visual_projection''']['''kernel''']
									else:
																_lowercase							:		int				=   vision_model.params

									_lowercase							:		Union[str, Any]				=   text_model.params

									return model








'''simple docstring'''






import math as m





def 				_SCREAMING_SNAKE_CASE(					snake_case_    :     Union[str, Any] ,       snake_case_    :     int ,       snake_case_    :     Optional[Any]=0.0_0_1  )		->Tuple:






							'''simple docstring'''
							return (f(a + h  ) - f(a - h  )) / (2 * h)





def 				_SCREAMING_SNAKE_CASE(					snake_case_    :     Dict ,       snake_case_    :     List[Any]=0 ,       snake_case_    :     Dict=1_00 ,       snake_case_    :     List[str]=0.0_0_0_1 ,       snake_case_    :     str=1E-6 ,       snake_case_    :     Tuple=False  )		->List[Any]:






							'''simple docstring'''
							_lowercase							:		Tuple				=   xa  # set the initial guess
							_lowercase							:		List[str]				=   [a]
							_lowercase							:		str				=   abs(f(snake_case_  )  )
							_lowercase							:		Dict				=   lambda snake_case_  : calc_derivative(snake_case_ ,       snake_case_ ,       h=snake_case_  )  # noqa: E731  Derivative of f(x)
							for _ in range(snake_case_  ):
														if fa(snake_case_  ) == 0:
																					raise ValueError('''No converging solution found'''  )
														_lowercase							:		Tuple				=   a - f(snake_case_  ) / fa(snake_case_  )  # Calculate the next estimate
														if logsteps:
																					steps.append(snake_case_  )
														if error < maxerror:
																					break
							else:
														raise ValueError('''Iteration limit reached, no converging solution found'''  )
							if logsteps:
														# If logstep is true, then log intermediate steps
														return a, error, steps
							return a, error


if __name__ == "__main__":
						from matplotlib import pyplot as plt

						lowerCamelCase__	  =		lambda x: m.tanh(x) ** 2 - m.exp(3 * x)  # noqa: E731
						lowerCamelCase__  ,      lowerCamelCase__  ,      lowerCamelCase__	  =		newton_raphson(
						    f, xa=10, maxiter=10_00, step=1e-6, logsteps=True
						)
						plt.plot([abs(f(x)) for x in steps])
						plt.xlabel('step')
						plt.ylabel('error')
						plt.show()
						print(f'''solution = {{{solution:f}}}, error = {{{error:f}}}''')



