#![allow(clippy::result_large_err)]
use std::path::Path;
use tenflowers_core::{Result, Tensor};
#[cfg(feature = "serialize")]
pub fn save_tensor<T>(tensor: &Tensor<T>, path: &Path) -> Result<()>
where
T: bytemuck::Pod + scirs2_core::num_traits::Float + Default + 'static,
{
tenflowers_core::serialization::save_tensor(tensor, path)
}
#[cfg(feature = "serialize")]
pub fn load_tensor<T>(path: &Path) -> Result<Tensor<T>>
where
T: bytemuck::Pod + scirs2_core::num_traits::Float + Default + 'static,
{
tenflowers_core::serialization::load_tensor::<T>(path)
}
#[cfg(not(feature = "serialize"))]
pub fn save_tensor<T>(_tensor: &Tensor<T>, _path: &Path) -> Result<()>
where
T: Clone + Default,
{
Err(tenflowers_core::TensorError::not_implemented_simple(
"save_tensor requires the `serialize` Cargo feature".to_string(),
))
}
#[cfg(not(feature = "serialize"))]
pub fn load_tensor<T>(_path: &Path) -> Result<Tensor<T>>
where
T: Clone + Default,
{
Err(tenflowers_core::TensorError::not_implemented_simple(
"load_tensor requires the `serialize` Cargo feature".to_string(),
))
}
#[cfg(test)]
mod tests {
use super::*;
use std::env::temp_dir;
use tenflowers_core::Tensor;
#[cfg(feature = "serialize")]
#[test]
fn test_tensor_round_trip_f32() {
let original = Tensor::<f32>::ones(&[2, 3]);
let path = temp_dir().join("tenflowers_io_test_f32.bin");
save_tensor(&original, &path).expect("save_tensor should succeed");
let restored: Tensor<f32> = load_tensor(&path).expect("load_tensor should succeed");
assert_eq!(
original.shape().dims(),
restored.shape().dims(),
"shape must be preserved across round-trip"
);
let orig_data = original.as_slice().expect("contiguous");
let rest_data = restored.as_slice().expect("contiguous");
assert_eq!(
orig_data, rest_data,
"data must be identical after round-trip"
);
let _ = std::fs::remove_file(&path);
}
#[cfg(not(feature = "serialize"))]
#[test]
fn test_stub_returns_not_implemented() {
let dummy = Tensor::<f32>::zeros(&[1]);
let path = temp_dir().join("tenflowers_stub_test.bin");
let result = save_tensor(&dummy, &path);
assert!(result.is_err(), "stub should return Err");
}
}