From b7a40e97a619929f8a06782cd0861a2e6b7170f2 Mon Sep 17 00:00:00 2001 From: Nicolas Patry Date: Wed, 19 Oct 2022 12:16:07 +0200 Subject: [PATCH] Push for `.safetensors` instead of `.bin`. (#22) * Push for `.safetensors` instead of `.bin`. * Bad replace. --- bindings/python/README.md | 8 ++++---- bindings/python/tests/test_flax_comparison.py | 4 ++-- bindings/python/tests/test_pt_comparison.py | 18 +++++++++--------- bindings/python/tests/test_simple.py | 8 ++++---- bindings/python/tests/test_tf_comparison.py | 2 +- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/bindings/python/README.md b/bindings/python/README.md index 59509b6..add5f75 100644 --- a/bindings/python/README.md +++ b/bindings/python/README.md @@ -18,11 +18,11 @@ tensors = { "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 @@ -36,11 +36,11 @@ tensors = { "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 diff --git a/bindings/python/tests/test_flax_comparison.py b/bindings/python/tests/test_flax_comparison.py index 1595641..f839d9e 100644 --- a/bindings/python/tests/test_flax_comparison.py +++ b/bindings/python/tests/test_flax_comparison.py @@ -40,7 +40,7 @@ class SafeTestCase(unittest.TestCase): 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): @@ -64,7 +64,7 @@ class SafeTestCase(unittest.TestCase): 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) diff --git a/bindings/python/tests/test_pt_comparison.py b/bindings/python/tests/test_pt_comparison.py index 319839b..ab2a05f 100644 --- a/bindings/python/tests/test_pt_comparison.py +++ b/bindings/python/tests/test_pt_comparison.py @@ -19,7 +19,7 @@ class TorchTestCase(unittest.TestCase): "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"])) @@ -31,14 +31,14 @@ class TorchTestCase(unittest.TestCase): 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) @@ -47,7 +47,7 @@ class SpeedTestCase(unittest.TestCase): 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) @@ -69,12 +69,12 @@ class SpeedTestCase(unittest.TestCase): 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 @@ -87,7 +87,7 @@ class SliceTestCase(unittest.TestCase): 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) @@ -99,7 +99,7 @@ class SliceTestCase(unittest.TestCase): 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: @@ -121,7 +121,7 @@ class SliceTestCase(unittest.TestCase): # 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: diff --git a/bindings/python/tests/test_simple.py b/bindings/python/tests/test_simple.py index c92cebd..6c020da 100644 --- a/bindings/python/tests/test_simple.py +++ b/bindings/python/tests/test_simple.py @@ -46,10 +46,10 @@ class ReadmeTestCase(unittest.TestCase): 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): @@ -61,8 +61,8 @@ class ReadmeTestCase(unittest.TestCase): # 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) diff --git a/bindings/python/tests/test_tf_comparison.py b/bindings/python/tests/test_tf_comparison.py index 2acca90..41dc2e3 100644 --- a/bindings/python/tests/test_tf_comparison.py +++ b/bindings/python/tests/test_tf_comparison.py @@ -64,7 +64,7 @@ class SafeTestCase(unittest.TestCase): print(f"Deserialization (TF) took {tf_time} (Safe is {tf_time/safe_time} faster)") def test_serialization_safe(self): - outfilename = "./tests/data/out_safe.bin" + outfilename = "./tests/data/out_safe.safetensors" data = self.data.copy() save_file(data, outfilename) -- 2.43.0