fix(embedding): mean-centre the neural space so semantic search actually works - #329
Merged
Conversation
SMK1705
commented
Jul 28, 2026
Owner
…lly works (#316) The on-device run of the opt-in neural embedder found it useless: the SAME handful of notes surfaced for completely unrelated queries, and genuinely-related items didn't. Instrumenting the cosine distribution on-device showed why — USE is strongly ANISOTROPIC: every note scored 0.6–0.95 to ANY query (mean ~0.78), so a few "hub" vectors sit close to everything and dominate. The searchFloor was a red herring; no threshold can fix a space where the hubs out-score the real match for every query. Fix: mean-centre the vectors before comparing. Subtracting the corpus centroid removes the shared hub direction so the residuals discriminate — the standard remedy for anisotropic sentence embeddings. - Embedder.meanCenter (default false); MediaPipeEmbedder sets it true, HashingEmbedder (isotropic) stays off; RoutingEmbedder delegates. - SemanticIndex.scores() centres the query and each note by the SAME corpus mean and re-normalises when the active embedder asks for it. Search-time, so no stored state and no migration; the mean is always consistent with the current corpus. - searchFloor recalibrated for the CENTRED space: 0.55 (raw) -> 0.50. On-device the centred distribution spreads to ~[-0.4, +0.9], mean ~0; genuine zero-overlap matches land ~0.53–0.77, noise is near zero, so 0.50 keeps the matches and drops the bulk. Verified on device (Galaxy S25): before, "kitchen sink tap" returned unrelated finance notes and missed the plumber note; after, it returns exactly "Call the plumber about the leak" — a real semantic match with zero shared words, the thing USE was added for. Post-centre distribution mean fell 0.78 -> ~0.01 and per-query top results became distinct and relevant. Tests: SemanticIndexTest gains an anisotropic stand-in embedder and proves the mechanism — without centring an unrelated note scores >0.95 and the true match beats it by <0.05; with centring the match stays >0.8 and unrelated notes collapse below 0.1. Residual limitation (noted on #316): a couple of broadly-similar "hub" notes still sit ~0.5–0.7 after centring; full "all-but-the-top" PC removal would tighten it further, but that's over-investment for an opt-in fallback.
…arger APK CI's packageDebug hit java.lang.OutOfMemoryError at the 2 GB daemon heap now that the APK is ~199 MB — the MediaPipe tasks-text native runtime added in #328 pushed packaging over the edge (#328 passed narrowly; the next build tipped it). GitHub runners have ~7 GB, so 4 GB leaves comfortable headroom.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.