Remove useless

add-more-languages
Meng Zhang 2023-03-18 22:19:07 +08:00
parent 0cd5482eaf
commit 65743ba1b9
47 changed files with 0 additions and 474 deletions

View File

@ -1,253 +0,0 @@
# Copyright (c) 2021-2022, NVIDIA CORPORATION. All rights reserved.
# Modified by Brendan Dolan-Gavitt, 2022
# Modified by Meng Zhang, 2023
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import configparser
import multiprocessing
import os
import sys
from pathlib import Path
import numpy as np
import torch
from transformers import GPTJForCausalLM
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(dir_path + "/../../../..")
sys.path.append(dir_path)
def get_weight_data_type(data_type):
if data_type == "fp32":
return np.float32
elif data_type == "fp16":
return np.float16
else:
assert False, f"Invalid weight data type {data_type}"
def split_and_convert_process(i, saved_dir, factor, key, val):
if (
key.find("input_layernorm.weight") != -1
or key.find("input_layernorm.bias") != -1
or key.find("attention.dense.bias") != -1
or key.find("post_attention_layernorm.weight") != -1
or key.find("post_attention_layernorm.bias") != -1
or key.find("mlp.dense_4h_to_h.bias") != -1
or key.find("final_layernorm.weight") != -1
or key.find("final_layernorm.bias") != -1
):
# shared weights, only need to convert the weights of rank 0
if i == 0:
saved_path = saved_dir + "/model." + key + ".bin"
val.tofile(saved_path)
elif (
key.find("attention.dense.weight") != -1
or key.find("mlp.dense_4h_to_h.weight") != -1
):
split_vals = np.split(val, factor, axis=0)
for j in range(factor):
saved_path = saved_dir + "/model." + key + ".%d.bin" % (i * factor + j)
split_vals[j].tofile(saved_path)
elif (
key.find("mlp.dense_h_to_4h.weight") != -1
or key.find("mlp.dense_h_to_4h.bias") != -1
):
split_vals = np.split(val, factor, axis=-1)
for j in range(factor):
saved_path = saved_dir + "/model." + key + ".%d.bin" % (i * factor + j)
split_vals[j].tofile(saved_path)
elif key.find("attention.query_key_value.weight") != -1:
split_vals = np.split(val, factor, axis=-1)
for j in range(factor):
saved_path = saved_dir + "/model." + key + ".%d.bin" % (i * factor + j)
split_vals[j].tofile(saved_path)
else:
print("[ERROR] cannot find key '{}'".format(key))
def split_and_convert(args):
saved_dir = args.saved_dir + "/%d-gpu/" % args.infer_gpu_num
if os.path.exists(saved_dir) is False:
os.makedirs(saved_dir)
t_gpu_num = args.trained_gpu_num
i_gpu_num = args.infer_gpu_num
assert i_gpu_num % t_gpu_num == 0
factor = (int)(i_gpu_num / t_gpu_num)
model = GPTJForCausalLM.from_pretrained(args.in_file)
try:
config = configparser.ConfigParser()
config["gpt"] = {}
for key in vars(args):
config["gpt"][key] = f"{vars(args)[key]}"
for k, v in vars(model.config).items():
config["gpt"][k] = f"{v}"
config["gpt"]["weight_data_type"] = args.weight_data_type
with open((Path(saved_dir) / "config.ini").as_posix(), "w") as configfile:
config.write(configfile)
except Exception:
print("Fail to save the config in config.ini.")
np_weight_data_type = get_weight_data_type(args.weight_data_type)
huggingface_model_name_pattern = [
"ln_1.bias",
"ln_1.weight",
"attn.q_proj.weight",
"attn.out_proj.weight",
"mlp.fc_in.bias",
"mlp.fc_in.weight",
"mlp.fc_out.bias",
"mlp.fc_out.weight",
]
ft_model_name_pattern = [
"input_layernorm.bias",
"input_layernorm.weight",
"attention.query_key_value.weight",
"attention.dense.weight",
"mlp.dense_h_to_4h.bias",
"mlp.dense_h_to_4h.weight",
"mlp.dense_4h_to_h.bias",
"mlp.dense_4h_to_h.weight",
]
torch.multiprocessing.set_start_method("spawn")
pool = multiprocessing.Pool(args.processes)
for name, param in model.named_parameters():
if name.find("weight") == -1 and name.find("bias") == -1:
continue
print(name)
if name == "transformer.wte.weight":
param.detach().cpu().numpy().astype(np_weight_data_type).tofile(
saved_dir + "model.wte.bin"
)
elif name == "transformer.ln_f.bias":
param.detach().cpu().numpy().astype(np_weight_data_type).tofile(
saved_dir + "model.final_layernorm.bias.bin"
)
elif name == "transformer.ln_f.weight":
param.detach().cpu().numpy().astype(np_weight_data_type).tofile(
saved_dir + "model.final_layernorm.weight.bin"
)
elif name == "lm_head.weight":
param.detach().cpu().numpy().astype(np_weight_data_type).tofile(
saved_dir + "model.lm_head.weight.bin"
)
elif name == "lm_head.bias":
param.detach().cpu().numpy().astype(np_weight_data_type).tofile(
saved_dir + "model.lm_head.bias.bin"
)
else:
for i in range(len(huggingface_model_name_pattern)):
if name.find(huggingface_model_name_pattern[i]) != -1:
# Special case for QKV weights
if name.find("attn.q_proj.weight") != -1:
layer = name.split(".")[2]
base_k = f"transformer.h.{layer}."
w = model.state_dict()
QKV_w = torch.stack(
[
w[base_k + "attn.q_proj.weight"],
w[base_k + "attn.k_proj.weight"],
w[base_k + "attn.v_proj.weight"],
]
) # [qkv, n_heads * dim_head, latent_space]
QKV_w = QKV_w.permute(2, 0, 1)
weights = (
QKV_w.detach().cpu().numpy().astype(np_weight_data_type)
)
else:
weights = (
param.detach().cpu().numpy().astype(np_weight_data_type)
)
# Some weights need to be transposed
if (
name.find("mlp.fc_in.weight") != -1
or name.find("mlp.fc_out.weight") != -1
or name.find("attn.out_proj.weight") != -1
):
weights = weights.T
new_name = name.replace("transformer.h.", "layers.").replace(
huggingface_model_name_pattern[i], ft_model_name_pattern[i]
)
pool.starmap(
split_and_convert_process,
[(0, saved_dir, factor, new_name, weights)],
)
pool.close()
pool.join()
if __name__ == "__main__":
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument(
"-saved_dir", "-o", type=str, help="file name of output file", required=True
)
parser.add_argument(
"-in_file", "-i", type=str, help="HF model name or directory", required=True
)
parser.add_argument(
"-trained_gpu_num",
"-t_g",
type=int,
help="How many gpus for training",
default=1,
)
parser.add_argument(
"-infer_gpu_num",
"-i_g",
type=int,
help="How many gpus for inference",
required=True,
)
parser.add_argument(
"-processes",
"-p",
type=int,
help="How many processes to spawn for conversion (default: 4)",
default=4,
)
parser.add_argument(
"-weight_data_type",
type=str,
default="fp32",
choices=["fp32", "fp16"],
help="output weight data type",
)
args = parser.parse_args()
print("\n=============== Argument ===============")
for key in vars(args):
print("{}: {}".format(key, vars(args)[key]))
print("========================================")
split_and_convert(args)

