forked from numba/numba
-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
224 lines (187 loc) · 7.33 KB
/
__init__.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
"""
Expose top-level symbols that are safe for import *
"""
import platform
import re
import sys
import warnings
from ._version import get_versions
from numba.misc.init_utils import generate_version_info
__version__ = get_versions()['version']
version_info = generate_version_info(__version__)
del get_versions
del generate_version_info
from numba.core import config
from numba.core import types, errors
# Re-export typeof
from numba.misc.special import (
typeof, prange, pndindex, gdb, gdb_breakpoint, gdb_init,
literally, literal_unroll,
)
# Re-export error classes
from numba.core.errors import *
# Re-export types itself
import numba.core.types as types
# Re-export all type names
from numba.core.types import *
# Re-export decorators
from numba.core.decorators import (cfunc, generated_jit, jit, njit, stencil,
jit_module)
# Re-export vectorize decorators and the thread layer querying function
from numba.np.ufunc import (vectorize, guvectorize, threading_layer,
get_num_threads, set_num_threads,
set_parallel_chunksize, get_parallel_chunksize,
get_thread_id)
# Re-export Numpy helpers
from numba.np.numpy_support import carray, farray, from_dtype
# Re-export experimental
from numba import experimental
# Initialize withcontexts
import numba.core.withcontexts
from numba.core.withcontexts import objmode_context as objmode
from numba.core.withcontexts import parallel_chunksize
# Initialize target extensions
import numba.core.target_extension
# Initialize typed containers
import numba.typed
# Keep this for backward compatibility.
def test(argv, **kwds):
# To speed up the import time, avoid importing `unittest` and other test
# dependencies unless the user is actually trying to run tests.
from numba.testing import _runtests as runtests
return runtests.main(argv, **kwds)
__all__ = """
cfunc
from_dtype
guvectorize
jit
experimental
njit
stencil
jit_module
typeof
prange
gdb
gdb_breakpoint
gdb_init
vectorize
objmode
literal_unroll
get_num_threads
set_num_threads
set_parallel_chunksize
get_parallel_chunksize
parallel_chunksize
""".split() + types.__all__ + errors.__all__
_min_llvmlite_version = (0, 39, 0)
_min_llvm_version = (11, 0, 0)
def _ensure_llvm():
"""
Make sure llvmlite is operational.
"""
import warnings
import llvmlite
# Only look at the the major, minor and bugfix version numbers.
# Ignore other stuffs
regex = re.compile(r'(\d+)\.(\d+).(\d+)')
m = regex.match(llvmlite.__version__)
if m:
ver = tuple(map(int, m.groups()))
if ver < _min_llvmlite_version:
msg = ("Numba requires at least version %d.%d.%d of llvmlite.\n"
"Installed version is %s.\n"
"Please update llvmlite." %
(_min_llvmlite_version + (llvmlite.__version__,)))
raise ImportError(msg)
else:
# Not matching?
warnings.warn("llvmlite version format not recognized!")
from llvmlite.binding import llvm_version_info, check_jit_execution
if llvm_version_info < _min_llvm_version:
msg = ("Numba requires at least version %d.%d.%d of LLVM.\n"
"Installed llvmlite is built against version %d.%d.%d.\n"
"Please update llvmlite." %
(_min_llvm_version + llvm_version_info))
raise ImportError(msg)
check_jit_execution()
def _ensure_critical_deps():
"""
Make sure Python, NumPy and SciPy have supported versions.
"""
from numba.np.numpy_support import numpy_version
from numba.core.utils import PYVERSION
if PYVERSION < (3, 7):
raise ImportError("Numba needs Python 3.7 or greater")
if numpy_version < (1, 18):
raise ImportError("Numba needs NumPy 1.18 or greater")
elif numpy_version > (1, 22):
raise ImportError("Numba needs NumPy 1.22 or less")
try:
import scipy
except ImportError:
pass
else:
sp_version = tuple(map(int, scipy.__version__.split('.')[:2]))
if sp_version < (1, 0):
raise ImportError("Numba requires SciPy version 1.0 or greater")
def _try_enable_svml():
"""
Tries to enable SVML if configuration permits use and the library is found.
"""
if not config.DISABLE_INTEL_SVML:
try:
if sys.platform.startswith('linux'):
llvmlite.binding.load_library_permanently("libsvml.so")
elif sys.platform.startswith('darwin'):
llvmlite.binding.load_library_permanently("libsvml.dylib")
elif sys.platform.startswith('win'):
llvmlite.binding.load_library_permanently("svml_dispmd")
else:
return False
# The SVML library is loaded, therefore SVML *could* be supported.
# Now see if LLVM has been compiled with the SVML support patch.
# If llvmlite has the checking function `has_svml` and it returns
# True, then LLVM was compiled with SVML support and the the setup
# for SVML can proceed. We err on the side of caution and if the
# checking function is missing, regardless of that being fine for
# most 0.23.{0,1} llvmlite instances (i.e. conda or pip installed),
# we assume that SVML was not compiled in. llvmlite 0.23.2 is a
# bugfix release with the checking function present that will always
# produce correct behaviour. For context see: #3006.
try:
if not getattr(llvmlite.binding.targets, "has_svml")():
# has detection function, but no svml compiled in, therefore
# disable SVML
return False
except AttributeError:
if platform.machine() == 'x86_64' and config.DEBUG:
msg = ("SVML was found but llvmlite >= 0.23.2 is "
"needed to support it.")
warnings.warn(msg)
# does not have detection function, cannot detect reliably,
# disable SVML.
return False
# All is well, detection function present and reports SVML is
# compiled in, set the vector library to SVML.
llvmlite.binding.set_option('SVML', '-vector-library=SVML')
return True
except:
if platform.machine() == 'x86_64' and config.DEBUG:
warnings.warn("SVML was not found/could not be loaded.")
return False
_ensure_llvm()
_ensure_critical_deps()
# we know llvmlite is working as the above tests passed, import it now as SVML
# needs to mutate runtime options (sets the `-vector-library`).
import llvmlite
"""
Is set to True if Intel SVML is in use.
"""
config.USING_SVML = _try_enable_svml()
# ---------------------- WARNING WARNING WARNING ----------------------------
# The following imports occur below here (SVML init) because somewhere in their
# import sequence they have a `@njit` wrapped function. This triggers too early
# a bind to the underlying LLVM libraries which then irretrievably sets the LLVM
# SVML state to "no SVML". See https://github.com/numba/numba/issues/4689 for
# context.
# ---------------------- WARNING WARNING WARNING ----------------------------