tarekfurthy commited on
Commit
ea40aaa
·
verified ·
1 Parent(s): 71ad0ff

RESTORE the llama-quantize step: splice into a MIX GGUF (experts TQ1_0, rest F16), not into all-F16

Browse files

The all-f16 base made the splice impossible (9.5x byte mismatch); the size assert at blk.0.ffn_gate_exps caught it. Now: llama-quantize --tensor-type ffn_(gate|up|down)_exps=TQ1_0 with F16 outtype builds a 2.33 GB mix, verified (48 TQ1_0 experts, trnr.rot still F16), the 13.85 GB f16 base is deleted, and the absmax floor is measured before our bytes replace it.

Files changed (1) hide show
  1. UPLOAD_1_trnr_ship.py +58 -8
UPLOAD_1_trnr_ship.py CHANGED
@@ -908,13 +908,63 @@ assert SH("cd %s && python -u %s %s --outtype f16 --outfile %s --rot %s/v1_g256/
908
  % (K, K + "/trnr_convert.py", HF, BASE, K)) == 0, "conversion failed -- paste this back"
909
  P("trnr-base.gguf %.3f GB" % (os.path.getsize(BASE) / 1e9))
910
 
911
- B("5/8 build the patched runtime (~5 min), splice IN PLACE, validate")
912
  sys.path.insert(0, SRC + "/gguf-py"); import gguf
913
  PP = SRC + "/build/bin/llama-perplexity"
914
- if not os.path.isfile(PP):
 
915
  assert SH("cd %s && cmake -B build -DCMAKE_BUILD_TYPE=Release -DLLAMA_CURL=OFF -DGGML_CUDA=OFF "
916
  "-DGGML_CCACHE=OFF >/dev/null 2>&1 && cmake --build build --config Release -j "
917
- "--target llama-perplexity 2>&1" % SRC) == 0, "build failed"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
918
  DST = K + "/trnr-OLMoE-1B-7B-ternary-tq1_0.gguf"
919
  by = {t.name: t for t in gguf.GGUFReader(BASE).tensors} if os.path.exists(BASE) else {}
920
  off = lambda t: int(np.asarray(t.data_offset).reshape(-1)[0])
@@ -931,11 +981,11 @@ def ppl_of(path, chunks=8):
931
  return float(g[-1]) if g else float("nan")
932
 
933
  if os.path.exists(BASE):
934
- # NOTE: measuring the rotated fp16 BASE here would be the cleanest isolation check, but
935
- # llama-perplexity on 13.85 GB of f16 with -ngl 0 runs at roughly a second per token --
936
- # one 512-token chunk is ~10 min and the full 8 would be over an hour. The ternary
937
- # number carries the same information for free: ~28 means trnr.rot loaded and the router
938
- # fold is right, thousands means the rotation tensor is being ignored.
939
  TQB = pickle.load(open(K + "/v1_g256/tq1_0_bytes.pkl", "rb"))
940
  f = open(BASE, "r+b"); nb = 0
941
  for L in range(16):
 
908
  % (K, K + "/trnr_convert.py", HF, BASE, K)) == 0, "conversion failed -- paste this back"
909
  P("trnr-base.gguf %.3f GB" % (os.path.getsize(BASE) / 1e9))
910
 
911
+ B("5/8 quantize experts -> TQ1_0, splice OUR bytes IN PLACE, validate")
912
  sys.path.insert(0, SRC + "/gguf-py"); import gguf
913
  PP = SRC + "/build/bin/llama-perplexity"
