Validate num_splits in split (#3234)

Co-authored-by: KD2YCU <[email protected]>
This commit is contained in:
Dan Anderson
2026-03-09 21:47:08 -07:00
committed by GitHub
co-authored by KD2YCU
parent 5a347b2ec8
commit a25399cbd4
3 changed files with 30 additions and 1 deletions
+19
View File
@@ -1328,6 +1328,25 @@ class TestOps(mlx_tests.MLXTestCase):
self.assertEqual(y.tolist(), [1, 2, 3, 4])
self.assertEqual(z.tolist(), [5, 6, 7])
def test_split_invalid_num_splits(self):
"""Regression: split with num_splits <= 0 should raise, not crash."""
a = mx.arange(6)
# num_splits = 0: should raise cleanly (was UB via divide-by-zero)
with self.assertRaises(ValueError):
mx.split(a, 0)
# num_splits = -1: should raise cleanly (was SIGBUS via huge allocation)
with self.assertRaises(ValueError):
mx.split(a, -1)
# Also check with explicit axis
b = mx.zeros((4, 6))
with self.assertRaises(ValueError):
mx.split(b, 0, axis=1)
with self.assertRaises(ValueError):
mx.split(b, -2, axis=0)
def test_arange_overload_dispatch(self):
with self.assertRaises(ValueError):
a = mx.arange(float("nan"), 1, 5)