interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / evidence / amd-speed-20261114 / recompute / kernels.py

132 lines · 9198 bytes Raw Blame History
1#!/usr/bin/env python3
2# Kernels: AmdKernelSpeed card session e787e65d (one run per candidate) plus the bytes-per-step model.
3# Per-class times follow the lane's method (measure/analyze.py): delta against the median of three 64-token base runs.
4import csv, json, re, statistics
5from common import ROOT, Book, omega_run, first_difference, sha256
6
7b = Book('kernels')
8S = 'amd-kernel-speed/card/261114001549-e787e65d'
9R = f'{S}/out/runs'
10PEAK = 5.3e12 # B/s, AMD's MI300X figure (quoted, not measured)
11pub = json.loads((ROOT / 'amd-launch-gap/card/bundle/published-ids.json').read_text())['ids']
12b.check('aks-results tarball sha256 equals card value',
13 sha256(ROOT / f'{S}/aks-results.tar.gz') == (ROOT / f'{S}/aks-results.sha256').read_text().split()[0])
14
15rows = list(csv.DictReader(open(ROOT / 'amd-kernel-speed/model/classes.tsv'), delimiter='\t'))
16total = sum(int(r['weight_bytes_per_step']) for r in rows)
17b.add('model.bytes_per_step', total, 'B', 'MODELED', 'amd-kernel-speed/model/classes.tsv',
18 'sum of weight_bytes_per_step (weights and block scales, BF16 lm_head in full; embedding one row)')
19floor = b.add('model.floor_ms', round(total / PEAK * 1e3, 4), 'ms/token', 'MODELED', 'amd-kernel-speed/model/classes.tsv',
20 'bytes_per_step / 5.3 TB/s')
21b.add('model.dispatches_per_step', sum(int(r['dispatches_per_step']) for r in rows), 'dispatches', 'COUNT',
22 'amd-kernel-speed/model/classes.tsv', 'sum of dispatches_per_step')
23
24ladder = {}
25for name in ['full-a', 'full-b', 'cap286', 'v2-rmsnorm5120', 'v2-gdnstate_step', 'v1all']:
26 x = omega_run(ROOT / f'{R}/{name}.txt')
27 b.check(f'{name} prints the 256 published ids', x['ids'] == pub, f'first difference {first_difference(x["ids"], pub)}')
28 ms = x['decode_ms_median']
29 ladder[name] = ms
30 b.add(f'ladder.{name}_ms', round(ms, 3), 'ms/token', 'MEASURED', f'{R}/{name}.txt', 'median of 255 decode steps, one run')
31 b.add(f'ladder.{name}_TBps', round(total / (ms / 1e3) / 1e12, 3), 'TB/s', 'DERIVED', [f'{R}/{name}.txt', 'amd-kernel-speed/model/classes.tsv'],
32 'modelled bytes per step / measured time')
33 b.add(f'ladder.{name}_pct_peak', round(100 * total / (ms / 1e3) / PEAK, 1), '%', 'DERIVED', [f'{R}/{name}.txt', 'amd-kernel-speed/model/classes.tsv'],
34 'percent of 5.3 TB/s')
35 b.add(f'ladder.{name}_prefill_ms', round(x['prefill_ms'], 1), 'ms', 'MEASURED', f'{R}/{name}.txt', 'sum of 30 prompt steps')
36b.add('ladder.v1all_speedup_vs_full_a', round(ladder['full-a'] / ladder['v1all'], 3), 'x', 'DERIVED',
37 [f'{R}/full-a.txt', f'{R}/v1all.txt'], 'full-a / v1all, same session')
38b.add('ladder.baseline_spread_ms', round(ladder['full-b'] - ladder['full-a'], 3), 'ms/token', 'DERIVED',
39 [f'{R}/full-a.txt', f'{R}/full-b.txt'], 'full-b minus full-a: two runs of the same unchanged plan, same session')
40b.add('ladder.v1all_speedup_vs_full_b', round(ladder['full-b'] / ladder['v1all'], 3), 'x', 'DERIVED',
41 [f'{R}/full-b.txt', f'{R}/v1all.txt'], 'full-b / v1all, same session')
42b.add('ladder.v1all_over_floor', round(ladder['v1all'] / floor, 2), 'x', 'DERIVED', [f'{R}/v1all.txt'], 'v1all / modelled floor')
43ctl = omega_run(ROOT / f'{R}/control.txt')
44b.add('ladder.control_first_difference', first_difference(ctl['ids'], pub[:64]), 'index', 'MEASURED', f'{R}/control.txt',
45 'altered-weights 64-token plan against the first 64 published ids')
46
47# per-class time per dispatch (lane method), checked against the lane's analysis.json
48P = 30
49def steps(name):
50 x = (ROOT / f'{R}/{name}.txt').read_text()
51 import re
52 return {int(m.group(1)): int(m.group(2)) for m in re.finditer(r'^OMEGA_STEP (\d+) tok=-?\d+ us=(\d+)$', x, re.M)}
53base = [steps(f'base-{i}') for i in (1, 2, 3)]
54for i in (1, 2, 3):
55 b.check(f'base-{i} ids', omega_run(ROOT / f'{R}/base-{i}.txt')['ids'] == pub[:64])
56dec = range(P, P + 63)
57ref = {s: statistics.median(bs[s] for bs in base) for s in dec}
58b.add('classes.base_decode_ms', round(statistics.median(ref.values()) / 1e3, 3), 'ms/token', 'MEASURED',
59 [f'{R}/base-{i}.txt' for i in (1, 2, 3)], 'median over decode steps of the per-step median of three 64-token base runs')
60lane = json.loads((ROOT / f'{S}/analysis.json').read_text())['classes']
61
62
63def per_step(cls):
64 k, _, shape = cls.partition(':')
65 if k == 'empty':
66 return None
67 n = 0
68 for r in rows:
69 if r['kernel'] == k and (not shape or shape in r['class']):
70 n += int(r['dispatches_per_step'])
71 return n
72
73
74table = []
75for v in sorted((ROOT / R).glob('*.variant')):
76 if v.stem.startswith(('base', 'control')):
77 continue
78 meta = json.loads(v.read_text().split('VARIANT ', 1)[1])
79 st = steps(v.stem)
80 b.check(f'{v.stem} ids', omega_run(ROOT / f'{R}/{v.stem}.txt')['ids'] == pub[:64])
81 med = statistics.median(st[s] - ref[s] for s in dec)
82 usd = med / meta['inserted']
83 b.check(f'{meta["cls"]} equals lane analysis.json', abs(usd - lane[meta['cls']]['us_per_dispatch']) < 1e-9)
84 n = per_step(meta['cls'])
85 table.append((meta['cls'], n, usd, None if n is None else usd * n / 1e3))
86table.sort(key=lambda t: -(t[3] or 0))
87for cls, n, usd, ms in table:
88 key = cls.replace(':', '_')
89 b.add(f'classes.{key}.us_per_dispatch', round(usd, 2), 'us', 'MEASURED', f'{R}/{cls.replace(":", "_")}.txt',
90 'median step delta / inserted copies, against the base runs')
91 if ms is not None:
92 b.add(f'classes.{key}.ms_per_step', round(ms, 3), 'ms/token', 'DERIVED', [f'{R}/{cls.replace(":", "_")}.txt', 'amd-kernel-speed/model/classes.tsv'],
93 f'us per dispatch x {n} dispatches per step')
94 k, _, shape = cls.partition(':')
95 wb = sum(int(r['weight_bytes_per_dispatch']) for r in rows if r['kernel'] == k and shape and shape in r['class'][:]) \
96 / max(1, sum(1 for r in rows if r['kernel'] == k and shape and shape in r['class']))
97 if wb:
98 b.add(f'classes.{key}.pct_peak', round(100 * wb / (usd / 1e6) / PEAK, 1), '%', 'DERIVED',
99 [f'{R}/{cls.replace(":", "_")}.txt', 'amd-kernel-speed/model/classes.tsv'],
100 'weight bytes per dispatch / time per dispatch, as percent of 5.3 TB/s (in place on the card, inserted copies)')
101# addnorm5120 v2 (kernel step 2): CPU gate only, not run on the card
102GA = 'amd-kernel-speed/gates/gate-addnorm5120.log'
103ma = re.search(r'^ADDNORM5120-V2-GATE PASS cases=(\d+) mutants=(\d+) fail=0$', (ROOT / GA).read_text(), re.M)
104b.check('addnorm5120 v2 gate PASS line present', bool(ma))
105b.add('addnorm_gate.cases', int(ma.group(1)), 'cases', 'COUNT', GA, 'ADDNORM5120-V2-GATE PASS line')
106b.add('addnorm_gate.mutants', int(ma.group(2)), 'mutants', 'COUNT', GA, 'same line')
107# fp8gemv_b128f v3r2 and v3r4: CPU gates only, the lane's record copied into the folder
108FR = 'amd-kernel-speed/kernels/FP8-R2-RESULT.md'
109fr = (ROOT / FR).read_text()
110for r in ('R2', 'R4'):
111 mf = re.search(rf'^FP8GEMV-B128F-{r}-GATE PASS cases=(\d+) mutants=(\d+) controls=\d+ fail=0$', fr, re.M)
112 b.check(f'fp8gemv_b128f v3{r.lower()} gate PASS line present, two builds byte-identical',
113 bool(mf) and f'PASS v3{r.lower()} two builds byte-identical' in fr)
114 b.add(f'fp8_v3{r.lower()}_gate.cases', int(mf.group(1)), 'cases', 'COUNT', FR, f'FP8GEMV-B128F-{r}-GATE PASS line')
115 b.add(f'fp8_v3{r.lower()}_gate.mutants', int(mf.group(2)), 'mutants', 'COUNT', FR, 'same line')
116# Kernel step 2 card bundle, simulated on the Spark (no card): plan fusion lines and per-run exit codes of the bundle's runs.
117SN = 'amd-report/extracted/spark-logs-20261114/kernel-step2-sim-next.log'
118sn = (ROOT / SN).read_text()
119fu = {tuple(int(x) for x in m) for m in re.findall(r"^FUSED \{.*'dispatches_before': (\d+), 'dispatches_after': (\d+),", sn, re.M)}
120fab = {tuple(int(x) for x in m) for m in re.findall(r"^FUSED_AB \{.*'dispatches_before': (\d+), 'dispatches_after': (\d+),", sn, re.M)}
121b.check('step-2 sim log: one FUSED and one FUSED_AB dispatch pair, chained', len(fu) == 1 and len(fab) == 1 and next(iter(fu))[1] == next(iter(fab))[0])
122b.add('step2.dispatches_before', next(iter(fu))[0], 'dispatches', 'COUNT', SN, 'FUSED line: dispatches per step before the addnorm fusion')
123b.add('step2.dispatches_after_addnorm', next(iter(fu))[1], 'dispatches', 'COUNT', SN, 'FUSED line: after it')
124b.add('step2.ab_merge_fewer', next(iter(fab))[0] - next(iter(fab))[1], 'dispatches', 'DERIVED', SN, 'FUSED_AB line: before minus after')
125runs = re.findall(r'^RUN (\S+) rc=(\d+) end ', sn, re.M)
126b.check('step-2 sim log: every run name appears once and the script finished', len({r for r, _ in runs}) == len(runs) and '\nNEXT_DONE\n' in sn)
127b.add('step2.sim_runs', len(runs), 'runs', 'COUNT', SN, 'RUN ... rc= lines')
128b.add('step2.sim_runs_exit0', sum(rc == '0' for _, rc in runs), 'runs', 'COUNT', SN, 'of those, rc=0')
129b.add('classes.sum_ms', round(sum(t[3] for t in table if t[3]), 3), 'ms/token', 'DERIVED', R, 'sum over classes of ms per step')
130b.add('classes.unattributed_ms', round(statistics.median(ref.values()) / 1e3 - sum(t[3] for t in table if t[3]), 3), 'ms/token', 'DERIVED', R,
131 '64-token base step minus the sum over classes: the part this method does not attribute')
132b.close()