numba ahead of time compilation of recursive function












3














I am trying to compile a recursive function ahead of time. As a MCVE, let's take the following function:



#import numba as nb
from numba.pycc import CC
cc = CC('precompiled')

#@nb.njit
@cc.export('gsum', 'int64(int64)')
def gsum(n):
if n>1:
return n+gsum(n-1)
else:
return 1

if __name__== '__main__':
## print(gsum(5))
cc.compile()


If I run this code, I get the following error trace:



Traceback (most recent call last):
File "numba_ahead.py", line 17, in <module>
cc.compile()
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/pycc/cc.py", line 212, in compile
objects, dll_exports = self._compile_object_files(build_dir)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/pycc/cc.py", line 200, in _compile_object_files
compiler.write_native_object(temp_obj, wrap=True)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/pycc/compiler.py", line 198, in write_native_object
library = self._cull_exports()
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/pycc/compiler.py", line 157, in _cull_exports
locals={}, library=library)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 904, in compile_extra
return pipeline.compile_extra(func)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 367, in compile_extra
return self._compile_bytecode()
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 835, in _compile_bytecode
return self._compile_core()
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 822, in _compile_core
res = pm.run(self.status)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 253, in run
raise patched_exception
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 244, in run
stage()
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 477, in stage_nopython_frontend
self.locals)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 1005, in type_inference_stage
infer.build_constraint()
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 816, in build_constraint
self.constrain_statement(inst)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 1016, in constrain_statement
self.typeof_assign(inst)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 1079, in typeof_assign
self.typeof_global(inst, inst.target, value)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 1177, in typeof_global
typ = self.resolve_value_type(inst, gvar.value)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 1100, in resolve_value_type
raise TypingError(msg, loc=inst.loc)
numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Untyped global name 'gsum': cannot determine Numba type of <class 'function'>

File "numba_ahead.py", line 9:
def gsum(n):
<source elided>
if n>1:
return n+gsum(n-1)
^


So apparently cc.export doesn't know the type of the function it is compiling, if that function calls itself. Is there any way to fix the problem? When I compile the same code just in time with njit (commented-out lines) the code compiles just fine.










