pub mod count_lstm;
pub mod count_pred;
pub mod schema_gather;
pub mod scorer;
pub mod span_rep;
pub mod token_gather;
pub(crate) const MAX_WIDTH: usize = 8;
#[cfg(not(target_arch = "wasm32"))]
use std::path::Path;
use candle_core::Device;
use candle_nn::VarBuilder;
pub struct AllHeads {
pub span_rep: span_rep::SpanRep,
pub count_lstm: count_lstm::CountLstmFixed,
pub count_pred: count_pred::CountPred,
}
impl AllHeads {
#[cfg(not(target_arch = "wasm32"))]
#[allow(unsafe_code)]
pub fn from_safetensors(weights_path: &Path, device: &Device) -> crate::candle::Result<Self> {
let vb = unsafe { VarBuilder::from_mmaped_safetensors(&[weights_path], candle_core::DType::F32, device) }
.map_err(|e| crate::candle::GlinerCandleError::Backend(format!("heads safetensors: {e}")))?;
Self::load(vb, device)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn from_var_builder(vb: VarBuilder<'_>, device: &Device) -> crate::candle::Result<Self> {
Self::load(vb, device)
}
pub fn from_buffered_safetensors(
bytes: &[u8],
device: &Device,
dtype: candle_core::DType,
) -> crate::candle::Result<Self> {
let tensors = crate::candle::streaming_load::load_buffer_streaming(bytes, device, dtype)?;
let vb = VarBuilder::from_tensors(tensors, dtype, device);
Self::load(vb, device)
}
fn load(vb: VarBuilder<'_>, device: &Device) -> crate::candle::Result<Self> {
let span_rep = span_rep::SpanRep::from_var_builder(&vb.pp("span_rep").pp("span_rep_layer"))
.map_err(|e| crate::candle::GlinerCandleError::Backend(format!("span_rep load: {e}")))?;
let count_lstm = count_lstm::CountLstmFixed::from_var_builder(&vb.pp("count_embed"), device)
.map_err(|e| crate::candle::GlinerCandleError::Backend(format!("count_embed load: {e}")))?;
let count_pred = count_pred::CountPred::from_var_builder(&vb.pp("count_pred"))
.map_err(|e| crate::candle::GlinerCandleError::Backend(format!("count_pred load: {e}")))?;
Ok(Self {
span_rep,
count_lstm,
count_pred,
})
}
}