



import argparse
import collections

import torch
from flax import traverse_util
from tax import checkpoints

from transformers import TaConfig, TaEncoderModel, TaForConditionalGeneration
from transformers.utils import logging


logging.set_verbosity_info()






def      lowerCAmelCase__		(   lowerCamelCase_		:		Union[str, Any]	,lowerCamelCase_		:		Tuple	,lowerCamelCase_		:		List[str]	,lowerCamelCase_		:		List[Any]="attention"):





				'''simple docstring'''

				lowerCAmelCase__					:    Union[str, Any]												=					params[f"""{prefix}/layers_{i}/{layer_name}/key/kernel"""]
				lowerCAmelCase__					:    List[Any]												=					params[f"""{prefix}/layers_{i}/{layer_name}/out/kernel"""]
				lowerCAmelCase__					:    Optional[Any]												=					params[f"""{prefix}/layers_{i}/{layer_name}/query/kernel"""]
				lowerCAmelCase__					:    str												=					params[f"""{prefix}/layers_{i}/{layer_name}/value/kernel"""]
				return k, o, q, v






def      lowerCAmelCase__		(   lowerCamelCase_		:		Optional[Any]	,lowerCamelCase_		:		str	,lowerCamelCase_		:		Dict	,lowerCamelCase_		:		List[Any]=False):





				'''simple docstring'''

				if split_mlp_wi:
								lowerCAmelCase__					:    Dict												=					params[f"""{prefix}/layers_{i}/mlp/wi_0/kernel"""]
								lowerCAmelCase__					:    Optional[Any]												=					params[f"""{prefix}/layers_{i}/mlp/wi_1/kernel"""]
								lowerCAmelCase__					:    Optional[Any]												=					(wi_a, wi_a)
				else:
								lowerCAmelCase__					:    List[str]												=					params[f"""{prefix}/layers_{i}/mlp/wi/kernel"""]

				lowerCAmelCase__					:    str												=					params[f"""{prefix}/layers_{i}/mlp/wo/kernel"""]
				return wi, wo






def      lowerCAmelCase__		(   lowerCamelCase_		:		List[str]	,lowerCamelCase_		:		Optional[int]	,lowerCamelCase_		:		Tuple	,lowerCamelCase_		:		Optional[Any]):





				'''simple docstring'''

				return params[f"""{prefix}/layers_{i}/{layer_name}/scale"""]






