Skip to content

[BUG] Mixed slice + fancy indexing produces wrong shape/garbage data for out-of-range slice bounds #4399

Description

@Adityaj0

Describe the bug

When a slice is combined with an array/int index (e.g. a[start:stop, idx_array]), the slice bounds are adjusted for negative indices but never clamped into the valid [0, axis_size] range before being passed to arange(). Out-of-range or heavily negative slice bounds produce arrays of the wrong shape containing bogus/repeated data instead of matching NumPy's clamping behavior. A sufficiently large negative start could also attempt to allocate a huge array.

To Reproduce

import mlx.core as mx
import numpy as np

a_npy = np.arange(20, dtype=np.int32).reshape(4, 5)
a_mlx = mx.array(a_npy)
idx = mx.array([0, 1], dtype=mx.uint32)

out_mlx = a_mlx[-100:4, idx]
out_npy = a_npy[-100:4, np.array([0, 1])]
print(out_mlx.shape, out_npy.shape)  # mismatched shapes / wrong data

Expected behavior
Slice bounds should be clamped into [0, axis_size] the same way NumPy does, for both getitem and setitem paths.

Fix
Fix + regression test up in #4397.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions