"b": np.zeros((2, 3), dtype=np.uint8)
}
-save_file(tensors, "./out.bin")
+save_file(tensors, "./model.safetensors")
# Now loading
-loaded = load_file("./out.bin")
+loaded = load_file("./model.safetensors")
```
### Torch
"b": torch.zeros((2, 3), dtype=torch.uint8)
}
-save_file(tensors, "./out.bin")
+save_file(tensors, "./model.safetensors")
# Now loading
-loaded = load_file("./out.bin")
+loaded = load_file("./model.safetensors")
```
### Installation
with open(self.filename, "rb") as f:
data = f.read()
self.data = load(msgpack_restore(data))
- self.local = "./tests/data/out_safe_flax_mmap.bin"
+ self.local = "./tests/data/out_safe_flax_mmap.safetensors"
save_file(self.data, self.local)
def test_deserialization_safe(self):
print(f"Deserialization (flax) took {flax_time} (Safe is {flax_time/safe_time} faster)")
def test_serialization_safe(self):
- outfilename = "./tests/data/out_safe.bin"
+ outfilename = "./tests/data/out_safe.safetensors"
save_file(self.data, outfilename)
start = datetime.datetime.now()
save_file(self.data, outfilename)
"test2": torch.zeros((2, 2), dtype=torch.float16),
"test3": torch.zeros((2, 2), dtype=torch.bool),
}
- local = "./tests/data/out_safe_pt_mmap.bin"
+ local = "./tests/data/out_safe_pt_mmap.safetensors"
save_file(data, local)
reloaded = load_file(local)
self.assertTrue(torch.equal(data["test"], reloaded["test"]))
data = {
"test": torch.arange(4).view((2, 2)).to("cuda:0"),
}
- local = "./tests/data/out_safe_pt_mmap.bin"
+ local = "./tests/data/out_safe_pt_mmap.safetensors"
save_file(data, local)
reloaded = load_file(local)
self.assertTrue(torch.equal(torch.arange(4).view((2, 2)), reloaded["test"]))
def test_sparse(self):
data = {"test": torch.sparse_coo_tensor(size=(2, 3))}
- local = "./tests/data/out_safe_pt_sparse.bin"
+ local = "./tests/data/out_safe_pt_sparse.safetensors"
with self.assertRaises(ValueError):
save_file(data, local)
def setUp(self):
self.filename = hf_hub_download(MODEL_ID, filename="pytorch_model.bin")
self.data = torch.load(self.filename, map_location="cpu")
- self.local = "./tests/data/out_safe_pt_mmap.bin"
+ self.local = "./tests/data/out_safe_pt_mmap.safetensors"
# Need to copy since that call mutates the tensors to numpy
save_file(self.data.copy(), self.local)
def test_serialization_safe(self):
start = datetime.datetime.now()
- outfilename = "./tests/data/out_safe.bin"
+ outfilename = "./tests/data/out_safe.safetensors"
save_file(self.data, outfilename)
safe_time = datetime.datetime.now() - start
start = datetime.datetime.now()
- with open("./tests/data/out_pt.bin", "wb") as f:
+ with open("./tests/data/out_pt.safetensors", "wb") as f:
torch.save(self.data, f)
pt_time = datetime.datetime.now() - start
def setUp(self):
self.tensor = torch.arange(6, dtype=torch.float32).reshape((1, 2, 3))
self.data = {"test": self.tensor}
- self.local = "./tests/data/out_safe_pt_mmap.bin"
+ self.local = "./tests/data/out_safe_pt_mmap.safetensors"
# Need to copy since that call mutates the tensors to numpy
save_file(self.data.copy(), self.local)
x.is_contiguous(),
)
with self.assertRaises(ValueError):
- save_file(data, "./tests/data/out.bin")
+ save_file(data, "./tests/data/out.safetensors")
def test_deserialization_slice(self):
with safe_open(self.local, framework="pt") as f:
# Save another one *with* metadata
tensor = torch.arange(6, dtype=torch.float32).reshape((1, 2, 3))
data = {"test": tensor}
- local = "./tests/data/out_safe_pt_mmap2.bin"
+ local = "./tests/data/out_safe_pt_mmap2.safetensors"
# Need to copy since that call mutates the tensors to numpy
save_file(data, local, metadata={"Something": "more"})
with safe_open(local, framework="pt") as f:
def test_numpy_example(self):
tensors = {"a": np.zeros((2, 2)), "b": np.zeros((2, 3), dtype=np.uint8)}
- save_file(tensors, "./out.bin")
+ save_file(tensors, "./out.safetensors")
# Now loading
- loaded = load_file("./out.bin")
+ loaded = load_file("./out.safetensors")
self.assertTensorEqual(tensors, loaded, np.allclose)
def test_torch_example(self):
# test to be correct.
tensors2 = tensors.copy()
- save_file_pt(tensors, "./out.bin")
+ save_file_pt(tensors, "./out.safetensors")
# Now loading
- loaded = load_file_pt("./out.bin")
+ loaded = load_file_pt("./out.safetensors")
self.assertTensorEqual(tensors2, loaded, torch.allclose)