






"""simple docstring"""







from math import isclose, sqrt





def 						lowercase__(A ,		A ,		A						)    ->tuple[float, float, float]:




						"""simple docstring"""



						lowercase__    :				int=	point_y / 4 / point_x
						lowercase__    :				Union[str, Any]=	2 * normal_gradient / (1 + normal_gradient * normal_gradient)
						lowercase__    :				List[Any]=	(1 - normal_gradient * normal_gradient) / (
						    1 + normal_gradient * normal_gradient
						)
						lowercase__    :				List[Any]=	(sa - ca * incoming_gradient) / (ca + sa * incoming_gradient)

						# to find the next point, solve the simultaeneous equations:
						# y^2 + 4x^2 = 100
						# y - b = m * (x - a)
						# ==> A x^2 + B x + C = 0
						lowercase__    :				Optional[int]=	outgoing_gradient**2 + 4
						lowercase__    :				Any=	2 * outgoing_gradient * (point_y - outgoing_gradient * point_x)
						lowercase__    :				Tuple=	(point_y - outgoing_gradient * point_x) ** 2 - 100

						lowercase__    :				Optional[int]=	(
						    -linear_term - sqrt(linear_term**2 - 4 * quadratic_term * constant_term						)
						) / (2 * quadratic_term)
						lowercase__    :				Union[str, Any]=	(
						    -linear_term + sqrt(linear_term**2 - 4 * quadratic_term * constant_term						)
						) / (2 * quadratic_term)

						# two solutions, one of which is our input point
						lowercase__    :				int=	x_minus if isclose(A ,		A						) else x_plus
						lowercase__    :				int=	point_y + outgoing_gradient * (next_x - point_x)

						return next_x, next_y, outgoing_gradient





def 						lowercase__(A = 1.4 ,		A = -9.6						)    ->int:




						"""simple docstring"""



						lowercase__    :				int=	0
						lowercase__    :				float=	first_x_coord
						lowercase__    :				float=	first_y_coord
						lowercase__    :				float=	(10.1 - point_y) / (0.0 - point_x)

						while not (-0.01 <= point_x <= 0.01 and point_y > 0):
												lowercase__,     lowercase__,     lowercase__    :				Any=	next_point(A ,		A ,		A						)
												num_reflections += 1

						return num_reflections


if __name__ == "__main__":
			print(F"""{solution() = }""")














"""simple docstring"""







import argparse
import os
import re

import torch
from flax.traverse_util import flatten_dict
from tax import checkpoints

from transformers import (
    AutoTokenizer,
    PixaStructConfig,
    PixaStructForConditionalGeneration,
    PixaStructImageProcessor,
    PixaStructProcessor,
    PixaStructTextConfig,
    PixaStructVisionConfig,
)





def 						lowercase__(A						)    ->Dict:




						"""simple docstring"""



						lowercase__    :				Dict=	checkpoints.load_tax_checkpoint(A						)
						lowercase__    :				List[Any]=	flatten_dict(A						)
						return flax_params





