

'''simple docstring'''

import argparse
import os
from os.path import isfile

import torch

from transformers import GPTaConfig





def 				lowercase (					__magic_name__			,   __magic_name__			,   __magic_name__=0							):



					'''simple docstring'''







					if name is None:
										UpperCAmelCase		:							List[str]        =       None
					else:
										UpperCAmelCase		:							str        =       "." * max(0			,   spaces - 2							) + "# {:" + str(50 - spaces							) + "s}"
										UpperCAmelCase		:							int        =       fmt.format(__magic_name__							)

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





def 				lowercase (					__magic_name__			,   __magic_name__			,   __magic_name__			,   __magic_name__							):



					'''simple docstring'''







					UpperCAmelCase		:							Any        =       param.size()
					# other versions store [num_heads * num_splits * hidden_size, :]
					UpperCAmelCase		:							Optional[int]        =       (num_heads, num_splits, hidden_size) + input_shape[1:]
					UpperCAmelCase		:							Dict        =       param.view(*__magic_name__							)
					UpperCAmelCase		:							Optional[Any]        =       param.transpose(0			,   1							).contiguous()
					UpperCAmelCase		:							Union[str, Any]        =       param.view(*__magic_name__							)
					return param





def 				lowercase (					__magic_name__			,   __magic_name__							):



					'''simple docstring'''







					UpperCAmelCase		:							int        =       config.n_positions
					UpperCAmelCase		:							List[str]        =       config.n_layer
					UpperCAmelCase		:							Dict        =       config.vocab_size
					UpperCAmelCase		:							int        =       config.n_head
					UpperCAmelCase		:							Optional[Any]        =       config.n_embd // config.n_head

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

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

										UpperCAmelCase		:							Any        =       sd_megatron[F"{pf}{i}.input_layernorm.weight"]
										UpperCAmelCase		:							Dict        =       sd_megatron[F"{pf}{i}.input_layernorm.bias"]

										UpperCAmelCase		:							Optional[int]        =       sd_megatron[F"{pf}{i}.self_attention.query_key_value.weight"]
										UpperCAmelCase		:							int        =       fix_query_key_value_ordering(__magic_name__			,   3			,   __magic_name__			,   __magic_name__							)
										UpperCAmelCase		:							List[str]        =       vala.transpose(0			,   1							).contiguous()

										UpperCAmelCase		:							str        =       sd_megatron[F"{pf}{i}.self_attention.query_key_value.bias"]
										UpperCAmelCase		:							List[str]        =       fix_query_key_value_ordering(__magic_name__			,   3			,   __magic_name__			,   __magic_name__							)
										UpperCAmelCase		:							Any        =       vala

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

					# For LM head, transformers' wants the matrix to weight embeddings.
					UpperCAmelCase		:							Optional[int]        =       word_embeddings

					return sd_hf





def 				lowercase (					__magic_name__			,   __magic_name__							):



					'''simple docstring'''







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

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

					return config_hf





def 				lowercase (					__magic_name__							):



					'''simple docstring'''







					print(__magic_name__							)

					UpperCAmelCase		:							Dict        =       args.checkpoint_path
					UpperCAmelCase		:							List[Any]        =       args.save_path
					if isfile(__magic_name__							):
										raise FileNotFoundError(F"ERROR! could not find file {checkpoint_path}"							)

					# Load the model.
					UpperCAmelCase		:							List[str]        =       torch.load(__magic_name__			,   map_location="cpu"							)

					# Load the config.
					UpperCAmelCase		:							Union[str, Any]        =       checkpoint["hyper_parameters"]["cfg"]
					UpperCAmelCase		:							Optional[int]        =       GPTaConfig()
					UpperCAmelCase		:							List[str]        =       copy_config(config_hf=__magic_name__			,   config_megatron=__magic_name__							)
					UpperCAmelCase		:							List[str]        =       ["GPT2LMHeadModel"]

					UpperCAmelCase		:							Union[str, Any]        =       checkpoint["state_dict"]

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

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

					UpperCAmelCase		:							Any        =       "GPTSw3Tokenizer"

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

					# Store the state_dict to file.
					UpperCAmelCase		:							Tuple        =       os.path.join(__magic_name__			,   "pytorch_model.bin"							)
					print(F"Saving checkpoint to \"{output_checkpoint_file}\""							)
					torch.save(__magic_name__			,   __magic_name__							)


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       :      Any  	=			parser.parse_args()
				main(_args)



'''simple docstring'''

import math

import qiskit
from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute





def 				lowercase (					__magic_name__ = 1			,   __magic_name__ = 1							):



					'''simple docstring'''







					if isinstance(__magic_name__			,   __magic_name__							) or isinstance(__magic_name__			,   __magic_name__							):
										raise TypeError("inputs must be integers."							)
					if (bit_a < 0) or (bit_a < 0):
										raise ValueError("inputs must be positive."							)
					if (math.floor(__magic_name__							) != bit_a) or (math.floor(__magic_name__							) != bit_a):
										raise ValueError("inputs must be exact integers."							)
					if (bit_a > 1) or (bit_a > 1):
										raise ValueError("inputs must be less or equal to 1."							)

					# build registers
					UpperCAmelCase		:							List[Any]        =       QuantumRegister(2			,   "qr"							)
					UpperCAmelCase		:							Optional[int]        =       ClassicalRegister(2			,   "cr"							)

					UpperCAmelCase		:							Optional[int]        =       QuantumCircuit(__magic_name__			,   __magic_name__							)

					# entanglement the qubits
					quantum_circuit.h(1							)
					quantum_circuit.cx(1			,   0							)

					# send the information
					UpperCAmelCase		:							Any        =       str(__magic_name__							) + str(__magic_name__							)

					if c_information == "11":
										quantum_circuit.x(1							)
										quantum_circuit.z(1							)
					elif c_information == "10":
										quantum_circuit.z(1							)
					elif c_information == "01":
										quantum_circuit.x(1							)
					else:
										quantum_circuit.i(1							)

					# unentangled the circuit
					quantum_circuit.cx(1			,   0							)
					quantum_circuit.h(1							)

					# measure the circuit
					quantum_circuit.measure(__magic_name__			,   __magic_name__							)

					UpperCAmelCase		:							Tuple        =       Aer.get_backend("aer_simulator"							)
					UpperCAmelCase		:							Any        =       execute(__magic_name__			,   __magic_name__			,   shots=1000							)

					return job.result().get_counts(__magic_name__							)


if __name__ == "__main__":
				print(F'Counts for classical state send: {superdense_coding(1,1)}')
