blacked gpt4all-to-ggml

This commit is contained in:
Geeks-sid 2023-03-29 19:15:37 -04:00
parent dfa2d707e9
commit eec105a86a

View file

@ -15,37 +15,43 @@ from sentencepiece import SentencePieceProcessor
HPARAMS = keys = ["vocab_size", "dim", "multiple_of", "n_heads", "n_layers"] HPARAMS = keys = ["vocab_size", "dim", "multiple_of", "n_heads", "n_layers"]
def parse_args(): def parse_args():
parser = argparse.ArgumentParser(description='Upgrade a GPT4All model to the current format') parser = argparse.ArgumentParser(
parser.add_argument('gpt4all_model', help='path to gpt4all-lora-quantized.bin') description="Upgrade a GPT4All model to the current format"
parser.add_argument('tokenizer_model', help='path to LLaMA tokenizer.model file') )
parser.add_argument("gpt4all_model", help="path to gpt4all-lora-quantized.bin")
parser.add_argument("tokenizer_model", help="path to LLaMA tokenizer.model file")
return parser.parse_args() return parser.parse_args()
def read_header(f_in): def read_header(f_in):
struct_fmt = "i" * (3 + len(HPARAMS)) struct_fmt = "i" * (3 + len(HPARAMS))
struct_size = struct.calcsize(struct_fmt) struct_size = struct.calcsize(struct_fmt)
buf = f_in.read(struct_size) buf = f_in.read(struct_size)
return struct.unpack(struct_fmt, buf) return struct.unpack(struct_fmt, buf)
def write_header(f_out, header): def write_header(f_out, header):
(magic, vocab_size, dim, multiple_of, n_heads, n_layers, rot, ftype) = header (magic, vocab_size, dim, multiple_of, n_heads, n_layers, rot, ftype) = header
if magic != 0x67676d6c: if magic != 0x67676D6C:
raise Exception('Invalid file magic. Must be an old style ggml file.') raise Exception("Invalid file magic. Must be an old style ggml file.")
values = [ values = [
0x67676d66, # magic: ggml in hex 0x67676D66, # magic: ggml in hex
1, # file version 1, # file version
vocab_size, vocab_size,
dim, dim,
multiple_of, multiple_of,
n_heads, n_heads,
n_layers, n_layers,
rot, rot,
ftype ftype,
] ]
f_out.write(struct.pack("i" * len(values), *values)) f_out.write(struct.pack("i" * len(values), *values))
def write_tokens(fout, tokenizer): def write_tokens(fout, tokenizer):
for i in range(tokenizer.vocab_size()): for i in range(tokenizer.vocab_size()):
if tokenizer.is_unknown(i): if tokenizer.is_unknown(i):
@ -71,12 +77,14 @@ def write_tokens(fout, tokenizer):
fout.write(text) fout.write(text)
fout.write(struct.pack("f", 0.0)) fout.write(struct.pack("f", 0.0))
def read_tokens(f_in, tokenizer): def read_tokens(f_in, tokenizer):
for i in range(tokenizer.vocab_size()): for i in range(tokenizer.vocab_size()):
len_b = f_in.read(4) len_b = f_in.read(4)
(length,) = struct.unpack("i", len_b) (length,) = struct.unpack("i", len_b)
f_in.read(length) f_in.read(length)
def copy_all_data(f_out, f_in): def copy_all_data(f_out, f_in):
while True: while True:
buf = f_in.read(1024 * 1024) buf = f_in.read(1024 * 1024)
@ -84,9 +92,10 @@ def copy_all_data(f_out, f_in):
break break
f_out.write(buf) f_out.write(buf)
def convert_one_file(path_in, tokenizer): def convert_one_file(path_in, tokenizer):
path_tmp = f"{path_in}.tmp" path_tmp = f"{path_in}.tmp"
path_orig= f"{path_in}.orig" path_orig = f"{path_in}.orig"
print(f"converting {path_in}") print(f"converting {path_in}")
with open(path_in, "rb") as f_in, open(path_tmp, "wb") as f_out: with open(path_in, "rb") as f_in, open(path_tmp, "wb") as f_out:
write_header(f_out, read_header(f_in)) write_header(f_out, read_header(f_in))
@ -96,6 +105,7 @@ def convert_one_file(path_in, tokenizer):
os.rename(path_in, path_orig) os.rename(path_in, path_orig)
os.rename(path_tmp, path_in) os.rename(path_tmp, path_in)
def main(): def main():
args = parse_args() args = parse_args()
@ -103,5 +113,6 @@ def main():
convert_one_file(args.gpt4all_model, tokenizer) convert_one_file(args.gpt4all_model, tokenizer)
if __name__ == "__main__": if __name__ == "__main__":
main() main()