Numba: Failed in nopython mode pipeline (step: nopython mode backend)

Created on 29 Aug 2020  路  3Comments  路  Source: numba/numba

Reporting a bug

  • [O] I have tried using the latest released version of Numba (most recent is
    visible in the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
  • [O] I have included below a minimal working reproducer (if you are unsure how
    to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).

Numba version: '0.49.1'
python verions: 3.7.4

Below is a code I tried to run.
numba_test.py is at 'D:/code/numba_test..py'
another_test.py is at 'D:/other_codes/another_test.py'

numba_test.py

import numba
import numpy as np

@numba.jit(nopython = True)
def calc(vector):
    x = 0
    for v in vector:
        x += v
    return x

class Test:
    def run(self):
        return calc(np.arange(10))

another_test.py

import importlib.util
spec = importlib.util.spec_from_file_location('numba_test', 'D:/code/numba_test.py')
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
test = foo.Test()
test.run()

I ran another_test.py and got following error.

Just running test.run() in the directory of numba_test.py works fine without error.
Would you please help?
Thank you

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
c:\program files\python37\lib\site-packages\numba\core\lowering.py in from_fndesc(cls, fndesc)
     29             # Avoid creating new Env
---> 30             return cls._memo[fndesc.env_name]
     31         except KeyError:

c:\program files\python37\lib\weakref.py in __getitem__(self, key)
    136             self._commit_removals()
--> 137         o = self.data[key]()
    138         if o is None:

KeyError: '_ZN08NumbaEnv10numba_test8calc$241E5ArrayIiLi1E1C7mutable7alignedE'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-6-01cc4cff09a5> in <module>
----> 1 test.run()

D:/code/numba_test.py in run(self)
     11 class Test:
     12     def run(self):
---> 13         return calc(np.arange(10))

c:\program files\python37\lib\site-packages\numba\core\dispatcher.py in _compile_for_args(self, *args, **kws)
    418                     e.patch_message('\n'.join((str(e).rstrip(), help_msg)))
    419             # ignore the FULL_TRACEBACKS config, this needs reporting!
--> 420             raise e
    421 
    422     def inspect_llvm(self, signature=None):

c:\program files\python37\lib\site-packages\numba\core\dispatcher.py in _compile_for_args(self, *args, **kws)
    351                 argtypes.append(self.typeof_pyval(a))
    352         try:
--> 353             return self.compile(tuple(argtypes))
    354         except errors.ForceLiteralArg as e:
    355             # Received request for compiler re-entry with the list of arguments

c:\program files\python37\lib\site-packages\numba\core\compiler_lock.py in _acquire_compile_lock(*args, **kwargs)
     30         def _acquire_compile_lock(*args, **kwargs):
     31             with self:
---> 32                 return func(*args, **kwargs)
     33         return _acquire_compile_lock
     34 

c:\program files\python37\lib\site-packages\numba\core\dispatcher.py in compile(self, sig)
    792             self._cache_misses[sig] += 1
    793             try:
--> 794                 cres = self._compiler.compile(args, return_type)
    795             except errors.ForceLiteralArg as e:
    796                 def folded(args, kws):

c:\program files\python37\lib\site-packages\numba\core\dispatcher.py in compile(self, args, return_type)
     75 
     76     def compile(self, args, return_type):
---> 77         status, retval = self._compile_cached(args, return_type)
     78         if status:
     79             return retval

c:\program files\python37\lib\site-packages\numba\core\dispatcher.py in _compile_cached(self, args, return_type)
     89 
     90         try:
---> 91             retval = self._compile_core(args, return_type)
     92         except errors.TypingError as e:
     93             self._failed_cache[key] = e

c:\program files\python37\lib\site-packages\numba\core\dispatcher.py in _compile_core(self, args, return_type)
    107                                       args=args, return_type=return_type,
    108                                       flags=flags, locals=self.locals,
--> 109                                       pipeline_class=self.pipeline_class)
    110         # Check typing error if object mode is used
    111         if cres.typing_error is not None and not flags.enable_pyobject:

c:\program files\python37\lib\site-packages\numba\core\compiler.py in compile_extra(typingctx, targetctx, func, args, return_type, flags, locals, library, pipeline_class)
    566     pipeline = pipeline_class(typingctx, targetctx, library,
    567                               args, return_type, flags, locals)
--> 568     return pipeline.compile_extra(func)
    569 
    570 

c:\program files\python37\lib\site-packages\numba\core\compiler.py in compile_extra(self, func)
    337         self.state.lifted = ()
    338         self.state.lifted_from = None
--> 339         return self._compile_bytecode()
    340 
    341     def compile_ir(self, func_ir, lifted=(), lifted_from=None):

