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

fix ENOSPC: splice the base IN PLACE and rename it instead of copying 13.85 GB inside a 20 GB quota

Browse files

Also frees the 1.3 GB BITCOS debug blob before the conversion and the 1.36 GB tq1_0_bytes.pkl after the splice, removes a stale partial output, and prints df -h at each heavy step.

Files changed (1) hide show
  1. UPLOAD_1_trnr_ship.py +72 -26
UPLOAD_1_trnr_ship.py CHANGED
@@ -833,18 +833,34 @@ FORK_URL = "https://github.com/fourthyplayz/trnr.cpp" # PUBLISHED: real fork o
833
  B = lambda t: print("=" * 70 + "\n" + t + "\n" + "=" * 70, flush=True)
834
  P = lambda s: print(" " + s, flush=True)
835
  SH = lambda c: subprocess.run(c, shell=True).returncode
 
 
836
  os.makedirs(W, exist_ok=True)
837
 
838
- B("0/8 stage the uploaded source files")
839
- STAGE = {"UPLOAD_1_pack_g256.py": "pack_g256.py", "UPLOAD_2_trnr_convert.py": "trnr_convert.py",
840
- "UPLOAD_3_apply_stage1.py": "apply_stage1.py", "UPLOAD_4_model_card.md": "card.md"}
841
- for up, want in STAGE.items():
842
- if not os.path.exists(K + "/" + want):
843
- shutil.copyfile(K + "/" + up, K + "/" + want)
844
  P("%-22s %.1f KB" % (want, os.path.getsize(K + "/" + want) / 1024))
845
  for f in ("pack_g256.py", "trnr_convert.py", "apply_stage1.py"):
846
  ast.parse(open(K + "/" + f).read())
847
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
848
  B("1/8 clone llama.cpp @ pin")
849
  if not os.path.isdir(SRC + "/src"):
850
  assert SH("git init -q %s && cd %s && git remote add origin https://github.com/ggml-org/llama.cpp.git "
@@ -880,12 +896,19 @@ B("4/8 quantizer (~9 min, GPU) then converter -> rotated base GGUF")
880
  assert SH("cd %s && python -u %s" % (K, K + "/pack_g256.py")) == 0, "pipeline failed -- paste this back"
881
  for f in ("tq1_0_bytes.pkl", "rotation_H.pt"):
882
  P("%-16s %.4f GB" % (f, os.path.getsize(K + "/v1_g256/" + f) / 1e9))
 
 
 
 
 
 
 
883
  BASE = K + "/trnr-base.gguf"
884
  assert SH("cd %s && python -u %s %s --outtype f16 --outfile %s --rot %s/v1_g256/rotation_H.pt"
885
  % (K, K + "/trnr_convert.py", HF, BASE, K)) == 0, "conversion failed -- paste this back"
886
  P("trnr-base.gguf %.3f GB" % (os.path.getsize(BASE) / 1e9))
887
 
888
- B("5/8 build the patched runtime (~5 min), splice, validate")
889
  sys.path.insert(0, SRC + "/gguf-py"); import gguf
890
  PP = SRC + "/build/bin/llama-perplexity"
891
  if not os.path.isfile(PP):
@@ -893,29 +916,52 @@ if not os.path.isfile(PP):
893
  "-DGGML_CCACHE=OFF >/dev/null 2>&1 && cmake --build build --config Release -j "
894
  "--target llama-perplexity 2>&1" % SRC) == 0, "build failed"
895
  DST = K + "/trnr-OLMoE-1B-7B-ternary-tq1_0.gguf"
896
- TQB = pickle.load(open(K + "/v1_g256/tq1_0_bytes.pkl", "rb"))
897
- by = {t.name: t for t in gguf.GGUFReader(BASE).tensors}
898
  off = lambda t: int(np.asarray(t.data_offset).reshape(-1)[0])
899
  def peek(p, o):
900
  with open(p, "rb") as fh:
901
  fh.seek(o); return fh.read(64)
