M9 refinement: operate geometry descriptor in mean-centered (isotropic) embedding space

The nomic-embed-text space over the corpus is strongly anisotropic (mean
pairwise cosine ~0.55), which compresses cosine-based domain separation almost
to nothing so the design-doc s5 operators (distance/overlap/Wasserstein) cannot
discriminate. Subtracting the global mean of the normalized embeddings restores
isotropy (mean pairwise cosine ~0) and sharpens the operators.

- add GeoMeanCache (engram_geo_mean_build / _maybe_refresh / _vec / _free): a
  store-derived centering offset over the embed-eligible set, cached and
  refreshed on significant drift; lives in geometry.c, not the store.
- engram_geometry_descriptor gains an optional global_mean: when supplied the
  centroid, per-member cosine distance, and co-registration run in centered
  space (GM=zeros reproduces the legacy raw path exactly).
- co-registration choice (b): the ANN query stays in raw unit space (index
  unchanged) since centering is a rigid translation that ~preserves neighborhood
  membership; only the descriptor statistics move to the centered frame.
  Covariance/axes/radius are translation-invariant and therefore unchanged.
- test: synthetic ground-truth suite stays green (PERF + ASan/UBSan), plus new
  centered/raw/mean-cache assertions.
- add bench_discrimination.c (env-gated, read-only, skips in CI): on a copy of
  the real store the two-domain overlap operator drops 1.13 -> 0.008 and
  cross-centroid cosine 0.899 -> 0.003 after centering, Euclid distance
  unchanged (translation-invariant control).

No change to activation/retrieval behavior; wiring geometry into retrieval is a
separate, behavior-changing cutover.
This commit is contained in:
2026-08-12 19:54:41 -05:00
parent 2a4c5c645a
commit 8cae0f94eb
5 changed files with 355 additions and 23 deletions
+26 -8
View File
@@ -78,18 +78,31 @@ int main(void){
/* seed inside cluster A -> expect an A-dominated neighborhood */
st=store_open(path);
/* global-mean cache over the embedded set: the centering offset */
GeoMeanCache* mc=engram_geo_mean_build(st);
const float* gm=engram_geo_mean_vec(mc);
CHECK(mc!=NULL && engram_geo_mean_dim(mc)==DIM, "global-mean cache built over embedded set");
CHECK(engram_geo_mean_count(mc)==(uint64_t)(NA+NB), "global mean averaged all embedded nodes");
const char* seeds[1]={aids[0]};
GeoDescriptor* g=engram_geometry_descriptor(st, ix, ids, nids, seeds, 1, &P);
/* CENTERED descriptor: pass the global mean so geometry runs in isotropic space */
GeoDescriptor* g=engram_geometry_descriptor(st, ix, ids, nids, seeds, 1, &P, gm);
CHECK(g!=NULL, "descriptor computed");
if(g){
printf(" members=%d embedded=%d edges=%d k_core=%d radius=%.4f co_reg=%.3f n_axes=%d\n",
g->n_members,g->n_embedded,g->n_edges,g->k_core,g->radius,g->co_registration,g->n_axes);
/* centroid near cluster-A center (+e0): centroid[0] should dominate */
int argmax=0; for(int d=1;d<g->dim;d++) if(fabsf(g->centroid[d])>fabsf(g->centroid[argmax])) argmax=d;
printf(" centroid dominant axis = %d (expect 0), centroid[0]=%.3f centroid[1]=%.3f\n",
/* geometry ran in CENTERED space: g->centroid is the centered centroid,
* g->global_mean the applied offset. Reconstruct the raw prototype
* (centroid + global_mean) and check it sits on cluster-A's axis. */
CHECK(g->global_mean!=NULL, "descriptor recorded the centering offset (centered mode)");
int argmax=0; float best=-1.f;
for(int d=0;d<g->dim;d++){ float raw=g->centroid[d]+(g->global_mean?g->global_mean[d]:0.f);
if(fabsf(raw)>best){ best=fabsf(raw); argmax=d; } }
printf(" raw-prototype dominant axis = %d (expect 0); centered c[0]=%.3f c[1]=%.3f\n",
argmax, g->centroid[0], g->centroid[1]);
CHECK(argmax==0, "centroid sits on cluster-A's axis (near members)");
CHECK(argmax==0, "raw prototype sits on cluster-A's axis (near members)");
/* centering pushes A off cluster-B's axis: centered c[0] > c[1] */
CHECK(g->centroid[0] > g->centroid[1], "centered centroid leans off B's axis (isotropy)");
/* hub should be A0 (the intra-A hub with NA-1 strong edges) */
CHECK(g->hub_id && strcmp(g->hub_id,"A0")==0, "hub = the relational center A0");
@@ -128,12 +141,17 @@ int main(void){
engram_geo_free(g);
/* edge cases: NULL store, no seeds, relational-only (NULL vindex) */
CHECK(engram_geometry_descriptor(NULL,ix,ids,nids,seeds,1,&P)==NULL, "NULL store -> NULL");
CHECK(engram_geometry_descriptor(st,ix,ids,nids,seeds,0,&P)==NULL, "zero seeds -> NULL");
GeoDescriptor* g2=engram_geometry_descriptor(st, NULL, NULL, 0, seeds, 1, &P);
CHECK(engram_geometry_descriptor(NULL,ix,ids,nids,seeds,1,&P,gm)==NULL, "NULL store -> NULL");
CHECK(engram_geometry_descriptor(st,ix,ids,nids,seeds,0,&P,gm)==NULL, "zero seeds -> NULL");
GeoDescriptor* g2=engram_geometry_descriptor(st, NULL, NULL, 0, seeds, 1, &P, gm);
CHECK(g2!=NULL && g2->n_members>=NA-1, "relational-only path (no vindex) works");
engram_geo_free(g2);
/* raw (uncentered) mode still supported: global_mean=NULL -> no offset recorded */
GeoDescriptor* g3=engram_geometry_descriptor(st, ix, ids, nids, seeds, 1, &P, NULL);
CHECK(g3!=NULL && g3->global_mean==NULL, "raw mode (global_mean=NULL) leaves offset unset");
engram_geo_free(g3);
engram_geo_mean_free(mc);
for(int i=0;i<nids;i++) free(ids[i]); free(ids);
vindex_free(ix); store_close(st); unlink(path);
printf("\n=== %s ===\n", g_fail?"FAILURES PRESENT":"ALL TESTS PASSED");