Repository navigation
Conversation
b04268d to
44af64e
Compare
| if (!completed) { | ||
| // TODO: clarify the contract here - should we return partial or no | ||
| // result if the hit rate is too low | ||
| set_empty_result(results); |
There was a problem hiding this comment.
This would clear results for entire batch, if one query got cancelled. Previous design which only empty the cancelled query make more sense. Maybe either have a for loop to empty queries that have less than k results here or check inside the search per query by passing a checker to search per query
// C API: pass a factory instead of one shared HitRateFilter
auto make_hit_rate = [&] { return svs::HitRateFilter{*id_filter, filter_rate, sample_size}; };
index.filtered_search(results.view(), queries, params, make_hit_rate);
// index.h, inside greedy_search_closure (runs once per query):
auto hr = make_hit_rate(); // this query's own counters
greedy_search(..., internal_search_builder(hr.filter()), prefetch,
EarlyStop{done, hr.cancel()});
if (hr.cancel()) { buffer.clear(); } // gave up → empty only this rowThere was a problem hiding this comment.
Imho, the contract have to be changed to return all available results even if search was cancelled or stopped due filter hit rate - let user decide if a result to be ignored if it's size is less than K.
I think it should be common for both TopK and Range searches (where results.apply() is called) and I want to apply such change in next steps after range search optimization.
For now - just keeping the change as simple as possible.
c16212d to
7df64de
Compare
Let a caller end a Vamana search early and learn that it happened, and use that to enforce the filter hit rate in the C API without the batch iterator. Index search (static, dynamic, concurrent): - `cancel` is a template parameter (default: never) called before each query and before each graph hop of a query. A query it ends keeps the results found so far; a query starting while it holds gets an empty row instead of its entry points. - `search()` returns `true` if every query ran to completion and `false` if any ended early. - `greedy_search()` takes the termination rule (`DefaultStop`, `EarlyStop`) in place of `cancel`. - `single_search()` and `per_thread_batch_search()` lose their `index` and `cancel` parameters; overloads of these extension points must drop them too. The buffer top-up moves to `BatchIteratorBase::next()`, its only user. Orchestrators: - `filtered_search(results, queries, params, cancel, filter)` on `svs::Vamana` and `svs::DynamicVamana` takes the predicate and returns the completion flag. Helpers: - `svs::HitRateFilter` (core/filter.h) wraps an ID filter, counts its checks and hits across threads, and provides the matching cancel predicate. - `lib::ReturnsFalseType`. C API: - Filtered top-k search always filters inside the graph search. With a `filter_rate` estimate, the search is cancelled once the observed hit rate falls below it, and the call then returns no results, as before. Tests: shared `check_cancelled_search()` for the three indices and both orchestrators, a unit test for `HitRateFilter`, and C API tests on distinct random vectors with a case for an overestimated filter rate.
7df64de to
e4dc862
Compare
Cancel predicate and completion flag for Vamana search
Summary
Lets a caller end a Vamana search early and learn that it happened. The C API uses this to enforce the filter hit rate inside the graph search, so filtered top-k search no longer needs the batch iterator.
Stacked on #489 (filtered search in the graph), which must merge first. This PR adds one commit on top of it.
Behaviour
cancelis called before each query and before each graph hop of a query.cancelends keeps the results it found so far.cancelholds returns an empty row. Before this PR it returned its entry points.search()andfiltered_search()returntrueif every query ran to completion andfalseif any ended early.API
Index level (
VamanaIndex,MutableVamanaIndex,concurrent::MutableVamanaIndex):Orchestrators (
svs::Vamana,svs::DynamicVamana,svs::ConcurrentDynamicVamana):New helper,
svs::HitRateFilter(include/svs/core/filter.h):filter()is the wrapped filter, counting each check and whether it passed.cancel()holds oncemin_callschecks were made and less thanmin_rateof them passed.Changes
greedy_search.h, both variants):greedy_search()takes the termination rule in place ofcancel.DefaultStopisbuffer.done();EarlyStop{base, early}adds a no-argument early-stop predicate. The loop iswhile (!stop(search_buffer)).index.h,dynamic_index.h,concurrent/dynamic_index.h):greedy_search_closure()takes the early-stop and completion predicates, checks the early one before starting a query, and records whether the query ran to completion.extensions.h):single_search()andper_thread_batch_search()lose theirindexandcancelparameters. Overloads of these extension points must drop them too.BatchIteratorBase::next(), its only user. This part is already onmain(refactor(search): Top-up search-buffer for BatchIterator only #498) and leaves the diff after a rebase.vamana.h,dynamic_vamana.h):filtered_search()takescancelbeforefilterand returns the completion flag.lib::ReturnsFalseType(lib/misc.h), next toReturnsTrueType.bindings/c/src/filtered_search.hpp): filtered top-k search always filters inside the graph search.filter_rateestimate, nothing is enforced.HitRateFilterduring it. A rate below the estimate returns no results for the whole call.Compatibility
filtered_search()on the orchestrators is introduced by Implement top-k filtered search inside [Mutable]Vamana indices and orchestrators wrappers #489 and changes signature here. It has not been released.search()on the indices changes its return type fromvoidtobool, andcancelfromconst lib::DefaultPredicate&to a template parameter. Existing calls compile unchanged.single_search()orper_thread_batch_search()must drop the two removed parameters.mainsince refactor(search): Top-up search-buffer for BatchIterator only #498.Testing
tests/utils/filtered_search.h:check_cancelled_search()is shared by the three indices and their orchestrators. It checks:tests/svs/core/filter.cpp: unit test forHitRateFilter, including counting from several threads.tests/svs/index/vamana/search_buffer.cpp:DefaultStopandEarlyStopon a plain buffer.filter_rateabove the hit rate seen during the search.Passing:
[filter],[filtered],[cancel],[iterator],[search_buffer]and the C API suite (30 consecutive runs).Performance
Not measured yet on this commit. The change touches the
greedy_searchloop condition, so static and dynamic search QPS and the batch iterator need an A/B run against the base before merge.Notes for reviewers
filter_rate, which is all the C API contract needs.bindings/c/src/index_builder.cppstill describe the iterator path; they are left for a follow-up.🤖 Generated with Claude Code