Repository navigation
Add continuous distributions to dpnp.random.Generator
#3158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ndgrigorian
wants to merge
10
commits into
master
Choose a base branch
from
feature/device-rng-continuous
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e1f49f7
Add the continuous distributions to dpnp.random.Generator
ndgrigorian 8d85af8
Test the C-contiguous out check of the continuous samplers
ndgrigorian 5a830b1
Compute vonmises without cancellation for large kappa
ndgrigorian 46b6f5a
Take the beta shortcut for tiny parameters in single precision too
ndgrigorian ff71661
Leave the int64 sentinel to the discrete distributions in the docs
ndgrigorian 37b9ab9
Document the parameters of the continuous distributions
ndgrigorian 506bceb
Force inlining into the blocked lognormal, Cauchy and Wald kernels
ndgrigorian c72ada3
Document the ValueError of power
ndgrigorian cf84fec
Document the ValueErrors of chisquare
ndgrigorian dcd4617
Skip the CuPy KS tests of the continuous distributions without fp64
ndgrigorian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two lines slow #3157's
normal/standard_normalon the OpenCL CPU device, by up to 6x in float64 kernel time, and the newlognormalandstandard_cauchywith them. The GPUs are not affected.The cause is the device link:
philox4x32x10_impl::skip_ahead<1>,round_10) have internal linkage, so every source has its own copy.UniformBits::next_uniform<double>,StandardNormal<float>::operator()) are kept from the first source that has them, nowgamma.cpp.normal.cppcall two engine copies:gamma.cpp's through the helper and their own from the kernel. The OpenCL CPU compiler handles that much worse.NormalSampler's SPIR-V goes from 4 functions to 6.Kernel time head/master at n = 2e7 on opencl:cpu (median of 2-3 rounds; the same rows on the four GPUs measured are 0.87-1.01):
normalfloat64 scalarnormalfloat32 scalarEnd to end, float64
normalat 2e7 is 2.9-4.4x master's on the three client CPUs (1.03x on the Xeon, where allocation dominates). On the Lunar Lake CPU it is then slower than single-thread NumPy.Listing the new sources after #3157's restores master. Master's 40 images are byte-identical again, the values stay bit-identical, the RNG tests pass on GPU and CPU, and the CPU kernels are back at master's speed:
normal2.39 ms against master's 2.43 and head's 5.66; float32 1.32 ms against 1.31 and 2.11;lognormalandstandard_cauchyfloat64 at 0.42x and 0.35x head;normal1.46-1.48 ns per sample against master's 1.47-1.50 and head's 6.10-6.17.What else I tried:
lognormal,standard_cauchyandwaldout ofnormal.cppinto a source of their own: no change.next_uniformandStandardNormal::operator()inline: float32 is fixed, but the cross-source call moves tonext_double(), so float64 stays slow.UniformBits::next_float/next_double/next_uniform,BitGen::next_uint32/next_uint64,StandardNormal::operator()): the CPU is fixed whatever the link order. But it rewrites 10 of master's kernels (+90 KB), and on the Max 1100 float64gumbel,laplaceandlogisticgot 4-7 % slower at n = 2e7 and ~1.5x at n = 19997457.