Skip to content

[BUG] __array_namespace__ missing asarray #2945

@billziss-gh

Description

@billziss-gh

Describe the bug
An important operation that is missing from MLX's implementation of __array_namespace__ is asarray. Without this operation array agnostic code cannot easily create new arrays and must resort to awkward code like:

a(Z) if (a := getattr(xp, "asarray", None)) else xp.array(Z)

To Reproduce

>>> import mlx.core as mx
>>> x=mx.array([1,2,3])
>>> x.__array_namespace__().asarray([4,5,6])
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    x.__array_namespace__().asarray([4,5,6])
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'mlx.core' has no attribute 'asarray'. Did you mean: 'array'?

Expected behavior

>>> import mlx.core as mx
>>> x=mx.array([1,2,3])
>>> x.__array_namespace__().asarray([4,5,6])
array([4, 5, 6], dtype=int32)

Desktop:

  • OS Version: Tahoe 26.0.1
  • Version: mlx==0.30.0

Additional context

Related to #48. However that issue does not contain a list of missing APIs, making organized work towards implementing all of them difficult.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions