okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1"""tools/nvfp4_convert.py - a ModelOpt NVFP4 Qwen3.8-Flash-Next checkpoint -> a GGUF Strata can pack. 2 3python tools/nvfp4_convert.py --model <ModelOpt NVFP4 dir> --outfile <model.gguf> 4 5This is llama.cpp's own converter (third_party/llama.cpp at the commit setup.py pins), run with the type policy 6Strata's engine needs. The converter already repacks ModelOpt NVFP4 experts into ggml's block_nvfp4 without loss 7(tools/nvfp4_verify.py proves it bit for bit) and writes each expert's weight_scale_2 as a separate F32 tensor 8`blk.N.ffn_{gate,up,down}_exps.scale`, one value per expert. What it does not know is Strata's layout: 9 10* the small projections the residual, router, GDN, QSA and PLE kernels read as BF16 (iq_pack.BF16_PROJECTIONS) 11must be BF16 - the converter would otherwise write routers and indexer projections as F32, which iq_pack 12refuses, and the rest as the --outtype; 13* everything the engine serves natively (attention, ssm_out, shared experts, output) goes to Q8_0: the native 14GEMV has no BF16 path, and Q8_0 is the closest to the checkpoint's BF16 it has; 15* token_embd goes to Q8_0 too. The abliteration edited embed_tokens, so another model's table cannot be reused, 16and gguf-py cannot write the IQ types the native embedding reads today - the engine learns Q8_0 instead; 17* the 51.2B-parameter PLE n-gram table is left out. The engine only reads it as IQ4_NL (ngram.cpp), and 18dequantizing the checkpoint's FP8 copy would need ~205 GB of RAM. The abliteration did not touch this table, 19so the engine is pointed at ISTA-DASLab's IQ4_NL shard 2 with --ple-gguf. The PLE hash constants and the 20row width still come from this checkpoint and are written into the metadata. 21 22The MTP head is dropped here as llama.cpp does; tools/mtp_pack.py packs it separately. 23""" 24from __future__import annotations 25 26import argparse 27import json 28import pathlib 29import sys 30import time 31 32ENGINE = pathlib .Path (__file__ ).resolve ().parents [1 ] 33sys .path .insert (0 ,str (ENGINE / "tools" )) 34from _paths import add_gguf_py ,converter_dir # noqa: E402 (the vendored gguf-py and converter, third_party/strata-gguf-py) 35 36CONVERTER = pathlib .Path (converter_dir ()) 37add_gguf_py () 38sys .path .insert (0 ,str (CONVERTER )) 39 40import gguf # noqa: E402 41from conversion import qwen4exp as Q4 # noqa: E402 42 43T = gguf .GGMLQuantizationType 44 45# Strata reads these as BF16 (tools/iq_pack.py BF16_PROJECTIONS + BF16_OUTPUT, and the routers). 46BF16_SUFFIXES = ( 47"hc_attn_down.weight" ,"hc_attn_up.weight" ,"hc_attn_inject.weight" , 48"hc_ffn_down.weight" ,"hc_ffn_up.weight" ,"hc_ffn_inject.weight" , 49"ssm_alpha.weight" ,"ssm_beta.weight" ,"indexer.q_proj.weight" ,"indexer.k_proj.weight" , 50"ple_value.weight" ,"ple_key.weight" ,"ffn_gate_inp.weight" ,"ffn_gate_inp_shexp.weight" , 51"output_hc_down.weight" ,"output_hc_up.weight" , 52) 53F16_SUFFIXES = ("ple_conv1d.weight" ,) 54Q8_NAMES = {"token_embd.weight" ,"output.weight" } 55 56dropped_ple :list [str ]= [] 57 58 59def install_policy ()-> None : 60cls = Q4 .Qwen4ExpTextModel 61base_force = cls .tensor_force_quant 62 63def tensor_force_quant (self ,name ,new_name ,bid ,n_dims ): 64if n_dims >= 2 : 65if new_name .endswith (BF16_SUFFIXES ): 66return T .BF16 67if new_name .endswith (F16_SUFFIXES ): 68return T .F16 69if new_name in Q8_NAMES : 70return T .Q8_0 71return base_force (self ,name ,new_name ,bid ,n_dims ) 72 73def place_ple_shard (self ,data_torch ,name ): 74# Record the shard so the parts check in prepare_tensors still holds, and the row width for the 75# metadata; write nothing - the table comes from the IQ4_NL PLE GGUF at run time. 76idx = int (name .rsplit ("shard_" ,1 )[1 ].split ("." ,1 )[0 ]) 77self ._ple_shards [idx ]= name 78self ._ple_row_dim = int (data_torch .shape [- 1 ]) 79dropped_ple .append (name ) 80return [] 81 82cls .tensor_force_quant = tensor_force_quant 83cls ._place_ple_shard = place_ple_shard 84 85 86def main ()-> int : 87ap = argparse .ArgumentParser () 88ap .add_argument ("--model" ,required = True ,help = "the ModelOpt NVFP4 checkpoint directory" ) 89ap .add_argument ("--outfile" ,required = True ) 90ap .add_argument ("--dry-run" ,action = "store_true" ) 91a = ap .parse_args () 92model = pathlib .Path (a .model ) 93out = pathlib .Path (a .outfile ) 94out .parent .mkdir (parents = True ,exist_ok = True ) 95 96install_policy () 97import convert_hf_to_gguf as C # noqa: E402 (after the policy is installed on the class) 98 99argv = [str (model ),"--outtype" ,"q8_0" ,"--outfile" ,str (out )] 100if a .dry_run : 101argv .append ("--dry-run" ) 102sys .argv = ["convert_hf_to_gguf.py" ]+ argv 103t0 = time .time () 104rc = C .main () 105took = time .time ()- t0 106 107commit = "" # the converter is vendored at the llama.cpp pin: third_party/strata-gguf-py/UPSTREAM.md names it 108try : 109for line in (CONVERTER .parent / "UPSTREAM.md" ).read_text (encoding = "utf-8" ).splitlines (): 110if line .startswith ("pin:" ): 111commit = line .split (":" ,1 )[1 ].strip () 112break 113except OSError : 114pass 115manifest = { 116"source" :str (model ), 117"llama_cpp_commit" :commit , 118"policy" : {"bf16" :list (BF16_SUFFIXES ),"f16" :list (F16_SUFFIXES ),"q8_0" :sorted (Q8_NAMES ), 119"default_2d" :"Q8_0" ,"experts" :"NVFP4 (lossless repack) + per-expert F32 .scale" }, 120"ple_table" : {"written" :False ,"shards_dropped" :len (dropped_ple ), 121"use" :"--ple-gguf with an IQ4_NL per_layer_token_embd (ISTA-DASLab shard 2)" }, 122"mtp" :"dropped here; pack with tools/mtp_pack.py" , 123"seconds" :round (took ,1 ), 124 } 125if not a .dry_run : 126out .with_suffix (".manifest.json" ).write_text (json .dumps (manifest ,indent = 2 )+ "\n" ,encoding = "utf-8" ) 127"nvfp4_convert: done in %.0f s, PLE shards dropped: %d" % (took ,len (dropped_ple ))) 128return rc or 0 129 130 131if __name__ == "__main__" : 132raise SystemExit (main ())