Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added support for boolean input in `dpnp.positive` and the `+` operator of `dpnp.ndarray` [#3128](https://github.com/IntelPython/dpnp/pull/3128)
* Added `dpnp.top_k` to return the `k` largest or smallest elements and their indices along an axis, with a `sorted` keyword to return them in sorted order [#3133](https://github.com/IntelPython/dpnp/pull/3133)
* Added the `dpnp.random.Generator` API with `default_rng`, `BitGenerator`, `SeedSequence` and the `Philox4x3210` bit generator, sampling on the device with the oneMKL device RNG API [#3157](https://github.com/IntelPython/dpnp/pull/3157)
* 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
24 changes: 23 additions & 1 deletion doc/reference/random.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ 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 or the smallest ``int64``,
but the kernels handle them safely: they never loop without end or access
memory out of bounds.

Random Generator
----------------
Expand Down Expand Up @@ -65,9 +67,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 oneMKL device RNG API and links no oneMKL library.
- Release the GIL around blocking oneMKL calls (`py::gil_scoped_release`).
- `rng` values 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`); each engine's `items_per_work_item` (`kernels/engines.hpp`) is part of its stream definition.
- `rng` values 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`); 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
2 changes: 2 additions & 0 deletions dpnp/libtensor/extensions/rng/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

set(python_module_name _rng_impl)
set(_module_src
${CMAKE_CURRENT_SOURCE_DIR}/exponential.cpp
${CMAKE_CURRENT_SOURCE_DIR}/gamma.cpp
${CMAKE_CURRENT_SOURCE_DIR}/normal.cpp
${CMAKE_CURRENT_SOURCE_DIR}/rng_py.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sample.cpp
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