Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
cccaaec
Add the continuous distributions to dpnp.random.Generator
ndgrigorian Nov 4, 2026
424dbbb
Test the C-contiguous out check of the continuous samplers
ndgrigorian Nov 7, 2026
9d9722d
Compute vonmises without cancellation for large kappa
ndgrigorian Nov 13, 2026
c25f6e4
Take the beta shortcut for tiny parameters in single precision too
ndgrigorian Nov 13, 2026
9523b7a
Leave the int64 sentinel to the discrete distributions in the docs
ndgrigorian Nov 13, 2026
4c9fd57
Document the parameters of the continuous distributions
ndgrigorian Nov 17, 2026
89342a9
Force inlining into the blocked lognormal, Cauchy and Wald kernels
ndgrigorian Nov 20, 2026
b8809e5
Document the ValueError of power
ndgrigorian Nov 20, 2026
3110878
Document the ValueErrors of chisquare
ndgrigorian Nov 20, 2026
3c36b49
Skip the CuPy KS tests of the continuous distributions without fp64
ndgrigorian Nov 20, 2026
a10c9cf
List the sources of the first Generator methods first in the RNG exte…
ndgrigorian Nov 29, 2026
99446e3
Keep wald finite for a large or infinite scale
ndgrigorian Nov 29, 2026
4b0f401
Keep the float32 u1 of the normal pairs below 1
ndgrigorian Nov 29, 2026
0e9333c
Divide each chi-square of f by its own degrees of freedom
ndgrigorian Nov 29, 2026
ec54827
Raise tiny host parameters of f, standard_t and wald to float32 tiny
ndgrigorian Nov 29, 2026
d4a512d
Test triangular with a parameter on the device
ndgrigorian Nov 29, 2026
eb5d4aa
Test float32 beta with a parameter on the device
ndgrigorian Nov 29, 2026
a3d9ed5
Don't check the method of standard_exponential, as NumPy
ndgrigorian Nov 29, 2026
221fd36
Test the USM type of every continuous distribution
ndgrigorian Nov 29, 2026
b81d1ab
Check pairs of host parameters of triangular with others on the device
ndgrigorian Nov 29, 2026
b436291
Integrate with trapz in test_vonmises on NumPy 1.26
ndgrigorian Nov 29, 2026
0692270
Draw standard_gamma through _cont
ndgrigorian Nov 29, 2026
c00bd9e
Take beta's limits for a zero uniform before the logarithms
ndgrigorian Nov 29, 2026
dfd9d4d
Keep the float32 u1 of the normal pairs below 1 with a select
ndgrigorian Dec 2, 2026
66ec802
Say why normal.cpp is the first source of the RNG extension
ndgrigorian Dec 2, 2026
de1c4e3
Raise a tiny host scale of wald to float32 tiny too
ndgrigorian Dec 2, 2026
c5c0b29
Say that the df of standard_t is raised to twice float32 tiny
ndgrigorian Dec 2, 2026
2a973ea
Take Python scalars without NumPy when raising to float32 tiny
ndgrigorian Dec 2, 2026
3be0868
Reject left > right in triangular with the mode on the device
ndgrigorian Dec 2, 2026
cf83a34
Test float32 beta with a zero U and a tiny b
ndgrigorian Dec 2, 2026
7efd0c2
Test the edge branches of standard_gamma, beta and vonmises in both d…
ndgrigorian Dec 2, 2026
a9d4bf7
Say how closely the samplers agree across devices
ndgrigorian Dec 2, 2026
927cef3
Say how closely the samplers agree across devices in the libtensor guide
ndgrigorian Dec 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This release drops support for Python 3.10, making Python 3.11 the minimum requi
* Added implementation of `dpnp.scipy.linalg.cho_factor` and `dpnp.scipy.linalg.cho_solve` (SciPy-compatible) [#3116](https://github.com/IntelPython/dpnp/pull/3116)
* Added the `dpnp.random.Generator` API with `default_rng`, `BitGenerator`, `SeedSequence` and the `Philox4x3210` bit generator, sampling on the device with the device RNG API of oneMKL or oneMath [#3157](https://github.com/IntelPython/dpnp/pull/3157)
* Added `dpnp.argpartition` and `dpnp.ndarray.argpartition` to return the indices that would partition an array [#3138](https://github.com/IntelPython/dpnp/pull/3138)
* Added the continuous distributions of `numpy.random.Generator`, such as `gamma`, `beta` and `vonmises`, to `dpnp.random.Generator` [#3158](https://github.com/IntelPython/dpnp/pull/3158)

### Changed

Expand Down
31 changes: 28 additions & 3 deletions doc/reference/random.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ Differences from NumPy
----------------------

* The streams differ from NumPy's. For a given seed, output size and data
type, the raw bits and the uniform floats are the same on every device,
while the normal samples agree to a few ULP.
type, the raw bits and the uniform floats are the same on every device.
The other distributions use transcendental functions, which may round
differently on another device: their samples agree to a few ULP where
the formula is well conditioned and more loosely where it isn't, and
where a rejection sampler takes another candidate on one device, some
of the later samples differ too.
* The values depend on the output size: a draw is not a prefix of a
larger one, and two draws of ``n`` values differ from one draw of
``2 * n``. Code splitting its draws into chunks gets other values.
Expand All @@ -44,7 +48,8 @@ Differences from NumPy
* Parameters on the host (Python scalars, lists and NumPy arrays) are
checked as in NumPy. Parameters in :class:`dpnp.ndarray` are
not, since that would wait for the device.
Invalid ones give undefined values, such as NaN.
Invalid ones give undefined values, such as NaN, but the kernels handle
them safely: they never loop without end or access memory out of bounds.
* Parameters given as lists or NumPy arrays are copied to the device first,
which waits for the work already submitted to the queue. Scalars and
:class:`dpnp.ndarray` parameters don't wait.
Expand Down Expand Up @@ -89,9 +94,29 @@ Distributions
:toctree: generated/
:nosignatures:

Generator.beta
Generator.chisquare
Generator.exponential
Generator.f
Generator.gamma
Generator.gumbel
Generator.laplace
Generator.logistic
Generator.lognormal
Generator.normal
Generator.pareto
Generator.power
Generator.rayleigh
Generator.standard_cauchy
Generator.standard_exponential
Generator.standard_gamma
Generator.standard_normal
Generator.standard_t
Generator.triangular
Generator.uniform
Generator.vonmises
Generator.wald
Generator.weibull

Bit generators
--------------
Expand Down
2 changes: 1 addition & 1 deletion dpnp/libtensor/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ A new extension's `CMakeLists.txt` lists its sources and calls `build_dpnp_pybin
- Guard double, complex<double> and half with `dpnp::tensor::type_utils::validate_type_for_device<T>(q)`; don't use double literals or casts in kernels that must run without fp64.
- `blas`, `fft`, `lapack` and `sparse` build against either oneMKL or oneMath (`USE_ONEMATH`); `vm` has kernels only with oneMKL: with oneMath it builds empty and `_is_available()` returns `False`; `rng` uses the header-only device RNG API of oneMKL, or of oneMath with `USE_ONEMATH`, and links neither library.
- Release the GIL around blocking oneMKL calls (`py::gil_scoped_release`).
- `rng` draws depend only on the engine state and the number of elements, never on the device or the layout chosen for it (`extensions/rng/kernels/sample.hpp`): raw bits and uniform floats are bit-identical across devices, samplers using transcendental functions agree to a few ULP. Each engine's `items_per_work_item` (`kernels/engines.hpp`) is part of its stream definition.
- `rng` draws depend only on the engine state and the number of elements, never on the device or the layout chosen for it (`extensions/rng/kernels/sample.hpp`): raw bits and uniform floats are bit-identical across devices, samplers using transcendental functions agree to a few ULP where their formula is well conditioned and more loosely where it isn't, and where a rejection sampler takes another candidate on one device, the later values of that work-item differ. Each engine's `items_per_work_item` (`kernels/engines.hpp`) is part of its stream definition. Parameters on the device are unchecked, so a rejection sampler returns NaN for the inputs that would make it loop forever, including values that are valid but round badly in fp32.
- An extension whose kernels may be launched over more than `INT_MAX` work-items passes `LARGE_RANGES` to `build_dpnp_pybind11_ext`, which adds `-fno-sycl-id-queries-fit-in-int` at compile and link time (DPC++ 2026.2 and later need it at link time). `source/CMakeLists.txt` and `dpnp_backend_c` pass it to both `target_compile_options` and `target_link_options`.
- clang-format 23 formats C++ (`.clang-format`: LLVM base, 4-space indent, 80 columns); gersemi formats CMake at 88 columns, except `dpnp/backend/cmake/Modules/`.

Expand Down
5 changes: 5 additions & 0 deletions dpnp/libtensor/extensions/rng/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@

set(python_module_name _rng_impl)
set(_module_src
# normal.cpp first: oneMKL's Philox helpers are static inline, and the
# device link takes the inline engine helpers from the first source, so
# later sources' kernels carry two engine copies (slow on the OpenCL CPU)
${CMAKE_CURRENT_SOURCE_DIR}/normal.cpp
${CMAKE_CURRENT_SOURCE_DIR}/rng_py.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sample.cpp
${CMAKE_CURRENT_SOURCE_DIR}/uniform.cpp
${CMAKE_CURRENT_SOURCE_DIR}/exponential.cpp
${CMAKE_CURRENT_SOURCE_DIR}/gamma.cpp
)

# a launch takes one work-item per 8 elements, which can exceed INT_MAX
Expand Down
2 changes: 2 additions & 0 deletions dpnp/libtensor/extensions/rng/distributions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ namespace py = pybind11;

void init_uniform(py::module_ m);
void init_normal(py::module_ m);
void init_exponential(py::module_ m);
void init_gamma(py::module_ m);
} // namespace dpnp::extensions::rng
65 changes: 65 additions & 0 deletions dpnp/libtensor/extensions/rng/exponential.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//*****************************************************************************
// Copyright (c) 2026, Intel Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// - Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************
#include <pybind11/pybind11.h>

#include "common.hpp"
#include "distributions.hpp"

namespace dpnp::extensions::rng
{
static sample_dispatch_table_t exponential_dispatch_table;
static sample_dispatch_table_t pareto_dispatch_table;
static sample_dispatch_table_t weibull_dispatch_table;
static sample_dispatch_table_t power_dispatch_table;
static sample_dispatch_table_t rayleigh_dispatch_table;

void init_exponential(py::module_ m)
{
def_sampler<rng_ns::ExponentialSampler, RealTypes>(
m, "_exponential",
"Fill the result with exponential samples of scale scale",
exponential_dispatch_table);

def_sampler<rng_ns::ParetoSampler, RealTypes>(
m, "_pareto", "Fill the result with Pareto II samples of shape a",
pareto_dispatch_table);

def_sampler<rng_ns::WeibullSampler, RealTypes>(
m, "_weibull", "Fill the result with Weibull samples of shape a",
weibull_dispatch_table);

def_sampler<rng_ns::PowerSampler, RealTypes>(
m, "_power",
"Fill the result with power distribution samples of exponent a - 1",
power_dispatch_table);

def_sampler<rng_ns::RayleighSampler, RealTypes>(
m, "_rayleigh", "Fill the result with Rayleigh samples of mode scale",
rayleigh_dispatch_table);
}
} // namespace dpnp::extensions::rng
69 changes: 69 additions & 0 deletions dpnp/libtensor/extensions/rng/gamma.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//*****************************************************************************
// Copyright (c) 2026, Intel Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// - Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************
#include <pybind11/pybind11.h>

#include "common.hpp"
#include "distributions.hpp"

namespace dpnp::extensions::rng
{
static sample_dispatch_table_t gamma_dispatch_table;
static sample_dispatch_table_t beta_dispatch_table;
static sample_dispatch_table_t chisquare_dispatch_table;
static sample_dispatch_table_t standard_t_dispatch_table;
static sample_dispatch_table_t f_dispatch_table;

void init_gamma(py::module_ m)
{
def_sampler<rng_ns::GammaSampler, RealTypes>(
m, "_gamma",
"Fill the result with gamma samples of shape shape and scale scale",
gamma_dispatch_table);

def_sampler<rng_ns::BetaSampler, RealTypes>(
m, "_beta",
"Fill the result with beta samples of shape parameters a and b",
beta_dispatch_table);

def_sampler<rng_ns::ChisquareSampler, RealTypes>(
m, "_chisquare",
"Fill the result with chi-square samples of df degrees of freedom",
chisquare_dispatch_table);

def_sampler<rng_ns::StandardTSampler, RealTypes>(
m, "_standard_t",
"Fill the result with Student's t samples of df degrees of freedom",
standard_t_dispatch_table);

def_sampler<rng_ns::FSampler, RealTypes>(
m, "_f",
"Fill the result with F samples of dfnum and dfden degrees of "
"freedom",
f_dispatch_table);
}
} // namespace dpnp::extensions::rng
Loading
Loading