code wiki / _hdl_build / nx_f32_embed_gate.nx

nx_f32_embed_gate.nx source

↩ module page · 155 lines · 8094 B

1// nx_f32_embed_gate.nx -- KAT for the NEURAL EMBEDDING TAP (rung 1 of the retrieval ladder). 2// 3// WHY THIS GATE IS SHAPED THIS WAY. Our count-based PPMI reranker was measured on BEIR/nfcorpus at 255 4// permille against BM25's 305 -- a LOSS -- and the banked conclusion was: A GATE PROVING A MECHANISM ON 5// A FIXTURE HAS NOT MEASURED THE MODEL. So this gate deliberately does NOT assert "an embedding came out 6// non-zero"; that passes on noise. It asserts SEMANTIC ORDERING a count model cannot fake: the related 7// pair shares almost NO surface tokens (cat/kitten, sat/rested, mat/rug), so a lexical or 8// co-occurrence model has nothing to latch onto. Only learned semantics ranks it correctly. 9// 10// TEETH (both polarities -- a gate that can only pass is furniture): 11// 1 NON-VACUITY : the embedding is not all-zero 12// 2 SELF-SIM : cos(x,x) == 1000 permille -- the metric itself is sane 13// 3 SEMANTIC : related pair scores ABOVE unrelated pair <- the real assertion 14// 4 NEG-CONTROL : the unrelated pair must NOT also score near-1; if everything is similar to 15// everything the space has COLLAPSED and tooth 3 passes for the wrong reason 16// 5 DETERMINISM : same input twice = identical vector (integer f32, no hidden RNG) 17// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 18import "nx_syscalls.nx" 19import "nx_tier.nx" 20import "nx_bpe.nx" 21import "nx_gguf.nx" 22import "nx_gguf_load.nx" 23import "nx_gguf_meta.nx" 24import "nx_f32_llm_read_dims.nx" 25import "nx_f32_bpe_load.nx" 26import "nx_f32.nx" 27import "nx_f32_div.nx" 28import "nx_f32_llm_v4.nx" 29 30const EG_MAXTOK: i64 = 256 31const EG_PERMILLE: i64 = 1000 32// f32 rounding slack only -- not slack for error 33const EG_SELF_TOL: i64 = 2 34// collapse tripwire: if the UNRELATED pair also scores this high the space is degenerate and tooth 3 35// is vacuous. Derived from the collapse case (all cosines -> 1000), not from taste. 36const EG_COLLAPSE_PERMILLE: i64 = 990 37const EG_KV_SLOTS: i64 = 512 38// forward hyperparams -- byte-identical to the values the WORKING nx_f32_llm_serve seat uses 39const EG_EPS: i64 = 0x358637BD 40const EG_ATTN_SCALE: i64 = 0x3E000000 41const EG_ROPE_BASE: i64 = 0x415D0EAB 42const EG_VOCAB_BYTES: i64 = 67108864 43const EG_VOCAB_IDS: i64 = 262144 44const EG_VOCAB_MERGES: i64 = 524288 45 46func eg_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 47func eg_n(v: i64) -> i64 { 48 if v == 0 { eg_w("0" as *u8); return 0 } 49 var x: i64 = v 50 if x < 0 { eg_w("-" as *u8); x = 0 - x } 51 let b: *u8 = sys_mmap(32) 52 var i: i64 = 0 53 while x > 0 { b[i] = ((x % 10) + 48) as u8; x = x / 10; i = i + 1 } 54 while i > 0 { i = i - 1; sys_write(1, ((b as i64) + i) as *u8, 1) } 55 return 0 56} 57func eg_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 58 59// cosine in PERMILLE, integer-reproducible. Negative cosine clamps to 0 (rankings only need the 60// positive cone here, and a negative would print as a confusing large unsigned). 61func eg_cos(a: *i64, b: *i64, n: i64) -> i64 { 62 var dot: i64 = 0 63 var na: i64 = 0 64 var nb: i64 = 0 65 var i: i64 = 0 66 while i < n { 67 dot = nx_f32_add(dot, nx_f32_mul(a[i], b[i])) 68 na = nx_f32_add(na, nx_f32_mul(a[i], a[i])) 69 nb = nx_f32_add(nb, nx_f32_mul(b[i], b[i])) 70 i = i + 1 71 } 72 if nx_f32_is_zero(na) == 1 { return 0 - 1 } 73 if nx_f32_is_zero(nb) == 1 { return 0 - 1 } 74 let den: i64 = nx_f32_mul(nx_f32_sqrt(na), nx_f32_sqrt(nb)) 75 if nx_f32_is_zero(den) == 1 { return 0 - 1 } 76 let c: i64 = nx_f32_div(dot, den) 77 if nx_f32_sign(c) == 1 { return 0 } 78 var acc: i64 = 0 79 var k: i64 = 0 80 while k < EG_PERMILLE { acc = nx_f32_add(acc, c); k = k + 1 } 81 return f32_int(acc) 82} 83 84func main(argc: i64, argv: *i64) -> i64 { 85 var path: *u8 = "/volume1/homes/elderwesto/nx_bench/model/nx_real_model.gguf" as *u8 86 if argc > 1 { path = argv[1] as *u8 } 87 let len_out: *i64 = sys_mmap(8) as *i64 88 let buf: *u8 = sys_read_file(path, len_out) 89 if buf == (0 as *u8) { eg_w("{\"gate\":\"nx_f32_embed_gate\",\"refused\":\"no model at path\"}\n" as *u8); sys_exit(2); return 2 } 90 let hdr: *NxGgufHeader = sys_mmap(NX_GGUF_HDR_BYTES) as *NxGgufHeader 91 if nx_gguf_parse(buf, len_out[0], hdr) != NX_GGUF_OK { eg_w("{\"refused\":\"gguf parse\"}\n" as *u8); sys_exit(2); return 2 } 92 let model: *NxF32LlamaModel = nx_f32_llama_model_alloc() 93 let oe: *i64 = sys_mmap(8) as *i64 94 if nx_f32_llm_read_dims_from_gguf(buf, len_out[0], hdr, model, oe) != NX_FLD_OK { eg_w("{\"refused\":\"read_dims\"}\n" as *u8); sys_exit(2); return 2 } 95 if nx_f32_llm_load_weights_v4_from_gguf(buf, hdr, model, oe) != NX_FLV4_OK { eg_w("{\"refused\":\"load_weights\"}\n" as *u8); sys_exit(2); return 2 } 96 let vocab: *NxBpeVocab = nx_bpe_vocab_new(EG_VOCAB_BYTES, EG_VOCAB_IDS, EG_VOCAB_MERGES) 97 let nt: *i64 = sys_mmap(8) as *i64 98 let nm: *i64 = sys_mmap(8) as *i64 99 if nx_f32_bpe_load_from_gguf(buf, len_out[0], hdr, vocab, nt, nm, oe) != NX_FBL_OK { eg_w("{\"refused\":\"bpe load\"}\n" as *u8); sys_exit(2); return 2 } 100 101 let hd: i64 = model.hidden_dim 102 let vA: *i64 = sys_mmap(hd * 8) as *i64 103 let vB: *i64 = sys_mmap(hd * 8) as *i64 104 let vC: *i64 = sys_mmap(hd * 8) as *i64 105 let vA2: *i64 = sys_mmap(hd * 8) as *i64 106 107 let toks: *i64 = sys_mmap(EG_MAXTOK * 8) as *i64 108 var fail: i64 = 0 109 eg_w("{\"gate\":\"nx_f32_embed_gate\",\"hidden_dim\":" as *u8); eg_n(hd); eg_w(",\"cells\":[" as *u8) 110 111 var which: i64 = 0 112 while which < 4 { 113 var s: *u8 = "the cat sat on the mat" as *u8 114 var dst: *i64 = vA 115 if which == 1 { s = "a kitten rested on the rug" as *u8; dst = vB } 116 if which == 2 { s = "quarterly tax depreciation schedule" as *u8; dst = vC } 117 if which == 3 { dst = vA2 } 118 let n: nx_int = nx_bpe_encode_bytelevel(vocab, s, eg_slen(s) as nx_int, toks) 119 let cache: *NxF32KVCache = nx_f32_kv_cache_alloc(model.n_layers, model.n_kv_heads, EG_KV_SLOTS, model.head_dim) 120 let rv: nx_int = nx_f32_llm_embed_v4(model, toks, n, cache, EG_EPS, EG_ATTN_SCALE, EG_ROPE_BASE, 1, dst) 121 if rv != NX_FLV4_OK { eg_w("{\"embed_rc\":" as *u8); eg_n(rv); eg_w("}," as *u8); fail = fail + 1 } 122 which = which + 1 123 } 124 125 var nz: i64 = 0 126 var d: i64 = 0 127 while d < hd { if nx_f32_is_zero(vA[d]) == 0 { nz = nz + 1 } d = d + 1 } 128 eg_w("{\"tooth\":\"non_vacuity\",\"nonzero_dims\":" as *u8); eg_n(nz) 129 if nz == 0 { fail = fail + 1; eg_w(",\"verdict\":\"RED\"}" as *u8) } else { eg_w(",\"verdict\":\"GREEN\"}" as *u8) } 130 131 let self_c: i64 = eg_cos(vA, vA, hd) 132 eg_w(",{\"tooth\":\"self_sim\",\"permille\":" as *u8); eg_n(self_c) 133 if self_c < EG_PERMILLE - EG_SELF_TOL { fail = fail + 1; eg_w(",\"verdict\":\"RED\"}" as *u8) } else { eg_w(",\"verdict\":\"GREEN\"}" as *u8) } 134 135 let rel: i64 = eg_cos(vA, vB, hd) 136 let unrel: i64 = eg_cos(vA, vC, hd) 137 eg_w(",{\"tooth\":\"semantic_order\",\"related\":" as *u8); eg_n(rel) 138 eg_w(",\"unrelated\":" as *u8); eg_n(unrel) 139 if rel > unrel { eg_w(",\"verdict\":\"GREEN\"}" as *u8) } else { fail = fail + 1; eg_w(",\"verdict\":\"RED\"}" as *u8) } 140 141 eg_w(",{\"tooth\":\"neg_control_not_collapsed\",\"unrelated\":" as *u8); eg_n(unrel) 142 if unrel >= 0 && unrel < EG_COLLAPSE_PERMILLE { eg_w(",\"verdict\":\"GREEN\"}" as *u8) } else { fail = fail + 1; eg_w(",\"verdict\":\"RED -- space COLLAPSED, semantic tooth is vacuous\"}" as *u8) } 143 144 var same: i64 = 1 145 var q: i64 = 0 146 while q < hd { if vA[q] != vA2[q] { same = 0; q = hd } else { q = q + 1 } } 147 eg_w(",{\"tooth\":\"determinism\",\"identical\":" as *u8); eg_n(same) 148 if same == 1 && nz > 0 { eg_w(",\"verdict\":\"GREEN\"}" as *u8) } else { fail = fail + 1; eg_w(",\"verdict\":\"RED\"}" as *u8) } 149 150 eg_w("],\"failed\":" as *u8); eg_n(fail) 151 if fail == 0 { eg_w(",\"verdict\":\"GREEN -- neural embeddings are semantically ordered, non-collapsed and deterministic\"}\n" as *u8); return 0 } 152 eg_w(",\"verdict\":\"RED\"}\n" as *u8) 153 sys_exit(1) 154 return 1 155}