Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
109 lines
4.7 KiB
Python
109 lines
4.7 KiB
Python
"""Flag waits on the CPU/PCIe leg of the verify graph, per layer, from one CUPTI window (ledger rows V5, V7).
|
|
|
|
usage: python3 cupti_flag_waits.py TRACE_DIR LEL|VANL [T]
|
|
|
|
Per layer (doorbell to the next doorbell) the three waits in stream order are: A (plan published; LEL fuses the plan copy
|
|
into `chain_wait_copy_i32`, VANL has `wait_flag_ge` + `copy_i32_from_mapped`), B (PCIe copies landed) and M (CPU rows
|
|
written). A wait whose flag was already set when it started shows its floor (1.3-1.4 us: one mapped read and the fence);
|
|
a longer wait is the real wait for the other side. The script prints, per layer class (with / without a PCIe leg),
|
|
the share of layers, the wait durations, when the M wait started and ended relative to the doorbell, and each wait's
|
|
contribution to one graph (48 layers). For LEL the PCIe leg is a B wait above 3 us (DMA, flag B behind the copies);
|
|
for VANL it is a `fetch_blobs` kernel above 2 us. Optional T keeps only windows of T tokens (the grid of the CPU-row
|
|
copy is T * 10 blocks). Host-side Python only; reads the trace, uses no GPU.
|
|
"""
|
|
import collections
|
|
import statistics as stats
|
|
import sys
|
|
|
|
sys.path.insert(0, "/data/leleka-e2e/decode/tools")
|
|
import lct as L # noqa: E402
|
|
|
|
FLOOR_US = {"LEL": 3.0, "VANL": 2.0}
|
|
LONG_M_US = 4.0
|
|
|
|
|
|
def load(path):
|
|
t = L.Trace(path)
|
|
by_corr = collections.defaultdict(list)
|
|
for k in t.kernels:
|
|
by_corr[k.corr].append(k)
|
|
graphs = [c for c, v in by_corr.items() if len(v) >= 900]
|
|
graphs.sort(key=lambda c: min(k.start for k in by_corr[c]))
|
|
return t, [sorted(by_corr[c], key=lambda k: k.start) for c in graphs[2:-2]]
|
|
|
|
|
|
def is_wait(name):
|
|
return "wait_flag_ge" in name or "wait_copy_i32" in name
|
|
|
|
|
|
def layer_record(name_of, seg, doorbell, mode):
|
|
waits = [k for k in seg if is_wait(name_of(k))]
|
|
if len(waits) != 3:
|
|
return None
|
|
a, b, m = waits
|
|
if mode == "LEL":
|
|
pcie = (b.end - b.start) / 1e3 > FLOOR_US["LEL"]
|
|
else:
|
|
fetch = [k for k in seg if "fetch_blobs" in name_of(k)]
|
|
pcie = bool(fetch) and (fetch[0].end - fetch[0].start) / 1e3 > FLOOR_US["VANL"]
|
|
copy_rows = [k for k in seg if "copy_rows" in name_of(k)]
|
|
us = lambda x: x / 1e3 # noqa: E731
|
|
return dict(
|
|
pcie=pcie, tokens=copy_rows[0].gx // 10 if copy_rows else -1,
|
|
a=us(a.end - a.start), b=us(b.end - b.start), m=us(m.end - m.start),
|
|
m_start=us(m.start - doorbell.start), m_end=us(m.end - doorbell.start),
|
|
)
|
|
|
|
|
|
def layers(path, mode, tokens):
|
|
t, graphs = load(path)
|
|
name_of = lambda k: t.names.get(k.name, "") # noqa: E731
|
|
out = []
|
|
for ks in graphs:
|
|
bells = [k for k in ks if "doorbell_publish" in name_of(k)]
|
|
if len(bells) != 48:
|
|
continue
|
|
for i, bell in enumerate(bells):
|
|
end = bells[i + 1].start if i < 47 else bell.start + 400000
|
|
rec = layer_record(name_of, [k for k in ks if bell.start <= k.start < end], bell, mode)
|
|
if rec and (tokens is None or rec["tokens"] == tokens):
|
|
out.append(rec)
|
|
return out
|
|
|
|
|
|
def quantiles(values):
|
|
v = sorted(values)
|
|
n = len(v)
|
|
return "mean %.1f p10 %.1f p50 %.1f p90 %.1f" % (stats.mean(v), v[n // 10], v[n // 2], v[9 * n // 10])
|
|
|
|
|
|
def report_class(rows, total, label):
|
|
share = len(rows) / total
|
|
print("%s: %.1f%% of layers (%d)" % (label, 100 * share, len(rows)))
|
|
for key in ("a", "b", "m"):
|
|
print(" wait %s duration: %s; per graph %.3f ms" % (
|
|
key.upper(), quantiles([r[key] for r in rows]), 48 * share * stats.mean(r[key] for r in rows) / 1e3))
|
|
print(" M wait start after doorbell: %s" % quantiles([r["m_start"] for r in rows]))
|
|
print(" M wait end after doorbell: %s" % quantiles([r["m_end"] for r in rows]))
|
|
long_m = [r for r in rows if r["m"] > LONG_M_US]
|
|
if long_m:
|
|
print(" M wait longer than %.0f us (CPU rows not yet there): %.1f%% of these layers, end after doorbell: %s" % (
|
|
LONG_M_US, 100 * len(long_m) / len(rows), quantiles([r["m_end"] for r in long_m])))
|
|
|
|
|
|
def main():
|
|
path, mode = sys.argv[1], sys.argv[2]
|
|
tokens = int(sys.argv[3]) if len(sys.argv) > 3 else None
|
|
rows = layers(path, mode, tokens)
|
|
print("%s %s layers %d (graphs %d)" % (mode, path, len(rows), len(rows) // 48))
|
|
for flag, label in ((False, "layers without a PCIe leg"), (True, "layers with a PCIe leg")):
|
|
subset = [r for r in rows if r["pcie"] == flag]
|
|
if subset:
|
|
report_class(subset, len(rows), label)
|
|
total = {k: 48 * stats.mean(r[k] for r in rows) / 1e3 for k in ("a", "b", "m")}
|
|
print("per graph: A %.3f + B %.3f + M %.3f = %.3f ms" % (total["a"], total["b"], total["m"], sum(total.values())))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|