Repository navigation
Add dpnp.random.Generator on the oneMKL device RNG API
#3157
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,112 @@ | |
| Random sampling (:mod:`dpnp.random`) | ||
| ==================================== | ||
|
|
||
| .. hint:: `NumPy API Reference: Random sampling <https://numpy.org/doc/stable/reference/random/index.html>`_ | ||
|
|
||
| DPNP draws random numbers on the SYCL device, with the counter-based | ||
| Philox4x32x10 generator of the oneMKL device RNG API. It provides the | ||
| :class:`Generator` API of NumPy, recommended for new code. The legacy | ||
| :class:`RandomState` API and the module-level functions still use the | ||
| host-side oneMKL RNG. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import dpnp | ||
|
|
||
| rng = dpnp.random.default_rng(seed=42) | ||
| x = rng.standard_normal((3, 4)) # on the default device | ||
| y = rng.random(5, usm_type="shared") | ||
|
|
||
| rng = dpnp.random.default_rng(42, device="cpu") | ||
|
|
||
| A generator draws all its samples on one SYCL queue, chosen with the | ||
| ``device`` or ``sycl_queue`` keyword when it is created. All samples are | ||
| :class:`dpnp.ndarray`, zero-dimensional when ``size`` is ``None``. | ||
|
|
||
| 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. | ||
| * 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. | ||
| * The floating-point distributions take a ``dtype`` keyword, as in CuPy, | ||
| which defaults to the default floating-point type of the device, so on a | ||
| device without double precision the same seed gives other values by | ||
| default. | ||
| * All methods take a ``usm_type`` keyword. With ``usm_type=None``, the | ||
| default, the samples take the USM type of the array parameters, or | ||
| ``"device"`` when there are none. | ||
| * 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. | ||
| * Generators and bit generators cannot be pickled or copied, and the bit | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion: "cannot be pickled or deep-copied". |
||
| generators have no ``advance`` or ``jumped`` method. | ||
|
|
||
| Random Generator | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this heading and the legacy "Random generator" (line 181) differ only by one capital letter. Suggestion: rename one of them, e.g. the legacy one to "RandomState", so the two sections are easy to tell apart. |
||
| ---------------- | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
| :nosignatures: | ||
|
|
||
| default_rng | ||
| Generator | ||
|
|
||
| Accessing the bit generator and spawning | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
| :nosignatures: | ||
|
|
||
| Generator.bit_generator | ||
| Generator.spawn | ||
|
|
||
| Simple random data | ||
| ^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
| :nosignatures: | ||
|
|
||
| Generator.random | ||
|
|
||
| Distributions | ||
| ^^^^^^^^^^^^^ | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
| :nosignatures: | ||
|
|
||
| Generator.normal | ||
| Generator.standard_normal | ||
| Generator.uniform | ||
|
|
||
| Bit generators | ||
| -------------- | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
| :nosignatures: | ||
|
|
||
| BitGenerator | ||
| BitGenerator.random_raw | ||
| BitGenerator.spawn | ||
| BitGenerator.state | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some documented properties have no page. Suggestion: add them to the |
||
| Philox4x3210 | ||
| SeedSequence | ||
|
|
||
| Legacy random generation | ||
| ------------------------ | ||
|
|
||
| .. hint:: `NumPy API Reference: Legacy random generation <https://numpy.org/doc/stable/reference/random/legacy.html>`_ | ||
|
|
||
| Simple random data | ||
| ------------------ | ||
| ^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
|
|
@@ -23,9 +125,8 @@ Simple random data | |
| choice | ||
| bytes | ||
|
|
||
|
|
||
| Permutations | ||
| ------------ | ||
| ^^^^^^^^^^^^ | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
|
|
@@ -34,9 +135,8 @@ Permutations | |
| shuffle | ||
| permutation | ||
|
|
||
|
|
||
| Distributions | ||
| ------------- | ||
| ^^^^^^^^^^^^^ | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
|
|
@@ -78,9 +178,8 @@ Distributions | |
| weibull | ||
| zipf | ||
|
|
||
|
|
||
| Random generator | ||
| ---------------- | ||
| ^^^^^^^^^^^^^^^^ | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # ***************************************************************************** | ||
| # 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. | ||
| # ***************************************************************************** | ||
|
|
||
| set(python_module_name _rng_impl) | ||
| set(_module_src | ||
| ${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 | ||
| ) | ||
|
|
||
| # a launch takes one work-item per 8 elements, which can exceed INT_MAX | ||
| build_dpnp_pybind11_ext( | ||
| ${python_module_name} | ||
| SOURCES ${_module_src} | ||
| LARGE_RANGES | ||
| ) | ||
|
|
||
| # oneMKL device RNG API is header-only, so no oneMKL library is linked | ||
| target_include_directories( | ||
| ${python_module_name} | ||
| SYSTEM | ||
| PRIVATE $<TARGET_PROPERTY:MKL::MKL_SYCL,INTERFACE_INCLUDE_DIRECTORIES> | ||
| ) |
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.
Nit:
Philoxbelongs afterPenrose.On the 41 words from my review: that was the number of new warning lines from the spelling step, not of words. At this head the step reports 10 lines more than master. The only new word is
rng, from thedpnp.random.default_rngpage;oneMKL,syclandreproducibilityare flagged on master already. Sorngis the only word left to add.