8000 BUG: Set writeable flag for writeable dlpacks. by karl3wm · Pull Request #28600 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Set writeable flag for writeable dlpacks. #28600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025

Conversation

karl3wm
Copy link
@karl3wm karl3wm commented Mar 28, 2025

Explicitly set the writeable flag in from_dlpack as the inverse of the dlpack readable flag. Previously it was not actually being set.

Fixes #28599

Copy link
Member
@seberg seberg left a comment

Choose a reason for hiding this comment

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

Thanks, this looks right to me, sorry about that!

I am actually not sure we want to change bahvior for dlpack <v1 (i.e. unversioned path). Could you check if older NumPy versions returned read-only arrays (I think this did)? I think the old path should maybe set readonly=1/True just to keep behavior (by now the new path should be taken anyway).

I suspect it's not tested, as you would have to do add a helper like:

class myarr:
    def __dlpack__(self):
        return np.array([1]).__dlpack__(max_version=None)

to avoid getting the new (versioned) protocol.

(The error comes from writeable being default for new allocations, but not for wrapped ones data!=NULL, I suspect it was always slightly off, but before the new path it didn't matter.)

@seberg seberg added the 09 - Backport-Candidate PRs tagged should be backported label Mar 28, 2025
@karl3wm
Copy link
Author
karl3wm commented Mar 28, 2025

Thanks for your rapid comment!

This is likely the first time from_dlpack has returned something writeable, but I did test this as you recommend.

  • numpy 2.1.3: Read-only array returned with max_version=None.
  • numpy 2.0.2: max_version not supported. Read-only array returned.
  • numpy 1.26.4: max_version not supported. Read-only array returned.
  • older versions: not compatible with my python 3.12 installation

It doesn't look like these flags have been changed since they were written in 3163d57#diff-b6927246f0fd28501d169c22cbbdd27e319b8c4659f23bb6b9423f336c2818ebR4349 . fb60521 added the readonly state but only cleared the writeable flag in response to it (it was already unset).

I've changed the readonly state to always be readonly for older unversioned dlpacks, respecting the old behavior.

I'll squash these changes.

@karl3wm karl3wm force-pushed the writeable-from-dlpack branch from 5156275 to a36442b Compare March 28, 2025 11:07
@seberg
Copy link
Member
seberg commented Mar 28, 2025

Perfect, thanks! I think the old behavior may have unset it twice effectively and I didn't notice when adding the new one.
I think the release note is a bit more than needed, but happy to keep it, it is a pretty big bug w.r.t. to this :).

Copy link
Member
@seberg seberg left a comment

Choose a reason for hiding this comment

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

Will merge very soon if no one comments, thanks!

Explicitly set the writeable flag in from_dlpack as the inverse of the
dlpack read_only flag. Previously it was not actually being set.

Additionally, update the readonly logic such that legacy unversioned
DLPacks are never writeable, for compatibility with old behavior.

Fixes numpy#28599
@karl3wm karl3wm force-pushed the writeable-from-dlpack branch from a36442b to 155f32b Compare March 29, 2025 06:56
@karl3wm
Copy link
Author
karl3wm commented Mar 29, 2025

Thanks for your comments. I removed the release note, could also add it back shorter.

@melissawm melissawm moved this to Awaiting a code review in NumPy first-time contributor PRs Mar 31, 2025
@charris charris merged commit a9a9bf8 into numpy:main Mar 31, 2025
72 checks passed
@github-project-automation github-project-automation bot moved this from Awaiting a code review to Completed in NumPy first-time contributor PRs Mar 31, 2025
@charris
Copy link
Member
charris commented Mar 31, 2025

Thanks @karl3wm .

@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Apr 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

Successfully merging this pull request may close these issues.

BUG: .from_dlpack never sets writeable flag
3 participants
0