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 / amd-qwen38-native-20261113 / oracle / lighthouse-oracle.py

58 lines · 4199 bytes Raw Blame History
1# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2"""Harness (oracle only): run real2's own Card and AMD-library reference on the lighthouse256 fixture to place the
3native plan's first divergence (index 109). Card source is not edited; GOLD and capacity are overridden here as in
4emit-plan.py. One mode per process, so each gets a fresh device:
5 graph Card, HIP graph replay, free-running greedy (the path the native plan was recorded from)
6 forced Card, HIP graph replay, teacher-forced on the Spark ids: at every generated index, Spark token rank,
7 Omega top-1 minus top-2 logit and top-1 minus Spark-token logit
8 reference AMD library (transformers, same FP8 checkpoint), free-running greedy, top-5 per index
9Usage: lighthouse-oracle.py DRIVER WEIGHTS FIXTURE MODE OUT.json [--capacity=N] (capacity default: fixture's)
10Reading: graph ids equal to the native ids means the plan is faithful to Card. A small margin at the first divergence,
11with reference also near-tied there, points to precision; a large margin or reference agreeing with Spark points to a kernel.
12"""
13import importlib.util,json,os,statistics,sys
14from pathlib import Path
15
16driver,weights,fixture_path,mode,out=Path(sys.argv[1]),Path(sys.argv[2]),Path(sys.argv[3]),sys.argv[4],Path(sys.argv[5])
17if mode not in ('graph','forced','reference'):raise SystemExit('mode must be graph, forced or reference')
18fixture=json.loads(fixture_path.read_bytes())
19cap=next((int(a.split('=',1)[1]) for a in sys.argv[6:] if a.startswith('--capacity=')),fixture.get('capacity'))
20P,G=fixture['prompt_ids'],fixture['generated_ids']
21os.environ['AMDSERVE_WEIGHTS']=str(weights);os.environ['AMDSERVE_OUTPUT']=str(out)+'.log'
22spec=importlib.util.spec_from_file_location('real2driver',driver);drv=importlib.util.module_from_spec(spec);spec.loader.exec_module(drv)
23# measure()/reference() compare against the driver's own top-5 references; this fixture has none, so those columns are void.
24drv.GOLD=dict(drv.GOLD,prompt_ids=list(P),generated_ids=list(G),top5=[[(0,0.0)]]*len(G))
25drv.top5_difference=lambda logits,expected:None
26if cap is not None:
27 if cap<len(P)+len(G):raise SystemExit('capacity below prompt+generated')
28 drv.Card.cap=property(lambda self:cap,lambda self,value:None)
29import torch
30def first_div(ids):return next((i for i,(a,b) in enumerate(zip(ids,G)) if a!=b),None)
31def row(i,logits):
32 val,idx=logits.float().topk(5);s=G[i];ls=float(logits[s])
33 return dict(i=i,spark=s,top5=list(zip(idx.tolist(),[round(v,4) for v in val.tolist()])),
34 spark_rank=int((logits.float()>ls).sum()),top1_minus_top2=round(float(val[0]-val[1]),4),top1_minus_spark=round(float(val[0])-ls,4))
35drv.prepare()
36if mode=='reference':
37 r=drv.reference(torch);ids=r['ids']
38 res=dict(mode=mode,ids=ids,first_divergence=first_div(ids),match=sum(a==b for a,b in zip(ids,G)),top5=r['top5'],decode_ms_median=r.get('decode_ms_median'))
39elif mode=='graph':
40 with torch.inference_mode():r=drv.measure(torch,'omega-graph')
41 ids=r['ids'];res=dict(mode=mode,capacity=cap,ids=ids,first_divergence=first_div(ids),match=sum(a==b for a,b in zip(ids,G)),top5=r['top5'],decode_ms_median=r['decode_ms_median'])
42else:
43 card=drv.Card(torch,drv.Hip(torch),'omega');rows=[]
44 try:
45 with torch.inference_mode():
46 for t in P[:-1]:card.forward(t,graph=True)
47 logits,_,_=card.forward(P[-1],graph=True);rows.append(row(0,logits))
48 for i in range(1,len(G)):
49 logits,_,_=card.forward(G[i-1],graph=True);rows.append(row(i,logits))
50 finally:
51 if card.graph is not None:card.hip.release_graph(card.graph)
52 bad=[r for r in rows if r['spark_rank']!=0]
53 res=dict(mode=mode,capacity=cap,rows=rows,disagree=[r['i'] for r in bad],
54 disagree_max_top1_minus_spark=max((r['top1_minus_spark'] for r in bad),default=0.0),
55 median_top1_minus_top2=statistics.median(r['top1_minus_top2'] for r in rows))
56res.update(fixture_sha256=__import__('hashlib').sha256(fixture_path.read_bytes()).hexdigest(),driver=str(driver))
57out.write_text(json.dumps(res)+'\n')
58print('ORACLE',mode,json.dumps({k:v for k,v in res.items() if k not in ('ids','top5','rows')}))