


'''simple docstring'''



from __future__ import annotations

from decimal import Decimal
from math import *  # noqa: F403

from sympy import diff


def 				lowerCamelCase__				(	A       :      List[Any]				,       A       :      Tuple				,       A       :      List[str] = 10**-10					):




      '''simple docstring'''






      UpperCAmelCase					    =     a
      while True:
            UpperCAmelCase					    =     Decimal(__SCREAMING_SNAKE_CASE					) - (
                Decimal(eval(__SCREAMING_SNAKE_CASE					)					) / Decimal(eval(str(diff(__SCREAMING_SNAKE_CASE					)					)					)					)  # noqa: S307
            )
            # This number dictates the accuracy of the answer
            if abs(eval(__SCREAMING_SNAKE_CASE					)					) < precision:  # noqa: S307
                  return float(__SCREAMING_SNAKE_CASE					)


# Let's Execute
if __name__ == "__main__":
 # Find root of trigonometric function
 # Find value of pi
 print(F"""The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}""")
 # Find root of polynomial
 print(F"""The root of x**2 - 5*x + 2 = 0 is {newton_raphson("x**2 - 5*x + 2", 0.4)}""")
 # Find Square Root of 5
 print(F"""The root of log(x) - 1 = 0 is {newton_raphson("log(x) - 1", 2)}""")
 # Exponential Roots
 print(F"""The root of exp(x) - 1 = 0 is {newton_raphson("exp(x) - 1", 0)}""")









'''simple docstring'''



import flax.linen as nn
import jax.numpy as jnp

from .attention_flax import FlaxTransformeraDModel
from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD






