Skip to content

Add multivariate distributions and permutations to dpnp.random.Generator - #3160

Open
ndgrigorian wants to merge 14 commits into
feature/device-rng-discretefrom
feature/device-rng-multivariate
Open

ndgrigorian wants to merge 14 commits into
feature/device-rng-discretefrom
feature/device-rng-multivariate

Conversation

@ndgrigorian

@ndgrigorian ndgrigorian commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

This PR completes the NumPy Generator method set in dpnp.random.Generator, as part of the series splitting #3147. It is stacked on #3159

Adds

  • dirichlet, multinomial, multivariate_hypergeometric and multivariate_normal
  • permutation, shuffle and permuted, which argsort random keys
  • choice and bytes

These are built on the kernels of the earlier PRs, so this PR is Python-only

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.22.0dev3=np2py314h8d9cdd5_93 ran successfully.
Passed: 1491
Failed: 0
Skipped: 6

@coveralls

Copy link
Copy Markdown
Collaborator

Coverage Status

Coverage is 82.003% — feature/device-rng-multivariate into feature/device-rng-discrete. No base build found for feature/device-rng-discrete.

@coveralls

coveralls commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

Coverage is 84.847% — feature/device-rng-multivariate into feature/device-rng-discrete. No base build found for feature/device-rng-discrete.

@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-discrete branch from 7918e32 to 66e7a74 Compare November 6, 2026 17:55
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-multivariate branch from 9c726f8 to 2c9b5f0 Compare November 6, 2026 17:55
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/pull/3160/index.html

@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-discrete branch from 6388643 to f48c80e Compare November 8, 2026 21:54
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-multivariate branch from bf0b546 to 9dab4d3 Compare November 8, 2026 21:54
Comment thread dpnp/random/_generator.py Resolved
Comment thread dpnp/random/_generator.py Resolved
Comment thread dpnp/random/_generator.py Resolved
Comment thread dpnp/random/_generator.py Resolved
Comment thread dpnp/random/_generator.py Resolved
Comment thread dpnp/tests/test_random_generator.py Resolved
Comment thread dpnp/random/_generator.py Resolved
Comment thread doc/reference/random.rst Resolved
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-multivariate branch from 9dab4d3 to ebeb60b Compare November 13, 2026 20:06
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-discrete branch 2 times, most recently from dcabbc2 to bb4b69b Compare November 13, 2026 22:41
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-multivariate branch 2 times, most recently from 9b8329f to f8e50e8 Compare November 16, 2026 15:57
Comment thread dpnp/random/_generator.py Resolved
Comment thread dpnp/random/_generator.py Resolved
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-multivariate branch from f8e50e8 to c91c3c8 Compare November 17, 2026 21:13
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-discrete branch from f4938e0 to a64886a Compare November 17, 2026 21:13

@vlad-perevezentsev vlad-perevezentsev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread dpnp/random/_generator.py Resolved
Comment thread dpnp/random/_generator.py Resolved
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-multivariate branch from c91c3c8 to fb583eb Compare November 20, 2026 01:26
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-discrete branch from a64886a to ba52aac Compare November 20, 2026 01:26
Add dirichlet, multinomial, multivariate_hypergeometric,
multivariate_normal, permutation, shuffle, permuted, choice and bytes,
built on the existing kernels.
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-multivariate branch from 4ba2f5b to 9ceb8d2 Compare November 23, 2026 20:18
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-discrete branch from 76467f9 to 2b5045e Compare November 23, 2026 20:18

@vlad-perevezentsev vlad-perevezentsev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The float32 dirichlet comments of the previous round are fixed, and the tests pass everywhere.
A few small comments below:

Comment thread dpnp/random/_generator.py
Comment on lines +2878 to +2883
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread dpnp/random/_generator.py
Comment on lines +2798 to +2799
if dtype == numpy.float32:
return self._dirichlet_log_gamma(alpha, shape, usm_type)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 s

The 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)),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)),

Comment thread dpnp/random/_generator.py
Comment on lines +3293 to +3296
raise TypeError(
f"Cannot cast array data from {x.dtype} to {out.dtype} "
"according to the rule 'safe'"
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants