Repository navigation
Add continuous distributions to dpnp.random.Generator - #3158
ndgrigorian wants to merge 6 commits into
Conversation
dbdb2c8 to
3963d20
Compare
0160558 to
adf7178
Compare
3963d20 to
6c3ff46
Compare
|
Array API standard conformance tests for dpnp=0.22.0dev2=np2py314h8d9cdd5_26 ran successfully. |
9fef498 to
1bd3cee
Compare
adf7178 to
cfa00bd
Compare
|
View rendered docs @ https://intelpython.github.io/dpnp/pull/3158/index.html |
df5cb09 to
7c8127b
Compare
9f9f13e to
88c286e
Compare
|
Comments on lines outside the diff:
|
88c286e to
ef331fa
Compare
7c8127b to
9ed9f9a
Compare
Port NumPy's continuous samplers to device kernels: exponential, gamma, beta, chisquare, standard_t, f, lognormal, standard_cauchy, wald, pareto, weibull, power, rayleigh, laplace, gumbel, logistic, triangular and vonmises. The rejection samplers return NaN for device parameters that would make them loop forever.
Also pass the dpnp array straight to assert_allclose in the chisquare test.
Give the Generator methods from lognormal to beta the Parameters, Returns and Examples sections that normal has, with their dtype, usm_type and out defaults. The examples check statistics of 10**5 samples, so they hold for any stream.
78535ba to
6317de6
Compare
antonwolfy
left a comment
There was a problem hiding this comment.
Docs of the continuous distributions checked against NumPy 2.5.5: every method links to NumPy's page and states NumPy's parameter constraints. Only the two Raises sections below are missing.
| out : dpnp.ndarray | ||
| The samples. |
There was a problem hiding this comment.
Nit: NumPy's Raises section is missing. NumPy documents a ValueError for a <= 0, which is raised here the same way for an a on the host (_CONS_POSITIVE in _check_constraint); an a on the device isn't checked, as the Generator notes say.
| out : dpnp.ndarray | |
| The samples. | |
| out : dpnp.ndarray | |
| The samples. | |
| Raises | |
| ------ | |
| ValueError | |
| If `a` <= 0, for an `a` on the host. |
| out : dpnp.ndarray | ||
| The samples. |
There was a problem hiding this comment.
Nit: NumPy's Raises section is missing here too. NumPy documents a ValueError when df <= 0 or for an inappropriate size such as -1; both are raised here the same way, df <= 0 for a df on the host.
| out : dpnp.ndarray | |
| The samples. | |
| out : dpnp.ndarray | |
| The samples. | |
| Raises | |
| ------ | |
| ValueError | |
| When `df` <= 0, for a `df` on the host, or when an inappropriate | |
| `size` (e.g. ``size=-1``) is given. |
vlad-perevezentsev
left a comment
There was a problem hiding this comment.
I tested the PR on Linux and Windows, on GPU and CPU, on a GPU without fp64 (Iris Xe) and with free-threaded Python 3.14t, and the samples match across them. Two comments below: the KS tests without fp64 and a race with threads. Agreed with @antonwolfy on the Raises sections.
| @@ -78,7 +78,6 @@ def test_uniform_ks(self): | |||
| @testing.parameterize(*common_distributions.exponential_params) | |||
There was a problem hiding this comment.
On a GPU without fp64 (Iris Xe), the *_ks tests of the classes enabled here fail: 22 of them in TestExponential, TestBeta, TestGamma, TestPower, TestChisquare and TestF. These methods return float32 there and NumPy float64, and two_sample_Kolmogorov_Smirnov_test asserts equal dtypes (common_distributions.py:17):
pytest dpnp/tests/third_party/cupy/random_tests/test_generator_api.py
# Iris Xe, level_zero: 22 failed, AssertionError: observed1.dtype == observed2.dtype
Could _check_ks cast the NumPy sample to vals_cupy.dtype when no dtype is given?
|
|
||
| // F with `dfnum` and `dfden` degrees of freedom | ||
| template <typename T> | ||
| class FSampler : public SamplerBase<T, T, T> |
There was a problem hiding this comment.
With threads sharing one Generator, asnumpy right after f, vonmises or beta sometimes returns a partly written result: the copy runs before the sampling kernel has finished, though asnumpy calls queue.wait() first. normal, random, laplace and other dpnp kernels (cumsum, sort, matmul) don't show it:
import threading
import numpy, dpnp
g = dpnp.random.default_rng(1234)
got = []
def work():
for _ in range(4):
x = g.f(3.0, 4.0, size=4096)
got.append((dpnp.asnumpy(x), x)) # asnumpy() calls queue.wait() first
for _ in range(20):
ts = [threading.Thread(target=work) for _ in range(32)]
for t in ts:
t.start()
for t in ts:
t.join()
g.sycl_queue.wait()
print(sum(not numpy.array_equal(a, dpnp.asnumpy(x)) for a, x in got), "of", len(got))
# GPU Max: 3 to 7 of 2560 on Python 3.14, 15 to 29 on 3.14t; CPU: 0 to 1Could you check where the wait for these kernels gets lost?
This PR adds the continuous distributions to
dpnp.random.Generator, as part of the series splitting #3147. It is stacked on #3157The samplers are device ports of NumPy's algorithms:
exponential,standard_exponential,pareto,weibull,power,rayleighgamma,standard_gamma,beta,chisquare,standard_t,flognormal,standard_cauchy,wald,laplace,gumbel,logistic,triangular,vonmisesParameters on the device aren't checked, since that would wait for the device. The rejection samplers instead return NaN for parameters that would make them loop forever, including valid ones that round badly in single precision