|
| 1 | +# Copyright (c) 2026, Intel Corporation |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions are met: |
| 5 | +# |
| 6 | +# * Redistributions of source code must retain the above copyright notice, |
| 7 | +# this list of conditions and the following disclaimer. |
| 8 | +# * Redistributions in binary form must reproduce the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer in the |
| 10 | +# documentation and/or other materials provided with the distribution. |
| 11 | +# * Neither the name of Intel Corporation nor the names of its contributors |
| 12 | +# may be used to endorse or promote products derived from this software |
| 13 | +# without specific prior written permission. |
| 14 | +# |
| 15 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 19 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 21 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 22 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + |
| 25 | +"""Persistent patch management for NumPy random submodule.""" |
| 26 | + |
| 27 | +import site |
| 28 | +import sys |
| 29 | +import warnings |
| 30 | +from pathlib import Path |
| 31 | + |
| 32 | + |
| 33 | +class PatchOperationError(RuntimeError): |
| 34 | + """Raised when a persistent patch operation cannot be completed.""" |
| 35 | + |
| 36 | + |
| 37 | +def get_pth_path(): |
| 38 | + """Get the path to mkl_random_patch.pth in the appropriate site-packages.""" |
| 39 | + site_packages = site.getsitepackages() |
| 40 | + if site_packages: |
| 41 | + target_site = site_packages[0] |
| 42 | + else: |
| 43 | + target_site = site.getusersitepackages() |
| 44 | + return Path(target_site) / "mkl_random_patch.pth" |
| 45 | + |
| 46 | + |
| 47 | +PTH_CONTENT = """import mkl_random._patch_startup""" |
| 48 | + |
| 49 | + |
| 50 | +def install_patch(verbose=False): |
| 51 | + """Install persistent NumPy random patch using .pth file.""" |
| 52 | + pth_path = get_pth_path() |
| 53 | + |
| 54 | + if pth_path.exists(): |
| 55 | + if verbose: |
| 56 | + warnings.warn( |
| 57 | + f"Persistent patch already installed at {pth_path}", |
| 58 | + UserWarning, |
| 59 | + stacklevel=2, |
| 60 | + ) |
| 61 | + return |
| 62 | + |
| 63 | + try: |
| 64 | + pth_path.parent.mkdir(parents=True, exist_ok=True) |
| 65 | + pth_path.write_text(PTH_CONTENT) |
| 66 | + if verbose: |
| 67 | + print(f"Persistent patch installed at {pth_path}") |
| 68 | + print() |
| 69 | + print( |
| 70 | + "NumPy random will now use MKL-accelerated implementations in" |
| 71 | + ) |
| 72 | + print("all Python sessions. To disable, run:") |
| 73 | + print(" python -m mkl_random --patch uninstall") |
| 74 | + except OSError as e: |
| 75 | + raise PatchOperationError( |
| 76 | + f"Error installing patch at {pth_path}: {e}\n\n" |
| 77 | + "You may need to run with appropriate permissions or install to " |
| 78 | + "a user site-packages directory." |
| 79 | + ) from e |
| 80 | + |
| 81 | + |
| 82 | +def uninstall_patch(verbose=False): |
| 83 | + """Uninstall persistent NumPy random patch.""" |
| 84 | + pth_path = get_pth_path() |
| 85 | + |
| 86 | + if not pth_path.exists(): |
| 87 | + if verbose: |
| 88 | + print("No persistent patch found.") |
| 89 | + return |
| 90 | + |
| 91 | + try: |
| 92 | + pth_path.unlink() |
| 93 | + if verbose: |
| 94 | + print(f"Persistent patch removed from {pth_path}") |
| 95 | + print() |
| 96 | + print("NumPy random will now use the default implementations.") |
| 97 | + except OSError as e: |
| 98 | + raise PatchOperationError( |
| 99 | + f"Error removing patch at {pth_path}: {e}" |
| 100 | + ) from e |
| 101 | + |
| 102 | + |
| 103 | +def check_status(verbose=False): |
| 104 | + """Check if persistent patch is installed.""" |
| 105 | + pth_path = get_pth_path() |
| 106 | + |
| 107 | + if pth_path.exists(): |
| 108 | + if verbose: |
| 109 | + print(f"Persistent patch is installed at {pth_path}") |
| 110 | + print() |
| 111 | + print( |
| 112 | + "NumPy random is configured to use MKL-accelerated " |
| 113 | + "implementations." |
| 114 | + ) |
| 115 | + return True |
| 116 | + else: |
| 117 | + if verbose: |
| 118 | + print("No persistent patch installed") |
| 119 | + print() |
| 120 | + print("To enable MKL-accelerated NumPy random globally, run:") |
| 121 | + print(" python -m mkl_random --patch install") |
| 122 | + return False |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + if len(sys.argv) < 2: |
| 127 | + print("Usage: python -m mkl_random.patch <install|uninstall|status>") |
| 128 | + sys.exit(1) |
| 129 | + |
| 130 | + command = sys.argv[1] |
| 131 | + try: |
| 132 | + if command == "install": |
| 133 | + install_patch(verbose=True) |
| 134 | + elif command == "uninstall": |
| 135 | + uninstall_patch(verbose=True) |
| 136 | + elif command == "status": |
| 137 | + sys.exit(0 if check_status(verbose=True) else 1) |
| 138 | + else: |
| 139 | + print(f"Unknown command: {command}") |
| 140 | + sys.exit(1) |
| 141 | + except PatchOperationError as exc: |
| 142 | + print(exc, file=sys.stderr) |
| 143 | + sys.exit(1) |
0 commit comments