-
Notifications
You must be signed in to change notification settings - Fork 242
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
Add __array__ method to pyopencl.Array #301
base: main
Are you sure you want to change the base?
Conversation
@inducer Yikes, looks like a bunch of stuff broken in CI that I hope is independent from this change and that probably takes priority over this. However, could I get some comment from you about whether this would be a welcome addition? Thanks! |
CI says the failure is in the test you added:
(You can click through to "view details on Azure Pipelines" from the CI results) I'm not sure I'm a fan of this, as |
I should ask, could you explain the specific need filled by having |
Ooops! Sorry, I somehow didn't find that line in the logs! Re the utility of it: it's common and very useful to allow jni/scikit-image@0c6fb69#diff-28412ceac9e3406ba1842d7ba3938e44R177-R183 Yes, I understand that there are performance considerations, but a certain amount of responsibility should fall on library creators. In this case, because not everything I need is implemented in gputools, some operations have to happen in NumPy, and it'd be great if I could just feed in my pyopencl arrays transparently. This whole endeavour is reasonably contentious, so the smaller I can keep the impact to the code, the better... Thanks! |
|
@inducer Apologies for the churn!
Fixed.
Of course. Thanks for the reminder. =) The Travis failures appear to be NumPy compilation failures, which I think are unrelated? (That's what confused me with the first commit.)
This currently fails with a ValueError in PyOpenCL: In [6]: np.random.random((32, 32)) + arr
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-80631e85a40f> in <module>
----> 1 np.random.random((32, 32)) + arr
~/miniconda3/envs/cf/lib/python3.6/site-packages/pyopencl/array.py in __add__(self, other)
920 else:
921 # add a scalar
--> 922 if other == 0:
923 return self.copy()
924 else:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() This failure is the same with and without this PR. More work needs to be done to support this, which I'd be interested in pursuing but probably belongs in a new PR. I think the correct way to get automatic coercion here is to raise
Only if a specific dtype is requested that is different from the one provided by
|
👍 to this change... It would make dispatching to pyopencl arrays much easier. |
@jni if you think that the CI issue is with numpy compilation, did you try rebasing on master and see if it disappears? |
@inducer, @emmanuelle's suggestion worked! Would you mind giving this another round of review? =) Thank you! |
@inducer do you have any lingering doubts about this? If it doesn't fit with your vision for pyopencl, please let me know, as I can potentially get this done from our end with subclassing or monkey-patching. I just prefer to contribute to the libraries I find useful — but I don't mean to presume what should go in! Thanks again! |
Hello! We are beginning to experiment with OpenCL over at scikit-image (see e.g. scikit-image/scikit-image#4215). For us, it would be ideal
np.asarray(pyopencl_array)
worked transparently. The__array__
method just callsget
, but enables easier compatibility within the NumPy ecosystem.Here's the approach demonstrated with monkeypatching:
For more information, see:
https://docs.scipy.org/doc/numpy/user/basics.dispatch.html#module-numpy.doc.dispatch
https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html
Thank you!