interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / evidence / FLASH-NEXT-CONTROL-FIX-20261118 / exercise.py

72 lines · 3676 bytes Raw Blame History
1# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2# exercise.py: re-run every matrix whose historical midpoint flip was reported as missed
3# (flip_detected 0 in the fullcover JSONL), plus one clean control per miss, through the historical
4# binary (reproduces the old verdict) and rowrans_strict (says why). Measuring instrument only.
5# Reads each tensor by byte range from the local checkpoint into one temporary file in /dev/shm (removed
6# after use); no checkpoint copy is kept. Writes one JSON object per run to OUT_JSONL.
7# usage: python3 -I exercise.py FULLCOVER_OUT MODEL_DIR HIST_BIN STRICT_BIN OUT_JSONL
8import glob, hashlib, json, mmap, os, struct, subprocess, sys
9
10FC, MODEL, HIST, STRICT, OUT = sys.argv[1:6]
11
12
13def kv(line):
14 return dict(t.split('=', 1) for t in line.split()[1:])
15
16
17def run(cmd):
18 return subprocess.check_output(cmd, text=True)
19
20
21def main():
22 shards = sorted(glob.glob(os.path.join(FC, '*.jsonl')))
23 work = [] # (shard, name, role, historical record)
24 for p in shards:
25 recs = [json.loads(l) for l in open(p)]
26 assert recs[-1].get('done') == 1, p
27 recs = recs[:-1]
28 for i, r in enumerate(recs):
29 if r['flip_detected'] == 0:
30 work.append((os.path.basename(p)[:-6], r['name'], 'missed', r))
31 # control: the next matrix in the same shard whose historical flip was detected
32 for j in list(range(i + 1, len(recs))) + list(range(0, i)):
33 if recs[j]['flip_detected'] == 1:
34 work.append((os.path.basename(p)[:-6], recs[j]['name'], 'control', recs[j]))
35 break
36 tmp = f'/dev/shm/control-fix-{os.getpid()}.raw'
37 with open(OUT, 'w') as g:
38 by_shard = {}
39 for sh, name, role, rec in work:
40 by_shard.setdefault(sh, []).append((name, role, rec))
41 for sh in sorted(by_shard):
42 with open(os.path.join(MODEL, sh), 'rb') as f:
43 n = struct.unpack('<Q', f.read(8))[0]
44 hdr = json.loads(f.read(n))
45 mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
46 try:
47 for name, role, rec in by_shard[sh]:
48 a, b = hdr[name]['data_offsets']
49 rows, cols = hdr[name]['shape']
50 data = mm[8 + n + a:8 + n + b]
51 with open(tmp, 'wb') as t:
52 t.write(data)
53 h0 = run([HIST, tmp, str(rows), str(cols)]).split()
54 h1 = run([HIST, tmp, str(rows), str(cols), 'flip']).split()
55 s = run([STRICT, tmp, str(rows), str(cols), 'mid']).splitlines()
56 head, clean, pos, flip = kv('x ' + s[0]), kv(s[1]), kv(s[2]), kv(s[3])
57 out = {'shard': sh, 'name': name, 'role': role, 'rows': rows, 'cols': cols,
58 'tensor_sha256': hashlib.sha256(data).hexdigest(),
59 'fullcover': {k: rec[k] for k in ('coded', 'roundtrip', 'flip_detected')},
60 'hist_rerun': {'coded': int(h0[1]), 'roundtrip': int(h0[2]),
61 'flip_detected': int(h1[2] == '0')},
62 'strict': {'coded': int(head['coded']), 'clean': clean, 'flip': {**pos, **flip}}}
63 g.write(json.dumps(out, sort_keys=True) + '\n')
64 g.flush()
65 finally:
66 mm.close()
67 if os.path.exists(tmp):
68 os.remove(tmp)
69
70
71if __name__ == '__main__':
72 main()