-
Notifications
You must be signed in to change notification settings - Fork 338
Fix #1059 missing fastmath #1060
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
280eb9b
add function to check fastmath
NimaSarajpoor e9d1256
revise fastmath script and add support for reading arg from command line
NimaSarajpoor 857c281
minor fixes
NimaSarajpoor df9840f
rename function to improve readability
NimaSarajpoor e4edee2
simplify code by passing boolean value
NimaSarajpoor 9bf37f7
fix to catch njit functions with decorator
NimaSarajpoor a90c8d4
use regex to find njit functions
NimaSarajpoor b8ffac9
minor change
NimaSarajpoor ed6cef0
revise code to detect bare njit decorator
NimaSarajpoor 799aceb
minor fix
NimaSarajpoor 341e914
fix path
NimaSarajpoor 9f84dd5
add missing fastmath
NimaSarajpoor f479dbe
revise fastmath flag
NimaSarajpoor d7d1a79
Improve ValueError msg
NimaSarajpoor b6adc93
fix format
NimaSarajpoor f497d44
enable function to accept path as input
NimaSarajpoor 9fa7805
pass param via CLI, and some minor changes
NimaSarajpoor 56fd344
adapt changes in test script
NimaSarajpoor 3c5ba61
use type str for the param pkg_dir
NimaSarajpoor 8e82765
minor changes
NimaSarajpoor de242d5
Revised string concatenation in error message
NimaSarajpoor 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| import argparse | ||
| import ast | ||
| import importlib | ||
| import pathlib | ||
|
|
||
|
|
||
| def get_njit_funcs(pkg_dir): | ||
| """ | ||
| Identify all njit functions | ||
|
|
||
| Parameters | ||
| ---------- | ||
| pkg_dir : str | ||
| The path to the directory containing some .py files | ||
|
|
||
| Returns | ||
| ------- | ||
| njit_funcs : list | ||
| A list of all njit functions, where each element is a tuple of the form | ||
| (module_name, func_name) | ||
| """ | ||
| ignore_py_files = ["__init__", "__pycache__"] | ||
| pkg_dir = pathlib.Path(pkg_dir) | ||
|
|
||
| module_names = [] | ||
| for fname in pkg_dir.iterdir(): | ||
| if fname.stem not in ignore_py_files and not fname.stem.startswith("."): | ||
| module_names.append(fname.stem) | ||
|
|
||
| njit_funcs = [] | ||
| for module_name in module_names: | ||
| filepath = pkg_dir / f"{module_name}.py" | ||
| file_contents = "" | ||
| with open(filepath, encoding="utf8") as f: | ||
| file_contents = f.read() | ||
| module = ast.parse(file_contents) | ||
| for node in module.body: | ||
| if isinstance(node, ast.FunctionDef): | ||
| func_name = node.name | ||
| for decorator in node.decorator_list: | ||
| decorator_name = None | ||
| if isinstance(decorator, ast.Name): | ||
| # Bare decorator | ||
| decorator_name = decorator.id | ||
| if isinstance(decorator, ast.Call) and isinstance( | ||
| decorator.func, ast.Name | ||
| ): | ||
| # Decorator is a function | ||
| decorator_name = decorator.func.id | ||
|
|
||
| if decorator_name == "njit": | ||
| njit_funcs.append((module_name, func_name)) | ||
|
|
||
| return njit_funcs | ||
|
|
||
|
|
||
| def check_fastmath(pkg_dir, pkg_name): | ||
| """ | ||
| Check if all njit functions have the `fastmath` flag set | ||
|
|
||
| Parameters | ||
| ---------- | ||
| pkg_dir : str | ||
| The path to the directory containing some .py files | ||
|
|
||
| pkg_name : str | ||
| The name of the package | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| missing_fastmath = [] # list of njit functions with missing fastmath flags | ||
| for module_name, func_name in get_njit_funcs(pkg_dir): | ||
| module = importlib.import_module(f".{module_name}", package=pkg_name) | ||
| func = getattr(module, func_name) | ||
| if "fastmath" not in func.targetoptions.keys(): | ||
| missing_fastmath.append(f"{module_name}.{func_name}") | ||
|
|
||
| if len(missing_fastmath) > 0: | ||
| msg = ( | ||
| "Found one or more `@njit` functions that are missing the `fastmath` flag. " | ||
| + f"The functions are:\n {missing_fastmath}\n" | ||
| ) | ||
| raise ValueError(msg) | ||
|
|
||
| return | ||
|
|
||
|
|
||
NimaSarajpoor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--check", dest="pkg_dir") | ||
| args = parser.parse_args() | ||
|
|
||
| if args.pkg_dir: | ||
| pkg_dir = pathlib.Path(args.pkg_dir) | ||
| pkg_name = pkg_dir.name | ||
| check_fastmath(str(pkg_dir), pkg_name) | ||
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
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.