Repository navigation
Reimplement dpnp.random.RandomState on the device RNG
#3162
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
19
commits into
feature/device-rng-bit-generators
Choose a base branch
from
feature/device-rng-random-state
base: feature/device-rng-bit-generators
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
19 commits
Select commit
Hold shift + click to select a range
5053f66
Rebuild RandomState and dpnp.random functions on the device RNG
ndgrigorian e819fc9
Say that RandomState.shuffle takes a usm_ndarray too
ndgrigorian e99d838
Import dpnp.random with docstrings stripped by python -OO
ndgrigorian bc3499d
Default usm_type to None in RandomState and the dpnp.random functions
ndgrigorian 2a0219d
Add the distributions RandomState now gets right to the changelog
ndgrigorian be7a077
Check n of RandomState.multinomial as NumPy does, before an empty pvals
ndgrigorian 10d0433
Check the queues of RandomState.uniform before its range
ndgrigorian 229fcf4
Raise NumPy's TypeError for a complex parameter on the device in Rand…
ndgrigorian 1434db3
Skip the conversion of an unconstrained device parameter in the legac…
ndgrigorian 322e566
Add 1 to a device high of random_integers in int64
ndgrigorian f2eec99
Give seed, rand, randn, randint and normal of dpnp.random examples
ndgrigorian ff731fb
Seed the states of new queues from the seed given to dpnp.random.seed
ndgrigorian 4398ead
Skip the CuPy test expecting randint(3, 3, size=0) to raise
ndgrigorian 686d1a3
Seed the new queue states from the reseeded bit generator's sequence
ndgrigorian e49062f
Add the queue copies of multivariate_normal and others to the changelog
ndgrigorian 90c0e9c
Spawn from a seed sequence under the lock of its bit generator
ndgrigorian 161f175
Raise NumPy's TypeError for a complex unconstrained device parameter
ndgrigorian 0527e5e
Say that every integer result of RandomState is int64
ndgrigorian 3194517
Name only the new RandomState methods in the changelog
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,9 +9,9 @@ DPNP draws random numbers on the SYCL device, by default with the | |
| counter-based Philox4x32x10 generator of the oneMKL device RNG API, or with | ||
| NumPy's PCG64DXSM generator or the MRG32k3a generator of the oneMKL device | ||
| RNG API. A oneMath build takes Philox4x32x10 and MRG32k3a from oneMath's | ||
| device RNG API instead. 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. | ||
| device RNG API instead. It provides both NumPy random APIs: the | ||
| :class:`Generator` API recommended for new code, and the legacy | ||
| :class:`RandomState` API with the module-level functions built on it. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
|
|
@@ -27,6 +27,8 @@ 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``. | ||
|
|
||
| .. _random-differences: | ||
|
|
||
| Differences from NumPy | ||
| ---------------------- | ||
|
|
||
|
|
@@ -52,8 +54,8 @@ Differences from NumPy | |
| 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. | ||
| checked as in NumPy. In :class:`Generator`, parameters in | ||
| :class:`dpnp.ndarray` are not, since that would wait for the device. | ||
| 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. Floating-point arrays given for integer parameters | ||
|
|
@@ -72,15 +74,22 @@ Differences from NumPy | |
| * ``zipf`` follows the Zipf distribution truncated to the largest ``int64``. | ||
| NumPy's rejection test rounds to accepting every sample above ``2**53``, | ||
| which makes its tail too heavy for ``a`` close to 1. | ||
| * ``hypergeometric`` requires ``ngood`` and ``nbad`` below ``10**9``, and | ||
| ``multivariate_hypergeometric`` requires ``sum(colors)`` below ``10**9`` | ||
| for the ``"count"`` method too. | ||
| * The integer results of :class:`RandomState` and of the :mod:`dpnp.random` | ||
| functions are ``int64`` by default, such as those of ``randint``, | ||
| ``random_integers``, ``choice``, ``permutation`` of an integer and the | ||
| discrete distributions, and :meth:`RandomState.tomaxint` reaches the | ||
| largest ``int64``. NumPy returns a C ``long`` instead, which is ``int32`` | ||
| on Windows. | ||
| * ``hypergeometric`` requires ``ngood`` and ``nbad`` below ``10**9``, also in | ||
| :class:`RandomState`, and ``multivariate_hypergeometric`` requires | ||
| ``sum(colors)`` below ``10**9`` for the ``"count"`` method too. | ||
| * ``choice`` always returns a sample without replacement in random order, | ||
| so its ``shuffle`` parameter has no effect. | ||
|
Comment on lines
+85
to
87
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: The next bullet (line 88) still says that |
||
| * ``p`` of ``choice`` and ``pvals`` of ``multinomial`` are only checked when | ||
| they are on the host, as the other parameters are. | ||
| * :meth:`Generator.shuffle` only accepts an array on the device, a | ||
| :class:`dpnp.ndarray` or :class:`dpnp.tensor.usm_ndarray`, as in CuPy. | ||
| * :meth:`Generator.shuffle` and :func:`shuffle` only accept an array on the | ||
| device, a :class:`dpnp.ndarray` or :class:`dpnp.tensor.usm_ndarray`, as in | ||
| CuPy. | ||
| * Generators and bit generators cannot be pickled or deep-copied, | ||
| and :class:`Philox4x3210` and :class:`MRG32k3a` have no ``advance`` or | ||
| ``jumped`` method. | ||
|
|
@@ -204,6 +213,24 @@ Legacy random generation | |
|
|
||
| .. hint:: `NumPy API Reference: Legacy random generation <https://numpy.org/doc/stable/reference/random/legacy.html>`_ | ||
|
|
||
| :class:`RandomState` keeps the parameter checks of NumPy's legacy one, also | ||
| for parameters in :class:`dpnp.ndarray`, as CuPy's does, so that a call with | ||
| such parameters waits for the device to check them. The module-level | ||
| functions use one :class:`RandomState` per SYCL queue: the | ||
| queue given by the ``device`` or ``sycl_queue`` keyword, or else the queue of | ||
| a :class:`dpnp.ndarray` parameter, or else the default queue. | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
| :nosignatures: | ||
|
|
||
| RandomState | ||
| seed | ||
| get_state | ||
| set_state | ||
| get_bit_generator | ||
| set_bit_generator | ||
|
|
||
| Simple random data | ||
| ^^^^^^^^^^^^^^^^^^ | ||
|
|
||
|
|
@@ -274,15 +301,3 @@ Distributions | |
| wald | ||
| weibull | ||
| zipf | ||
|
|
||
| RandomState | ||
| ^^^^^^^^^^^ | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
| :nosignatures: | ||
|
|
||
| RandomState | ||
| seed | ||
| get_random_state | ||
| set_random_state | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.