-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathfastmath.py
executable file
·486 lines (383 loc) · 13.6 KB
/
fastmath.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/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
class FunctionCallVisitor(ast.NodeVisitor):
"""
A class to traverse the AST of the modules of a package to collect
the call stacks of njit functions.
Parameters
----------
pkg_dir : str
The path to the package directory containing some .py files.
pkg_name : str
The name of the package.
Attributes
----------
module_names : list
A list of module names to track the modules as the visitor traverses them.
call_stack : list
A list of njit functions, representing a chain of function calls,
where each element is a string of the form "module_name.func_name".
out : list
A list of unique `call_stack`s.
njit_funcs : list
A list of all njit functions in `pkg_dir`'s modules. Each element is a tuple
of the form `(module_name, func_name)`.
njit_modules : set
A set that contains the names of all modules, each of which contains at least
one njit function.
njit_nodes : dict
A dictionary mapping njit function names to their corresponding AST nodes.
A key is a string, and it is of the form "module_name.func_name", and its
corresponding value is the AST node- with type ast.FunctionDef- of that
function.
ast_modules : dict
A dictionary mapping module names to their corresponding AST objects. A key
is the name of a module, and its corresponding value is the content of that
module as an AST object.
Methods
-------
push_module(module_name)
Push the name of a module onto the stack `module_names`.
pop_module()
Pop the last module name from the stack `module_names`.
push_call_stack(module_name, func_name)
Push a function call onto the stack of function calls, `call_stack`.
pop_call_stack()
Pop the last function call from the stack of function calls, `call_stack`
goto_deeper_func(node)
Calls the visit method from class `ast.NodeVisitor` on all children of
the `node`.
goto_next_func(node)
Calls the visit method from class `ast.NodeVisitor` on all children of
the `node`.
push_out()
Push the current function call stack, `call_stack`, onto the output list, `out`,
unless it is already included in one of the so-far-collected call stacks.
visit_Call(node)
This method is called when the visitor encounters a function call in the AST. It
checks if the called function is a njit function and, if so, traverses its AST
to collect its call stack.
"""
def __init__(self, pkg_dir, pkg_name):
"""
Initialize the FunctionCallVisitor class. This method sets up the necessary
attributes and prepares the visitor for traversing the AST of STUMPY's modules.
Parameters
----------
pkg_dir : str
The path to the package directory containing some .py files.
pkg_name : str
The name of the package.
Returns
-------
None
"""
super().__init__()
self.module_names = []
self.call_stack = []
self.out = []
# Setup lists, dicts, and ast objects
self.njit_funcs = get_njit_funcs(pkg_dir)
self.njit_modules = set(mod_name for mod_name, func_name in self.njit_funcs)
self.njit_nodes = {}
self.ast_modules = {}
filepaths = sorted(f for f in pathlib.Path(pkg_dir).iterdir() if f.is_file())
ignore = ["__init__.py", "__pycache__"]
for filepath in filepaths:
file_name = filepath.name
if (
file_name not in ignore
and not file_name.startswith("gpu")
and str(filepath).endswith(".py")
):
module_name = file_name.replace(".py", "")
file_contents = ""
with open(filepath, encoding="utf8") as f:
file_contents = f.read()
self.ast_modules[module_name] = ast.parse(file_contents)
for node in self.ast_modules[module_name].body:
if isinstance(node, ast.FunctionDef):
func_name = node.name
if (module_name, func_name) in self.njit_funcs:
self.njit_nodes[f"{module_name}.{func_name}"] = node
def push_module(self, module_name):
"""
Push a module name onto the stack of module names.
Parameters
----------
module_name : str
The name of the module to be pushed onto the stack.
Returns
-------
None
"""
self.module_names.append(module_name)
return
def pop_module(self):
"""
Pop the last module name from the stack of module names.
Parameters
----------
None
Returns
-------
None
"""
if self.module_names:
self.module_names.pop()
return
def push_call_stack(self, module_name, func_name):
"""
Push a function call onto the stack of function calls.
Parameters
----------
module_name : str
A module's name
func_name : str
A function's name
Returns
-------
None
"""
self.call_stack.append(f"{module_name}.{func_name}")
return
def pop_call_stack(self):
"""
Pop the last function call from the stack of function calls.
Parameters
----------
None
Returns
-------
None
"""
if self.call_stack:
self.call_stack.pop()
return
def goto_deeper_func(self, node):
"""
Calls the visit method from class `ast.NodeVisitor` on
all children of the `node`.
Parameters
----------
node : ast.AST
The AST node to be visited.
Returns
-------
None
"""
self.generic_visit(node)
return
def goto_next_func(self, node):
"""
Calls the visit method from class `ast.NodeVisitor` on
all children of the node.
Parameters
----------
node : ast.AST
The AST node to be visited.
Returns
-------
None
"""
self.generic_visit(node)
return
def push_out(self):
"""
Push the current function call stack onto the output list unless it
is already included in one of the so-far-collected call stacks.
Parameters
----------
None
Returns
-------
None
"""
unique = True
for cs in self.out:
if " ".join(self.call_stack) in " ".join(cs):
unique = False
break
if unique:
self.out.append(self.call_stack.copy())
return
def visit_Call(self, node):
"""
Called when visiting an AST node of type `ast.Call`.
Parameters
----------
node : ast.Call
The AST node representing a function call.
Returns
-------
None
"""
callee_name = ast.unparse(node.func)
module_changed = False
if "." in callee_name:
new_module_name, new_func_name = callee_name.split(".")[:2]
if new_module_name in self.njit_modules:
self.push_module(new_module_name)
module_changed = True
else:
if self.module_names:
new_module_name = self.module_names[-1]
new_func_name = callee_name
callee_name = f"{new_module_name}.{new_func_name}"
if callee_name in self.njit_nodes.keys():
callee_node = self.njit_nodes[callee_name]
self.push_call_stack(new_module_name, new_func_name)
self.goto_deeper_func(callee_node)
self.push_out()
self.pop_call_stack()
if module_changed:
self.pop_module()
self.goto_next_func(node)
return
def get_njit_call_stacks(pkg_dir, pkg_name):
"""
Get the call stacks of all njit functions in `pkg_dir`
Parameters
----------
pkg_dir : str
The path to the package directory containing some .py files
pkg_name : str
The name of the package
Returns
-------
out : list
A list of unique function call stacks. Each item is of type list,
representing a chain of function calls.
"""
visitor = FunctionCallVisitor(pkg_dir, pkg_name)
for module_name in visitor.njit_modules:
visitor.push_module(module_name)
for node in visitor.ast_modules[module_name].body:
if isinstance(node, ast.FunctionDef):
func_name = node.name
if (module_name, func_name) in visitor.njit_funcs:
visitor.push_call_stack(module_name, func_name)
visitor.visit(node)
visitor.pop_call_stack()
visitor.pop_module()
return visitor.out
def check_call_stack_fastmath(pkg_dir, pkg_name):
"""
Check if all njit functions in a call stack have the same `fastmath` flag.
This function raises a ValueError if it finds any inconsistencies in the
`fastmath` flags in at lease one call stack of njit functions.
Parameters
----------
pkg_dir : str
The path to the directory containing some .py files
pkg_name : str
The name of the package
Returns
-------
None
"""
# List of call stacks with inconsistent fastmath flags
inconsistent_call_stacks = []
njit_call_stacks = get_njit_call_stacks(pkg_dir, pkg_name)
for cs in njit_call_stacks:
# Set the fastmath flag of the first function in the call stack
# as the reference flag
module_name, func_name = cs[0].split(".")
module = importlib.import_module(f".{module_name}", package="stumpy")
func = getattr(module, func_name)
flag_ref = func.targetoptions["fastmath"]
for item in cs[1:]:
module_name, func_name = cs[0].split(".")
module = importlib.import_module(f".{module_name}", package="stumpy")
func = getattr(module, func_name)
flag = func.targetoptions["fastmath"]
if flag != flag_ref:
inconsistent_call_stacks.append(cs)
break
if len(inconsistent_call_stacks) > 0:
msg = (
"Found at least one call stack that has inconsistent `fastmath` flags. "
+ f"Those call stacks are:\n {inconsistent_call_stacks}\n"
)
raise ValueError(msg)
return
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)
check_call_stack_fastmath(str(pkg_dir), pkg_name)