nx_fd_f32.nx
buildroot/runtime/nx_fd_f32.nx
about
nx_fd_f32.nx -- FD v2: Frequent Directions in f32 (the cliff fix; the SIMD-substrate-serves-VRAM-math synthesis).
module: nishi-core.ai.fd_f32 capability: VRAM_REDUCTION (low-rank KV-cache compression, precision-fixed)
WHY (measured, 2026-06-16): the Q14 FD (sketch_freq_directions.nx) gives quality-preserving KV compression
only up to ~n=48 (12x), then COLLAPSES (n=64 -> 521% covariance error). Autonomous cliff-research confirmed
FD is numerically stable BY DESIGN in exact arithmetic (the shrink step bounds ||A^T A - B^T B||_2 <= eps||A||_F^2,
Liberty 1501.01711); the cliff is purely a FINITE-PRECISION artifact, and the precision-critical step is the
orthogonalization/deflation (Tropp 1902.08651: columns stay near-orthonormal only with enough precision).
FIX: do FD in f32 (24-bit mantissa vs Q14's ~14) -- arithmetic on the SSE hardware substrate (nx_f32_hw:
__f32_add/mul/div) + the one op hw lacks, nx_f32_sqrt. f32 compares done via plain bit-ops on the f32 bits.
This UNITES the SIMD/matmul perf arc with the VRAM-math arc -- the fast sovereign kernels SERVE the math.
Sovereign: nx_cc->nxasm SSE, no gcc. HONEST: still REFERENCE-SCALE (l<=4,d<=8 design); v3 = Jacobi/QR for d>8.
dependencies 4 imports · 0 importers
imports: syscalls.nxnx_itoa_lib.nxnx_f32_hw.nxnx_f32.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| 31 | struct FreqDir32 |
consts
| 18 | const FD32_MAGIC_1103515245: i64 = 1103515245 |
| 19 | const FD32_MAGIC_12345: i64 = 12345 |
| 20 | const FD32_MAGIC_7919: i64 = 7919 |
| 21 | const FD32_MAGIC_2048: i64 = 2048 |
| 22 | const FD32_MAGIC_4096: i64 = 4096 |
| 24 | const FD32_L_MAX: i64 = 4 |
| 25 | const FD32_D_MAX: i64 = 8 |
| 26 | const FD32_POWER_ITERS: i64 = 64 |
| 27 | const FD32_OK: i64 = 0 |
| 28 | const FD32_ERR_TOO_BIG: i64 = -1 |
| 29 | const FD32_ERR_DIM: i64 = -2 |
functions
| 44 | func f32_is_zero_bits(x: i64) -> i64 { if (x & 0x7fffffff) == 0 { return 1 } return 0 } |
| 45 | func f32_is_neg_bits(x: i64) -> i64 { if (x & 0x80000000) != 0 { if (x & 0x7fffffff) != 0 { return 1 } } return 0 } called by 1: fd32_shrink |
| 47 | func f32_lt_nonneg(a: i64, b: i64) -> i64 { if a < b { return 1 } return 0 } |
| 50 | func fd32_alloc(l: i64, d: i64) -> *FreqDir32 called by 1: g_run |
| 70 | func fd32_b_get(fd: *FreqDir32, r: i64, c: i64) -> i64 { return fd.b[r * fd.d + c] } |
| 71 | func fd32_b_set(fd: *FreqDir32, r: i64, c: i64, v: i64) -> i64 { fd.b[r * fd.d + c] = v; return 0 } |
| 72 | func fd32_basis_get(fd: *FreqDir32, idx: i64, c: i64) -> i64 { return fd.scratch_basis[idx * fd.d + c] } |
| 73 | func fd32_basis_set(fd: *FreqDir32, idx: i64, c: i64, v: i64) -> i64 { fd.scratch_basis[idx * fd.d + c] = v; return 0 } called by 1: fd32_power_one |
| 76 | func fd32_dot(a: *i64, b: *i64, n: i64) -> i64 |
| 83 | func fd32_row_norm_sq(fd: *FreqDir32, r: i64) -> i64 |
| 90 | func fd32_empty_row_index(fd: *FreqDir32) -> i64 |
| 96 | func fd32_vscale(v: *i64, n: i64, factor: i64) -> i64 |
| 102 | func fd32_vnormalize(v: *i64, n: i64) -> i64 |
| 112 | func fd32_matvec_Bv(fd: *FreqDir32, v: *i64, out: *i64) -> i64 |
| 124 | func fd32_matvec_Btw(fd: *FreqDir32, w: *i64, out: *i64) -> i64 |
| 137 | func fd32_deflate(fd: *FreqDir32, v: *i64, found: i64) -> i64 |
| 153 | func fd32_reorth(fd: *FreqDir32, v: *i64, found: i64) -> i64 |
| 160 | func fd32_power_one(fd: *FreqDir32, found_idx: i64, init_idx: i64) -> i64 called by 1: fd32_shrink calls 8: f32_offd32_reorthfd32_vnormalizefd32_matvec_Bvfd32_matvec_Btwf32_is_zero_bits+2 |
| 185 | func fd32_shrink(fd: *FreqDir32) -> i64 called by 1: fd32_add_row calls 8: fd32_power_onef32_lt_nonnegf32_subf32_is_neg_bitsnx_f32_sqrtfd32_b_set+2 |
| 203 | func fd32_add_row(fd: *FreqDir32, row: *i64, n: i64) -> i64 |
| 231 | func g_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } called by 1: main |
| 236 | func g_wn(v: i64) -> i64 { nxi_out(v); return 0 } |
| 238 | func f32frac(num: i64, den: i64) -> i64 { return f32_div(f32_of(num), f32_of(den)) } |
| 242 | func g_lcg_next(st: *i64) -> i64 called by 1: g_build_rankr |
| 250 | func g_build_rankr(K: *i64, n: i64, d: i64, r: i64) -> i64 |
| 274 | func g_build_lowrank(K: *i64, n: i64, d: i64) -> i64 { return g_build_rankr(K, n, d, 2) } // genuinely rank-2 |
| 275 | func g_build_fullrank(K: *i64, n: i64, d: i64) -> i64 { return g_build_rankr(K, n, d, d) } // genuinely full rank |
| 277 | func g_gram_full(K: *i64, n: i64, d: i64, G: *i64) -> i64 |
| 293 | func g_gram_sketch(fd: *FreqDir32, d: i64, G: *i64) -> i64 |
| 310 | func g_rel_permille(Gf: *i64, Gs: *i64, d: i64) -> i64 |
| 325 | func g_run(K: *i64, n: i64, d: i64, l: i64) -> i64 called by 3: g_lowrank_errg_rankr_errmain calls 5: fd32_allocfd32_add_rowg_gram_fullg_gram_sketchg_rel_permille |
| 343 | func g_lowrank_err(n: i64, d: i64, l: i64) -> i64 |
| 349 | func g_rankr_err(n: i64, d: i64, l: i64, r: i64) -> i64 |
| 355 | func main() -> i64 |