gpu-head-to-head
aien-dev/gpu-head-to-head
Brought by drakestapleton
gpu-head-to-head / bench / nsys_classify.py
| 1 | #!/usr/bin/env python3 |
| 2 | """PROTOCOL.md A10/A8: classify the kernels of one vLLM decode trace into the l2 timeline buckets. |
| 3 | |
| 4 | External test equipment, not part of any arm. Input: the CSV of |
| 5 | nsys stats -r cuda_gpu_trace -f csv <report>.nsys-rep |
| 6 | (columns "Start (ns)", "Duration (ns)", "Name"). Output: integer nanosecond sums and kernel counts per |
| 7 | bucket as JSON on stdout plus a Wolfram association on the last line; every ratio is taken in Wolfram. |
| 8 | Exit 0 = classified, 2 = INCONCLUSIVE (unclassified time > 10 % of total), 1 = unusable input. |
| 9 | |
| 10 | Kernel names are matched case-insensitively, first rule wins. Dense GEMMs carry no role in their |
| 11 | names, so their role comes from the position inside a layer: |
| 12 | before the token mixer -> mix (QKV / in_proj), after the mixer -> o_proj, after the post-mixer norm |
| 13 | -> router (gate linear), inside the MoE phase -> shared_gate_up / shared_down alternately; |
| 14 | any GEMM of >= 1 ms is lm_head, and steps = number of lm_head kernels. Prefill steps inside the |
| 15 | captured range are counted the same way; the trace covers one decode-c8 cell (A10). |
| 16 | """ |
| 17 | import csv |
| 18 | import json |
| 19 | import sys |
| 20 | |
| 21 | BUCKETS = ['gate_up', 'down', 'lm_head', 'gdn', 'mix', 'attn', 'res', 'o_proj', 'router', 'perm', |
| 22 | 'shared_gate_up', 'shared_down', 'act', 'sample', 'copy', 'other'] |
| 23 | RULES = [ # (bucket, substrings); first match wins |
| 24 | ('copy', ['memcpy', 'memset']), |
| 25 | ('moe', ['marlin_moe', 'moe_wna16', 'fused_moe', 'cutlass_moe', 'grouped']), |
| 26 | ('router', ['topk', 'gating', 'routing']), |
| 27 | ('perm', ['moe_align', 'moe_sum', 'permute', 'sort', 'count_and_sort', 'scatter', 'gather']), |
| 28 | ('gdn', ['gated_delta', 'recurrent', 'causal_conv1d', 'gdn', 'l2norm', 'fused_sigmoid_gating']), |
| 29 | ('attn', ['flashinfer', 'batchdecode', 'batchprefill', 'flash_fwd', 'fmha', 'reshape_and_cache', 'rotary']), |
| 30 | ('res', ['rms_norm', 'rmsnorm', 'fused_add', 'layernorm']), |
| 31 | ('act', ['silu', 'act_and_mul']), |
| 32 | ('sample', ['argmax', 'sampl', 'softmax', 'logits']), |
| 33 | ('gemm', ['gemm', 'gemv', 'cutlass', 'nvjet', 'cublas', 'matmul', 'splitk', 'scaled_mm', 'marlin', 'sm90_', 'sm100_']), |
| 34 | ] |
| 35 | LM_HEAD_NS = 1_000_000 |
| 36 | |
| 37 | |
| 38 | def rule(name): |
| 39 | low = name.lower() |
| 40 | for bucket, keys in RULES: |
| 41 | if any(k in low for k in keys): |
| 42 | return bucket |
| 43 | return 'other' |
| 44 | |
| 45 | |
| 46 | def classify(rows): |
| 47 | ns = dict.fromkeys(BUCKETS, 0) |
| 48 | count = dict.fromkeys(BUCKETS, 0) |
| 49 | other = {} |
| 50 | phase, moe_gemms, shared = 'pre', 0, 0 |
| 51 | for start, dur, name in rows: |
| 52 | kind = rule(name) |
| 53 | if kind == 'moe': |
| 54 | if phase != 'moe': |
| 55 | phase, moe_gemms, shared = 'moe', 0, 0 |
| 56 | bucket = 'gate_up' if moe_gemms % 2 == 0 else 'down' |
| 57 | moe_gemms += 1 |
| 58 | elif kind == 'router': |
| 59 | bucket, phase = 'router', 'moe' |
| 60 | elif kind in ('gdn', 'attn'): |
| 61 | bucket, phase = kind, 'core' |
| 62 | elif kind == 'res': |
| 63 | bucket = 'res' |
| 64 | if phase == 'moe': |
| 65 | phase = 'pre' |
| 66 | elif kind == 'gemm': |
| 67 | if dur >= LM_HEAD_NS: |
| 68 | bucket, phase = 'lm_head', 'pre' |
| 69 | elif phase == 'pre': |
| 70 | bucket = 'mix' |
| 71 | elif phase == 'core': |
| 72 | bucket, phase = 'o_proj', 'post' |
| 73 | elif phase == 'post': |
| 74 | bucket = 'router' |
| 75 | else: |
| 76 | bucket = 'shared_gate_up' if shared % 2 == 0 else 'shared_down' |
| 77 | shared += 1 |
| 78 | else: |
| 79 | bucket = kind |
| 80 | if kind == 'other': |
| 81 | other[name] = other.get(name, 0) + dur |
| 82 | ns[bucket] += dur |
| 83 | count[bucket] += 1 |
| 84 | return ns, count, other |
| 85 | |
| 86 | |
| 87 | def read(path): |
| 88 | with open(path, newline='') as f: |
| 89 | reader = csv.DictReader(f) |
| 90 | need = {'Start (ns)', 'Duration (ns)', 'Name'} |
| 91 | if not reader.fieldnames or not need <= set(reader.fieldnames): |
| 92 | raise ValueError('missing columns %s' % sorted(need - set(reader.fieldnames or []))) |
| 93 | rows = [(int(r['Start (ns)']), int(r['Duration (ns)']), r['Name']) for r in reader] |
| 94 | rows.sort() |
| 95 | if not rows: |
| 96 | raise ValueError('no kernels') |
| 97 | return rows |
| 98 | |
| 99 | |
| 100 | def main(argv): |
| 101 | if len(argv) != 2: |
| 102 | print('usage: nsys_classify.py cuda_gpu_trace.csv', file=sys.stderr) |
| 103 | return 1 |
| 104 | try: |
| 105 | rows = read(argv[1]) |
| 106 | except (OSError, ValueError, KeyError) as e: |
| 107 | print('unusable input: %s' % e, file=sys.stderr) |
| 108 | return 1 |
| 109 | ns, count, other = classify(rows) |
| 110 | total = sum(ns.values()) |
| 111 | top = sorted(other.items(), key=lambda kv: -kv[1])[:10] |
| 112 | verdict = 'INCONCLUSIVE' if ns['other'] * 10 > total else 'CLASSIFIED' |
| 113 | out = dict(verdict=verdict, kernels=len(rows), total_ns=total, steps=count['lm_head'], |
| 114 | ns=ns, count=count, other_top=[dict(name=n, ns=v) for n, v in top]) |
| 115 | print(json.dumps(out, indent=1)) |
| 116 | assoc = ', '.join('"%s" -> %d' % (b, ns[b]) for b in BUCKETS) |
| 117 | print('h2hNsys = <|"total_ns" -> %d, "steps" -> %d, "ns" -> <|%s|>|>;' % (total, count['lm_head'], assoc)) |
| 118 | return 2 if verdict == 'INCONCLUSIVE' else 0 |
| 119 | |
| 120 | |
| 121 | if __name__ == '__main__': |
| 122 | sys.exit(main(sys.argv)) |