Files
leleka/engine/strata/tools/dflash2/run_eval.py
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 12:49:15 +01:00

71 lines
3.6 KiB
Python

"""Offline DFlash2 acceptance on OUR quantized target: walks each prompt's dumped sequence the way the speculative loop would
(anchor, draft block, accept the leading matches of the drafted path against the engine's own greedy tokens, move on)."""
import json, sys, time
from pathlib import Path
import numpy as np
import torch
sys.path.insert(0, r"C:\dflash2-work")
import dflash2_ref as R
W = Path(r"C:\dflash2-work")
names = sys.argv[1].split(",")
blocks = [int(x) for x in sys.argv[2].split(",")] if len(sys.argv) > 2 else [4, 8]
threads = int(sys.argv[3]) if len(sys.argv) > 3 else 6
t0 = time.time()
embed = R.load_dequant(str(W / "embd_iq4xs.bin"), 248320, 2560, R.gguf.GGMLQuantizationType.IQ4_XS)
head = R.load_dequant(str(W / "head_q6k.bin"), 248320, 2560, R.gguf.GGMLQuantizationType.Q6_K)
print("dequantized embed/head in %.0f s" % (time.time() - t0), flush=True)
d = R.Drafter(str(W / "drafter.safetensors"), str(W / "drafter_config.json"), embed, head, threads=threads)
res_path = W / "eval_results.json"
results = json.load(open(res_path)) if res_path.exists() else {}
for name in names:
seqj = json.load(open(W / "p" / ("%s.seq.json" % name)))
prompt, out = seqj["prompt"], seqj["out"]
seq = prompt + out
layers, pos, tok, taps = R.read_taps(str(W / "taps" / ("%s.bin" % name)))
keep = pos < len(seq) # the tail of the last window is padding (zeros) past the sequence
pos, tok, taps = pos[keep], tok[keep], taps[keep]
n = int(pos.max()) + 1
feats = np.zeros((n, taps.shape[1]), np.float32)
seen = np.zeros(n, bool)
for i in range(len(pos)): # the last record of a position wins
feats[pos[i]] = taps[i]
seen[pos[i]] = True
assert tok[i] == seq[pos[i]], ("token mismatch at", pos[i], tok[i], seq[pos[i]])
if not seen[0] and seen[1]: # position 0 goes through the batched prefill (no tap): the next row stands in
feats[0] = feats[1]
seen[0] = True
assert seen.all(), "positions missing: %s" % np.where(~seen)[0][:5]
d.set_context(torch.from_numpy(feats))
print("%s: prompt %d, generated %d, tap records %d, layers %s" % (name, len(prompt), len(out), len(pos), layers), flush=True)
results.setdefault(name, {})
for b in blocks:
s = len(prompt) # the first generated token is the first anchor, as after a prefill
cyc, cond_ok, cond_n = [], np.zeros(b - 1), np.zeros(b - 1)
top1_ok = np.zeros(b - 1)
t1 = time.time()
with torch.no_grad():
while s + b <= len(seq) - 0 and s < n:
path, top1 = d.draft(s, seq[s], b)
truth = seq[s + 1:s + b]
a = 0
while a < b - 1 and path[a] == truth[a]:
a += 1
for i in range(b - 1): # conditional on all earlier correct
if i <= a:
cond_n[i] += 1
cond_ok[i] += (i < a)
if i <= a and top1[i] == truth[i]:
top1_ok[i] += 1
cyc.append(a + 1)
s += a + 1
tau = float(np.mean(cyc))
results[name]["b%d" % b] = {"anchors": len(cyc), "tokens_per_round": tau,
"cond_accept": (cond_ok / np.maximum(cond_n, 1)).round(3).tolist(),
"hist": np.bincount(cyc, minlength=b + 1).tolist()}
print(" block %d: %d rounds, %.3f tokens per round, cond %s (%.0f s)" %
(b, len(cyc), tau, (cond_ok / np.maximum(cond_n, 1)).round(2).tolist(), time.time() - t1), flush=True)
json.dump(results, open(res_path, "w"), indent=1)