Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
99 lines
4.9 KiB
Python
99 lines
4.9 KiB
Python
#!/usr/bin/env python3
|
|
"""GPU idle-gap anatomy of one chunk window of a CUPTI trace (tools/cupti-trace), by cause and by call site.
|
|
|
|
python3 gap_anatomy.py TRACE.lct --t0 MS --t1 MS [--min-us 20] [--top 14] (MS from the window start, e.g. from lct_report --chunk)
|
|
|
|
A gap is a stretch where NO kernel of any stream runs (union of all streams) of at least --min-us. For every gap the cause is decided in this order:
|
|
copy-wait an H2D copy of the expert stream ends within 4 us before the gap's end (the compute stream waited for its `copied` event);
|
|
host-sync a blocking call (cuStreamSynchronize, cuMemcpyDtoH_v2, cuMemcpyHtoD_v2, cuMemAlloc/Free, cuMemsetD8_v2, cuEventSynchronize) overlaps the gap
|
|
for at least 30 % of it (the host was blocked: the GPU idles from the sync's return until the next launch is issued and started);
|
|
launch-bound launch API calls cover at least 30 % of the gap (the next kernel was being launched: chains of small kernels);
|
|
host-gap the rest (CPU work between calls, no API record).
|
|
Output: totals per cause, then the top sites (previous kernel family -> next kernel family, cause) by total idle ms, and the top sites by blocking call.
|
|
(docs/upstream-port-plan.md, 256k prefill gap.)
|
|
"""
|
|
import argparse
|
|
import bisect
|
|
import os
|
|
import sys
|
|
from collections import defaultdict
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + "/../profile")
|
|
import lct as L
|
|
|
|
TIGHT_NS = 4_000 # a copy-wait gap ends within 4 us after an expert copy completed (the event wakes the compute stream at once)
|
|
BLOCKING = ("Synchronize", "MemcpyDtoH_v2", "MemcpyHtoD_v2", "cuMemAlloc", "cuMemFree", "cuMemsetD8_v2", "cudaMemcpy_")
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("trace")
|
|
ap.add_argument("--t0", type=float, default=0.0)
|
|
ap.add_argument("--t1", type=float, default=1e9)
|
|
ap.add_argument("--min-us", type=float, default=20.0)
|
|
ap.add_argument("--top", type=int, default=14)
|
|
a = ap.parse_args()
|
|
tr = L.Trace(a.trace)
|
|
lo, hi = tr.t0 + int(a.t0 * 1e6), min(tr.t1, tr.t0 + int(a.t1 * 1e6))
|
|
tr.clip(lo, hi)
|
|
ks = sorted((k for k in tr.kernels if k.end > lo and k.start < hi), key=lambda k: k.start)
|
|
apis = sorted((x for x in tr.apis if x.end > lo - 50_000_000 and x.start < hi), key=lambda x: x.start)
|
|
starts = [x.start for x in apis]
|
|
h2d_ends = sorted(c.end for c in tr.copies if c.ckind == 1)
|
|
gaps = []
|
|
cur_end, prev = ks[0].end, ks[0]
|
|
for k in ks[1:]:
|
|
if k.start - cur_end >= a.min_us * 1e3:
|
|
gaps.append((cur_end, k.start, prev, k))
|
|
if k.end > cur_end:
|
|
cur_end, prev = k.end, k
|
|
span = ks[-1].end - ks[0].start
|
|
busy = L.total(L.union([(k.start, k.end) for k in ks]))
|
|
print(f"window {a.t0:.1f}-{min(a.t1, a.t1):.1f} ms: span {span / 1e6:.1f} ms, GPU busy {busy / 1e6:.1f} ms ({100 * busy / span:.1f} %), idle {(span - busy) / 1e6:.1f} ms; "
|
|
f"gaps >= {a.min_us:.0f} us: {len(gaps)}, {sum(g[1] - g[0] for g in gaps) / 1e6:.1f} ms")
|
|
by_cause = defaultdict(lambda: [0, 0])
|
|
by_site = defaultdict(lambda: [0, 0])
|
|
by_call = defaultdict(lambda: [0, 0])
|
|
for s, e, p, n in gaps:
|
|
length = e - s
|
|
i = bisect.bisect_left(starts, s - 50_000_000)
|
|
launch = 0
|
|
best, best_ov = None, 0
|
|
while i < len(apis) and apis[i].start < e:
|
|
x = apis[i]
|
|
i += 1
|
|
ov = min(e, x.end) - max(s, x.start)
|
|
if ov <= 0:
|
|
continue
|
|
nm = tr.names.get(x.name, "?")
|
|
if "Launch" in nm or "Graph" in nm:
|
|
launch += ov
|
|
elif any(b in nm for b in BLOCKING) and ov > best_ov:
|
|
best, best_ov = nm, ov
|
|
j = bisect.bisect_left(h2d_ends, e - TIGHT_NS)
|
|
if j < len(h2d_ends) and h2d_ends[j] <= e + 1_000 and h2d_ends[j] >= s:
|
|
cause = "copy-wait"
|
|
elif best and best_ov >= 0.3 * length:
|
|
cause = "host-sync"
|
|
elif launch >= 0.3 * length:
|
|
cause = "launch-bound"
|
|
else:
|
|
cause = "host-gap"
|
|
site = (L.family(tr.kname(p))[:30], L.family(tr.kname(n))[:30], cause + (" " + best if cause == "host-sync" else ""))
|
|
for d, key in ((by_cause, cause), (by_site, site), (by_call, best or "-")):
|
|
d[key][0] += 1
|
|
d[key][1] += length
|
|
print("\nby cause:")
|
|
for c, (n, t) in sorted(by_cause.items(), key=lambda kv: -kv[1][1]):
|
|
print(f" {c:<14} {n:6d} gaps {t / 1e6:9.2f} ms ({100 * t / span:4.1f} % of the window)")
|
|
print(f"\ntop {a.top} sites by idle ms (previous kernel -> next kernel, cause):")
|
|
for (p, n, c), (cnt, t) in sorted(by_site.items(), key=lambda kv: -kv[1][1])[: a.top]:
|
|
print(f" {t / 1e6:8.2f} ms {cnt:5d} gaps {p} -> {n} [{c}]")
|
|
print("\nby blocking call overlapping the gap:")
|
|
for c, (n, t) in sorted(by_call.items(), key=lambda kv: -kv[1][1])[:8]:
|
|
print(f" {c:<28} {n:6d} gaps {t / 1e6:9.2f} ms")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|