Skip to content

Exact speedups for the prefilter and the SW rerank - #45

Closed
Cassie818 wants to merge 2 commits into
mainfrom
perf/exact-search-speedups
Closed

Cassie818 wants to merge 2 commits into
mainfrom
perf/exact-search-speedups

Conversation

@Cassie818

Copy link
Copy Markdown
Collaborator

Summary

Speeds up Searcher.search (default tm_a rerank) by about 2.2–2.3× without changing any result: candidates, prefilter scores, SW scores, TM_a, structure scores, aligned-pair counts and ranks are identical to main bit for bit. No algorithm parameter, index file format or public signature changes.

  • Prefilter (rfoldsearch/prefilter.py): best-diagonal counts are accumulated in cache-sized target blocks, using one cursor per expanded query code. Each block uses a dense histogram that is stamped rather than cleared. Postings are packed as (target << pbits) | position in one uint32, which halves the in-memory posting arrays. The top-N candidates are selected with argpartition on a unique (-score, target) key. The counting-sort kernel is kept as the fallback for very large databases and for queries of 65,535 or more tokens.
  • Rerank (rfoldsearch/structure_score.py): up to 8 length-sorted candidates are aligned at once, using the same float32 recurrence and tie rules as smith_waterman_traceback. Padding cells can never beat a lane's best cell, so scores, end cells and paths are unchanged. Each batch's traceback pointers stay within 32 MiB, so long chains get fewer lanes, down to one. Inputs other than a float32 matrix with float32 gap penalties are scored pair by pair. TM_a skips the block seed when the block covers the whole alignment, because it would repeat the first refinement exactly.

Verification

  • Tests: 140 passed, 2 skipped. New tests check:
    • top-N selection against a full lexicographic sort;
    • small target blocks and stamp wraparound;
    • agreement of the packed, unpacked and fallback kernels;
    • the batched scorer equals score_pair bitwise under three batching regimes, serial and parallel;
    • batch bounds respect the lane and memory limits;
    • non-float32 inputs.
  • Side-by-side checks against main found 0 differences in all of these:
    • 5,760 randomized prefilter cases, covering targets shorter than k, long targets, low-complexity sequences, self exclusion and forced stamp wraps;
    • 6,090 real candidate pairs;
    • the longest RNAsolo chains (2,802–4,416 tokens).
  • Benchmark on the 33,043-chain predicted database, 40 queries, top-k 500, interleaved with main, digests hashed on float bit patterns:
main this PR speedup queries with different hits
1 thread, search median 128.0 ms 56.1 ms 2.28× 0/40
1 thread, prefilter median ~46 ms ~20 ms ~2.3× —
8 threads, search median 77.5 ms 35.4 ms 2.19× 0/40

These timings come from a heavily loaded laptop, so the ratios are meaningful but the absolute times are not.

Notes

  • Multithreaded runs that also import torch need NUMBA_THREADING_LAYER=workqueue on macOS. Without it, Numba's OpenMP layer clashes with torch's libomp and crashes. This happens on main as well.
  • Loading a database briefly uses about 450 MB more at the 530k-chain scale while the postings are packed. Steady-state and per-query memory are lower than on main.

Count each target's best diagonal in cache-sized target blocks: posting
lists are walked once with one cursor per expanded query code, a dense
(targets x diagonals) histogram is stamped per block instead of cleared,
and postings are packed as (target << pbits) | position in one uint32,
halving the index. The top-N candidates are taken with argpartition on a
unique (-score, target) key. Counts, candidates and scores are identical
to the counting-sort kernel, which remains the fallback for very large
databases and queries of 65,535+ tokens.
Align up to eight length-sorted candidates at once with the same float32
recurrence and tie rules as smith_waterman_traceback; padding cells can
never beat a lane's best cell, so scores, end cells and paths are
unchanged. Each batch's traceback pointers stay within 32 MiB (long
chains get fewer lanes, down to one), inputs other than a float32 matrix
and float32 gap penalties are scored pair by pair, and TM_a skips the
block seed when the block is the whole alignment. Results equal
score_pair bit for bit.
@Cassie818 Cassie818 closed this Nov 19, 2026
@Cassie818
Cassie818 deleted the perf/exact-search-speedups branch November 19, 2026 02:08
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.

1 participant