code wiki / (root) / nx_lowrank_kv_budget.nx

nx_lowrank_kv_budget.nx

buildroot/runtime/nx_lowrank_kv_budget.nx

6623 B93 linesdepth 2pulls 2 transitivereach 0 importersview sourcekind tooltopic lowrank
docsdependenciesstructsconstsfunctions

about

nx_lowrank_kv_budget.nx -- the HARDWARE PAYOFF of data-driven low-rank KV (nx_lowrank_rank_plan) on the operator's real budget: RTX 5080, 16 GiB VRAM (rule #21 resource awareness; telos = capable AI on AFFORDABLE consumer hardware). KV cache is the context-length bottleneck: full KV grows O(n_layers * 2 * kv_dim) per token; data-driven low-rank KV grows O(n_layers * (r_K + r_V)) per token. This computes the MAX CONTEXT LENGTH full-vs-low-rank for a given model on a given VRAM budget. Pure integer math, NO slow forward. Real Qwen2.5-0.5B config + ranks (measured); 7B shown as an honest illustrative extrapolation. 100% sovereign (nx_cc->nxasm, no gcc). license_tier: ORIGINAL ⚠ SUPERSEDED 2026-06-17: the context-multipliers here (42x for 0.5B, 170x for 7B) used K=1/V=5 ranks from a DEGENERATE-context KV spectrum. The definitive seq=64 measurement (nx_lowrank_kv_real M4d) shows real KV needs avg rank K=27/V=37 of 64 for 90% energy => the real KV VRAM win is ~2-3x, NOT 42x. The budget MATH here is valid; the input ranks were wrong. WEIGHTS are heterogeneous too (attn low-rank but small, big ffn NOT low-rank) => weight-SVD also modest. BOTH low-rank VRAM levers are MODEST on real Qwen-0.5B.

dependencies 1 imports · 0 importers

nx_syscalls.nx nx_lowrank_kv_budget.nx

imports: nx_syscalls.nx

imported by: nobody (leaf or entry point)

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main bp_puts sys_write bp_model bp_puts ↻ bp_putM bp_putn sys_write ↻ sys_mmap bp_puts ↻ bp_putn ↻ bp_ctx_full bp_ctx_low bp_putn ↻ bp_ctx_low ↻ bp_ctx_full ↻ sys_exit

structs

none

consts

14const K_MAGIC_1048576: i64 = 1048576
15const K_MAGIC_491400032: i64 = 491400032
16const K_MAGIC_5046586573: i64 = 5046586573
18const GIB: i64 = 1073741824
19const VRAM: i64 = 17179869184 // 16 GiB (RTX 5080)
20const KVB: i64 = 4 // KV cache element bytes (f32, matches our sovereign forward; f16 would 2x the absolute token counts, ratio unchanged)

functions

22func bp_puts(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 3: bp_putMbp_modelmain calls 1: sys_write
23func bp_putn(v: i64) -> i64
33func bp_putM(bytes: i64) -> i64 { bp_putn(bytes / K_MAGIC_1048576); bp_puts(" MiB"); return 0 }
called by 1: bp_model calls 2: bp_putnbp_puts
36func bp_ctx_full(kv_budget: i64, L: i64, kv_dim: i64) -> i64 { return kv_budget / (L * 2 * kv_dim * KVB) }
called by 2: bp_modelmain
39func bp_ctx_low(kv_budget: i64, L: i64, kv_dim: i64, rK: i64, rV: i64) -> i64
called by 2: bp_modelmain
46func bp_model(name: *u8, weights: i64, L: i64, kv_dim: i64, rK: i64, rV: i64, measured: i64) -> i64
61func main() -> i64