code wiki / (root) / nx_nofloat_llm.nx

nx_nofloat_llm.nx

buildroot/runtime/nx_nofloat_llm.nx

78066 B1340 linesdepth 7pulls 15 transitivereach 74 importersview sourcekind librarytopic nofloat
docsdependenciesstructsconstsfunctions

about

nx_nofloat_llm.nx -- CANONICAL sovereign no-float LLM library (ONE source of truth; no copy-paste debt). Every integer-Q16 building block for running a real GGUF transformer (Qwen2.5) in deterministic no-float: - dequant of every quant the model uses (F32 / Q5_0 / Q8_0 / Q4_K / Q6_K) -> Q16 - by-name tensor load + window-dequant (for embeddings / LM-head rows) - fixed-point transcendentals (exp, sigmoid, SiLU, sin, cos) - the ONE matmul: full-precision accumulate-then-shift (the correct, non-underflowing version) - RMSNorm, RoPE, attention sublayer, FFN sublayer, the lazy N-layer stack Gates IMPORT this instead of re-defining (CLAUDE.md #15 DRY). One canonical, correct implementation: a precision/convention fix is made HERE, once, not in 25 copies. No `main` (pure library). No hw writes (Rule 26). license_tier: ORIGINAL

dependencies 7 imports · 30 importers

nx_syscalls.nx nx_tier.nx nx_le.nx nx_tensor.nx nx_gguf.nx nx_gguf_load.nx nx_thread_pool.nx nx_nofloat_llm.nx nx_coder_swap_gate.nx nx_gguf_dequant_kat_gate.nx nx_gpu_export.nx nx_gpu_serve_gate.nx nx_llm_capcheck.nx nx_llm_loopb_gate.nx nx_llm_ppl_bench_gate.nx nx_nofloat_arch.nx nx_nofloat_arch_config_gate.nx nx_nofloat_arch_moe_gate.nx

diagram shows first 10 each side; +0 more imports, +20 more importers in the complete lists below.

imports: nx_syscalls.nxnx_tier.nxnx_le.nxnx_tensor.nxnx_gguf.nxnx_gguf_load.nxnx_thread_pool.nx

imported by: nx_coder_swap_gate.nxnx_gguf_dequant_kat_gate.nxnx_gpu_export.nxnx_gpu_serve_gate.nxnx_llm_capcheck.nxnx_llm_loopb_gate.nxnx_llm_ppl_bench_gate.nxnx_nofloat_arch.nxnx_nofloat_arch_config_gate.nxnx_nofloat_arch_moe_gate.nxnx_nofloat_mla.nxnx_nofloat_moe.nxnx_nofloat_moe_real_gate.nxnx_nofloat_olmoe.nxnx_nofloat_olmoe_forward_gate.nxnx_nofloat_olmoe_gate.nxnx_nofloat_qwen_diag_gate.nxnx_nofloat_qwen_dqprobe_gate.nxnx_nofloat_qwen_fastgen_gate.nxnx_nofloat_qwen_fastgen_i32_gate.nxnx_nofloat_qwen_fastgen_i8_gate.nxnx_nofloat_qwen_fidelity_gate.nxnx_nofloat_qwen_forward_gate.nxnx_nofloat_qwen_gen_gate.nxnx_nofloat_qwen_kvgen_gate.nxnx_nofloat_qwen_rope_h2h_gate.nxnx_nofloat_qwen_stack_gate.nxnx_nofloat_qwen_text_gate.nxnx_nofloat_qwen_wload_gate.nxnx_nofloat_serve_core.nx

structs

none

consts

21const Q16: i64 = 65536
22const LOG2E: i64 = 94548
23const PC0: i64 = 65536
24const PC1: i64 = 45426
25const PC2: i64 = 15743
26const PC3: i64 = 4367
27const LN_BASE_Q16: i64 = 905421 // ln(1e6), Qwen2.5 rope base
28const HALF_PI: i64 = 102944
29const PI: i64 = 205887
30const THREE_HALF_PI: i64 = 308831
31const TWO_PI: i64 = 411775

functions

33func qmul(a: i64, b: i64) -> i64 { return (a*b) >> 16 }
34func cpy(d: *i64, s: *i64, n: i64) -> i64 { var i: i64=0; while i<n { d[i]=s[i]; i=i+1 } return 0 }
35func q_i8(buf: *u8, off: i64) -> i64 { let v: i64=nx_le_read_u8(buf, off); if v>=128 { return v-256 } return v }
called by 2: q8_0_blockq6k_block calls 1: nx_le_read_u8
38func f32_to_q16(bits: i64) -> i64 { let s: i64=(bits>>31)&1; let e: i64=(bits>>23)&255; let mant: i64=bits&8388607; var v: i64=0; if e==0 { v=0 } else { if e==255 { v=2147483647 } else { let m: i64=8388608+mant; let ee: i64=e-134; if ee>=0 { v=m<<ee } else { v=m>>(0-ee) } } } if s==1 { v=0-v } return v }
39func f16_to_q16(h: i64) -> i64 { let s: i64=(h>>15)&1; let e: i64=(h>>10)&31; let mant: i64=h&1023; var v: i64=0; if e==0 { v=mant>>8 } else { if e==31 { v=2147483647 } else { let m: i64=1024+mant; let ee: i64=e-9; if ee>=0 { v=m<<ee } else { v=m>>(0-ee) } } } if s==1 { v=0-v } return v }
42func q5_0_block(buf: *u8, off: i64, n: i64, out: *i64) -> i64 { let d: i64=f16_to_q16(nx_le_read_u16(buf, off)); let qh: i64=nx_le_read_u32(buf, off+2); let qo: i64=off+6; var j: i64=0; while j<16 { let qs: i64=nx_le_read_u8(buf, qo+j); let xh0: i64=((qh>>j)<<4)&16; let xh1: i64=(qh>>(j+12))&16; if j<n { out[j]=d*(((qs&15)|xh0)-16) } if j+16<n { out[j+16]=d*(((qs>>4)|xh1)-16) } j=j+1 } return 0 }
43func q8_0_block(buf: *u8, off: i64, n: i64, out: *i64) -> i64 { let d: i64=f16_to_q16(nx_le_read_u16(buf, off)); var j: i64=0; while j<32 { if j<n { out[j]=d*q_i8(buf, off+2+j) } j=j+1 } return 0 }
47func q6k_block(buf: *u8, so: i64, n: i64, out: *i64) -> i64
66func q4_0_block(buf: *u8, off: i64, n: i64, out: *i64) -> i64 { let d: i64=f16_to_q16(nx_le_read_u16(buf, off)); let qo: i64=off+2; var j: i64=0; while j<16 { let qs: i64=nx_le_read_u8(buf, qo+j); if j<n { out[j]=d*((qs&15)-8) } if j+16<n { out[j+16]=d*((qs>>4)-8) } j=j+1 } return 0 }
68func q4_1_block(buf: *u8, off: i64, n: i64, out: *i64) -> i64 { let d: i64=f16_to_q16(nx_le_read_u16(buf, off)); let m: i64=f16_to_q16(nx_le_read_u16(buf, off+2)); let qo: i64=off+4; var j: i64=0; while j<16 { let qs: i64=nx_le_read_u8(buf, qo+j); if j<n { out[j]=d*(qs&15)+m } if j+16<n { out[j+16]=d*(qs>>4)+m } j=j+1 } return 0 }
70func q5_1_block(buf: *u8, off: i64, n: i64, out: *i64) -> i64 { let d: i64=f16_to_q16(nx_le_read_u16(buf, off)); let m: i64=f16_to_q16(nx_le_read_u16(buf, off+2)); let qh: i64=nx_le_read_u32(buf, off+4); let qo: i64=off+8; var j: i64=0; while j<16 { let qs: i64=nx_le_read_u8(buf, qo+j); let xh0: i64=((qh>>j)<<4)&16; let xh1: i64=(qh>>(j+12))&16; if j<n { out[j]=d*((qs&15)|xh0)+m } if j+16<n { out[j+16]=d*((qs>>4)|xh1)+m } j=j+1 } return 0 }
72func qk_scm(buf: *u8, sco: i64, is: i64, box: *i64) -> i64
called by 1: q5k_block calls 1: nx_le_read_u8
87func q5k_block(buf: *u8, so: i64, n: i64, out: *i64) -> i64
121func q3k_sc(buf: *u8, sco: i64, is: i64) -> i64
called by 1: q3k_block calls 1: nx_le_read_u8
136func q3k_block(buf: *u8, so: i64, n: i64, out: *i64) -> i64
179func q2k_block(buf: *u8, so: i64, n: i64, out: *i64) -> i64
221func nf_type_stride(ty: i64, vb: *i64) -> i64
239func nf_expert_byteoff(ty: i64, voff: i64) -> i64
called by 1: nolmoe_moe calls 2: sys_mmapnf_type_stride
245func dequant_to_q16(buf: *u8, base: i64, gt: i64, n: i64, out: *i64) -> i64
262func load_named_q16(buf: *u8, hdr: *NxGgufHeader, name: *u8, nlen: i64, out: *i64, want: i64) -> i64
274func dequant_row(buf: *u8, base: i64, gt: i64, v: i64, ne: i64, out: *i64, tmp: *i64) -> i64
296func build_name(out: *u8, L: i64, suffix: *u8) -> i64
304func load_blk(buf: *u8, hdr: *NxGgufHeader, nm: *u8, L: i64, suffix: *u8, out: *i64, want: i64) -> i64 { let len: i64=build_name(nm, L, suffix); return load_named_q16(buf, hdr, nm, len, out, want) }
307func isqrt(v: i64) -> i64 { if v<=0 { return 0 } if v<4 { return 1 } var x: i64=v; var y: i64=(x+1)>>1; var go: i64=1; while go==1 { if y<x { x=y; y=(x+v/x)>>1 } else { go=0 } } return x }
308func fx_exp(x: i64) -> i64 { var xm: i64=0-x; if x>0 { xm=0 } let ym: i64=(xm*LOG2E)>>16; let yi: i64=ym>>16; let yf: i64=ym-(yi<<16); let g: i64=Q16-yf; var t: i64=PC3; t=PC2+((g*t)>>16); t=PC1+((g*t)>>16); t=PC0+((g*t)>>16); t=t>>1; if yi>=31 { return 0 } return t>>yi }
309func sigmoid(x: i64) -> i64 { if x>=0 { let ex: i64=fx_exp(0-x); return (Q16*Q16)/(Q16+ex) } let ex: i64=fx_exp(x); let sp: i64=(Q16*Q16)/(Q16+ex); return Q16-sp }
called by 1: silu calls 1: fx_exp
310func silu(x: i64) -> i64 { return qmul(x, sigmoid(x)) }
311func sin_q(x: i64) -> i64 { let x2: i64=qmul(x,x); let x3: i64=qmul(x2,x); let x5: i64=qmul(x3,x2); let x7: i64=qmul(x5,x2); let x9: i64=qmul(x7,x2); return x - x3/6 + x5/120 - x7/5040 + x9/362880 }
called by 1: sin_full calls 1: qmul
312func cos_q(x: i64) -> i64 { let x2: i64=qmul(x,x); let x4: i64=qmul(x2,x2); let x6: i64=qmul(x4,x2); let x8: i64=qmul(x6,x2); return Q16 - x2/2 + x4/24 - x6/720 + x8/40320 }
called by 1: cos_full calls 1: qmul
313func reduce2pi(a: i64) -> i64 { var t: i64=a; while t<0 { t=t+TWO_PI } while t>=TWO_PI { t=t-TWO_PI } return t }
called by 2: sin_fullcos_full
314func sin_full(a: i64) -> i64 { let t: i64=reduce2pi(a); if t<HALF_PI { return sin_q(t) } if t<PI { return sin_q(PI-t) } if t<THREE_HALF_PI { return 0-sin_q(t-PI) } return 0-sin_q(TWO_PI-t) }
called by 1: rope_apply calls 2: reduce2pisin_q
315func cos_full(a: i64) -> i64 { let t: i64=reduce2pi(a); if t<HALF_PI { return cos_q(t) } if t<PI { return 0-cos_q(PI-t) } if t<THREE_HALF_PI { return 0-cos_q(t-PI) } return cos_q(TWO_PI-t) }
called by 1: rope_apply calls 2: reduce2picos_q
319func rope_apply(v: *i64, hd: i64, pos: i64, freqs: *i64) -> i64 { let np: i64=hd/2; var i: i64=0; while i<np { let ang: i64=pos*freqs[i]; let c: i64=cos_full(ang); let s: i64=sin_full(ang); let a: i64=v[i]; let b: i64=v[i+np]; v[i]=qmul(a,c)-qmul(b,s); v[i+np]=qmul(a,s)+qmul(b,c); i=i+1 } return 0 }
321func rope_freqs(freqs: *i64, hd: i64) -> i64 { let np: i64=hd/2; var i: i64=0; while i<np { freqs[i]=fx_exp(0-((i*LN_BASE_Q16)/np)); i=i+1 } return 0 }
called by 18: mainmainmainmainmainmain+12 calls 1: fx_exp
326func fx_ln_int(x: i64) -> i64
called by 2: nac_read_configmain
351func rope_freqs_base(freqs: *i64, hd: i64, ln_base_q16: i64) -> i64 { let np: i64=hd/2; var i: i64=0; while i<np { freqs[i]=fx_exp(0-((i*ln_base_q16)/np)); i=i+1 } return 0 }
called by 2: mainmain calls 1: fx_exp
354func mm_out_in(inp: *i64, W: *i64, dst: *i64, T: i64, in_dim: i64, out_dim: i64, rev: i64) -> i64
364func rmsnorm_gamma_row(x: *i64, gamma: *i64, xoff: i64, D: i64, out: *i64, ooff: i64) -> i64
373func rmsnorm_gamma_row_q24(x: *i64, gamma: *i64, xoff: i64, D: i64, out: *i64, ooff: i64) -> i64
called by 21: mainmaingdx_generatemainmainmain+15 calls 1: isqrt
380func mm_q24in(inp: *i64, W: *i64, dst: *i64, T: i64, in_dim: i64, out_dim: i64, rev: i64) -> i64
403func nf_pool() -> *NxThreadPool
420func nf_hsum_sx(acc: *u8) -> i64
called by 1: _nfmm_i8_task
427func _nfmm_task(ctx_i: i64) -> i64
440func mm_pool(inp: *i64, W: *i64, dst: *i64, T: i64, in_dim: i64, out_dim: i64, shift: i64) -> i64
462func _nfld_task(ctx_i: i64) -> i64
calls 1: load_blk
468func nf_ld1(p: *NxThreadPool, i: i64, buf: *u8, hdr: *NxGgufHeader, L: i64, sfx: *u8, out: i64, want: i64) -> i64
called by 1: nf_load_layer_pool calls 1: nx_pool_submit
475func nf_load_layer_pool(buf: *u8, hdr: *NxGgufHeader, L: i64, wb: *i64, ne: i64, qd: i64, kvd: i64, fd: i64) -> i64
494func _nfhd_task(ctx_i: i64) -> i64
calls 1: dequant_row
516func head_argmax_pool(hp: *i64) -> i64
565func attn_sublayer(x: *i64, gamma: *i64, Wq: *i64, Wk: *i64, Wv: *i64, Wo: *i64, bq: *i64, bk: *i64, bv: *i64, freqs: *i64, xn: *i64, Q: *i64, K: *i64, V: *i64, concat: *i64, sc: *i64, at: *i64, proj: *i64, out: *i64, cfg: *i64, rev: i64) -> i64
601func ffn_sublayer(x: *i64, gamma: *i64, Wg: *i64, Wu: *i64, Wd: *i64, xn: *i64, gate: *i64, up: *i64, hbuf: *i64, proj: *i64, out: *i64, cfg: *i64, rev: i64) -> i64
612func run_stack(buf: *u8, hdr: *NxGgufHeader, x: *i64, out: *i64, wb: *i64, sb: *i64, nm: *u8, freqs: *i64, cfgA: *i64, cfgF: *i64, N: i64, rev: i64) -> i64
650func run_stack_prefill_kv(buf: *u8, hdr: *NxGgufHeader, x: *i64, out: *i64, wb: *i64, sb: *i64, nm: *u8, freqs: *i64, kvc: *i64, cfgA: *i64, cfgF: *i64, N: i64) -> i64
678func decode_step_kv(buf: *u8, hdr: *NxGgufHeader, x1: *i64, out1: *i64, wb: *i64, sb: *i64, nm: *u8, freqs: *i64, kvc: *i64, pos: i64, cfgA: *i64, cfgF: *i64, N: i64) -> i64
737func nf_alloc_layer_wb(ne: i64, qd: i64, kvd: i64, fd: i64) -> *i64
746func nf_dequant_all_layers(buf: *u8, hdr: *NxGgufHeader, wcache: *i64, N: i64, ne: i64, qd: i64, kvd: i64, fd: i64) -> i64
759func decode_step_kv_cached(buf: *u8, hdr: *NxGgufHeader, x1: *i64, out1: *i64, wcache: *i64, sb: *i64, nm: *u8, freqs: *i64, kvc: *i64, pos: i64, cfgA: *i64, cfgF: *i64, N: i64) -> i64
813func _nfhdq_task(ctx_i: i64) -> i64
calls 1: dequant_row
822func nf_dequant_head_all(buf: *u8, oh_base: i64, oh_ty: i64, vocab: i64, ne: i64) -> *i64
847func _nfhac_task(ctx_i: i64) -> i64
865func head_argmax_cached(hcp: *i64) -> i64
905func nf_narrow_i32(src: *i64, dst: *i32, cnt: i64) -> i64
918func _nfmm_i32_task(ctx_i: i64) -> i64
932func mm_pool_i32(inp: *i64, W: *i32, dst: *i64, T: i64, in_dim: i64, out_dim: i64, shift: i64) -> i64
954func nf_alloc_layer_wb_i32(ne: i64, qd: i64, kvd: i64, fd: i64) -> *i64
called by 1: nf_dequant_all_layers_i32 calls 1: sys_mmap
963func nf_dequant_all_layers_i32(buf: *u8, hdr: *NxGgufHeader, wc32: *i64, N: i64, ne: i64, qd: i64, kvd: i64, fd: i64) -> i64
989func decode_step_kv_cached_i32(buf: *u8, hdr: *NxGgufHeader, x1: *i64, out1: *i64, wcache: *i64, sb: *i64, nm: *u8, freqs: *i64, kvc: *i64, pos: i64, cfgA: *i64, cfgF: *i64, N: i64) -> i64
1043func _nfhdq_i32_task(ctx_i: i64) -> i64
calls 1: dequant_row
1051func nf_dequant_head_all_i32(buf: *u8, oh_base: i64, oh_ty: i64, vocab: i64, ne: i64) -> *i32
1088func _nfhac_i32_task(ctx_i: i64) -> i64
1107func _nfhlg_task(ctx_i: i64) -> i64
1121func head_logits_cached_i32(hlp: *i64) -> i64
1145func head_argmax_cached_i32(hcp: *i64) -> i64
1185func nf_pack2(buf: *u8, idx: i64, val: i64) -> i64 { buf[idx*2]=(val) as u8; buf[idx*2+1]=(val>>8) as u8; return 0 }
1186func nf_quant_w_i8(Wsrc: *i64, Wi8: *u8, sw: *i64, out_dim: i64, in_dim: i64) -> i64
called by 1: nf_dequant_all_layers_i8 calls 1: nf_pack2
1199func nf_alloc_layer_wb_i8(ne: i64, qd: i64, kvd: i64, fd: i64) -> *i64
called by 1: nf_dequant_all_layers_i8 calls 1: sys_mmap
1213func nf_dequant_all_layers_i8(buf: *u8, hdr: *NxGgufHeader, wc8: *i64, N: i64, ne: i64, qd: i64, kvd: i64, fd: i64) -> i64
1236func _nfmm_i8_task(ctx_i: i64) -> i64
calls 1: nf_hsum_sx
1253func mm_pool_i8(x: *i64, Wi8: *u8, sw: *i64, dst: *i64, in_dim: i64, out_dim: i64, shift: i64) -> i64
1283func decode_step_kv_cached_i8(buf: *u8, hdr: *NxGgufHeader, x1: *i64, out1: *i64, wc8: *i64, sb: *i64, nm: *u8, freqs: *i64, kvc: *i64, pos: i64, cfgA: *i64, cfgF: *i64, N: i64) -> i64