View File

@ -1,86 +0,0 @@
[gpt]
saved_dir = out
in_file = hf-internal-testing/tiny-random-gptj
trained_gpu_num = 1
infer_gpu_num = 1
processes = 4
weight_data_type = fp32
vocab_size = 1000
n_positions = 512
n_embd = 32
n_layer = 5
n_head = 4
n_inner = None
rotary_dim = 4
activation_function = gelu_new
resid_pdrop = 0.0
embd_pdrop = 0.0
attn_pdrop = 0.0
layer_norm_epsilon = 1e-05
initializer_range = 0.02
use_cache = True
bos_token_id = 98
eos_token_id = 98
return_dict = True
output_hidden_states = False
output_attentions = False
torchscript = False
torch_dtype = None
use_bfloat16 = False
tf_legacy_loss = False
pruned_heads = {}
tie_word_embeddings = False
is_encoder_decoder = False
is_decoder = False
cross_attention_hidden_size = None
add_cross_attention = False
tie_encoder_decoder = False
max_length = 20
min_length = 0
do_sample = False
early_stopping = False
num_beams = 1
num_beam_groups = 1
diversity_penalty = 0.0
temperature = 1.0
top_k = 50
top_p = 1.0
typical_p = 1.0
repetition_penalty = 1.0
length_penalty = 1.0
no_repeat_ngram_size = 0
encoder_no_repeat_ngram_size = 0
bad_words_ids = None
num_return_sequences = 1
chunk_size_feed_forward = 0
output_scores = False
return_dict_in_generate = False
forced_bos_token_id = None
forced_eos_token_id = None
remove_invalid_values = False
exponential_decay_length_penalty = None
suppress_tokens = None
begin_suppress_tokens = None
architectures = None
finetuning_task = None
id2label = {0: 'LABEL_0', 1: 'LABEL_1'}
label2id = {'LABEL_0': 0, 'LABEL_1': 1}
tokenizer_class = None
prefix = None
pad_token_id = 98
sep_token_id = None
decoder_start_token_id = None
task_specific_params = None
problem_type = None
_name_or_path = hf-internal-testing/tiny-random-gptj
_commit_hash = b96595a4bcdeb272096214589efa0314259853a0
transformers_version = 4.11.0.dev0
attention_probs_dropout_prob = 0.0
gradient_checkpointing = False
hidden_act = gelu
hidden_dropout_prob = 0.0
intermediate_size = 37
model_type = gptj
n_ctx = 512
scale_attn_weights = True
type_vocab_size = 16

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:38723a2e5e8a17aa7950dc008209944e898f69a7bd10a23c839d341e935fd5ca
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b638277a8690e175a9137feff1e43c067f9faf4e2f600caf468fb05b0403b717
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:125bd07dc5e7a5c6444f7689ba78df0f4c7959b6dcfbaf7c89edd0634a147ea0
size 4096

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:11fd52f2b94fad5fe54e2e03c5848050703ec4d798a43a4c1813f109a6703883
size 12288

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:38723a2e5e8a17aa7950dc008209944e898f69a7bd10a23c839d341e935fd5ca
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b638277a8690e175a9137feff1e43c067f9faf4e2f600caf468fb05b0403b717
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:38723a2e5e8a17aa7950dc008209944e898f69a7bd10a23c839d341e935fd5ca
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:c7459667751237cef87ba33e5faf46e82ea33f7f53aaae7bf95a8667b6f9e639
size 16384

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:076a27c79e5ace2a3d47f9dd2e83e4ff6ea8872b3c2218f66c92b89b55f36560
size 512

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:55de52e2d73e7e92962bc02673a7dce6123d77e486ac4f8ce7bead1a6727d227
size 16384

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:ec12f3d7c73cbc440b476ad66bb4a1ef43881c4d740e8114dc1578bb0197a17e
size 4096

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:86d3c23c240260084ac27bd98d52524f0b3559d8106839d85bc927b44750bd81
size 12288

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:38723a2e5e8a17aa7950dc008209944e898f69a7bd10a23c839d341e935fd5ca
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b638277a8690e175a9137feff1e43c067f9faf4e2f600caf468fb05b0403b717
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:38723a2e5e8a17aa7950dc008209944e898f69a7bd10a23c839d341e935fd5ca
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:75338924b49fb652d556c260013520b58ca70c8bd782bf59732c5ca8d1de111d
size 16384

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:076a27c79e5ace2a3d47f9dd2e83e4ff6ea8872b3c2218f66c92b89b55f36560
size 512

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:be97681ddf43d42ee25d0439c6958edac3549b0edda8a4e3e8bce4275916bb7c
size 16384

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:52bfbb09f85dba050a997ddd1869ae74aa8e76a70a63fd4b7c047531128bec07
size 4096

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:869b24c7a494832730ba41e0286966b23e700e4239d39198e78551a7ef5e8d7f
size 12288

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:38723a2e5e8a17aa7950dc008209944e898f69a7bd10a23c839d341e935fd5ca
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b638277a8690e175a9137feff1e43c067f9faf4e2f600caf468fb05b0403b717
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:38723a2e5e8a17aa7950dc008209944e898f69a7bd10a23c839d341e935fd5ca
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:029775363050d9e4c232301274a8085474ca7992a42cabc1112ff46938c5a178
size 16384

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:076a27c79e5ace2a3d47f9dd2e83e4ff6ea8872b3c2218f66c92b89b55f36560
size 512

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:95beee5255e9d3d5d255ebc6d3f18465e1ab005b7e33e3d0d6495eb7a6178eed
size 16384

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:7af1303e224fc1185c5d936ca8fb3ab336e46f1bd09c94f1e749936d0b023713
size 4096

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:55961585790611913b727f6043af3589274c4b2350ea68bcacf25009e750cc37
size 12288

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:38723a2e5e8a17aa7950dc008209944e898f69a7bd10a23c839d341e935fd5ca
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b638277a8690e175a9137feff1e43c067f9faf4e2f600caf468fb05b0403b717
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:38723a2e5e8a17aa7950dc008209944e898f69a7bd10a23c839d341e935fd5ca
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:9f37f3e28a6e716d08d29d02126854bd6e6248763b9a30af2f2e1dcf7b8fd9a5
size 16384

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:076a27c79e5ace2a3d47f9dd2e83e4ff6ea8872b3c2218f66c92b89b55f36560
size 512

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d903dfb50cce4eae673a2d96a7814353ac1436c0c4d65490bb3ed7d8f8cfc586
size 16384

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:cb76390eda7af1d23d5b162446b4c5da0b791ec337d20d9a643b9716377ae514
size 4096

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:8f54ff65f519de2db6343ec257da92091162381082fbb5dba04df50d8e12a158
size 12288

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:38723a2e5e8a17aa7950dc008209944e898f69a7bd10a23c839d341e935fd5ca
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b638277a8690e175a9137feff1e43c067f9faf4e2f600caf468fb05b0403b717
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:38723a2e5e8a17aa7950dc008209944e898f69a7bd10a23c839d341e935fd5ca
size 128

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:67e7225a2de28f7e21a72b7ea1cab517071744c53d13f4c6964cbc27f4d261e4
size 16384

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:076a27c79e5ace2a3d47f9dd2e83e4ff6ea8872b3c2218f66c92b89b55f36560
size 512

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:cb7582613a3b40b73ac166af2e6b9773511cf6fba7bec12f340119f588c8ea48
size 16384

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:fc19b1997119425765295aeab72d76faa6927d4f83985d328c26f20468d6cc76
size 4000

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:884fae8e52776e900d3800048825503048c95ab2850c38c1a595c6da962d3286
size 128000

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d0d0fec061d1987d0f19acbd319c3471037691cebecd5aa2b00cedcdc2c6177c
size 128000