Repository navigation
Add multivariate distributions and permutations to dpnp.random.Generator - #3160
ndgrigorian wants to merge 14 commits into
Conversation
9fa550e to
bb0eae2
Compare
be00c84 to
742674f
Compare
bb0eae2 to
7918e32
Compare
742674f to
9c726f8
Compare
|
Array API standard conformance tests for dpnp=0.22.0dev3=np2py314h8d9cdd5_93 ran successfully. |
7918e32 to
66e7a74
Compare
9c726f8 to
2c9b5f0
Compare
|
View rendered docs @ https://intelpython.github.io/dpnp/pull/3160/index.html |
6388643 to
f48c80e
Compare
bf0b546 to
9dab4d3
Compare
9dab4d3 to
ebeb60b
Compare
dcabbc2 to
bb4b69b
Compare
9b8329f to
f8e50e8
Compare
f8e50e8 to
c91c3c8
Compare
f4938e0 to
a64886a
Compare
vlad-perevezentsev
left a comment
There was a problem hiding this comment.
I tested the new changes on GPU and CPU, on an Iris Xe GPU without fp64, on Windows with an Arc B390 and with free-threaded Python 3.14t: everything from the previous round is fixed, and the tests pass. Two small float32 dirichlet comments below.
c91c3c8 to
fb583eb
Compare
a64886a to
ba52aac
Compare
Add dirichlet, multinomial, multivariate_hypergeometric, multivariate_normal, permutation, shuffle, permuted, choice and bytes, built on the existing kernels.
A float32 row is 0 / 0 only when all its gamma variates underflow, which depends on sum(alpha). alpha.max() < 1 launched one beta kernel per component for many components of alpha 0.1 to 1.
4ba2f5b to
9ceb8d2
Compare
76467f9 to
2b5045e
Compare
vlad-perevezentsev
left a comment
There was a problem hiding this comment.
The float32 dirichlet comments of the previous round are fixed, and the tests pass everywhere.
A few small comments below:
| e /= _on_device(numpy.where(nonzero, alpha, 1)) | ||
| e = dpnp.minimum(e, numpy.finfo(dtype).max) | ||
| log_g -= e | ||
| log_g -= _on_device(numpy.where(nonzero, 0, numpy.inf).astype(dtype)) | ||
|
|
||
| log_g -= log_g.max(axis=-1, keepdims=True) |
There was a problem hiding this comment.
For tiny alpha, E / alpha is clamped to the largest float32, so both components of a row get the same value and the softmax splits the row in half. With one nonzero alpha below about 6e-8, log(0) of a zero exponential gives NaN rows:
import dpnp
x = dpnp.random.default_rng(0).dirichlet([1e-40, 1e-40], size=10**5, dtype=dpnp.float32)
print(float((x.max(axis=1) < 0.99).mean())) # 0.93 on GPU and CPU; numpy: 0.0
y = dpnp.random.default_rng(1).dirichlet([1e-8, 0.0], size=2**24, dtype=dpnp.float32)
print(int(dpnp.isnan(y).any(axis=1).sum())) # 1; numpy: 0, every row is [1, 0]Could a clamped row go to the component with the smallest log(E) - log(alpha), and a single nonzero alpha give its one-hot row without sampling?
| if dtype == numpy.float32: | ||
| return self._dirichlet_log_gamma(alpha, shape, usm_type) |
There was a problem hiding this comment.
Float64 still breaks a stick here: one beta kernel per component, and the later components are exactly 0 far more often than the first one (the true share is about 0). The new float32 path has neither problem:
import time, dpnp
rng = dpnp.random.default_rng(0)
for dt in (dpnp.float32, dpnp.float64):
x = rng.dirichlet([0.05] * 5, size=10**6, dtype=dt)
print(dt.__name__, (x == 0).mean(axis=0))
for dt in (dpnp.float32, dpnp.float64):
rng.dirichlet([0.05] * 1000, size=10, dtype=dt)
t = time.perf_counter()
rng.dirichlet([0.05] * 1000, size=2000, dtype=dt).sycl_queue.wait()
print(dt.__name__, f"{time.perf_counter() - t:.3f} s")
# float32 [0.004388 0.004518 0.004515 0.004404 0.004438]
# float64 [0. 0.000146 0.001125 0.009427 0.086724]
# B570 GPU: float32 0.002 s, float64 0.287 sThe values differ from NumPy's anyway. Could float64 take the log path too?
| ("multivariate_hypergeometric", ([[1, 2]], 1)), | ||
| ("multivariate_hypergeometric", ([1, -2], 1)), | ||
| ("multivariate_hypergeometric", ([1, 2], 4)), | ||
| ("multivariate_hypergeometric", ([10**9], 1)), |
There was a problem hiding this comment.
Missed in my earlier rounds: three error paths of multivariate_hypergeometric have no test (--cov-branch on the OpenCL CPU): nsample above int64, ragged colors, and sum(colors) above int64. Their messages match NumPy's, so three more cases in this list would keep it so:
("multivariate_hypergeometric", ([1, 2], 2**63)),
("multivariate_hypergeometric", ([[1], [2, 3]], 1)),
("multivariate_hypergeometric", ([2**62, 2**62], 1)),| raise TypeError( | ||
| f"Cannot cast array data from {x.dtype} to {out.dtype} " | ||
| "according to the rule 'safe'" | ||
| ) |
There was a problem hiding this comment.
Nit: Missed in my earlier rounds: NumPy writes the dtypes here with repr: Cannot cast array data from dtype('float64') to dtype('int64') according to the rule 'safe'. dpnp prints from float64 to int64. {x.dtype!r} and {out.dtype!r} would give NumPy's text.
This PR completes the NumPy
Generatormethod set indpnp.random.Generator, as part of the series splitting #3147. It is stacked on #3159Adds
dirichlet,multinomial,multivariate_hypergeometricandmultivariate_normalpermutation,shuffleandpermuted, which argsort random keyschoiceandbytesThese are built on the kernels of the earlier PRs, so this PR is Python-only