code wiki / _hdl_build / nx_nofloat_muon_ns_gate.nx
nx_nofloat_muon_ns_gate.nx source
↩ module page · 83 lines · 3411 B
1// nx_nofloat_muon_ns_gate.nx -- gate for integer Newton-Schulz orthogonalization (Muon core).
2// HONEST claim (Muon NS = APPROXIMATE orthogonalization, SVs -> ~1, not exactly 1):
3// T1 a non-orthogonal matrix is orthogonalized -- X X^T -> I error drops hard (>5x) after NS
4// T2 MONOTONE: more iterations reduce the error (err@5 < err@1) -- the iteration converges, not diverges
5// T3 the result's X X^T diagonal lands near 1.0 (each in [0.6,1.4]*Q16) -- Muon-style approx-orthogonal
6// T4 DETERMINISTIC: two runs bit-identical -- the NOVEL EXCEED (float NS is order-dependent, ours is not)
7// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
8import "nx_nofloat_muon_ns.nx"
9import "nx_gate_verdict.nx"
10import "nx_syscalls.nx"
11
12const MG_N: i64 = 4
13const MG_Q: i64 = 65536
14
15func mg_set_test(g: *i64, n: i64) -> i64 {
16 var i: i64 = 0
17 while i < n*n { g[i] = 0; i = i + 1 }
18 // symmetric tridiagonal [[2,1,0,0],[1,2,1,0],[0,1,2,1],[0,0,1,2]] * Q (clearly non-orthogonal)
19 g[0]=2*MG_Q; g[1]=1*MG_Q; g[4]=1*MG_Q; g[5]=2*MG_Q; g[6]=1*MG_Q; g[9]=1*MG_Q; g[10]=2*MG_Q; g[11]=1*MG_Q; g[14]=1*MG_Q; g[15]=2*MG_Q
20 return 0
21}
22
23func main() -> i64 {
24 let ctr: *i64 = gv_ctr()
25 gv_head("nx_nofloat_muon_ns gate -- integer Newton-Schulz orthogonalization (Muon core, novel exceed)" as *u8)
26 let n: i64 = MG_N
27 let g: *i64 = sys_mmap(64*8) as *i64
28 let x: *i64 = sys_mmap(64*8) as *i64
29
30 mg_set_test(g, n)
31 let err_before: i64 = mn_ortho_err(g, n)
32 mn_orthogonalize_it(g, x, n, 5)
33 let err5: i64 = mn_ortho_err(x, n)
34
35 // T1: error drops >5x
36 var t1: i64 = 0
37 if err5 * 5 < err_before { t1 = 1 }
38 gv_check("T1 non-orthogonal matrix orthogonalized (X X^T->I error drops >5x)" as *u8, t1, ctr)
39
40 // T2: monotone -- err@5 < err@1 (iteration converges)
41 let x1: *i64 = sys_mmap(64*8) as *i64
42 mg_set_test(g, n)
43 mn_orthogonalize_it(g, x1, n, 1)
44 let err1: i64 = mn_ortho_err(x1, n)
45 var t2: i64 = 0
46 if err5 < err1 { t2 = 1 }
47 gv_check("T2 MONOTONE convergence (err@5-iters < err@1-iter, not diverging)" as *u8, t2, ctr)
48
49 // T3: X X^T diagonal near 1.0 (each in [0.6,1.4]*Q16) -- Muon-style approximate orthogonality
50 let xt: *i64 = sys_mmap(64*8) as *i64
51 let gg: *i64 = sys_mmap(64*8) as *i64
52 mn_transpose(x, xt, n)
53 mn_mm(x, xt, gg, n)
54 var t3: i64 = 1
55 var i: i64 = 0
56 let lo: i64 = (MG_Q * 6) / 10
57 let hi: i64 = (MG_Q * 14) / 10
58 while i < n {
59 let dv: i64 = gg[i*n+i]
60 if dv < lo { t3 = 0 }
61 if dv > hi { t3 = 0 }
62 i = i + 1
63 }
64 gv_check("T3 X X^T diagonal near 1.0 (each in [0.6,1.4] Q16) = approx-orthogonal" as *u8, t3, ctr)
65
66 // T4: deterministic
67 let xa: *i64 = sys_mmap(64*8) as *i64
68 let xb: *i64 = sys_mmap(64*8) as *i64
69 mg_set_test(g, n)
70 mn_orthogonalize_it(g, xa, n, 5)
71 mg_set_test(g, n)
72 mn_orthogonalize_it(g, xb, n, 5)
73 var t4: i64 = 0
74 var d: i64 = 0
75 var k: i64 = 0
76 while k < n*n { if xa[k] != xb[k] { d = 1 } k = k + 1 }
77 if d == 0 { t4 = 1 }
78 gv_check("T4 DETERMINISTIC bit-identical (the exceed: float NS is order-dependent, ours is not)" as *u8, t4, ctr)
79
80 let rc: i64 = gv_verdict("NOFLOAT-MUON-NS-GATE" as *u8, ctr, "integer Newton-Schulz orthogonalization (Muon core): directional, monotone-convergent, approx-orthogonal, bit-exact deterministic" as *u8)
81 sys_exit(rc)
82 return rc
83}