nx_fabric_collective_gate.nx source
↩ module page · 129 lines · 6390 B
1// nx_fabric_collective_gate.nx -- LIVE GATE for R4 (collective communication: ring all-reduce).
2// Proves ring all-reduce is byte-exact correct AND its per-process bottleneck stays ~constant as N grows,
3// where naive root-based all-reduce bottlenecks at O(N) -- with negative controls.
4//
5// criteria:
6// 1 ring all-reduce byte-exact correct (N=8) (the algorithm is REAL, not a cost model)
7// 2 naive all-reduce byte-exact correct (N=8) (baseline sanity)
8// 3 ring bottleneck * 3 <= naive bottleneck (N=8) (>=3x less per-process data; actual ~Nx)
9// 4 ring max_bytes(N=16) <= 2 * ring max_bytes(N=4) (ring per-process BOUNDED as N grows)
10// 5 naive max_bytes(N=16) >= 3 * naive max_bytes(N=4)(naive root grows ~linearly -- the contrast)
11// negative controls:
12// NC1 ring with the REDUCTION disabled -> WRONG result (the sum is load-bearing, not the shuffle)
13// NC2 ring correct at N=4 AND N=16 (correct across scale, not a fixed-N fluke)
14// NC3 ring(N=8) result == naive(N=8) result byte-exact (two independent algorithms agree)
15//
16// expect_exit: 0 license_tier: ORIGINAL
17import "nx_fabric_collective.nx"
18
19func gp(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
20func gn(v: i64) -> i64 {
21 let b: *u8 = sys_mmap(28); var m: i64 = v
22 if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) }
23 let t: *u8 = sys_mmap(28); var k: i64 = 0
24 if m == 0 { t[0] = 48 as u8; k = 1 }
25 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
26 var i: i64 = 0
27 while i < k { b[i] = t[k - 1 - i]; i = i + 1 }
28 sys_write(1, b, k); return 0
29}
30func fdn(fd: i64, v: i64) -> i64 {
31 let b: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m }
32 let t: *u8 = sys_mmap(28); var k: i64 = 0
33 if m == 0 { t[0] = 48 as u8; k = 1 }
34 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
35 var i: i64 = 0
36 while i < k { b[i] = t[k - 1 - i]; i = i + 1 }
37 sys_write(fd, b, k); return 0
38}
39func chk(name: *u8, ok: i64) -> i64 {
40 if ok == 1 { gp(" PASS " as *u8); gp(name); gp("\n" as *u8); return 1 }
41 gp(" FAIL " as *u8); gp(name); gp("\n" as *u8); return 0
42}
43func show(tag: *u8, m: *ColMetrics) -> i64 {
44 gp(tag); gp(": N=" as *u8); gn(m.n); gp(" max_bytes/proc=" as *u8); gn(m.max_bytes)
45 gp(" correct=" as *u8); gn(m.correct); gp("\n" as *u8); return 0
46}
47
48func main() -> i64 {
49 let V: i64 = 64
50
51 let r4: *ColMetrics = sys_mmap(64) as *ColMetrics
52 let nv: *ColMetrics = sys_mmap(64) as *ColMetrics
53 let r4a: *ColMetrics = sys_mmap(64) as *ColMetrics // ring N=4
54 let r4b: *ColMetrics = sys_mmap(64) as *ColMetrics // ring N=16
55 let nva: *ColMetrics = sys_mmap(64) as *ColMetrics // naive N=4
56 let nvb: *ColMetrics = sys_mmap(64) as *ColMetrics // naive N=16
57 let ns: *ColMetrics = sys_mmap(64) as *ColMetrics // ring-no-sum N=8
58
59 let ring_out: *i64 = sys_mmap(8 * V) as *i64
60 let naive_out: *i64 = sys_mmap(8 * V) as *i64
61 let scratch: *i64 = sys_mmap(8 * V) as *i64
62
63 gp("nx_fabric_collective R4 gate -- ring all-reduce vs naive root, V=" as *u8); gn(V); gp(" elements\n" as *u8)
64
65 col_run(COL_RING, 8, V, r4, ring_out)
66 col_run(COL_NAIVE, 8, V, nv, naive_out)
67 col_run(COL_RING, 4, V, r4a, scratch)
68 col_run(COL_RING, 16, V, r4b, scratch)
69 col_run(COL_NAIVE, 4, V, nva, scratch)
70 col_run(COL_NAIVE, 16, V, nvb, scratch)
71 col_run(COL_RING_NOSUM, 8, V, ns, scratch)
72
73 show("RING N=8 " as *u8, r4)
74 show("NAIVE N=8 " as *u8, nv)
75 show("RING N=4 " as *u8, r4a); show("RING N=16" as *u8, r4b)
76 show("NAIVE N=4 " as *u8, nva); show("NAIVE N=16" as *u8, nvb)
77 show("RING-NOSUM N=8 (NC1)" as *u8, ns)
78
79 var pass: i64 = 0
80 var tot: i64 = 0
81
82 pass = pass + chk("1 ring all-reduce byte-exact correct (N=8)" as *u8, r4.correct); tot = tot + 1
83 pass = pass + chk("2 naive all-reduce byte-exact correct (N=8)" as *u8, nv.correct); tot = tot + 1
84
85 var c3: i64 = 0; if r4.max_bytes * 3 <= nv.max_bytes { c3 = 1 }
86 pass = pass + chk("3 ring bottleneck*3 <= naive bottleneck (N=8)" as *u8, c3); tot = tot + 1
87
88 var c4: i64 = 0; if r4b.max_bytes <= 2 * r4a.max_bytes { c4 = 1 }
89 pass = pass + chk("4 ring max_bytes(N=16) <= 2*ring(N=4) (per-proc BOUNDED as N grows)" as *u8, c4); tot = tot + 1
90
91 var c5: i64 = 0; if nvb.max_bytes >= 3 * nva.max_bytes { c5 = 1 }
92 pass = pass + chk("5 naive max_bytes(N=16) >= 3*naive(N=4) (root grows ~linearly)" as *u8, c5); tot = tot + 1
93
94 var cn1: i64 = 0; if ns.correct == 0 { cn1 = 1 }
95 pass = pass + chk("NC1 ring with reduction disabled -> WRONG (the sum is load-bearing)" as *u8, cn1); tot = tot + 1
96
97 var cn2: i64 = 0; if r4a.correct == 1 { if r4b.correct == 1 { cn2 = 1 } }
98 pass = pass + chk("NC2 ring correct at N=4 AND N=16 (correct across scale)" as *u8, cn2); tot = tot + 1
99
100 var cn3: i64 = 0
101 var agree: i64 = 1
102 var i: i64 = 0
103 while i < V { if ring_out[i] != naive_out[i] { agree = 0 } i = i + 1 }
104 if agree == 1 { cn3 = 1 }
105 pass = pass + chk("NC3 ring(N=8) == naive(N=8) result byte-exact (two algorithms agree)" as *u8, cn3); tot = tot + 1
106
107 gp("---- nx_fabric_collective R4 gate: passed " as *u8); gn(pass); gp(" / " as *u8); gn(tot); gp("\n" as *u8)
108
109 if pass == tot {
110 let lfd: i64 = sys_openat_append("knowledge/status/fabric_nx_fabric_collective.log" as *u8, 0x1a4)
111 if lfd >= 0 {
112 sys_write(lfd, "R4-FABRIC-COLLECTIVE organ=nx_fabric_collective checks=" as *u8, 55)
113 fdn(lfd, pass); sys_write(lfd, "/" as *u8, 1); fdn(lfd, tot)
114 sys_write(lfd, " ring-allreduce-byte-exact+bounded-bottleneck+NC1/2/3 verdict=GREEN\n" as *u8, 67)
115 sys_close(lfd)
116 }
117 gp("R4 LIVE GREEN -- ring all-reduce: byte-exact + per-process bottleneck bounded as N scales\n" as *u8)
118 sys_exit(0)
119 }
120 let rfd: i64 = sys_openat_append("knowledge/status/fabric_nx_fabric_collective.log" as *u8, 0x1a4)
121 if rfd >= 0 {
122 sys_write(rfd, "R4-FABRIC-COLLECTIVE organ=nx_fabric_collective checks=" as *u8, 55)
123 fdn(rfd, pass); sys_write(rfd, "/" as *u8, 1); fdn(rfd, tot)
124 sys_write(rfd, " verdict=RED\n" as *u8, 13)
125 sys_close(rfd)
126 }
127 sys_exit(1)
128 return 0
129}