-
Notifications
You must be signed in to change notification settings - Fork 24
task: module CLI patching methods #319
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
jharlow-intel
wants to merge
4
commits into
master
Choose a base branch
from
task/SAT-8469
base: master
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
4 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Copyright (c) 2017, 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_fft.""" | ||
|
|
||
| import sys | ||
|
|
||
|
|
||
| def main_impl(): | ||
| if len(sys.argv) < 2: | ||
| print("Usage: python -m mkl_fft <command> [args]") | ||
| print() | ||
| print("Commands:") | ||
|
jharlow-intel marked this conversation as resolved.
|
||
| print(" patch install Install persistent NumPy FFT patch") | ||
| print(" patch uninstall Uninstall persistent NumPy FFT patch") | ||
| print(" patch status Check if persistent patch is installed") | ||
| print(" with_patch <cmd> Run command with temporary NumPy FFT patch") | ||
| print() | ||
| print("Examples:") | ||
| print(" python -m mkl_fft patch install") | ||
| print(" python -m mkl_fft with_patch python script.py") | ||
| sys.exit(1) | ||
|
|
||
| command = sys.argv[1] | ||
|
|
||
| if command == "patch": | ||
| from mkl_fft.patch import main as patch_main | ||
|
|
||
| patch_main(sys.argv[2:]) | ||
| elif command == "with_patch": | ||
| from mkl_fft.with_patch import main as with_patch_main | ||
|
|
||
| with_patch_main(sys.argv[2:]) | ||
| else: | ||
| print(f"Unknown command: {command}") | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| def main(): | ||
| """Entry point for the CLI.""" | ||
| main_impl() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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 |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Copyright (c) 2017, 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_fft | ||
|
|
||
| mkl_fft.patch_numpy_fft() | ||
| except Exception: | ||
| pass |
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 |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| # Copyright (c) 2017, 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 | ||
| # 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. | ||
|
|
||
| """Persistent patch management for NumPy FFT submodule.""" | ||
|
|
||
| import argparse | ||
| import site | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def get_pth_path(): | ||
| """Get the path to mkl_fft_patch.pth in the appropriate site-packages.""" | ||
| site_packages = site.getsitepackages() | ||
| if site_packages: | ||
| target_site = site_packages[0] | ||
| else: | ||
| target_site = site.getusersitepackages() | ||
| return Path(target_site) / "mkl_fft_patch.pth" | ||
|
|
||
|
|
||
| PTH_CONTENT = """import mkl_fft._patch_startup""" | ||
|
|
||
|
|
||
| def install_patch(): | ||
| """Install persistent NumPy FFT patch using .pth file.""" | ||
| pth_path = get_pth_path() | ||
|
|
||
| if pth_path.exists(): | ||
| print(f"Persistent patch already installed at {pth_path}") | ||
| return | ||
|
|
||
| try: | ||
| pth_path.parent.mkdir(parents=True, exist_ok=True) | ||
| pth_path.write_text(PTH_CONTENT) | ||
| print(f"Persistent patch installed at {pth_path}") | ||
| print() | ||
| print("NumPy FFT will now use MKL-accelerated implementations in all") | ||
| print("Python sessions. To disable, run:") | ||
| print(" python -m mkl_fft patch uninstall") | ||
| except OSError as e: | ||
| print(f"Error installing patch: {e}") | ||
| print() | ||
| print("You may need to run with appropriate permissions or install to") | ||
| print("a user site-packages directory.") | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| def uninstall_patch(): | ||
| """Uninstall persistent NumPy FFT patch.""" | ||
| pth_path = get_pth_path() | ||
|
|
||
| if not pth_path.exists(): | ||
| print("No persistent patch found.") | ||
| return | ||
|
|
||
| try: | ||
| pth_path.unlink() | ||
| print(f"Persistent patch removed from {pth_path}") | ||
| print() | ||
| print("NumPy FFT will now use the default implementations.") | ||
| except OSError as e: | ||
| print(f"Error removing patch: {e}") | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| def check_status(): | ||
| """Check if persistent patch is installed.""" | ||
| pth_path = get_pth_path() | ||
|
|
||
| if pth_path.exists(): | ||
| print(f"Persistent patch is installed at {pth_path}") | ||
| print() | ||
| print("NumPy FFT is configured to use MKL-accelerated implementations.") | ||
| return True | ||
| else: | ||
| print("No persistent patch installed") | ||
| print() | ||
| print("To enable MKL-accelerated NumPy FFT globally, run:") | ||
| print(" python -m mkl_fft patch install") | ||
| return False | ||
|
|
||
|
|
||
| def main(args=None): | ||
| """Main entry point for patch command.""" | ||
| parser = argparse.ArgumentParser( | ||
| prog="python -m mkl_fft patch", | ||
| description="Manage persistent NumPy FFT patching with MKL acceleration", | ||
| ) | ||
| subparsers = parser.add_subparsers( | ||
| dest="command", help="Available commands" | ||
| ) | ||
|
|
||
| subparsers.add_parser("install", help="Install persistent NumPy FFT patch") | ||
| subparsers.add_parser( | ||
| "uninstall", help="Uninstall persistent NumPy FFT patch" | ||
| ) | ||
| subparsers.add_parser( | ||
| "status", help="Check if persistent patch is installed" | ||
| ) | ||
|
|
||
| parsed_args = parser.parse_args(args) | ||
|
|
||
| if not parsed_args.command: | ||
| parser.print_help() | ||
| sys.exit(1) | ||
|
|
||
| if parsed_args.command == "install": | ||
| install_patch() | ||
| elif parsed_args.command == "uninstall": | ||
| uninstall_patch() | ||
| elif parsed_args.command == "status": | ||
| sys.exit(0 if check_status() else 1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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.