okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b0abdeb44
main
1"""tools/ple_fp8_pack.py - the PLE n-gram table exactly as Qwen shipped it (FP8 E4M3), as a GGUF the engine reads. 2 3python tools/ple_fp8_pack.py --model <checkpoint dir> --out <ple-fp8.gguf> 4 5Qwen3.8-Flash-Next's checkpoint stores the table as 128 shards `...ngram_embedding.shard_{k}.weight` of 6[2500012, 160] F8_E4M3 with one BF16 `weight_scale`: 320,001,536 rows of 160 values, 51.2 GB. The engine used to take 7it from ISTA-DASLab's GGUF as IQ4_NL (90 B/row), which is 8% off these values per row; this keeps them bit for bit. 8 9The output is a one-tensor GGUF: `per_layer_token_embd.weight`, ne = [160, 320001536], type I8 holding the FP8 bytes 10(GGUF has no FP8 type), with `strata.ple.format` = "f8_e4m3" and `strata.ple.scale` (F32). The bytes are copied 11straight out of the safetensors files in shard order - nothing is decoded or rounded - so memory stays flat. 12""" 13import argparse 14import json 15import pathlib 16import struct 17import sys 18 19GGUF_TYPE_STRING ,GGUF_TYPE_F32 = 8 ,6 20GGML_TYPE_I8 = 24 21ALIGN = 32 22CHUNK = 64 << 20 23 24 25def safetensors_header (path ): 26with open (path ,"rb" )as f : 27n = struct .unpack ("<Q" ,f .read (8 ))[0 ] 28return json .loads (f .read (n )),8 + n 29 30 31def gguf_string (s ): 32b = s .encode ("utf-8" ) 33return struct .pack ("<Q" ,len (b ))+ b 34 35 36def kv_string (key ,val ): 37return gguf_string (key )+ struct .pack ("<I" ,GGUF_TYPE_STRING )+ gguf_string (val ) 38 39 40def kv_f32 (key ,val ): 41return gguf_string (key )+ struct .pack ("<I" ,GGUF_TYPE_F32 )+ struct .pack ("<f" ,val ) 42 43 44def main (): 45ap = argparse .ArgumentParser (description = __doc__ .split ("\n\n" )[0 ]) 46ap .add_argument ("--model" ,required = True ,help = "the checkpoint directory (model.safetensors.index.json)" ) 47ap .add_argument ("--out" ,required = True ) 48a = ap .parse_args () 49model ,out = pathlib .Path (a .model ),pathlib .Path (a .out ) 50wmap = json .loads ((model / "model.safetensors.index.json" ).read_text (encoding = "utf-8" ))["weight_map" ] 51 52shards = {} 53scale_name = None 54for name in wmap : 55if ".ngram_embedding.shard_" in name and name .endswith (".weight" ): 56shards [int (name .rsplit (".shard_" ,1 )[1 ].split ("." )[0 ])]= name 57elif name .endswith (".ngram_embedding.weight_scale" ): 58scale_name = name 59if not shards or scale_name is None : 60sys .exit ("no ngram_embedding shards / weight_scale in " + str (model )) 61if sorted (shards )!= list (range (len (shards ))): 62sys .exit ("the n-gram shards are not numbered 0..%d without gaps" % (len (shards )- 1 )) 63 64hdr ,base = safetensors_header (model / wmap [scale_name ]) 65t = hdr [scale_name ] 66if t ["dtype" ]!= "BF16" or t ["shape" ]not in ([1 ], []): 67sys .exit ("unexpected weight_scale: %s %s" % (t ["dtype" ],t ["shape" ])) 68with open (model / wmap [scale_name ],"rb" )as f : 69f .seek (base + t ["data_offsets" ][0 ]) 70 (bits ,)= struct .unpack ("<H" ,f .read (2 )) 71scale = struct .unpack ("<f" ,struct .pack ("<I" ,bits << 16 ))[0 ] 72 73parts ,rows ,dim = [],0 ,None 74for k in range (len (shards )): 75name = shards [k ] 76hdr ,base = safetensors_header (model / wmap [name ]) 77t = hdr [name ] 78if t ["dtype" ]!= "F8_E4M3" or len (t ["shape" ])!= 2 : 79sys .exit ("%s is %s %s, not a 2-D F8_E4M3 tensor" % (name ,t ["dtype" ],t ["shape" ])) 80if dim is None : 81dim = t ["shape" ][1 ] 82if t ["shape" ][1 ]!= dim or t ["data_offsets" ][1 ]- t ["data_offsets" ][0 ]!= t ["shape" ][0 ]* dim : 83sys .exit ("%s: inconsistent shape %s" % (name ,t ["shape" ])) 84parts .append ((model / wmap [name ],base + t ["data_offsets" ][0 ],t ["shape" ][0 ]* dim )) 85rows += t ["shape" ][0 ] 86"%d shards, %d rows x %d, scale %.9g, %.2f GB" % (len (parts ),rows ,dim ,scale ,rows * dim / 1e9 ),flush = True ) 87 88kvs = [kv_string ("general.architecture" ,"strata-ple" ), 89kv_string ("general.name" ,"PLE n-gram table, FP8 E4M3 as shipped" ), 90kv_string ("strata.ple.format" ,"f8_e4m3" ), 91kv_f32 ("strata.ple.scale" ,scale ), 92kv_string ("strata.ple.source" ,model .name )] 93head = b"GGUF" + struct .pack ("<IQQ" ,3 ,1 ,len (kvs ))+ b"" .join (kvs ) 94head += gguf_string ("per_layer_token_embd.weight" )+ struct .pack ("<I" ,2 )+ struct .pack ("<QQ" ,dim ,rows ) 95head += struct .pack ("<I" ,GGML_TYPE_I8 )+ struct .pack ("<Q" ,0 ) 96head += b"\0" * ((- len (head ))% ALIGN ) 97 98out .parent .mkdir (parents = True ,exist_ok = True ) 99tmp = out .with_suffix (out .suffix + ".part" ) 100written = 0 101with open (tmp ,"wb" )as w : 102w .write (head ) 103for i , (path ,off ,n )in enumerate (parts ): 104with open (path ,"rb" )as f : 105f .seek (off ) 106left = n 107while left : 108b = f .read (min (CHUNK ,left )) 109if not b : 110sys .exit ("short read in " + str (path )) 111w .write (b ) 112left -= len (b ) 113written += n 114if i % 16 == 15 or i == len (parts )- 1 : 115" %3d/%d shards, %.1f GB" % (i + 1 ,len (parts ),written / 1e9 ),flush = True ) 116if tmp .stat ().st_size != len (head )+ rows * dim : 117sys .exit ("size check failed: %d != %d" % (tmp .stat ().st_size ,len (head )+ rows * dim )) 118tmp .replace (out ) 119"wrote %s (%d rows x %d FP8, header %d B)" % (out ,rows ,dim ,len (head ))) 120 121 122if __name__ == "__main__" : 123main ()