Repository navigation
Add multivariate distributions and permutations to dpnp.random.Generator - #3160
ndgrigorian wants to merge 13 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.0dev2=np2py314h8d9cdd5_51 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
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.
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.
| if k == 0 or not ( | ||
| alpha.max() < 0.1 or (dtype == numpy.float32 and alpha.sum() < 1) | ||
| ): |
There was a problem hiding this comment.
On the float32 stick-breaking path, a component below about 1e-7 of the stick left becomes exactly 0, since it is a product of 1 - v. With sum(alpha) < 1 this covers common alpha:
import numpy, dpnp
x = dpnp.random.default_rng(1).dirichlet([0.2, 0.15], size=10**6, dtype=dpnp.float32)
y = numpy.random.default_rng(1).dirichlet([0.2, 0.15], size=10**6).astype(numpy.float32)
print(float((x[:, 1] == 0).mean()), float((y[:, 1] == 0).mean())) # 0.047 0.0Could float32 draw log G(a) = log G(a + 1) - E / a (E exponential) and normalize by a softmax? Two kernels, no 0 / 0 rows, small values kept.
| if k == 0 or not ( | ||
| alpha.max() < 0.1 or (dtype == numpy.float32 and alpha.sum() < 1) | ||
| ): |
There was a problem hiding this comment.
In float32, an alpha below the float32 range is checked as nonzero on the host but passed to the beta kernel as 0, so every row is NaN. beta() itself clamps such values to the smallest float32:
import dpnp
rng = dpnp.random.default_rng(0)
print(rng.dirichlet([0.5, 1e-50], size=2, dtype=dpnp.float32)) # all nan; numpy: [[1. 0.] [1. 0.]]Could the zero checks use alpha rounded to dtype?
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