Skip to content

Add copy keyword to mx.asarray#3510

Open
eyupcanakman wants to merge 1 commit intoml-explore:mainfrom
eyupcanakman:fix/asarray-copy-kwarg-3484
Open

Add copy keyword to mx.asarray#3510
eyupcanakman wants to merge 1 commit intoml-explore:mainfrom
eyupcanakman:fix/asarray-copy-kwarg-3484

Conversation

@eyupcanakman
Copy link
Copy Markdown

Proposed changes

mx.asarray does not accept the copy keyword from the array-API spec. References #3484.

The fix adds copy as a keyword-only argument with the spec semantics: True always copies the input, False never copies and raises ValueError if a copy would be required, and the default None keeps the existing behavior. For copy=True on an mlx array of matching dtype, the binding routes through mx::copy(arr). Note that mlx is lazy and functional, so Copy::eval calls out.copy_shared_buffer(inputs[0]); the post-eval buffer can be shared with the source until a downstream op writes a fresh allocation. This matches the existing __copy__ / __deepcopy__ semantics on mx.array.

The other half of #3484 (from_dlpack in __array_namespace__) is left out. PR #3495 reworks dlpack consumption through the mx.array(...) constructor and the namespace wiring depends on that landing.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

Copy link
Copy Markdown
Collaborator

@zcbenz zcbenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copy parameter only makes sense if the framework supports in-place operation and you want to make a deep copy whose content would stay unchanged when the original copied array got an in-place update. But since MLX has no in-place operation and the supposed use case would never happen.

So I think the best way to support copy parameter is to simply throw when copy=False is passed and return create_array(a, dtype) otherwise like before. Because copy=False means shallow-copy which makes no sense in MLX, and code using copy=True|None would stay semantically correct.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants