nx_recall_fuse_gate.nx source
↩ module page · 65 lines · 2807 B
1// nx_recall_fuse_gate.nx -- KAT + HONEST BENCH for nx_recall_fuse.
2// exit 0 = all pass; N = assertion N failed. Sovereign lane (nx_cc->nxasm).
3//
4// The bench is the point: it MEASURES the nDCG lift of hybrid fusion over BM25-alone
5// on the nx_ir_eval ruler, on a fixture that reproduces the live failure -- a
6// relevant doc buried by lexical BM25 but caught by the dense/semantic signal. A
7// green gate here = the SOTA rung moved a real number, not a claim.
8
9import "fx.nx"
10import "nx_ir_eval.nx"
11import "nx_recall_fuse.nx"
12import "syscalls.nx"
13
14func main() -> i64 {
15 let ncand: i64 = 5
16
17 // per-candidate graded relevance (id -> gain): doc3 highly relevant, doc2 mild.
18 let g: *i64 = sys_mmap(5 * 8) as *i64
19 g[0] = 0; g[1] = 0; g[2] = 1; g[3] = 3; g[4] = 0
20
21 // BM25 order: the relevant doc3 is BURIED at rank 4 (idx 3) -- the lexical-miss
22 // case (like "cross encoder reranking" -> JPEG/HLS above the IR doc).
23 let bm: *i64 = sys_mmap(5 * 8) as *i64
24 bm[0] = 0; bm[1] = 1; bm[2] = 2; bm[3] = 3; bm[4] = 4
25 // Dense/semantic order: doc3 at rank 1 -- the semantic signal catches it.
26 let dn: *i64 = sys_mmap(5 * 8) as *i64
27 dn[0] = 3; dn[1] = 2; dn[2] = 0; dn[3] = 1; dn[4] = 4
28
29 let sc: *i64 = sys_mmap(5 * 8) as *i64
30 rf_fuse(bm, 5, dn, 5, ncand, sc)
31
32 // 1: RRF is additive -- doc3 appears in BOTH (bm25 rank4 + dense rank1); its
33 // fused score is exactly 1/(60+4) + 1/(60+1) in Q16.16.
34 if sc[3] != fx_from_frac(1, 64) + fx_from_frac(1, 61) { return 1 }
35 // 2: doc4 is last in both rankers -> smallest fused score = 1/65 + 1/65.
36 if sc[4] != fx_from_frac(1, 65) + fx_from_frac(1, 65) { return 2 }
37
38 let ford: *i64 = sys_mmap(5 * 8) as *i64
39 rf_order_by_score(sc, ncand, ford)
40 // 3: fused order is a full permutation with the both-last doc4 at the bottom.
41 if ford[4] != 4 { return 3 }
42
43 // ---- HONEST BENCH: nDCG@5 lift, fused vs BM25-alone, on the R0 ruler ----
44 let gb: *i64 = sys_mmap(5 * 8) as *i64 // gains in BM25 rank order
45 var i: i64 = 0
46 while i < 5 { gb[i] = g[bm[i]]; i = i + 1 }
47 let gf: *i64 = sys_mmap(5 * 8) as *i64 // gains in FUSED rank order
48 i = 0
49 while i < 5 { gf[i] = g[ford[i]]; i = i + 1 }
50
51 let ndcg_bm: i64 = ie_ndcg_at_k(gb, 5, 5)
52 let ndcg_fs: i64 = ie_ndcg_at_k(gf, 5, 5)
53 // 4: fusion STRICTLY improves nDCG over BM25-alone -- the measured SOTA lift.
54 if ndcg_fs <= ndcg_bm { return 4 }
55 // 5: the relevant doc3 moved UP -- its fused rank index < its BM25 rank index (3).
56 var fr3: i64 = 0 - 1
57 i = 0
58 while i < 5 { if ford[i] == 3 { fr3 = i } i = i + 1 }
59 if fr3 >= 3 { return 5 }
60 // 6: both nDCG values are well-formed (in (0, 1.0]).
61 if ndcg_bm <= 0 { return 6 }
62 if ndcg_fs > fx_from_int(1) { return 7 }
63
64 return 0
65}