code wiki / (root) / nx_fd_f32.nx

nx_fd_f32.nx

buildroot/runtime/nx_fd_f32.nx

17104 B425 linesdepth 3pulls 6 transitivereach 0 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

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

syscalls.nx nx_itoa_lib.nx nx_f32_hw.nx nx_f32.nx nx_fd_f32.nx

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

main g_w g_wn nxi_out nxi_fd sys_mmap ccz_cat_num sys_write sys_munmap g_lowrank_err g_build_lowrank g_build_rankr f32frac f32_div f32_of g_lcg_next f32_add f32_mul g_run fd32_alloc fd32_add_row fd32_empty_row_index f32_is_zero_bits fd32_row_norm_sq fd32_b_get f32_add ↻ f32_mul ↻ fd32_shrink fd32_power_one f32_of ↻ fd32_reorth fd32_vnormalize fd32_matvec_Bv fd32_matvec_Btw f32_is_zero_bits ↻ fd32_dot fd32_basis_set f32_lt_nonneg f32_sub f32_neg

structs

31struct FreqDir32

consts

18const FD32_MAGIC_1103515245: i64 = 1103515245
19const FD32_MAGIC_12345: i64 = 12345
20const FD32_MAGIC_7919: i64 = 7919
21const FD32_MAGIC_2048: i64 = 2048
22const FD32_MAGIC_4096: i64 = 4096
24const FD32_L_MAX: i64 = 4
25const FD32_D_MAX: i64 = 8
26const FD32_POWER_ITERS: i64 = 64
27const FD32_OK: i64 = 0
28const FD32_ERR_TOO_BIG: i64 = -1
29const FD32_ERR_DIM: i64 = -2

functions

44func f32_is_zero_bits(x: i64) -> i64 { if (x & 0x7fffffff) == 0 { return 1 } return 0 }
45func f32_is_neg_bits(x: i64) -> i64 { if (x & 0x80000000) != 0 { if (x & 0x7fffffff) != 0 { return 1 } } return 0 }
called by 1: fd32_shrink
47func f32_lt_nonneg(a: i64, b: i64) -> i64 { if a < b { return 1 } return 0 }
50func fd32_alloc(l: i64, d: i64) -> *FreqDir32
called by 1: g_run
70func fd32_b_get(fd: *FreqDir32, r: i64, c: i64) -> i64 { return fd.b[r * fd.d + c] }
71func fd32_b_set(fd: *FreqDir32, r: i64, c: i64, v: i64) -> i64 { fd.b[r * fd.d + c] = v; return 0 }
72func fd32_basis_get(fd: *FreqDir32, idx: i64, c: i64) -> i64 { return fd.scratch_basis[idx * fd.d + c] }
73func 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
76func fd32_dot(a: *i64, b: *i64, n: i64) -> i64
83func fd32_row_norm_sq(fd: *FreqDir32, r: i64) -> i64
90func fd32_empty_row_index(fd: *FreqDir32) -> i64
96func fd32_vscale(v: *i64, n: i64, factor: i64) -> i64
called by 1: fd32_vnormalize calls 1: f32_mul
102func fd32_vnormalize(v: *i64, n: i64) -> i64
112func fd32_matvec_Bv(fd: *FreqDir32, v: *i64, out: *i64) -> i64
124func fd32_matvec_Btw(fd: *FreqDir32, w: *i64, out: *i64) -> i64
137func fd32_deflate(fd: *FreqDir32, v: *i64, found: i64) -> i64
153func fd32_reorth(fd: *FreqDir32, v: *i64, found: i64) -> i64
called by 1: fd32_power_one calls 1: fd32_deflate
160func fd32_power_one(fd: *FreqDir32, found_idx: i64, init_idx: i64) -> i64
185func fd32_shrink(fd: *FreqDir32) -> i64
203func fd32_add_row(fd: *FreqDir32, row: *i64, n: i64) -> i64
231func 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
236func g_wn(v: i64) -> i64 { nxi_out(v); return 0 }
called by 1: main calls 1: nxi_out
238func f32frac(num: i64, den: i64) -> i64 { return f32_div(f32_of(num), f32_of(den)) }
called by 1: g_build_rankr calls 2: f32_divf32_of
242func g_lcg_next(st: *i64) -> i64
called by 1: g_build_rankr
250func g_build_rankr(K: *i64, n: i64, d: i64, r: i64) -> i64
274func g_build_lowrank(K: *i64, n: i64, d: i64) -> i64 { return g_build_rankr(K, n, d, 2) } // genuinely rank-2
called by 1: g_lowrank_err calls 1: g_build_rankr
275func g_build_fullrank(K: *i64, n: i64, d: i64) -> i64 { return g_build_rankr(K, n, d, d) } // genuinely full rank
called by 1: main calls 1: g_build_rankr
277func g_gram_full(K: *i64, n: i64, d: i64, G: *i64) -> i64
called by 1: g_run calls 2: f32_addf32_mul
293func g_gram_sketch(fd: *FreqDir32, d: i64, G: *i64) -> i64
called by 1: g_run calls 3: f32_addf32_mulfd32_b_get
310func g_rel_permille(Gf: *i64, Gs: *i64, d: i64) -> i64
325func g_run(K: *i64, n: i64, d: i64, l: i64) -> i64
343func g_lowrank_err(n: i64, d: i64, l: i64) -> i64
called by 1: main calls 2: g_build_lowrankg_run
349func g_rankr_err(n: i64, d: i64, l: i64, r: i64) -> i64
called by 1: main calls 2: g_build_rankrg_run
355func main() -> i64