jax.random.split fails within io_callback
Description
When using jax.random.split within jax.experimental.io_callback using a shape that is provided as an argument to the callback function, an error occurs.
The shape argument gets parsed into a vanilla numpy array, which seems to be incompatible with jax.random.split.
Any quick fixes for this bug?
The error is reproduced in this very simple Colab notebook: https://colab.research.google.com/drive/1DNOKYVxMyC52sooOl0AzCKNOGFhnLBuX?usp=sharing
What jax/jaxlib version are you using?
jax 0.4.23
Which accelerator(s) are you using?
cpu
Additional system info?
No response
NVIDIA GPU info
For anyone interested, you can simply avoid the issue by parsing the shape argument into an int inside the callback function n = int(n), but still this is not desired behaviour I expect.
Thanks for the report! Agreed the error is confusing, but you can reproduce the core issue this way:
import numpy as np
import jax
n = np.array(10, dtype=int)
key = jax.random.key(0)
jax.random.split(key, n)
The problem is that n is marked as a static argument in split, and static arguments must be hashable, but numpy arrays are not hashable. All arguments passed through to an io_callback function will be numpy arrays, and thus you hit this error. The fix I'd recommend is the one you landed on, passing n = int(n).
Another option would be to close over n in the function passed to io_callback:
return jax.experimental.io_callback(callback=partial(f, n=n), result_shape_dtypes=k, key=k)
What do you think?
Simple enough, thanks for the clarification @jakevdp :)