902
- if not os.path.exists(DST): shutil.copyfile(BASE, DST)
903
- f = open(DST, "r+b"); nb = 0
904
- for L in range(16):
905
- for nm, sl, key in (("ffn_gate_exps", slice(0, 8192), "gate_up_proj"),
906
- ("ffn_up_exps", slice(8192, 16384), "gate_up_proj"), ("ffn_down_exps", None, "down_proj")):
907
- buf = np.stack([np.asarray(TQB["%d_%s_%d" % (L, key, e)])[sl] for e in range(64)]).reshape(-1, 54)
908
- k = "blk.%d.%s.weight" % (L, nm); t = by[k]; o = off(t)
909
- assert t.data.nbytes == buf.nbytes and peek(DST, o) == np.asarray(t.data).reshape(-1).tobytes()[:64], k
910
- f.seek(o); f.write(buf.tobytes()); nb += buf.nbytes
911
- f.close()
912
- P("spliced %.4f GB -> %.3f GB" % (nb / 1e9, os.path.getsize(DST) / 1e9))
913
- r = subprocess.run([PP, "-m", DST, "-f", K + "/wiki.test.txt", "-c", "512", "--chunks", "8", "-ngl", "0"],
914
- capture_output=True, text=True)
915
- txt = (r.stdout or "") + (r.stderr or "")
916
- for l in txt.splitlines()[-4:]: P(l[:150])
917
- g = re.findall(r"Final estimate: PPL = ([0-9.]+)", txt)
918
- ppl = float(g[-1]) if g else float("nan")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
919
  F16 = 9.1512
920
  ratio = ppl / F16 if ppl == ppl else float("nan")
921
  P("ppl %.4f ratio vs f16 %.4f = %.3fx" % (ppl, F16, ratio))
 
833
  B = lambda t: print("=" * 70 + "\n" + t + "\n" + "=" * 70, flush=True)
834
  P = lambda s: print(" " + s, flush=True)
835
  SH = lambda c: subprocess.run(c, shell=True).returncode
836
+ DU = lambda tag: P("disk %-24s %s" % (tag, subprocess.run(["df", "-h", K], capture_output=True,
837
+ text=True).stdout.strip().splitlines()[-1]))
838
  os.makedirs(W, exist_ok=True)
839
 
840
+ B("0/8 stage the embedded source files")
841
+ for want in ("pack_g256.py", "trnr_convert.py", "apply_stage1.py", "card.md"):
842
+ assert os.path.exists(K + "/" + want), "%s missing -- the embedded writer above failed" % want
 
 
 
843
  P("%-22s %.1f KB" % (want, os.path.getsize(K + "/" + want) / 1024))
844
  for f in ("pack_g256.py", "trnr_convert.py", "apply_stage1.py"):
845
  ast.parse(open(K + "/" + f).read())
846
 
847
+ B("0.5/8 disk hygiene -- /kaggle/working is a 20 GB quota")
848
+ # The 2026-11-29 run died here with ENOSPC: it copied the 13.85 GB base to a new name
849
+ # before splicing. The splice is size-neutral, so the copy bought nothing and cost
850
+ # everything -- inside a 20 GB quota holding base 13.85 + pkl 1.36 + blobs 1.29.
851
+ _dbg = K + "/v1_g256/ternary_experts.pt"
852
+ if os.path.exists(_dbg):
853
+ P("freeing %.2f GB: ternary_experts.pt is a BITCOS debug blob nothing downstream reads"
854
+ % (os.path.getsize(_dbg) / 1e9))
855
+ os.remove(_dbg)
856
+ _dst = K + "/trnr-OLMoE-1B-7B-ternary-tq1_0.gguf"
857
+ if os.path.exists(_dst) and os.path.exists(K + "/trnr-base.gguf"):
858
+ P("freeing %.2f GB: %s is a leftover -- the splice RENAMES the base, so a DST sitting"
859
+ % (os.path.getsize(_dst) / 1e9, os.path.basename(_dst)))
860
+ P(" next to a BASE can only have come from a run that failed after copying it")
861
+ os.remove(_dst)
862
+ DU("before anything heavy")
863
+
864
  B("1/8 clone llama.cpp @ pin")
