Skip to content

fix(api): optimize MMR selection by removing redundant rescans - #2615

Merged
endxxxx merged 1 commit into
MemTensor:dev-v2.0.37from
yiwang-one:fix/mmr-loop-incremental-max
Nov 17, 2026
Merged

endxxxx merged 1 commit into
MemTensor:dev-v2.0.37from
yiwang-one:fix/mmr-loop-incremental-max

Conversation

@yiwang-one

Copy link
Copy Markdown

Description

What & Why

The MMR selection loop in _mmr_dedup_text_memories computed each
candidate's maximum similarity to the selected set twice per round
(once inside the text-similarity gate, once for the diversity penalty),
each time by rescanning the whole selected set with Python-level numpy
element access. It also recomputed strip() on every memory in every
round. With dozens of rounds over dozens of candidates, this redundant
work dominated the dedup stage.

Approach

  • Maintain the per-candidate maximum similarity to the selected set
    incrementally: after each selection, one vectorized np.maximum
    against the new column updates running_max. Since this performs
    only elementwise comparisons (no arithmetic reordering), every read
    is bit-identical to the previous full rescan.
  • The text-similarity gate accepts the precomputed maximum via an
    optional max_sim parameter; when it is not provided the gate
    behaves exactly as before. The index lookup inside the gate stays
    lazy and only runs on the rare high-similarity path, preserving the
    original tie-break order.
  • Precompute stripped texts once (strip is a pure function of each
    memory).

Deliberately out of scope (they would change results): enabling
candidate pruning, changing the similarity-matrix construction or the
penalty arithmetic (float summation-order effects), or changing the
remaining set iteration order.

No new dependencies (numpy is already a core dependency).

中文说明

_mmr_dedup_text_memories 的 MMR 选择循环此前对每个候选、每轮都对已选
集合全量重扫两次最大相似度(文本相似门控一次、多样性惩罚一次),并在
每轮对每条记忆重复执行 strip()。本 PR 改为:入选后用一次向量化的
np.maximum 增量维护每候选对已选集的最大相似度(纯比较运算,每次读取与
全量重扫逐位一致);门控接受预计算的最大值、索引查找保持惰性且平局
裁决顺序不变;strip 一次性预计算。选择结果完全不变;候选剪枝、矩阵
构建与惩罚算术均刻意未触碰。无新增依赖。

Equivalence: unit tests compare the optimized implementation against
an inline reference that rescans and recomputes every round, across
adversarial batches — near-duplicate embedding/text pairs, exact
duplicate texts, score ties, negative similarities (exercising the
-inf initialisation), and bucket capacity pressure. Outputs are
deep-compared bucket by bucket, in order.

Related Issue (Required): N/A — performance fix with no functional change

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Unit Test
  • Test Script Or Steps (please provide)

5 tests in tests/api/test_search_handler_mmr.py (new file):
mixed adversarial batches over three seeds, all-similar batch,
negative-similarity batch, single-memory early return, and a direct
bit-identity check of the incremental max maintenance against full
rescans.

Reproduce (repo root):

PYTHONPATH=src python -m pytest tests/api/test_search_handler_mmr.py -q
# => 5 passed

PYTHONPATH=src python -m pytest tests/api/ tests/memories/textual/ -q
# => 239 passed

ruff format --check src/memos/api/handlers/search_handler.py tests/api/test_search_handler_mmr.py
# => already formatted

Checklist

  • I have performed a self-review of my own code | 我已自行检查了自己的代码
  • I have commented my code in hard-to-understand areas | 我已在难以理解的地方对代码进行了注释
  • I have added tests that prove my fix is effective or that my feature works | 我已添加测试以证明我的修复有效或功能正常
  • MemOS-Docs not applicable — internal performance fix, no public API change | 不适用——内部性能修复,无公共 API 变化
  • I have linked the issue to this PR (if applicable) | N/A
  • I have mentioned the person who will review this PR | 我已提及将审查此 PR 的人

Reviewer Checklist

  • Made sure Checks passed
  • Tests have been provided

Targeting main following the post-v2.0.36 release flow; happy to retarget if
a dedicated dev branch is introduced.

@Memtensor-AI Memtensor-AI added area:api 云服务 / FastAPI / OpenAPI / MCP status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Nov 11, 2026
@Memtensor-AI

Memtensor-AI commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

🤖 Open Code Review

Target: PR #2615
Task: b16127f60c551023
Base: main
Head: fix/mmr-loop-incremental-max
Head SHA: 795258f37a4a201a960217d92e346db745b5edbe

✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

Generated by cloud-assistant via Open Code Review.

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

✅ Automated Test Results: PASSED

Required tests passed (5/5 executed, 0 skipped). Duration: 10s [advisory, non-gating] AI-generated tests on branch test/auto-gen-2aff4698ef45f6f8-20261111163656: 67/67 passed — these do NOT affect the PR verdict; review the branch manually.

Branch: fix/mmr-loop-incremental-max

Tested source: c83b538681e91643e6d7baa2cf51466dac56ddca
Tested base: 3a16e5fe3048a209f722806f4123fa2e533588fd
Tested commit: c83b538681e91643e6d7baa2cf51466dac56ddca
These results apply only to this immutable source/base snapshot.

@Memtensor-AI Memtensor-AI added status:ready Ready for implementation; waiting for assignee or AI dispatch | 可进入实现,等待认领或派发 and removed status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Nov 11, 2026
The MMR selection loop computed each candidate's maximum similarity to
the selected set twice per round (once inside the text-similarity gate,
once for the diversity penalty) by rescanning the whole selected set,
and recomputed strip() on every memory in every round.

- Maintain the per-candidate maximum similarity incrementally with
  np.maximum after each selection (pure elementwise comparisons, so
  every read is bit-identical to the full rescan).
- The text-similarity gate accepts the precomputed maximum; its index
  lookup stays lazy, running only on the rare high-similarity path with
  unchanged tie-break order.
- Precompute stripped texts once (strip is a pure function of each
  memory).

Selection results are unchanged: unit tests compare the output against
a reference implementation that rescans and recomputes every round,
across adversarial batches (near-duplicates, duplicate texts, score
ties, negative similarities, capacity pressure).
@yiwang-one
yiwang-one force-pushed the fix/mmr-loop-incremental-max branch from c83b538 to 795258f Compare November 13, 2026 07:58
@Memtensor-AI Memtensor-AI added status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 and removed status:ready Ready for implementation; waiting for assignee or AI dispatch | 可进入实现,等待认领或派发 labels Nov 13, 2026
@endxxxx
endxxxx changed the base branch from main to dev-v2.0.37 November 17, 2026 07:55
@endxxxx
endxxxx merged commit bc255c4 into MemTensor:dev-v2.0.37 Nov 17, 2026
13 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:api 云服务 / FastAPI / OpenAPI / MCP status:in-progress Someone or AI is working on it | 人工或 AI 正在处理

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants