Skip to content

Commit 0ba5d48

Browse files
committed
convert-new.py : vocab-only option should work now
1 parent f9db574 commit 0ba5d48

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

convert-new.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ class OutputFile:
750750
def __init__(self, fname_out: Path) -> None:
751751
self.gguf = gguf.GGUFWriter.open(fname_out)
752752

753-
def write_file_header(self, params: Params, file_type: GGMLFileType) -> None:
753+
def add_meta_arch(self, params: Params, file_type: GGMLFileType) -> None:
754754
llm_arch = "llama"
755755

756756
self.gguf.add_architecture (llm_arch)
@@ -763,14 +763,14 @@ def write_file_header(self, params: Params, file_type: GGMLFileType) -> None:
763763
self.gguf.add_head_count_kv (llm_arch, params.n_head_kv)
764764
self.gguf.add_layer_norm_rms_eps (llm_arch, params.f_norm_eps)
765765

766-
def write_tensor_header(self, name: str, shape: Sequence[int], data_type: DataType) -> None:
767-
sname = name.encode('utf-8')
768-
self.fout.write(struct.pack("iii", len(shape), len(sname), DATA_TYPE_TO_FTYPE[data_type]))
769-
self.fout.write(struct.pack("i" * len(shape), *shape[::-1]))
770-
self.fout.write(sname)
771-
self.fout.seek((self.fout.tell() + 31) & -32)
766+
#def write_tensor_header(self, name: str, shape: Sequence[int], data_type: DataType) -> None:
767+
# sname = name.encode('utf-8')
768+
# self.fout.write(struct.pack("iii", len(shape), len(sname), DATA_TYPE_TO_FTYPE[data_type]))
769+
# self.fout.write(struct.pack("i" * len(shape), *shape[::-1]))
770+
# self.fout.write(sname)
771+
# self.fout.seek((self.fout.tell() + 31) & -32)
772772

773-
def write_vocab(self, vocab: Vocab) -> None:
773+
def add_meta_vocab(self, vocab: Vocab) -> None:
774774
tokens = []
775775
scores = []
776776
for text, score in vocab.all_tokens():
@@ -784,21 +784,28 @@ def write_vocab(self, vocab: Vocab) -> None:
784784

785785
# TODO: added / special tokens
786786

787+
def write_meta(self) -> None:
788+
self.gguf.write_header_to_file()
789+
self.gguf.write_kv_data_to_file()
790+
791+
def close(self) -> None:
792+
self.gguf.close()
793+
787794
@staticmethod
788795
def write_vocab_only(fname_out: Path, params: Params, vocab: Vocab) -> None:
789796
of = OutputFile(fname_out)
790-
of = OutputFile(fname_out)
791-
of.write_file_header(params, file_type=GGMLFileType.AllF32)
792-
of.write_vocab(vocab)
793-
of.fout.close()
797+
of.add_meta_arch(params, file_type=GGMLFileType.AllF32)
798+
of.add_meta_vocab(vocab)
799+
of.write_meta()
800+
of.close()
794801

795802
@staticmethod
796803
def write_all(fname_out: Path, params: Params, file_type: GGMLFileType, model: LazyModel, vocab: Vocab) -> None:
797804
check_vocab_size(params, vocab)
805+
798806
of = OutputFile(fname_out)
799-
of.write_file_header(params, file_type)
800-
print("Writing vocab...")
801-
of.write_vocab(vocab)
807+
of.add_meta_arch(params, file_type)
808+
of.add_meta_vocab(vocab)
802809

803810
def do_item(item: Tuple[str, LazyTensor]) -> NDArray:
804811
name, lazy_tensor = item
@@ -809,7 +816,7 @@ def do_item(item: Tuple[str, LazyTensor]) -> NDArray:
809816
size = ' x '.join(f"{dim:6d}" for dim in lazy_tensor.shape)
810817
padi = len(str(len(model)))
811818
print(f"[{i+1:{padi}d}/{len(model)}] Writing tensor {name:38s} | size {size:16} | type {lazy_tensor.data_type}")
812-
of.write_tensor_header(name, lazy_tensor.shape, lazy_tensor.data_type)
819+
#of.write_tensor_header(name, lazy_tensor.shape, lazy_tensor.data_type)
813820
ndarray.tofile(of.fout)
814821
of.fout.close()
815822

@@ -997,7 +1004,7 @@ def main(args_in: Optional[List[str]] = None) -> None:
9971004
vocab = load_vocab(args.vocab_dir or args.model, args.vocabtype)
9981005
assert args.outfile, "need --outfile if using --vocab-only"
9991006
outfile = args.outfile
1000-
OutputFile.write_vocab_only(outfile, vocab)
1007+
OutputFile.write_vocab_only(outfile, params, vocab)
10011008
print(f"Wrote {outfile}")
10021009
else:
10031010
if args.dump:

tests/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ llama_build_and_test_executable(test-quantize-fns.cpp)
2626
llama_build_and_test_executable(test-quantize-perf.cpp)
2727
llama_build_and_test_executable(test-sampling.cpp)
2828
llama_build_executable(test-tokenizer-0.cpp)
29-
llama_test_executable(test-tokenizer-0.llama test-tokenizer-0.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../models/ggml-vocab-llama.bin)
29+
llama_test_executable(test-tokenizer-0.llama test-tokenizer-0.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../models/ggml-vocab-llama.gguf)
3030
llama_build_executable(test-tokenizer-1.cpp)
31-
llama_test_executable(test-tokenizer-1.llama test-tokenizer-1.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../models/ggml-vocab-llama.bin)
32-
llama_test_executable(test-tokenizer-1.aquila test-tokenizer-1.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../models/ggml-vocab-aquila.bin)
31+
llama_test_executable(test-tokenizer-1.llama test-tokenizer-1.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../models/ggml-vocab-llama.gguf)
32+
#llama_test_executable(test-tokenizer-1.aquila test-tokenizer-1.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../models/ggml-vocab-aquila.gguf)
3333
llama_build_and_test_executable(test-grammar-parser.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../examples/grammar-parser.cpp)
3434
llama_build_and_test_executable(test-grad0.cpp) # SLOW
3535
# llama_build_and_test_executable(test-opt.cpp) # SLOW

0 commit comments

Comments
 (0)