158 lines
6.8 KiB
Python
158 lines
6.8 KiB
Python
"""The tail of the verify graph and the idle gaps inside it (ledger rows R6, R7, R8).
|
|
|
|
Offline analysis of CUPTI trace directories (tools/cupti-trace, `lct.py`); host-side Python, stdlib only, no GPU.
|
|
|
|
extract TRACE_DIR LABEL [--lct DIR] (python3 -B; reads the trace, writes JSON on stdout)
|
|
per window row count T (the grid of the CPU-row copy kernel / 10) of every verify graph (>= 900 kernels per
|
|
correlation id, first and last two dropped): graphs, union gaps and idle, first doorbell, and the tail after the
|
|
last `add_hits` end split into the hyper-connection part (to the head quantize), the quantize, what sits between it and the
|
|
language-model head matmul (the longest kernel of the tail), the matmul and what follows it (greedy pick, finite scan); the kernel sequence
|
|
of the tail (p50 duration and gap before each launch); the non-verify graphs per round.
|
|
compare LEL.json VANL.json (markdown on stdout)
|
|
per T the differences of the tail sections, and the sum over LEL's own T mix (VANL's T without graphs, here T=5, is
|
|
the mean of its neighbours).
|
|
|
|
A union gap is an interval inside a graph where no kernel of the graph runs (first start to last end). `--lct` defaults
|
|
to /data/leleka-e2e/decode/tools (read only).
|
|
"""
|
|
import argparse
|
|
import collections
|
|
import json
|
|
import sys
|
|
|
|
SECTIONS = ("total", "hc", "quant", "misc", "matmul", "post")
|
|
|
|
|
|
def p50(values):
|
|
v = sorted(values)
|
|
return v[len(v) // 2] if v else float("nan")
|
|
|
|
|
|
def union_gaps(ks):
|
|
cur, gaps = ks[0].end, []
|
|
for k in ks[1:]:
|
|
if k.start > cur:
|
|
gaps.append(k.start - cur)
|
|
cur = max(cur, k.end)
|
|
return gaps
|
|
|
|
|
|
def window_rows(ks, name):
|
|
rows = collections.Counter(k.gx // 10 for k in ks if "copy_rows" in name(k))
|
|
return rows.most_common(1)[0][0] if rows else -1
|
|
|
|
|
|
def tail_of(ks, name):
|
|
"""(anchor end, kernels from the last add_hits end on); with HITS_IN_COMBINE the anchor is the last combine."""
|
|
hits = [k for k in ks if "add_hi" in name(k)]
|
|
if hits:
|
|
a = max(hits, key=lambda k: k.end).end
|
|
else:
|
|
a = max((k for k in ks if "combine" in name(k)), key=lambda k: k.start).start - 1
|
|
return a, [k for k in ks if k.start >= a]
|
|
|
|
|
|
def sections(ks, name):
|
|
a, tail = tail_of(ks, name)
|
|
end = max(k.end for k in ks)
|
|
mm = max(range(len(tail)), key=lambda i: tail[i].end - tail[i].start)
|
|
q = next((i for i in range(mm - 1, max(mm - 4, -1), -1) if "quant" in name(tail[i])), mm - 1)
|
|
dur = lambda k: (k.end - k.start) / 1e3 # noqa: E731
|
|
return {"total": (end - a) / 1e3, "hc": (tail[q].start - a) / 1e3, "quant": dur(tail[q]),
|
|
"misc": (tail[mm].start - tail[q].end) / 1e3, "matmul": dur(tail[mm]), "post": (end - tail[mm].end) / 1e3}
|
|
|
|
|
|
def sequence(graphs, name):
|
|
"""The p50 duration and gap before each launch of the most common tail length."""
|
|
tails = [tail_of(ks, name) for ks in graphs]
|
|
length = collections.Counter(len(t) for _, t in tails).most_common(1)[0][0]
|
|
good = [(a, t) for a, t in tails if len(t) == length]
|
|
out = []
|
|
for i in range(length):
|
|
d = [(t[i].end - t[i].start) / 1e3 for _, t in good]
|
|
g = [(t[i].start - (t[i - 1].end if i else a)) / 1e3 for a, t in good]
|
|
out.append({"name": name(good[0][1][i])[-60:], "dur": p50(d), "gap": p50(g), "grid": [good[0][1][i].gx, good[0][1][i].gy]})
|
|
return out
|
|
|
|
|
|
def per_t(graphs, name):
|
|
d = {"n": len(graphs), "kernels": p50([len(ks) for ks in graphs]), "streams": p50([len({k.stream for k in ks}) for ks in graphs])}
|
|
gaps = [union_gaps(ks) for ks in graphs]
|
|
d["union_gaps"] = p50([len(g) for g in gaps])
|
|
d["idle_us"] = p50([sum(g) / 1e3 for g in gaps])
|
|
doors = [(db[0].start - ks[0].start) / 1e3 for ks in graphs for db in [[k for k in ks if "doorbell" in name(k)]] if db]
|
|
d["first_doorbell_us"] = p50(doors)
|
|
secs = [sections(ks, name) for ks in graphs]
|
|
d.update({s: round(p50([x[s] for x in secs]), 2) for s in SECTIONS})
|
|
d["tail_launches"] = collections.Counter(len(tail_of(ks, name)[1]) for ks in graphs).most_common(1)[0][0]
|
|
d["sequence"] = sequence(graphs, name)
|
|
return d
|
|
|
|
|
|
def extract(path, label, lct_dir):
|
|
sys.path.insert(0, lct_dir)
|
|
sys.dont_write_bytecode = True
|
|
import lct
|
|
t = lct.Trace(path)
|
|
name = lambda k: t.names.get(k.name, "?") # noqa: E731
|
|
by = collections.defaultdict(list)
|
|
for k in t.kernels:
|
|
by[k.corr].append(k)
|
|
big = sorted((c for c, v in by.items() if len(v) >= 900), key=lambda c: min(k.start for k in by[c]))
|
|
kept = [sorted(by[c], key=lambda k: k.start) for c in big[2:-2]]
|
|
groups = collections.defaultdict(list)
|
|
for ks in kept:
|
|
groups[window_rows(ks, name)].append(ks)
|
|
rest = [union_gaps(sorted(v, key=lambda k: k.start)) for c, v in by.items() if 2 <= len(v) < 900]
|
|
return {"label": label, "graphs": len(kept), "T": {str(T): per_t(g, name) for T, g in sorted(groups.items())},
|
|
"non_verify_per_round": {"graphs": len(rest) / len(kept), "union_gaps": sum(map(len, rest)) / len(kept),
|
|
"idle_us": sum(sum(g) for g in rest) / 1e3 / len(kept)}}
|
|
|
|
|
|
def filled(vanl):
|
|
"""VANL per T with the missing Ts filled by the mean of the neighbours that exist."""
|
|
have = sorted(int(t) for t in vanl["T"])
|
|
out = {}
|
|
for T in range(1, 9):
|
|
if str(T) in vanl["T"]:
|
|
out[T] = vanl["T"][str(T)]
|
|
elif have[0] < T < have[-1]:
|
|
lo, hi = max(t for t in have if t < T), min(t for t in have if t > T)
|
|
a, b = vanl["T"][str(lo)], vanl["T"][str(hi)]
|
|
out[T] = {s: (a[s] + b[s]) / 2 for s in SECTIONS + ("idle_us", "union_gaps")}
|
|
return out
|
|
|
|
|
|
def compare(lel, vanl):
|
|
v = filled(vanl)
|
|
mix = {int(T): d["n"] for T, d in lel["T"].items() if int(T) in v}
|
|
total = sum(mix.values())
|
|
print("| section (LEL - VANL, us) | " + " | ".join("T%d" % T for T in mix) + " | weighted at LEL's T mix |")
|
|
print("| --- |" + " ---: |" * (len(mix) + 1))
|
|
for s in SECTIONS + ("idle_us", "union_gaps"):
|
|
diffs = {T: lel["T"][str(T)][s] - v[T][s] for T in mix}
|
|
w = sum(mix[T] * diffs[T] for T in mix) / total
|
|
print("| %s | %s | %.1f |" % (s, " | ".join("%+.1f" % diffs[T] for T in mix), w))
|
|
print("graphs per T (LEL): %s" % mix)
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser()
|
|
sub = ap.add_subparsers(dest="cmd", required=True)
|
|
e = sub.add_parser("extract")
|
|
e.add_argument("trace")
|
|
e.add_argument("label")
|
|
e.add_argument("--lct", default="/data/leleka-e2e/decode/tools")
|
|
c = sub.add_parser("compare")
|
|
c.add_argument("lel")
|
|
c.add_argument("vanl")
|
|
a = ap.parse_args()
|
|
if a.cmd == "extract":
|
|
json.dump(extract(a.trace, a.label, a.lct), sys.stdout, indent=1)
|
|
else:
|
|
compare(json.load(open(a.lel)), json.load(open(a.vanl)))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|