


import math as m



def       lowerCAmelCase			(		lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_=0.001			)->			str:
				return (f(a + h			) - f(a - h			)) / (2 * h)



def       lowerCAmelCase			(		lowerCAmelCase_			, lowerCAmelCase_=0			, lowerCAmelCase_=100			, lowerCAmelCase_=0.0001			, lowerCAmelCase_=1e-6			, lowerCAmelCase_=False			)->			List[Any]:
				lowerCAmelCase_			:   str     					=					xa  # set the initial guess
				lowerCAmelCase_			:   str     					=					[a]
				lowerCAmelCase_			:   Dict     					=					abs(f(lowerCAmelCase_			)			)
				lowerCAmelCase_			:   int     					=					lambda lowerCAmelCase_			: calc_derivative(lowerCAmelCase_			, lowerCAmelCase_			, h=lowerCAmelCase_			)  # noqa: E731  Derivative of f(x)
				for _ in range(lowerCAmelCase_			):
								if fa(lowerCAmelCase_			) == 0:
												raise ValueError('''No converging solution found'''			)
								lowerCAmelCase_			:   Optional[Any]     					=					a - f(lowerCAmelCase_			) / fa(lowerCAmelCase_			)  # Calculate the next estimate
								if logsteps:
												steps.append(lowerCAmelCase_			)
								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

						_UpperCAmelCase      :	Optional[int]  =lambda x: m.tanh(x) ** 2 - m.exp(3 * x)  # noqa: E731
						_UpperCAmelCase  ,       _UpperCAmelCase  ,       _UpperCAmelCase      :	Any  =newton_raphson(
						    f, xa=10, maxiter=1000, 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}}}""")



import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
						import transformer_engine.pytorch as te



def       lowerCAmelCase			(		lowerCAmelCase_			, lowerCAmelCase_=True			, lowerCAmelCase_=True			, lowerCAmelCase_=True			)->			Union[str, Any]:
				if not is_fpa_available():
								raise ImportError('''Using `convert_model` requires transformer_engine to be installed.'''			)
				for name, module in model.named_children():
								if isinstance(lowerCAmelCase_			, nn.Linear			) and to_transformer_engine and _convert_linear:
												# Return early if the linear layer weights are not multiples of 16
												if any(p % 16 != 0 for p in module.weight.shape			):
																return
												lowerCAmelCase_			:   Optional[int]     					=					module.bias is not None
												lowerCAmelCase_			:   Any     					=					te.Linear(
												    module.in_features			, module.out_features			, bias=lowerCAmelCase_			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   Dict     					=					module.weight.data.clone()
												if has_bias:
																lowerCAmelCase_			:   Any     					=					module.bias.data.clone()

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								elif isinstance(lowerCAmelCase_			, nn.LayerNorm			) and to_transformer_engine and _convert_ln:
												lowerCAmelCase_			:   List[str]     					=					te.LayerNorm(module.normalized_shape[0]			, eps=module.eps			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   Union[str, Any]     					=					module.weight.data.clone()
												lowerCAmelCase_			:   List[Any]     					=					module.bias.data.clone()

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								elif isinstance(lowerCAmelCase_			, te.Linear			) and not to_transformer_engine and _convert_linear:
												lowerCAmelCase_			:   Dict     					=					module.bias is not None
												lowerCAmelCase_			:   Union[str, Any]     					=					nn.Linear(
												    module.in_features			, module.out_features			, bias=lowerCAmelCase_			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   Optional[Any]     					=					module.weight.data.clone()
												if has_bias:
																lowerCAmelCase_			:   Union[str, Any]     					=					module.bias.data.clone()

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								elif isinstance(lowerCAmelCase_			, te.LayerNorm			) and not to_transformer_engine and _convert_ln:
												lowerCAmelCase_			:   Union[str, Any]     					=					nn.LayerNorm(module.normalized_shape[0]			, eps=module.eps			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   List[str]     					=					module.weight.data.clone()
												lowerCAmelCase_			:   List[Any]     					=					module.bias.data.clone()

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								else:
												convert_model(
												    lowerCAmelCase_			, to_transformer_engine=lowerCAmelCase_			, _convert_linear=_convert_linear			, _convert_ln=_convert_ln			, )




def       lowerCAmelCase			(		lowerCAmelCase_			)->			str:
				if not is_fpa_available():
								raise ImportError('''Using `has_transformer_engine_layers` requires transformer_engine to be installed.'''			)
				for m in model.modules():
								if isinstance(lowerCAmelCase_			, (te.LayerNorm, te.Linear)			):
												return True
				return False