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

Invalid use of Function(<built-in function setitem>) #5521

Closed
2 tasks
HannoHennighausen opened this issue Apr 7, 2020 · 10 comments
Closed
2 tasks

Invalid use of Function(<built-in function setitem>) #5521

HannoHennighausen opened this issue Apr 7, 2020 · 10 comments
Labels
no action required No action was needed to resolve. question Notes an issue as a question

Comments

@HannoHennighausen
Copy link

Reporting a bug

@HannoHennighausen HannoHennighausen changed the title Hey guys, Invalid use of Function(<built-in function setitem>) Apr 7, 2020
@HannoHennighausen
Copy link
Author

Hey guys,

the following code is supposed to solve a diffusion equation for a specialized 3 part system which I
won't get into too much detail. broken down my code reads:


a = np.array(Nx)
apl = np.zeros((Nt,Nx))
@jit(nopython=True)
def functza(apl, a):
  for t in range (1, Nt):
    a+= (delta_t/(delta_x**2))*(np.roll(a,1)-2*a + np.roll(a,-1))
   
    if (t%1000 == 0) or (t==1):
            apl[m] = a 
            m += 1
return apl

error:

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<built-in function setitem>) with argument(s) of type(s): (array(float64, 2d, C), float64, array(float64, 1d, C))
 * parameterized
In definition 0:
    All templates rejected with literals.
In definition 1:
    All templates rejected without literals.
In definition 2:
    All templates rejected with literals.
In definition 3:
    All templates rejected without literals.
In definition 4:
    All templates rejected with literals.
In definition 5:
    All templates rejected without literals.
In definition 6:
    All templates rejected with literals.
In definition 7:
    All templates rejected without literals.
In definition 8:
    TypeError: unsupported array index type float64 in [float64]
    raised from C:\Users\hanno\anaconda3\lib\site-packages\numba\typing\arraydecl.py:71
In definition 9:
    TypeError: unsupported array index type float64 in [float64]
    raised from C:\Users\hanno\anaconda3\lib\site-packages\numba\typing\arraydecl.py:71
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: typing of setitem at <ipython-input-6-e302eb532e0b> (69)

File "<ipython-input-6-e302eb532e0b>", line 69:
def functza(apl,bpl,cpl, a, b, c,alpha,beta, cl,cr, kappa):
    <source elided>
        if (t%1000 == 0) or (t==1):
            apl[m] = a

any help, comments or tips are greatly appreciated ?!
Thank You guys very much!

@gmarkall
Copy link
Member

gmarkall commented Apr 7, 2020

Thanks for the question - do you have a self-contained executable reproducer of the issue? How are you calling functza?

@gmarkall gmarkall added question Notes an issue as a question more info needed This issue needs more information labels Apr 7, 2020
@gmarkall
Copy link
Member

gmarkall commented Apr 7, 2020

It somehow looks like you are indexing into the array with a float rather than an integer - this may be because Numba somehow typed it as a floating point value, or because it already is one - it's difficult to tell without knowing more about the context in which the function is being called.

@HannoHennighausen
Copy link
Author

Thank you so much for the quick reply.
Let me double-check the float vs int issue and then post an executable reproducer if needed.
Sorry about that

@HannoHennighausen
Copy link
Author

HannoHennighausen commented Apr 7, 2020

It somehow looks like you are indexing into the array with a float rather than an integer - this may be because Numba somehow typed it as a floating point value, or because it already is one - it's difficult to tell without knowing more about the context in which the function is being called.

So I fixed my initial float error. Thank you for that. I now opened a new but very similar error which shows up only two lines after my initial error and looks like this:

import numba
from numba import jit
import numpy as np


delta_t = 0.00001
delta_x = 0.02
Nx = int(10/delta_x)
Nt = int(100/ delta_t)
nt = int(Nt/1000)

qpl = np.zeros((nt,Nx))
q = np.full(Nx,1.0)
q4 = np.zeros(4)
mL = np.zeros(nt)
alpha, beta = 0.4,0.4
ql=1.0

@jit(nopython=True)
def mcell(q4,j):
    return delta_x*(q4[j])

@jit(nopython=True)
def Jmerlin(ql,alpha,beta,x):
    return ((alpha*ql)-beta*mcell(q4,x))


@jit(nopython=True)
def functza(qpl, q):
    m = 0
    for t in range (1, Nt):
        q4 = [q[0],q[1],q[Nx-2],q[Nx-1]]
        q += (delta_t/(delta_x**2))*(np.roll(q,1)-2*q + np.roll(q,-1))
        
        q[0] = q4[0] + (delta_t/(delta_x**2))*(2*q4[1]+2*delta_x*Jmerlin(ql,alpha,beta,0)- 2*q4[0]) 
        q[Nx-1] = q4[3] + (delta_t/(delta_x**2))*(2*q4[2]-2*q4[3])
        if (t%1000 == 0) or (t==1):
            qpl[m] = q
            mL[m] = ql-(Jmerlin(ql,alpha,beta,0)*delta_t)
            m += 1
    return qpl,mL

