Skip to content

Commit 416ceb1

Browse files
committed
address review comments
1 parent fe7b7a2 commit 416ceb1

File tree

5 files changed

+105
-20
lines changed

5 files changed

+105
-20
lines changed

docs/source/how_to.rst

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ How-to Guides
44
How to save and resume long computation
55
---------------------------------------
66

7-
:class:`RandomState` is pickleable. Pickling allows to save and restore
7+
:class:`MKLRandomState` is pickleable. Pickling allows to save and restore
88
the internal state of the pseudo-random number generators.
99

1010
.. code-block:: python
@@ -14,7 +14,7 @@ the internal state of the pseudo-random number generators.
1414
import mkl_random
1515
import pickle
1616
17-
rs = mkl_random.RandomState(seed=777, brng="r250")
17+
rs = mkl_random.MKLRandomState(seed=777, brng="r250")
1818
draw = rs.standard_normal(size=1357913)
1919
2020
# pickle random state
@@ -45,7 +45,7 @@ from such family, initialized equally, produce streams of randomness statistical
4545
indistinguishable from independent.
4646

4747
.. py:method:: skipahead(nskips)
48-
:canonical: mkl_random.RandomState.skipahead
48+
:canonical: mkl_random.MKLRandomState.skipahead
4949

5050
Advance the state of the generator using skip-ahead method, or raise :code:`ValueError`
5151
exception if not supported.
@@ -63,7 +63,7 @@ indistinguishable from independent.
6363
independence breaks down.
6464

6565
.. py:method:: leapfrog(k, nstreams)
66-
:canonical: mkl_random.RandomState.leapfrog
66+
:canonical: mkl_random.MKLRandomState.leapfrog
6767

6868
Initialize the state of the generator using leap-frog method, or raise :code:`ValueError`
6969
exception if not supported.
@@ -85,3 +85,81 @@ indistinguishable from independent.
8585
randomness stasistically indistunguishable from independent. To use such families in parallel computation, assign
8686
difference family generators to different parallel workers and sample those assigned generators in each parallel worker.
8787
Please refer to "examples/" folder in the `GitHub repo <https://github.com/IntelPython/mkl_random>`_ for more details.
88+
89+
90+
Using :mod:`mkl_random` as a drop-in replacement for `numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_
91+
-----------------------------------------------------------------
92+
93+
The :mod:`mkl_random.interfaces.numpy_random` module is aligned to the legacy
94+
portion of the `numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_ legacy API.
95+
You can import it in place of `numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_
96+
without changing the rest of your code:
97+
98+
.. code-block:: python
99+
:caption: Drop-in replacement for numpy.random
100+
101+
from mkl_random.interfaces import numpy_random as rng
102+
103+
rng.seed(1234)
104+
x = rng.standard_normal(size=100)
105+
y = rng.uniform(0, 1, size=100)
106+
107+
See :ref:`interfaces` for a full list of available functions.
108+
109+
.. note::
110+
While the API is the same, :mod:`mkl_random.interfaces.numpy_random` is **not** seed-compatible
111+
with `numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_. Given the same seed,
112+
the two modules will produce different sequences. There also may be differences in some edge cases, such as
113+
behavior of functions when given specific inputs.
114+
115+
116+
How to patch `numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_ with :mod:`mkl_random`
117+
-------------------------------------------------------------
118+
119+
Existing code that calls `numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_
120+
directly can be patched to use :mod:`mkl_random.interfaces.numpy_random` at runtime.
121+
122+
The recommended approach is to use the :class:`mkl_random.mkl_random` context manager:
123+
124+
.. code-block:: python
125+
:caption: Temporarily patch numpy.random using context manager
126+
127+
import numpy as np
128+
import mkl_random
129+
130+
with mkl_random.mkl_random():
131+
x = np.random.standard_normal(100) # uses mkl_random
132+
y = np.random.uniform(0, 1, size=100) # uses mkl_random
133+
134+
:mod:`mkl_random` also exposes the explicit patching functions:
135+
136+
.. code-block:: python
137+
:caption: Patch numpy.random for the duration of a script
138+
139+
import mkl_random
140+
mkl_random.patch_numpy_random() # subsequent numpy.random calls use mkl_random
141+
142+
import numpy as np
143+
data = np.random.normal(0, 1, size=100)
144+
145+
.. note::
146+
The patching functions are provided for users' convenience, but they are not recommended
147+
for new code. It is instead recommended to use :mod:`mkl_random` directly for new code.
148+
For existing code where patching may be desirable, it is also suggested to prefer the
149+
context manager, as it scopes the patch to blocks and thus, prevents user error of
150+
forgetting to restore the original state, calling the patch multiple times, or
151+
creating undefined behavior when patching in a multi-threaded program.
152+
153+
You can also use :class:`mkl_random.mkl_random` as a decorator:
154+
155+
.. code-block:: python
156+
:caption: Patch numpy.random as a decorator
157+
158+
import numpy as np
159+
import mkl_random
160+
161+
@mkl_random.mkl_random()
162+
def get_data():
163+
return np.random.standard_normal(100)
164+
165+
See :ref:`patching` for details.

docs/source/reference/index.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ Drop-in interfaces
2828

2929
The :mod:`mkl_random.interfaces` submodule provides drop-in replacements for standard random modules:
3030

