use std::path::Path;
use burn::store::burn_pack::Tensor as PackTensor;
use super::ParakeetVocabulary;
use crate::{
data::formats::fermion::{
PhononArchive,
read_five_value_bytes,
},
errors::{
BunsenError,
BunsenResult,
},
};
pub const PHONON_2_CONTAINER: &str = "model.fermion";
pub const PHONON_2_CONFIG: &str = "config.json";
pub fn read_phonon2_archive(path: &Path) -> BunsenResult<(Vec<PackTensor>, ParakeetVocabulary)> {
let read = || -> BunsenResult<(Vec<PackTensor>, ParakeetVocabulary)> {
let mut archive = PhononArchive::read(path)?;
let container = archive.take(PHONON_2_CONTAINER).ok_or_else(|| {
BunsenError::invalid_resource(format!(
"a Phonon-2 archive without `{PHONON_2_CONTAINER}`"
))
})?;
let config = core::str::from_utf8(archive.expect(PHONON_2_CONFIG)?).map_err(|e| {
BunsenError::invalid_resource("a config that is not UTF-8").with_cause(e)
})?;
Ok((
read_five_value_bytes(&container)?,
ParakeetVocabulary::from_nemo_config(config)?,
))
};
read().map_err(|e| e.context(path.display()))
}