code wiki / _hdl_build / nx_bert_ce_gate.nx

nx_bert_ce_gate.nx source

↩ module page · 138 lines · 8791 B

1// nx_bert_ce_gate.nx -- teeth for the sovereign BERT cross-encoder (nx_bert_ce_lib over nx_wordpiece_lib) against the 2// PUBLISHED known answers on the model card of cross-encoder ms-marco-MiniLM-L-6-v2 (Apache-2.0, mirrored with 3// provenance under knowledge/fetched/cmp_search_ce_minilm_*): the card prints the scores 8.607138 and -4.320078 for 4// the Berlin population pair and the Berlin museums pair, so the encoder is held to an EXTERNAL number, never to 5// itself. Tolerance: f32 accumulation order differs from the reference framework and over six layers that drift stays 6// below 1e-2 on a logit; the bar is 50 milli, five times that and 86 times below the smaller published magnitude, so a 7// wrong weight, a wrong norm placement or a wrong tokenisation cannot pass. The pooled attention path must equal the 8// serial path bit for bit (the vit_mhsa_pool claim) and a forward must be deterministic. Search rung R0, 2026-09-14. 9// Declared in organ_gate.conf. expect_exit: 0 10import "nx_gate_verdict.nx" 11import "nx_estate_path.nx" 12import "nx_wordpiece_lib.nx" 13import "nx_bert_ce_lib.nx" 14import "nx_thread_pool.nx" 15 16const BG_EXPECT_POP_MILLI: i64 = 8607 // model card: 8.607138 17const BG_EXPECT_MUS_MAG_MILLI: i64 = 4320 // model card: -4.320078 (magnitude; negated in code) 18const BG_TOL_MILLI: i64 = 50 19const BG_BERLIN_QUERY_PIECES: i64 = 7 // how many people live in berlin ? 20const BG_CLS_ID: i64 = 101 // published bert-base-uncased special ids 21const BG_SEP_ID: i64 = 102 22const BG_UNK_ID: i64 = 100 23const BG_PAD_ID: i64 = 0 24const BG_POOL_WORKERS: i64 = 2 25const BG_BOX: i64 = 8 26 27func bg_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v } 28func bg_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 29 30func main() -> i64 { 31 ep_anchor() 32 gv_puts("=== nx_bert_ce gate (sovereign BERT cross-encoder vs the published model-card logits) ===\n" as *u8) 33 let ctr: *i64 = gv_ctr() 34 let bg: *i64 = sys_mmap(BC_G_BYTES) as *i64 35 bg[BC_G_POOL] = 0 36 let bok: i64 = bc_load(bg, "knowledge/fetched/cmp_search_ce_minilm_model.safetensors" as *u8, "knowledge/fetched/cmp_search_ce_minilm_config.json" as *u8) 37 if bok != 1 { gv_puts(" model load refused, missing: " as *u8); gv_puts(bg[BC_G_MISSING] as *u8); gv_puts("\n" as *u8) } 38 gv_check("T1 fixture-reached: config.json and model.safetensors load with every tensor present" as *u8, (bok == 1) as i64, ctr) 39 let wg: *i64 = sys_mmap(WP_G_BYTES) as *i64 40 let wok: i64 = wp_load(wg, "knowledge/fetched/cmp_search_ce_minilm_vocab.txt" as *u8) 41 gv_check("T2 vocab.txt loads and its token count equals the config vocab_size" as *u8, ((wok == 1) as i64) * ((wg[WP_G_NTOK] == bg[BC_G_VOCAB]) as i64), ctr) 42 var sp: i64 = 1 43 if wg[WP_G_CLS] != BG_CLS_ID { sp = 0 } 44 if wg[WP_G_SEP] != BG_SEP_ID { sp = 0 } 45 if wg[WP_G_UNK] != BG_UNK_ID { sp = 0 } 46 if wg[WP_G_PAD] != BG_PAD_ID { sp = 0 } 47 gv_check("T3 special ids are the published bert-base-uncased ids (CLS 101, SEP 102, UNK 100, PAD 0)" as *u8, sp, ctr) 48 let q: *u8 = "How many people live in Berlin?" as *u8 49 let qlen: i64 = bg_slen(q) 50 let qtok: *i64 = sys_mmap(qlen * 8) as *i64 51 let nq: i64 = wp_tokenize(wg, q, 0, qlen, qtok) 52 var nounk: i64 = 1 53 var qi: i64 = 0 54 while qi < nq { if qtok[qi] == wg[WP_G_UNK] { nounk = 0 } qi = qi + 1 } 55 gv_check("T4 tokeniser: the card's query yields exactly seven pieces and none is UNK" as *u8, ((nq == BG_BERLIN_QUERY_PIECES) as i64) * nounk, ctr) 56 let uw: *u8 = "hyperparameterization" as *u8 57 let ulen: i64 = bg_slen(uw) 58 let utok: *i64 = sys_mmap(ulen * 8) as *i64 59 let nu: i64 = wp_tokenize(wg, uw, 0, ulen, utok) 60 var cont: i64 = 0 61 if nu >= 2 { 62 let toff: *i64 = wg[WP_G_TOFF] as *i64 63 let tlen: *i64 = wg[WP_G_TLEN] as *i64 64 let vb: *u8 = wg[WP_G_VBUF] as *u8 65 let p1: i64 = toff[utok[1]] 66 if tlen[utok[1]] > 2 { if vb[p1] == (WP_HASH_BYTE as u8) { if vb[p1 + 1] == (WP_HASH_BYTE as u8) { cont = 1 } } } 67 } 68 gv_check("T5 wordpiece continuation: a long rare word splits and its second piece carries the two-byte prefix" as *u8, cont, ctr) 69 let d1: *u8 = "Berlin had a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers." as *u8 70 let d2: *u8 = "Berlin is well known for its museums." as *u8 71 let d1len: i64 = bg_slen(d1) 72 let d2len: i64 = bg_slen(d2) 73 let d1tok: *i64 = sys_mmap(d1len * 8) as *i64 74 let d2tok: *i64 = sys_mmap(d2len * 8) as *i64 75 let nd1: i64 = wp_tokenize(wg, d1, 0, d1len, d1tok) 76 let nd2: i64 = wp_tokenize(wg, d2, 0, d2len, d2tok) 77 let maxpos: i64 = bg[BC_G_MAXPOS] 78 let ids: *i64 = sys_mmap(maxpos * 8) as *i64 79 let types: *i64 = sys_mmap(maxpos * 8) as *i64 80 let t1: i64 = wp_pair(wg, qtok, nq, d1tok, nd1, maxpos, ids, types) 81 let l1: i64 = bc_forward(bg, ids, types, t1) 82 let m1: i64 = bc_f32_milli(l1) 83 gv_check("T6 KAT: the Berlin population pair scores within 50 milli of the card's 8.607" as *u8, (bg_abs(m1 - BG_EXPECT_POP_MILLI) <= BG_TOL_MILLI) as i64, ctr) 84 let t2: i64 = wp_pair(wg, qtok, nq, d2tok, nd2, maxpos, ids, types) 85 let l2: i64 = bc_forward(bg, ids, types, t2) 86 let m2: i64 = bc_f32_milli(l2) 87 gv_check("T7 KAT: the Berlin museums pair scores within 50 milli of the card's -4.320" as *u8, (bg_abs(m2 - (0 - BG_EXPECT_MUS_MAG_MILLI)) <= BG_TOL_MILLI) as i64, ctr) 88 gv_check("T8 neg-control-ordering: the relevant passage outscores the irrelevant one" as *u8, (m1 > m2) as i64, ctr) 89 let t1b: i64 = wp_pair(wg, qtok, nq, d1tok, nd1, maxpos, ids, types) 90 let l1b: i64 = bc_forward(bg, ids, types, t1b) 91 gv_check("T9 determinism: the same pair forwards to the identical f32 bits" as *u8, (l1b == l1) as i64, ctr) 92 let pool: *NxThreadPool = nx_pool_new(BG_POOL_WORKERS, 0) 93 bg[BC_G_POOL] = pool as i64 94 let t1c: i64 = wp_pair(wg, qtok, nq, d1tok, nd1, maxpos, ids, types) 95 let l1c: i64 = bc_forward(bg, ids, types, t1c) 96 bg[BC_G_POOL] = 0 97 nx_pool_shutdown(pool) 98 gv_check("T10 pooled attention and matmuls equal the serial path bit for bit" as *u8, (l1c == l1) as i64, ctr) 99 // R0 PLANNING INSTRUMENT (2026-09-16): the cost of ONE full-length pass is the number every rerank-run estimate needs and 100 // this gate never printed it -- a 16,150-pair nfcorpus rerank ran 8 hours on the NAS with no way to say how far it was. 101 // The document is the population sentence repeated until the pair fills maxpos positions (wp_pair truncates the document 102 // to the room left after CLS, the query and two SEP), forwarded once serial and once pooled; the bits must agree (T11) 103 // and the two wall times are published so a run's total can be derived from its pair count instead of guessed. 104 let dl: *u8 = sys_mmap(8192) 105 var dlo: i64 = 0 106 while dlo + d1len + 1 < 8000 { var ci: i64 = 0; while ci < d1len { dl[dlo] = d1[ci]; dlo = dlo + 1; ci = ci + 1 } dl[dlo] = 32 as u8; dlo = dlo + 1 } 107 let dltok: *i64 = sys_mmap(dlo * 8) as *i64 108 let ndl: i64 = wp_tokenize(wg, dl, 0, dlo, dltok) 109 let tl: i64 = wp_pair(wg, qtok, nq, dltok, ndl, maxpos, ids, types) 110 let tf0: i64 = sys_now_us() 111 let ll: i64 = bc_forward(bg, ids, types, tl) 112 let tf1: i64 = sys_now_us() 113 let pool2: *NxThreadPool = nx_pool_new(BG_POOL_WORKERS, 0) 114 bg[BC_G_POOL] = pool2 as i64 115 let tp0: i64 = sys_now_us() 116 let llp: i64 = bc_forward(bg, ids, types, tl) 117 let tp1: i64 = sys_now_us() 118 bg[BC_G_POOL] = 0 119 nx_pool_shutdown(pool2) 120 gv_check("T11 a full-length pair (every position filled) forwards to the same bits serial and pooled" as *u8, ((llp == ll) as i64) * ((tl == maxpos) as i64), ctr) 121 gv_values_head() 122 gv_kv("logit_population_milli" as *u8, m1) 123 gv_kv("logit_population_expected_milli" as *u8, BG_EXPECT_POP_MILLI) 124 gv_kv("logit_museums_milli" as *u8, m2) 125 gv_kv("logit_museums_expected_milli" as *u8, 0 - BG_EXPECT_MUS_MAG_MILLI) 126 gv_kv("tolerance_milli" as *u8, BG_TOL_MILLI) 127 gv_kv("pair1_tokens" as *u8, t1) 128 gv_kv("pair2_tokens" as *u8, t2) 129 gv_kv("query_pieces" as *u8, nq) 130 gv_kv("hidden" as *u8, bg[BC_G_HID]) 131 gv_kv("layers" as *u8, bg[BC_G_LAYERS]) 132 gv_kv("vocab" as *u8, wg[WP_G_NTOK]) 133 gv_kv("fulllen_pair_tokens" as *u8, tl) 134 gv_kv("fulllen_forward_us_serial" as *u8, tf1 - tf0) 135 gv_kv("fulllen_forward_us_pooled" as *u8, tp1 - tp0) 136 gv_kv("pool_workers" as *u8, BG_POOL_WORKERS) 137 return gv_verdict("NX-BERT-CE-GATE" as *u8, ctr, "a sovereign BERT cross-encoder composed from the estate's f32 tower reproduces the model card's published logits for both Berlin pairs within 50 milli, orders them correctly, is deterministic, and its pooled attention equals the serial path bit for bit" as *u8) 138}