okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge commit 'b8d604d28' into f-clean-1ab0fc432ed

main
3.0 KiB55 linesraw
1# A lab script, not a unit test: it needs the owner PC's work folder (the drafter's weights and config) and the reference model
2# file under phaseA-tests/ornith_tmp. pytest collects it as a module, so it says so and skips when they are not there.
3import os
4_LAB = (r"C:\dflash2-work", r"C:\Users\AI-Server\Desktop\Strata\phaseA-tests\ornith_tmp\gh_model.py")
5if not all(os.path.exists(p) for p in _LAB):
6    try:
7        import pytest
8        pytest.skip("needs the DFlash2 lab files on the owner PC: " + " and ".join(_LAB), allow_module_level=True)
9    except ImportError:
10        raise SystemExit("needs the DFlash2 lab files on the owner PC: " + " and ".join(_LAB))
11import sys, importlib.util, json, torch
12sys.path.insert(0, r"C:\dflash2-work")
13import dflash2_ref as R
14torch.manual_seed(0)
15# the reference (z-lab) model on transformers 4.57
16spec = importlib.util.spec_from_file_location("ghm", r"C:\Users\AI-Server\Desktop\Strata\phaseA-tests\ornith_tmp\gh_model.py")
17ghm = importlib.util.module_from_spec(spec); spec.loader.exec_module(ghm)
18from transformers.models.qwen3.modeling_qwen3 import Qwen3Config
19cfgd = json.load(open(r"C:\dflash2-work\drafter_config.json"))
20c = 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")})
21c.rope_theta = cfgd["rope_parameters"]["rope_theta"]
22c.dflash_config = cfgd["dflash_config"]
23c.is_causal = False
24c.num_target_layers = 48
25m = ghm.DFlash2DraftModel(c)
26from safetensors.torch import load_file
27sd = load_file(r"C:\dflash2-work\drafter.safetensors")
28sd = {(k + ".weight" if k.startswith("candidate_selector.") and k.endswith("codebook") else k): v for k, v in sd.items()}
29miss = m.load_state_dict(sd, strict=False); print("missing", miss.missing_keys[:5], "unexpected", miss.unexpected_keys[:5])
30m = m.float().eval()
31# my port with a tiny random embed/head
32V = 248320; H = 2560
33embed = torch.randn(V, H) * 0.02
34head = torch.randn(V, H) * 0.02
35d = R.Drafter(r"C:\dflash2-work\drafter.safetensors", r"C:\dflash2-work\drafter_config.json", embed, head)
36n = 300; b = 8; s = 250
37feats = torch.randn(n, 5 * H) * 2.0
38d.set_context(feats)
39anchor = 1234
40mine_path, _ = d.draft(s, anchor, b)
41ids = torch.tensor([[anchor] + [d.mask_id] * (b - 1)])
42noise = embed[ids]
43pos = torch.arange(0, s + b)[None]       # context 0..s-1 then block
44with torch.no_grad():
45    out = m(position_ids=pos, noise_embedding=noise, target_hidden=feats[None, :s], past_key_values=None, use_cache=False)
46print("shape", out.shape, "mine", d.last_all.shape)
47err = (out[0] - d.last_all).abs().max().item()
48print("max abs err all rows", err, "ref scale", out.abs().mean().item())
49# the selector path against the reference
50logits = (out[:, 1:] @ head.T)
51class Tgt: pass
52with torch.no_grad():
53    path, idx, q = m.candidate_selector.select(out[:, 1:], logits, torch.tensor([anchor]), 0.0)
54print("ref path ", path[0].tolist())
55print("mine path", mine_path)