






"""simple docstring"""
import argparse
import os
from os.path import isfile

import torch

from transformers import GPTaConfig






def        lowerCamelCase_  (						_lowerCamelCase			,   _lowerCamelCase			,   _lowerCamelCase=0				):


 if name is None:
  lowerCamelCase__     :			List[Any]     				=						None
 else:
  lowerCamelCase__     :			Dict     				=						'.' * max(0			,   spaces - 2				) + '# {:' + str(50 - spaces				) + 's}'
  lowerCamelCase__     :			Optional[int]     				=						fmt.format(a_				)

 # Print and recurse (if needed).
 if isinstance(a_			,   a_				):
  if msg is not None:
   print(a_				)
  for k in val.keys():
   recursive_print(a_			,   val[k]			,   spaces + 2				)
 elif isinstance(a_			,   torch.Tensor				):
  print(a_			,   ':'			,   val.size()				)
 else:
  print(a_			,   ':'			,   a_				)






def        lowerCamelCase_  (						_lowerCamelCase			,   _lowerCamelCase			,   _lowerCamelCase			,   _lowerCamelCase				):
 lowerCamelCase__     :			int     				=						param.size()
 # other versions store [num_heads * num_splits * hidden_size, :]
 lowerCamelCase__     :			Union[str, Any]     				=						(num_heads, num_splits, hidden_size) + input_shape[1:]
 lowerCamelCase__     :			Optional[int]     				=						param.view(*a_				)
 lowerCamelCase__     :			str     				=						param.transpose(0			,   1				).contiguous()
 lowerCamelCase__     :			Optional[Any]     				=						param.view(*a_				)
 return param






def        lowerCamelCase_  (						_lowerCamelCase			,   _lowerCamelCase				):
 lowerCamelCase__     :			Optional[Any]     				=						config.n_positions
 lowerCamelCase__     :			List[Any]     				=						config.n_layer
 lowerCamelCase__     :			int     				=						config.vocab_size
 lowerCamelCase__     :			int     				=						config.n_head
 lowerCamelCase__     :			Optional[int]     				=						config.n_embd // config.n_head

 lowerCamelCase__     :			int     				=						sd_megatron['model.language_model.embedding.word_embeddings.weight'][:vocab_size, :]
 lowerCamelCase__     :			List[str]     				=						{
     'transformer.wte.weight': word_embeddings,
     'transformer.wpe.weight': sd_megatron['model.language_model.embedding.position_embeddings.weight'],
     'transformer.ln_f.weight': sd_megatron['model.language_model.encoder.final_layernorm.weight'],
     'transformer.ln_f.bias': sd_megatron['model.language_model.encoder.final_layernorm.bias'],
 }

 lowerCamelCase__     :			Optional[Any]     				=						'model.language_model.encoder.layers.'
 for i in range(a_				):
  lowerCamelCase__     :			Optional[Any]     				=						torch.tril(torch.ones((n_positions, n_positions)			,   dtype=torch.bool				)				)
  lowerCamelCase__     :			List[str]     				=						causal_mask.view(1			,   1			,   a_			,   a_				)
  lowerCamelCase__     :			Tuple     				=						causal_mask
  lowerCamelCase__     :			str     				=						torch.tensor(-1e4			,   dtype=torch.bfloataa				)

  lowerCamelCase__     :			str     				=						sd_megatron[f'''{pf}{i}.input_layernorm.weight''']
  lowerCamelCase__     :			Optional[int]     				=						sd_megatron[f'''{pf}{i}.input_layernorm.bias''']

  lowerCamelCase__     :			int     				=						sd_megatron[f'''{pf}{i}.self_attention.query_key_value.weight''']
  lowerCamelCase__     :			Union[str, Any]     				=						fix_query_key_value_ordering(a_			,   3			,   a_			,   a_				)
  lowerCamelCase__     :			int     				=						vala.transpose(0			,   1				).contiguous()

  lowerCamelCase__     :			str     				=						sd_megatron[f'''{pf}{i}.self_attention.query_key_value.bias''']
  lowerCamelCase__     :			Dict     				=						fix_query_key_value_ordering(a_			,   3			,   a_			,   a_				)
  lowerCamelCase__     :			Union[str, Any]     				=						vala

  lowerCamelCase__     :			Optional[int]     				=						sd_megatron[f'''{pf}{i}.self_attention.dense.weight'''].transpose(
      0			,   1				)
  lowerCamelCase__     :			List[str]     				=						sd_megatron[f'''{pf}{i}.self_attention.dense.bias''']
  lowerCamelCase__     :			Dict     				=						sd_megatron[f'''{pf}{i}.post_attention_layernorm.weight''']
  lowerCamelCase__     :			Any     				=						sd_megatron[f'''{pf}{i}.post_attention_layernorm.bias''']
  lowerCamelCase__     :			str     				=						sd_megatron[f'''{pf}{i}.mlp.dense_h_to_4h.weight'''].transpose(0			,   1				)
  lowerCamelCase__     :			Union[str, Any]     				=						sd_megatron[f'''{pf}{i}.mlp.dense_h_to_4h.bias''']
  lowerCamelCase__     :			Tuple     				=						sd_megatron[f'''{pf}{i}.mlp.dense_4h_to_h.weight'''].transpose(
      0			,   1				)
  lowerCamelCase__     :			Tuple     				=						sd_megatron[f'''{pf}{i}.mlp.dense_4h_to_h.bias''']

 # For LM head, transformers' wants the matrix to weight embeddings.
 lowerCamelCase__     :			int     				=						word_embeddings

 return sd_hf






