



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

import torch

from transformers import GPTaConfig
def        A_  (      _lowercase, _lowercase, _lowercase=0     ):







						'''simple docstring'''


						if name is None:
												snake_case_			:Union[str, Any]						=      None
						else:
												snake_case_			:str						=      """.""" * max(0, spaces - 2     ) + """# {:""" + str(50 - spaces     ) + """s}"""
												snake_case_			:str						=      fmt.format(_lowercase     )

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







						'''simple docstring'''


						snake_case_			:int						=      param.size()
						# other versions store [num_heads * num_splits * hidden_size, :]
						snake_case_			:Tuple						=      (num_heads, num_splits, hidden_size) + input_shape[1:]
						snake_case_			:List[Any]						=      param.view(*_lowercase     )
						snake_case_			:Tuple						=      param.transpose(0, 1     ).contiguous()
						snake_case_			:str						=      param.view(*_lowercase     )
						return param
def        A_  (      _lowercase, _lowercase     ):







						'''simple docstring'''


						snake_case_			:Union[str, Any]						=      config.n_positions
						snake_case_			:str						=      config.n_layer
						snake_case_			:Tuple						=      config.vocab_size
						snake_case_			:Optional[int]						=      config.n_head
						snake_case_			:Dict						=      config.n_embd // config.n_head

						snake_case_			:int						=      sd_megatron["""model.language_model.embedding.word_embeddings.weight"""][:vocab_size, :]
						snake_case_			:List[Any]						=      {
						    """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"""],
						}

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

												snake_case_			:Union[str, Any]						=      sd_megatron[f"""{pf}{i}.input_layernorm.weight"""]
												snake_case_			:Optional[int]						=      sd_megatron[f"""{pf}{i}.input_layernorm.bias"""]

												snake_case_			:int						=      sd_megatron[f"""{pf}{i}.self_attention.query_key_value.weight"""]
												snake_case_			:Tuple						=      fix_query_key_value_ordering(_lowercase, 3, _lowercase, _lowercase     )
												snake_case_			:Any						=      vala.transpose(0, 1     ).contiguous()

												snake_case_			:Any						=      sd_megatron[f"""{pf}{i}.self_attention.query_key_value.bias"""]
												snake_case_			:Union[str, Any]						=      fix_query_key_value_ordering(_lowercase, 3, _lowercase, _lowercase     )
												snake_case_			:int						=      vala

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

						# For LM head, transformers' wants the matrix to weight embeddings.
						snake_case_			:List[Any]						=      word_embeddings

						return sd_hf
def        A_  (      _lowercase, _lowercase     ):







						'''simple docstring'''


						snake_case_			:List[Any]						=      64000
						snake_case_			:int						=      config_megatron["""encoder_seq_length"""]
						snake_case_			:List[Any]						=      config_megatron["""hidden_size"""]
						snake_case_			:str						=      config_megatron["""num_layers"""]
						snake_case_			:Union[str, Any]						=      config_megatron["""num_attention_heads"""]
						snake_case_			:Dict						=      config_megatron["""ffn_hidden_size"""]
						snake_case_			:Union[str, Any]						=      """gelu"""
						snake_case_			:int						=      0.1
						snake_case_			:Union[str, Any]						=      0.1
						snake_case_			:Dict						=      0.1
						snake_case_			:int						=      config_megatron["""layernorm_epsilon"""]  # 1e-5
						snake_case_			:Union[str, Any]						=      config_megatron["""init_method_std"""]  # 0.02
						snake_case_			:List[str]						=      config_megatron["""apply_query_key_layer_scaling"""]  # True
						snake_case_			:Optional[Any]						=      True
						snake_case_			:Optional[Any]						=      True

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

						return config_hf
