okami.mom/strata

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

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

main
2.2 KiB60 linesraw
1"""Does a Q8_0 drafter (the matrices block-quantized like ggml Q8_0) keep the acceptance? Walk over two prompts, block 4 and 8."""
2import json, sys
3from pathlib import Path
4import numpy as np
5import torch
6sys.path.insert(0, r"C:\dflash2-work")
7import dflash2_ref as R
8
9W = Path(r"C:\dflash2-work")
10embed = R.load_dequant(str(W / "embd_iq4xs.bin"), 248320, 2560, R.gguf.GGMLQuantizationType.IQ4_XS)
11head = R.load_dequant(str(W / "head_q6k.bin"), 248320, 2560, R.gguf.GGMLQuantizationType.Q6_K)
12d = R.Drafter(str(W / "drafter.safetensors"), str(W / "drafter_config.json"), embed, head, threads=6)
13d.enable_fast_head()
14
15
16def q8_0(w):
17    """ggml Q8_0 along the last dimension: blocks of 32, scale = amax/127 stored as fp16, int8 codes"""
18    shp = w.shape
19    x = w.reshape(-1, 32)
20    amax = x.abs().amax(dim=1, keepdim=True)
21    sc = (amax / 127.0).to(torch.float16).float()
22    sc_safe = torch.where(sc == 0, torch.ones_like(sc), sc)
23    q = torch.round(x / sc_safe).clamp(-127, 127)
24    return (q * sc).reshape(shp)
25
26
27def run(name, b, tag):
28    seqj = json.load(open(W / "p" / ("%s.seq.json" % name)))
29    seq = seqj["prompt"] + seqj["out"]
30    layers, pos, tok, taps = R.read_taps(str(W / "taps" / ("%s.bin" % name)))
31    keep = pos < len(seq)
32    pos, taps = pos[keep], taps[keep]
33    feats = np.zeros((int(pos.max()) + 1, taps.shape[1]), np.float32)
34    for i in range(len(pos)):
35        feats[pos[i]] = taps[i]
36    feats[0] = feats[1]
37    d.set_context(torch.from_numpy(feats))
38    s = len(seqj["prompt"])
39    cyc = []
40    with torch.no_grad():
41        while s + b <= len(seq):
42            path, _ = d.draft(s, seq[s], b)
43            truth = seq[s + 1:s + b]
44            a = 0
45            while a < b - 1 and path[a] == truth[a]:
46                a += 1
47            cyc.append(a + 1)
48            s += a + 1
49    print("%s %s block %d: %.3f tokens per round (%d rounds)" % (tag, name, b, np.mean(cyc), len(cyc)), flush=True)
50
51
52names = sys.argv[1].split(",")
53orig = {k: v.clone() for k, v in d.w.items() if k.endswith("proj.weight") or k == "fc.weight" or k.endswith("kernel_projection.weight")}
54for tag in ("fp32", "q8_0"):
55    if tag == "q8_0":
56        for k in orig:
57            d.w[k] = q8_0(orig[k])
58    for n in names:
59        run(n, 4, tag)
60        run(n, 8, tag)