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/CUDApp/blob/main/tools/latmeas/diff_nak.py # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. 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 `CUDApp / tools / latmeas / diff_nak.py` 209 lines · 8783 bytes Raw Blame History 1 #!/usr/bin/env python3 2 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 """Diff CUDA++ fixed-latency stalls against Mesa NAK at one pinned commit. 4 5 The numbers in catalog.json are citations (file, line, cell), not a copy of 6 Mesa. --check reads the CUDA++ lines and checks the SM120 +1 arithmetic. 7 --verify-mesa re-downloads the pinned files and checks the cited cells. 8 It does not write a chip result. 9 """ 10 import argparse 11 import json 12 import os 13 import sys 14 import tempfile 15 import urllib.request 16 17 ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) 18 CATALOG = os.path.join(os.path.dirname(__file__), "catalog.json") 19 20 21 def load(): 22 with open(CATALOG, encoding="utf-8") as f: 23 return json.load(f) 24 25 26 def sm120_gpr(base, adjust): 27 """sm120_instr_latencies.rs GPR raw: table value + max(adjust, 1).""" 28 return base + max(adjust, 1) 29 30 31 def check_sources(cat): 32 errors = [] 33 for row in cat["rows"]: 34 path = os.path.join(ROOT, row["encoder_file"]) 35 with open(path, encoding="utf-8") as f: 36 lines = f.read().splitlines() 37 n = row["encoder_line"] 38 omega = row["encoder_file"].endswith(".om") 39 actual = lines[n - 1].strip() if 1 <= n <= len(lines) else None 40 expected = row["encoder_substr"] 41 if actual is None or (actual != expected if omega else expected not in actual): 42 errors.append("%s: %s:%d does not match %r" % (row["id"], row["encoder_file"], n, expected)) 43 # The Omega stall assignment is a separate statement from NOP creation. 44 # Also retain the preceding tensor instruction's stall in the total. 45 contexts = { 46 "qmma_reader": ((-4, "put x 7 15"), (-1, "omega_cg_qmma_i x 0"), 47 (1, "omega_cg_qmma_emit b m x 11")), 48 "hmma_reader": ((-5, "put x 7 ulong 15"), (-1, "tc_i x 0"), 49 (1, "tc_emit b m x 11")), 50 } 51 if omega: 52 for offset, expected in contexts.get(row["id"], ()): 53 index = n - 1 + offset 54 if not 0 <= index < len(lines) or lines[index].strip() != expected: 55 errors.append("%s: %s:%d does not match %r" % 56 (row["id"], row["encoder_file"], n + offset, expected)) 57 # The pair tightener's default class excludes IMAD.WIDE and FMNMX. 58 sched = os.path.join(ROOT, "src/omega_gpu_elementwise_api.c") 59 with open(sched, encoding="utf-8") as f: 60 text = f.read().splitlines() 61 window = "\n".join(text[791:801]) 62 if "IMAD_WIDE" in window or "FMNMX" in window: 63 errors.append("fixed_pair_class now names IMAD_WIDE or FMNMX; the catalog's blanket-6 claim changed") 64 if "case K_FIXED: in.control = CW(6," not in text[76]: 65 errors.append("K_FIXED blanket stall is not 6 at src/omega_gpu_elementwise_api.c:77") 66 return errors 67 68 69 def disagreements(cat): 70 out = [] 71 for row in cat["rows"]: 72 nak = row["nak_stall"] 73 enc = row["encoder_stall"] 74 if nak is None or enc != nak: 75 out.append(row) 76 return out 77 78 79 def render(cat, rows): 80 lines = [] 81 lines.append("# CUDA++ fixed latencies vs NAK") 82 lines.append("") 83 lines.append("Mesa commit `%s`." % cat["mesa_commit"]) 84 lines.append("Clock: %s." % cat["mesa_clock"]) 85 lines.append("Priority 1 pairs match NAK on paper. They are measured because that match is circular.") 86 lines.append("Rows below are the ones that differ. A blank NAK stall means NAK calls the op decoupled.") 87 lines.append("") 88 lines.append("| id | pair | CUDA++ | NAK | kernel |") 89 lines.append("|---|---|---|---|---|") 90 for row in rows: 91 nak = row["nak"] 92 lines.append("| %s | %s | %s | %s | %s |" % (row["id"], row["pair"], row["encoder"], nak, row["kernel"])) 93 lines.append("") 94 return "\n".join(lines) + "\n" 95 96 97 def self_check(cat): 98 """Negative control: an equal pair is not a disagreement; 1 vs 2 is.""" 99 fake = {"rows": [ 100 {"encoder_stall": 5, "nak_stall": 5, "id": "equal"}, 101 {"encoder_stall": 1, "nak_stall": 2, "id": "differ"}, 102 {"encoder_stall": 5, "nak_stall": None, "id": "class"}, 103 ]} 104 got = [r["id"] for r in disagreements(fake)] 105 if got != ["differ", "class"]: 106 return "negative control listed %s" % got 107 if sm120_gpr(4, 0) != 5 or sm120_gpr(19, 9) != 28 or sm120_gpr(3, 0) != 4: 108 return "SM120 +1 arithmetic drifted" 109 if sm120_gpr(20, 9) != 29 or sm120_gpr(19, 7) != 26 or (13 + 1) != 14: 110 return "cited NAK totals drifted" 111 # Priority 1 must not show up as a disagreement. 112 p1 = [r["id"] for r in disagreements(cat) if r["priority"] == 1] 113 if p1: 114 return "priority 1 rows differ from NAK: %s" % p1 115 # Exercise the real source checker, not a parallel string-match oracle. 116 # A neighboring line and both changed/prefix-preserving stall values fail. 117 for row in cat["rows"]: 118 if not row["encoder_file"].endswith(".om"): 119 continue 120 bad = dict(row, encoder_line=row["encoder_line"] - 1) 121 if not any(e.startswith(row["id"] + ":") for e in check_sources({"rows": [bad]})): 122 return row["id"] + ": wrong-line negative control escaped" 123 with open(os.path.join(ROOT, row["encoder_file"]), encoding="utf-8") as f: 124 source = f.read().splitlines() 125 n = row["encoder_line"] - 1 126 with tempfile.TemporaryDirectory(prefix="latmeas-source-") as directory: 127 path = os.path.join(directory, "mutant.om") 128 bad = dict(row, encoder_file=path) 129 for replacement in (source[n].rsplit(" ", 1)[0] + " 0", source[n] + "0"): 130 changed = source[:] 131 changed[n] = replacement 132 with open(path, "w", encoding="utf-8") as f: 133 f.write("\n".join(changed) + "\n") 134 if not any(e.startswith(row["id"] + ":") for e in check_sources({"rows": [bad]})): 135 return row["id"] + ": changed-stall negative control escaped" 136 return None 137 138 139 def verify_mesa(commit): 140 base = "https://gitlab.freedesktop.org/mesa/mesa/-/raw/%s/" % commit 141 reg = urllib.request.urlopen(base + "src/nouveau/compiler/latencies/sm100/reg_raw.csv", timeout=60).read().decode() 142 pred = urllib.request.urlopen(base + "src/nouveau/compiler/latencies/sm100/pred_raw.csv", timeout=60).read().decode() 143 rs = urllib.request.urlopen(base + "src/nouveau/compiler/nak/sm120_instr_latencies.rs", timeout=60).read().decode() 144 lines = reg.splitlines() 145 hdr = lines[0].split(",") 146 alu = lines[1].split(",") 147 fma = lines[4].split(",") 148 149 def cell(parts, name): 150 return parts[hdr.index(name)] 151 152 checks = [ 153 (cell(alu, "alu"), "4"), 154 (cell(alu, "fma"), "5"), 155 (cell(alu, "dualalu"), "4"), 156 (cell(alu, "hmma"), "19"), 157 (cell(alu, "imma"), "19"), 158 (cell(alu, "imad_wide_write_dl"), "3"), 159 (cell(alu, "imad_wide_write_dh"), "5"), 160 (cell(fma, "fma"), "4"), 161 (cell(fma, "alu"), "5"), 162 ] 163 for got, want in checks: 164 if got != want: 165 raise SystemExit("reg_raw cell %s != %s" % (got, want)) 166 pred_lines = pred.splitlines() 167 pred_hdr = pred_lines[0].split(",") 168 redir = pred_lines[10].split(",") 169 if redir[0] != "redirected_fp64" or redir[pred_hdr.index("dualalu")] != "13": 170 raise SystemExit("pred_raw redirected_fp64/dualalu is not 13") 171 for needle in ("RegLatencySM100::Hmma => 9", "RegLatencySM100::Imma => 7", "NAK_SV_CLOCK" ): 172 # CLOCK is not in this file. Check the adjust arms and the +1 in raw(). 173 pass 174 if "RegLatencySM100::Hmma => 9" not in rs or "RegLatencySM100::Imma => 7" not in rs: 175 raise SystemExit("sm120 latency_adjust does not match the catalog") 176 if "+ 1" not in rs: 177 raise SystemExit("sm120 raw no longer adds padding") 178 print("mesa %s cells match the catalog" % commit) 179 180 181 def main(): 182 p = argparse.ArgumentParser() 183 p.add_argument("--check", action="store_true") 184 p.add_argument("--md", help="write the disagreement table") 185 p.add_argument("--verify-mesa", action="store_true") 186 args = p.parse_args() 187 cat = load() 188 errors = check_sources(cat) 189 bad = self_check(cat) 190 if bad: 191 errors.append(bad) 192 if errors: 193 for e in errors: 194 print("FAIL:", e, file=sys.stderr) 195 return 1 196 text = render(cat, disagreements(cat)) 197 sys.stdout.write(text) 198 if args.md: 199 with open(args.md, "w", encoding="utf-8") as f: 200 f.write(text) 201 if args.verify_mesa: 202 verify_mesa(cat["mesa_commit"]) 203 if not args.check and not args.md and not args.verify_mesa: 204 return 0 205 return 0 206 207 208 if __name__ == "__main__": 209 sys.exit(main())