@@ -750,7 +750,7 @@ class OutputFile:
750
750
def __init__ (self , fname_out : Path ) -> None :
751
751
self .gguf = gguf .GGUFWriter .open (fname_out )
752
752
753
- def write_file_header (self , params : Params , file_type : GGMLFileType ) -> None :
753
+ def add_meta_arch (self , params : Params , file_type : GGMLFileType ) -> None :
754
754
llm_arch = "llama"
755
755
756
756
self .gguf .add_architecture (llm_arch )
@@ -763,14 +763,14 @@ def write_file_header(self, params: Params, file_type: GGMLFileType) -> None:
763
763
self .gguf .add_head_count_kv (llm_arch , params .n_head_kv )
764
764
self .gguf .add_layer_norm_rms_eps (llm_arch , params .f_norm_eps )
765
765
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)
772
772
773
- def write_vocab (self , vocab : Vocab ) -> None :
773
+ def add_meta_vocab (self , vocab : Vocab ) -> None :
774
774
tokens = []
775
775
scores = []
776
776
for text , score in vocab .all_tokens ():
@@ -784,21 +784,28 @@ def write_vocab(self, vocab: Vocab) -> None:
784
784
785
785
# TODO: added / special tokens
786
786
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
+
787
794
@staticmethod
788
795
def write_vocab_only (fname_out : Path , params : Params , vocab : Vocab ) -> None :
789
796
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 ()
794
801
795
802
@staticmethod
796
803
def write_all (fname_out : Path , params : Params , file_type : GGMLFileType , model : LazyModel , vocab : Vocab ) -> None :
797
804
check_vocab_size (params , vocab )
805
+
798
806
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 )
802
809
803
810
def do_item (item : Tuple [str , LazyTensor ]) -> NDArray :
804
811
name , lazy_tensor = item
@@ -809,7 +816,7 @@ def do_item(item: Tuple[str, LazyTensor]) -> NDArray:
809
816
size = ' x ' .join (f"{ dim :6d} " for dim in lazy_tensor .shape )
810
817
padi = len (str (len (model )))
811
818
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)
813
820
ndarray .tofile (of .fout )
814
821
of .fout .close ()
815
822
@@ -997,7 +1004,7 @@ def main(args_in: Optional[List[str]] = None) -> None:
997
1004
vocab = load_vocab (args .vocab_dir or args .model , args .vocabtype )
998
1005
assert args .outfile , "need --outfile if using --vocab-only"
999
1006
outfile = args .outfile
1000
- OutputFile .write_vocab_only (outfile , vocab )
1007
+ OutputFile .write_vocab_only (outfile , params , vocab )
1001
1008
print (f"Wrote { outfile } " )
1002
1009
else :
1003
1010
if args .dump :
0 commit comments