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
50 changes: 24 additions & 26 deletions mlx/ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5326,17 +5326,22 @@ std::vector<array> fp_quantize(
auto new_shape = w.shape();
new_shape.back() = -1;
auto wq = reshape(w, {-1, group_size}, s);
auto scales =
divide(max(abs(wq, s), -1, true, s), array(maxval, w.dtype()), s);
// Skip NaN in the group max, as the GPU kernels do
auto w_abs = abs(wq, s);
w_abs = where(isnan(w_abs, s), array(0, w.dtype()), w_abs, s);
auto scales = divide(max(w_abs, -1, true, s), array(maxval, w.dtype()), s);
if (group_size == 16) {
// convert to e4m3
auto scale_encode = inputs.size() > 1
? divide(array(448.0f * 6.0f, float32), inputs[1], s)
: array(1.0f, float32);
scales = multiply(scales, scale_encode, s);
scales = to_fp8(scales, s);
wq = multiply(
divide(wq, from_fp8(scales, w.dtype(), s), s), scale_encode, s);
auto dec = from_fp8(scales, w.dtype(), s);
auto zero = array(0, dec.dtype());
wq = where(
not_equal(dec, zero, s), divide(wq, dec, s), zeros_like(wq, s), s);
wq = multiply(wq, scale_encode, s);
} else {
// convert to e8m0
auto z = array(0, scales.dtype());
Expand All @@ -5350,33 +5355,26 @@ std::vector<array> fp_quantize(
add(exponent, array(1, int32), s),
exponent,
s);
scales = where(equal(scales, z, s), z, exponent, s);
auto zero_max = equal(scales, z, s);
scales = where(zero_max, z, exponent, s);

wq = divide(wq, power(array(2.0f, w.dtype()), scales, s), s);
scales = astype(add(scales, array(127, int32), s), uint8, s);
scales = where(zero_max, array(0, uint8), scales, s);
}
if (bits == 4) {
auto lut = array({
+0.0f,
+0.5f,
+1.0f,
+1.5f,
+2.0f,
+3.0f,
+4.0f,
+6.0f,
-0.0f,
-0.5f,
-1.0f,
-1.5f,
-2.0f,
-3.0f,
-4.0f,
-6.0f,
});
lut = astype(lut, w.dtype(), s);
wq = argmin(
abs(subtract(expand_dims(wq, -1, s), lut, s), s), -1, false, s);
// Midpoints between the e2m1 magnitudes, ties go to the even code
auto mid = array({0.25f, 0.75f, 1.25f, 1.75f, 2.5f, 3.5f, 5.0f});
mid = astype(mid, w.dtype(), s);
auto wa = abs(wq, s);
auto lo = searchsorted(mid, wa, "left", s);
auto hi = searchsorted(mid, wa, "right", s);
auto odd = astype(bitwise_and(lo, array(1, uint32), s), bool_, s);
auto sign_bit = multiply(
astype(less(wq, array(0, wq.dtype()), s), uint32, s),
array(8, uint32),
s);
wq = add(where(odd, hi, lo, s), sign_bit, s);
auto shifts = power(array(2, uint32), arange(0, 32, 4, uint32, s), s);
wq = reshape(wq, {-1, 4, 8}, s);
wq = sum(multiply(wq, shifts, s), -1, false, s);
Expand Down
20 changes: 20 additions & 0 deletions python/tests/test_quantized.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ def test_mxfp4_quantize_dequantize(self):
w_hat = mx.dequantize(w_q, scales, mode="mxfp4")
self.assertTrue(mx.all(w_hat == 0))

def test_fp4_quantize_ties_to_even(self):
ties = mx.array([0.25, 0.75, 1.25, 1.75, 2.5, 3.5, 5.0])
expected = mx.array([0.0, 1.0, 1.0, 2.0, 2.0, 4.0, 4.0])
w = mx.zeros((3, 32))
w[0, :7] = ties
w[1, :7] = -ties
w[:2, 7] = 6.0
# A NaN should not change the group scale
w[0, 8] = float("nan")
for mode in ["mxfp4", "nvfp4"]:
w_q, scales = mx.quantize(w, mode=mode)
w_hat = mx.dequantize(w_q, scales, mode=mode)
self.assertTrue(mx.array_equal(w_hat[0, :7], expected))
self.assertTrue(mx.array_equal(w_hat[1, :7], -expected))
self.assertEqual(w_q[0, 1].item(), 7)
# FIXME: The zero group checks fail in the CUDA backend.
if not mx.cuda.is_available():
self.assertTrue(mx.all(w_q[:, 2:] == 0))
self.assertTrue(mx.all(scales[2] == 0))

def test_mxfp8_quantize_dequantize(self):
w = 2 * mx.random.uniform(shape=(512, 32)) - 1
w = w.astype(mx.bfloat16)
Expand Down
Loading