* Fixing error message for FileNotFound.
* Old PR readme fix.
### Notes
- Zero-copy: No format is really zero-copy in ML, it needs to go from disk to RAM/GPU RAM (that takes time). Also
- In PyTorch/numpy, you need a mutable buffer, and we don't really want to mutate a mmaped file, so 1 copy will be necessary if you modify an array (copy-on-write). That being said, zero-copy is achievable in Rust if it's wanted and safety can be guaranteed by some other means.
+ In PyTorch/numpy, you need a mutable buffer, and we don't really want to mutate a mmaped file, so 1 copy is really necessary to use the thing freely in user code. That being said, zero-copy is achievable in Rust if it's wanted and safety can be guaranteed by some other means.
SafeTensors is not zero-copy for the header. The choice of JSON is pretty arbitrary, but since deserialization is <<< of the time required to load the actual tensor data and is readable I went that way, (also space is <<< to the tensor data).
- Endianness: Little-endian. This can be modified later, but it feels really unecessary at the
//! Dummy doc
use libloading::{Library, Symbol};
use memmap2::{Mmap, MmapOptions};
-use pyo3::exceptions::PyException;
+use pyo3::exceptions::{PyException, PyFileNotFoundError};
use pyo3::once_cell::GILOnceCell;
use pyo3::prelude::*;
use pyo3::types::IntoPyDict;
impl Open {
fn new(filename: PathBuf, framework: Framework, device: Option<Device>) -> PyResult<Self> {
- let file = File::open(&filename)?;
+ let file = File::open(&filename).map_err(|_| {
+ PyFileNotFoundError::new_err(format!("No such file or directory: {filename:?}"))
+ })?;
let device = device.unwrap_or(Device::Cpu);
if device != Device::Cpu && framework != Framework::Pytorch {
g.write("something")
+class ErrorsTestCase(unittest.TestCase):
+ def test_file_not_found(self):
+ with self.assertRaises(FileNotFoundError) as ctx:
+ with safe_open("notafile", framework="pt"):
+ pass
+ self.assertEqual(str(ctx.exception), 'No such file or directory: "notafile"')
+
+
class ReadmeTestCase(unittest.TestCase):
def assertTensorEqual(self, tensors1, tensors2, equality_fn):
self.assertEqual(tensors1.keys(), tensors2.keys(), "tensor keys don't match")