numba / numba

NumPy aware dynamic Python compiler using LLVM
https://numba.pydata.org/
BSD 2-Clause "Simplified" License
10.02k stars 1.13k forks source link

Object mode fall back control flow issue, hits opcode RERAISE #7773

Open brodkemd opened 2 years ago

brodkemd commented 2 years ago

I got the following error message when I used the @jit decorator on the function below the error message. This is not the biggest deal but it said to report it so I did.

Traceback (most recent call last):
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/errors.py", line 776, in new_error_context
    yield
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/lowering.py", line 235, in lower_block
    self.lower_inst(inst)
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/pylowering.py", line 168, in lower_inst
    raise NotImplementedError(type(inst), inst)
NotImplementedError: (<class 'numba.core.ir.StaticRaise'>, <numba.core.ir.StaticRaise object at 0x7f94bc91b5b0>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/brodkemd/Documents/ns_q/python_version/ns_q.py", line 1644, in <module>
    run()
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/dispatcher.py", line 501, in _compile_for_args
    raise e
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/dispatcher.py", line 434, in _compile_for_args
    return_val = self.compile(tuple(argtypes))
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/dispatcher.py", line 979, in compile
    cres = self._compiler.compile(args, return_type)
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/dispatcher.py", line 141, in compile
    status, retval = self._compile_cached(args, return_type)
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/dispatcher.py", line 155, in _compile_cached
    retval = self._compile_core(args, return_type)
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/dispatcher.py", line 168, in _compile_core
    cres = compiler.compile_extra(self.targetdescr.typing_context,
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/compiler.py", line 686, in compile_extra
    return pipeline.compile_extra(func)
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/compiler.py", line 428, in compile_extra
    return self._compile_bytecode()
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/compiler.py", line 492, in _compile_bytecode
    return self._compile_core()
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/compiler.py", line 471, in _compile_core
    raise e
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/compiler.py", line 462, in _compile_core
    pm.run(self.state)
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/compiler_machinery.py", line 343, in run
    raise patched_exception
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/compiler_machinery.py", line 334, in run
    self._runPass(idx, pass_inst, state)
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/compiler_lock.py", line 35, in _acquire_compile_lock
    return func(*args, **kwargs)
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/compiler_machinery.py", line 289, in _runPass
    mutated |= check(pss.run_pass, internal_state)
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/compiler_machinery.py", line 262, in check
    mangled = func(compiler_state)
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/object_mode_passes.py", line 120, in run_pass
    lowered = backend_object_mode()
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/object_mode_passes.py", line 115, in backend_object_mode
    return self._py_lowering_stage(state.targetctx,
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/object_mode_passes.py", line 78, in _py_lowering_stage
    lower.lower()
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/lowering.py", line 138, in lower
    self.lower_normal_function(self.fndesc)
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/lowering.py", line 192, in lower_normal_function
    entry_block_tail = self.lower_function_body()
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/lowering.py", line 221, in lower_function_body
    self.lower_block(block)
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/lowering.py", line 235, in lower_block
    self.lower_inst(inst)
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/contextlib.py", line 137, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/home/brodkemd/anaconda3/envs/qiskit/lib/python3.9/site-packages/numba/core/errors.py", line 786, in new_error_context
    raise newerr.with_traceback(tb)
numba.core.errors.LoweringError: Failed in object mode pipeline (step: object mode backend)
(<class 'numba.core.ir.StaticRaise'>, <numba.core.ir.StaticRaise object at 0x7f94bc91b5b0>)

File "ns_q.py", line 1642:
def run():
    <source elided>
        if option:
            inst.WriteResults()
            ^

During: lowering "<static> raise <class 'AssertionError'>("Unreachable condition reached (op code RERAISE executed)\n-------------------------------------------------------------------------------\nThis should not have happened, a problem has occurred in Numba's internals.\nYou are currently using Numba version 0.54.1.\n\nPlease report the error message and traceback, along with a minimal reproducer\nat: https://github.com/numba/numba/issues/new\n\nIf more help is needed please feel free to speak to the Numba core developers\ndirectly at: https://gitter.im/numba/numba\n\nThanks in advance for your help in improving Numba!\n\n")" at /home/brodkemd/Documents/ns_q/python_version/ns_q.py (1642)

When the decorator is removed it works as intended. Here is the Function:

@jit
def run():
    inst = ns_q()
    # catches keyboard interupts and asks if you want the data from the run
    try:
        inst.run()
    except KeyboardInterrupt:
        print("\n\nExecution halted")
        option = int(input("Do you want to record the data that was generated(1=yes, 0=no)?:"))
        # saves the data
        if option:
            inst.WriteResults()
stuartarchibald commented 2 years ago

Thanks for the report. The issue is that the @jit decorated code has a RERAISE op code in it which is not supported. Numba is best at dealing with numerical code and it is generally not a good fit for code like the above. The JIT compiler has fallen back to using Object mode (see what this is in the 5 minute guide, link below) and as a result, even if it compiled, there's a reasonable chance it would be no faster than the CPython interpreter, and could even be slower!

If you are new to Numba, perhaps take a look at the 5 minute guide https://numba.readthedocs.io/en/stable/user/5minguide.html first and/or click one of the Try Now buttons here https://numba.pydata.org/ to open up an interactive Numba demo in your browser.

stuartarchibald commented 2 years ago

Marking this as a bug, I think this should have either compiled or failed to compile more gracefully. I'm reasonably convinced that this use case is unsupported in object mode and so failing to compile gracefully is probably the aim for fixing this issue.