mirror of
https://github.com/ml-explore/mlx.git
synced 2025-08-21 12:06:42 +08:00
Fix unsqueeze with None (#899)
* Fix unsqueeze with None * Clean unnecessary files
This commit is contained in:
parent
570f2bf29e
commit
5611e1a95e
@ -367,6 +367,7 @@ array mlx_get_item_nd(array src, const nb::tuple& entries) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool squeeze_needed = false;
|
bool squeeze_needed = false;
|
||||||
|
bool unsqueeze_needed = false;
|
||||||
|
|
||||||
// Slice handling
|
// Slice handling
|
||||||
{
|
{
|
||||||
@ -395,17 +396,19 @@ array mlx_get_item_nd(array src, const nb::tuple& entries) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
axis++;
|
axis++;
|
||||||
|
} else {
|
||||||
|
unsqueeze_needed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
src = slice(src, starts, ends, strides);
|
src = slice(src, starts, ends, strides);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unsqueeze handling
|
// Unsqueeze handling
|
||||||
if (remaining_indices.size() > src.ndim() || squeeze_needed) {
|
if (unsqueeze_needed || squeeze_needed) {
|
||||||
std::vector<int> out_shape;
|
std::vector<int> out_shape;
|
||||||
int axis = 0;
|
int axis = 0;
|
||||||
for (auto& idx : remaining_indices) {
|
for (auto& idx : remaining_indices) {
|
||||||
if (idx.is_none()) {
|
if (unsqueeze_needed && idx.is_none()) {
|
||||||
out_shape.push_back(1);
|
out_shape.push_back(1);
|
||||||
} else if (squeeze_needed && nb::isinstance<nb::int_>(idx)) {
|
} else if (squeeze_needed && nb::isinstance<nb::int_>(idx)) {
|
||||||
axis++;
|
axis++;
|
||||||
|
@ -763,6 +763,10 @@ class TestArray(mlx_tests.MLXTestCase):
|
|||||||
a_sliced_npy = np.asarray(a_sliced_mlx)
|
a_sliced_npy = np.asarray(a_sliced_mlx)
|
||||||
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[None]))
|
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[None]))
|
||||||
|
|
||||||
|
a_sliced_mlx = a_mlx[:, None]
|
||||||
|
a_sliced_npy = np.asarray(a_sliced_mlx)
|
||||||
|
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[:, None]))
|
||||||
|
|
||||||
# Multi dim indexing, all ints
|
# Multi dim indexing, all ints
|
||||||
self.assertEqual(a_mlx[0, 0].item(), 0)
|
self.assertEqual(a_mlx[0, 0].item(), 0)
|
||||||
self.assertEqual(a_mlx[0, 0].ndim, 0)
|
self.assertEqual(a_mlx[0, 0].ndim, 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user