Skip to content
Closed
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
1 change: 1 addition & 0 deletions mlx/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,7 @@ class Scan : public UnaryPrimitive {

DEFINE_VMAP()
DEFINE_GRADS();
DEFINE_INPUT_OUTPUT_SHAPE()

const char* name() const override {
switch (reduce_type_) {
Expand Down
33 changes: 33 additions & 0 deletions python/tests/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,39 @@ def fun(x):

self.assertEqual(mx.compile(fun, shapeless=True)(x).shape, (1, 1, 4, 32))

def test_shapeless_compile_scan(self):
@partial(mx.compile, shapeless=True)
def fun(x):
return mx.cumsum(x, axis=0)

x1 = mx.arange(4, dtype=mx.float32)
out1 = fun(x1)
mx.eval(out1)
self.assertTrue(mx.array_equal(out1, mx.array([0.0, 1.0, 3.0, 6.0])))

x2 = mx.arange(6, dtype=mx.float32)
out2 = fun(x2)
mx.eval(out2)
self.assertTrue(
mx.array_equal(out2, mx.array([0.0, 1.0, 3.0, 6.0, 10.0, 15.0]))
)

@partial(mx.compile, shapeless=True)
def fun2d(x):
return mx.cumsum(x, axis=-1)

x2d = mx.ones((2, 3), dtype=mx.float32)
out2d = fun2d(x2d)
mx.eval(out2d)
self.assertTrue(
mx.array_equal(out2d, mx.array([[1.0, 2.0, 3.0], [1.0, 2.0, 3.0]]))
)

x2d_larger = mx.ones((3, 5), dtype=mx.float32)
out2d_larger = fun2d(x2d_larger)
mx.eval(out2d_larger)
self.assertEqual(out2d_larger.shape, (3, 5))

def test_shapeless_compile_gather(self):
x = mx.zeros((1, 1, 32))

Expand Down
12 changes: 12 additions & 0 deletions tests/compile_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,18 @@ TEST_CASE("test shapeless compile") {
out2 = cfun({array({1.0, 2.0}, {1, 2})})[0];
CHECK_NE(out.inputs()[1].id(), out2.inputs()[1].id());
}

{
auto compile_shapeless_scan = [](const std::vector<array>& inputs) {
return std::vector<array>{cumsum(inputs[0], 0)};
};
auto cfun = compile(compile_shapeless_scan, /* shapeless */ true);
auto out1 = cfun({array({1.0f, 2.0f, 3.0f})})[0];
CHECK(array_equal(out1, array({1.0f, 3.0f, 6.0f})).item<bool>());

auto out2 = cfun({array({1.0f, 2.0f, 3.0f, 4.0f})})[0];
CHECK(array_equal(out2, array({1.0f, 3.0f, 6.0f, 10.0f})).item<bool>());
}
}

auto compile_broadcast_add(const std::vector<array>& inputs) {
Expand Down