Files
leleka/scripts/e2e/decode/draftround-kernels.py

90 lines
4.3 KiB
Python

#!/usr/bin/env python3
"""Per-kernel table of the small graphs of a decode round (commit, MTP round graph, MTP chain steps) from a CUPTI window (tools/cupti-trace, lct.py).
python3 -I draftround-kernels.py <win.lct> [--lct-dir /data/leleka-e2e/decode/tools] [--seq CLASS] [--top N]
A small graph launch is classified by its kernel-name signature: `commit` (has verify_gdn_conv_commit_kernel), `round` (the first MTP graph of a draft: its kernel count is
160..173 by rows), `step` (113 kernels). For every class: launches, GPU span (first kernel start to last kernel end), kernel sum, in-graph idle (span - kernel union), then one line
per kernel name: calls per graph, mean duration, total per graph, share of the span. --seq CLASS prints the ordered kernel sequence of the median graph with start offsets and gaps.
"""
import argparse
import collections
import os
import statistics
import sys
def main():
ap = argparse.ArgumentParser()
ap.add_argument("dir")
ap.add_argument("--lct-dir", default="/data/leleka-e2e/decode/tools")
ap.add_argument("--seq", default=None)
ap.add_argument("--top", type=int, default=40)
a = ap.parse_args()
sys.path.insert(0, a.lct_dir)
import lct as L
t = L.Trace(a.dir)
gl = {x.corr: x for x in t.apis if "GraphLaunch" in t.names.get(x.name, "")}
byc = collections.defaultdict(list)
for k in t.kernels:
byc[k.corr].append(k)
classes = collections.defaultdict(list)
for corr, x in gl.items():
ks = sorted(byc.get(corr, []), key=lambda k: k.start)
if not ks or len(ks) >= 1000:
continue
names = [t.names.get(k.name, "?") for k in ks]
if any("conv_commit" in n or "draft_commit" in n for n in names):
cl = "commit"
elif len(ks) == 113:
cl = "step"
elif 140 <= len(ks) <= 180 and len(ks) != 182:
cl = "round"
else:
cl = "other%d" % len(ks)
classes[cl].append((ks, names))
for cl, gs in sorted(classes.items()):
spans = [(ks[-1].end - ks[0].start) / 1e3 for ks, _ in gs]
ksum = [sum(k.end - k.start for k in ks) / 1e3 for ks, _ in gs]
# union of kernel intervals (concurrent branches in a graph overlap)
un = []
for ks, _ in gs:
tot, cur_s, cur_e = 0, None, None
for k in ks:
if cur_e is None or k.start > cur_e:
if cur_e is not None:
tot += cur_e - cur_s
cur_s, cur_e = k.start, k.end
else:
cur_e = max(cur_e, k.end)
tot += cur_e - cur_s
un.append(tot / 1e3)
print("== %s: %d launches, kernels/graph %s, span mean %.1f us (p10 %.1f p90 %.1f), kernel sum %.1f us, union busy %.1f us, in-graph idle %.1f us"
% (cl, len(gs), sorted(set(len(g[0]) for g in gs)), statistics.mean(spans), sorted(spans)[len(spans) // 10], sorted(spans)[len(spans) * 9 // 10],
statistics.mean(ksum), statistics.mean(un), statistics.mean(spans) - statistics.mean(un)))
agg = collections.defaultdict(lambda: [0, 0.0, 0.0])
for ks, names in gs:
for k, n in zip(ks, names):
r = agg[n]
r[0] += 1
r[1] += (k.end - k.start) / 1e3
r[2] += k.regs if False else 0
rows = sorted(agg.items(), key=lambda kv: -kv[1][1])
sp = statistics.mean(spans)
print(" %-64s %8s %9s %10s %7s" % ("kernel", "calls/g", "mean us", "total us/g", "%span"))
for n, (c, tot, _) in rows[: a.top]:
print(" %-64s %8.2f %9.2f %10.2f %6.1f%%" % (n[:64], c / len(gs), tot / c, tot / len(gs), 100.0 * tot / len(gs) / sp))
if a.seq == cl:
ks, names = sorted(gs, key=lambda g: g[0][-1].end - g[0][0].start)[len(gs) // 2]
t0 = ks[0].start
prev_end = t0
print(" -- median graph, ordered kernels: idx start_us dur_us gap_us grid block name")
for i, (k, n) in enumerate(zip(ks, names)):
print(" %3d %8.1f %7.2f %6.2f %s x %s %s" % (i, (k.start - t0) / 1e3, (k.end - k.start) / 1e3, (k.start - prev_end) / 1e3,
(k.gx, k.gy, k.gz), (k.bx, k.by, k.bz), n[:70]))
prev_end = max(prev_end, k.end)
if __name__ == "__main__":
main()