#!/usr/bin/env python3 """Per-kernel table of one decode verify window (port vs Original) from two CUPTI traces (tools/cupti-trace, scripts/profile/lct.py of claude/ledger). Windows are the big graphs (>= 1000 kernels); `--rows R` keeps the windows of R rows (grid.y of the embedding gather). python3 tools/chain-probe/window_table.py --lct PATH_TO_scripts_profile PORT.lct ORIG.lct [--rows 4] [--md] Prints, per canonical kernel role: kernels per window, GPU microseconds per window and the share of the chain (everything that is not a host or PCIe wait) for both engines, plus the idle gap between kernels inside the graph. Kernel names are mapped to the Original's names so the rows line up (port: window_moe_gu_*, head_n*, mtp_scalar_gr_*, verify_*, ...). """ import argparse import collections import re import sys NAME_MAP = [ (r"^mtp_scalar_gr_split$|^gr_norm_split$|^chain_gr_split$", "GR norm (gr_norm_split)"), (r"^mtp_scalar_gr_staged$|^gr_down_staged$|^chain_gr_down", "GR down (gr_down_staged)"), (r"^mtp_scalar_gr_up$|^gr_up_multi$|^chain_gr_up", "GR up (gr_up_multi)"), (r"^window_moe_gu|^native_gu_multi$", "routed experts gate/up"), (r"^window_moe_down|^native_down_multi$", "routed experts down"), (r"^swiglu_entries$|^chain_swiglu_quant", "experts swiglu (+quantize when fused)"), (r"^verify_copy_rows_from_mapped$|^copy_rows_from_mapped$|^chain_copy_rows", "CPU-leg rows copy (copy_rows_from_mapped)"), (r"^verify_copy_i32_from_mapped$|^copy_i32_from_mapped$|^chain_copy_i32|^chain_wait_copy", "plan copy (copy_i32_from_mapped)"), (r"^verify_doorbell_publish$|^doorbell_publish$|^chain_doorbell", "doorbell publish"), (r"^verify_gdn_ab_multi$|^gdn_ab_multi$|^chain_gdn_ab", "GDN alpha/beta"), (r"^verify_gdn_step_norm_multi$|^gdn_step_norm_multi$", "GDN recurrence + out norm"), (r"^verify_gdn_conv_l2_multi$|^gdn_conv_l2_multi$", "GDN conv + L2"), (r"^leleka_qsa_decode_chunk$|^attn_chunk$|^chain_qsa_decode_chunk$", "QSA decode attention chunk"), (r"^leleka_qsa_decode_merge$|^attn_merge$", "QSA decode attention merge"), (r"^sampler_greedy|^chain_sampler", "greedy argmax"), (r"^mtp_full_route$|^route$", "router top-10"), (r"^mtp_full_combine$|^combine$", "MoE combine"), (r"^add_hits$", "add_hits"), (r"^head_quant$|^native_quantize_q8_1$", "activation quantize (q8_1, projections)"), (r"^quantize_q8_1$", "quantize_q8_1 (experts / shared)"), (r"^mtp_full_mmvf|^bf16_f32_mmvf", "bf16 mmvf (router logits, shared gate, head mix)"), (r"^to_bf16$|^mtp_full_to_bf16$", "to_bf16 (dead with native shared gate)"), (r"^native_swiglu$|^mtp_full_native_swiglu$", "shared swiglu"), (r"^native_scalar_sigmoid|^mtp_full_native_scalar_sigmoid|^scale_rows$|^mtp_full_scale$|^chain_scale_rows", "shared sigmoid / scale_rows"), (r"^wait_flag_ge$|^verify_wait_flag_ge$", "WAIT host/PCIe flags"), (r"^fetch_blobs$", "WAIT PCIe copy kernel (Original)"), (r"^rebase_ptrs$", "rebase_ptrs (Original)"), ] PROJ = re.compile(r"^head_n\d+_|^native_.*mmvq|^native_mmvq_multi|^native_small_mmvq") def role(fam, grid): for rx, name in NAME_MAP: if re.search(rx, fam): return name if PROJ.search(fam): if grid >= 200000: return "lm head projection (248,320 rows)" if grid >= 2000: return "dense projections (GDN qkv/z/out, QSA q/out)" return "small projections (QSA k/v, shared expert)" return "other: " + fam def windows(trace, want_rows, L): byc = collections.defaultdict(list) for k in trace.kernels: byc[k.corr].append(k) out = [] for c, ks in byc.items(): if len(ks) < 1000: continue ks.sort(key=lambda k: k.start) emb = [k for k in ks[:20] if "embed_rows" in trace.kname(k)] rows = emb[0].gy if emb else -1 if rows == want_rows: out.append(ks) return out def table(trace, want_rows, L): ws = windows(trace, want_rows, L) agg = collections.defaultdict(lambda: [0, 0]) span = ksum = 0 for ks in ws: span += ks[-1].end - ks[0].start for k in ks: r = role(L.family(trace.kname(k)), k.gx * k.gy * k.gz) agg[r][0] += 1 agg[r][1] += k.end - k.start ksum += k.end - k.start n = max(1, len(ws)) return n, {r: (v[0] / n, v[1] / n / 1e3) for r, v in agg.items()}, span / n / 1e3, ksum / n / 1e3 def main(): ap = argparse.ArgumentParser() ap.add_argument("--lct", required=True, help="scripts/profile directory of claude/ledger (lct.py)") ap.add_argument("port") ap.add_argument("orig") ap.add_argument("--rows", type=int, default=4) ap.add_argument("--md", action="store_true") a = ap.parse_args() sys.path.insert(0, a.lct) import lct as L tp = L.Trace(a.port) to = L.Trace(a.orig) np_, P, spanp, ksp = table(tp, a.rows, L) no, O, spano, kso = table(to, a.rows, L) waits = [r for r in set(P) | set(O) if r.startswith("WAIT")] chain_p = sum(v[1] for r, v in P.items() if r not in waits) chain_o = sum(v[1] for r, v in O.items() if r not in waits) print("rows=%d: %d port windows, %d Original windows" % (a.rows, np_, no)) print("window GPU span: port %.0f us, Original %.0f us; kernel sum %.0f / %.0f us" % (spanp, spano, ksp, kso)) print("chain (kernels that are not host/PCIe waits): port %.0f us, Original %.0f us" % (chain_p, chain_o)) print("idle gaps inside the graph (span - kernel sum): port %.0f us, Original %.0f us" % (spanp - ksp, spano - kso)) sep = "|" if a.md else " " rows = sorted(set(P) | set(O), key=lambda r: -(P.get(r, (0, 0))[1] + O.get(r, (0, 0))[1])) if a.md: print("\n| role | port n | port us | port % | Original n | Original us | Original % | port - Original us |") print("| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: |") for r in rows: pn, pu = P.get(r, (0, 0)) on, ou = O.get(r, (0, 0)) ps = 100 * pu / chain_p if r not in waits else 0 os_ = 100 * ou / chain_o if r not in waits else 0 if a.md: print("| %s | %.1f | %.0f | %.1f | %.1f | %.0f | %.1f | %+.0f |" % (r, pn, pu, ps, on, ou, os_, pu - ou)) else: print("%-52s port %6.1f x %8.0f us %5.1f%% | orig %6.1f x %8.0f us %5.1f%% | %+7.0f" % (r, pn, pu, ps, on, ou, os_, pu - ou)) if __name__ == "__main__": main()