-
-
Notifications
You must be signed in to change notification settings - Fork 5k
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
ENH: _array_api.Generator: unified RNG interface #20549
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
|
||
import os | ||
import warnings | ||
import random | ||
|
||
import numpy as np | ||
|
||
|
@@ -354,3 +355,61 @@ def xp_unsupported_param_msg(param): | |
|
||
def is_complex(x, xp): | ||
return xp.isdtype(x.dtype, 'complex floating') | ||
|
||
|
||
# Random Number Generation | ||
|
||
class Generator: | ||
def __new__(cls, xp, seed=None): | ||
if is_numpy(xp) or is_cupy(xp): | ||
# could maybe just return xp.default_rng(seed) | ||
return super().__new__(_Generator_numpy_cupy) | ||
elif is_torch(xp): | ||
return super().__new__(_Generator_torch) | ||
# elif is_jax(xp): | ||
# return super().__new__(_Generator_jax) | ||
else: | ||
message = f"`Generator` for {xp} is not implemented." | ||
raise NotImplementedError(message) | ||
|
||
|
||
class _Generator_jax(Generator): | ||
def __init__(self, xp, seed=None): | ||
min = -2 ** 63 | ||
seed = random.randint(min, -min - 1) if seed is None else seed | ||
self._xp = xp | ||
self._key = xp.random.key(seed) | ||
|
||
def _next(self): | ||
key, subkey = self._xp.random.split(self._key) | ||
self._key = subkey | ||
return key | ||
|
||
def random(self, shape=None, dtype=None): | ||
key = self._next() | ||
shape = () if shape is None else shape | ||
return self._xp.random.uniform(key, shape, dtype=dtype) | ||
|
||
|
||
class _Generator_torch(Generator): | ||
def __init__(self, xp, seed=None): | ||
rng = xp.Generator() | ||
seed = rng.seed() if seed is None else seed | ||
rng.manual_seed(seed) | ||
self._rng = rng | ||
self._xp = xp | ||
|
||
def random(self, shape=None, dtype=None): | ||
shape = () if shape is None else shape | ||
return self._xp.rand(shape, generator=self._rng, dtype=dtype) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm seeing a few new test failures in the array API suite with these changes and
A small patch seems to return us to baseline: --- a/scipy/_lib/_array_api.py
+++ b/scipy/_lib/_array_api.py
@@ -393,7 +393,7 @@ class _Generator_jax(Generator):
class _Generator_torch(Generator):
def __init__(self, xp, seed=None):
- rng = xp.Generator()
+ rng = xp.Generator(device=SCIPY_DEVICE)
seed = rng.seed() if seed is None else seed
rng.manual_seed(seed)
self._rng = rng There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, that's why I suggested that we might want to add a |
||
|
||
|
||
class _Generator_numpy_cupy(Generator): | ||
def __init__(self, xp, seed=None): | ||
self._xp = xp | ||
self._rng = xp.random.default_rng(seed) | ||
|
||
def random(self, shape=None, dtype=None): | ||
# (for NumPy) ensure output is not Python float | ||
temp = self._rng.random(size=shape, dtype=dtype) | ||
return self._xp.asarray(temp, dtype=dtype)[()] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uncomment after gh-20085 merges.