]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Improving the bf16 tests for PT+TF. (#505)
authorNicolas Patry <patry.nicolas@protonmail.com>
Fri, 26 Jul 2024 10:03:34 +0000 (12:03 +0200)
committerGitHub <noreply@github.com>
Fri, 26 Jul 2024 10:03:34 +0000 (12:03 +0200)
bindings/python/tests/test_pt_comparison.py
bindings/python/tests/test_tf_comparison.py

index 0f3821b8bf0bb7bdf1a682fe4c2099fcd3b55a53..eb1aa65a7ff48baeabe5eb606aa74f6619d01ce2 100644 (file)
@@ -52,9 +52,9 @@ class TorchTestCase(unittest.TestCase):
 
     def test_odd_dtype(self):
         data = {
-            "test": torch.zeros((2, 2), dtype=torch.bfloat16),
-            "test2": torch.zeros((2, 2), dtype=torch.float16),
-            "test3": torch.zeros((2, 2), dtype=torch.bool),
+            "test": torch.randn((2, 2), dtype=torch.bfloat16),
+            "test2": torch.randn((2, 2), dtype=torch.float16),
+            "test3": torch.randn((2, 2), dtype=torch.bool),
         }
         local = "./tests/data/out_safe_pt_mmap_small.safetensors"
 
index ac41e6f616d2743dada375297d0423bf4f5cfd1c..2c50bb76be593b604f1a6ea00ba3646723976aeb 100644 (file)
@@ -64,7 +64,7 @@ class SafeTestCase(unittest.TestCase):
 
     def test_bfloat16(self):
         data = {
-            "test": tf.zeros((1024, 1024), dtype=tf.bfloat16),
+            "test": tf.randn((1024, 1024), dtype=tf.bfloat16),
         }
         save_file(data, self.sf_filename)
         weights = {}
@@ -76,6 +76,11 @@ class SafeTestCase(unittest.TestCase):
             tv = data[k]
             self.assertTrue(tf.experimental.numpy.allclose(v, tv))
 
+        weights = load_file(self.sf_filename)
+        for k, v in weights.items():
+            tv = data[k]
+            self.assertTrue(tf.experimental.numpy.allclose(v, tv))
+
     def test_deserialization_safe_open(self):
         weights = {}
         with safe_open(self.sf_filename, framework="tf") as f: