Skip to content

Add continuous distributions to dpnp.random.Generator - #3158

Open
ndgrigorian wants to merge 6 commits into
feature/device-rng-generatorfrom
feature/device-rng-continuous
Open

ndgrigorian wants to merge 6 commits into
feature/device-rng-generatorfrom
feature/device-rng-continuous

Conversation

@ndgrigorian

@ndgrigorian ndgrigorian commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

This PR adds the continuous distributions to dpnp.random.Generator, as part of the series splitting #3147. It is stacked on #3157

The samplers are device ports of NumPy's algorithms:

  • exponential, standard_exponential, pareto, weibull, power, rayleigh
  • gamma, standard_gamma, beta, chisquare, standard_t, f
  • lognormal, standard_cauchy, wald, laplace, gumbel, logistic, triangular, vonmises

Parameters 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

  • 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.0dev2=np2py314h8d9cdd5_26 ran successfully.
Passed: 1491
Failed: 0
Skipped: 6

@coveralls

coveralls commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

Coverage is 84.469% — feature/device-rng-continuous into feature/device-rng-generator. No base build found for feature/device-rng-generator.

@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-generator branch from 9fef498 to 1bd3cee Compare November 6, 2026 14:58
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-continuous branch from adf7178 to cfa00bd 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/3158/index.html

@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-continuous branch from df5cb09 to 7c8127b Compare November 8, 2026 21:54
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-generator branch from 9f9f13e to 88c286e Compare November 8, 2026 21:54
Comment thread doc/reference/random.rst Resolved
@vlad-perevezentsev

Copy link
Copy Markdown
Contributor

Comments on lines outside the diff:

  • dpnp/random/_generator.py:797-1260: The 20 new methods have only a summary and Limitations, while normal, uniform and random in the same class have Parameters, Returns and Examples. dpnp/AGENTS.md asks for these sections, and the dtype/usm_type defaults and the accepted array types are not documented anywhere now. Could they get the same sections as normal?

@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-generator branch from 88c286e to ef331fa Compare November 13, 2026 20:06
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-continuous branch from 7c8127b to 9ed9f9a Compare November 13, 2026 20:06
Comment thread dpnp/random/_generator.py Resolved
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.
@ndgrigorian
ndgrigorian force-pushed the feature/device-rng-continuous branch from 78535ba to 6317de6 Compare November 17, 2026 21:13

@antonwolfy antonwolfy 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.

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.

Comment thread dpnp/random/_generator.py
Comment on lines +1286 to +1287
out : dpnp.ndarray
The samples.

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: 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.

Suggested change
out : dpnp.ndarray
The samples.
out : dpnp.ndarray
The samples.
Raises
------
ValueError
If `a` <= 0, for an `a` on the host.

Comment thread dpnp/random/_generator.py
Comment on lines +1863 to +1864
out : dpnp.ndarray
The samples.

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: 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.

Suggested change
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 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 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)

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.

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>

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.

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 1

Could you check where the wait for these kernels gets lost?

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.

4 participants