Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
115 lines
8.5 KiB
Python
115 lines
8.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Offline roofline model for docs/roofline.md (Python is allowed offline: it is not part of serving).
|
|
|
|
Constants of the model files are counted (data/iq3_s/index.txt, native_experts.txt); ceilings come from the microbenchmark logs
|
|
(tools/microbench, parsed by --logs) or the defaults below; every other input is labelled A (assumption) in the output notes.
|
|
|
|
roofline_model.py [--ceilings ceilings.json] [--tokens 8192]
|
|
"""
|
|
import argparse, json
|
|
|
|
# ---------------- model constants
|
|
H, L, L_QSA = 2560, 48, 12
|
|
L_GDN = L - L_QSA
|
|
NE, TOPK = 512, 10
|
|
S_AVG = 98_227_200 / 48 # bytes per routed expert, mean over layers (2.0464 MB)
|
|
EXPERT_TOTAL = 98_227_200 * 512 # 50.29 GB
|
|
MAC_GDN = 2560 * 10240 + 2560 * 6144 + 6144 * 2560 + 2 * 2560 * 48 + 3 * 2560 * 640 + 2560 * 512 + 4 * 10240 * 320
|
|
MAC_QSA = 2560 * 12288 + 2 * 2560 * 512 + 6144 * 2560 + 2560 * 512 + 2560 * 128 + 3 * 2560 * 640 + 2560 * 512 + 4 * 10240 * 320
|
|
MAC_EXP = TOPK * 3 * 2560 * 640
|
|
MAC_ATT = 2 * 24 * 256 * 2048
|
|
BYTES_GDN, BYTES_QSA, BYTES_HEAD = 96_241_536, 90_509_312, 675_430_400
|
|
DENSE_WINDOW = L_GDN * BYTES_GDN + L_QSA * BYTES_QSA + BYTES_HEAD # 5.23 GB read once per decode window
|
|
|
|
# ---------------- ceilings (5090). Measured in tools/microbench jobs 905-909 unless a source is named.
|
|
CEIL_5090 = dict(
|
|
pcie_gbps=46.0, # job 907: pinned/registered 2 MB copies, one stream 46.3 (8 MB and larger 51.3; 3 x 682 KB pieces 40.3)
|
|
pcie_contended_gbps=34.0, # job 909: with 1-2 CPU readers streaming (36.1 / 33.4)
|
|
cpu_contended_gbps=40.0, # job 909: the readers' rate in that mix (38.3 / 41.1)
|
|
dram_gbps=74.5, # job 909: CPU + PCIe sum, max over reader counts (74.3-74.7); job 987-b: 74 with 15 readers
|
|
cpu_alone_gbps=61.0, # job 908: two readers on different CCDs; one CCD alone tops at 51-54
|
|
hbm_gbps=1693.0, # job 906 read kernel (nameplate 1792)
|
|
bf16_tflops=229.0, # job 906 cuBLAS on the model's shapes at T=8192 (peak 243.5 square)
|
|
int8_tops=450.0, # A: 65% of the cuBLAS int8 peak 695 TOPS (jobs 906: 620-870 on the model's shapes) for grouped MMQ tiles
|
|
graph_gap_us=0.51, # job 905 kernel_to_kernel_gap_graph_chain (timer quantum 0.26 us)
|
|
)
|
|
|
|
def prefill_table(c, T=8192):
|
|
bw, bf, i8 = c['hbm_gbps'] * 1e9, c['bf16_tflops'] * 1e12, c['int8_tops'] * 1e12
|
|
rows = []
|
|
def add(name, hbm, flops, rate, cur, note, floor_ms=None):
|
|
t = max(hbm / bw * 1e3, flops / rate * 1e3 if flops else 0.0)
|
|
rows.append(dict(step=name, hbm=hbm, flops=flops, floor=t if floor_ms is None else floor_ms, now=cur, note=note))
|
|
f = lambda macs: 2 * T * macs
|
|
add('GDN qkv/z/out GEMMs (36)', L_GDN * 57.6e6 + L_GDN * T * (2560 + 10240 + 6144) * 4 * 2, f((2560 * 10240 + 2560 * 6144 + 6144 * 2560) * L_GDN), bf, 148, 'cuBLAS at the ceiling')
|
|
add('GDN conv/gates/L2/out-norm (36)', L_GDN * 6 * T * 10240 * 4, 0, bf, 49, 'A: 6 passes over the 335 MB qkv tensor')
|
|
add('GDN recurrence (36)', 0, 0.93e12, bf, 150, 'latency chain: 2 x 128 dependent FMA + update per token, ~1280 cycles @2.9 GHz (E)',
|
|
floor_ms=L_GDN * T * 1280 / 2.9e9 * 1e3)
|
|
add('QSA q/k/v/out + indexer GEMMs (12)', L_QSA * 52e6, f((2560 * 12288 + 2 * 2560 * 512 + 6144 * 2560 + 2560 * 640) * L_QSA), bf, 56, 'GEMM ceiling')
|
|
add('QSA index/norm/rope/select (12)', L_QSA * 4 * T * 24 * 256 * 4, 0, bf, 42, 'A: 4 passes over q')
|
|
add('QSA attention, 2048 cells (12)', L_QSA * (T // 16) * 2048 * 1024, f(MAC_ATT * L_QSA), 0.5 * bf, 113, 'A: mma flash-style at 50% of the bf16 ceiling')
|
|
add('KV append, gate, misc (12)', L_QSA * 2 * T * 512 * 4 * 3, 0, bf, 17, '')
|
|
add('hyper-connection GEMMs (96 halves)', 2 * L * T * (10240 * 2 + 320 * 4) * 2, f(4 * 10240 * 320 * L), bf, 71, 'K or N = 320 shapes')
|
|
add('hyper-connection norm/mix/inject (96)', 2 * L * T * 10240 * 4 * 3 + 2 * L * T * 2560 * 4 * 2, 0, bf, 110, 'A: floor = read R, write R, read gated per half')
|
|
add('router + shared expert (48)', L * T * 2560 * 4 * 3 + L * 7.2e6, f((3 * 2560 * 640 + 2560 * 512) * L), bf, 25, 'A: current not separately profiled')
|
|
add('routed experts MMQ (48)', L * (T * TOPK) * (2560 * 1.125 + 1280 * 4 + 640 * 1.125 + 2560 * 4 * 2), f(MAC_EXP * L), i8, 330, 'integer MMQ; current = ring with expert DMA skipped (job 705)')
|
|
add('PLE, init, validate, publish', 0, 0, bf, 25, '', floor_ms=3)
|
|
return rows
|
|
|
|
def chunk_ideals(c, rows, T, routed_frac, resident_gb):
|
|
gpu = sum(r['floor'] for r in rows)
|
|
pcie = max(0.0, EXPERT_TOTAL * routed_frac - resident_gb * 1e9) / (c['pcie_gbps'] * 1e9) * 1e3
|
|
return gpu, pcie, T / max(gpu, pcie) * 1e3
|
|
|
|
def union_experts(rows):
|
|
return TOPK * rows ** 0.85 # fit to the per-layer-window counts of job 984/985 (T = 2..3.8 rows -> 18..34)
|
|
|
|
def window(c, rows, h, chain_ms, draft_ms, tok, x=None):
|
|
"""One decode window: serial = chain + miss phase; overlapped = max of the two (perfect prefetch, E)."""
|
|
U = union_experts(rows)
|
|
miss = U * (1 - h) * L * S_AVG
|
|
t_miss = miss / (c['dram_gbps'] * 1e9) * 1e3
|
|
P, C = c['pcie_contended_gbps'], c['cpu_contended_gbps']
|
|
# per layer: tau_g = chain share, ms = missed bytes. Prefetch starts the DMA leg tau_g earlier; the CPU leg needs the activations so it cannot.
|
|
tau, mb = chain_ms / L * 1e-3, miss / L
|
|
saving = (tau * P / (P + C) if mb >= P * 1e9 * tau else mb / ((P + C) * 1e9)) * L * 1e3
|
|
pre = chain_ms + t_miss + draft_ms - saving
|
|
return dict(U=U, miss_gb=miss / 1e9, t_miss=t_miss, serial=chain_ms + t_miss + draft_ms, overlapped=max(chain_ms, t_miss) + draft_ms, prefetch=pre,
|
|
tok_serial=tok / (chain_ms + t_miss + draft_ms) * 1e3, tok_over=tok / (max(chain_ms, t_miss) + draft_ms) * 1e3, tok_pre=tok / pre * 1e3)
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument('--ceilings')
|
|
ap.add_argument('--tokens', type=int, default=8192)
|
|
a = ap.parse_args()
|
|
c = dict(CEIL_5090)
|
|
if a.ceilings:
|
|
c.update(json.load(open(a.ceilings)))
|
|
print('ceilings', c)
|
|
rows = prefill_table(c, a.tokens)
|
|
print('\nPrefill step table, T=%d' % a.tokens)
|
|
for r in rows:
|
|
print('| %-42s | %7.2f | %6.2f | %6.1f | %6.1f | %4.0f%% | %s |' % (r['step'], r['hbm'] / 1e9, r['flops'] / 1e12, r['floor'], r['now'], r['floor'] / r['now'] * 100, r['note']))
|
|
print('GPU floor %.0f ms, GPU now %.0f ms' % (sum(r['floor'] for r in rows), sum(r['now'] for r in rows)))
|
|
print('\nChunk ideals (gpu floor, pcie, tok/s)')
|
|
for T in (2048, 4096, 8192, 16384):
|
|
rr = prefill_table(c, T)
|
|
rho = {2048: 0.60, 4096: 0.65, 8192: 0.68, 16384: 0.75}[T] # routed share of the bank: 0.60 (overlap agent), 0.68 (job 705); others A
|
|
for name, rf, res in (('stream-all, 0 resident', 1.0, 0), ('routed-only, 0 resident', rho, 0), ('stream-all, 10 GB resident', 1.0, 10), ('routed-only, 10 GB resident', rho, 10)):
|
|
g, p, t = chunk_ideals(c, rr, T, rf, res)
|
|
print(' T=%5d %-32s gpu_floor %6.0f ms pcie %6.0f ms ideal %6.0f ms -> %6.0f tok/s' % (T, name, g, p, max(g, p), t))
|
|
print('\nDecode window')
|
|
for label, rws, h, chain, tok in (('2048 slots', 2.7, 0.41, 12.9, 2.0), ('auto cache', 2.6, 0.68, 11.1, 1.9)):
|
|
for chain_name, ch in (('chain as measured', chain), ('chain floor 5 ms', 5.0)):
|
|
w = window(c, rws, h, ch, 1.2, tok)
|
|
print(' %-10s %-18s U=%.1f miss %.2f GB t_miss %.1f ms serial %.1f ms (%.0f tok/s) overlapped %.1f ms (%.0f tok/s)' % (label, chain_name, w['U'], w['miss_gb'], w['t_miss'], w['serial'], w['tok_serial'], w['overlapped'], w['tok_over']))
|
|
print('\nHit-rate sweep (rows 2.7, tok 2.0, draft 1.2, chain 6 ms floor / 12.9 measured)')
|
|
for h in (0.0, 0.2, 0.41, 0.6, 0.68, 0.8, 0.9):
|
|
wf, wm = window(c, 2.7, h, 6.0, 1.2, 2.0), window(c, 2.7, h, 12.9, 1.2, 2.0)
|
|
print(' h=%.2f miss %.2f GB t_miss %5.1f ms | chain 12.9: serial %5.1f (%3.0f tok/s) prefetch %5.1f (%3.0f) | chain 6: serial %5.1f (%3.0f) prefetch %5.1f (%3.0f) bound(max) %5.1f (%3.0f tok/s)' % (h, wf['miss_gb'], wf['t_miss'], wm['serial'], wm['tok_serial'], wm['prefetch'], wm['tok_pre'], wf['serial'], wf['tok_serial'], wf['prefetch'], wf['tok_pre'], wf['overlapped'], wf['tok_over']))
|
|
P, C, D = c['pcie_contended_gbps'], c['cpu_contended_gbps'], c['dram_gbps']
|
|
print('\nsplit: balance x* = P/(P+C) = %.3f ; DRAM-capped tie interval [%.2f, %.2f]' % (P / (P + C), max(0, 1 - C / D), min(1, P / D)))
|
|
|
|
if __name__ == '__main__':
|
|
main()
|