31-
* :ref:`mkl_random.interfaces.numpy_random <numpy_random_interface>` - a drop-in replacement for the legacy :mod:`numpy.random` module
31+
* :ref:`mkl_random.interfaces.numpy_random <numpy_random_interface>` - a drop-in replacement for the legacy `numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_ module
3232

3333

3434
Patching
3535
--------
3636

37-
:mod:`mkl_random` can :ref:`patch numpy.random <patching>` so that existing code calling :mod:`numpy.random`
38-
functions can use :mod:`mkl_random` implementations.
37+
:mod:`mkl_random` can :ref:`patch numpy.random <patching>` so that existing code calling `numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_ functions can use :mod:`mkl_random` implementations.
3938

4039
.. toctree::
4140
:hidden:

docs/source/reference/interfaces.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ NumPy interface --- :mod:`mkl_random.interfaces.numpy_random`
1414
-------------------------------------------------------------
1515

1616
:mod:`mkl_random.interfaces.numpy_random` is a drop-in replacement for the legacy portion of
17-
:mod:`numpy.random`.
17+
`numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_.
1818

1919
.. note::
2020
While the API is the same, :mod:`mkl_random.interfaces.numpy_random` is **not** seed-compatible
21-
with :mod:`numpy.random`. Given the same seed, the two modules will produce different sequences.
22-
The output of `get_state` and accepted input to `set_state` may also differ. It is not
23-
recommended to provide the output of `get_state` from one module to `set_state` of the other.
21+
with `numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_. Given the same seed, the two modules
22+
will produce different sequences. The output of :func:`get_state` and accepted input to :func:`set_state` may also differ.
23+
It is not recommended to provide the output of :func:`get_state` from one module to :func:`set_state` of the other.
2424
There also may be differences in some edge cases, such as behavior of functions when given specific inputs.
2525

2626

docs/source/reference/patching.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
.. _patching:
22

3-
Patching :mod:`numpy.random`
3+
Patching `numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_
44
============================
55

6-
:mod:`mkl_random` can temporarily replace functions and classes in :mod:`numpy.random` with
7-
:mod:`mkl_random`implementations from the :ref:`numpy interface <numpy_random_interface>`.
6+
:mod:`mkl_random` can temporarily replace functions and classes in `numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_ with
7+
:mod:`mkl_random` implementations from the :ref:`numpy interface <numpy_random_interface>`.
88

99

1010
Functions

docs/source/tutorials.rst

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,31 @@ The :mod:`mkl_random` is also distributed as part of `Intel® Distribution for P
3939
First steps
4040
-----------
4141

42-
The :mod:`mkl_random` package has followed the design of :class:`numpy.random` package to
43-
make :mod:`mkl_random` easy to use for those already familiar with the :mod:`numpy.random` module.
42+
The :mod:`mkl_random` package has followed the design of `numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_
43+
package to make :mod:`mkl_random` easy to use for those already familiar with the
44+
`numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_ module.
4445

4546
.. note::
4647
Since the first release of :mod:`mkl_random`, NumPy introduced new classes :class:`numpy.random.Generator` and
4748
:class:`numpy.random.BitGenerator`, while also retaining :class:`numpy.random.RandomState` for backwards
4849
compatibility. :mod:`mkl_random`, at present, does not provide classes mirroring :class:`Generator` or
4950
:class:`BitGenerators`.
5051

51-
The state of pseudo-random number generator is stored in :class:`mkl_random.RandomState` class,
52+
.. tip::
53+
If you want a drop-in replacement for the `numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_ legacy interface,
54+
see :ref:`interfaces`. While it is recommended users rewrite code to use :mod:`mkl_random` or :mod:`<|mkl_random.interfaces.numpy_random|>`
55+
directly, :mod:`mkl_random` provides tools to patch `numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_ so that
56+
existing code transparently uses the MKL-backed implementations in `numpy.random <https://numpy.org/doc/stable/reference/random/legacy.html>`_.
57+
See :ref:`patching` for details.
58+
59+
The state of pseudo-random number generator is stored in :class:`mkl_random.MKLRandomState` class,
5260
so using :mod:`mkl_random` begins with creating an instance of this class:
5361

5462
.. code-block:: python
5563
:caption: Construct random number generator
5664
5765
import mkl_random
58-
rs = mkl_random.RandomState(seed=1234)
66+
rs = mkl_random.MKLRandomState(seed=1234)
5967
6068
Sampling from difference probability distribution is done by calling the class methods on the constructed instance:
6169

@@ -75,7 +83,7 @@ Here is an example of estimating value of :math:`\pi` by using Monte-Carlo metho
7583
import numpy as np
7684
import mkl_random
7785
78-
rs = mkl_random.RandomState(seed=1234)
86+
rs = mkl_random.MKLRandomState(seed=1234)
7987
8088
sample_size = 10**8
8189
batch_size = 10**6
@@ -110,7 +118,7 @@ distributions of interest.
110118
`True random generator <https://en.wikipedia.org/wiki/Hardware_random_number_generator>`_ relies on
111119
laws of physics to provide those, leveraging dedicated hardware providing a source of entropy.
112120

113-
`Psuedo-random generator <https://en.wikipedia.org/wiki/Pseudorandom_number_generator>`_ is an algorithm that outputs a sequence that emulates true randomness.
121+
`Pseudo-random generator <https://en.wikipedia.org/wiki/Pseudorandom_number_generator>`_ is an algorithm that outputs a sequence that emulates true randomness.
114122
The quality of emulation is tested statistically through a battery of test, e.g. `Diehard tests <https://en.wikipedia.org/wiki/Diehard_tests>`_.
115123
These tests check if various statistical tests can separate the pseudo-random sequence from a true random one.
116124

0 commit comments

Comments
 (0)