Skip to content

fix(embedding): frequent-token subsampling matches its cited CBM source (#184)#185

Merged
cdeust merged 2 commits into
mainfrom
fix/embedding-subsampling-source-184
Jul 25, 2026
Merged

fix(embedding): frequent-token subsampling matches its cited CBM source (#184)#185
cdeust merged 2 commits into
mainfrom
fix/embedding-subsampling-source-184

Conversation

@cdeust

@cdeust cdeust commented Jul 25, 2026

Copy link
Copy Markdown
Owner

Closes #184.

What actually diverged

mcp_server/shared/algorithmic_embedding.py::embed_text cited CBM's
CBM_SEM_MAX_OCCUR = 512 (semantic.h:52, applied in semantic.c:874 cooccur_sparse_one_target) for frequent-token subsampling, but implemented
different semantics on both axes the issue names:

CBM (cited source) Cortex as shipped
Trigger one token occurs >512 times the document exceeds 512 tokens
Scope that token's co-occurrence pass only the whole loop — first-order terms too

Mikolov et al. 2013's P(discard) = 1 - sqrt(t/f(w)) was never the mechanism in
play: it needs a corpus-relative f(w) this stateless per-text seam does not
have (the same reason IDF is excluded). CBM's mechanism is stride-sampling of a
frequent token's occurrence positions — inspired by word2vec subsampling, not
implementing its formula. The shipped code borrowed the constant and the
citation but applied a document-length cost bound the source does not describe.

Which fix path the evidence chose: (a) match the source

Measured on LongMemEval-S (n=50 haystack, 2556 memories):

Three-way retrieval comparison (benchmarks/results/subsample-fidelity-184/):

variant MRR Recall@10 vs off
off 0.3786 68.0%
current 0.3777 66.0% ΔMRR −0.0009, ΔR@10 −2.0 pp
faithful_a 0.3786 68.0% ΔMRR ±0.0000, ΔR@10 ±0.0 pp

The shipped document-striding does not merely fail to help — it hurts (−2.0
pp Recall@10), because it drops the first-order term at half the positions of the
~86% of memories over the cap. Chose (a): CBM-faithful. It restores §8 source
fidelity (code now matches the citation and the module docstring, which already
described the CBM semantics), scores best, and keeps a source-faithful cost guard
that genuinely fires (2/2556), which the acceptance criteria expect tested rather
than removed. Removal would score identically but delete a documented, firing
guard for no §8 benefit.

Benchmark deltas

Committed #169 sweep re-run on the fixed embedder (§8 — a changed embedder
invalidates recorded numbers; benchmarks/results/semantic-fallback-169/
updated, only the fallback row moves — no-vector/sentence-transformers
do not use this embedder):

mode before (#169) after (#184)
algorithmic fallback MRR 0.378, R@10 66.0% MRR 0.379, R@10 68.0%

Elapsed rose (38.6 → 62.9 s) because the co-occurrence pass is no longer strided
on long prose — an accepted trade for a non-hot-path encode whose vectors upgrade
to neural on the first consolidate.

Completion Ledger

Acceptance criterion (#184) Status
1. Decide + state semantics at the use site Done — option (a); # source: on _MAX_OCCUR states per-token trigger + co-occurrence scope
2. Docstring and code agree Done — module docstring already described (a); code now matches; Mikolov relationship clarified (inspiration, not formula)
3. Tests cover the branch (striding + boundary at exactly _MAX_OCCUR) Done — test_frequent_token_triggers_subsampling…, test_boundary_at_exactly_max_occur_does_not_stride, direction-preserved test
4. Negative assertion: normal memory → stride == 1 Done — test_normal_memory_takes_no_subsampling, test_first_order_terms_are_never_subsampled
5. Mutation run scoped to the module; stride branch the target Done — 0 survivors in _cooccurrence_pass/_first_order_pass/_accumulate_window; remaining survivors documented-equivalent or pre-existing in unchanged functions
6. Since (a), re-run committed #169 sweep + update manifest Done — semantic-fallback-169/ MANIFEST + JSON updated

Gates

§15 No-Deviation

Scope held to the one module, its test, and the two benchmark result dirs. No
unrelated files touched; pyproject.toml mutmut scope restored to its committed
value after the per-change run.

🤖 Generated with Claude Code

cdeust and others added 2 commits July 25, 2026 13:04
…ource (#184)

The algorithmic fallback embedder cited CBM_SEM_MAX_OCCUR (semantic.c:874
cooccur_sparse_one_target) but implemented different semantics: it strided the
WHOLE per-position loop whenever a *document* exceeded 512 tokens, dropping
first-order terms too. CBM strides only a *single token's* co-occurrence pass
when that *token* occurs >512 times, never the first-order term. A §8
source-fidelity defect.

On LongMemEval-S, 86.2% of memories exceed 512 tokens (median 1622), so the
document-striding fired on most stored vectors and measurably HURT retrieval
(-2.0 pp Recall@10 vs no striding). The CBM-faithful per-token trigger fires on
only 2/2556 memories — inert on prose, exactly as semantic.h:52 claims.

Fix (option (a)): first-order terms always complete; co-occurrence strided only
for a token whose within-document occurrence count exceeds _MAX_OCCUR. The code
now matches both the cited source and the module docstring (which already
described the CBM semantics). Extracted _first_order_pass / _cooccurrence_pass /
_accumulate_window seams.

Evidence (benchmarks/results/subsample-fidelity-184/, n=50):
  off        MRR 0.3786  R@10 68.0%
  current    MRR 0.3777  R@10 66.0%   (shipped — hurts)
  faithful_a MRR 0.3786  R@10 68.0%   (this fix)
Committed #169 sweep re-run on the fixed embedder: fallback 0.378/66% -> 0.379/68%.

Tests: 9 new (stride trigger, boundary at exactly _MAX_OCCUR, negative
stride==1 assertion, first-order-never-subsampled, direct window/first-order
contract). Scoped mutation (mutmut): 0 survivors in the stride/first-order/
window path; remaining survivors are documented-equivalent (internal dtype,
norm guard) or pre-existing in unchanged functions.

Closes #184

Co-Authored-By: Claude <noreply@anthropic.com>
…ree)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@cdeust
cdeust merged commit 8817374 into main Jul 25, 2026
14 checks passed
@cdeust
cdeust deleted the fix/embedding-subsampling-source-184 branch July 25, 2026 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: algorithmic embedding subsampling diverges from its cited CBM source, and the stride>1 branch is untested

1 participant