use candle_core::DType;
use serde::Deserialize;
use serde::Serialize;
use crate::models::RerankHead;
use crate::models::jina_bert::AlibiSlopeVariant;
use crate::models::xlm_roberta_flash::PositionScheme;
use crate::registry::BackendSupport;
use crate::registry::CellException;
use crate::registry::DtypeBackendSupport;
use crate::registry::DtypeSupport;
use crate::registry::Family;
use crate::registry::Support;
use crate::registry::WeightsFormat;
use crate::registry::rerank::RerankInput;
use crate::registry::support::dtype_serde;
use crate::rerank::ScoreActivation;
pub(crate) struct RerankEntry {
pub id: &'static str,
pub family: Family,
pub head: RerankHead,
pub default_max_length: usize,
pub score_activation: ScoreActivation,
pub input: RerankInput,
pub alibi_variant: Option<AlibiSlopeVariant>,
pub position_scheme: Option<PositionScheme>,
pub default_dtype: DType,
pub cpu: Support,
pub metal: Support,
pub cuda: Support,
pub dtypes: DtypeSupport,
pub license: &'static str,
pub size_gb: f32,
pub phase: u8,
pub weights: WeightsFormat,
pub gated: bool,
pub accel_only_dtypes: &'static [DType],
pub cell_exceptions: &'static [CellException],
}
impl RerankEntry {
pub fn descriptor(&self) -> RerankDescriptor {
let backends = BackendSupport {
cpu: self.cpu,
metal: self.metal,
cuda: self.cuda,
};
RerankDescriptor {
id: self.id.to_string(),
family: self.family,
head: self.head,
default_max_length: self.default_max_length,
score_activation: self.score_activation,
input: self.input.clone(),
alibi_variant: self.alibi_variant,
position_scheme: self.position_scheme,
default_dtype: self.default_dtype,
backends,
dtype_backends: DtypeBackendSupport::derive(backends, self.dtypes, self.accel_only_dtypes)
.with_exceptions(self.cell_exceptions),
license: self.license.to_string(),
size_gb: self.size_gb,
phase: self.phase,
weights: self.weights,
gated: self.gated,
}
}
pub fn needs_accelerator(&self, dtype: DType) -> bool {
self
.descriptor()
.dtype_backends
.row(dtype)
.needs_accelerator()
}
#[cfg(test)]
pub(crate) fn cells(&self) -> crate::registry::support::rules::Cells<'static> {
let descriptor = self.descriptor();
crate::registry::support::rules::Cells {
id: self.id,
backends: descriptor.backends,
dtypes: self.dtypes,
accel_only: self.accel_only_dtypes,
exceptions: self.cell_exceptions,
rows: descriptor.dtype_backends,
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RerankDescriptor {
pub id: String,
pub family: Family,
pub head: RerankHead,
pub default_max_length: usize,
pub score_activation: ScoreActivation,
pub input: RerankInput,
pub alibi_variant: Option<AlibiSlopeVariant>,
pub position_scheme: Option<PositionScheme>,
#[serde(with = "dtype_serde")]
pub default_dtype: DType,
pub backends: BackendSupport,
pub dtype_backends: DtypeBackendSupport,
pub license: String,
pub size_gb: f32,
pub phase: u8,
pub weights: WeightsFormat,
pub gated: bool,
}
impl RerankDescriptor {
pub fn new(
id: impl Into<String>,
family: Family,
head: RerankHead,
score_activation: ScoreActivation,
input: RerankInput,
) -> Self {
Self {
id: id.into(),
family,
head,
default_max_length: 512,
score_activation,
input,
alibi_variant: (family == Family::JinaBert)
.then_some(AlibiSlopeVariant::HalvedInterpolatedTail),
position_scheme: (family == Family::XlmRobertaFlash).then_some(PositionScheme::Absolute),
default_dtype: DType::F32,
backends: BackendSupport::untested(),
dtype_backends: DtypeBackendSupport::untested(),
license: "unknown".to_string(),
size_gb: 0.0,
phase: u8::MAX,
weights: WeightsFormat::Safetensors,
gated: false,
}
}
}