Repository navigation
fix(api): optimize MMR selection by removing redundant rescans - #2615
Conversation
🤖 Open Code ReviewTarget: PR #2615 ✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s). Generated by cloud-assistant via Open Code Review. |
✅ Automated Test Results: PASSEDRequired 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: Tested source: |
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).
c83b538 to
795258f
Compare
Description
What & Why
The MMR selection loop in
_mmr_dedup_text_memoriescomputed eachcandidate'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 everyround. With dozens of rounds over dozens of candidates, this redundant
work dominated the dedup stage.
Approach
incrementally: after each selection, one vectorized
np.maximumagainst the new column updates
running_max. Since this performsonly elementwise comparisons (no arithmetic reordering), every read
is bit-identical to the previous full rescan.
optional
max_simparameter; when it is not provided the gatebehaves 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.
stripis a pure function of eachmemory).
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
remainingset 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
-infinitialisation), and bucket capacity pressure. Outputs aredeep-compared bucket by bucket, in order.
Related Issue (Required): N/A — performance fix with no functional change
Type of change
How Has This Been Tested?
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):
Checklist
Reviewer Checklist
Targeting
mainfollowing the post-v2.0.36 release flow; happy to retarget ifa dedicated dev branch is introduced.