task: CLI patch methods by jharlow-intel · Pull Request #133 · IntelPython/mkl_random · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,45 @@ The list of supported by `mkl_random.RandomState` constructor `brng` keywords is

# Patching Mechanisms

`mkl_random` provides a convenient programmatic patch method to enable MKL-accelerated random operations in NumPy.
`mkl_random` provides convenient patch methods to enable MKL-accelerated
random operations in NumPy with or without modifying your code.

## CLI Quickstart

### Persistent patch (all Python sessions)

```bash
# Install
python -m mkl_random --patch install

# Status (exit code: 0 = installed, 1 = not installed)
python -m mkl_random --patch status

# Remove
python -m mkl_random --patch uninstall
```

### Verify current random backend

```bash
python -c "import numpy; print(f'numpy.random.normal.__module__: {numpy.random.normal.__module__}')"
```

### One-shot patch (single command only)

```bash
# Script
python -m mkl_random --with-numpy-patch my_script.py

# Pytest
python -m mkl_random --with-numpy-patch -m pytest tests/

# One-liner
python -m mkl_random --with-numpy-patch -c "import numpy; print(f\"numpy.random.normal.__module__: {numpy.random.normal.__module__}\")"

# Non-Python command
python -m mkl_random --with-numpy-patch -- <command> [args...]
```

## Programmatic Quickstart

Expand Down
85 changes: 85 additions & 0 deletions mkl_random/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright (c) 2026, Intel Corporation
#
# 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.

"""Command-line interface for mkl_random."""

import argparse
import sys


def main():
"""Entry point for the CLI."""
parser = argparse.ArgumentParser(
prog="python -m mkl_random",
description="MKL-accelerated random generation for NumPy",
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="Enable verbose output"
)
parser.add_argument(
"--patch",
choices=["install", "uninstall", "status"],
help="Manage persistent NumPy random patching",
)
parser.add_argument(
"--with-numpy-patch",
dest="with_numpy_patch",
nargs=argparse.REMAINDER,
help="Run command with temporary NumPy random patch",
)

args = parser.parse_args()

if args.patch:
from mkl_random.patch import (
PatchOperationError,
check_status,
install_patch,
uninstall_patch,
)

try:
if args.patch == "install":
install_patch(verbose=args.verbose)
elif args.patch == "uninstall":
uninstall_patch(verbose=args.verbose)
elif args.patch == "status":
sys.exit(0 if check_status(verbose=args.verbose) else 1)
except PatchOperationError as exc:
print(exc, file=sys.stderr)
sys.exit(1)

elif args.with_numpy_patch is not None:
from mkl_random.with_patch import run_with_numpy_patch

run_with_numpy_patch(args.with_numpy_patch)

else:
parser.print_help()
sys.exit(1)


if __name__ == "__main__":
main()
33 changes: 33 additions & 0 deletions mkl_random/_patch_startup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (c) 2026, Intel Corporation
#
# 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.

"""Helper module for .pth-based persistent patching with error handling."""

try:
import mkl_random

mkl_random.patch_numpy_random()
except Exception:
pass
143 changes: 143 additions & 0 deletions mkl_random/patch.py
Loading
Loading