ml-explore / ml-explore/mlx

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

Open
#4,399 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug low priority
Dominant language
C++
Stars
28.5k
Forks
2.3k
Avg merge
3d 8h
Merged PRs (30d)
62

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the mixed slice and array-index handling in the getitem and setitem paths, especially where adjusted bounds are passed to arange(). Compare the reproduction with NumPy for heavily negative and out-of-range bounds. Done means both paths clamp slice bounds to the valid axis range and a regression test covers the mismatch described here.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.