code wiki / (root) / nx_quant.nx

nx_quant.nx

buildroot/runtime/nx_quant.nx

25392 B661 linesdepth 2pulls 2 transitivereach 215 importersview sourcekind tooltopic quant
docsdependenciesstructsconstsfunctions

about

quant.nx -- quantization primitives for AI inference. Packs / unpacks tensor data between fp16/fp32 and the low-precision formats nxgguf supports (int8, int4, int2, ternary, fp8 E4M3). These are the actual VRAM-saving transforms that take a 32GB Llama 70B fp16 model down to 8GB int4 or 4GB int2. Reference quantization schemes: int8 SmoothQuant (Xiao et al. 2022) int4 GPTQ (Frantar et al. 2022) int4 AWQ (Lin et al. 2023) GGUF k-quants (llama.cpp project) BitNet b1.58 (Wang et al. 2024) -- ternary {-1, 0, +1} v0.0.1 ships symmetric quantization with per-tensor scale. k-quants (per-block scale + zero-point) follow in v0.1.0.

dependencies 1 imports · 2 importers

nx_syscalls.nx nx_quant.nx nx_parse.nx nx_parse_field_candidate_t280.nx

imports: nx_syscalls.nx

imported by: nx_parse.nxnx_parse_field_candidate_t280.nx

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

main fp32_to_fp16 fp16_to_fp32 pack_int4_pair unpack_int4_lo unpack_int4_hi pack_int2_quad unpack_int2_at ternary_decode ternary_encode

structs

none

consts

26const K_MAGIC_2046: i64 = 2046

functions

49func fp32_from_parts(whole: i64, frac_num: i64, frac_digits: i64) -> i64 {
125func fp64_from_parts(whole: i64, frac_num: i64, frac_digits: i64) -> i64 {
called by 1: main calls 1: fp64_from_ratio
143func fp64_from_ratio(num: i64, denom: i64, sticky_in: i64) -> i64 {
248func fp64_lsr(x: i64, s: i64) -> i64 {
called by 1: fp64_from_dec_wide
256func fp64_pow5(c: i64) -> i64 {
called by 1: fp64_from_dec_wide
267func fp64_from_sig(sig: i64, bexp: i64, sticky_in: i64) -> i64 {
called by 1: fp64_from_dec_wide
296func fp64_from_dec_wide(num: i64, e10: i64, sticky_in: i64) -> i64 {
339func fp64_from_dec(m: i64, e10: i64, sticky: i64) -> i64 {
376func fp64_to_fp32(bits64: i64) -> i64 {
430func fp32_to_fp16(bits32: i64) -> i64 {
called by 1: main
451func fp16_to_fp32(bits16: i64) -> i64 {
called by 1: main
483func fp32_to_fp8e4m3(bits32: i64) -> i64 {
508func find_max_abs_fp32(data: *u8, n: i64) -> i64 {
530func quant_fp32_to_int8(value_bits: i64, scale_bits: i64) -> i64 {
546func dequant_int8_to_fp32(q: i64, scale_bits: i64) -> i64 {
564func pack_int4_pair(lo: i64, hi: i64) -> i64 {
called by 2: pack_int4_arraymain
571func unpack_int4_lo(byte: i64) -> i64 {
called by 1: main
578func unpack_int4_hi(byte: i64) -> i64 {
called by 1: main
586func pack_int4_array(in_vals: *u8, n: i64, out: *u8) -> i64 {
calls 1: pack_int4_pair
602func pack_int2_quad(a: i64, b: i64, c: i64, d: i64) -> i64 {
called by 1: main
606func unpack_int2_at(byte: i64, idx: i64) -> i64 {
called by 1: main
611func ternary_encode(v: i64) -> i64 {
called by 1: main
616func ternary_decode(b: i64) -> i64 {
called by 1: main
624func main() -> i64 {