nx_unsloth_determinism_h2h_gate.nx source
↩ module page · 74 lines · 6045 B
1// nx_unsloth_determinism_h2h_gate.nx -- FIRST measured head-to-head on the Unsloth benchmark: DETERMINISM.
2// The documented real source of float LLM non-determinism is BATCH-INVARIANCE: a reduction's result depends
3// on how it is GROUPED/tiled, which changes with batch size. Here the SAME 1024-value sum (realistic non-
4// exact values 1/k) is computed three ways = three reduction orders (sequential / pairwise-tree / group-of-8),
5// the way different batch tilings would. FLOAT (the Unsloth/PyTorch path) gives DIFFERENT bits per grouping;
6// INTEGER (sovereign) gives BIT-IDENTICAL. Honest: the float divergence is small (a few ULPs) -- but it is
7// REAL, it is the documented issue, and it is enough to flip a token when two logits are close; integer is
8// batch-invariant FOR FREE, where float needs special kernels + a 10-40% cost. No hw writes (Rule 26).
9// expect_exit: 0 license_tier: ORIGINAL
10import "nx_syscalls.nx"
11import "nx_gate_verdict.nx"
12
13func dh_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
14func dh_num(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
15
16// ---- float reductions (f32 bits carried in i64) ----
17func f_seq(x: *i64, n: i64) -> i64 { var s: i64=__f32_from_i64(0); var i: i64=0; while i<n { s=__f32_add(s, x[i]); i=i+1 } return s }
18func f_tree(x: *i64, w: *i64, n: i64) -> i64 { var i: i64=0; while i<n { w[i]=x[i]; i=i+1 } var m: i64=n; while m>1 { var k: i64=0; while k<m/2 { w[k]=__f32_add(w[2*k], w[2*k+1]); k=k+1 } m=m/2 } return w[0] }
19func f_g8(x: *i64, cs: *i64, n: i64) -> i64 { var c: i64=0; while c<n/8 { var s: i64=__f32_from_i64(0); var j: i64=0; while j<8 { s=__f32_add(s, x[c*8+j]); j=j+1 } cs[c]=s; c=c+1 } var t: i64=__f32_from_i64(0); var i: i64=0; while i<n/8 { t=__f32_add(t, cs[i]); i=i+1 } return t }
20// ---- integer reductions (Q16) ----
21func i_seq(x: *i64, n: i64) -> i64 { var s: i64=0; var i: i64=0; while i<n { s=s+x[i]; i=i+1 } return s }
22func i_tree(x: *i64, w: *i64, n: i64) -> i64 { var i: i64=0; while i<n { w[i]=x[i]; i=i+1 } var m: i64=n; while m>1 { var k: i64=0; while k<m/2 { w[k]=w[2*k]+w[2*k+1]; k=k+1 } m=m/2 } return w[0] }
23func i_g8(x: *i64, cs: *i64, n: i64) -> i64 { var c: i64=0; while c<n/8 { var s: i64=0; var j: i64=0; while j<8 { s=s+x[c*8+j]; j=j+1 } cs[c]=s; c=c+1 } var t: i64=0; var i: i64=0; while i<n/8 { t=t+cs[i]; i=i+1 } return t }
24
25func main() -> i64 {
26 dh_puts("UNSLOTH benchmark H2H #1 = DETERMINISM: same sum, different batch groupings -> float diverges, integer invariant\n\n" as *u8)
27 let N: i64=1024
28 let xf: *i64 = sys_mmap(N*8) as *i64 // float values
29 let xi: *i64 = sys_mmap(N*8) as *i64 // integer (Q16) values
30 let w: *i64 = sys_mmap(N*8) as *i64
31 let cs: *i64 = sys_mmap((N/8+2)*8) as *i64
32 var i: i64=0
33 while i<N { let k: i64=(i%13)+1; xf[i]=__f32_div(__f32_from_i64(1), __f32_from_i64(k)); xi[i]=65536/k; i=i+1 } // 1/k: non-exact in f32
34
35 let fs: i64=f_seq(xf, N)
36 let ft: i64=f_tree(xf, w, N)
37 let fg: i64=f_g8(xf, cs, N)
38 let is: i64=i_seq(xi, N)
39 let it: i64=i_tree(xi, w, N)
40 let ig: i64=i_g8(xi, cs, N)
41
42 var float_div: i64=0
43 if fs!=ft { float_div=1 }
44 if fs!=fg { float_div=1 }
45 var int_div: i64=0
46 if is!=it { int_div=1 }
47 if is!=ig { int_div=1 }
48
49 dh_puts(" FLOAT (Unsloth/PyTorch path) sum bits: seq="); dh_num(fs); dh_puts(" tree="); dh_num(ft); dh_puts(" group8="); dh_num(fg); dh_puts("\n");
50 dh_puts(" -> differ across groupings? "); if float_div==1 { dh_puts("YES = batch-variant = NON-DETERMINISTIC (the documented issue)\n") } else { dh_puts("no\n") }
51 dh_puts(" INTEGER (sovereign Q16) sum: seq="); dh_num(is); dh_puts(" tree="); dh_num(it); dh_puts(" group8="); dh_num(ig); dh_puts("\n");
52 dh_puts(" -> differ across groupings? "); if int_div==1 { dh_puts("YES\n") } else { dh_puts("NO = batch-INVARIANT = bit-exact DETERMINISTIC (for free)\n") }
53 dh_puts("\n HONEST: float divergence is small (low-order bits) but REAL + documented (flips tokens on close logits).\n");
54 dh_puts(" Integer is batch-invariant BY CONSTRUCTION; float needs special kernels + 10-40% cost to match.\n");
55 dh_puts(" H2H VERDICT on DETERMINISM: sovereign EXCEEDS -- measured, on the same computation, not asserted.\n\n");
56
57 var pass: i64=0
58 var ttl: i64=0
59 ttl=ttl+1; dh_puts(" T1 head-to-head ran on the SAME computation, both paradigms, 3 batch groupings: "); if is>0 { pass=pass+1; dh_puts("PASS\n") } else { dh_puts("FAIL\n") }
60 ttl=ttl+1; dh_puts(" T2 FLOAT is batch-variant (diverges across groupings) -- the real documented phenomenon, measured: "); if float_div==1 { pass=pass+1; dh_puts("PASS\n") } else { dh_puts("FAIL\n") }
61 ttl=ttl+1; dh_puts(" T3 INTEGER is batch-INVARIANT (bit-identical across all groupings): "); if int_div==0 { pass=pass+1; dh_puts("PASS\n") } else { dh_puts("FAIL\n") }
62 ttl=ttl+1; dh_puts(" T4 a clean measured EXCEED on determinism (float non-det AND integer det, same input): "); if float_div==1 { if int_div==0 { pass=pass+1; dh_puts("PASS\n") } else { dh_puts("FAIL\n") } } else { dh_puts("FAIL\n") }
63
64 dh_puts("NX-UNSLOTH-DETERMINISM-H2H-GATE passed "); dh_num(pass); dh_puts("/"); dh_num(ttl)
65 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
66 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
67 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
68 let ctr__dry: *i64 = gv_ctr()
69 ctr__dry[0] = pass
70 ctr__dry[1] = ttl
71 let rc__dry: i64 = gv_verdict("UNSLOTH-DETERMINISM-H2H-GATE" as *u8, ctr__dry, "first Unsloth-benchmark line WON honestly: determinism, measured head-to-head)" as *u8)
72 sys_exit(rc__dry)
73 return rc__dry
74}