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
35 changes: 35 additions & 0 deletions integration_tests/linspace_test.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 3 additions & 3 deletions keras_mlx/src/ops/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion keras_mlx/src/ops/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def relu6(x):


def sigmoid(x):
x = convert_to_tensor(x)
x = promote_to_float(x)
return mx.sigmoid(x)


Expand Down
41 changes: 22 additions & 19 deletions keras_mlx/src/ops/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading