Skip to content

Conversation

@rcurtin
Copy link
Member

@rcurtin rcurtin commented Jul 3, 2024

This should fix #3723; the behavior of np.array() changed in NumPy 2.0.0 and now throws a ValueError if the copy=False argument is given and a copy cannot be avoided. But we were using copy=False with the intention of "don't copy unless you need to"---where the more correct option is just to pass None instead. According to the documentation that should be backwards-compatible with NumPy 1.x too; CI will tell us if that's true in just a few minutes. :)

@jakirkham if you happen to have a moment to review, it would be appreciated, but no worries if too much is going on. 👍

Copy link
Member

@zoq zoq left a comment

Choose a reason for hiding this comment

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

Works on my end.

@rcurtin
Copy link
Member Author

rcurtin commented Jul 3, 2024

@mlpack-jenkins test this please

Copy link

@jakirkham jakirkham left a comment

Choose a reason for hiding this comment

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

Thanks Ryan! 🙏

Had a question below

Comment on lines 163 to 166
if copy:
out = np.asarray(x, dtype=dtype, copy=True)
else:
out = np.asarray(x, dtype=dtype, copy=None)

Choose a reason for hiding this comment

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

Don't think asarray had the copy argument in NumPy 1. It isn't listed in the NumPy 1 docs. Also NumPy 2 docs note copy is a new argument. Testing local with NumPy 1 am seeing an error. Am I missing something? Or are these meant to be array instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ahh, thank you! I did mean to write array() there, that was the documentation I checked to come up with this fix. Just pushed a fix now.

Choose a reason for hiding this comment

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

All good 😊 Thanks Ryan 🙏

if copy:
out = np.array(x, dtype=dtype, copy=True)
else:
out = np.array(x, dtype=dtype, copy=None)

Choose a reason for hiding this comment

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

Think the None option is new in NumPy 2. It isn't mentioned in NumPy 1. So likely there is another condition needed to check for NumPy 2 and use the copy option then and if not drop the copy argument. That is assuming that is the behavior intended here

Copy link
Member Author

@rcurtin rcurtin Jul 6, 2024

Choose a reason for hiding this comment

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

Thanks! I thought I could get away with it (and was depending on CI to tell me if I was wrong, which it did), but you're right. Should be fixed in 57d2e8b.

@rcurtin rcurtin merged commit dca5b15 into mlpack:master Jul 8, 2024
@rcurtin rcurtin deleted the numpy2 branch July 8, 2024 19:08
@jakirkham
Copy link

Thanks all! 🙏

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NumPy 2.0 support

4 participants