diff --git a/integration_tests/linspace_test.py b/integration_tests/linspace_test.py new file mode 100644 index 0000000..535568e --- /dev/null +++ b/integration_tests/linspace_test.py @@ -0,0 +1,35 @@ +"""Tests for `linspace` cases the keras op tests do not cover.""" + +import mlx.core as mx +import numpy as np +from absl.testing import parameterized + +from keras import ops +from keras.src import testing + + +class LinspaceTest(testing.TestCase): + def test_traced_scalar_bounds(self): + f = mx.compile(lambda a, b: ops.linspace(a, b, 5)) + self.assertAllClose( + f(mx.array(-1.0), mx.array(3.0)), np.linspace(-1.0, 3.0, 5) + ) + + @parameterized.named_parameters( + ("negative_int", -3, 4, 4, "int32"), + ("int_array", np.array([0, 0]), np.array([10, -7]), 4, "int32"), + ("bool", 0, 1, 3, "bool"), + ) + def test_non_float_dtype(self, start, stop, num, dtype): + self.assertAllEqual( + ops.linspace(start, stop, num, dtype=dtype), + np.linspace(start, stop, num, dtype=dtype), + ) + + def test_retstep_single_sample(self): + _, step = ops.linspace(0.0, 1.0, 1, retstep=True) + self.assertTrue(np.isnan(step)) + + def test_negative_num_raises(self): + with self.assertRaisesRegex(ValueError, "num=-1"): + ops.linspace(0.0, 1.0, -1) diff --git a/keras_mlx/src/ops/math.py b/keras_mlx/src/ops/math.py index 7d1951a..3cc966c 100644 --- a/keras_mlx/src/ops/math.py +++ b/keras_mlx/src/ops/math.py @@ -530,17 +530,17 @@ def istft( def rsqrt(x): - x = convert_to_tensor(x) + x = promote_to_float(x) return mx.rsqrt(x) def erf(x): - x = convert_to_tensor(x) + x = promote_to_float(x) return mx.erf(x) def erfinv(x): - x = convert_to_tensor(x) + x = promote_to_float(x) return mx.erfinv(x) diff --git a/keras_mlx/src/ops/nn.py b/keras_mlx/src/ops/nn.py index 8c1e56a..07d56ca 100644 --- a/keras_mlx/src/ops/nn.py +++ b/keras_mlx/src/ops/nn.py @@ -67,7 +67,7 @@ def relu6(x): def sigmoid(x): - x = convert_to_tensor(x) + x = promote_to_float(x) return mx.sigmoid(x) diff --git a/keras_mlx/src/ops/numpy.py b/keras_mlx/src/ops/numpy.py index 7b3b48c..4cca798 100644 --- a/keras_mlx/src/ops/numpy.py +++ b/keras_mlx/src/ops/numpy.py @@ -787,34 +787,37 @@ def linspace( if dtype is None: dtype = result_type(start.dtype, stop.dtype, float) mlx_dtype = to_mlx_dtype(dtype) - - # mx.linspace takes Python scalars and a plain int for num. num = int(num) - if start.ndim == 0 and stop.ndim == 0: - result = mx.linspace( - start.item(), - stop.item(), - num=num if endpoint else num + 1, - dtype=mlx_dtype, + if num < 0: + raise ValueError( + f"`num` must be a non-negative integer. Received: num={num}" ) + real_dtype = mx.float64 if mlx_dtype == mx.float64 else mx.float32 + if mx.issubdtype(mlx_dtype, mx.complexfloating): + inner_dtype = mx.complex64 else: - start = start.astype(mlx_dtype) - stop = stop.astype(mlx_dtype) - zero_one = mx.linspace( - 0, 1, num=num if endpoint else num + 1, dtype=mlx_dtype - ) - zero_one = zero_one.reshape([-1] + [1] * start.ndim) - result = zero_one * (stop - start)[None] + start[None] - - if not endpoint: - result = result[:-1] + inner_dtype = real_dtype + + # mx.linspace's formula, exact at both ends. mx.linspace itself takes + # only Python scalars, so traced bounds can't call it. + start = start.astype(inner_dtype) + stop = stop.astype(inner_dtype) + divisor = num - 1 if endpoint else num + t = mx.arange(num, dtype=real_dtype) / builtins.max(divisor, 1) + t = t.reshape([-1] + [1] * builtins.max(start.ndim, stop.ndim)) + result = (1 - t) * start + t * stop + if mx.issubdtype(mlx_dtype, mx.integer): + # numpy floors for integer dtypes. + result = mx.floor(result) + result = result.astype(mlx_dtype) if axis != 0: # The samples are generated along axis 0, numpy places them at `axis`. result = mx.moveaxis(result, 0, axis) if retstep: - step = (stop - start) / (num - 1 if endpoint else num) + # numpy gives nan for fewer than two samples. + step = (stop - start) / (divisor if divisor > 0 else math.nan) return result, step return result