EagerJAXArrayContext: return mutable numpy array in to_numpy#315
EagerJAXArrayContext: return mutable numpy array in to_numpy#315matthiasdiener wants to merge 2 commits intoinducer:mainfrom
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR modifies the to_numpy method in the JAX array context to return a mutable NumPy array by explicitly copying the result of jax.device_get.
- Wraps
jax.device_getresult innp.copyto produce a writable array.
Comments suppressed due to low confidence (1)
arraycontext/impl/jax/init.py:104
- The code uses
np.copybutnumpyis not imported in this scope, which will raise aNameError. Addimport numpy as npat the module level or within the function.
return np.copy(jax.device_get(ary))
|
I'm a bit out of the loop, so I have questions 😁
|
|
Thanks for the review @alexfikl !
There are in-place modifications in grudge, e.g. Without this PR, some tests in grudge with Jax fail (see e.g. inducer/grudge#380).
I think there are copies done, both for CPU and GPU, the problem is that |
I remember writing some version of that code.. it should really be rewritten in a nicer way :( But fair enough, I can see how that breaks. Are there any other places? (the tests in inducer/grudge#380 seem to be passing nicely at the moment) EDIT: It wasn't running tests with jax, I see them now! 😁 |
5d68e6e to
4386b19
Compare
alexfikl
left a comment
There was a problem hiding this comment.
I would personally prefer if this gets fixed in grudge, since it's just that one place that seems to fail (?). Doing two copies on to_numpy here seems like a hack.
If we do go for it, we should also document it somewhere that the arrays returned by to_numpy are writable, since it's apparently not a given!
I agree, and I've just added a (sketchy) reimplementation in inducer/grudge#380, feel free to take a look. Setting this PR back to draft.
Right, either way of what happens with this PR, we should document this in |
AFAICS, there is no way to make an immutable numpy array mutable again except for copying it.