Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/gpu-head-to-head/blob/main/bench/test-nsys-classify.py # gpu-head-to-head aien-dev/gpu-head-to-head Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `gpu-head-to-head / bench / test-nsys-classify.py` 85 lines ยท 4255 bytes Raw Blame History 1 #!/usr/bin/env python3 2 """PROTOCOL.md A10: nsys_classify.py on a synthetic decode step, with a negative control.""" 3 import csv 4 import json 5 from pathlib import Path 6 import subprocess 7 import sys 8 import tempfile 9 import unittest 10 11 HERE = Path(__file__).resolve().parent 12 13 # One synthetic step: a GDN layer, an attention layer, final norm, lm_head, sampling. 14 LAYER_GDN = [('fused_add_rms_norm_kernel', 100), ('cutlass_scaled_mm_sm100', 300), 15 ('causal_conv1d_update_kernel', 50), ('fused_recurrent_gated_delta_rule_fwd', 400), 16 ('rms_norm_kernel', 30), ('cutlass_scaled_mm_sm100', 200), 17 ('fused_add_rms_norm_kernel', 100), ('cutlass_scaled_mm_sm100', 40), 18 ('topkGatingSoftmax', 60), ('moe_align_block_size_kernel', 20), 19 ('marlin_moe_wna16::Marlin<4>', 2000), ('act_and_mul_kernel', 30), ('marlin_moe_wna16::Marlin<4>', 1500), 20 ('cutlass_scaled_mm_sm100', 120), ('cutlass_scaled_mm_sm100', 90), ('moe_sum_kernel', 10)] 21 LAYER_ATTN = [('fused_add_rms_norm_kernel', 100), ('cutlass_scaled_mm_sm100', 250), 22 ('rotary_embedding_kernel', 20), ('reshape_and_cache_flash_kernel', 15), 23 ('BatchDecodeWithPagedKVCacheKernel', 500), ('cutlass_scaled_mm_sm100', 210), 24 ('fused_add_rms_norm_kernel', 100), ('cutlass_scaled_mm_sm100', 40), 25 ('topkGatingSoftmax', 60), ('marlin_moe_wna16::Marlin<4>', 2100), 26 ('marlin_moe_wna16::Marlin<4>', 1600), ('cutlass_scaled_mm_sm100', 130), ('cutlass_scaled_mm_sm100', 95)] 27 TAIL = [('rms_norm_kernel', 40), ('nvjet_tst_bf16_gemm', 4_000_000), ('argmax_kernel', 25), 28 ('Memcpy DtoH (Device -> Pageable)', 8)] 29 30 31 def run(kernels): 32 with tempfile.TemporaryDirectory() as tmp: 33 path = Path(tmp, 'trace.csv') 34 with open(path, 'w', newline='') as f: 35 w = csv.writer(f) 36 w.writerow(['Start (ns)', 'Duration (ns)', 'CorrId', 'Name']) 37 t = 1000 38 for i, (name, dur) in enumerate(kernels): 39 w.writerow([t, dur, i, name]) 40 t += dur + 5 41 r = subprocess.run([sys.executable, str(HERE / 'nsys_classify.py'), str(path)], 42 capture_output=True, text=True, timeout=10) 43 out = json.loads(r.stdout[:r.stdout.rindex('}') + 1]) if r.stdout.strip() else None 44 return r.returncode, out, r.stdout 45 46 47 class ClassifyTests(unittest.TestCase): 48 def test_synthetic_step(self): 49 rc, out, stdout = run(LAYER_GDN + LAYER_ATTN + TAIL) 50 self.assertEqual(rc, 0, stdout) 51 ns = out['ns'] 52 self.assertEqual(out['steps'], 1) 53 self.assertEqual(ns['gate_up'], 2000 + 2100) 54 self.assertEqual(ns['down'], 1500 + 1600) 55 self.assertEqual(ns['lm_head'], 4_000_000) 56 self.assertEqual(ns['gdn'], 450) 57 self.assertEqual(ns['attn'], 535) 58 self.assertEqual(ns['mix'], 300 + 250) 59 self.assertEqual(ns['o_proj'], 200 + 210) 60 self.assertEqual(ns['router'], 40 + 60 + 40 + 60) 61 self.assertEqual(ns['shared_gate_up'], 120 + 130) 62 self.assertEqual(ns['shared_down'], 90 + 95) 63 self.assertEqual(ns['perm'], 30) 64 self.assertEqual(ns['other'], 0) 65 self.assertEqual(out['total_ns'], sum(d for _, d in LAYER_GDN + LAYER_ATTN + TAIL)) 66 self.assertIn('h2hNsys = <|"total_ns" -> %d' % out['total_ns'], stdout.splitlines()[-1]) 67 68 def test_unknown_kernels_are_inconclusive(self): 69 # negative control: same step with most time in kernels the rules do not know 70 rc, out, _ = run(LAYER_GDN + [('mystery_kernel_a', 9_000_000), ('mystery_kernel_b', 10)] + TAIL) 71 self.assertEqual(rc, 2) 72 self.assertEqual(out['verdict'], 'INCONCLUSIVE') 73 self.assertEqual(out['other_top'][0], dict(name='mystery_kernel_a', ns=9_000_000)) 74 75 def test_missing_columns_are_unusable(self): 76 with tempfile.TemporaryDirectory() as tmp: 77 path = Path(tmp, 'bad.csv') 78 path.write_text('Start,Name\n1,x\n') 79 r = subprocess.run([sys.executable, str(HERE / 'nsys_classify.py'), str(path)], 80 capture_output=True, text=True, timeout=10) 81 self.assertEqual(r.returncode, 1) 82 83 84 if __name__ == '__main__': 85 unittest.main()