class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	True
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




				def 					a__(		self      :			str				)->					Dict:

										"""simple docstring"""




										UpperCAmelCase					    =     []
										UpperCAmelCase					    =     []

										for i in range(self.num_layers				):
																UpperCAmelCase					    =     self.in_channels if i == 0 else self.out_channels

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=lowerCAmelCase				,   out_channels=self.out_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)

																UpperCAmelCase					    =     FlaxTransformeraDModel(
																    in_channels=self.out_channels				,   n_heads=self.num_attention_heads				,   d_head=self.out_channels // self.num_attention_heads				,   depth=1				,   use_linear_projection=self.use_linear_projection				,   only_cross_attention=self.only_cross_attention				,   use_memory_efficient_attention=self.use_memory_efficient_attention				,   dtype=self.dtype				,   )
																attentions.append(lowerCAmelCase				)

										UpperCAmelCase					    =     resnets
										UpperCAmelCase					    =     attentions

										if self.add_downsample:
																UpperCAmelCase					    =     FlaxDownsampleaD(self.out_channels				,   dtype=self.dtype				)





				def __call__(		self      :			List[Any]				,   lowerCAmelCase      :			Any				,   lowerCAmelCase      :			int				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			List[str]=True				)->					Optional[int]:

										"""simple docstring"""




										UpperCAmelCase					    =     ()

										for resnet, attn in zip(self.resnets				,   self.attentions				):
																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																UpperCAmelCase					    =     attn(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																output_states += (hidden_states,)

										if self.add_downsample:
																UpperCAmelCase					    =     self.downsamplers_a(lowerCAmelCase				)
																output_states += (hidden_states,)

										return hidden_states, output_states







class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	True
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




				def 					a__(		self      :			List[str]				)->					Any:

										"""simple docstring"""




										UpperCAmelCase					    =     []

										for i in range(self.num_layers				):
																UpperCAmelCase					    =     self.in_channels if i == 0 else self.out_channels

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=lowerCAmelCase				,   out_channels=self.out_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)
										UpperCAmelCase					    =     resnets

										if self.add_downsample:
																UpperCAmelCase					    =     FlaxDownsampleaD(self.out_channels				,   dtype=self.dtype				)





				def __call__(		self      :			List[str]				,   lowerCAmelCase      :			Tuple				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			List[str]=True				)->					Optional[int]:

										"""simple docstring"""




										UpperCAmelCase					    =     ()

										for resnet in self.resnets:
																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																output_states += (hidden_states,)

										if self.add_downsample:
																UpperCAmelCase					    =     self.downsamplers_a(lowerCAmelCase				)
																output_states += (hidden_states,)

										return hidden_states, output_states







class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	True
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




				def 					a__(		self      :			List[str]				)->					Tuple:

										"""simple docstring"""




										UpperCAmelCase					    =     []
										UpperCAmelCase					    =     []

										for i in range(self.num_layers				):
																UpperCAmelCase					    =     self.in_channels if (i == self.num_layers - 1) else self.out_channels
																UpperCAmelCase					    =     self.prev_output_channel if i == 0 else self.out_channels

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=resnet_in_channels + res_skip_channels				,   out_channels=self.out_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)

																UpperCAmelCase					    =     FlaxTransformeraDModel(
																    in_channels=self.out_channels				,   n_heads=self.num_attention_heads				,   d_head=self.out_channels // self.num_attention_heads				,   depth=1				,   use_linear_projection=self.use_linear_projection				,   only_cross_attention=self.only_cross_attention				,   use_memory_efficient_attention=self.use_memory_efficient_attention				,   dtype=self.dtype				,   )
																attentions.append(lowerCAmelCase				)

										UpperCAmelCase					    =     resnets
										UpperCAmelCase					    =     attentions

										if self.add_upsample:
																UpperCAmelCase					    =     FlaxUpsampleaD(self.out_channels				,   dtype=self.dtype				)





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

										"""simple docstring"""




										for resnet, attn in zip(self.resnets				,   self.attentions				):
																# pop res hidden states
																UpperCAmelCase					    =     res_hidden_states_tuple[-1]
																UpperCAmelCase					    =     res_hidden_states_tuple[:-1]
																UpperCAmelCase					    =     jnp.concatenate((hidden_states, res_hidden_states)				,   axis=-1				)

																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																UpperCAmelCase					    =     attn(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)

										if self.add_upsample:
																UpperCAmelCase					    =     self.upsamplers_a(lowerCAmelCase				)

										return hidden_states







class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	True
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




				def 					a__(		self      :			Optional[int]				)->					str:

										"""simple docstring"""




										UpperCAmelCase					    =     []

										for i in range(self.num_layers				):
																UpperCAmelCase					    =     self.in_channels if (i == self.num_layers - 1) else self.out_channels
																UpperCAmelCase					    =     self.prev_output_channel if i == 0 else self.out_channels

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=resnet_in_channels + res_skip_channels				,   out_channels=self.out_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)

										UpperCAmelCase					    =     resnets

										if self.add_upsample:
																UpperCAmelCase					    =     FlaxUpsampleaD(self.out_channels				,   dtype=self.dtype				)





				def __call__(		self      :			Union[str, Any]				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			Union[str, Any]				,   lowerCAmelCase      :			Tuple				,   lowerCAmelCase      :			Dict=True				)->					Tuple:

										"""simple docstring"""




										for resnet in self.resnets:
																# pop res hidden states
																UpperCAmelCase					    =     res_hidden_states_tuple[-1]
																UpperCAmelCase					    =     res_hidden_states_tuple[:-1]
																UpperCAmelCase					    =     jnp.concatenate((hidden_states, res_hidden_states)				,   axis=-1				)

																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)

										if self.add_upsample:
																UpperCAmelCase					    =     self.upsamplers_a(lowerCAmelCase				)

										return hidden_states







class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




				def 					a__(		self      :			int				)->					Optional[int]:

										"""simple docstring"""




										UpperCAmelCase					    =     [
										    FlaxResnetBlockaD(
										        in_channels=self.in_channels				,   out_channels=self.in_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
										]

										UpperCAmelCase					    =     []

										for _ in range(self.num_layers				):
																UpperCAmelCase					    =     FlaxTransformeraDModel(
																    in_channels=self.in_channels				,   n_heads=self.num_attention_heads				,   d_head=self.in_channels // self.num_attention_heads				,   depth=1				,   use_linear_projection=self.use_linear_projection				,   use_memory_efficient_attention=self.use_memory_efficient_attention				,   dtype=self.dtype				,   )
																attentions.append(lowerCAmelCase				)

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=self.in_channels				,   out_channels=self.in_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)

										UpperCAmelCase					    =     resnets
										UpperCAmelCase					    =     attentions





				def __call__(		self      :			Dict				,   lowerCAmelCase      :			Optional[Any]				,   lowerCAmelCase      :			Tuple				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			Any=True				)->					Union[str, Any]:

										"""simple docstring"""




										UpperCAmelCase					    =     self.resnets[0](lowerCAmelCase				,   lowerCAmelCase				)
										for attn, resnet in zip(self.attentions				,   self.resnets[1:]				):
																UpperCAmelCase					    =     attn(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)

										return hidden_states





