mirror of
https://github.com/huggingface/candle.git
synced 2025-06-16 18:48:51 +00:00
Fix the npy read function and add some testing. (#1080)
This commit is contained in:
@ -250,8 +250,6 @@ impl Tensor {
|
|||||||
if header.fortran_order {
|
if header.fortran_order {
|
||||||
return Err(Error::Npy("fortran order not supported".to_string()));
|
return Err(Error::Npy("fortran order not supported".to_string()));
|
||||||
}
|
}
|
||||||
let mut data: Vec<u8> = vec![];
|
|
||||||
reader.read_to_end(&mut data)?;
|
|
||||||
Self::from_reader(header.shape(), header.descr, &mut reader)
|
Self::from_reader(header.shape(), header.descr, &mut reader)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
9
candle-core/tests/npy.py
Normal file
9
candle-core/tests/npy.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import numpy as np
|
||||||
|
x = np.arange(10)
|
||||||
|
|
||||||
|
# Write a npy file.
|
||||||
|
np.save("test.npy", x)
|
||||||
|
|
||||||
|
# Write multiple values to a npz file.
|
||||||
|
values = { "x": x, "x_plus_one": x + 1 }
|
||||||
|
np.savez("test.npz", **values)
|
24
candle-core/tests/serialization_tests.rs
Normal file
24
candle-core/tests/serialization_tests.rs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
use candle_core::{DType, Result, Tensor};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn npy() -> Result<()> {
|
||||||
|
let npy = Tensor::read_npy("tests/test.npy")?;
|
||||||
|
assert_eq!(
|
||||||
|
npy.to_dtype(DType::U8)?.to_vec1::<u8>()?,
|
||||||
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn npz() -> Result<()> {
|
||||||
|
let npz = Tensor::read_npz("tests/test.npz")?;
|
||||||
|
assert_eq!(npz.len(), 2);
|
||||||
|
assert_eq!(npz[0].0, "x");
|
||||||
|
assert_eq!(npz[1].0, "x_plus_one");
|
||||||
|
assert_eq!(
|
||||||
|
npz[1].1.to_dtype(DType::U8)?.to_vec1::<u8>()?,
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
BIN
candle-core/tests/test.npy
Normal file
BIN
candle-core/tests/test.npy
Normal file
Binary file not shown.
BIN
candle-core/tests/test.npz
Normal file
BIN
candle-core/tests/test.npz
Normal file
Binary file not shown.
Reference in New Issue
Block a user