24 lines
2.3 KiB
Python
24 lines
2.3 KiB
Python
"""Prove reset independence, CPU/GPU parity and packet classifier semantics."""
|
|
from pathlib import Path
|
|
import numpy as np,subprocess,json,time,csv
|
|
R=Path.home()/'run/pets-knp';O=R/'checks';O.mkdir(exist_ok=False)
|
|
B=Path.home()/'KNP/build/bin'
|
|
def detector(images,tag,backend):
|
|
inp=O/(tag+'.bin');images.astype(np.uint8).tofile(inp);pre=O/tag
|
|
start=time.monotonic()
|
|
with (O/(tag+'.log')).open('w') as log:subprocess.run([str(R/'pets_infer'),str(inp),'0',str(len(images)),str(R/'detectors'),str(B/f'libknp-{backend}-backend.so'),str(pre)],check=True,stdout=log,stderr=subprocess.STDOUT)
|
|
return np.fromfile(str(pre)+'_counts.bin',np.int32).reshape(-1,800),time.monotonic()-start
|
|
x=np.load(R/'data/val.npz')['rgb']; seq=np.stack([x[0],x[1],x[1],np.full_like(x[0],255),x[1],np.zeros_like(x[0]),x[1],x[0]])
|
|
a,ga=detector(seq,'sequence-gpu','altai-cuda');b,cb=detector(seq,'sequence-cpu','cpu-single-threaded');assert np.array_equal(a,b)
|
|
fresh,ff=detector(x[1:2],'fresh-b','altai-cuda');assert all(np.array_equal(a[i],fresh[0]) for i in (1,2,4,6));assert np.array_equal(a[0],a[7]);assert np.count_nonzero(a[5])==0
|
|
res={'detector_cpu_gpu_exact':True,'fresh_B_equals_A_B_B_white_B_black_B':True,'black_spikes':0,'sequence_gpu_wall_seconds':ga,'sequence_cpu_wall_seconds':cb,'fresh_gpu_wall_seconds':ff}
|
|
# Dedicated fixtures exercise boundary decisions, duplicates and unbiased integer semantics.
|
|
F=O/'fixture';F.mkdir();w=np.zeros(800,np.int32);w[:3]=[1,-1,2];w.tofile(F/'weights.bin');np.array([0],np.int32).tofile(F/'bias.bin')
|
|
xq=np.zeros((8,800),np.int32);xq[:,:3]=[[0,0,0],[1,0,0],[0,1,0],[640,640,0],[640,0,0],[0,640,0],[1,3,1],[2,1,0]];xq.tofile(F/'counts.bin');expected=(xq.astype(np.int64)@w>0).astype(int)
|
|
for mode in ('cpu','cuda'):
|
|
p=F/(mode+'.csv')
|
|
with (F/(mode+'.log')).open('w') as log:subprocess.run([str(R/'pets_readout'),str(F/'counts.bin'),str(F),mode,str(p)],check=True,stdout=log,stderr=subprocess.STDOUT)
|
|
out=np.genfromtxt(p,delimiter=',',names=True,dtype=int);assert np.array_equal(out['prediction'],expected);assert np.array_equal(out['cat'],(xq@w<0).astype(int));assert np.array_equal(out['dog'],(xq@w>0).astype(int))
|
|
assert (F/'cpu.csv').read_bytes()==(F/'cuda.csv').read_bytes()
|
|
res['readout_zero_plusminus_one_and_maxmultiplicity_exact']=True
|
|
(O/'VERIFIED.json').write_text(json.dumps(res,indent=2)+'\n');print(json.dumps(res))
|