def        lowerCamelCase_  (						_lowerCamelCase			,   _lowerCamelCase				):
 lowerCamelCase__     :			int     				=						6_4000
 lowerCamelCase__     :			Dict     				=						config_megatron['encoder_seq_length']
 lowerCamelCase__     :			Tuple     				=						config_megatron['hidden_size']
 lowerCamelCase__     :			Tuple     				=						config_megatron['num_layers']
 lowerCamelCase__     :			Any     				=						config_megatron['num_attention_heads']
 lowerCamelCase__     :			List[Any]     				=						config_megatron['ffn_hidden_size']
 lowerCamelCase__     :			str     				=						'gelu'
 lowerCamelCase__     :			Union[str, Any]     				=						0.1
 lowerCamelCase__     :			Tuple     				=						0.1
 lowerCamelCase__     :			Optional[Any]     				=						0.1
 lowerCamelCase__     :			Union[str, Any]     				=						config_megatron['layernorm_epsilon']  # 1e-5
 lowerCamelCase__     :			Optional[int]     				=						config_megatron['init_method_std']  # 0.02
 lowerCamelCase__     :			Tuple     				=						config_megatron['apply_query_key_layer_scaling']  # True
 lowerCamelCase__     :			Dict     				=						True
 lowerCamelCase__     :			Optional[int]     				=						True

 # This identifies the 6.7B (7B) model which uses a different tokenizer
 if config_megatron["hidden_size"] == 4096:
  lowerCamelCase__     :			Union[str, Any]     				=						1  # <|endoftext|>
  lowerCamelCase__     :			Union[str, Any]     				=						1  # <|endoftext|>
  lowerCamelCase__     :			Union[str, Any]     				=						0  # <unk>
 else:
  lowerCamelCase__     :			List[Any]     				=						2  # <s>
  lowerCamelCase__     :			Dict     				=						3  # <|endoftext|>
  lowerCamelCase__     :			List[str]     				=						0  # <pad>

 return config_hf






def        lowerCamelCase_  (						_lowerCamelCase				):
 print(a_				)

 lowerCamelCase__     :			Union[str, Any]     				=						args.checkpoint_path
 lowerCamelCase__     :			List[str]     				=						args.save_path
 if isfile(a_				):
  raise FileNotFoundError(f'''ERROR! could not find file {checkpoint_path}'''				)

 # Load the model.
 lowerCamelCase__     :			Optional[Any]     				=						torch.load(a_			,   map_location='cpu'				)

 # Load the config.
 lowerCamelCase__     :			Any     				=						checkpoint['hyper_parameters']['cfg']
 lowerCamelCase__     :			Optional[int]     				=						GPTaConfig()
 lowerCamelCase__     :			Optional[Any]     				=						copy_config(config_hf=a_			,   config_megatron=a_				)
 lowerCamelCase__     :			Union[str, Any]     				=						['GPT2LMHeadModel']

 lowerCamelCase__     :			Optional[int]     				=						checkpoint['state_dict']

 # Convert.
 print('Converting'				)
 lowerCamelCase__     :			List[Any]     				=						convert_megatron_checkpoint(a_			,   a_				)

 # Print the structure of converted state dict.
 if args.print_checkpoint_structure:
  recursive_print(a_			,   a_				)

 lowerCamelCase__     :			Dict     				=						'GPTSw3Tokenizer'

 # Store the config to file.
 print('Saving config'				)
 config_hf.save_pretrained(a_				)

 # Store the state_dict to file.
 lowerCamelCase__     :			List[Any]     				=						os.path.join(a_			,   'pytorch_model.bin'				)
 print(f'''Saving checkpoint to \"{output_checkpoint_file}\"'''				)
 torch.save(a_			,   a_				)


if __name__ == "__main__":
      A_			: List[Any]				=			argparse.ArgumentParser()
      parser.add_argument(
          "--checkpoint_path",
          type=str,
          required=True,
          help="e.g. megatron_gpt--val_loss=2.42-step=38000-consumed_samples=54720000",
      )
      parser.add_argument("--save_path", type=str, required=True, help="e.g. /home/user/gpt-sw3/hf")
      parser.add_argument("--print-checkpoint-structure", action="store_true")
      A_			: Dict				=			parser.parse_args()
      main(_args)



"""simple docstring"""

from __future__ import annotations
def 		UpperCAmelCase					(		a_			):





		'''simple docstring'''


		for r in range(1,						a_			):
				lowerCamelCase     :			Tuple		=    []
				for x in range(n - 1			):
						lowerCamelCase     :			List[str]		=    pow(a_,						a_,						a_			)
						if val in li:
								break
						li.append(a_			)
				else:
						return r
		return None


if __name__ == "__main__":
					_A            = int(input('Enter a prime number q: '))
					_A            = find_primitive(q)
					if a is None:
										print(F"""Cannot find the primitive for the value: {a!r}""")
					else:
										_A            = int(input('Enter private key of A: '))
										_A            = pow(a, a_private, q)
										_A            = int(input('Enter private key of B: '))
										_A            = pow(a, b_private, q)

										_A            = pow(b_public, a_private, q)
										_A            = pow(a_public, b_private, q)

										print('The key value generated by A is: ', a_secret)
										print('The key value generated by B is: ', b_secret)






