Hi everyone,
I am facing a Python issue that I think is related to Numba and I would be very grateful to have the opinion of someone more familiar with Numba source code. Unfortunately, I do not have a minimal reproducer, but I will try my best to give as many details as I can in the following.
I am using a fluid dynamics framework, mostly written in FORTRAN, for which it is possible to provide Python snippets that define parts of the simulation into the simulation’s input file. I am making use of Numba AOT compilation to speed up some of the Python functions these snippets rely on. Unfortunately, I am running into the following (scary) error while attempting to run the simulation:
SystemError: ../Objects/tupleobject.c:159: bad argument to internal function
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<string>", line 2, in val
File "<frozen importlib._bootstrap>", line 1044, in _handle_fromlist
SystemError: <built-in function isinstance> returned a result with an error set
After some time, I have understood that this error originates from an import statement within one of the Python snippets. The import accesses a regular Python file (say, constants.py) in the same directory as the input file where some constant values for the simulation are written/calculated. Going through this file line by line, I have further discovered that the error above is linked to another import statement, within constants.py this time. This import again accesses another Python file in the same directory (say, data.py) that contains some data the simulation requires. Going line by line through data.py, I have found that the following lines lead to the error:
from numba import float64
from numba.types import unicode_type
from numba.typed import Dict
foo = Dict.empty(key_type=unicode_type, value_type=float64[:, :])
foo["bar"] = array([[0.456, -0.123, -0.678], [-0.256, 0.856, 0.541]])
In particular, the very last line is problematic. Commenting it out (in other words, having foo empty) avoids the error. Moreover, getting rid of foo in data.py and directly having it in the functions that require it also looks to work fine. However, foo is not the only Typed Dict I am using within this simulation, and another one later on yields the exact same error. The problem with the latter is that it is created and destroyed at every simulation step, meaning that its content is not constant and, hence, I cannot just hard-code it where it would not raise the error. Leaving it empty raises a legitimate KeyError as some code is trying to access the non-existing content of the Typed Dict. I have tried to define the dictionary using curly braces instead of the Dict.empty constructor. This time, I do not get the error, but instead a SegFault, which I believe is linked to the inferred type of the dictionary data not matching the signature provided for the functions compiled AOT. Finally, the last piece of evidence I have, running the simulation on different systems does not always yield the error, despite all systems using the same major versions of Python and same versions of Numba and NumPy.
On a side note, I have found this Python bug which might be relevant.
I am very much puzzled at what is going on here, and I would definitely appreciate some guidance. I am also happy to provide more information but, as I mentioned earlier, I have not succeeded in reproducing the error outside of the fluid dynamics framework.
Thank you for any help.