nx_f32_parity.nx source
↩ module page · 70 lines · 3451 B
1// nx_f32_parity.nx -- differential-parity measurement organ: the MEASURED-EXCEED engine for the
2// Z-Image benchmark (operator 2026-06-30 "use z image as the benchmark... until we exceed them").
3//
4// Per [[feedback-no-wave-measured-exceed]] an EXCEED is MEASURED head-to-head, never self-graded. This is
5// the reusable primitive every "our sovereign op vs the sdcpp/ggml oracle" gate composes: feed identical
6// inputs/weights to our f32 organ AND ggml's op, then nx_f32_parity_check(ours, ref, n, tol) renders a
7// PASS/FAIL verdict + the max abs error. Reference values are reference KATs extracted from ggml/sdcpp
8// (the same pattern `nx_q4k_to_f32`'s bit-vs-ggml gate already uses). Parity-first, then exceed.
9//
10// f32 magnitude compare trick: for FINITE values the bit pattern with the sign bit cleared (raw & 0x7FFFFFFF)
11// is monotonic in magnitude, so |a-b| as an f32 magnitude pattern is order-comparable as a plain integer,
12// and clearing the sign of (a-b) yields |a-b| itself as a positive f32 value (returned as max_abs).
13// ours, ref: flat *i64 f32 bits [n]. tol_abs: f32 (the parity tolerance). out_maxabs[0] <- max |error| (f32).
14// license_tier: ORIGINAL
15import "nx_syscalls.nx"
16import "nx_f32.nx"
17import "nx_f32_div.nx"
18import "nx_f32_cvt.nx"
19
20const NX_F32PAR_FAIL: i64 = 0
21const NX_F32PAR_PASS: i64 = 1
22
23// returns 1 (PASS, all |ours-ref| <= tol_abs) or 0 (FAIL); writes the max |error| (f32) to out_maxabs[0].
24func nx_f32_parity_check(ours: *i64, ref: *i64, n: i64, tol_abs: i64, out_maxabs: *i64) -> i64 {
25 var maxmag: i64 = 0 // running max |error| as f32 magnitude bits (+0.0)
26 var i: i64 = 0
27 while i < n {
28 let d: i64 = nx_f32_sub(ours[i], ref[i])
29 let mag: i64 = d & 0x7FFFFFFF // |ours[i] - ref[i]| as a positive-f32 bit pattern
30 if mag > maxmag { maxmag = mag }
31 i = i + 1
32 }
33 out_maxabs[0] = maxmag
34 let tolmag: i64 = tol_abs & 0x7FFFFFFF
35 if maxmag <= tolmag { return NX_F32PAR_PASS }
36 return NX_F32PAR_FAIL
37}
38
39// ===== Self-test (inline gate) ====================================
40// (a) identical -> PASS, max_abs == 0
41// (b) within tolerance (error 0.001 < tol 0.01) -> PASS
42// (c) over tolerance (error 0.5 > tol 0.01) -> FAIL
43func main() -> i64 {
44 let a: *i64 = sys_mmap(8 * 8) as *i64
45 let b: *i64 = sys_mmap(8 * 8) as *i64
46 let mx: *i64 = sys_mmap(8) as *i64
47 let tol: i64 = nx_f32_div(nx_i32_to_f32(1), nx_i32_to_f32(100)) // 0.01
48 let small: i64 = nx_f32_div(nx_i32_to_f32(1), nx_i32_to_f32(1000)) // 0.001
49 let big: i64 = nx_f32_div(nx_i32_to_f32(1), nx_i32_to_f32(2)) // 0.5
50
51 // ref = [1,2,3]
52 b[0] = nx_i32_to_f32(1); b[1] = nx_i32_to_f32(2); b[2] = nx_i32_to_f32(3)
53
54 // (a) identical
55 a[0] = b[0]; a[1] = b[1]; a[2] = b[2]
56 if nx_f32_parity_check(a, b, 3, tol, mx) != NX_F32PAR_PASS { return 10 }
57 if mx[0] != 0 { return 11 } // max_abs == +0.0
58
59 // (b) within tol: bump element 1 by 0.001
60 a[1] = nx_f32_add(b[1], small)
61 if nx_f32_parity_check(a, b, 3, tol, mx) != NX_F32PAR_PASS { return 20 }
62
63 // (c) over tol: bump element 1 by 0.5
64 a[1] = nx_f32_add(b[1], big)
65 if nx_f32_parity_check(a, b, 3, tol, mx) != NX_F32PAR_FAIL { return 30 }
66 // and the reported max_abs must exceed the tolerance
67 if (mx[0] & 0x7FFFFFFF) <= (tol & 0x7FFFFFFF) { return 31 }
68
69 return 0
70}