use candle_core::DType;
use serde::Deserialize;
use serde::Serialize;
use crate::models::SparseHead;
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;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SparseComputation {
Encoded,
Computed,
}
pub(crate) struct SparseEntry {
pub id: &'static str,
pub family: Option<Family>,
pub head: SparseHead,
pub vocab_size: usize,
pub default_max_length: usize,
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 head_source: Option<&'static str>,
pub head_revision: Option<&'static str>,
pub computation: SparseComputation,
pub cell_exceptions: &'static [CellException],
}
impl SparseEntry {
pub fn descriptor(&self) -> SparseDescriptor {
let backends = BackendSupport {
cpu: self.cpu,
metal: self.metal,
cuda: self.cuda,
};
SparseDescriptor {
id: self.id.to_string(),
family: self.family,
head: self.head,
vocab_size: self.vocab_size,
default_max_length: self.default_max_length,
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,
head_source: self.head_source.map(str::to_string),
head_revision: self.head_revision.map(str::to_string),
computation: self.computation,
}
}
#[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 SparseDescriptor {
pub id: String,
pub family: Option<Family>,
pub head: SparseHead,
pub vocab_size: usize,
pub default_max_length: usize,
pub backends: BackendSupport,
pub dtype_backends: DtypeBackendSupport,
pub license: String,
pub size_gb: f32,
pub phase: u8,
pub weights: WeightsFormat,
pub gated: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub head_source: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub head_revision: Option<String>,
pub computation: SparseComputation,
}
impl SparseDescriptor {
pub fn new(id: impl Into<String>, family: Family, head: SparseHead, vocab_size: usize) -> Self {
Self {
id: id.into(),
family: Some(family),
head,
vocab_size,
default_max_length: 512,
backends: BackendSupport::untested(),
dtype_backends: DtypeBackendSupport::untested(),
license: "unknown".to_string(),
size_gb: 0.0,
phase: u8::MAX,
weights: WeightsFormat::Safetensors,
gated: false,
head_source: None,
head_revision: None,
computation: SparseComputation::Encoded,
}
}
}