Skip to main content

CrossEncoder

Struct CrossEncoder 

Source
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

Source

pub fn new(config: CrossEncoderConfig) -> Self

Create a new cross-encoder with the given configuration.

Source

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.

Source

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.

Source

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.

Source

pub fn dot_product(a: &[f64], b: &[f64]) -> f64

Raw dot product: Σ q_i · d_i.

Stops at the shorter slice length.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn stats(&self) -> &CrossEncoderStats

Return a reference to the accumulated runtime statistics.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more