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 / tests / amd-exec / replay_bundle.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """Outside verification/transport instrument; never a model runtime dependency.""" |
| 4 | import hashlib |
| 5 | import json |
| 6 | import pathlib |
| 7 | import runpy |
| 8 | import shutil |
| 9 | import subprocess |
| 10 | import sys |
| 11 | |
| 12 | ROOT=pathlib.Path(__file__).resolve().parents[2] |
| 13 | SRC=ROOT/'backend/amd/gfx942' |
| 14 | |
| 15 | def sha(path): return hashlib.sha256(path.read_bytes()).hexdigest() |
| 16 | |
| 17 | def bundle(evidence,dest,golden,config): |
| 18 | tokens=json.loads(golden.read_text()) |
| 19 | capacity=len(tokens['prompt_ids'])+len(tokens['generated_ids']) |
| 20 | model=json.loads(config.read_text()) |
| 21 | vocab=model.get('text_config',model)['vocab_size'] |
| 22 | report=json.loads((evidence/'report.json').read_text()) |
| 23 | assert report['result']=='PASS' and report['hardware']=='NOT_RUN' |
| 24 | assert capacity>0 and vocab>0 |
| 25 | for name,pin in report['source_sha256'].items(): |
| 26 | assert sha(ROOT/name)==pin, 'source changed after gate: '+name |
| 27 | controls=runpy.run_path(str(ROOT/'tests/amd-exec/replay.py'))['MUTANTS'] |
| 28 | assert not dest.exists(),'refusing to overwrite an evidence bundle' |
| 29 | head=subprocess.check_output(['git','rev-parse','HEAD'],cwd=ROOT,text=True).strip() |
| 30 | # Model-shape constants belong to these specialized kernels. Capacity/vocabulary are caller inputs. |
| 31 | cache=4*capacity*256*2 |
| 32 | tiles=(capacity+63)//64 |
| 33 | state=48*128*128*4 |
| 34 | definitions={ |
| 35 | 'embed5120_step': ([80,1,1],[vocab*5120*2,5120*4,4]), |
| 36 | 'attn256_prep_step': ([24,1,1],[24*512*4,4*256*4,4*256*4,256*4,256*4,capacity*64*4,24*256*4,cache,cache,4]), |
| 37 | 'attn256_part_step': ([tiles,24,1],[24*256*4,cache,cache,24*tiles*1040,24*256*4,4]), |
| 38 | 'attn256_merge_step': ([1,24,1],[24*256*4,cache,cache,24*tiles*1040,24*256*4,4]), |
| 39 | 'gdnconv4_step': ([160,1,1],[10240*4,10240*8,10240*8,10240*8,10240*4,4]), |
| 40 | 'gdnstate_step': ([3,16,1],[2*16*128*4,48*128*4,state,state,48*128*4,48*4,48*4,4]), |
| 41 | 'step_advance': ([1,1,1],[4]), |
| 42 | 'readfirst_probe': ([1,1,1],[64*4,4]), |
| 43 | } |
| 44 | manifest=dict(source_head=head,hardware='NOT_RUN',capacity=capacity,vocab=vocab, |
| 45 | compiler_sha256=report['compiler_sha256'],evidence=dict(path=str(evidence/'report.json'),sha256=sha(evidence/'report.json')), |
| 46 | input_pins={str(p):sha(p) for p in (golden,config)}, |
| 47 | kernels=[],contract=dict(state_banks='even position A->B; odd B->A; start at position0', |
| 48 | workspace='capacity-sized allocation, active ceil((position+1)/64)*1040 head stride', |
| 49 | increment='exactly once after every layer and argmax; replay count must not exceed capacity', |
| 50 | position='u32 device counter shared by all step kernels',token='u32 device output of argmax_final', |
| 51 | rope='capacity rows of 32 cos + 32 sin FP32 words; one256-byte row per position')) |
| 52 | dest.mkdir(parents=True) |
| 53 | for entry in report['objects']: |
| 54 | name=entry['name']; grid,sizes=definitions[name] |
| 55 | args=[]; i=0 |
| 56 | for arg in entry['args']: |
| 57 | arg=dict(arg) |
| 58 | if arg['kind']=='ptr': arg['buffer_bytes']=sizes[i]; i+=1 |
| 59 | elif arg['name'] in ('cap','capacity'): arg['value']=capacity |
| 60 | elif arg['name'] in ('vocab','vocabulary'): arg['value']=vocab |
| 61 | args.append(arg) |
| 62 | assert i==len(sizes),name |
| 63 | names=[name]+[n for n,(base,_,_) in controls.items() if base==name] |
| 64 | objects=[] |
| 65 | for variant in names: |
| 66 | origin=evidence/(variant+'.hsaco') |
| 67 | if variant==name: assert sha(origin)==entry['sha256'] |
| 68 | shutil.copyfile(origin,dest/origin.name) |
| 69 | objects.append(dict(file=origin.name,entry=name,sha256=sha(origin),bytes=origin.stat().st_size)) |
| 70 | access=[a for a in report['access'] if a['kernel'] in names] |
| 71 | manifest['kernels'].append(dict(kernel=name,kernarg_bytes=entry['kernarg_bytes'],args=args, |
| 72 | grid=grid,block=entry['block'],objects=objects,measured_access_runs=access, |
| 73 | numerical_comparison='bit-exact original-kernel parity; no new floating arithmetic',hardware='NOT_RUN')) |
| 74 | probe=json.loads((evidence/'readfirst-probe.json').read_text()) |
| 75 | probe_abi=next(k for k in manifest['kernels'] if k['kernel']=='readfirst_probe') |
| 76 | probe['args']=[a['name'] for a in probe_abi['args']] |
| 77 | probe['kernarg_bytes']=probe_abi['kernarg_bytes'] |
| 78 | (dest/'readfirst-probe.json').write_text(json.dumps(probe,indent=2)+'\n') |
| 79 | (dest/'list.json').write_text(json.dumps(manifest,indent=2)+'\n') |
| 80 | receipt=dict(report,source_head=head,list_sha256=sha(dest/'list.json'), |
| 81 | original_receipt=dict(path=str(evidence/'report.json'),sha256=sha(evidence/'report.json'))) |
| 82 | receipt['objects']=[obj for kernel in manifest['kernels'] for obj in kernel['objects']] |
| 83 | (dest/'report.json').write_text(json.dumps(receipt,indent=2)+'\n') |
| 84 | lines=['# Device-stepped kernel objects','',f'Source head: `{head}`', |
| 85 | f'Capacity: {capacity}; vocabulary: {vocab}. Hardware execution: NOT_RUN.', |
| 86 | 'All grids are workgroups; block=(64,1,1). The measured-access runs below are CPU fixtures, not full-model hardware qualification.', |
| 87 | 'Position/token values are read through VMEM. Keep arguments, buffers and the full RoPE table alive across capture/replay.', |
| 88 | 'Start at position0 with fixed A/B state pointers; increment once after argmax. Stop replay before exhausting capacity.', |
| 89 | 'No host pointer swap, scalar argument change or row copy is required during replay.', |
| 90 | '', '| Object | SHA256 | Kernarg bytes | Grid |', '|---|---|---:|---|'] |
| 91 | for kernel in manifest['kernels']: |
| 92 | for obj in kernel['objects']: |
| 93 | lines.append(f'| {obj["file"]} | `{obj["sha256"]}` | {kernel["kernarg_bytes"]} | {kernel["grid"]} |') |
| 94 | for kernel in manifest['kernels']: |
| 95 | lines+=['','## '+kernel['kernel'],'| Arg | Offset | Kind | Buffer bytes |','|---|---:|---|---:|'] |
| 96 | for arg in kernel['args']: |
| 97 | lines.append('| '+' | '.join(str(arg.get(key,'—')) for key in ('name','offset','kind','buffer_bytes'))+' |') |
| 98 | lines+=['','Measured CPU access (each address range is independently checked on every access):', |
| 99 | '```json',json.dumps(kernel['measured_access_runs'],indent=2),'```'] |
| 100 | (dest/'list.md').write_text('\n'.join(lines)+'\n') |
| 101 | (dest/'SHA256SUMS').write_text(''.join(sha(p)+' '+p.name+'\n' for p in sorted(dest.iterdir()) if p.is_file())) |
| 102 | print('BUNDLE',dest,'head='+head,'receipt_sha256='+sha(dest/'report.json')) |
| 103 | |
| 104 | if __name__=='__main__': |
| 105 | bundle(pathlib.Path(sys.argv[1]).resolve(),pathlib.Path(sys.argv[2]).resolve(), |
| 106 | pathlib.Path(sys.argv[3]).resolve(),pathlib.Path(sys.argv[4]).resolve()) |