|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import ast |
| 5 | +import importlib |
| 6 | +import pathlib |
| 7 | + |
| 8 | + |
| 9 | +def get_njit_funcs(pkg_dir): |
| 10 | + """ |
| 11 | + Identify all njit functions |
| 12 | +
|
| 13 | + Parameters |
| 14 | + ---------- |
| 15 | + pkg_dir : str |
| 16 | + The path to the directory containing some .py files |
| 17 | +
|
| 18 | + Returns |
| 19 | + ------- |
| 20 | + njit_funcs : list |
| 21 | + A list of all njit functions, where each element is a tuple of the form |
| 22 | + (module_name, func_name) |
| 23 | + """ |
| 24 | + ignore_py_files = ["__init__", "__pycache__"] |
| 25 | + pkg_dir = pathlib.Path(pkg_dir) |
| 26 | + |
| 27 | + module_names = [] |
| 28 | + for fname in pkg_dir.iterdir(): |
| 29 | + if fname.stem not in ignore_py_files and not fname.stem.startswith("."): |
| 30 | + module_names.append(fname.stem) |
| 31 | + |
| 32 | + njit_funcs = [] |
| 33 | + for module_name in module_names: |
| 34 | + filepath = pkg_dir / f"{module_name}.py" |
| 35 | + file_contents = "" |
| 36 | + with open(filepath, encoding="utf8") as f: |
| 37 | + file_contents = f.read() |
| 38 | + module = ast.parse(file_contents) |
| 39 | + for node in module.body: |
| 40 | + if isinstance(node, ast.FunctionDef): |
| 41 | + func_name = node.name |
| 42 | + for decorator in node.decorator_list: |
| 43 | + decorator_name = None |
| 44 | + if isinstance(decorator, ast.Name): |
| 45 | + # Bare decorator |
| 46 | + decorator_name = decorator.id |
| 47 | + if isinstance(decorator, ast.Call) and isinstance( |
| 48 | + decorator.func, ast.Name |
| 49 | + ): |
| 50 | + # Decorator is a function |
| 51 | + decorator_name = decorator.func.id |
| 52 | + |
| 53 | + if decorator_name == "njit": |
| 54 | + njit_funcs.append((module_name, func_name)) |
| 55 | + |
| 56 | + return njit_funcs |
| 57 | + |
| 58 | + |
| 59 | +def check_fastmath(pkg_dir, pkg_name): |
| 60 | + """ |
| 61 | + Check if all njit functions have the `fastmath` flag set |
| 62 | +
|
| 63 | + Parameters |
| 64 | + ---------- |
| 65 | + pkg_dir : str |
| 66 | + The path to the directory containing some .py files |
| 67 | +
|
| 68 | + pkg_name : str |
| 69 | + The name of the package |
| 70 | +
|
| 71 | + Returns |
| 72 | + ------- |
| 73 | + None |
| 74 | + """ |
| 75 | + missing_fastmath = [] # list of njit functions with missing fastmath flags |
| 76 | + for module_name, func_name in get_njit_funcs(pkg_dir): |
| 77 | + module = importlib.import_module(f".{module_name}", package=pkg_name) |
| 78 | + func = getattr(module, func_name) |
| 79 | + if "fastmath" not in func.targetoptions.keys(): |
| 80 | + missing_fastmath.append(f"{module_name}.{func_name}") |
| 81 | + |
| 82 | + if len(missing_fastmath) > 0: |
| 83 | + msg = ( |
| 84 | + "Found one or more `@njit` functions that are missing the `fastmath` flag. " |
| 85 | + + f"The functions are:\n {missing_fastmath}\n" |
| 86 | + ) |
| 87 | + raise ValueError(msg) |
| 88 | + |
| 89 | + return |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + parser = argparse.ArgumentParser() |
| 94 | + parser.add_argument("--check", dest="pkg_dir") |
| 95 | + args = parser.parse_args() |
| 96 | + |
| 97 | + if args.pkg_dir: |
| 98 | + pkg_dir = pathlib.Path(args.pkg_dir) |
| 99 | + pkg_name = pkg_dir.name |
| 100 | + check_fastmath(str(pkg_dir), pkg_name) |
0 commit comments