Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How make a python class jitclass compatible when it contains itself jitclass classes? #4940

Closed
ymmx2 opened this issue Dec 10, 2019 · 4 comments
Labels
jitclass question Notes an issue as a question stale Marker label for stale issues.

Comments

@ymmx2
Copy link

ymmx2 commented Dec 10, 2019

I'm trying to make a class that could be a part of jitclass but has some attribute that are themself jitclass objects.

for example, if I have two class with the decorator @jitclass, I would like instanced those in a third class (combined).

import numpy as np
from numba import jitclass
from numba import boolean, int32, float64,uint8

spec = [
    ('type' ,int32),
    ('val' ,float64[:]),
    ('result',float64)]

@jitclass(spec)
class First:
    def __init__(self):
        self.type = 1
        self.val = np.ones(100)
        self.result = 0.
    def sum(self):
        self.result = np.sum(self.val)

@jitclass(spec)
class Second:
    def __init__(self):
        self.type = 2
        self.val = np.ones(100)
        self.result = 0.
    def sum(self):
        self.result = np.sum(self.val)



@jitclass(spec)
class Combined:
    def __init__(self):
        self.List = []
        for i in range(10):
            self.List.append(First())
            self.List.append(Second())

    def sum(self):
        for i, c in enumerate(self.List):
            c.sum()
    def getresult(self):
        result = []
        for i, c in enumerate(self.List):
            result.append(c.result)
        return result


C = Combined()
C.sum()
result = C.getresult()
print(result)

In that example I get an error because numba cannot determine the type of self.List which is a combination of the two jitclasses.
How can I make the class Combined be jitclass compatible?

update

It tried Something I found elsewhere:

import numpy as np
from numba import jitclass, deferred_type
from numba import boolean, int32, float64,uint8
from numba.typed import List

spec = [
    ('type' ,int32),
    ('val' ,float64[:]),
    ('result',float64)]

@jitclass(spec)
class First:
    def __init__(self):
        self.type = 1
        self.val = np.ones(100)
        self.result = 0.
    def sum(self):
        self.result = np.sum(self.val)
 

 
spec1 = [('ListA',List.empty_list(First.class_type.instance_type)  )]

@jitclass(spec1)
class Combined:
    def __init__(self):
        self.ListA = [First(),First()] 

    def sum(self):
        for i, c in enumerate(self.ListA):
            c.sum()
    def getresult(self):
        result = []
        for i, c in enumerate(self.ListA):
            result.append(c.result)
        return result


C = Combined()
C.sum()
result = C.getresult()
print(result)

but I get this error

TypeError: spec values should be Numba type instances, got ListType[instance.jitclass.First#20b0cbcf5f8<type:int32,val:array(float64, 1d, A),result:float64>]([])
@github-actions
Copy link

github-actions bot commented Jun 3, 2021

This issue is marked as stale as it has had no activity in the past 30 days. Please close this issue if no further response or action is needed. Otherwise, please respond with any updates and confirm that this issue still needs to be addressed.

@github-actions github-actions bot added the stale Marker label for stale issues. label Jun 3, 2021
@gmarkall
Copy link
Member

gmarkall commented Jun 3, 2021

@ymmx2 apologies for the long delay in the response here. The problem is that List gives an instance of the list, not its type. You can instead use a ListType for the spec, like:

import numpy as np
from numba.experimental import jitclass
from numba import int32, float64
from numba.typed import List
from numba.types import ListType

spec = [("type", int32), ("val", float64[:]), ("result", float64)]


@jitclass(spec)
class First:
    def __init__(self):
        self.type = 1
        self.val = np.ones(100)
        self.result = 0.0

    def sum(self):
        self.result = np.sum(self.val)


spec1 = [("ListA", ListType(First.class_type.instance_type))]


@jitclass(spec1)
class Combined:
    def __init__(self):
        self.ListA = List((First(), First()))

    def sum(self):
        for i, c in enumerate(self.ListA):
            c.sum()

    def getresult(self):
        result = []
        for i, c in enumerate(self.ListA):
            result.append(c.result)
        return result


C = Combined()
C.sum()
result = C.getresult()
print(result)

which prints:

$ python repro.py 
[100.0, 100.0]

@gmarkall gmarkall added question Notes an issue as a question and removed needtriage labels Jun 3, 2021
@github-actions github-actions bot removed the stale Marker label for stale issues. label Jun 4, 2021
@github-actions
Copy link

github-actions bot commented Jul 5, 2021

This issue is marked as stale as it has had no activity in the past 30 days. Please close this issue if no further response or action is needed. Otherwise, please respond with any updates and confirm that this issue still needs to be addressed.

@github-actions github-actions bot added the stale Marker label for stale issues. label Jul 5, 2021
@gmarkall
Copy link
Member

gmarkall commented Jul 5, 2021

Closing this issue as no further feedback has been received.

@gmarkall gmarkall closed this as completed Jul 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
jitclass question Notes an issue as a question stale Marker label for stale issues.
Projects
None yet
Development

No branches or pull requests

3 participants