def        A_  (      _lowercase     ):







						'''simple docstring'''


						print(_lowercase     )

						snake_case_			:Optional[Any]						=      args.checkpoint_path
						snake_case_			:List[Any]						=      args.save_path
						if isfile(_lowercase     ):
												raise FileNotFoundError(f"""ERROR! could not find file {checkpoint_path}"""     )

						# Load the model.
						snake_case_			:Dict						=      torch.load(_lowercase, map_location="""cpu"""     )

						# Load the config.
						snake_case_			:List[str]						=      checkpoint["""hyper_parameters"""]["""cfg"""]
						snake_case_			:Dict						=      GPTaConfig()
						snake_case_			:Any						=      copy_config(config_hf=_lowercase, config_megatron=_lowercase     )
						snake_case_			:Union[str, Any]						=      ["""GPT2LMHeadModel"""]

						snake_case_			:int						=      checkpoint["""state_dict"""]

						# Convert.
						print("""Converting"""     )
						snake_case_			:List[str]						=      convert_megatron_checkpoint(_lowercase, _lowercase     )

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

						snake_case_			:Tuple						=      """GPTSw3Tokenizer"""

						# Store the config to file.
						print("""Saving config"""     )
						config_hf.save_pretrained(_lowercase     )

						# Store the state_dict to file.
						snake_case_			:str						=      os.path.join(_lowercase, """pytorch_model.bin"""     )
						print(f"""Saving checkpoint to \"{output_checkpoint_file}\""""     )
						torch.save(_lowercase, _lowercase     )


if __name__ == "__main__":
						__a     =    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     =    parser.parse_args()
						main(_args)





"""simple docstring"""
from math import radians as angle_to_radians
from math import sin

# Acceleration Constant on Earth (unit m/s^2)
__a     =    9.8_06_65
def        A_  (      _lowercase, _lowercase     ):







						'''simple docstring'''


						if not isinstance(_lowercase, (int, float)     ):
												raise TypeError("""Invalid velocity. Should be a positive number."""     )

						if not isinstance(_lowercase, (int, float)     ):
												raise TypeError("""Invalid angle. Range is 1-90 degrees."""     )

						# Ensure valid angle
						if angle > 90 or angle < 1:
												raise ValueError("""Invalid angle. Range is 1-90 degrees."""     )

						# Ensure valid velocity
						if init_velocity < 0:
												raise ValueError("""Invalid velocity. Should be a positive number."""     )
def        A_  (      _lowercase, _lowercase     ):







						'''simple docstring'''


						check_args(_lowercase, _lowercase     )
						snake_case_			:str						=      angle_to_radians(2 * angle     )
						return round(init_velocity**2 * sin(_lowercase     ) / g, 2     )
def        A_  (      _lowercase, _lowercase     ):







						'''simple docstring'''


						check_args(_lowercase, _lowercase     )
						snake_case_			:str						=      angle_to_radians(_lowercase     )
						return round(init_velocity**2 * sin(_lowercase     ) ** 2 / (2 * g), 2     )
def        A_  (      _lowercase, _lowercase     ):







						'''simple docstring'''


						check_args(_lowercase, _lowercase     )
						snake_case_			:Optional[int]						=      angle_to_radians(_lowercase     )
						return round(2 * init_velocity * sin(_lowercase     ) / g, 2     )
def        A_  (      ):







						'''simple docstring'''


						snake_case_,	snake_case_			:List[str]						=      25, 20
						assert horizontal_distance(_lowercase, _lowercase     ) == 40.97
						assert max_height(_lowercase, _lowercase     ) == 3.73
						assert total_time(_lowercase, _lowercase     ) == 1.74


if __name__ == "__main__":
						from doctest import testmod

						testmod()

						# Get input from user
						__a     =    float(input("Initial Velocity: ").strip())

						# Get input from user
						__a     =    float(input("angle: ").strip())

						# Print results
						print()
						print("Results: ")
						print(F"""Horizontal Distance: {horizontal_distance(init_vel, angle)!s} [m]""")
						print(F"""Maximum Height: {max_height(init_vel, angle)!s} [m]""")
						print(F"""Total Time: {total_time(init_vel, angle)!s} [s]""")
