def save(tensor_dict: Dict[str, np.ndarray]) -> bytes:
- import datetime
-
- start = datetime.datetime.now()
flattened = {
k: {"dtype": v.dtype.name, "shape": v.shape, "data": v.tobytes()}
for k, v in tensor_dict.items()
}
- print("Flattened", datetime.datetime.now() - start)
serialized = serialize(flattened)
- print("Serialized", datetime.datetime.now() - start)
result = bytes(serialized)
- print("Bytes", datetime.datetime.now() - start)
return result
use pyo3::exceptions;
use pyo3::prelude::*;
-use pyo3::types::{PyBytes, PyDict, PyList};
+use pyo3::types::{PyByteArray, PyBytes, PyDict, PyList};
use safetensors::{Dtype, SafeTensor, SafeTensorBorrowed, Tensor};
use std::collections::HashMap;
let pyshape: PyObject = PyList::new(py, tensor.shape.into_iter()).into();
let pydtype: PyObject = format!("{:?}", tensor.dtype).into_py(py);
- let pydata: PyObject = PyBytes::new(py, tensor.data).into();
+ let pydata: PyObject = PyByteArray::new(py, tensor.data).into();
map.insert("shape".to_string(), pyshape);
map.insert("dtype".to_string(), pydtype);
--- /dev/null
+import unittest
+from safetensors import load_pt, save_pt
+from huggingface_hub import hf_hub_download
+import torch
+import datetime
+
+
+class BigTestCase(unittest.TestCase):
+ def test_gpt2_deserialization_safe(self):
+ filename = hf_hub_download("gpt2", filename="pytorch_model.bin")
+ local = "out_safe.bin"
+ data = torch.load(filename)
+
+ out = save_pt(data)
+ with open(local, "wb") as f:
+ f.write(out)
+
+ start = datetime.datetime.now()
+ with open(local, "rb") as f:
+ serialized = f.read()
+ print()
+ print("Reading file took ", datetime.datetime.now() - start)
+ load_pt(serialized)
+ print("Deserialization (Safe) took ", datetime.datetime.now() - start)
+
+ def test_gpt2_deserialization_pt(self):
+ filename = hf_hub_download("gpt2", filename="pytorch_model.bin")
+
+ start = datetime.datetime.now()
+ torch.load(filename)
+ print()
+ print("Deserialization (PT) took ", datetime.datetime.now() - start)
+
+ def test_gpt2_serialization(self):
+ filename = hf_hub_download("gpt2", filename="pytorch_model.bin")
+ data = torch.load(filename)
+
+ start = datetime.datetime.now()
+ out = save_pt(data)
+ with open("out_py_test.bin", "wb") as f:
+ f.write(out)
+ print()
+ print("Serialization (safe) took ", datetime.datetime.now() - start)
+ del out
+
+ start = datetime.datetime.now()
+ with open("out_pt.bin", "wb") as f:
+ torch.save(data, f)
+ print("Serialization (PT) took ", datetime.datetime.now() - start)
import numpy as np
from huggingface_hub import hf_hub_download
import torch
-import os
import datetime
out = load(serialized)
self.assertEqual(list(out.keys()), ["test"])
np.testing.assert_array_equal(out["test"], np.zeros((2, 2), dtype=np.int32))
-
-
-class BigTestCase(unittest.TestCase):
- def test_gpt2_deserialization(self):
- start = datetime.datetime.now()
- with open("out_py.bin", "rb") as f:
- serialized = f.read()
- print("Reading file took ", datetime.datetime.now() - start)
- out = load_pt(serialized)
- print("Deserialization took ", datetime.datetime.now() - start)
-
- def test_gpt2_serialization(self):
- filename = hf_hub_download("gpt2", filename="pytorch_model.bin")
- data = torch.load(filename)
-
- start = datetime.datetime.now()
- out = save_pt(data)
- with open("out_py_test.bin", "wb") as f:
- f.write(out)
- print("Saving took ", datetime.datetime.now() - start)
-
- start = datetime.datetime.now()
- with open("out_py_test_pt.bin", "wb") as f:
- torch.save(data, f)
- print("PT Saving took ", datetime.datetime.now() - start)
use std::time::Instant;
let start = Instant::now();
let out = SafeTensor::serialize(&metadata);
- println!("Serialization {:?}", start.elapsed());
let start = Instant::now();
std::fs::write(&filename, out).unwrap();
- println!("Write to file {:?}", start.elapsed());
let start = Instant::now();
let raw = std::fs::read(&filename).unwrap();
- println!("Read from file {:?}", start.elapsed());
let start = Instant::now();
SafeTensor::deserialize(raw).unwrap();
- println!("Deserialization {:?}", start.elapsed());
}
-
- // use std::fs::File;
- // use std::io::Read;
- // use std::time::Instant;
- // #[test]
- // fn read_gpt2() {
- // let start = Instant::now();
- // let filename = "gpt2.bin";
- // let mut f = File::open(&filename).unwrap();
- // let metadata = std::fs::metadata(&filename).expect("unable to read metadata");
- // let mut buffer = vec![0; metadata.len() as usize];
- // f.read(&mut buffer).expect("buffer overflow");
- // println!("Read gpt2 in {:?}", start.elapsed());
- // }
}