mod cache;
mod common;
mod direct;
mod error;
mod expert;
mod slots;
mod stream;
mod sweep;
#[cfg(test)]
pub(crate) mod testutil;
use std::fs;
use std::path::Path;
pub use cache::{CacheError, CachePlan, CacheStats, LayerCache, MAX_SLOTS, STUCK_PROTECTED_PLANS};
pub use common::MappedCommon;
pub use direct::{DIO_ALIGN, DirectFault, DirectSupport};
pub use error::IoError;
pub use expert::{ExpertReader, ExpertSlab, ExpertView};
pub(crate) use slots::mib;
pub use slots::{
Footprint, FootprintError, MAX_POOL_BYTES, RUNTIME_ANON_BYTES, SLOT_ALIGN, SlotError,
SlotGuard, SlotPool, resident_ceiling,
};
pub use stream::{ExpertStream, StreamMode, StreamPhase, StreamStats};
pub use sweep::{
DEFAULT_EXPERTS_PER_WINDOW, DEFAULT_WINDOWS_IN_FLIGHT, LayerSweep, MAX_WINDOWS_IN_FLIGHT,
PrefillSession, SweepConfig, SweepError, SweepExpert, SweepPlan, SweepWindow,
};
use crate::format::{FormatError, Manifest, sha256_file};
use crate::kernels::quants::QuantFormat;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct LoadOptions {
pub skip_hashes: bool,
pub verify_layer_hashes: bool,
pub common_memory: CommonMemory,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CommonMemory {
Mapped,
#[default]
Resident,
}
pub fn parse_quant_format(name: &str) -> Option<QuantFormat> {
match name {
"q4_k" => Some(QuantFormat::Q4_K),
"q5_k" => Some(QuantFormat::Q5_K),
"q6_k" => Some(QuantFormat::Q6_K),
"q8_0" => Some(QuantFormat::Q8_0),
_ => None,
}
}
pub fn verify_named_file(
dir: &Path,
manifest: &Manifest,
name: &str,
options: LoadOptions,
) -> Result<(), IoError> {
let entry = manifest
.files
.get(name)
.ok_or_else(|| FormatError::MissingFileEntry(name.to_owned()))?;
let path = dir.join(name);
let metadata = fs::metadata(&path).map_err(|e| IoError::io(&path, e))?;
if metadata.len() != entry.size {
return Err(FormatError::SizeMismatch {
name: name.to_owned(),
expected: entry.size,
actual: metadata.len(),
}
.into());
}
if !options.skip_hashes {
let actual = sha256_file(&path)?;
if !actual.eq_ignore_ascii_case(&entry.sha256) {
return Err(FormatError::HashMismatch {
name: name.to_owned(),
expected: entry.sha256.clone(),
actual,
}
.into());
}
tracing::debug!(file = name, bytes = entry.size, "file verified");
}
Ok(())
}
pub(crate) fn to_usize(value: u64, what: &'static str) -> Result<usize, IoError> {
usize::try_from(value).map_err(|_| IoError::TooLarge { what, value })
}