I'm using numba
to compile some expensive calcualtion for signifcant performance gains - this is wonderful! Recently I made a small change to the calcualtion to extract some additional values (eigenvalues), cleared the cache and started to test. Everything compiles without error and starts to run.
Most times nothing happens but then every so often, it just crashes with the following error message:
SystemError: <function _numba_unpickle at 0x0000027532E4EFC0> returned a result with an exception set
I've compiled all my functions with either:
@njit(error_model="numpy", cache=True)
or
@njit(error_model="numpy", cache=True, parallel=True)
where applicable. I'm using puthon 3.12.9
, numpy 2.1.3
and numba 0.61.0
and whatever other depdendencies they pull in.
Although I've set the debugger to stop upon a raised exception, it doesn't really help (much) as there appears to be nothing wrong with the data or code. Running all the data through the code contained in the compiled function in the console yields no errors and I can even call the compiled function that raised the error in the debugger and it runs. It is not aconsistently reproducible error.
When I call the compiled function that raised the error one of three things happens: it returns the correct result, it returns a nonesense result where everything is junk (some value xxxxxe+303 - possibly meaning the eigenvalue calc has failed to converge?) or it returns the exception set error.
The occurence of the error seems to be quite random as I test repeatedly on the same data: sometimes it runs into an error after one or two fuction calls, other times it can be a hundred calls to the function before the error re-occurs. In that case I'm able to extract the following error:
Traceback (most recent call last):
File "C:\Users\jpmor\anaconda3\envs\new_base\Lib\site-packages\numba\np\linalg.py", line 841, in _check_finite_matrix
raise np.linalg.LinAlgError(
numpy.linalg.LinAlgError: Array must not contain infs or NaNs.
The above exception was the direct cause of the following exception:
I only use np.linalg
twice in my entire code which helps narrow the field:
len = np.linalg.norm(vect[indx, :])
and later
eigenvalues = np.linalg.eigvalsh(stress.reshape(3, 3))
Since _check_finite_matrix
is called by
@overload(np.linalg.eigvals)
def eigvals_impl(a):
....
this must be the source of my problem but I don't understand why on the same data sometimes it will throw an error and othertimes it runs perfectly. From the numpy
docs I can see that a LinAlgError
will be raised if the eigenvalue computation does not converge.
I believe this error is convergence related but only seems to happen with numba
as I never seem to get the error when running with the slower, uncompiled function in pure numpy
.
I guess a possible simple solution would be to not use numba
but I would like my code to finish running in the same day. Is there an alternative to np.linalg.eigvals
that is stable with numba
? Is it possible to write a home-made function that would work and be as quick?