c:\program files\python37\lib\site-packages\numba\core\compiler.py in _compile_bytecode(self)
    399         """
    400         assert self.state.func_ir is None
--> 401         return self._compile_core()
    402 
    403     def _compile_ir(self):

c:\program files\python37\lib\site-packages\numba\core\compiler.py in _compile_core(self)
    379                 self.state.status.fail_reason = e
    380                 if is_final_pipeline:
--> 381                     raise e
    382         else:
    383             raise CompilerError("All available pipelines exhausted")

c:\program files\python37\lib\site-packages\numba\core\compiler.py in _compile_core(self)
    370             res = None
    371             try:
--> 372                 pm.run(self.state)
    373                 if self.state.cr is not None:
    374                     break

c:\program files\python37\lib\site-packages\numba\core\compiler_machinery.py in run(self, state)
    339                     (self.pipeline_name, pass_desc)
    340                 patched_exception = self._patch_error(msg, e)
--> 341                 raise patched_exception
    342 
    343     def dependency_analysis(self):

c:\program files\python37\lib\site-packages\numba\core\compiler_machinery.py in run(self, state)
    330                 pass_inst = _pass_registry.get(pss).pass_inst
    331                 if isinstance(pass_inst, CompilerPass):
--> 332                     self._runPass(idx, pass_inst, state)
    333                 else:
    334                     raise BaseException("Legacy pass in use")

c:\program files\python37\lib\site-packages\numba\core\compiler_lock.py in _acquire_compile_lock(*args, **kwargs)
     30         def _acquire_compile_lock(*args, **kwargs):
     31             with self:
---> 32                 return func(*args, **kwargs)
     33         return _acquire_compile_lock
     34 

c:\program files\python37\lib\site-packages\numba\core\compiler_machinery.py in _runPass(self, index, pss, internal_state)
    289             mutated |= check(pss.run_initialization, internal_state)
    290         with SimpleTimer() as pass_time:
--> 291             mutated |= check(pss.run_pass, internal_state)
    292         with SimpleTimer() as finalize_time:
    293             mutated |= check(pss.run_finalizer, internal_state)

c:\program files\python37\lib\site-packages\numba\core\compiler_machinery.py in check(func, compiler_state)
    262 
    263         def check(func, compiler_state):
--> 264             mangled = func(compiler_state)
    265             if mangled not in (True, False):
    266                 msg = ("CompilerPass implementations should return True/False. "

c:\program files\python37\lib\site-packages\numba\core\typed_passes.py in run_pass(self, state)
    440 
    441         # TODO: Pull this out into the pipeline
--> 442         NativeLowering().run_pass(state)
    443         lowered = state['cr']
    444         signature = typing.signature(state.return_type, *state.args)

c:\program files\python37\lib\site-packages\numba\core\typed_passes.py in run_pass(self, state)
    367             with targetctx.push_code_library(library):
    368                 lower = lowering.Lower(targetctx, library, fndesc, interp,
--> 369                                        metadata=metadata)
    370                 lower.lower()
    371                 if not flags.no_cpython_wrapper:

c:\program files\python37\lib\site-packages\numba\core\lowering.py in __init__(self, context, library, fndesc, func_ir, metadata)
     78         # Python execution environment (will be available to the compiled
     79         # function).
---> 80         self.env = Environment.from_fndesc(self.fndesc)
     81 
     82         # Internal states

c:\program files\python37\lib\site-packages\numba\core\lowering.py in from_fndesc(cls, fndesc)
     30             return cls._memo[fndesc.env_name]
     31         except KeyError:
---> 32             inst = cls(fndesc.lookup_globals())
     33             inst.env_name = fndesc.env_name
     34             cls._memo[fndesc.env_name] = inst

c:\program files\python37\lib\site-packages\numba\core\funcdesc.py in lookup_globals(self)
     79         dynamically (i.e. exec)
     80         """
---> 81         return self.global_dict or self.lookup_module().__dict__
     82 
     83     def lookup_module(self):

c:\program files\python37\lib\site-packages\numba\core\funcdesc.py in lookup_module(self)
     90             return _dynamic_module
     91         else:
---> 92             return sys.modules[self.modname]
     93 
     94     def lookup_function(self):

KeyError: "Failed in nopython mode pipeline (step: nopython mode backend)\n'numba_test'"

question

All 3 comments

I reformatted the original description.

For future reference, please use triple backtick "```" to quote source code and terminal output. see https://guides.github.com/features/mastering-markdown/

@pro-quant, it failed because the custom importing has not insert the module into sys.modules before running the module:

import sys
import importlib.util
spec = importlib.util.spec_from_file_location('numba_test', '/Users/siu/dev/numba/numba_test.py')
foo = importlib.util.module_from_spec(spec)
sys.modules[foo.__name__] = foo       # <--- this is needed
spec.loader.exec_module(foo)
test = foo.Test()
test.run()

Closing this issue as it seems to be resolved. If this is not the case please re-open with a comment about any item that appears to be unresolved. Many thanks.

Was this page helpful?
0 / 5 - 0 ratings