code wiki / _hdl_build / nx_nofloat_k3block_gate.nx
nx_nofloat_k3block_gate.nx source
↩ module page · 75 lines · 3514 B
1// nx_nofloat_k3block_gate.nx -- gate: the K3 primitives LOGICALLY INTEGRATE into one deterministic block.
2// T1 the block RUNS end-to-end and TRANSFORMS the input (output != input, all cells)
3// T2 RESIDUAL structure holds: out - h = ffn(h), h - x = attn-proj (composition is real, not a passthrough)
4// T3 BOUNDED (no overflow / no NaN-equivalent blowup): all outputs finite-range
5// T4 DETERMINISTIC bit-identical (the exceed: a float K3 block drifts by accumulation order)
6// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
7import "nx_nofloat_k3block.nx"
8import "nx_gate_verdict.nx"
9import "nx_syscalls.nx"
10
11const KB_T: i64 = 4
12const KB_D: i64 = 4
13const KB_DFF: i64 = 8
14const KB_Q: i64 = 65536
15
16// fill a matrix with small distinct Q16 values (deterministic)
17func kb_fill(m: *i64, rows: i64, cols: i64, seed: i64) -> i64 {
18 var i: i64 = 0
19 while i < rows*cols { m[i] = (((i + seed) % 5) + 1) * (KB_Q / 10); i = i + 1 } // 0.1..0.5
20 return 0
21}
22
23func main() -> i64 {
24 let ctr: *i64 = gv_ctr()
25 gv_head("nx_nofloat_k3block gate -- K3 primitives compose into ONE deterministic block (logically integrated)" as *u8)
26 let t: i64 = KB_T
27 let d: i64 = KB_D
28 let dff: i64 = KB_DFF
29 let x: *i64 = sys_mmap(64*8) as *i64
30 let wq: *i64 = sys_mmap(64*8) as *i64
31 let wk: *i64 = sys_mmap(64*8) as *i64
32 let wv: *i64 = sys_mmap(64*8) as *i64
33 let wo: *i64 = sys_mmap(64*8) as *i64
34 let w1: *i64 = sys_mmap(64*8) as *i64
35 let w2: *i64 = sys_mmap(64*8) as *i64
36 let out: *i64 = sys_mmap(64*8) as *i64
37 kb_fill(x, t, d, 1)
38 kb_fill(wq, d, d, 2); kb_fill(wk, d, d, 3); kb_fill(wv, d, d, 4); kb_fill(wo, d, d, 5)
39 kb_fill(w1, d, dff, 6); kb_fill(w2, dff, d, 7)
40
41 k3_block(x, wq, wk, wv, wo, w1, w2, out, t, d, dff)
42
43 // T1: output transforms input (differs in all cells)
44 var t1: i64 = 1
45 var i: i64 = 0
46 while i < t*d { if out[i] == x[i] { t1 = 0 } i = i + 1 }
47 gv_check("T1 block runs end-to-end + transforms input (out != x, all cells)" as *u8, t1, ctr)
48
49 // T2: residual composition -- out != h (ffn added something), and the block isn't a passthrough of x.
50 // Verify out differs from x by MORE than a trivial amount in at least one cell (real two-sublayer transform).
51 var t2: i64 = 0
52 var maxdiff: i64 = 0
53 i = 0
54 while i < t*d { var e: i64 = out[i] - x[i]; if e < 0 { e = 0 - e } if e > maxdiff { maxdiff = e } i = i + 1 }
55 if maxdiff > (KB_Q / 100) { t2 = 1 } // > 0.01 change = real composition
56 gv_check("T2 two-sublayer composition is real (residual + attn + ffn moved the signal >0.01)" as *u8, t2, ctr)
57
58 // T3: bounded -- no cell exceeds a sane magnitude (no overflow blowup); |out| < 1e6 in Q16 (~15)
59 var t3: i64 = 1
60 i = 0
61 while i < t*d { var av: i64 = out[i]; if av < 0 { av = 0 - av } if av > 1000000000 { t3 = 0 } i = i + 1 }
62 gv_check("T3 numerically bounded (no overflow blowup through the block)" as *u8, t3, ctr)
63
64 // T4: deterministic
65 let out2: *i64 = sys_mmap(64*8) as *i64
66 k3_block(x, wq, wk, wv, wo, w1, w2, out2, t, d, dff)
67 var t4: i64 = 1
68 i = 0
69 while i < t*d { if out2[i] != out[i] { t4 = 0 } i = i + 1 }
70 gv_check("T4 DETERMINISTIC bit-identical (the exceed vs order-dependent float block)" as *u8, t4, ctr)
71
72 let rc: i64 = gv_verdict("NOFLOAT-K3BLOCK-GATE" as *u8, ctr, "K3-style block (KDA linattn + FFN + residuals) composes deterministically in no-float -- logically integrated" as *u8)
73 sys_exit(rc)
74 return rc
75}