use std::path::PathBuf;
use oa::matrix::Matrix;
use oa::{Engine, Error, Result};
use crate::idx::IdxSplit;
use crate::pack::{FetchProgress, Pack};
pub struct MatrixData {
pub name: &'static str,
pub shape: Vec<usize>,
pub values: Vec<f32>,
}
pub struct Decoded {
pub matrices: Vec<MatrixData>,
pub text: Option<String>,
pub seq_len: usize,
}
impl Decoded {
pub fn matrix(&self, name: &str) -> Result<&MatrixData> {
self
.matrices
.iter()
.find(|matrix| matrix.name == name)
.ok_or_else(|| Error::data_loss(format!("decoded pack has no '{name}' matrix")))
}
pub fn upload(&self, name: &str, engine: &Engine) -> Result<Matrix> {
let matrix = self.matrix(name)?;
Matrix::new_from_slice_on(engine, matrix.shape.clone(), &matrix.values)
}
}
#[derive(Clone, Debug)]
pub struct DecodeConfig {
pub split: IdxSplit,
pub normalize: bool,
pub count: Option<usize>,
pub cache_dir: Option<PathBuf>,
}
impl Default for DecodeConfig {
fn default() -> Self {
Self {
split: IdxSplit::Train,
normalize: true,
count: None,
cache_dir: None,
}
}
}
pub fn manifest(name: &str) -> Result<&'static Pack> {
crate::decode_dispatch::manifest_of(name)
}
pub fn load(name: &str, config: &DecodeConfig) -> Result<Decoded> {
load_with_progress(name, config, &mut |_| true)
}
pub fn load_with_progress(
name: &str,
config: &DecodeConfig,
progress: &mut impl FnMut(FetchProgress) -> bool,
) -> Result<Decoded> {
crate::decode_dispatch::load_of(name, config, progress)
}