Files
leleka/engine/strata/tools/dflash2/test_port.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

46 lines
2.4 KiB
Python

import sys, importlib.util, json, torch
sys.path.insert(0, r"C:\dflash2-work")
import dflash2_ref as R
torch.manual_seed(0)
# the reference (z-lab) model on transformers 4.57
spec = importlib.util.spec_from_file_location("ghm", r"C:\Users\AI-Server\Desktop\Strata\phaseA-tests\ornith_tmp\gh_model.py")
ghm = importlib.util.module_from_spec(spec); spec.loader.exec_module(ghm)
from transformers.models.qwen3.modeling_qwen3 import Qwen3Config
cfgd = json.load(open(r"C:\dflash2-work\drafter_config.json"))
c = Qwen3Config(**{k: v for k, v in cfgd.items() if k in ("hidden_size","intermediate_size","num_hidden_layers","num_attention_heads","num_key_value_heads","head_dim","vocab_size","rms_norm_eps","max_position_embeddings","attention_bias","sliding_window","layer_types","use_sliding_window","max_window_layers")})
c.rope_theta = cfgd["rope_parameters"]["rope_theta"]
c.dflash_config = cfgd["dflash_config"]
c.is_causal = False
c.num_target_layers = 48
m = ghm.DFlash2DraftModel(c)
from safetensors.torch import load_file
sd = load_file(r"C:\dflash2-work\drafter.safetensors")
sd = {(k + ".weight" if k.startswith("candidate_selector.") and k.endswith("codebook") else k): v for k, v in sd.items()}
miss = m.load_state_dict(sd, strict=False); print("missing", miss.missing_keys[:5], "unexpected", miss.unexpected_keys[:5])
m = m.float().eval()
# my port with a tiny random embed/head
V = 248320; H = 2560
embed = torch.randn(V, H) * 0.02
head = torch.randn(V, H) * 0.02
d = R.Drafter(r"C:\dflash2-work\drafter.safetensors", r"C:\dflash2-work\drafter_config.json", embed, head)
n = 300; b = 8; s = 250
feats = torch.randn(n, 5 * H) * 2.0
d.set_context(feats)
anchor = 1234
mine_path, _ = d.draft(s, anchor, b)
ids = torch.tensor([[anchor] + [d.mask_id] * (b - 1)])
noise = embed[ids]
pos = torch.arange(0, s + b)[None] # context 0..s-1 then block
with torch.no_grad():
out = m(position_ids=pos, noise_embedding=noise, target_hidden=feats[None, :s], past_key_values=None, use_cache=False)
print("shape", out.shape, "mine", d.last_all.shape)
err = (out[0] - d.last_all).abs().max().item()
print("max abs err all rows", err, "ref scale", out.abs().mean().item())
# the selector path against the reference
logits = (out[:, 1:] @ head.T)
class Tgt: pass
with torch.no_grad():
path, idx, q = m.candidate_selector.select(out[:, 1:], logits, torch.tensor([anchor]), 0.0)
print("ref path ", path[0].tolist())
print("mine path", mine_path)