-
-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Expand file tree
/
Copy pathintrospect.py
More file actions
94 lines (82 loc) · 2.68 KB
/
introspect.py
File metadata and controls
94 lines (82 loc) · 2.68 KB
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
"""
Introspection helper functions.
"""
__all__ = ['opt_func_info']
def opt_func_info(func_name=None, signature=None):
"""
Returns a dictionary containing the currently supported CPU dispatched
features for all optimized functions.
Parameters
----------
func_name : str (optional)
Regular expression to filter by function name.
signature : str (optional)
Regular expression to filter by data type.
Returns
-------
dict
A dictionary where keys are optimized function names and values are
nested dictionaries indicating supported targets based on data types.
Examples
--------
Retrieve dispatch information for functions named 'add' or 'sub' and
data types 'float64' or 'float32':
>>> import numpy as np
>>> dict = np.lib.introspect.opt_func_info(
... func_name="add|abs", signature="float64|complex64"
... )
>>> import json
>>> print(json.dumps(dict, indent=2)) # may vary (architecture)
{
"absolute": {
"dd": {
"current": "SSE41",
"available": "SSE41 baseline(SSE SSE2 SSE3)"
},
"Ff": {
"current": "FMA3__AVX2",
"available": "AVX512F FMA3__AVX2 baseline(SSE SSE2 SSE3)"
},
"Dd": {
"current": "FMA3__AVX2",
"available": "AVX512F FMA3__AVX2 baseline(SSE SSE2 SSE3)"
}
},
"add": {
"ddd": {
"current": "FMA3__AVX2",
"available": "FMA3__AVX2 baseline(SSE SSE2 SSE3)"
},
"FFF": {
"current": "FMA3__AVX2",
"available": "FMA3__AVX2 baseline(SSE SSE2 SSE3)"
}
}
}
"""
import re
from numpy._core._multiarray_umath import __cpu_targets_info__ as targets, dtype
if func_name is not None:
func_pattern = re.compile(func_name)
matching_funcs = {
k: v for k, v in targets.items()
if func_pattern.search(k)
}
else:
matching_funcs = targets
if signature is not None:
sig_pattern = re.compile(signature)
matching_sigs = {}
for k, v in matching_funcs.items():
matching_chars = {}
for chars, targets in v.items():
if any(
sig_pattern.search(c) or sig_pattern.search(dtype(c).name)
for c in chars
):
matching_chars[chars] = targets # noqa: PERF403
if matching_chars:
matching_sigs[k] = matching_chars
else:
matching_sigs = matching_funcs
return matching_sigs