Skip to content

refactor: make Reranker stateless with std::variant value semantics (… - #471

Merged
Cuiyus merged 9 commits into
alibaba:mainfrom
Cuiyus:refactor/reranker-stateless-461
Jun 9, 2026
Merged

refactor: make Reranker stateless with std::variant value semantics (…#471
Cuiyus merged 9 commits into
alibaba:mainfrom
Cuiyus:refactor/reranker-stateless-461

Conversation

@Cuiyus

@Cuiyus Cuiyus commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

#461)

Replace class hierarchy (Reranker/ScoreBasedReranker/RrfReranker/ WeightedReranker/CallbackReranker) with std::variant<RrfParams, WeightedParams, CallbackParams> value type and a stateless free function reranker::rerank().

Key changes:

  • reranker.h: define RerankParams variant + reranker::rerank() API
  • query.h: MultiQuery::reranker (shared_ptr) -> MultiQuery::rerank (value)
  • schema.h: add CollectionSchema::get_field_ptr() returning FieldSchema::Ptr
  • collection.cc: push field lookup to caller, pass vectorFieldSchema::Ptr
  • c_api: remove opaque zvec_reranker_t, add zvec_multi_query_set_rerank_*
  • python binding: expose _RrfParams/_WeightedParams/_CallbackParams + setters
  • python layer: WeightedReRanker(list[float]), remove Python rerank logic
  • all tests updated to new interface

Benefits:

  • Thread-safe by design: no mutable state, safe to share across threads
  • Collection-decoupled: no bind_schema(), field info passed as parameter
  • Simpler lifecycle: value semantics, no shared_ptr management

Closes #461

@Cuiyus
Cuiyus marked this pull request as draft June 8, 2026 03:33
…libaba#461)

Replace class hierarchy (Reranker/ScoreBasedReranker/RrfReranker/
WeightedReranker/CallbackReranker) with std::variant<RrfParams,
WeightedParams, CallbackParams> value type and a stateless free function
reranker::rerank().

Key changes:
- reranker.h: define RerankParams variant + reranker::rerank() API
- query.h: MultiQuery::reranker (shared_ptr) -> MultiQuery::rerank (value)
- schema.h: add CollectionSchema::get_field_ptr() returning FieldSchema::Ptr
- collection.cc: push field lookup to caller, pass vector<FieldSchema::Ptr>
- c_api: remove opaque zvec_reranker_t, add zvec_multi_query_set_rerank_*
- python binding: expose _RrfParams/_WeightedParams/_CallbackParams + setters
- python layer: WeightedReRanker(list[float]), remove Python rerank logic
- all tests updated to new interface

Benefits:
- Thread-safe by design: no mutable state, safe to share across threads
- Collection-decoupled: no bind_schema(), field info passed as parameter
- Simpler lifecycle: value semantics, no shared_ptr management

Closes alibaba#461
@Cuiyus
Cuiyus force-pushed the refactor/reranker-stateless-461 branch from 5a02f13 to af54e77 Compare June 8, 2026 03:37
After the reranker stateless refactor the C++ MultiQuery rerank
strategy uses a std::variant with a default value, so the implicit
'reranker required' validation no longer triggered. Restore the
check in QueryExecutor._execute_multi_query so that a hybrid
(multi-query) request without a reranker raises ValueError.
@Cuiyus
Cuiyus marked this pull request as ready for review June 8, 2026 06:38
Comment thread src/db/reranker/reranker.cc Outdated
Comment thread src/db/reranker/reranker.cc Outdated
Comment thread python/zvec/executor/query_executor.py
Comment thread src/db/collection.cc Outdated
Comment thread python/zvec/extension/rerank_function.py
Comment thread src/include/zvec/db/reranker.h Outdated
Comment thread src/include/zvec/db/reranker.h
Cuiyus added 4 commits June 8, 2026 20:22
Replace dynamic_cast nullptr check with explicit IndexType::FTS check
and map FTS/BM25 positive scores to (0.0, 1.0) via 2*atan(score)/pi.
…lify usages

Move RrfParams, WeightedParams, CallbackParams and RerankParams into the
zvec::reranker namespace, and add explicit reranker:: qualification at all
usage sites outside the reranker module (query.h, python/c bindings, tests).
…ImplementedError

Remove @AbstractMethod from RerankFunction._to_cpp_params and provide a
default implementation raising NotImplementedError. Drop the redundant
_to_cpp_params overrides from Qwen and Sentence rerankers since they use
the Python rerank path and don't need the C++ conversion.

@egolearner egolearner left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Cuiyus
Cuiyus merged commit 0923f7c into alibaba:main Jun 9, 2026
13 checks passed
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.

Refactor: Make Reranker stateless and decoupled from Collection to fix race condition

2 participants