def      lowerCAmelCase__		(   lowerCamelCase_		:		dict	,*, lowerCamelCase_		:		int	,lowerCamelCase_		:		bool):





				'''simple docstring'''

				lowerCAmelCase__					:    Optional[Any]												=					traverse_util.flatten_dict(variables['''target'''])
				lowerCAmelCase__					:    Tuple												=					{'''/'''.join(lowerCamelCase_): v for k, v in old.items()}

				# v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi
				lowerCAmelCase__					:    int												=					'''encoder/layers_0/mlp/wi_0/kernel''' in old
				print('''Split MLP:'''	,lowerCamelCase_)

				lowerCAmelCase__					:    int												=					collections.OrderedDict()

				# Shared embeddings.
				lowerCAmelCase__					:    Union[str, Any]												=					old['''token_embedder/embedding''']

				# Encoder.
				for i in range(lowerCamelCase_):
								# Block i, layer 0 (Self Attention).
								lowerCAmelCase__					:    Optional[int]												=					tax_layer_norm_lookup(lowerCamelCase_	,lowerCamelCase_	,'''encoder'''	,'''pre_attention_layer_norm''')
								lowerCAmelCase__					:    Optional[int]												=					tax_attention_lookup(lowerCamelCase_	,lowerCamelCase_	,'''encoder'''	,'''attention''')
								lowerCAmelCase__					:    str												=					layer_norm
								lowerCAmelCase__					:    Any												=					k.T
								lowerCAmelCase__					:    List[str]												=					o.T
								lowerCAmelCase__					:    Tuple												=					q.T
								lowerCAmelCase__					:    List[str]												=					v.T

								# Block i, layer 1 (MLP).
								lowerCAmelCase__					:    Tuple												=					tax_layer_norm_lookup(lowerCamelCase_	,lowerCamelCase_	,'''encoder'''	,'''pre_mlp_layer_norm''')
								lowerCAmelCase__					:    List[Any]												=					tax_mlp_lookup(lowerCamelCase_	,lowerCamelCase_	,'''encoder'''	,lowerCamelCase_)
								lowerCAmelCase__					:    Optional[int]												=					layer_norm
								if split_mlp_wi:
												lowerCAmelCase__					:    Any												=					wi[0].T
												lowerCAmelCase__					:    str												=					wi[1].T
								else:
												lowerCAmelCase__					:    List[str]												=					wi.T
								lowerCAmelCase__					:    str												=					wo.T

				lowerCAmelCase__					:    Dict												=					old[
				    '''encoder/relpos_bias/rel_embedding'''
				].T
				lowerCAmelCase__					:    Any												=					old['''encoder/encoder_norm/scale''']

				if not is_encoder_only:
								# Decoder.
								for i in range(lowerCamelCase_):
												# Block i, layer 0 (Self Attention).
												lowerCAmelCase__					:    Dict												=					tax_layer_norm_lookup(lowerCamelCase_	,lowerCamelCase_	,'''decoder'''	,'''pre_self_attention_layer_norm''')
												lowerCAmelCase__					:    Tuple												=					tax_attention_lookup(lowerCamelCase_	,lowerCamelCase_	,'''decoder'''	,'''self_attention''')
												lowerCAmelCase__					:    Optional[Any]												=					layer_norm
												lowerCAmelCase__					:    Dict												=					k.T
												lowerCAmelCase__					:    str												=					o.T
												lowerCAmelCase__					:    int												=					q.T
												lowerCAmelCase__					:    Dict												=					v.T

												# Block i, layer 1 (Cross Attention).
												lowerCAmelCase__					:    List[str]												=					tax_layer_norm_lookup(lowerCamelCase_	,lowerCamelCase_	,'''decoder'''	,'''pre_cross_attention_layer_norm''')
												lowerCAmelCase__					:    int												=					tax_attention_lookup(lowerCamelCase_	,lowerCamelCase_	,'''decoder'''	,'''encoder_decoder_attention''')
												lowerCAmelCase__					:    str												=					layer_norm
												lowerCAmelCase__					:    Tuple												=					k.T
												lowerCAmelCase__					:    Union[str, Any]												=					o.T
												lowerCAmelCase__					:    Tuple												=					q.T
												lowerCAmelCase__					:    Dict												=					v.T

												# Block i, layer 2 (MLP).
												lowerCAmelCase__					:    int												=					tax_layer_norm_lookup(lowerCamelCase_	,lowerCamelCase_	,'''decoder'''	,'''pre_mlp_layer_norm''')
												lowerCAmelCase__					:    Tuple												=					tax_mlp_lookup(lowerCamelCase_	,lowerCamelCase_	,'''decoder'''	,lowerCamelCase_)
												lowerCAmelCase__					:    List[Any]												=					layer_norm
												if split_mlp_wi:
																lowerCAmelCase__					:    List[Any]												=					wi[0].T
																lowerCAmelCase__					:    Tuple												=					wi[1].T
												else:
																lowerCAmelCase__					:    int												=					wi.T
												lowerCAmelCase__					:    Union[str, Any]												=					wo.T

								lowerCAmelCase__					:    str												=					old['''decoder/decoder_norm/scale''']
								lowerCAmelCase__					:    Any												=					old[
								    '''decoder/relpos_bias/rel_embedding'''
								].T

								# LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead)
								if "decoder/logits_dense/kernel" in old:
												lowerCAmelCase__					:    Dict												=					old['''decoder/logits_dense/kernel'''].T

				return new






def      lowerCAmelCase__		(   lowerCamelCase_		:		List[str]	,lowerCamelCase_		:		bool):





				'''simple docstring'''

				lowerCAmelCase__					:    List[str]												=					collections.OrderedDict([(k, torch.from_numpy(v.copy())) for (k, v) in converted_params.items()])

				# Add what is missing.
				if "encoder.embed_tokens.weight" not in state_dict:
								lowerCAmelCase__					:    Tuple												=					state_dict['''shared.weight''']

				if not is_encoder_only:
								if "decoder.embed_tokens.weight" not in state_dict:
												lowerCAmelCase__					:    Optional[Any]												=					state_dict['''shared.weight''']

								if "lm_head.weight" not in state_dict:  # For old 1.0 models.
												print('''Using shared word embeddings as lm_head.''')
												lowerCAmelCase__					:    Optional[int]												=					state_dict['''shared.weight''']

				return state_dict






def      lowerCAmelCase__		(   lowerCamelCase_		:		Dict	,lowerCamelCase_		:		List[Any]	,lowerCamelCase_		:		Any	,lowerCamelCase_		:		Union[str, Any]):





				'''simple docstring'''

				lowerCAmelCase__					:    str												=					checkpoints.load_tax_checkpoint(lowerCamelCase_)
				lowerCAmelCase__					:    Tuple												=					convert_tax_to_pytorch(lowerCamelCase_	,num_layers=config.num_layers	,is_encoder_only=lowerCamelCase_)
				lowerCAmelCase__					:    Optional[int]												=					make_state_dict(lowerCamelCase_	,lowerCamelCase_)
				model.load_state_dict(lowerCamelCase_	,strict=lowerCamelCase_)






def      lowerCAmelCase__		(   lowerCamelCase_		:		List[Any]	,lowerCamelCase_		:		Union[str, Any]	,lowerCamelCase_		:		str	,lowerCamelCase_		:		bool = False):





				'''simple docstring'''

				lowerCAmelCase__					:    Optional[Any]												=					TaConfig.from_json_file(lowerCamelCase_)
				print(f"""Building PyTorch model from configuration: {config}""")
				# Non-v1.1 checkpoints could also use T5Model, but this works for all.
				# The v1.0 checkpoints will simply have an LM head that is the word embeddings.
				if is_encoder_only:
								lowerCAmelCase__					:    Any												=					TaEncoderModel(lowerCamelCase_)
				else:
								lowerCAmelCase__					:    List[str]												=					TaForConditionalGeneration(lowerCamelCase_)

				# Load weights from tf checkpoint
				load_tax_weights_in_ta(lowerCamelCase_	,lowerCamelCase_	,lowerCamelCase_	,lowerCamelCase_)

				# Save pytorch-model
				print(f"""Save PyTorch model to {pytorch_dump_path}""")
				model.save_pretrained(lowerCamelCase_)

				# Verify that we can load the checkpoint.
				model.from_pretrained(lowerCamelCase_)
				print('''Done''')


if __name__ == "__main__":
							__snake_case    :		Optional[Any]  =argparse.ArgumentParser(description='Converts a native T5X checkpoint into a PyTorch checkpoint.')
							# Required parameters
							parser.add_argument(
							    '--t5x_checkpoint_path', default=None, type=str, required=True, help='Path to the T5X checkpoint.'
							)
							parser.add_argument(
							    '--config_file',
							    default=None,
							    type=str,
							    required=True,
							    help='The config json file corresponding to the pre-trained T5 model.\nThis specifies the model architecture.',
							)
							parser.add_argument(
							    '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
							)
							parser.add_argument(
							    '--is_encoder_only', action='store_true', help='Check if the model is encoder-decoder model', default=False
							)
							__snake_case    :		Optional[int]  =parser.parse_args()
							convert_tax_checkpoint_to_pytorch(
							    args.tax_checkpoint_path, args.config_file, args.pytorch_dump_path, args.is_encoder_only
							)






def      lowerCAmelCase__		(   lowerCamelCase_		:		int = 1000):





				'''simple docstring'''

				lowerCAmelCase__						,		lowerCAmelCase__					:    int												=					1, 1
				lowerCAmelCase__					:    Any												=					2
				while True:
								lowerCAmelCase__					:    Optional[Any]												=					0
								lowerCAmelCase__					:    Any												=					fa + fa
								lowerCAmelCase__						,		lowerCAmelCase__					:    Optional[Any]												=					fa, f
								index += 1
								for _ in str(lowerCamelCase_):
												i += 1
								if i == n:
												break
				return index


if __name__ == "__main__":
							print(solution(int(str(input()).strip())))

