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 / check-plan.py
| 1 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | """Harness check, independent of emit-plan.py: parse PLAN with FORMAT.md rules only, then |
| 3 | (1) every file-backed buffer is exactly one checkpoint tensor's byte range (from the safetensors headers), |
| 4 | (2) kind 2/4 buffers are 1-D BF16 vectors and their widened values match safetensors' own decoding, |
| 5 | (3) the dispatch table obeys every structural rule L1214 validates, |
| 6 | (4) negative controls: a plan with one corrupted relocation and one with a misaligned weight range are refused, |
| 7 | (5) capacity: the RoPE table, the 32 KV caches, the attention partition grid and every capacity kernel argument |
| 8 | agree with the capacity recorded beside the plan; checking against any other capacity is refused. |
| 9 | Usage: check-plan.py PLAN WEIGHTS |
| 10 | """ |
| 11 | import hashlib,json,struct,sys |
| 12 | from pathlib import Path |
| 13 | import torch |
| 14 | from safetensors import safe_open |
| 15 | plan_path,weights=Path(sys.argv[1]),Path(sys.argv[2]);summary=json.loads(Path(str(plan_path)+'.json').read_text()) |
| 16 | ranges={} |
| 17 | for f in summary['files']: |
| 18 | with (weights/f).open('rb') as h: |
| 19 | n=struct.unpack('<Q',h.read(8))[0];hd=json.loads(h.read(n)) |
| 20 | for name,m in hd.items(): |
| 21 | if name!='__metadata__':ranges[(f,8+n+m['data_offsets'][0],m['data_offsets'][1]-m['data_offsets'][0])]=(name,m['dtype'],m['shape']) |
| 22 | def check(p,deep): |
| 23 | assert p[:8]==b'OMPLAN01';nc,nf,nb,nd,npm,ng,tb=struct.unpack_from('<7Q',p,8);assert npm>=1 and ng>=1 and nf==len(summary['files']) |
| 24 | sp,sl,dp,dl=struct.unpack_from('<4Q',p,len(p)-32);assert dp==sp+sl and dp+dl==len(p)-32 |
| 25 | q=64;codes=[] |
| 26 | for i in range(nc): |
| 27 | o,l=struct.unpack_from('<2Q',p,q);q+=16;assert o+l<=sl;codes.append(p[sp+o:sp+o+l].decode()) |
| 28 | bufs=[struct.unpack_from('<6Q',p,q+48*i) for i in range(nb)];q+=48*nb |
| 29 | names=[] |
| 30 | for size,kind,fi,off,length,flags in bufs: |
| 31 | assert flags in (1,2) |
| 32 | if kind==0:assert fi==off==length==0;names.append(None) |
| 33 | elif kind==3:assert fi==0 and off+length<=dl and size==length;names.append(None) |
| 34 | else: |
| 35 | assert kind in (1,2) and fi<nf |
| 36 | key=(summary['files'][fi],off,length);assert key in ranges,('not a tensor range',key) |
| 37 | name,dtype,shape=ranges[key];names.append((fi,name)) |
| 38 | if kind==1:assert size==length |
| 39 | else:assert dtype=='BF16' and len(shape)==1 and length%2==0 and size==2*length |
| 40 | assert bufs[tb][5]==2 and bufs[tb][0]>=4 |
| 41 | q+=4*npm;q+=(-q)%8 |
| 42 | for i in range(nd): |
| 43 | ci,gx,gy,gz,bx,kb,nr,pad=struct.unpack_from('<8I',p,q);q+=32 |
| 44 | assert ci<nc and gx>=1 and gy>=1 and gz==1 and bx==64 and pad==0 |
| 45 | blob=p[q:q+kb];q+=kb;slots=set() |
| 46 | for j in range(nr): |
| 47 | off,bi,bo=struct.unpack_from('<IIQ',p,q);q+=16 |
| 48 | assert off%8==0 and off+8<=kb and blob[off:off+8]==bytes(8) and off not in slots and bi<nb and bo<bufs[bi][0];slots.add(off) |
| 49 | q+=(-q)%8 |
| 50 | assert q==sp,(q,sp) |
| 51 | if deep: |
| 52 | widened=0 |
| 53 | for (size,kind,fi,off,length,flags),nm in zip(bufs,names): |
| 54 | if kind!=2:continue |
| 55 | fi_,name=nm |
| 56 | with safe_open(str(weights/summary['files'][fi_]),framework='pt') as f:t=f.get_tensor(name).float() |
| 57 | with (weights/summary['files'][fi_]).open('rb') as h:h.seek(off);raw=h.read(length) |
| 58 | assert torch.equal(torch.frombuffer(bytearray(raw),dtype=torch.bfloat16).float(),t);widened+=1 |
| 59 | # CONTROLS vectors (kind 3) must be exactly the checkpoint vector widened minus 1, decoded by safetensors itself. |
| 60 | mutated=0 |
| 61 | for bi,file,foff,flen in summary['control_vectors']: |
| 62 | size,kind,fi,off,length,flags=bufs[bi];assert kind==3 |
| 63 | name,dtype,shape=ranges[(file,foff,flen)] |
| 64 | with safe_open(str(weights/file),framework='pt') as f:t=f.get_tensor(name).float()-1 |
| 65 | assert torch.equal(torch.frombuffer(bytearray(p[dp+off:dp+off+length]),dtype=torch.float32),t);mutated+=1 |
| 66 | assert mutated==(161 if summary['controls'] else 0),mutated |
| 67 | return dict(code=len(codes),buffers=nb,file_backed=sum(1 for n in names if n),widened=widened,control_vectors=mutated,dispatches=nd) |
| 68 | def capcheck(p,cap): |
| 69 | # Independent of Card: RoPE recomputed from config.json, sizes from the attention shape (4 KV heads x 256, BF16). |
| 70 | nc,nf,nb,nd,npm,ng,tb=struct.unpack_from('<7Q',p,8);assert cap>=npm+ng |
| 71 | sp,sl,dp,dl=struct.unpack_from('<4Q',p,len(p)-32);q=64;codes=[] |
| 72 | for i in range(nc):o,l=struct.unpack_from('<2Q',p,q);q+=16;codes.append(p[sp+o:sp+o+l].decode()) |
| 73 | bufs=[struct.unpack_from('<6Q',p,q+48*i) for i in range(nb)];q+=48*nb |
| 74 | cfg=json.loads((weights/'config.json').read_text())['text_config'];theta=cfg['rope_parameters']['rope_theta'] |
| 75 | angles=torch.arange(cap,dtype=torch.float64).unsqueeze(1)/torch.pow(torch.tensor(theta,dtype=torch.float64),torch.arange(32,dtype=torch.float64)*2/64) |
| 76 | want=torch.cat((angles.cos(),angles.sin()),dim=1).float().numpy().tobytes() |
| 77 | size,kind,fi,off,length,flags=bufs[summary['rope_buffer']];assert kind==3 and length==len(want) and p[dp+off:dp+off+length]==want,'rope table' |
| 78 | kv=summary['kv_buffers'];assert len(kv)==2*sum(1 for k in cfg['layer_types'] if k=='full_attention')==32,len(kv) |
| 79 | for bi in kv:assert bufs[bi][1]==0 and bufs[bi][0]==4*cap*256*2,('kv size',bi,bufs[bi][0]) |
| 80 | q+=4*npm;q+=(-q)%8;seen={} |
| 81 | at={'attn256_prep_step':80,'attn256_part_step':48,'attn256_merge_step':48,'step_advance':8} |
| 82 | for i in range(nd): |
| 83 | ci,gx,gy,gz,bx,kb,nr,pad=struct.unpack_from('<8I',p,q);q+=32;blob=p[q:q+kb];q+=kb+16*nr;q+=(-q)%8;name=codes[ci] |
| 84 | if name in at: |
| 85 | assert struct.unpack_from('<I',blob,at[name])[0]==cap,(name,'capacity argument');seen[name]=seen.get(name,0)+1 |
| 86 | if name=='attn256_part_step':assert gx==(cap+63)//64,('partition grid',gx) |
| 87 | assert seen=={'attn256_prep_step':16,'attn256_part_step':16,'attn256_merge_step':16,'step_advance':1},seen |
| 88 | return dict(capacity=cap,kv_bytes=sum(bufs[bi][0] for bi in kv),rope_bytes=len(want),partitions=(cap+63)//64) |
| 89 | p=bytes(plan_path.read_bytes());result=check(p,True) |
| 90 | # Negative controls: each must be refused. |
| 91 | q=64+16*struct.unpack_from('<Q',p,8)[0] |
| 92 | r=q+48*next(i for i in range(struct.unpack_from('<Q',p,24)[0]) if struct.unpack_from('<Q',p,q+48*i+8)[0]==1) # first raw (kind 1) buffer |
| 93 | bad=bytearray(p);struct.pack_into('<Q',bad,r+8*3,struct.unpack_from('<Q',p,r+24)[0]+8) # its file_offset moved 8 bytes |
| 94 | caught=[] |
| 95 | for label,b in [('weight range shifted',bytes(bad))]: |
| 96 | try:check(b,False) |
| 97 | except AssertionError:caught.append(label) |
| 98 | nb=struct.unpack_from('<Q',p,24)[0];npm=struct.unpack_from('<Q',p,40)[0];d0=q+48*nb+4*npm;d0+=(-d0)%8 |
| 99 | kb,nr=struct.unpack_from('<2I',p,d0+20);bad=bytearray(p);struct.pack_into('<I',bad,d0+32+kb,4) # first reloc slot misaligned |
| 100 | try:check(bytes(bad),False) |
| 101 | except AssertionError:caught.append('reloc slot misaligned') |
| 102 | assert caught==['weight range shifted','reloc slot misaligned'],caught |
| 103 | cap=summary.get('capacity') |
| 104 | if cap is None:print('NOT_RUN capacity check: sidecar has no capacity record') |
| 105 | else: |
| 106 | result['capacity']=capcheck(p,cap) |
| 107 | for label,other in [('capacity halved',cap//2),('capacity plus 64',cap+64)]: |
| 108 | try:capcheck(p,other) |
| 109 | except AssertionError:caught.append(label) |
| 110 | assert caught[2:]==['capacity halved','capacity plus 64'],caught |
| 111 | fx=summary.get('fixture') |
| 112 | def fixcheck(p,fx): |
| 113 | # The plan header's prompt/generate counts and its prompt-id table equal the fixture file's own bytes, read again here. |
| 114 | raw=Path(fx['path']).read_bytes();assert hashlib.sha256(raw).hexdigest()==fx['sha256'],'fixture file changed' |
| 115 | f=json.loads(raw);nc,nf,nb,nd,npm,ng,tb=struct.unpack_from('<7Q',p,8) |
| 116 | assert npm==len(f['prompt_ids']) and ng==len(f['generated_ids']),('counts',npm,ng) |
| 117 | q=64+16*nc+48*nb;assert list(struct.unpack_from('<%di'%npm,p,q))==f['prompt_ids'],'prompt ids' |
| 118 | assert fx['prompt_ids']==f['prompt_ids'] and fx['generated_ids']==f['generated_ids'],'sidecar ids' |
| 119 | return dict(fixture=f.get('name'),sha256=fx['sha256'],prompt=npm,generate=ng) |
| 120 | if fx is None:print('NOT_RUN fixture check: plan uses the driver fixture') |
| 121 | else: |
| 122 | result['fixture']=fixcheck(p,fx) |
| 123 | bad=bytearray(p);q=64+16*struct.unpack_from('<Q',p,8)[0]+48*struct.unpack_from('<Q',p,24)[0];struct.pack_into('<i',bad,q,struct.unpack_from('<i',p,q)[0]+1) |
| 124 | try:fixcheck(bytes(bad),fx) |
| 125 | except AssertionError:caught.append('prompt id changed') |
| 126 | assert caught[-1]=='prompt id changed',caught |
| 127 | print('PLAN_CHECK PASS',json.dumps(result),'CONTROLS CAUGHT',caught) |