mirror of
https://github.com/ml-explore/mlx.git
synced 2025-11-01 08:38:12 +08:00
Add the roll op (#1455)
This commit is contained in:
committed by
GitHub
parent
f374b6ca4d
commit
9b12093739
@@ -4795,4 +4795,54 @@ void init_ops(nb::module_& m) {
|
||||
Returns:
|
||||
array: The output array.
|
||||
)pbdoc");
|
||||
m.def(
|
||||
"roll",
|
||||
[](const array& a,
|
||||
const IntOrVec& shift,
|
||||
const IntOrVec& axis,
|
||||
StreamOrDevice s) {
|
||||
return std::visit(
|
||||
[&](auto sh, auto ax) -> array {
|
||||
using T = decltype(ax);
|
||||
using V = decltype(sh);
|
||||
|
||||
if constexpr (std::is_same_v<V, std::monostate>) {
|
||||
throw std::invalid_argument(
|
||||
"[roll] Expected two arguments but only one was given.");
|
||||
} else {
|
||||
if constexpr (std::is_same_v<T, std::monostate>) {
|
||||
return roll(a, sh, s);
|
||||
} else {
|
||||
return roll(a, sh, ax, s);
|
||||
}
|
||||
}
|
||||
},
|
||||
shift,
|
||||
axis);
|
||||
},
|
||||
nb::arg(),
|
||||
"shift"_a,
|
||||
"axis"_a = nb::none(),
|
||||
nb::kw_only(),
|
||||
"stream"_a = nb::none(),
|
||||
nb::sig(
|
||||
"def roll(a: array, shift: Union[int, Tuple[int]], axis: Union[None, int, Tuple[int]] = None, /, *, stream: Union[None, Stream, Device] = None) -> array"),
|
||||
R"pbdoc(
|
||||
Roll array elements along a given axis.
|
||||
|
||||
Elements that are rolled beyond the end of the array are introduced at
|
||||
the beggining and vice-versa.
|
||||
|
||||
If the axis is not provided the array is flattened, rolled and then the
|
||||
shape is restored.
|
||||
|
||||
Args:
|
||||
a (array): Input array
|
||||
shift (int or tuple(int)): The number of places by which elements
|
||||
are shifted. If positive the array is rolled to the right, if
|
||||
negative it is rolled to the left. If an int is provided but the
|
||||
axis is a tuple then the same value is used for all axes.
|
||||
axis (int or tuple(int), optional): The axis or axes along which to
|
||||
roll the elements.
|
||||
)pbdoc");
|
||||
}
|
||||
|
||||
@@ -2641,6 +2641,40 @@ class TestOps(mlx_tests.MLXTestCase):
|
||||
out_t = vht_t(xb)
|
||||
np.testing.assert_allclose(out, out_t, atol=1e-4)
|
||||
|
||||
def test_roll(self):
|
||||
x = mx.arange(10).reshape(2, 5)
|
||||
|
||||
for s in [-2, -1, 0, 1, 2]:
|
||||
y1 = np.roll(x, s)
|
||||
y2 = mx.roll(x, s)
|
||||
self.assertTrue(mx.array_equal(y1, y2).item())
|
||||
|
||||
y1 = np.roll(x, (s, s, s))
|
||||
y2 = mx.roll(x, (s, s, s))
|
||||
self.assertTrue(mx.array_equal(y1, y2).item())
|
||||
|
||||
shifts = [
|
||||
1,
|
||||
2,
|
||||
-1,
|
||||
-2,
|
||||
(1, 1),
|
||||
(-1, 2),
|
||||
(33, 33),
|
||||
]
|
||||
axes = [
|
||||
0,
|
||||
1,
|
||||
(1, 0),
|
||||
(0, 1),
|
||||
(0, 0),
|
||||
(1, 1),
|
||||
]
|
||||
for s, a in product(shifts, axes):
|
||||
y1 = np.roll(x, s, a)
|
||||
y2 = mx.roll(x, s, a)
|
||||
self.assertTrue(mx.array_equal(y1, y2).item())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user