use std::cmp::Ordering;
use serde::Deserialize;
use serde::Serialize;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ScoreActivation {
Identity,
Sigmoid,
}
impl ScoreActivation {
pub fn apply(self, raw: f32) -> f32 {
match self {
ScoreActivation::Identity => raw,
ScoreActivation::Sigmoid => 1.0 / (1.0 + (-raw).exp()),
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct RerankScore {
value: f32,
}
impl RerankScore {
pub fn new(value: f32) -> Self {
Self { value }
}
pub fn value(&self) -> f32 {
self.value
}
}
impl PartialEq for RerankScore {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl Eq for RerankScore {}
impl PartialOrd for RerankScore {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for RerankScore {
fn cmp(&self, other: &Self) -> Ordering {
match (self.value.is_nan(), other.value.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
(false, false) => self.value.total_cmp(&other.value),
}
}
}
#[cfg(test)]
mod tests;