
"""simple docstring"""

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 		UpperCAmelCase (		a_,						a_,						a_,						a_="attention"      ):





		'''simple docstring'''


		lowerCamelCase					:      int          =   params[F"""{prefix}/layers_{i}/{layer_name}/key/kernel"""]
		lowerCamelCase					:      Dict          =   params[F"""{prefix}/layers_{i}/{layer_name}/out/kernel"""]
		lowerCamelCase					:      Union[str, Any]          =   params[F"""{prefix}/layers_{i}/{layer_name}/query/kernel"""]
		lowerCamelCase					:      Union[str, Any]          =   params[F"""{prefix}/layers_{i}/{layer_name}/value/kernel"""]
		return k, o, q, v
def 		UpperCAmelCase (		a_,						a_,						a_,						a_=False      ):





		'''simple docstring'''


		if split_mlp_wi:
				lowerCamelCase					:      Union[str, Any]          =   params[F"""{prefix}/layers_{i}/mlp/wi_0/kernel"""]
				lowerCamelCase					:      Optional[int]          =   params[F"""{prefix}/layers_{i}/mlp/wi_1/kernel"""]
				lowerCamelCase					:      Union[str, Any]          =   (wi_a, wi_a)
		else:
				lowerCamelCase					:      Optional[Any]          =   params[F"""{prefix}/layers_{i}/mlp/wi/kernel"""]

		lowerCamelCase					:      Optional[int]          =   params[F"""{prefix}/layers_{i}/mlp/wo/kernel"""]
		return wi, wo
def 		UpperCAmelCase (		a_,						a_,						a_,						a_      ):





		'''simple docstring'''


		return params[F"""{prefix}/layers_{i}/{layer_name}/scale"""]
def 		UpperCAmelCase (		a_,						*, a_,						a_      ):





		'''simple docstring'''


		lowerCamelCase					:      str          =   traverse_util.flatten_dict(variables['target']      )
		lowerCamelCase					:      Any          =   {'/'.join(a_      ): v for k, v in old.items()}

		# v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi
		lowerCamelCase					:      Union[str, Any]          =   'encoder/layers_0/mlp/wi_0/kernel' in old
		print('Split MLP:',						a_      )

		lowerCamelCase					:      List[Any]          =   collections.OrderedDict()

		# Shared embeddings.
		lowerCamelCase					:      List[str]          =   old['token_embedder/embedding']

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

				# Block i, layer 1 (MLP).
				lowerCamelCase					:      Optional[Any]          =   tax_layer_norm_lookup(a_,						a_,						'encoder',						'pre_mlp_layer_norm'      )
				lowerCamelCase					:      Dict          =   tax_mlp_lookup(a_,						a_,						'encoder',						a_      )
				lowerCamelCase					:      Union[str, Any]          =   layer_norm
				if split_mlp_wi:
						lowerCamelCase					:      Dict          =   wi[0].T
						lowerCamelCase					:      Union[str, Any]          =   wi[1].T
				else:
						lowerCamelCase					:      List[Any]          =   wi.T
				lowerCamelCase					:      Dict          =   wo.T

		lowerCamelCase					:      int          =   old[
		    'encoder/relpos_bias/rel_embedding'
		].T
		lowerCamelCase					:      Dict          =   old['encoder/encoder_norm/scale']

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

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

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

				lowerCamelCase					:      str          =   old['decoder/decoder_norm/scale']
				lowerCamelCase					:      Tuple          =   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					:      Tuple          =   old['decoder/logits_dense/kernel'].T

		return new
def 		UpperCAmelCase (		a_,						a_      ):





		'''simple docstring'''


		lowerCamelCase					:      List[Any]          =   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					:      Optional[int]          =   state_dict['shared.weight']

		if not is_encoder_only:
				if "decoder.embed_tokens.weight" not in state_dict:
						lowerCamelCase					:      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					:      str          =   state_dict['shared.weight']

		return state_dict
def 		UpperCAmelCase (		a_,						a_,						a_,						a_      ):





		'''simple docstring'''


		lowerCamelCase					:      int          =   checkpoints.load_tax_checkpoint(a_      )
		lowerCamelCase					:      Union[str, Any]          =   convert_tax_to_pytorch(a_,						num_layers=config.num_layers,						is_encoder_only=a_      )
		lowerCamelCase					:      List[str]          =   make_state_dict(a_,						a_      )
		model.load_state_dict(a_,						strict=a_      )
def 		UpperCAmelCase (		a_,						a_,						a_,						a_ = False      ):





		'''simple docstring'''


		lowerCamelCase					:      List[str]          =   TaConfig.from_json_file(a_      )
		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					:      List[Any]          =   TaEncoderModel(a_      )
		else:
				lowerCamelCase					:      Optional[int]          =   TaForConditionalGeneration(a_      )

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

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

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


if __name__ == "__main__":
					_A						=			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
					)
					_A						=			parser.parse_args()
					convert_tax_checkpoint_to_pytorch(
					    args.tax_checkpoint_path, args.config_file, args.pytorch_dump_path, args.is_encoder_only
					)








"""simple docstring"""

def 		UpperCAmelCase (		a_ = 1000      ):





		'''simple docstring'''


		lowerCamelCase					:      Dict          =   2**power
		lowerCamelCase					:      List[str]          =   str(a_      )
		lowerCamelCase					:      Dict          =   list(a_      )
		lowerCamelCase					:      Optional[Any]          =   0

		for i in list_num:
				sum_of_num += int(a_      )

		return sum_of_num


if __name__ == "__main__":
					_A						=			int(input('Enter the power of 2: ').strip())
					print('2 ^ ', power, ' = ', 2**power)
					_A						=			solution(power)
					print('Sum of the digits is: ', result)