def 						lowercase__(A						)    ->Tuple:




						"""simple docstring"""



						lowercase__    :				Union[str, Any]=	{}

						lowercase__    :				Any=	{
						    "token_embedder": "embeddings",
						    "encoder_norm": "layernorm",
						    "kernel": "weight",
						    ".out": ".output",
						    "scale": "weight",
						    "embedders_0.pos_embedding": "row_embedder.weight",
						    "embedders_1.pos_embedding": "column_embedder.weight",
						}

						lowercase__    :				Tuple=	{
						    "query": "attention.query",
						    "key": "attention.key",
						    "value": "attention.value",
						    "output.dense": "output",
						    "encoder_decoder_attention.o": "encoder_decoder_attention.attention.o",
						    "pre_self_attention_layer_norm": "self_attention.layer_norm",
						    "pre_cross_attention_layer_norm": "encoder_decoder_attention.layer_norm",
						    "mlp.": "mlp.DenseReluDense.",
						    "pre_mlp_layer_norm": "mlp.layer_norm",
						    "self_attention.o": "self_attention.attention.o",
						    "decoder.embeddings.embedding": "decoder.embed_tokens.weight",
						    "decoder.relpos_bias.rel_embedding": "decoder.layer.0.self_attention.attention.relative_attention_bias.weight",
						    "decoder.decoder_norm.weight": "decoder.final_layer_norm.weight",
						    "decoder.logits_dense.weight": "decoder.lm_head.weight",
						}

						for key in flax_dict.keys():
												if "target" in key:
																		# remove the first prefix from the key
																		lowercase__    :				Any=	".".join(key[1:]						)

																		# rename the key
																		for old, new in CONVERSION_MAPPING.items():
																								lowercase__    :				List[Any]=	new_key.replace(A ,		A						)

																		if "decoder" in new_key:
																								for old, new in DECODER_CONVERSION_MAPPING.items():
																														lowercase__    :				List[str]=	new_key.replace(A ,		A						)

																		if "layers" in new_key and "decoder" not in new_key:
																								# use regex to replace the layer number
																								lowercase__    :				List[str]=	re.sub(R"layers_(\d+)" ,		R"layer.\1" ,		A						)
																								lowercase__    :				Any=	new_key.replace("encoder" ,		"encoder.encoder"						)

																		elif "layers" in new_key and "decoder" in new_key:
																								# use regex to replace the layer number
																								lowercase__    :				str=	re.sub(R"layers_(\d+)" ,		R"layer.\1" ,		A						)

																		lowercase__    :				Dict=	flax_dict[key]

						lowercase__    :				str=	{}
						# convert converted_dict into torch format
						for key in converted_dict.keys():
												if ("embed_tokens" not in key) and ("embedder" not in key):
																		lowercase__    :				Dict=	torch.from_numpy(converted_dict[key].T						)
												else:
																		lowercase__    :				Any=	torch.from_numpy(converted_dict[key]						)

						return converted_torch_dict





def 						lowercase__(A ,		A ,		A=False ,		A=False						)    ->Union[str, Any]:




						"""simple docstring"""



						lowercase__    :				Tuple=	get_flax_param(A						)

						if not use_large:
												lowercase__    :				int=	PixaStructVisionConfig()
												lowercase__    :				str=	PixaStructTextConfig()
						else:
												lowercase__    :				List[Any]=	PixaStructVisionConfig(
												    hidden_size=1_536 ,		d_ff=3_968 ,		num_attention_heads=24 ,		num_hidden_layers=18						)
												lowercase__    :				Optional[Any]=	PixaStructTextConfig(hidden_size=1_536 ,		d_ff=3_968 ,		num_heads=24 ,		num_layers=18						)
						lowercase__    :				Any=	PixaStructConfig(
						    vision_config=encoder_config.to_dict() ,		text_config=decoder_config.to_dict() ,		is_vqa=A						)

						lowercase__    :				Optional[Any]=	PixaStructForConditionalGeneration(A						)

						lowercase__    :				Tuple=	rename_and_convert_flax_params(A						)
						model.load_state_dict(A						)

						lowercase__    :				List[str]=	AutoTokenizer.from_pretrained("ybelkada/test-pix2struct-tokenizer"						)
						lowercase__    :				List[str]=	PixaStructImageProcessor()
						lowercase__    :				Dict=	PixaStructProcessor(image_processor=A ,		tokenizer=A						)

						if use_large:
												lowercase__    :				Tuple=	4_096

						lowercase__    :				List[Any]=	True

						# mkdir if needed
						os.makedirs(A ,		exist_ok=A						)

						model.save_pretrained(A						)
						processor.save_pretrained(A						)

						print("Model saved in {}".format(A						)						)


if __name__ == "__main__":
			a			:    int				 =		argparse.ArgumentParser()
			parser.add_argument("""--t5x_checkpoint_path""", default=None, type=str, help="""Path to the original T5x checkpoint.""")
			parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""")
			parser.add_argument("""--use_large""", action="""store_true""", help="""Use large model.""")
			parser.add_argument("""--is_vqa""", action="""store_true""", help="""Use large model.""")
			a			:    Optional[int]				 =		parser.parse_args()

			convert_pixastruct_original_pytorch_checkpoint_to_hf(
			    args.tax_checkpoint_path, args.pytorch_dump_folder_path, args.use_large
			)






