use std::path::Path;
use burn::store::{
ModuleStore,
PytorchStore,
PytorchStoreError,
burn_pack::Tensor as PackTensor,
pytorch::PytorchError,
};
use crate::errors::{
BunsenError,
BunsenErrorKind,
BunsenResult,
LookupError,
LookupProblem,
io_error_kind,
};
pub fn pytorch_store_error(path: &Path) -> impl FnOnce(PytorchStoreError) -> BunsenError + '_ {
move |error| {
let kind = match &error {
PytorchStoreError::Io(io) | PytorchStoreError::Reader(PytorchError::Io(io)) => {
io_error_kind(io)
}
_ => BunsenErrorKind::InvalidResource,
};
if kind == BunsenErrorKind::Lookup {
let problem = match &error {
PytorchStoreError::Io(io) | PytorchStoreError::Reader(PytorchError::Io(io))
if io.kind() == std::io::ErrorKind::PermissionDenied =>
{
LookupProblem::Denied
}
_ => LookupProblem::Missing,
};
BunsenError::lookup(LookupError::path(path, problem).with_source(error))
} else {
BunsenError::from_cause(kind, error).context(path.display())
}
}
}
pub fn read_pytorch_tensors(path: &Path) -> BunsenResult<Vec<PackTensor>> {
Ok(PytorchStore::from_file(path.to_path_buf())
.map_indices_contiguous(false)
.get_all_tensors()
.map_err(pytorch_store_error(path))?
.values()
.cloned()
.collect())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::errors::testing::ErrorMatcher;
#[test]
fn test_missing_checkpoint_is_a_lookup() {
let dir = tempfile::tempdir().unwrap();
let err = read_pytorch_tensors(&dir.path().join("absent.pt")).unwrap_err();
ErrorMatcher::kind(BunsenErrorKind::Lookup).assert(&err);
}
#[test]
fn test_not_a_checkpoint_is_an_invalid_resource() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("junk.pt");
std::fs::write(&path, b"not a zip, not a pickle").unwrap();
let err = read_pytorch_tensors(&path).unwrap_err();
ErrorMatcher::kind(BunsenErrorKind::InvalidResource).assert(&err);
}
}