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: 10 additions & 6 deletions python/src/ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1473,21 +1473,25 @@ void init_ops(nb::module_& m) {
m.def(
"arange",
[](Scalar start,
Scalar stop,
std::optional<Scalar> stop,
const std::optional<Scalar>& step,
const std::optional<mx::Dtype>& dtype_,
mx::StreamOrDevice s) {
if (!stop) {
stop = start;
start = 0;
}
// Determine the final dtype based on input types
mx::Dtype dtype = dtype_
? *dtype_
: mx::promote_types(
scalar_to_dtype(start),
step ? mx::promote_types(
scalar_to_dtype(stop), scalar_to_dtype(*step))
: scalar_to_dtype(stop));
scalar_to_dtype(*stop), scalar_to_dtype(*step))
: scalar_to_dtype(*stop));
return mx::arange(
scalar_to_double(start),
scalar_to_double(stop),
scalar_to_double(*stop),
step ? scalar_to_double(*step) : 1.0,
dtype,
s);
Expand All @@ -1499,7 +1503,7 @@ void init_ops(nb::module_& m) {
nb::kw_only(),
"stream"_a = nb::none(),
nb::sig(
"def arange(start : Union[int, float], stop : Union[int, float], step : Union[None, int, float], dtype: Optional[Dtype] = None, *, stream: Union[None, Stream, Device] = None) -> array"),
"def arange(start : Union[int, float], stop : Union[None, int, float], step : Union[None, int, float], dtype: Optional[Dtype] = None, *, stream: Union[None, Stream, Device] = None) -> array"),
R"pbdoc(
Generates ranges of numbers.

Expand All @@ -1508,7 +1512,7 @@ void init_ops(nb::module_& m) {

Args:
start (float or int, optional): Starting value which defaults to ``0``.
stop (float or int): Stopping value.
stop (float or int, optional): Stopping value.
step (float or int, optional): Increment which defaults to ``1``.
dtype (Dtype, optional): Specifies the data type of the output. If unspecified will default to ``float32`` if any of ``start``, ``stop``, or ``step`` are ``float``. Otherwise will default to ``int32``.

Expand Down
4 changes: 4 additions & 0 deletions python/tests/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,10 @@ def test_arange_overload_dispatch(self):
expected = [0, -1, -2]
self.assertListEqual(a.tolist(), expected)

a = mx.arange(-3, None, -1)
expected = [0, -1, -2]
self.assertListEqual(a.tolist(), expected)

a = mx.arange(stop=2, step=0.5)
expected = [0, 0.5, 1.0, 1.5]
self.assertListEqual(a.tolist(), expected)
Expand Down
Loading