diff --git a/mlx/backend/cpu/quantized.cpp b/mlx/backend/cpu/quantized.cpp index 4c91208b9f..d4b8b6e403 100644 --- a/mlx/backend/cpu/quantized.cpp +++ b/mlx/backend/cpu/quantized.cpp @@ -1378,9 +1378,15 @@ void fast::ConvertFP8::eval_cpu( case bfloat16: unary_op(in, out, detail::ToFP8()); break; - default: + case float32: unary_op(in, out, detail::ToFP8()); break; + case float64: + unary_op(in, out, detail::ToFP8()); + break; + default: + throw std::runtime_error( + "[fast::ConvertFP8::eval_cpu] Unsupported input type."); } } else { switch (out.dtype()) { @@ -1390,9 +1396,15 @@ void fast::ConvertFP8::eval_cpu( case bfloat16: unary_op(in, out, detail::FromFP8()); break; - default: + case float32: unary_op(in, out, detail::FromFP8()); break; + case float64: + unary_op(in, out, detail::FromFP8()); + break; + default: + throw std::runtime_error( + "[fast::ConvertFP8::eval_cpu] Unsupported output type."); } } }); diff --git a/mlx/backend/cpu/unary_ops.h b/mlx/backend/cpu/unary_ops.h index 64e20c65da..1f49bb7482 100644 --- a/mlx/backend/cpu/unary_ops.h +++ b/mlx/backend/cpu/unary_ops.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "mlx/backend/cpu/simd/simd.h" @@ -117,13 +118,36 @@ Simd fp32_to_bits(Simd x) { return *(Simd*)(&x); } +// Narrow double to float with round-to-odd: when the conversion is inexact, +// pick the neighboring float whose last mantissa bit is set. Rounding that +// float to a much narrower format (fp8) then gives the same result as +// rounding the double directly. Plain round-to-nearest can land exactly on an +// fp8 tie and round the wrong way, e.g. 1.0625 + 2^-30 must encode as 1.125, +// not 1.0. +template +Simd narrow_round_to_odd(Simd x) { + Simd f = x; + Simd back = f; + auto bits = fp32_to_bits(f); + auto needs_fix = (back != x) && ((bits & 1u) == 0u); + // f is the neighbor on one side of x; step one ulp to the other side. + auto overshot = abs(back) > abs(x); + auto fixed = select(overshot, bits - 1u, bits + 1u); + return fp32_from_bits(select(needs_fix, fixed, bits)); +} + struct ToFP8 { template Simd operator()(Simd f) { uint32_t fp8_max = 543 << 21; auto denorm_mask = Simd(141 << 23); Simd f_bits; - Simd f32 = f; + Simd f32; + if constexpr (std::is_same_v) { + f32 = narrow_round_to_odd(f); + } else { + f32 = f; + } f_bits = fp32_to_bits(f32); Simd result = 0u; auto sign = f_bits & 0x80000000; diff --git a/python/tests/test_ops.py b/python/tests/test_ops.py index 0ec5a701a6..a87704ae7d 100644 --- a/python/tests/test_ops.py +++ b/python/tests/test_ops.py @@ -4960,6 +4960,39 @@ def test_to_from_fp8(self): ) ) + def test_to_from_fp8_float64(self): + # float64 is only supported on the CPU. + with mx.stream(mx.cpu): + vals = [448, 256, 192, 128, 96, 64, 48, 32, 24, 16, 12, 8, 6, 4, 3, 2] + vals += [0.015625, 0.001953125] + x64 = mx.array(vals, dtype=mx.float64) + x32 = mx.array(vals, dtype=mx.float32) + self.assertTrue(mx.array_equal(mx.to_fp8(x64), mx.to_fp8(x32))) + self.assertTrue(mx.array_equal(mx.to_fp8(-x64), mx.to_fp8(-x32))) + + codes = mx.array(list(range(256)), dtype=mx.uint8) + d64 = mx.from_fp8(codes, dtype=mx.float64) + d32 = mx.from_fp8(codes, dtype=mx.float32) + self.assertEqual(d64.dtype, mx.float64) + self.assertTrue(mx.array_equal(d64, d32.astype(mx.float64), equal_nan=True)) + + # 1.0625 is the midpoint between 1.0 (0x38) and 1.125 (0x39). + # Rounding to float32 first would move 1.0625 + eps onto the tie, + # which then rounds to even (0x38) instead of up. + eps = 2.0**-30 + x = mx.array( + [1.0625 - eps, 1.0625, 1.0625 + eps, -(1.0625 + eps)], + dtype=mx.float64, + ) + self.assertEqual(mx.to_fp8(x).tolist(), [0x38, 0x38, 0x39, 0xB9]) + + # Saturation and NaN behave as for the other float types. + special = mx.array( + [float("inf"), -float("inf"), 1e300, float("nan")], dtype=mx.float64 + ) + self.assertEqual(mx.to_fp8(special).tolist()[:3], [0x7E, 0xFE, 0x7E]) + self.assertIn(mx.to_fp8(special)[3].item(), [0x7F, 0xFF]) + def test_zeros_ones_empty_like_dtype(self): x = mx.array([1, 2, 3], dtype=mx.int32)