code wiki / (root) / nx_semruler.nx

nx_semruler.nx source

↩ module page · 299 lines · 14991 B

1// nx_semruler.nx -- THE SEMANTIC RULER, STEP 2: measure the EXISTING descriptors on a real 2// same-concept corpus. license_tier: ORIGINAL 3// 4// nx_imgsearch publishes its own gap honestly and names the order of work: 5// {"class":"semantic-lookalike","permille":-1,"status":"UNMEASURED", 6// "note":"... (1) build a semantic ruler from labelled same-concept pairs, (2) measure the EXISTING 7// descriptors as the honest baseline, (3) only then a quantised learned image embedding."} 8// nx_semruler_lib is step (1)'s decision core. THIS is step (2), and it deliberately stops short of 9// step (3): it builds NO new descriptor and trains nothing. 10// 11// WHERE THE LABELS COME FROM, AND WHY THEY ARE HONEST. knowledge/media/ is one directory per subject 12// (diora_baird, julia_kyoka, barack_obama...). Two images in the same directory are the SAME CONCEPT; 13// two in different directories are not. That is a weak label derived from the estate's own filing, not 14// an opinion, and it is the standard way a retrieval corpus is bootstrapped. NO LABEL IS INVENTED HERE. 15// 16// THE ONE NUMBER THIS EXISTS TO PRODUCE, and why it has to come first: 17// A "SAME-CONCEPT" PAIR THAT IS ACTUALLY A NEAR-DUPLICATE MEASURES THE COPY TIER, NOT SEMANTICS. 18// Before anyone can report a semantic recall figure, they have to know how much of the same-subject 19// corpus is genuinely different photographs and how much is re-uploads, re-crops and re-encodes. Run 20// a recall benchmark over an unfiltered folder of one person and it will score near 1000 permil and 21// "prove" the engine is already semantic -- the number real, the subject wrong. 22// So this organ reports the ADMISSIBILITY CENSUS: of all within-subject pairs, how many survive 23// sr_pair_admissible (distinct photographs) and how many are duplicates under some dihedral 24// transform. THE SURVIVING SET IS THE ONLY VALID DENOMINATOR for step 3. 25// 26// AND THE CONTROL THAT KEEPS IT HONEST. If the within-subject inadmissible count is ZERO, this corpus 27// contains no duplicates, the predicate was never exercised on real data, and the census says so rather 28// than reporting a clean sweep. Cross-subject pairs are sampled as the opposite control: they should be 29// overwhelmingly ADMISSIBLE, because two different people are never near-duplicates -- if they are not, 30// the distance is not measuring what it claims and no semantic figure built on it would mean anything. 31import "nx_syscalls.nx" 32import "nx_dir.nx" 33import "nx_image_gray.nx" 34import "nx_imgsearch_tier.nx" 35import "nx_semruler_lib.nx" 36 37const SR_MAXCLASS: i64 = 32 38const SR_MAXIMG: i64 = 48 // per-class image cap -- ANNOUNCED, never silent (see coverage line) 39const SR_PATHCAP: i64 = 1024 40const SR_ROWCAP: i64 = 512 41const SR_ARENA: i64 = 262144 42const SR_XPAIRS: i64 = 64 // cross-subject control pairs 43const SR_DESCCAP: i64 = 16 // i64 slots per descriptor (copy=1, orient index=1, orient query=8) 44 45func sw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 46func sn(v: i64) -> i64 { 47 let t: *u8 = sys_mmap(32) 48 var m: i64 = v 49 if m < 0 { sw("-" as *u8); m = 0 - m } 50 var k: i64 = 0 51 if m == 0 { t[0] = 48 as u8; k = 1 } 52 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 53 let b: *u8 = sys_mmap(32) 54 var j: i64 = 0 55 while j < k { b[j] = t[k - 1 - j]; j = j + 1 } 56 sys_write(1, b, k) 57 return 0 58} 59func swn(p: *u8, n: i64) -> i64 { sys_write(1, p, n); return 0 } 60 61func sr_is_image(name: *u8, nl: i64) -> i64 { 62 if nl < 5 { return 0 } 63 var i: i64 = nl - 1 64 while i > 0 { 65 if name[i] == (46 as u8) { 66 // .jpg .png .gif (webp/avif are a declared decoder gap in nx_imgsearch status) 67 if name[i+1] == (106 as u8) { return 1 } 68 if name[i+1] == (112 as u8) { return 1 } 69 if name[i+1] == (103 as u8) { return 1 } 70 return 0 71 } 72 i = i - 1 73 } 74 return 0 75} 76 77func sr_join(a: *u8, b: *u8, out: *u8) -> i64 { 78 var o: i64 = 0 79 var i: i64 = 0 80 while a[i] != (0 as u8) { out[o] = a[i]; o = o + 1; i = i + 1 } 81 out[o] = 47 as u8; o = o + 1 82 i = 0 83 while b[i] != (0 as u8) { out[o] = b[i]; o = o + 1; i = i + 1 } 84 out[o] = 0 as u8 85 return o 86} 87 88func main(argc: i64, argv: *i64) -> i64 { 89 var root: *u8 = "knowledge/media" as *u8 90 if argc > 1 { root = argv[1] as *u8 } 91 92 sw("=== nx_semruler -- ADMISSIBILITY CENSUS over a real same-concept corpus ===\n" as *u8) 93 sw("root=" as *u8); sw(root); sw("\n" as *u8) 94 sw("labels: one directory per subject; same dir = same concept. No label is invented.\n" as *u8) 95 96 // the two tiers whose distances define "is this the same photograph" 97 let tcopy: *nx_imgtier = nx_imgtier_new(NX_IT_KIND_COPY, NX_IT_COPY_THRESH, 1000) 98 let torient: *nx_imgtier = nx_imgtier_new(NX_IT_KIND_ORIENT, NX_IT_COPY_THRESH, 1000) 99 100 let rows: *NxDirRow = sys_mmap(NX_DIR_ROW_BYTES * SR_ROWCAP) as *NxDirRow 101 let arena: *u8 = sys_mmap(SR_ARENA) 102 let res: *NxDirResult = sys_mmap(NX_DIR_RESULT_BYTES) as *NxDirResult 103 let dv: i64 = nx_dir_list(root, rows, SR_ROWCAP, arena, SR_ARENA, 0, res) 104 if dv == NX_DIR_OPEN_FAILED { 105 sw("ROOT UNREADABLE -- no conclusion available in either direction\n" as *u8) 106 sys_exit(3); return 3 107 } 108 109 // per-class descriptor stores. cd[] = copy index desc, od[] = orient index desc, oq[] = orient query desc 110 let cd: *i64 = sys_mmap(8 * SR_MAXIMG * SR_MAXCLASS) as *i64 111 let od: *i64 = sys_mmap(8 * SR_MAXIMG * SR_MAXCLASS) as *i64 112 let oq: *i64 = sys_mmap(8 * SR_MAXIMG * SR_MAXCLASS * 8) as *i64 113 let ccount: *i64 = sys_mmap(8 * SR_MAXCLASS) as *i64 114 let cnameoff: *i64 = sys_mmap(8 * SR_MAXCLASS) as *i64 115 let cnamelen: *i64 = sys_mmap(8 * SR_MAXCLASS) as *i64 116 117 let subrows: *NxDirRow = sys_mmap(NX_DIR_ROW_BYTES * SR_ROWCAP) as *NxDirRow 118 let subarena: *u8 = sys_mmap(SR_ARENA) 119 let subres: *NxDirResult = sys_mmap(NX_DIR_RESULT_BYTES) as *NxDirResult 120 let dirpath: *u8 = sys_mmap(SR_PATHCAP) 121 let filepath: *u8 = sys_mmap(SR_PATHCAP) 122 let wh: *i64 = sys_mmap(16) as *i64 123 let dbuf: *i64 = sys_mmap(8 * SR_DESCCAP) as *i64 124 // HOISTED 2026-09-01: these three were allocated PER PAIR inside the inner loop, which on a 48-image 125 // class is over three thousand mmaps and blew the allocator arena (ARENA-OVERRUN on first live run). 126 // NEVER ALLOCATE IN A HOT LOOP -- pass the buffer in. The loop body only ever writes slot 0 before 127 // reading it, so one buffer per role is sufficient and carries no state between iterations. 128 let qc: *i64 = sys_mmap(8) as *i64 129 let dc: *i64 = sys_mmap(8) as *i64 130 let doo: *i64 = sys_mmap(8) as *i64 131 132 var nclass: i64 = 0 133 var total_seen: i64 = 0 134 var total_used: i64 = 0 135 var total_undecodable: i64 = 0 136 var any_capped: i64 = 0 137 138 var r: i64 = 0 139 while r < res.n_filled { 140 if nclass >= SR_MAXCLASS { r = res.n_filled } else { 141 let row: *NxDirRow = nx_dir_row_at(rows, r) 142 if nx_dir_row_is_regular_file(row) == 0 { 143 // a directory: one subject class. skip dot-entries and the _sheets helper dir. 144 if row.name_ptr[0] != (46 as u8) { 145 if row.name_ptr[0] != (95 as u8) { 146 sr_join(root, row.name_ptr, dirpath) 147 let sv: i64 = nx_dir_list(dirpath, subrows, SR_ROWCAP, subarena, SR_ARENA, 0, subres) 148 if sv != NX_DIR_OPEN_FAILED { 149 var used: i64 = 0 150 var seen: i64 = 0 151 var sr2: i64 = 0 152 while sr2 < subres.n_filled { 153 let frow: *NxDirRow = nx_dir_row_at(subrows, sr2) 154 if nx_dir_row_is_regular_file(frow) == 1 { 155 if sr_is_image(frow.name_ptr, frow.name_len) == 1 { 156 seen = seen + 1 157 if used < SR_MAXIMG { 158 sr_join(dirpath, frow.name_ptr, filepath) 159 let g: *u8 = nx_img_to_gray(filepath, wh) 160 if (g as i64) == 0 { total_undecodable = total_undecodable + 1 } else { 161 let base: i64 = nclass * SR_MAXIMG + used 162 nx_imgtier_describe_index(tcopy, g, 0 as *u8, wh[0], wh[1], dbuf) 163 cd[base] = dbuf[0] 164 nx_imgtier_describe_index(torient, g, 0 as *u8, wh[0], wh[1], dbuf) 165 od[base] = dbuf[0] 166 nx_imgtier_describe_query(torient, g, 0 as *u8, wh[0], wh[1], dbuf) 167 var k: i64 = 0 168 while k < 8 { oq[base * 8 + k] = dbuf[k]; k = k + 1 } 169 used = used + 1 170 } 171 } 172 } 173 } 174 sr2 = sr2 + 1 175 } 176 if seen > used { any_capped = 1 } 177 if used > 1 { 178 ccount[nclass] = used 179 cnameoff[nclass] = row.name_ptr as i64 180 cnamelen[nclass] = row.name_len 181 total_seen = total_seen + seen 182 total_used = total_used + used 183 nclass = nclass + 1 184 } 185 } 186 } 187 } 188 } 189 r = r + 1 190 } 191 } 192 193 if nclass < 1 { 194 sw("NO SUBJECT CLASS WITH >1 DECODABLE IMAGE -- the census has no population and REFUSES to\n" as *u8) 195 sw("publish a rate. This is not a pass: a ruler over an empty set proves nothing.\n" as *u8) 196 sys_exit(3); return 3 197 } 198 199 sw("\ncoverage: classes=" as *u8); sn(nclass) 200 sw(" images_seen=" as *u8); sn(total_seen) 201 sw(" images_used=" as *u8); sn(total_used) 202 sw(" undecodable=" as *u8); sn(total_undecodable) 203 sw(" per_class_cap=" as *u8); sn(SR_MAXIMG) 204 sw(" capped=" as *u8); sn(any_capped) 205 if any_capped == 1 { sw(" <== THE CAP BOUND: counts below are a FLOOR over the used set, not the whole corpus" as *u8) } 206 sw("\n (webp/avif are excluded here because they are a DECLARED decoder gap in nx_imgsearch status,\n" as *u8) 207 sw(" not because they were sampled away -- undecodable files are counted above, never dropped.)\n\n" as *u8) 208 209 // ---- WITHIN-SUBJECT: the census that decides whether a semantic benchmark is even possible ---- 210 var tot_pairs: i64 = 0 211 var tot_adm: i64 = 0 212 var tot_dup: i64 = 0 213 var ci: i64 = 0 214 while ci < nclass { 215 let n: i64 = ccount[ci] 216 var pairs: i64 = 0 217 var adm: i64 = 0 218 var a: i64 = 0 219 while a < n { 220 var b: i64 = a + 1 221 while b < n { 222 let ba: i64 = ci * SR_MAXIMG + a 223 let bb: i64 = ci * SR_MAXIMG + b 224 qc[0] = cd[ba] 225 dc[0] = cd[bb] 226 let cdist: i64 = nx_imgtier_distance(tcopy, qc, dc) 227 doo[0] = od[bb] 228 let odist: i64 = nx_imgtier_distance(torient, (oq as i64 + ba * 64) as *i64, doo) 229 pairs = pairs + 1 230 if sr_pair_admissible(cdist, odist) == 1 { adm = adm + 1 } 231 b = b + 1 232 } 233 a = a + 1 234 } 235 sw(" class " as *u8); swn(cnameoff[ci] as *u8, cnamelen[ci]) 236 sw(" images=" as *u8); sn(n) 237 sw(" pairs=" as *u8); sn(pairs) 238 sw(" admissible=" as *u8); sn(adm) 239 sw(" duplicate=" as *u8); sn(pairs - adm) 240 sw(" admissible_permil=" as *u8); sn(sr_rate_permil(adm, pairs)) 241 sw("\n" as *u8) 242 tot_pairs = tot_pairs + pairs 243 tot_adm = tot_adm + adm 244 tot_dup = tot_dup + (pairs - adm) 245 ci = ci + 1 246 } 247 248 // ---- CROSS-SUBJECT CONTROL: two different people must not read as near-duplicates ---- 249 var xp: i64 = 0 250 var xadm: i64 = 0 251 if nclass > 1 { 252 var i2: i64 = 0 253 while i2 < SR_XPAIRS { 254 let c1: i64 = i2 % nclass 255 var c2: i64 = (c1 + 1) % nclass 256 let a: i64 = i2 % ccount[c1] 257 let b: i64 = i2 % ccount[c2] 258 let ba: i64 = c1 * SR_MAXIMG + a 259 let bb: i64 = c2 * SR_MAXIMG + b 260 qc[0] = cd[ba] 261 dc[0] = cd[bb] 262 let cdist: i64 = nx_imgtier_distance(tcopy, qc, dc) 263 doo[0] = od[bb] 264 let odist: i64 = nx_imgtier_distance(torient, (oq as i64 + ba * 64) as *i64, doo) 265 xp = xp + 1 266 if sr_pair_admissible(cdist, odist) == 1 { xadm = xadm + 1 } 267 i2 = i2 + 1 268 } 269 } 270 271 sw("\nWITHIN-SUBJECT TOTAL pairs=" as *u8); sn(tot_pairs) 272 sw(" admissible=" as *u8); sn(tot_adm) 273 sw(" duplicate=" as *u8); sn(tot_dup) 274 sw(" admissible_permil=" as *u8); sn(sr_semantic_permil(tot_adm, tot_pairs)) 275 sw("\nCROSS-SUBJECT CONTROL pairs=" as *u8); sn(xp) 276 sw(" admissible=" as *u8); sn(xadm) 277 sw(" admissible_permil=" as *u8); sn(sr_rate_permil(xadm, xp)) 278 sw("\n\n" as *u8) 279 280 // ---- what the numbers MEAN, stated so a reader cannot take the wrong one ---- 281 sw("READ THIS BEFORE QUOTING ANY NUMBER ABOVE:\n" as *u8) 282 sw(" admissible pairs ARE the valid denominator for a semantic recall benchmark (step 3).\n" as *u8) 283 sw(" duplicate pairs would be scored by the COPY tiers and must never enter that denominator.\n" as *u8) 284 if tot_dup == 0 { 285 sw(" ** THE PREDICATE WAS NOT EXERCISED: zero within-subject duplicates were found, so this\n" as *u8) 286 sw(" corpus does not demonstrate the guard firing on real data. Do not read 1000 permil as\n" as *u8) 287 sw(" evidence the guard works -- read it as evidence this corpus has no duplicates.\n" as *u8) 288 } 289 if xp > 0 { 290 if xadm < xp { 291 sw(" ** CONTROL ANOMALY: some CROSS-SUBJECT pairs read as near-duplicates. Two different\n" as *u8) 292 sw(" subjects should never be the same photograph, so the distance is not measuring what\n" as *u8) 293 sw(" it claims and NO semantic figure built on it is trustworthy until this is explained.\n" as *u8) 294 } 295 } 296 sw(" the semantic-lookalike figure in nx_imgsearch status stays -1 UNMEASURED after this run:\n" as *u8) 297 sw(" this organ measures the CORPUS, not the ENGINE. Step 3 is the ranking benchmark.\n" as *u8) 298 return 0 299}