X,Y = functza(qpl,q)

error

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<built-in function setitem>) with argument(s) of type(s): (readonly array(float64, 1d, C), int64, float64)
 * parameterized
In definition 0:
    All templates rejected with literals.
In definition 1:
    All templates rejected without literals.
In definition 2:
    All templates rejected with literals.
In definition 3:
    All templates rejected without literals.
In definition 4:
    All templates rejected with literals.
In definition 5:
    All templates rejected without literals.
In definition 6:
    All templates rejected with literals.
In definition 7:
    All templates rejected without literals.
In definition 8:
    TypeError: Cannot modify value of type readonly array(float64, 1d, C)
    raised from C:\Users\hanno\anaconda3\lib\site-packages\numba\typing\arraydecl.py:179
In definition 9:
    TypeError: Cannot modify value of type readonly array(float64, 1d, C)
    raised from C:\Users\hanno\anaconda3\lib\site-packages\numba\typing\arraydecl.py:179
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: typing of setitem at <ipython-input-6-69d1f805c4f8> (39)

File "<ipython-input-6-69d1f805c4f8>", line 39:
def functza(qpl, q):
    <source elided>
            qpl[m] = q
            mL[m] = ql-(Jmerlin(ql,alpha,beta,0)*delta_t)
            ^

I thought that by giving my if condition an int all of the other conditions would be fixed as well. just that this one condition now wants something else.
Thank you for your help and I hope that the now provided code is executable.

@gmarkall
Copy link
Member

gmarkall commented Apr 7, 2020

The problem here is that Numba treats module-level globals as constants, so you can't write to mL[m] - if you make mL an argument and pass it in, that should resolve this problem.

@gmarkall
Copy link
Member

gmarkall commented Apr 7, 2020

BTW - the section of the docs mentioning the behaviour you're observing is: http://numba.pydata.org/numba-doc/latest/reference/pysemantics.html?highlight=global#global-and-closure-variables

@HannoHennighausen
Copy link
Author

Thank you so much! This is my first time working with numba and I am slowly figuring this out. But it works amazing. Great advice! I will close this issue now.

The problem here is that Numba treats module-level globals as constants, so you can't write to mL[m] - if you make mL an argument and pass it in, that should resolve this problem.

@stuartarchibald stuartarchibald added no action required No action was needed to resolve. and removed more info needed This issue needs more information labels Apr 7, 2020
@vantainguyen
Copy link

can you show us the latest code?

@EvagelineFEI
Copy link

EvagelineFEI commented Apr 4, 2023

I also encountered a bug similar to this recently, but I'm using numba.typed.List , and it seems that there are anything wrong with "append"?

`def countVgij(matrix_g):
    proc=List()
    for i in range(t):
      m_gbij = np.sum(matrix_g[i], axis=1)
      proc.append(m_gbij)
    proc=np.array(proc)
    proc/=L

    matrix_vgij=np.zeros((n,n))
    for i in range(n):
       for j in range(n):
           inlinesum=0
           for l in range(L):
              inlinesum+=(matrix_g[i][j][l]-proc[i][j])**2
              Var2t=countVar2t(t)

           inlinesum=inlinesum/(L*Var2t)
           matrix_vgij[i][j]=proc[i][j]-inlinesum

    return matrix_vgij`

error
`Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function() with argument(s) of type(s): (ListType[array(float64, 1d, C)])

parameterized
In definition 0:
NotImplementedError: ListType[array(float64, 1d, C)] cannot be represented as a Numpy dtype
raised from D:\anaconda\lib\site-packages\numba\numpy_support.py:152
In definition 1:
NotImplementedError: ListType[array(float64, 1d, C)] cannot be represented as a Numpy dtype
raised from D:\anaconda\lib\site-packages\numba\numpy_support.py:152
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: Function()
[2] During: typing of call at D:\ILOVE_CODING\pythonProject\codeforresearch _speedup\clculateRgroup.py (35)
File "clculateRgroup.py", line 35:
def countVgij(matrix_g):

proc.append(m_gbij)
proc=np.array(proc)
^

[1] During: resolving callee type: type(CPUDispatcher(<function countVgij at 0x0000022FF1901438>))
[2] During: typing of call at D:\ILOVE_CODING\pythonProject\codeforresearch _speedup\clculateRgroup.py (104)

[3] During: resolving callee type: type(CPUDispatcher(<function countVgij at 0x0000022FF1901438>))
[4] During: typing of call at D:\ILOVE_CODING\pythonProject\codeforresearch _speedup\clculateRgroup.py (104)
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no action required No action was needed to resolve. question Notes an issue as a question
Projects
None yet
Development

No branches or pull requests

5 participants