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
Adding writeable
to flags
#2616
Comments
I suppose you know this already? Changing But are you suggesting the opposite operation should be allowed? I could dig this myself, but perhaps it's faster to just ask 🙂: How does this flag get honored at the C level in NumPy? Does every NumPy operation need to consult it? The change sounds like some non-trivial task to me, but perhaps I overthink. FYI, in CUDA there's constant memory guaranteed not writable once initiated, but it's a scarce hardware resource and almost always not enough for serious computations. It'd be nice for |
Right the interest here would be to mark arrays as read-only. 🙂 Yeah NumPy honors this at the C level as well. It does bit masking against the flags (an I'm not sure. Would think That seems like a useful feature to include. Though agree it is a different concern. |
There are some case that NumPy return non-writable ndarray (e.g. |
In addition to RawKernel, I suppose any CUDA libraries that permit in-place operation (like cuFFT) would also require some nontrivial guard. I fantasize asking NVIDIA folks to allow setting a writable flag in the pointer attribute would probably be the easiest (though highly unlikely) approach 🤣 |
Discussed in the dev team today, the reasonable implementation is to to support writable flag only in ElementwiseKernel/ReductionKernel (i.e., writable arrays cannot be specified as an output array). @jakirkham Are there any immediate needs for this? |
It's a good question. Periodically there are situations where one would like to hand a buffer from lower-level code (like C/C++) to a Python user, but the buffer shouldn't be modified by the Python user. Today we solve these issues by copying before handing it off, but it would be nice to save on the copy. |
Another use case is related to
|
I think what @kmaehashi meant is that it is hard to respect that flag in almost all basic CuPy operations except in I think this puts an extra layer of burden in general across all Python GPU libraries. Note that Numba does not respect that flag either. @jakirkham, could you point us to examples where (and how) this is implemented? |
I also noticed that CuPy does not respect the For example, using a copy of jax and jaxlib with GPU support built at github head with CuPy 7.1.1.
"Laundering" the array via CuPy has dropped the readonly flag. |
That's true. Short term it would be good to just error if a read-only array is provided via |
My specific use case is JAX -> CuPy export, and JAX arrays are immutable. I guess I could lie and declare them mutable, and leave it up to the user to deal with the consequences. I note that PyTorch doesn't handle readonly arrays either: |
Neither does Numba handle readonly arrays. I think supporting read-only device arrays across all participating libraries is just too hard. This requires significant infrastructure changes in every library, if this request wasn't taken into consideration in the original design as in JAX. If I were asked, I would have suggested removing this flag from the Taking slicing as an example, how do we respect the following action in CuPy when arr[..., 0:30] = another_arr (Not saying it's impossible; I'll be more than happy to learn if I am wrong and if there's an easy way.) |
cc: @gmarkall @sklam @stuartarchibald for awareness |
At least in the example above, wouldn't it be sufficient to add a check in |
Hi John, sorry I dropped the ball. Indeed, for the simple case here, it might be easy to fix. But aren't this kind of fixes applicable on a case-by-case basis? AFAIK, in CuPy they have at least a few big categories to be considered: ufuncs / elementwise kernels, reduction kernels, raw kernels, and direct interaction with low-level CUDA APIs and libraries. As @kmaehashi pointed out earlier, the first two categories might be fixable (with considerable effort), but when it comes to passing raw pointers around for the rest, I can't imagine how difficult (or easy?) it could be to mark something read-only. For example, in the CUDA C space, even I think Numba needs to provide a reference implementation for the |
NumPy arrays have the
writeable
option inflags
. This can be useful for checking to see if one can write to an array or not. Also it can be useful to ensure an array is not written to (by setting this flag toFalse
). Would be useful to have this flag on CuPy arrays as well.The text was updated successfully, but these errors were encountered: