]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Push for `.safetensors` instead of `.bin`. (#22)
authorNicolas Patry <patry.nicolas@protonmail.com>
Wed, 19 Oct 2022 10:16:07 +0000 (12:16 +0200)
committerGitHub <noreply@github.com>
Wed, 19 Oct 2022 10:16:07 +0000 (12:16 +0200)
* Push for `.safetensors` instead of `.bin`.

* Bad replace.

bindings/python/README.md
bindings/python/tests/test_flax_comparison.py
bindings/python/tests/test_pt_comparison.py
bindings/python/tests/test_simple.py
bindings/python/tests/test_tf_comparison.py

index 59509b69f30e9f21b5310ea9141a1ade01651f0b..add5f757db9aeb2d50e783fb1c8855f2d44c6240 100644 (file)
@@ -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
index 1595641849780fbc621d60331365ca6b310420cc..f839d9e3e15eb81408fe97e7e95808a8b7429dac 100644 (file)
@@ -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)
index 319839b16e56b675c5ea67912066fe6d629cf19a..ab2a05f826b756405acbea1ab4ed3a6b49600849 100644 (file)
@@ -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:
index c92cebd18d5ff8a7d814b5530e5c50abe65c6582..6c020da42a455a909bf5064e9c3847be1168b873 100644 (file)
@@ -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)
index 2acca906665f3c93865877784d9828181e6079cc..41dc2e364b623a68f959cd67c3368db080a13058 100644 (file)
@@ -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)