Architecture rethink model (python)

Source reports/results/DRAFTER_SPEED_RESEARCH_20261107/arch_rethink_20261117/arch_model.py · source md5 3882bc4481742c6eeac1e3fd61177437 · EAGLE4 FPGA archive, 2026-11-18

#!/usr/bin/env python3
"""Per-item / per-level cycle model for the 7-card architecture rethink (2026-11-17).
[M] inputs are quoted in ARCH_RETHINK_7CARD_20261117.md section 1; everything computed here is [P]."""
import math, json
CYC_US = 0.015151  # 66.001 MHz drafter clock

def llama(N, T, W=1, fuse=False, tree4=False, ar=549, ctx=566):
    """cycles for one batch of W sibling items on N uniform cards, T = MAC per drafter cycle per card."""
    mac = dict(g4=56.62e6, g6=9.44e6, g7g=25.17e6, g7u=25.17e6, g9=25.17e6)
    arr = sum(mac.values()) / (N * T)                      # array, per sibling
    g1 = 66 if fuse else 456                               # G1FUSE -390
    a8q = (253 - (194 if fuse else 0)) + 157 + 157 + 82    # G4 / G7g / G7u / G9 prologues, A8Q_OVL -194
    norm = (491 - (387 if fuse else 0)) + 12               # R3 + pre-MLP norm (R3_EARLY on), NORMFUSE -387
    g5 = 1420 if W == 1 else 1420 * 4 / 3                  # 1 group = 3 sweeps (P2W 8); 2 siblings = 2 groups, G5_OVL 4 sweeps
    beats = 192
    ar2 = 2 * (ar + (W - 1) * beats)                       # o-proj + down all-reduce; W widens the payload only
    g7p = 8192 / N / 16
    fin = 466 + 226 + 22                                   # G11, GLM, G12 kick
    tiles = math.ceil(128256 / N / 256)
    tree = 216 + tiles * (4 if tree4 else 8)
    g8 = 737 * 2 / N
    merge = 120 + 60                                       # top-K merge + G8 merge, narrow syncs
    lvl = 261 + 200                                        # LVL_SEL/OUT/XCH + EMBG
    once = g1 + a8q + norm + fin + 216 + merge + lvl + ar2 + g5
    per = arr + g7p + (tree - 216) + g8
    return once + W * per, dict(arr=arr, once=once, per=per, tree=tree, g8=g8, g5=g5, ar2=ar2)

def qwen(N, T, W=1, fuse=False, tree_floor=False, ar=677, ctx=300):
    mac = 435.16e6
    arr = mac / (N * T)
    heads = math.ceil(40 / N)
    groups = math.ceil(heads * W / 8)
    sweeps = 3 if groups == 1 else 2 * groups              # G5_OVL form for >1 group
    g5 = sweeps * ctx + 307
    g1 = 713
    norm = 1377 - (900 if fuse else 0)
    beats = 320
    ar2 = 2 * (ar + (W - 1) * beats)
    silu = 16384 / N / 16
    fin = 723 + 24 + 13
    tiles = math.ceil(248320 / N / 256)
    tree = tiles * (7 if tree_floor else 14)
    g8 = 2000 * 2 / N
    merge = 180; lvl = 461; prol = 100
    once = g1 + norm + fin + merge + lvl + prol + ar2 + g5
    per = arr + silu + tree + g8
    return once + W * per, dict(arr=arr, once=once, per=per, tree=tree, g8=g8, g5=g5, ar2=ar2)

def toks_llama(tree_us, hit=0.0, pass_through=0.516):
    A, ha, jw, base_tree = 5.176, 0.510, 1.501, 2622.0
    jw2 = max(0.0, jw - pass_through * (base_tree - tree_us) / 1000.0)
    step = A + ha + (1 - hit) * jw2
    return 961.43 * 7.187 / step, step

if __name__ == "__main__":
    print("=== LLAMA  (today COMBO3 [M]: item 8,240 cyc = 124.8 us; level (2 items) 238.3 us; d11 tree 2,622 us)")
    rows = []
    for name, T in (("T1 16,384 (L512@66 or L256@132)", 16384), ("T2 32,768 (L512@132)", 32768), ("T2.5 41,300 (L512@166)", 41300)):
        for N in (6, 7):
            for W, fuse, t4, tag in ((1, False, False, "A"), (2, False, False, "A+C"), (2, True, True, "A+C+fuse+TREE4")):
                c, d = llama(N, T, W, fuse, t4)
                lvl = c if W == 2 else 2 * c
                tree = (c if W == 1 else c) and (llama(N, T, 1, fuse, t4)[0] + 10 * lvl) * CYC_US
                tk, st = toks_llama(tree)
                print(f"{name:32s} N={N} {tag:16s} batch {c:7.0f} cyc  level {lvl:7.0f} cyc = {lvl*CYC_US:6.1f} us  arr/sib {d['arr']:5.0f}  tree d11 {tree:6.0f} us  step {st:.3f}  tok/s {tk:6.1f}")
    print("\nsensitivity of the all-reduce term (N=7, T2, A+C+fuse+TREE4):")
    for ar in (150, 300, 549, 800, 1200):
        c, d = llama(7, 32768, 2, True, True, ar=ar)
        tree = (llama(7, 32768, 1, True, True, ar=ar)[0] + 10 * c) * CYC_US
        print(f"  AR {ar:5d} cyc -> level {c:6.0f} cyc  AR share {d['ar2']/c*100:4.1f} %  tree {tree:6.0f} us  tok/s {toks_llama(tree)[0]:6.1f}")
    print("\n(D) draft-ahead, Llama: tok/s vs hit rate, for today's tree and for a 1,400 us tree")
    for h in (0, .2, .3, .4, .5, .6, .8, 1.0):
        print(f"  hit {h:.1f}: today {toks_llama(2622, h)[0]:7.1f}   fast tree {toks_llama(1400, h)[0]:7.1f}")
    print("\n=== QWEN  (today xsim [M]: ATT 15,757 + MLPV 15,616 per item; d7 W2 = 13 items; serve tree wait 5.82 ms, tree RPC 8.18 ms)")
    for name, T in (("T0 8,192 (L256@66, today)", 8192), ("T1 16,384 (L256@132)", 16384), ("T2 32,768 (L512@132 / +DSP)", 32768)):
        for N in (6, 7):
            for W, fuse, tf, tag in ((1, False, False, "A"), (2, False, False, "A+C"), (2, True, True, "A+C+fuse+floor")):
                c, d = qwen(N, T, W, fuse, tf)
                lvl = c if W == 2 else 2 * c
                tree = (qwen(N, T, 1, fuse, tf)[0] + 6 * lvl) * CYC_US / 1000
                print(f"{name:30s} N={N} {tag:15s} batch {c:7.0f} cyc  level {lvl*CYC_US:7.1f} us  arr/sib {d['arr']:6.0f}  d7 tree {tree:5.2f} ms")

