use std::future::Future; use std::path::{Path, PathBuf}; use anyhow::{Context, Result}; use candle_core::{DType, Device}; use candle_nn::VarBuilder; use serde::de::DeserializeOwned; pub fn model_dtype(device: &Device) -> DType { crate::ops::model_dtype(device) } pub async fn resolve_manifest_path(manifest: F) -> Result where F: Future>, { manifest.await } pub async fn load_mmaped_safetensors( manifest: F, device: &Device, build: Build, ) -> Result where F: Future>, Build: FnOnce(VarBuilder) -> std::result::Result, E: Into, { let weights = resolve_manifest_path(manifest).await?; load_mmaped_safetensors_path(&weights, device, build) } pub fn load_mmaped_safetensors_path( weights: &Path, device: &Device, build: Build, ) -> Result where Build: FnOnce(VarBuilder) -> std::result::Result, E: Into, { load_mmaped_safetensors_path_with_dtype(weights, device, DType::F32, build) } pub fn load_mmaped_safetensors_path_with_dtype( weights: &Path, device: &Device, dtype: DType, build: Build, ) -> Result where Build: FnOnce(VarBuilder) -> std::result::Result, E: Into, { let vb = unsafe { VarBuilder::from_mmaped_safetensors(&[weights], dtype, device)? }; build(vb).map_err(Into::into) } pub async fn load_buffered_safetensors( manifest: F, device: &Device, build: Build, ) -> Result where F: Future>, Build: FnOnce(VarBuilder) -> std::result::Result, E: Into, { let weights = resolve_manifest_path(manifest).await?; load_buffered_safetensors_path(&weights, device, build) } pub fn load_buffered_safetensors_path( weights: &Path, device: &Device, build: Build, ) -> Result where Build: FnOnce(VarBuilder) -> std::result::Result, E: Into, { load_buffered_safetensors_path_with_dtype(weights, device, DType::F32, build) } pub fn load_buffered_safetensors_path_with_dtype( weights: &Path, device: &Device, dtype: DType, build: Build, ) -> Result where Build: FnOnce(VarBuilder) -> std::result::Result, E: Into, { let data = std::fs::read(weights).with_context(|| format!("failed to read {}", weights.display()))?; let vb = VarBuilder::from_buffered_safetensors(data, dtype, device)?; build(vb).map_err(Into::into) } pub fn read_json(path: &Path) -> Result { let data = std::fs::read_to_string(path) .with_context(|| format!("failed to read {}", path.display()))?; let parsed = serde_json::from_str(&data) .with_context(|| format!("failed to parse {}", path.display()))?; Ok(parsed) }