865
  if not os.path.isdir(SRC + "/src"):
866
  assert SH("git init -q %s && cd %s && git remote add origin https://github.com/ggml-org/llama.cpp.git "
 
896
  assert SH("cd %s && python -u %s" % (K, K + "/pack_g256.py")) == 0, "pipeline failed -- paste this back"
897
  for f in ("tq1_0_bytes.pkl", "rotation_H.pt"):
898
  P("%-16s %.4f GB" % (f, os.path.getsize(K + "/v1_g256/" + f) / 1e9))
899
+ # the quantizer re-creates ternary_experts.pt (1.3 GB); free it BEFORE the 13.9 GB conversion
900
+ _dbg = K + "/v1_g256/ternary_experts.pt"
901
+ if os.path.exists(_dbg):
902
+ P("freeing %.2f GB: ternary_experts.pt (debug blob, regenerable in ~9 min)"
903
+ % (os.path.getsize(_dbg) / 1e9))
904
+ os.remove(_dbg)
905
+ DU("after the quantizer")
906
  BASE = K + "/trnr-base.gguf"
907
  assert SH("cd %s && python -u %s %s --outtype f16 --outfile %s --rot %s/v1_g256/rotation_H.pt"
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):
 
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])
921
  def peek(p, o):
922
  with open(p, "rb") as fh:
923
  fh.seek(o); return fh.read(64)
924
+
925
+ def ppl_of(path, chunks=8):
926
+ r = subprocess.run([PP, "-m", path, "-f", K + "/wiki.test.txt", "-c", "512",
927
+ "--chunks", str(chunks), "-ngl", "0"], capture_output=True, text=True)
928
+ t = (r.stdout or "") + (r.stderr or "")
929
+ for l in t.splitlines()[-3:]: P(l[:150])
930
+ g = re.findall(r"Final estimate: PPL = ([0-9.]+)", t)
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):
942
+ for nm, sl, key in (("ffn_gate_exps", slice(0, 8192), "gate_up_proj"),
943
+ ("ffn_up_exps", slice(8192, 16384), "gate_up_proj"),
944
+ ("ffn_down_exps", None, "down_proj")):
945
+ buf = np.stack([np.asarray(TQB["%d_%s_%d" % (L, key, e)])[sl] for e in range(64)]).reshape(-1, 54)
946
+ k = "blk.%d.%s.weight" % (L, nm); t = by[k]; o = off(t)
947
+ assert t.data.nbytes == buf.nbytes and peek(BASE, o) == np.asarray(t.data).reshape(-1).tobytes()[:64], k
948
+ f.seek(o); f.write(buf.tobytes()); nb += buf.nbytes
949
+ f.close()
950
+ P("spliced %.4f GB of ternary bytes into the base (size-neutral, 64 B peek-asserted per tensor)"
951
+ % (nb / 1e9))
952
+ # rename, not copy: a copy needs another 13.85 GB and the quota is 20 GB total
953
+ os.replace(BASE, DST)
954
+ P("renamed -> %s (%.3f GB)" % (os.path.basename(DST), os.path.getsize(DST) / 1e9))
955
+ else:
956
+ P("no base present -- reusing the spliced %s from this session (%.3f GB)"
957
+ % (os.path.basename(DST), os.path.getsize(DST) / 1e9))
958
+ assert os.path.exists(DST), "neither the base nor the spliced model exists -- re-run from 4/8"
959
+ if os.path.exists(K + "/v1_g256/tq1_0_bytes.pkl"):
960
+ P("freeing %.2f GB: tq1_0_bytes.pkl is now spliced into the model"
961
+ % (os.path.getsize(K + "/v1_g256/tq1_0_bytes.pkl") / 1e9))
962
+ os.remove(K + "/v1_g256/tq1_0_bytes.pkl")
963
+ DU("after the splice")
964
+ ppl = ppl_of(DST)
965
  F16 = 9.1512
966
  ratio = ppl / F16 if ppl == ppl else float("nan")
967
  P("ppl %.4f ratio vs f16 %.4f = %.3fx" % (ppl, F16, ratio))