




from collections.abc import Callable

UpperCamelCase     =				Callable[[float], float]  # type alias for a real -> real function
def 				SCREAMING_SNAKE_CASE(      __lowercase		,							__lowercase		,							__lowercase		,							) ->   float:
     A:		Union[str, Any]        =		float(__lowercase  )
     while True:
          try:
               A:		int        =		prev_guess - function(__lowercase  ) / derivative(__lowercase  )
          except ZeroDivisionError:
               raise ZeroDivisionError('''Could not find root'''  ) from None
          if abs(prev_guess - next_guess  ) < 1_0**-5:
               return next_guess
          A:		str        =		next_guess
def 				SCREAMING_SNAKE_CASE(      __lowercase  ) ->   float:
     return (x**3) - (2 * x) - 5
def 				SCREAMING_SNAKE_CASE(      __lowercase  ) ->   float:
     return 3 * (x**2) - 2


if __name__ == "__main__":
     print(newton(f, fa, 3))












'''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 	lowerCAmelCase_  (					nn.Module       ):







  '''simple docstring'''

  UpperCamelCase_ :			HybridCLIPConfig
  UpperCamelCase_ :			jnp.dtype        =							jnp.floataa



  def 				_snake_case    (   self							:       List[str]  ) ->		int:







       '''simple docstring'''



       A:		List[Any]        =		self.config.text_config
       A:		Any        =		self.config.vision_config

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

       A:		List[Any]        =		FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
       A:		int        =		FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__				,     SCREAMING_SNAKE_CASE_  ).module_class

       A:		str        =		text_module(SCREAMING_SNAKE_CASE_				,     dtype=self.dtype  )
       A:		Union[str, Any]        =		vision_module(SCREAMING_SNAKE_CASE_				,     dtype=self.dtype  )

       A:		Any        =		nn.Dense(
           self.projection_dim				,     dtype=self.dtype				,     kernel_init=jax.nn.initializers.normal(0.02  )				,     use_bias=SCREAMING_SNAKE_CASE_				,     )
       A:		Tuple        =		nn.Dense(
           self.projection_dim				,     dtype=self.dtype				,     kernel_init=jax.nn.initializers.normal(0.02  )				,     use_bias=SCREAMING_SNAKE_CASE_				,     )
       A:		Dict        =		self.param('''logit_scale'''				,     jax.nn.initializers.ones				,     []  )




  def __call__(   self							:       List[Any]				,     SCREAMING_SNAKE_CASE_							:       Dict=None				,     SCREAMING_SNAKE_CASE_							:       List[Any]=None				,     SCREAMING_SNAKE_CASE_							:       Optional[int]=None				,     SCREAMING_SNAKE_CASE_							:       Dict=None				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]=None				,     SCREAMING_SNAKE_CASE_							:       bool = True				,     SCREAMING_SNAKE_CASE_							:       Tuple=None				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]=None				,     SCREAMING_SNAKE_CASE_							:       Tuple=None				,     ) ->		Tuple:







       '''simple docstring'''



       A:		Optional[int]        =		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:		Tuple        =		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:		Any        =		self.visual_projection(SCREAMING_SNAKE_CASE_  )

       A:		Optional[int]        =		text_outputs[1]
       A:		str        =		self.text_projection(SCREAMING_SNAKE_CASE_  )

       # normalized features
       A:		Tuple        =		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:		Optional[int]        =		jnp.exp(self.logit_scale  )
       A:		Union[str, Any]        =		jnp.matmul(SCREAMING_SNAKE_CASE_				,     image_embeds.T  ) * logit_scale
       A:		Tuple        =		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 	lowerCAmelCase_  (					UpperCAmelCase_       ):







  '''simple docstring'''

  UpperCamelCase_ :			Union[str, Any]        =							HybridCLIPConfig
  UpperCamelCase_ :			List[str]        =							FlaxHybridCLIPModule



  def __init__(   self							:       Union[str, Any]				,     SCREAMING_SNAKE_CASE_							:       HybridCLIPConfig				,     SCREAMING_SNAKE_CASE_							:       Optional[Tuple] = None				,     SCREAMING_SNAKE_CASE_							:       int = 0				,     SCREAMING_SNAKE_CASE_							:       jnp.dtype = jnp.floataa				,     **SCREAMING_SNAKE_CASE_							:       str				,     ) ->		int:







       '''simple docstring'''



       if input_shape is None:
            A:		List[Any]        =		((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

       A:		int        =		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 				_snake_case    (   self							:       List[Any]				,     SCREAMING_SNAKE_CASE_							:       jax.random.PRNGKey				,     SCREAMING_SNAKE_CASE_							:       Tuple				,     SCREAMING_SNAKE_CASE_							:       FrozenDict = None  ) ->		FrozenDict:







       '''simple docstring'''



       A:		List[Any]        =		jnp.zeros(input_shape[0]				,     dtype='''i4'''  )
       A:		List[str]        =		jnp.broadcast_to(jnp.arange(jnp.atleast_ad(SCREAMING_SNAKE_CASE_  ).shape[-1]  )				,     input_shape[0]  )
       A:		Union[str, Any]        =		jnp.ones_like(SCREAMING_SNAKE_CASE_  )
       A:		Union[str, Any]        =		jnp.ones_like(SCREAMING_SNAKE_CASE_  )

       A:		Any        =		jax.random.normal(SCREAMING_SNAKE_CASE_				,     input_shape[1]  )

       A    ,						A:		int        =		jax.random.split(SCREAMING_SNAKE_CASE_  )
       A:		Dict        =		{'''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							:       List[Any]				,     SCREAMING_SNAKE_CASE_							:       str				,     SCREAMING_SNAKE_CASE_							:       Any				,     SCREAMING_SNAKE_CASE_							:       int=None				,     SCREAMING_SNAKE_CASE_							:       Any=None				,     SCREAMING_SNAKE_CASE_							:       List[str]=None				,     SCREAMING_SNAKE_CASE_							:       dict = None				,     SCREAMING_SNAKE_CASE_							:       jax.random.PRNGKey = None				,     SCREAMING_SNAKE_CASE_							:       bool = False				,     SCREAMING_SNAKE_CASE_							:       Optional[bool] = None				,     SCREAMING_SNAKE_CASE_							:       Optional[bool] = None				,     SCREAMING_SNAKE_CASE_							:       Optional[bool] = None				,     ) ->		str:







       '''simple docstring'''



       A:		List[str]        =		output_attentions if output_attentions is not None else self.config.output_attentions
       A:		int        =		(
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )
       A:		Dict        =		return_dict if return_dict is not None else self.config.return_dict

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

       if token_type_ids is None:
            A:		Tuple        =		jnp.zeros_like(SCREAMING_SNAKE_CASE_  )

       if attention_mask is None:
            A:		Any        =		jnp.ones_like(SCREAMING_SNAKE_CASE_  )

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







       '''simple docstring'''



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

       if token_type_ids is None:
            A:		List[str]        =		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:		Any        =		{}
       if dropout_rng is not None:
            A:		Tuple        =		dropout_rng

       def _get_features(SCREAMING_SNAKE_CASE_							:       Optional[int]				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]				,     SCREAMING_SNAKE_CASE_							:       Union[str, Any]				,     SCREAMING_SNAKE_CASE_							:       List[Any]				,     SCREAMING_SNAKE_CASE_							:       List[str]				,     SCREAMING_SNAKE_CASE_							:       Tuple  ):
            A:		Tuple        =		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:		str        =		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 				_snake_case    (   self							:       int				,     SCREAMING_SNAKE_CASE_							:       List[Any]				,     SCREAMING_SNAKE_CASE_							:       dict = None				,     SCREAMING_SNAKE_CASE_							:       jax.random.PRNGKey = None				,     SCREAMING_SNAKE_CASE_							:       List[str]=False  ) ->		Union[str, Any]:







       '''simple docstring'''



       A:		Union[str, Any]        =		{}
       if dropout_rng is not None:
            A:		Any        =		dropout_rng

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







       '''simple docstring'''



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

       A:		str        =		{
           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:		Union[str, Any]        =		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[str]        =		AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_  )
                 A:		Optional[int]        =		text_config

            A:		Optional[int]        =		FlaxAutoModel.from_pretrained(SCREAMING_SNAKE_CASE_				,     *SCREAMING_SNAKE_CASE_				,     **SCREAMING_SNAKE_CASE_  )

       A:		Optional[int]        =		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:		Any        =		AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_  )
                 A:		Dict        =		vision_config

            A:		Union[str, Any]        =		FlaxAutoModel.from_pretrained(SCREAMING_SNAKE_CASE_				,     *SCREAMING_SNAKE_CASE_				,     **SCREAMING_SNAKE_CASE_  )

       # instantiate config with corresponding kwargs
       A:		Dict        =		kwargs.pop('''dtype'''				,     jnp.floataa  )
       A:		Any        =		HybridCLIPConfig.from_text_vision_configs(text_model.config				,     vision_model.config				,     **SCREAMING_SNAKE_CASE_  )

       # init model
       A:		List[str]        =		cls(SCREAMING_SNAKE_CASE_				,     *SCREAMING_SNAKE_CASE_				,     dtype=SCREAMING_SNAKE_CASE_				,     **SCREAMING_SNAKE_CASE_  )

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

       A:		Tuple        =		text_model.params

       return model






