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 / amd-qwen38-native-20261113 / oracle / lighthouse-oracle.py
| 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 |
| 3 | native plan's first divergence (index 109). Card source is not edited; GOLD and capacity are overridden here as in |
| 4 | emit-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 |
| 9 | Usage: lighthouse-oracle.py DRIVER WEIGHTS FIXTURE MODE OUT.json [--capacity=N] (capacity default: fixture's) |
| 10 | Reading: graph ids equal to the native ids means the plan is faithful to Card. A small margin at the first divergence, |
| 11 | with reference also near-tied there, points to precision; a large margin or reference agreeing with Spark points to a kernel. |
| 12 | """ |
| 13 | import importlib.util,json,os,statistics,sys |
| 14 | from pathlib import Path |
| 15 | |
| 16 | driver,weights,fixture_path,mode,out=Path(sys.argv[1]),Path(sys.argv[2]),Path(sys.argv[3]),sys.argv[4],Path(sys.argv[5]) |
| 17 | if mode not in ('graph','forced','reference'):raise SystemExit('mode must be graph, forced or reference') |
| 18 | fixture=json.loads(fixture_path.read_bytes()) |
| 19 | cap=next((int(a.split('=',1)[1]) for a in sys.argv[6:] if a.startswith('--capacity=')),fixture.get('capacity')) |
| 20 | P,G=fixture['prompt_ids'],fixture['generated_ids'] |
| 21 | os.environ['AMDSERVE_WEIGHTS']=str(weights);os.environ['AMDSERVE_OUTPUT']=str(out)+'.log' |
| 22 | spec=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. |
| 24 | drv.GOLD=dict(drv.GOLD,prompt_ids=list(P),generated_ids=list(G),top5=[[(0,0.0)]]*len(G)) |
| 25 | drv.top5_difference=lambda logits,expected:None |
| 26 | if 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) |
| 29 | import torch |
| 30 | def first_div(ids):return next((i for i,(a,b) in enumerate(zip(ids,G)) if a!=b),None) |
| 31 | def 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)) |
| 35 | drv.prepare() |
| 36 | if 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')) |
| 39 | elif 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']) |
| 42 | else: |
| 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)) |
| 56 | res.update(fixture_sha256=__import__('hashlib').sha256(fixture_path.read_bytes()).hexdigest(),driver=str(driver)) |
| 57 | out.write_text(json.dumps(res)+'\n') |
| 58 | print('ORACLE',mode,json.dumps({k:v for k,v in res.items() if k not in ('ids','top5','rows')})) |