Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions mlx/backend/cpu/quantized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1378,9 +1378,15 @@ void fast::ConvertFP8::eval_cpu(
case bfloat16:
unary_op<bfloat16_t, uint8_t>(in, out, detail::ToFP8());
break;
default:
case float32:
unary_op<float, uint8_t>(in, out, detail::ToFP8());
break;
case float64:
unary_op<double, uint8_t>(in, out, detail::ToFP8());
break;
default:
throw std::runtime_error(
"[fast::ConvertFP8::eval_cpu] Unsupported input type.");
}
} else {
switch (out.dtype()) {
Expand All @@ -1390,9 +1396,15 @@ void fast::ConvertFP8::eval_cpu(
case bfloat16:
unary_op<uint8_t, bfloat16_t>(in, out, detail::FromFP8());
break;
default:
case float32:
unary_op<uint8_t, float>(in, out, detail::FromFP8());
break;
case float64:
unary_op<uint8_t, double>(in, out, detail::FromFP8());
break;
default:
throw std::runtime_error(
"[fast::ConvertFP8::eval_cpu] Unsupported output type.");
}
}
});
Expand Down
26 changes: 25 additions & 1 deletion mlx/backend/cpu/unary_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdint.h>
#include <cmath>
#include <complex>
#include <type_traits>

#include "mlx/backend/cpu/simd/simd.h"

Expand Down Expand Up @@ -117,13 +118,36 @@ Simd<uint32_t, N> fp32_to_bits(Simd<float, N> x) {
return *(Simd<uint32_t, N>*)(&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 <int N>
Simd<float, N> narrow_round_to_odd(Simd<double, N> x) {
Simd<float, N> f = x;
Simd<double, N> 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 <typename T, int N>
Simd<uint8_t, N> operator()(Simd<T, N> f) {
uint32_t fp8_max = 543 << 21;
auto denorm_mask = Simd<uint32_t, N>(141 << 23);
Simd<uint32_t, N> f_bits;
Simd<float, N> f32 = f;
Simd<float, N> f32;
if constexpr (std::is_same_v<T, double>) {
f32 = narrow_round_to_odd(f);
} else {
f32 = f;
}
f_bits = fp32_to_bits(f32);
Simd<uint8_t, N> result = 0u;
auto sign = f_bits & 0x80000000;
Expand Down
33 changes: 33 additions & 0 deletions python/tests/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading