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
CUDApp / evidence / amd-speed-20261114 / raw / unpacked / amd-kernel-speed / card / GpuBenchSync-20261113T232737Z / aks-results / out / amplify-plan.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """Measuring instrument (Python, not part of Omega): per-dispatch-class timing with the unmodified native launcher. |
| 4 | |
| 5 | Reads one OMPLAN01 plan (CUDApp backend/amd/kfd/launch/PLAN.md) and writes a variant in which extra copies of one |
| 6 | dispatch class are inserted immediately before the last dispatch of the step (step_advance). The launcher, the code |
| 7 | objects, the weight files, every original dispatch and their order are unchanged, so: |
| 8 | |
| 9 | per-dispatch time of the class = (median decode step of the variant - median decode step of the base) / inserted |
| 10 | |
| 11 | and the variant must still print the published token ids (every inserted dispatch only rewrites a buffer that the |
| 12 | next step recomputes before reading it, or rewrites the same value). A variant whose ids differ is discarded. |
| 13 | |
| 14 | Usage: |
| 15 | amplify-plan.py list BASE.plan -> class table (JSON) of the plan's step |
| 16 | amplify-plan.py write BASE.plan OUT.plan CLASS N_GEN -> variant; CLASS 'base' only changes N_GEN |
| 17 | Classes are named KERNEL or KERNEL:MxK (matrix-vector shapes), plus the controls |
| 18 | empty:G G workgroups of fp8gemv_b128f with K=0 (each wave leaves before reading any weight) |
| 19 | The copies cycle through every dispatch of the class in step order (weights of all layers, so the cache cannot hold |
| 20 | them) for matrix-vector and attention part/merge classes; other classes repeat their last dispatch of the step. |
| 21 | """ |
| 22 | import json, struct, sys, hashlib |
| 23 | |
| 24 | GEMV = {'fp8gemv_b128f': (32, 36), 'bf16gemv2': (24, 28)} # kernarg byte offsets of m and k |
| 25 | CYCLE = {'attn256_part_step', 'attn256_merge_step'} # cycle all layers (per-layer key/value caches) |
| 26 | # Number of inserted dispatches per class: enough added time to stand clear of run-to-run spread (about 0.05 ms). |
| 27 | SWEEPS = {'fp8gemv_b128f:17408x5120': 1, 'fp8gemv_b128f:5120x17408': 2, 'fp8gemv_b128f:10240x5120': 2, |
| 28 | 'fp8gemv_b128f:6144x5120': 3, 'fp8gemv_b128f:5120x6144': 2, 'fp8gemv_b128f:12288x5120': 6, |
| 29 | 'fp8gemv_b128f:1024x5120': 4, 'bf16gemv2:48x5120': 2, 'bf16gemv2:248320x5120': 6, |
| 30 | 'attn256_part_step': 8, 'attn256_merge_step': 8} |
| 31 | REPEATS = {'rmsnorm5120': 258, 'residual5120': 256, 'mlpsilu': 256, 'gdnparams': 240, 'gdnconv4_step': 240, |
| 32 | 'gdnqknorm': 240, 'gdnstate_step': 240, 'gdnrmsgate': 240, 'attn256_prep_step': 128, 'attn256_gate': 128, |
| 33 | 'argmax_part': 64, 'argmax_final': 128, 'embed5120_step': 128} |
| 34 | EMPTY = {'empty:1': 256, 'empty:17408': 128} |
| 35 | |
| 36 | |
| 37 | def parse(p): |
| 38 | if p[:8] != b'OMPLAN01': raise SystemExit('not an OMPLAN01 plan') |
| 39 | nc, nf, nb, nd, npm, ng, tb = struct.unpack_from('<7Q', p, 8) |
| 40 | sp, sl, dp, dl = struct.unpack_from('<4Q', p, len(p) - 32) |
| 41 | q = 64 |
| 42 | names = [] |
| 43 | for i in range(nc): |
| 44 | o, l = struct.unpack_from('<2Q', p, q + 16 * i); names.append(p[sp + o:sp + o + l].decode()) |
| 45 | q += 16 * nc + 48 * nb + 4 * npm; q += (-q) % 8 |
| 46 | start = q; recs = [] |
| 47 | for i in range(nd): |
| 48 | ci, gx, gy, gz, bx, kb, nr, pad = struct.unpack_from('<8I', p, q) |
| 49 | if not (ci < nc and gz == 1 and bx == 64 and pad == 0): raise SystemExit('dispatch %d header' % i) |
| 50 | end = q + 32 + kb + 16 * nr; end += (-end) % 8 |
| 51 | recs.append(dict(name=names[ci], gx=gx, gy=gy, kb=kb, nr=nr, raw=bytes(p[q:end]))); q = end |
| 52 | if q != sp or sp + sl != dp or dp + dl != len(p) - 32: raise SystemExit('plan layout') |
| 53 | return dict(head=bytes(p[:start]), recs=recs, tail=bytes(p[sp:len(p) - 32]), sl=sl, dl=dl, n_gen=ng, n_prompt=npm) |
| 54 | |
| 55 | |
| 56 | def label(r): |
| 57 | if r['name'] in GEMV: |
| 58 | mo, ko = GEMV[r['name']] |
| 59 | m, k = struct.unpack_from('<I', r['raw'], 32 + mo)[0], struct.unpack_from('<I', r['raw'], 32 + ko)[0] |
| 60 | return '%s:%dx%d' % (r['name'], m, k) |
| 61 | return r['name'] |
| 62 | |
| 63 | |
| 64 | def classes(plan): |
| 65 | out = {} |
| 66 | for r in plan['recs']: |
| 67 | c = out.setdefault(label(r), dict(count=0, gx=r['gx'], gy=r['gy'])); c['count'] += 1 |
| 68 | return out |
| 69 | |
| 70 | |
| 71 | def inserted(plan, cls): |
| 72 | recs = plan['recs'] |
| 73 | if recs[-1]['name'] != 'step_advance': raise SystemExit('last dispatch is not step_advance') |
| 74 | if cls == 'base': return [] |
| 75 | if cls in EMPTY: |
| 76 | g = int(cls.split(':')[1]) |
| 77 | src = next(r for r in recs if label(r) == 'fp8gemv_b128f:17408x5120') |
| 78 | raw = bytearray(src['raw']); struct.pack_into('<I', raw, 4, g) # grid x = workgroups |
| 79 | struct.pack_into('<I', raw, 32 + GEMV['fp8gemv_b128f'][1], 0) # K = 0 |
| 80 | return [bytes(raw)] * EMPTY[cls] |
| 81 | mine = [r['raw'] for r in recs if label(r) == cls] |
| 82 | if not mine: raise SystemExit('class not in plan: ' + cls) |
| 83 | if cls in SWEEPS: return mine * SWEEPS[cls] |
| 84 | if cls in REPEATS: return [mine[-1]] * REPEATS[cls] |
| 85 | raise SystemExit('no amplification rule for ' + cls) |
| 86 | |
| 87 | |
| 88 | def write(plan, cls, n_gen): |
| 89 | extra = inserted(plan, cls) |
| 90 | recs = [r['raw'] for r in plan['recs']] |
| 91 | body = b''.join(recs[:-1]) + b''.join(extra) + recs[-1] |
| 92 | head = bytearray(plan['head']) |
| 93 | struct.pack_into('<Q', head, 8 + 8 * 3, len(recs) + len(extra)) |
| 94 | if n_gen > plan['n_gen']: raise SystemExit('N_GEN above the plan value') |
| 95 | struct.pack_into('<Q', head, 8 + 8 * 5, n_gen) |
| 96 | sp = len(head) + len(body) |
| 97 | out = bytes(head) + body + plan['tail'] + struct.pack('<4Q', sp, plan['sl'], sp + plan['sl'], plan['dl']) |
| 98 | return out, len(extra) |
| 99 | |
| 100 | |
| 101 | def main(): |
| 102 | mode = sys.argv[1] |
| 103 | base = open(sys.argv[2], 'rb').read(); plan = parse(base) |
| 104 | if mode == 'list': |
| 105 | print(json.dumps(dict(dispatches=len(plan['recs']), n_prompt=plan['n_prompt'], n_gen=plan['n_gen'], |
| 106 | classes=classes(plan), rules=sorted(list(SWEEPS) + list(REPEATS) + list(EMPTY))), indent=1)) |
| 107 | missing = [c for c in classes(plan) if c not in SWEEPS and c not in REPEATS and c != 'step_advance'] |
| 108 | if missing: raise SystemExit('classes without a rule: %s' % missing) |
| 109 | return |
| 110 | out, cls, n_gen = sys.argv[3], sys.argv[4], int(sys.argv[5]) |
| 111 | data, n = write(plan, cls, n_gen) |
| 112 | check = parse(data) # the variant must parse with the same reader |
| 113 | assert len(check['recs']) == len(plan['recs']) + n and check['n_gen'] == n_gen |
| 114 | assert [r['raw'] for r in check['recs'][:len(plan['recs']) - 1]] == [r['raw'] for r in plan['recs'][:-1]] |
| 115 | assert check['recs'][-1]['raw'] == plan['recs'][-1]['raw'] and check['tail'] == plan['tail'] |
| 116 | open(out, 'wb').write(data) |
| 117 | print('VARIANT', json.dumps(dict(cls=cls, inserted=n, dispatches=len(check['recs']), n_gen=n_gen, bytes=len(data), |
| 118 | sha256=hashlib.sha256(data).hexdigest(), base_sha256=hashlib.sha256(base).hexdigest()))) |
| 119 | |
| 120 | |
| 121 | if __name__ == '__main__': main() |