pub struct CrossEncoder { /* private fields */ }Expand description
Cross-encoder that jointly scores (query, document) pairs for reranking.
Construct with CrossEncoder::new and call CrossEncoder::rerank to
reorder a Vec<CandidateDoc> by cross-encoder relevance scores.
Implementations§
Source§impl CrossEncoder
impl CrossEncoder
Sourcepub fn new(config: CrossEncoderConfig) -> Self
pub fn new(config: CrossEncoderConfig) -> Self
Create a new cross-encoder with the given configuration.
Sourcepub fn score_pair(&self, query: &[f64], doc: &[f64]) -> f64
pub fn score_pair(&self, query: &[f64], doc: &[f64]) -> f64
Compute the relevance score for a single (query, document) pair.
The embedding lengths are independently capped at
CrossEncoderConfig::max_doc_length before scoring.
Sourcepub fn rerank(
&mut self,
query: &[f64],
candidates: Vec<CandidateDoc>,
) -> Vec<RerankedDoc>
pub fn rerank( &mut self, query: &[f64], candidates: Vec<CandidateDoc>, ) -> Vec<RerankedDoc>
Rerank a list of candidate documents for the given query embedding.
Returns documents sorted in descending order of cross_encoder_score.
Stats are updated after each call.
Sourcepub fn rerank_batch(
&mut self,
queries_and_candidates: Vec<(Vec<f64>, Vec<CandidateDoc>)>,
) -> Vec<Vec<RerankedDoc>>
pub fn rerank_batch( &mut self, queries_and_candidates: Vec<(Vec<f64>, Vec<CandidateDoc>)>, ) -> Vec<Vec<RerankedDoc>>
Rerank multiple (query, candidates) pairs in a single call.
Each element of the input slice is processed independently. The method
is equivalent to calling rerank for each pair
sequentially.
Sourcepub fn dot_product(a: &[f64], b: &[f64]) -> f64
pub fn dot_product(a: &[f64], b: &[f64]) -> f64
Raw dot product: Σ q_i · d_i.
Stops at the shorter slice length.
Sourcepub fn cosine_similarity(a: &[f64], b: &[f64]) -> f64
pub fn cosine_similarity(a: &[f64], b: &[f64]) -> f64
Cosine similarity: dot(a, b) / (|a| · |b|).
Returns 0.0 when either vector has near-zero norm to avoid division
by zero; the epsilon used is 1e-10.
Sourcepub fn bilinear_score(query: &[f64], doc: &[f64], weights: &[f64]) -> f64
pub fn bilinear_score(query: &[f64], doc: &[f64], weights: &[f64]) -> f64
Diagonal bilinear form: Σ w_i · q_i · d_i.
Weights are cycled when the embedding dimension exceeds weights.len().
A zero-length weights slice returns 0.0.
Sourcepub fn linear_score(
query: &[f64],
doc: &[f64],
weights: &[f64],
bias: f64,
) -> f64
pub fn linear_score( query: &[f64], doc: &[f64], weights: &[f64], bias: f64, ) -> f64
Affine linear model: dot(weights, q ⊙ d) + bias.
q ⊙ d is the element-wise product, capped at the minimum length of
query and doc. Weights beyond the element-wise product length are
ignored; product components without a corresponding weight are treated
as having weight 0.0.
Sourcepub fn normalize_scores(docs: &mut [RerankedDoc])
pub fn normalize_scores(docs: &mut [RerankedDoc])
Min-max normalize cross_encoder_score values to [0, 1] in-place.
When all scores are identical the method maps every score to 1.0 to
avoid a degenerate zero-range normalization.
Sourcepub fn rank_changed(initial_order: &[&str], reranked: &[RerankedDoc]) -> usize
pub fn rank_changed(initial_order: &[&str], reranked: &[RerankedDoc]) -> usize
Count the number of documents whose position changed after reranking.
initial_order holds document IDs in the order returned by the first-
stage retriever; reranked is the cross-encoder output. A document is
counted as “changed” when its 0-indexed position in reranked differs
from its position in initial_order. Documents present in only one list
are not counted.
Sourcepub fn stats(&self) -> &CrossEncoderStats
pub fn stats(&self) -> &CrossEncoderStats
Return a reference to the accumulated runtime statistics.
Auto Trait Implementations§
impl Freeze for CrossEncoder
impl RefUnwindSafe for CrossEncoder
impl Send for CrossEncoder
impl Sync for CrossEncoder
impl Unpin for CrossEncoder
impl UnsafeUnpin for CrossEncoder
impl UnwindSafe for CrossEncoder
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.