Fix copy bug in mlx.core.asarray - #4036
Closed
aaishwarymishra wants to merge 2 commits into
Closed
Conversation
zcbenz
reviewed
Aug 7, 2026
zcbenz
left a comment
Member
There was a problem hiding this comment.
The reason we throw for copy=False is that, semantically it means the copy would be shallow:
b = array(1)
a = asarray(b, copy=False)
b += 1
assert(a == 2)which is not true in MLX because we don't have in-place updates, and accepting copy=False would just confuse users.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Please include a description of the problem or feature this PR is addressing. If there is a corresponding issue, include the issue #.
This pull request refines how the
mx.asarrayandmx.arrayfunctions handle thecopy=Falseoption when given an existingmx::array. Now, if the input array already matches the requested dtype, a copy is avoided as expected. If a dtype conversion would be needed, an error is raised ifcopy=Falseis specified. The test suite is updated to verify this improved behavior.Improvements to array creation logic
create_arrayinpython/src/convert.cppto only raise an error forcopy=Falsewhen a dtype conversion is required, otherwise returning the original array without copying.mx::arrayinstances in the type dispatch, consolidating logic for clarity and correctness.Test suite updates
test_asarray_copyandtest_asarrayinpython/tests/test_array.pyto confirm that no error is raised whencopy=Falseis used with an array of the same dtype, and that errors are still raised if a dtype conversion is required without copying. [1] [2]## Proposed changesChecklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes