AOT compilation of jitclass

Does jitclass support ahead of time compilation?

Hi AbduKT,

One can create and use a jitclass inside an ahead of time function. See:

# example.py
from numba.pycc import CC
import numpy as np
from numba.core import types
from numba.experimental import jitclass

cc = CC('my_module')

spec = [
    ('array', types.int32[:])
]

@jitclass(spec)
class Foo(object):
    def __init__(self, value):
        self.array = np.arange(value, dtype=np.int32)

@cc.export('get_sum', 'int32(int32)')
def get_sum(n):
    myFoo = Foo(n)
    return myFoo.array.sum()

if __name__ == '__main__':
    cc.compile()
$ python example.py  # will produce my_module.so
$ python -c 'import my_module; print(my_module.get_sum(4))  # print "6"`

But it doesn’t seem to be possible to pass a jitclass as an argument:

# example2
from numba.pycc import CC
import numpy as np
from numba.core import types
from numba.experimental import jitclass

cc = CC('my_module')

spec = [
    ('array', types.int32[:])
]

@jitclass(spec)
class Foo(object):
    def __init__(self, value):
        self.array = np.arange(value, dtype=np.int32)

@cc.export('get_sum', 'int32(Foo)')
def get_sum(x):
    return x.array.sum()

if __name__ == '__main__':
    cc.compile()
$ python example2.py
Traceback (most recent call last):
  File "example2.py", line 18, in <module>
    @cc.export('get_first', 'int32(Foo)')
  File "/home/guilhermeleobas/git/numba/numba/pycc/cc.py", line 136, in export
    fn_args, fn_retty = sigutils.normalize_signature(sig)
  File "/home/guilhermeleobas/git/numba/numba/core/sigutils.py", line 29, in normalize_signature
    parsed = _parse_signature_string(sig)
  File "/home/guilhermeleobas/git/numba/numba/core/sigutils.py", line 19, in _parse_signature_string
    return eval(signature_str, {}, types.__dict__)
  File "<string>", line 1, in <module>
NameError: name 'Foo' is not defined
(numba) 

Numba crashes when it tries to parse the signature.

1 Like

As I understand it, functions with jitclass arguments cannot be cached as each jitclass instance ends up as a different type.

Prefer structuref, which has a bit more boilerplate and a steeper learning curve but works well with caching and AOT compilation.

1 Like

@nelson2005 I’ve been facing the similar issue and would like to find out structuref but failed. Would you share any relevant link for that? I’d like to try.

Hi @wsjeon! I’m not sure what you’re asking.

The structref docs are here.

An in-depth discussion with the OG of structref @DannyWeitekamp is here

If those links don’t help, perhaps you could paste some minimal code of what you’re trying to do?

1 Like

Hi, @nelson2005. I meant I couldn’t find out the documentation for StructRef (somehow, with two keywords “numba” and “structref”, the documentation couldn’t be found at that time), but I found it after asking this question and understood how to use it :slight_smile: Thanks for your help!

Now, I found that my code works well with StructRef with JIT Compilation and try to AOT compile my code with StructRef. Is there any example code I can use for this case?