tools/dsv41/engram_fold.py --gate-kv-from: make the ple table the only variable, and the answer it gave

Deriving their engram.gate from their k*q and transposing their wkv scored 0.409 then 2.996 KL - both my arithmetic, not
their data (their k/q/wkv are stored transposed and the gate is not k*q in the engine's layout).  --gate-kv-from <pack>
copies those two tensors from a pack that is known good, so a pack can carry their ple table and nothing else of theirs
differ.  That is how the comparison the plan asks for was finally made (numbers in EACH-PC-PLAN/R9700-BOX.md, step 2):
their 110.6 GB IQ4_NL table scores KL 0.16787 / ppl 3.627 against 0.16502 / 3.588 for our 203 GB MXFP8 one - +0.003 KL,
inside this engine's run-to-run placement noise - while saving 92 GB of disk.

Also fixes the fold's reads: Gguf's tensor offsets are relative to the shard's data section (pack_gguf adds data_start),
so the fold must add it too; without that every tensor came out as NaN.
This commit is contained in:
Niko1221 committed 2026-10-23 15:10:00 +02:00
1 parent 52543768c7
commit 226d73b45b
2 files changed
+36

No files matched your search

+10
View File
@@ -6,6 +6,9 @@
// are plain .cpp files that launch kernels and copy buffers, and they used to include <hip/hip_runtime.h> unconditionally,
// which no CUDA toolkit ships: every CUDA box had to pass a shim on -I to build them (found on l620 and z440, 2026-10-23).
// This header is that shim, in the tree: HIP names on a HIP build, mapped onto the CUDA runtime otherwise.
//
// Two details that a first version got wrong (z440, 2026-10-23): HIP's hipMalloc is a *template* taking T**, so a plain
// void** overload does not bind `hipMalloc(&uint8_ptr)`; and hipMemset has no CUDA counterpart under that name.
#pragma once
#if defined(__HIP_PLATFORM_AMD__) || defined(__HIPCC__)
@@ -21,10 +24,17 @@ constexpr int hipMemcpyHostToDevice = cudaMemcpyHostToDevice;
constexpr int hipMemcpyDeviceToHost = cudaMemcpyDeviceToHost;
inline hipError_t hipMalloc(void** p, size_t n) { return cudaMalloc(p, n); }
template <typename T>
inline hipError_t hipMalloc(T** p, size_t n) { return cudaMalloc(reinterpret_cast<void**>(p), n); }
inline hipError_t hipFree(void* p) { return cudaFree(p); }
inline hipError_t hipMemcpy(void* dst, const void* src, size_t n, int kind) {
return cudaMemcpy(dst, src, n, static_cast<cudaMemcpyKind>(kind));
}
template <typename T>
inline hipError_t hipMemcpy(T* dst, const T* src, size_t n, int kind) {
return cudaMemcpy(static_cast<void*>(dst), static_cast<const void*>(src), n, static_cast<cudaMemcpyKind>(kind));
}
inline hipError_t hipMemset(void* p, int v, size_t n) { return cudaMemset(p, v, n); }
inline hipError_t hipDeviceSynchronize() { return cudaDeviceSynchronize(); }
inline const char* hipGetErrorString(hipError_t e) { return cudaGetErrorString(e); }
inline hipError_t hipEventCreate(hipEvent_t* e) { return cudaEventCreate(e); }
+26
View File
@@ -23,6 +23,7 @@ import argparse
import json
import os
import pathlib
import re
import sys
import numpy as np
@@ -112,6 +113,7 @@ def main() -> int:
ap.add_argument("--model", required=True, help="the folder of the quant's GGUF shards")
ap.add_argument("--out", required=True, help="the override folder to write (dense.json + dense.bin)")
ap.add_argument("--layers", default="", help="comma list of Engram layers; default: every layer with an engram_k")
ap.add_argument("--gate-kv-from", default="", help="a known-good pack to copy engram.gate/wkv from (the ple table stays the only difference)")
a = ap.parse_args()
model = pathlib.Path(a.model).expanduser().resolve()
out = pathlib.Path(a.out).expanduser().resolve()
@@ -138,6 +140,30 @@ def main() -> int:
idx[name] = [tname, "dense.bin", off, len(data), shape]
off += len(data)
# --gate-kv-from PACK: take the gate and the kv from a pack that is known good (the engine's own layout, already in the
# right orientation) instead of deriving them from their k/q/wkv. That leaves the ple table as the only difference
# between the two packs, which is the only way to score their IQ4_NL Engram against our MXFP8 one.
if a.gate_kv_from:
src = pathlib.Path(a.gate_kv_from).expanduser().resolve()
md = [l.split(None, 1)[1] for l in (src / "ds4.txt").read_text().splitlines() if l.startswith("model_dir ")][0]
files, want = {}, {}
for line in (src / "tensors.txt").read_text().splitlines():
f = line.split()
if not f:
continue
if f[0] == "file":
files[int(f[1])] = " ".join(f[2:])
elif f[0] == "t" and re.match(r"layers\.\d+\.engram\.(gate|wkv)\.weight$", f[1]):
want[f[1]] = (f[2], int(f[3]), int(f[4]), int(f[5]), [int(x) for x in f[6:]])
for name, (tname, fi, o, nb, shape) in sorted(want.items()):
p = (pathlib.Path(md) / files[fi]).resolve()
add(name, tname, np.fromfile(p, dtype=np.uint8, count=nb, offset=o).tobytes(), shape)
print("%s: copied from %s (%s, %d bytes)" % (name, src.name, tname, nb))
(out / "dense.bin").write_bytes(b"".join(blobs))
json.dump(idx, open(out / "dense.json", "w"), indent=1)
print("wrote %s (%.2f MB), dense.json with %d tensors" % (out / "dense.bin", os.path.getsize(out / "dense.bin") / 1e6, len(idx)))
return 0
for il in layers:
kg, kt = find(gs, "blk.%d.engram_k.weight" % il)
qg, qt = find(gs, "blk.%d.engram_q.weight" % il)