-
Notifications
You must be signed in to change notification settings - Fork 14
task: add patch methods for mkl_random #90
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
f007ddd
15404d2
c56e34b
1f403ad
cf70971
4de0d12
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| [MASTER] | ||
| extension-pkg-allow-list=numpy,mkl_random.mklrand | ||
|
|
||
| [TYPECHECK] | ||
| generated-members=RandomState,min,max |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,292 @@ | ||||||||||||||||||||||||||||||
| # Copyright (c) 2019, Intel Corporation | ||||||||||||||||||||||||||||||
|
Collaborator
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. I don't think we need Cython here like with mkl_fft's patch, we can do it entirely on the Python level. We aren't using any of NumPy/Python C API.
Collaborator
Author
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. Oops, overlooked that thanks for mentioning it |
||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||
| # 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 Intel Corporation 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 OWNER 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. | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # distutils: language = c | ||||||||||||||||||||||||||||||
| # cython: language_level=3 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||
| Patch NumPy's `numpy.random` symbols to use mkl_random implementations. | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| This is attribute-level monkey patching. It can replace legacy APIs like | ||||||||||||||||||||||||||||||
| `numpy.random.RandomState` and global distribution functions, but it does not | ||||||||||||||||||||||||||||||
| replace NumPy's `Generator`/`default_rng()` unless mkl_random provides fully | ||||||||||||||||||||||||||||||
| compatible replacements. | ||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| from contextlib import ContextDecorator | ||||||||||||||||||||||||||||||
| from threading import Lock, local | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| import numpy as _np | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| from . import mklrand as _mr | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| cdef tuple _DEFAULT_NAMES = ( | ||||||||||||||||||||||||||||||
| # Legacy seeding / state | ||||||||||||||||||||||||||||||
| "seed", | ||||||||||||||||||||||||||||||
| "get_state", | ||||||||||||||||||||||||||||||
| "set_state", | ||||||||||||||||||||||||||||||
| "RandomState", | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Common global sampling helpers | ||||||||||||||||||||||||||||||
| "random", | ||||||||||||||||||||||||||||||
| "random_sample", | ||||||||||||||||||||||||||||||
| "sample", | ||||||||||||||||||||||||||||||
| "rand", | ||||||||||||||||||||||||||||||
| "randn", | ||||||||||||||||||||||||||||||
| "bytes", | ||||||||||||||||||||||||||||||
|
Comment on lines
+53
to
+59
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Integers | ||||||||||||||||||||||||||||||
| "randint", | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Common distributions (only patched if present on both sides) | ||||||||||||||||||||||||||||||
| "standard_normal", | ||||||||||||||||||||||||||||||
| "normal", | ||||||||||||||||||||||||||||||
| "uniform", | ||||||||||||||||||||||||||||||
| "exponential", | ||||||||||||||||||||||||||||||
| "gamma", | ||||||||||||||||||||||||||||||
| "beta", | ||||||||||||||||||||||||||||||
| "chisquare", | ||||||||||||||||||||||||||||||
| "f", | ||||||||||||||||||||||||||||||
| "lognormal", | ||||||||||||||||||||||||||||||
| "laplace", | ||||||||||||||||||||||||||||||
| "logistic", | ||||||||||||||||||||||||||||||
| "multivariate_normal", | ||||||||||||||||||||||||||||||
| "poisson", | ||||||||||||||||||||||||||||||
| "power", | ||||||||||||||||||||||||||||||
| "rayleigh", | ||||||||||||||||||||||||||||||
| "triangular", | ||||||||||||||||||||||||||||||
| "vonmises", | ||||||||||||||||||||||||||||||
| "wald", | ||||||||||||||||||||||||||||||
| "weibull", | ||||||||||||||||||||||||||||||
| "zipf", | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Permutations / choices | ||||||||||||||||||||||||||||||
| "choice", | ||||||||||||||||||||||||||||||
| "permutation", | ||||||||||||||||||||||||||||||
| "shuffle", | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| class _GlobalPatch: | ||||||||||||||||||||||||||||||
| def __init__(self): | ||||||||||||||||||||||||||||||
| self._lock = Lock() | ||||||||||||||||||||||||||||||
| self._patch_count = 0 | ||||||||||||||||||||||||||||||
| self._numpy_module = None | ||||||||||||||||||||||||||||||
| self._requested_names = None | ||||||||||||||||||||||||||||||
| self._originals = {} | ||||||||||||||||||||||||||||||
| self._patched = () | ||||||||||||||||||||||||||||||
| self._tls = local() | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def _normalize_names(self, names): | ||||||||||||||||||||||||||||||
| if names is None: | ||||||||||||||||||||||||||||||
| names = _DEFAULT_NAMES | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| names = _DEFAULT_NAMES | |
| names = _DEFAULT_NAMES | |
| elif isinstance(names, str): | |
| # Treat a single string as a single name, not an iterable of characters | |
| names = (names,) | |
| elif isinstance(names, bytes): | |
| # Attribute names must be strings; reject bytes explicitly | |
| raise TypeError("names must be a string or an iterable of strings, not bytes") | |
| else: | |
| names = tuple(names) | |
| for name in names: | |
| if not isinstance(name, str): | |
| raise TypeError("All names must be strings; got {!r} of type {!r}".format(name, type(name))) |
Copilot
AI
Mar 5, 2026
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.
restore(verbose=True) currently emits a warning via print(...). Elsewhere in this project warnings are surfaced via warnings.warn(...) (often with an appropriate category/stacklevel). Using warnings.warn here would make the warning easier to filter/test and avoids writing directly to stdout.
Copilot
AI
Mar 5, 2026
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.
use_in_numpy() is documented as a backward-compatible alias for monkey_patch(), but it's currently a separate wrapper function. This breaks identity-based expectations (and the added test asserts use_in_numpy is monkey_patch). Either make use_in_numpy a true alias (assignment) or adjust the test and docs to reflect wrapper semantics.
| def use_in_numpy(numpy_module=None, names=None, strict=False, verbose=False): | |
| """ | |
| Backward-compatible alias for monkey_patch(). | |
| """ | |
| monkey_patch( | |
| numpy_module=numpy_module, | |
| names=names, | |
| strict=strict, | |
| verbose=verbose, | |
| ) | |
| # Backward-compatible alias for monkey_patch(). | |
| use_in_numpy = monkey_patch |
Uh oh!
There was an error while loading. Please reload this page.