Skip to content

Commit 2bad04f

Browse files
author
obchain
authored
Guard scatter_axis against 64-bit outputs on the GPU (#3695)
1 parent 3f7932e commit 2bad04f

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

mlx/backend/metal/indexing.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,12 @@ void GatherAxis::eval_gpu(const std::vector<array>& inputs, array& out) {
519519
}
520520

521521
void ScatterAxis::eval_gpu(const std::vector<array>& inputs, array& out) {
522+
if (size_of(out.dtype()) == 8) {
523+
std::ostringstream msg;
524+
msg << "[ScatterAxis::eval_gpu] Does not support " << out.dtype();
525+
throw std::invalid_argument(msg.str());
526+
}
527+
522528
auto& src = inputs[0];
523529
auto& idx = inputs[1];
524530
auto& upd = inputs[2];

python/tests/test_ops.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,20 @@ def test_put_along_axis(self):
13531353
self.assertEqual(b.size, 0)
13541354
self.assertEqual(b.shape, a.shape)
13551355

1356+
# 64-bit outputs are not supported by the Metal scatter and should
1357+
# raise a clean error rather than failing the Metal JIT build. The CPU
1358+
# and CUDA backends handle them fine.
1359+
idx = mx.array([[0], [1], [2], [3]])
1360+
for dt in (mx.int64, mx.uint64):
1361+
x = mx.zeros((4, 8), dtype=dt)
1362+
upd = mx.ones((4, 1), dtype=dt)
1363+
if mx.metal.is_available():
1364+
with self.assertRaises(ValueError):
1365+
mx.eval(mx.put_along_axis(x, idx, upd, axis=1, stream=mx.gpu))
1366+
out = mx.put_along_axis(x, idx, upd, axis=1, stream=mx.cpu)
1367+
self.assertEqual(out.dtype, dt)
1368+
mx.eval(out)
1369+
13561370
def test_split(self):
13571371
a = mx.array([1, 2, 3])
13581372
splits = mx.split(a, 3)

0 commit comments

Comments
 (0)