def role_split(a, m, H=2680, F_us=427.6, cal=2622/2741.0):
    item = max(a, (a + m + H) / 2)
    return (F_us + 21 * item * CYC_US) * cal, item

if __name__ == "__main__":
    print("\n=== today's role split, pipeline law tree = F + 21*max(a,(a+m+H)/2), calibrated x0.957 to the COMBO3 census")
    for tag, a, m in (("COMBO3 + M1361 [M inputs]", 6533, 5327),
                      ("+ NORMFUSE/G1FUSE/A8Q_OVL (-971 a)", 5562, 5327),
                      ("+ ATT array 132 MHz (-853 a, xsim)", 4709, 5327),
                      ("+ TREE4 (-1,100 m)", 4709, 4227),
                      ("+ H halved (1,340)", 4709, 4227)):
        H = 1340 if "halved" in tag else 2680
        t, it = role_split(a, m, H)
        print(f"  {tag:40s} a {a} m {m} item {it:6.0f} tree {t:6.0f} us  tok/s {toks_llama(t)[0]:6.1f}")
    print("\nbreakdown, Llama N=7 T1, W2 batch, fuse+TREE4:")
    c, d = llama(7, 16384, 2, True, True)
    print(f"  level {c:.0f}: G5 {d['g5']:.0f} ({d['g5']/c*100:.0f}%)  all-reduce x2 {d['ar2']:.0f} ({d['ar2']/c*100:.0f}%)  array 2x{d['arr']:.0f} ({2*d['arr']/c*100:.0f}%)  tree scan 2x{d['tree']-216:.0f}  G8 2x{d['g8']:.0f}  other once {d['once']-d['g5']-d['ar2']:.0f}")
    c, d = qwen(7, 16384, 2, True, True)
    print(f"breakdown, Qwen N=7 T1, W2 batch, fuse+floor: level {c:.0f}: array 2x{d['arr']:.0f} ({2*d['arr']/c*100:.0f}%) G5 {d['g5']:.0f} ({d['g5']/c*100:.0f}%) AR {d['ar2']:.0f} ({d['ar2']/c*100:.0f}%) tree 2x{d['tree']:.0f} G8 2x{d['g8']:.0f}")
    # (D) hit rates from the GPU-only d11 per-level miss hazards [M]
    hz = [6.1, 7.3, 11.4, 11.4, 13.2, 13.0, 12.5, 11.3, 15.1, 14.0, 16.4]
    S = 1.0; out = []
    for i, h in enumerate(hz):
        S *= 1 - h / 100; out.append((i + 1, S))
    print("\nLlama survival S(depth) from the hazards:", " ".join(f"d{d}:{s:.3f}" for d, s in out))
    print("  full-accept d11 (product) %.3f vs logged 0.244; x(1-0.164) next-token = hit %.3f" % (out[-1][1], 0.244 * (1 - 0.164)))
    # Qwen: geometric hazard fitted to the mean accept
    def fitq(acc, d):
        lo, hi = 0.0, 0.9
        for _ in range(60):
            q = (lo + hi) / 2
            s = sum((1 - q) ** k for k in range(1, d + 1)) + 1
            lo, hi = (q, hi) if s > acc else (lo, q)
        return q
    for tag, acc in (("humaneval:80 7.4166", 7.4166), ("he4+mt4 mix 4.0674", 4.0674)):
        q = fitq(acc, 7)
        print(f"Qwen {tag}: fitted per-level miss {q*100:.1f} %  S(7) {(1-q)**7:.2f}  hit {(1-q)**8:.2f}  accept at d11 {1+sum((1-q)**k for k in range(1,12)):.2f}  d15 {1+sum((1-q)**k for k in range(1,16)):.2f}")
    print("Qwen round (he4+mt4, w3b [M] 36.50 ms; card RPC 12.54, IPC-inject 1.14): hit h removes 13.68 ms")
    for h in (0, .23, .5, .84, 1.0):
        r = 36.50 - h * 13.68
        print(f"  h {h:.2f}: round {r:5.1f} ms  ({36.50/r:.2f}x of today)  vs GPU-only 21.9 ms")