914
+ QF = SRC + "/build/bin/llama-quantize"
915
+ if not (os.path.isfile(PP) and os.path.isfile(QF)):
916
  assert SH("cd %s && cmake -B build -DCMAKE_BUILD_TYPE=Release -DLLAMA_CURL=OFF -DGGML_CUDA=OFF "
917
  "-DGGML_CCACHE=OFF >/dev/null 2>&1 && cmake --build build --config Release -j "
918
+ "--target llama-perplexity llama-quantize 2>&1" % SRC) == 0, "build failed"
919
+
920
+ # THE STEP THAT WAS MISSING (and the assert at blk.0.ffn_gate_exps.weight is what found it):
921
+ # the converter writes an ALL-F16 model. Splicing 1.359 GB of ternary bytes into F16 expert
922
+ # tensors cannot work -- the byte counts differ by 9.5x. The splice target has to be a MIX:
923
+ # experts already TQ1_0, everything else F16. llama-quantize with per-tensor overrides builds
924
+ # exactly that. Options MUST precede the positionals, or llama-quantize reads "--tensor-type"
925
+ # as the nthreads positional ("invalid nthread '--tensor-type'").
926
+ F16GG = BASE
927
+ MIX = K + "/trnr-mix.gguf"
928
+ if not os.path.exists(MIX):
929
+ NT = str(os.cpu_count() or 4)
930
+ ok = False
931
+ for rules in ((r"ffn_(gate|up|down)_exps=TQ1_0",),
932
+ (r"ffn_gate_exps=TQ1_0", r"ffn_up_exps=TQ1_0", r"ffn_down_exps=TQ1_0"),
933
+ (r"blk\.\d+\.ffn_(gate|up|down)_exps=TQ1_0",)):
934
+ cmd = [QF]
935
+ for rr in rules: cmd += ["--tensor-type", rr]
936
+ cmd += [F16GG, MIX, "F16", NT]
937
+ d = subprocess.run([cmd[0], "--dry-run"] + cmd[1:], capture_output=True, text=True)
938
+ if d.returncode != 0:
939
+ P("rejected %-40s %s" % (rules[0], ((d.stdout or "") + (d.stderr or "")).strip()
940
+ .splitlines()[-1][:80]))
941
+ continue
942
+ t0 = time.time()
943
+ r = subprocess.run(cmd, capture_output=True, text=True)
944
+ if r.returncode == 0 and os.path.exists(MIX):
945
+ P("quantized with %-40s %.1f min" % (rules[0], (time.time() - t0) / 60))
946
+ ok = True; break
947
+ tail = ((r.stdout or "") + (r.stderr or "")).strip().splitlines()[-3:]
948
+ for l in tail: P(" " + l[:140])
949
+ assert ok, "llama-quantize refused every --tensor-type spelling -- paste this back"
950
+ P("mix gguf %.3f GB" % (os.path.getsize(MIX) / 1e9))
951
+ _items = {t.name: t for t in gguf.GGUFReader(MIX).tensors}
952
+ _ex = [k for k in _items if k.endswith("_exps.weight")]
953
+ _types = {}
954
+ for k in _ex:
955
+ n = _items[k].tensor_type.name
956
+ _types[n] = _types.get(n, 0) + 1
957
+ P("expert tensors: %d types %s bytes %.4f GB"
958
+ % (len(_ex), _types, sum(int(_items[k].data.nbytes) for k in _ex) / 1e9))
959
+ assert len(_ex) == 48 and all(_items[k].tensor_type.name == "TQ1_0" for k in _ex), \
960
+ "MIX WRONG -- not all 48 expert tensors are TQ1_0: %s" % _types
961
+ assert _items["trnr.rot.weight"].tensor_type.name == "F16", \
962
+ "trnr.rot went ternary -- a ternarized R is not orthogonal and every expert breaks"
963
+ P("MIX CORRECT -- 48 TQ1_0 expert tensors, trnr.rot still F16")
964
+ P("freeing %.2f GB: the all-f16 base, superseded by the mix" % (os.path.getsize(F16GG) / 1e9))
965
+ os.remove(F16GG)
966
+ BASE = MIX
967
+ DU("after the quantize")
968
  DST = K + "/trnr-OLMoE-1B-7B-ternary-tq1_0.gguf"
969
  by = {t.name: t for t in gguf.GGUFReader(BASE).tensors} if os.path.exists(BASE) else {}
970
  off = lambda t: int(np.asarray(t.data_offset).reshape(-1)[0])
 
981
  return float(g[-1]) if g else float("nan")
982
 
983
  if os.path.exists(BASE):
984
+ # BASE is the MIX now (2.33 GB), so measuring the absmax floor is cheap -- and it prices
985
+ # our quantizer: stock TQ1_0 (absmax, no Hessian, no error feedback) rounds ~92% of these
986
+ # rotated weights to zero, which is the ~5000 ppl floor every earlier run measured.
987
+ ppl_floor = ppl_of(BASE)
988
+ P("absmax floor ppl = %.4f (our bytes replace this below)" % ppl_floor)
989
  TQB = pickle.load(open(K + "/v1_g256/tq1_0_bytes.pkl", "rb"))
990
  f = open(BASE, "r+b"); nb = 0
991
  for L in range(16):