share|improve this question



























    3














    I am trying to compile a recursive function ahead of time. As a MCVE, let's take the following function:



    #import numba as nb
    from numba.pycc import CC
    cc = CC('precompiled')

    #@nb.njit
    @cc.export('gsum', 'int64(int64)')
    def gsum(n):
    if n>1:
    return n+gsum(n-1)
    else:
    return 1

    if __name__== '__main__':
    ## print(gsum(5))
    cc.compile()


    If I run this code, I get the following error trace:



    Traceback (most recent call last):
    File "numba_ahead.py", line 17, in <module>
    cc.compile()
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
    return func(*args, **kwargs)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/pycc/cc.py", line 212, in compile
    objects, dll_exports = self._compile_object_files(build_dir)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
    return func(*args, **kwargs)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/pycc/cc.py", line 200, in _compile_object_files
    compiler.write_native_object(temp_obj, wrap=True)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/pycc/compiler.py", line 198, in write_native_object
    library = self._cull_exports()
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
    return func(*args, **kwargs)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/pycc/compiler.py", line 157, in _cull_exports
    locals={}, library=library)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 904, in compile_extra
    return pipeline.compile_extra(func)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 367, in compile_extra
    return self._compile_bytecode()
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 835, in _compile_bytecode
    return self._compile_core()
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 822, in _compile_core
    res = pm.run(self.status)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
    return func(*args, **kwargs)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 253, in run
    raise patched_exception
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 244, in run
    stage()
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 477, in stage_nopython_frontend
    self.locals)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 1005, in type_inference_stage
    infer.build_constraint()
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 816, in build_constraint
    self.constrain_statement(inst)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 1016, in constrain_statement
    self.typeof_assign(inst)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 1079, in typeof_assign
    self.typeof_global(inst, inst.target, value)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 1177, in typeof_global
    typ = self.resolve_value_type(inst, gvar.value)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 1100, in resolve_value_type
    raise TypingError(msg, loc=inst.loc)
    numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
    Untyped global name 'gsum': cannot determine Numba type of <class 'function'>

    File "numba_ahead.py", line 9:
    def gsum(n):
    <source elided>
    if n>1:
    return n+gsum(n-1)
    ^


    So apparently cc.export doesn't know the type of the function it is compiling, if that function calls itself. Is there any way to fix the problem? When I compile the same code just in time with njit (commented-out lines) the code compiles just fine.










    share|improve this question

























      3












      3








      3







      I am trying to compile a recursive function ahead of time. As a MCVE, let's take the following function:



      #import numba as nb
      from numba.pycc import CC
      cc = CC('precompiled')

      #@nb.njit
      @cc.export('gsum', 'int64(int64)')
      def gsum(n):
      if n>1:
      return n+gsum(n-1)
      else:
      return 1

      if __name__== '__main__':
      ## print(gsum(5))
      cc.compile()


      If I run this code, I get the following error trace:



      Traceback (most recent call last):
      File "numba_ahead.py", line 17, in <module>
      cc.compile()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
      return func(*args, **kwargs)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/pycc/cc.py", line 212, in compile
      objects, dll_exports = self._compile_object_files(build_dir)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
      return func(*args, **kwargs)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/pycc/cc.py", line 200, in _compile_object_files
      compiler.write_native_object(temp_obj, wrap=True)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/pycc/compiler.py", line 198, in write_native_object
      library = self._cull_exports()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
      return func(*args, **kwargs)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/pycc/compiler.py", line 157, in _cull_exports
      locals={}, library=library)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 904, in compile_extra
      return pipeline.compile_extra(func)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 367, in compile_extra
      return self._compile_bytecode()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 835, in _compile_bytecode
      return self._compile_core()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 822, in _compile_core
      res = pm.run(self.status)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
      return func(*args, **kwargs)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 253, in run
      raise patched_exception
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 244, in run
      stage()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 477, in stage_nopython_frontend
      self.locals)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 1005, in type_inference_stage
      infer.build_constraint()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 816, in build_constraint
      self.constrain_statement(inst)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 1016, in constrain_statement
      self.typeof_assign(inst)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 1079, in typeof_assign
      self.typeof_global(inst, inst.target, value)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 1177, in typeof_global
      typ = self.resolve_value_type(inst, gvar.value)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 1100, in resolve_value_type
      raise TypingError(msg, loc=inst.loc)
      numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
      Untyped global name 'gsum': cannot determine Numba type of <class 'function'>

      File "numba_ahead.py", line 9:
      def gsum(n):
      <source elided>
      if n>1:
      return n+gsum(n-1)
      ^


      So apparently cc.export doesn't know the type of the function it is compiling, if that function calls itself. Is there any way to fix the problem? When I compile the same code just in time with njit (commented-out lines) the code compiles just fine.










      share|improve this question













      I am trying to compile a recursive function ahead of time. As a MCVE, let's take the following function:



      #import numba as nb
      from numba.pycc import CC
      cc = CC('precompiled')

      #@nb.njit
      @cc.export('gsum', 'int64(int64)')
      def gsum(n):
      if n>1:
      return n+gsum(n-1)
      else:
      return 1

      if __name__== '__main__':
      ## print(gsum(5))
      cc.compile()


      If I run this code, I get the following error trace:



      Traceback (most recent call last):
      File "numba_ahead.py", line 17, in <module>
      cc.compile()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
      return func(*args, **kwargs)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/pycc/cc.py", line 212, in compile
      objects, dll_exports = self._compile_object_files(build_dir)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
      return func(*args, **kwargs)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/pycc/cc.py", line 200, in _compile_object_files
      compiler.write_native_object(temp_obj, wrap=True)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/pycc/compiler.py", line 198, in write_native_object
      library = self._cull_exports()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
      return func(*args, **kwargs)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/pycc/compiler.py", line 157, in _cull_exports
      locals={}, library=library)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 904, in compile_extra
      return pipeline.compile_extra(func)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 367, in compile_extra
      return self._compile_bytecode()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 835, in _compile_bytecode
      return self._compile_core()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 822, in _compile_core
      res = pm.run(self.status)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
      return func(*args, **kwargs)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 253, in run
      raise patched_exception
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 244, in run
      stage()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 477, in stage_nopython_frontend
      self.locals)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/compiler.py", line 1005, in type_inference_stage
      infer.build_constraint()
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 816, in build_constraint
      self.constrain_statement(inst)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 1016, in constrain_statement
      self.typeof_assign(inst)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 1079, in typeof_assign
      self.typeof_global(inst, inst.target, value)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 1177, in typeof_global
      typ = self.resolve_value_type(inst, gvar.value)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/typeinfer.py", line 1100, in resolve_value_type
      raise TypingError(msg, loc=inst.loc)
      numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
      Untyped global name 'gsum': cannot determine Numba type of <class 'function'>

      File "numba_ahead.py", line 9:
      def gsum(n):
      <source elided>
      if n>1:
      return n+gsum(n-1)
      ^


      So apparently cc.export doesn't know the type of the function it is compiling, if that function calls itself. Is there any way to fix the problem? When I compile the same code just in time with njit (commented-out lines) the code compiles just fine.







      python recursion numba






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 at 7:29









      Thomas Kühn

      5,20621837




      5,20621837





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442331%2fnumba-ahead-of-time-compilation-of-recursive-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442331%2fnumba-ahead-of-time-compilation-of-recursive-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks