Skip to content

[BUG] __setitem__ with a bare ellipsis fails to broadcast leading singleton dimensions #4560

Description

@shaeantoine

☑️ I understand it is strictly prohibited to use AI to write issues.

Describe the bug
__setitem__ has inconsistent behavior when using a bare ellipsis. When I attempted to broadcast an array with leading singleton dimensions (for instance (1,3) to a (3,)), __setitem__ threw a value error when using a bare ellipsis (a[...]). However, I don't encounter the same bug if I use a tuple wrapped ellipsis (a[(...,)]) or full slice (a[:]). All three should have consistent behavior.

To Reproduce

>>> import mlx.core as mx
>>> a = mx.arange(3)
>>> a[...] = mx.array([[1, 2, 3]])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    a[...] = mx.array([[1, 2, 3]])
    ~^^^^^
ValueError: Cannot broadcast array of shape (1,3) into shape (3).
>>> a[(...,)] = mx.array([[1, 2, 3]]) 
>>> a
array([1, 2, 3], dtype=int32)
>>> a[:] = mx.array([[1, 2, 3]]) 
>>> a
array([1, 2, 3], dtype=int32)

Expected behavior
__setitem__ with a bare ellipsis should have consistent behavior with a full slice or tuple based ellipsis. The leading singleton dimensions should be able to be squeezed since there is no loss of information. So I should be able to do the following:

>>> import mlx.core as mx
>>> a = mx.arange(3)
>>> a[...] = mx.array([[1, 2, 3]])
>>> a
array([1, 2, 3], dtype=int32)
>>> a[(...,)] = mx.array([[1, 2, 3]]) 
>>> a
array([1, 2, 3], dtype=int32)
>>> a[:] = mx.array([[1, 2, 3]]) 
>>> a
array([1, 2, 3], dtype=int32)

Desktop and MLX Version:

  • OS Version: MacOS 26.5.2
  • MLX Version: 0.32.2

Additional context
This issue was found when reviewing issue #4358 (now closed). That issue was solved by the earlier fix #4314 raised by the error #4313. This issue can be thought of as a remaining edge case from the original problem addressed by these earlier tickets.

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

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions