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
CUDApp / evidence / FLASH-NEXT-CODER-FULL-20261118 / fullcover.py
| 1 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | # fullcover.py: run the rowrans measurement coder over EVERY routed-expert weight matrix of a local |
| 3 | # Qwen3.8-Flash-Next-FP8 checkpoint. Measuring instrument only (host, CPU, sizes; no timing claim). |
| 4 | # usage: python3 -I fullcover.py MODEL_DIR ROWRANS_BIN OUT_DIR WORKERS [INDEX_JSON] |
| 5 | # One JSONL per shard in OUT_DIR (resumable); waits for shards that are still downloading |
| 6 | # (a shard is ready when its file exists and no <file>.aria2 control file is beside it). |
| 7 | import json, mmap, os, struct, subprocess, sys, time |
| 8 | from concurrent.futures import ProcessPoolExecutor |
| 9 | |
| 10 | MODEL, ROWRANS, OUT, WORKERS = sys.argv[1], sys.argv[2], sys.argv[3], int(sys.argv[4]) |
| 11 | |
| 12 | |
| 13 | def is_expert_weight(k, v): |
| 14 | return '.mlp.experts.' in k and k.endswith('.weight') and v['dtype'] == 'F8_E4M3' |
| 15 | |
| 16 | |
| 17 | def do_shard(sh): |
| 18 | path = os.path.join(MODEL, sh) |
| 19 | dst = os.path.join(OUT, sh + '.jsonl') |
| 20 | tmp = f'/dev/shm/fullcover-{os.getpid()}.raw' |
| 21 | rows = [] |
| 22 | with open(path, 'rb') as f: |
| 23 | n = struct.unpack('<Q', f.read(8))[0] |
| 24 | hdr = json.loads(f.read(n)) |
| 25 | base = 8 + n |
| 26 | mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) |
| 27 | try: |
| 28 | for k in sorted(hdr): |
| 29 | if k == '__metadata__' or not is_expert_weight(k, hdr[k]): |
| 30 | continue |
| 31 | v = hdr[k] |
| 32 | a, b = v['data_offsets'] |
| 33 | r, c = v['shape'] |
| 34 | assert b - a == r * c, (k, v) |
| 35 | with open(tmp, 'wb') as t: |
| 36 | t.write(mm[base + a:base + b]) |
| 37 | raw, coded, rt = map(int, subprocess.check_output([ROWRANS, tmp, str(r), str(c)]).split()) |
| 38 | raw2, coded2, rt2 = map(int, subprocess.check_output([ROWRANS, tmp, str(r), str(c), 'flip']).split()) |
| 39 | assert raw == r * c and raw2 == raw and coded2 == coded, k |
| 40 | rows.append({'name': k, 'rows': r, 'cols': c, 'raw': raw, 'coded': coded, |
| 41 | 'roundtrip': rt, 'flip_detected': int(rt2 == 0)}) |
| 42 | finally: |
| 43 | mm.close() |
| 44 | if os.path.exists(tmp): |
| 45 | os.remove(tmp) |
| 46 | with open(dst + '.part', 'w') as g: |
| 47 | for row in rows: |
| 48 | g.write(json.dumps(row, sort_keys=True) + '\n') |
| 49 | g.write(json.dumps({'shard': sh, 'expert_matrices': len(rows), 'done': 1}) + '\n') |
| 50 | os.replace(dst + '.part', dst) |
| 51 | return sh, len(rows) |
| 52 | |
| 53 | |
| 54 | def main(): |
| 55 | os.makedirs(OUT, exist_ok=True) |
| 56 | # INDEX may be given separately: the downloader fetches model.safetensors.index.json last. |
| 57 | index = sys.argv[5] if len(sys.argv) > 5 else os.path.join(MODEL, 'model.safetensors.index.json') |
| 58 | print('index_sha256', __import__('hashlib').sha256(open(index, 'rb').read()).hexdigest(), flush=True) |
| 59 | ix = json.load(open(index))['weight_map'] |
| 60 | shards = sorted(set(ix.values())) |
| 61 | pending = [s for s in shards if not os.path.exists(os.path.join(OUT, s + '.jsonl'))] |
| 62 | print('shards', len(shards), 'pending', len(pending), flush=True) |
| 63 | with ProcessPoolExecutor(WORKERS) as ex: |
| 64 | running = {} |
| 65 | while pending or running: |
| 66 | for s in list(pending): |
| 67 | p = os.path.join(MODEL, s) |
| 68 | if len(running) < WORKERS * 2 and os.path.exists(p) and not os.path.exists(p + '.aria2'): |
| 69 | running[s] = ex.submit(do_shard, s) |
| 70 | pending.remove(s) |
| 71 | for s, fut in list(running.items()): |
| 72 | if fut.done(): |
| 73 | sh, nrows = fut.result() |
| 74 | print('done', sh, nrows, 'left', len(pending) + len(running) - 1, flush=True) |
| 75 | del running[s] |
| 76 | time.sleep(2) |
| 77 | print('ALL_SHARDS_DONE', flush=True) |
| 78 | |
| 79 | |
| 80 | if __name__ == '__main__': |
| 81 | main() |