code wiki / _hdl_build / nx_kfd_gate.nx
nx_kfd_gate.nx source
↩ module page · 49 lines · 3470 B
1// nx_kfd_gate.nx -- gate for the native-Linux AMD executor (nx_kfd). Verifies the LOCALLY-verifiable core: a
2// composed minimal PM4 COMPUTE DISPATCH stream (SET_SH_REG + DISPATCH_DIRECT) has the documented packet headers
3// + structure, and reports the device-iface (on WSL2 /dev/kfd ABSENT -> EXECUTE cloud-pending on an AMD instance).
4// No hw writes (Rule 26). expect_exit: 0 license_tier: ORIGINAL
5import "nx_syscalls.nx"
6import "nx_kfd.nx"
7import "nx_pm4_asm.nx"
8
9func bp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10func bx(v: i64) -> i64 { bp("0x" as *u8); let bb:*u8=sys_mmap(20); var k:i64=0; var m:i64=v; if m==0{bb[0]=48 as u8;k=1} while m>0{ let d:i64=m&15; if d<10{bb[k]=(48+d) as u8}else{bb[k]=(87+d) as u8}; m=(m>>4); k=k+1 } var i:i64=0; let o:*u8=sys_mmap(20); while i<k{o[i]=bb[k-1-i];i=i+1} sys_write(1,o,k); return 0 }
11func bd(v: i64) -> i64 { let p: *u8=sys_mmap(1); p[0]=(48+v) as u8; sys_write(1,p,1); return 0 }
12func rget(ring: *u8, off: i64) -> i64 { return (ring[off] as i64)|((ring[off+1] as i64)<<8)|((ring[off+2] as i64)<<16)|((ring[off+3] as i64)<<24) }
13
14func chk(name: *u8, got: i64, ref: i64, st: *i64) -> i64 {
15 bp(" " as *u8); bp(name); bp(" -> " as *u8); bx(got)
16 if got==ref { bp(" == ref PASS\n" as *u8); st[0]=st[0]+1; return 0 }
17 bp(" != ref " as *u8); bx(ref); bp(" FAIL\n" as *u8); return 0
18}
19
20func main() -> i64 {
21 bp("=== native-Linux AMD executor (nx_kfd) -- PM4 dispatch composition + device-iface routing ===\n" as *u8)
22
23 let kfd: i64 = kfd_open()
24 var have_kfd: i64 = 0
25 if kfd >= 0 { have_kfd = 1; sys_close(kfd) }
26 bp(" /dev/kfd present=" as *u8); bd(have_kfd)
27 if have_kfd == 1 { bp(" -> native-Linux AMD: AM/PM4 EXECUTE runnable HERE\n" as *u8) } else { bp(" -> WSL2/no-AMD: EXECUTE cloud-pending (rent a native-Linux AMD instance)\n" as *u8) }
28
29 // compose a minimal PM4 compute dispatch (reg_base=0x2c00 COMPUTE, 4 regs, dims 256x1x1) + verify its structure
30 let ring: *u8 = sys_mmap(256)
31 let len: i64 = pm4_compose_dispatch(ring, 0x2c00, 4, 256, 1, 1)
32 let st: *i64 = sys_mmap(8); st[0]=0
33 chk("SET_SH_REG header @0 " as *u8, rget(ring, 0), 0xc0057600, st)
34 chk("reg_base @4 " as *u8, rget(ring, 4), 0x2c00, st)
35 chk("DISPATCH header @24 " as *u8, rget(ring, 24), 0xc0031500, st)
36 chk("dim_x @28 " as *u8, rget(ring, 28), 256, st)
37 chk("dispatch_initiator @40 " as *u8, rget(ring, 40), 1, st)
38 chk("stream byte length " as *u8, len, 44, st)
39
40 // negative control: the two packet headers must be DISTINCT (the stream has real structure)
41 var neg_ok: i64 = 0
42 if rget(ring, 0) != rget(ring, 24) { neg_ok = 1 }
43 bp(" NEG SET_SH_REG header != DISPATCH header: " as *u8); if neg_ok==1 { bp("PASS\n" as *u8) } else { bp("FAIL\n" as *u8) }
44
45 let pass: i64 = st[0]
46 bp("AMD-KFD-EXECUTOR-GATE dispatch_compose " as *u8); bd(pass); bp("/6 neg=" as *u8); bd(neg_ok); bp(" execute=" as *u8); if have_kfd==1 { bp("native-runnable" as *u8) } else { bp("CLOUD-PENDING" as *u8) }; bp("\n" as *u8)
47 if pass==6 { if neg_ok==1 { bp("verdict=GREEN (sovereign AMD PM4 compute-dispatch composed bit-correct [documented format, tinygrad AM-driver path]; KFD queue + ring + doorbell EXECUTE = cloud-pending on native-Linux AMD silicon)\n" as *u8); sys_exit(0); return 0 } }
48 bp("verdict=RED\n" as *u8); sys_exit(1); return 1
49}