nx_nofloat_llm.nx
buildroot/runtime/nx_nofloat_llm.nx
about
dependencies 7 imports · 30 importers
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
| 21 | const Q16: i64 = 65536 |
| 22 | const LOG2E: i64 = 94548 |
| 23 | const PC0: i64 = 65536 |
| 24 | const PC1: i64 = 45426 |
| 25 | const PC2: i64 = 15743 |
| 26 | const PC3: i64 = 4367 |
| 27 | const LN_BASE_Q16: i64 = 905421 // ln(1e6), Qwen2.5 rope base |
| 28 | const HALF_PI: i64 = 102944 |
| 29 | const PI: i64 = 205887 |
| 30 | const THREE_HALF_PI: i64 = 308831 |
| 31 | const TWO_PI: i64 = 411775 |
functions
| 33 | func qmul(a: i64, b: i64) -> i64 { return (a*b) >> 16 } |
| 34 | func cpy(d: *i64, s: *i64, n: i64) -> i64 { var i: i64=0; while i<n { d[i]=s[i]; i=i+1 } return 0 } |
| 35 | func q_i8(buf: *u8, off: i64) -> i64 { let v: i64=nx_le_read_u8(buf, off); if v>=128 { return v-256 } return v } |
| 38 | func 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 } |
| 39 | func 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 } |
| 42 | func 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 } |
| 43 | func 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 } |
| 47 | func q6k_block(buf: *u8, so: i64, n: i64, out: *i64) -> i64 |
| 66 | func 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 } |
| 68 | func 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 } |
| 70 | func 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 } called by 3: maindequant_to_q16dequant_row calls 4: f16_to_q16nx_le_read_u16nx_le_read_u32nx_le_read_u8 |
| 72 | func qk_scm(buf: *u8, sco: i64, is: i64, box: *i64) -> i64 |
| 87 | func q5k_block(buf: *u8, so: i64, n: i64, out: *i64) -> i64 called by 3: maindequant_to_q16dequant_row calls 5: _gguf_f16_to_q24nx_le_read_u16sys_mmapqk_scmnx_le_read_u8 |
| 121 | func q3k_sc(buf: *u8, sco: i64, is: i64) -> i64 |
| 136 | func q3k_block(buf: *u8, so: i64, n: i64, out: *i64) -> i64 called by 3: maindequant_to_q16dequant_row calls 4: _gguf_f16_to_q24nx_le_read_u16q3k_scnx_le_read_u8 |
| 179 | func q2k_block(buf: *u8, so: i64, n: i64, out: *i64) -> i64 |
| 221 | func nf_type_stride(ty: i64, vb: *i64) -> i64 |
| 239 | func nf_expert_byteoff(ty: i64, voff: i64) -> i64 |
| 245 | func dequant_to_q16(buf: *u8, base: i64, gt: i64, n: i64, out: *i64) -> i64 |
| 262 | func load_named_q16(buf: *u8, hdr: *NxGgufHeader, name: *u8, nlen: i64, out: *i64, want: i64) -> i64 called by 20: mainmainmainmainmainmain+14 calls 4: nx_gguf_find_tensornx_gguf_tensor_atnx_gguf_tensor_n_valuesdequant_to_q16 |
| 274 | func dequant_row(buf: *u8, base: i64, gt: i64, v: i64, ne: i64, out: *i64, tmp: *i64) -> i64 called by 27: mainge2_gpu_embedmainmaingdx_generatemain+21 calls 14: f32_to_q16nx_le_read_u32f16_to_q16nx_le_read_u16q8_0_blockq5_0_block+8 |
| 296 | func build_name(out: *u8, L: i64, suffix: *u8) -> i64 |
| 304 | func 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) } |
| 307 | func 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 } |
| 308 | func 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 } |
| 309 | func 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 } |
| 310 | func silu(x: i64) -> i64 { return qmul(x, sigmoid(x)) } |
| 311 | func 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 } |
| 312 | func 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 } |
| 313 | func 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 } |
| 314 | func 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) } |
| 315 | func 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) } |
| 319 | func 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 } |
| 321 | func 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 } |
| 326 | func fx_ln_int(x: i64) -> i64 |
| 351 | func 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 } |
| 354 | func mm_out_in(inp: *i64, W: *i64, dst: *i64, T: i64, in_dim: i64, out_dim: i64, rev: i64) -> i64 |
| 364 | func rmsnorm_gamma_row(x: *i64, gamma: *i64, xoff: i64, D: i64, out: *i64, ooff: i64) -> i64 |
| 373 | func rmsnorm_gamma_row_q24(x: *i64, gamma: *i64, xoff: i64, D: i64, out: *i64, ooff: i64) -> i64 |
| 380 | func mm_q24in(inp: *i64, W: *i64, dst: *i64, T: i64, in_dim: i64, out_dim: i64, rev: i64) -> i64 |
| 403 | func nf_pool() -> *NxThreadPool |
| 420 | func nf_hsum_sx(acc: *u8) -> i64 called by 1: _nfmm_i8_task |
| 427 | func _nfmm_task(ctx_i: i64) -> i64 |
| 440 | func mm_pool(inp: *i64, W: *i64, dst: *i64, T: i64, in_dim: i64, out_dim: i64, shift: i64) -> i64 |
| 462 | func _nfld_task(ctx_i: i64) -> i64 calls 1: load_blk |
| 468 | func nf_ld1(p: *NxThreadPool, i: i64, buf: *u8, hdr: *NxGgufHeader, L: i64, sfx: *u8, out: i64, want: i64) -> i64 |
| 475 | func nf_load_layer_pool(buf: *u8, hdr: *NxGgufHeader, L: i64, wb: *i64, ne: i64, qd: i64, kvd: i64, fd: i64) -> i64 |
| 494 | func _nfhd_task(ctx_i: i64) -> i64 calls 1: dequant_row |
| 516 | func head_argmax_pool(hp: *i64) -> i64 called by 3: mainmainmain calls 6: nf_poolsys_mmapnx_pool_n_completednx_pool_submitnx_pool_waitsys_munmap |
| 565 | func 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 called by 3: run_stackrun_stack_prefill_kvmain calls 7: rmsnorm_gamma_row_q24mm_poolmm_q24inrope_applyqmulfx_exp+1 |
| 601 | func 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 called by 3: run_stackrun_stack_prefill_kvmain calls 5: rmsnorm_gamma_row_q24mm_q24inqmulsilumm_out_in |
| 612 | func 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 |
| 650 | func 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 |
| 678 | func 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 |
| 737 | func nf_alloc_layer_wb(ne: i64, qd: i64, kvd: i64, fd: i64) -> *i64 called by 3: nf_dequant_all_layersnf_dequant_all_layers_i32nf_dequant_all_layers_i8 calls 1: sys_mmap |
| 746 | func nf_dequant_all_layers(buf: *u8, hdr: *NxGgufHeader, wcache: *i64, N: i64, ne: i64, qd: i64, kvd: i64, fd: i64) -> i64 |
| 759 | func 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 |
| 813 | func _nfhdq_task(ctx_i: i64) -> i64 calls 1: dequant_row |
| 822 | func nf_dequant_head_all(buf: *u8, oh_base: i64, oh_ty: i64, vocab: i64, ne: i64) -> *i64 |
| 847 | func _nfhac_task(ctx_i: i64) -> i64 |
| 865 | func head_argmax_cached(hcp: *i64) -> i64 |
| 905 | func nf_narrow_i32(src: *i64, dst: *i32, cnt: i64) -> i64 called by 1: nf_dequant_all_layers_i32 |
| 918 | func _nfmm_i32_task(ctx_i: i64) -> i64 |
| 932 | func mm_pool_i32(inp: *i64, W: *i32, dst: *i64, T: i64, in_dim: i64, out_dim: i64, shift: i64) -> i64 called by 1: decode_step_kv_cached_i32 calls 4: nf_poolnx_pool_n_completednx_pool_submitnx_pool_wait |
| 954 | func nf_alloc_layer_wb_i32(ne: i64, qd: i64, kvd: i64, fd: i64) -> *i64 |
| 963 | func nf_dequant_all_layers_i32(buf: *u8, hdr: *NxGgufHeader, wc32: *i64, N: i64, ne: i64, qd: i64, kvd: i64, fd: i64) -> i64 called by 2: mainnsv_build_caches calls 5: nf_alloc_layer_wbnf_load_layer_poolnf_alloc_layer_wb_i32cpynf_narrow_i32 |
| 989 | func 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 |
| 1043 | func _nfhdq_i32_task(ctx_i: i64) -> i64 calls 1: dequant_row |
| 1051 | func nf_dequant_head_all_i32(buf: *u8, oh_base: i64, oh_ty: i64, vocab: i64, ne: i64) -> *i32 |
| 1088 | func _nfhac_i32_task(ctx_i: i64) -> i64 |
| 1107 | func _nfhlg_task(ctx_i: i64) -> i64 |
| 1121 | func head_logits_cached_i32(hlp: *i64) -> i64 called by 3: gdx_pickmainnsv_next_token calls 4: nf_poolnx_pool_n_completednx_pool_submitnx_pool_wait |
| 1145 | func head_argmax_cached_i32(hcp: *i64) -> i64 called by 6: mainmaingdx_pickmainmainnsv_next_token calls 4: nf_poolnx_pool_n_completednx_pool_submitnx_pool_wait |
| 1185 | func 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 } |
| 1186 | func nf_quant_w_i8(Wsrc: *i64, Wi8: *u8, sw: *i64, out_dim: i64, in_dim: i64) -> i64 |
| 1199 | func nf_alloc_layer_wb_i8(ne: i64, qd: i64, kvd: i64, fd: i64) -> *i64 |
| 1213 | func nf_dequant_all_layers_i8(buf: *u8, hdr: *NxGgufHeader, wc8: *i64, N: i64, ne: i64, qd: i64, kvd: i64, fd: i64) -> i64 |
| 1236 | func _nfmm_i8_task(ctx_i: i64) -> i64 calls 1: nf_hsum_sx |
| 1253 | func mm_pool_i8(x: *i64, Wi8: *u8, sw: *i64, dst: *i64, in_dim: i64, out_dim: i64, shift: i64) -> i64 called by 1: decode_step_kv_cached_i8 calls 7: nf_poolsys_mmapnf_pack2nx_pool_n_completednx_pool_submitnx_pool_wait+1 |
| 1283 | func 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 |