Expose to/from fp8 in Python and don't auto-convert fp8 when loading from safetensors (#2985)

This commit is contained in:
Awni Hannun
2026-01-13 15:48:21 -08:00
committed by GitHub
parent 8654b8281d
commit 099dcc0f4c
6 changed files with 63 additions and 38 deletions
-1
View File
@@ -1,5 +1,4 @@
cuda_skip = {
"TestLoad.test_load_f8_e4m3",
"TestLayers.test_quantized_embedding",
# Block masked matmul NYI
"TestBlas.test_block_masked_matmul",
+4 -4
View File
@@ -168,8 +168,8 @@ class TestLoad(mlx_tests.MLXTestCase):
expected = [
0,
mx.nan,
mx.nan,
448,
-448,
-0.875,
0.4375,
-0.005859,
@@ -179,12 +179,12 @@ class TestLoad(mlx_tests.MLXTestCase):
-0.0039,
]
expected = mx.array(expected, dtype=mx.bfloat16)
contents = b'H\x00\x00\x00\x00\x00\x00\x00{"tensor":{"dtype":"F8_E4M3","shape":[10],"data_offsets":[0,10]}} \x00\x7f\xff\xb6.\x83\xba\xba\xbc\x82'
contents = b'H\x00\x00\x00\x00\x00\x00\x00{"tensor":{"dtype":"F8_E4M3","shape":[10],"data_offsets":[0,10]}} \x00~\xfe\xb6.\x83\xba\xba\xbc\x82'
with tempfile.NamedTemporaryFile(suffix=".safetensors") as f:
f.write(contents)
f.seek(0)
out = mx.load(f)["tensor"]
self.assertTrue(mx.allclose(out[0], expected[0], equal_nan=True))
self.assertTrue(mx.allclose(mx.from_fp8(out), expected))
def test_save_and_load_gguf_metadata_basic(self):
if not os.path.isdir(self.test_dir):
+7 -2
View File
@@ -3197,8 +3197,6 @@ class TestOps(mlx_tests.MLXTestCase):
)
)
class TestBroadcast(mlx_tests.MLXTestCase):
def test_broadcast_shapes(self):
# Basic broadcasting
self.assertEqual(mx.broadcast_shapes((1, 2, 3), (3,)), (1, 2, 3))
@@ -3243,6 +3241,13 @@ class TestBroadcast(mlx_tests.MLXTestCase):
self.assertTrue(mx.array_equal(mx.sort(x), expected, equal_nan=True))
x = mx.array([3.0, mx.nan, 2.0, 0.0]) + 1j * mx.array([1.0] * 4)
def test_to_from_fp8(self):
vals = mx.array(
[448, 256, 192, 128, 96, 64, 48, 32, 24, 16, 12, 8, 6, 4, 3, 2, 0.015625]
)
self.assertTrue(mx.array_equal(mx.from_fp8(mx.to_fp8(vals)), vals))
self.assertTrue(mx.array_equal(mx.from_fp8(mx.to_fp8(-vals)), -vals))
if __name__ == "__main__":
mlx_tests.MLXTestRunner()