-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Constant-folding pass needed to permit more "static"* expression rewrites. #2518
Comments
For anyone finding this bug from an internet search: this article may prove helpful for writing a workaround. |
Having the same issue (python 2.7, numba==0.37.0): import numpy as np
import numba as nb
@nb.njit()
def fixed():
return np.zeros((3,), dtype=np.float64)
@nb.njit()
def dynamic():
a = np.array([1,2,3])
return np.zeros(a, dtype=np.float64)
print fixed() Gives:
while print dynamic() Fails with:
any thoughts? |
@bmerry Thanks for the report. This can be done with from numba import generated_jit
import numpy as np
@generated_jit
def f(a, b):
A = b.ndim
B = A + 1
def specialize(a, b):
new_shape = a.shape[:A] + a.shape[B:]
return np.ones(new_shape)
return specialize
# Should return array with shape (5, 4, 2)
print(f(np.zeros((5, 4, 3, 2)), np.zeros((1, 1))).shape) |
@rk-roman I'm not sure what the intention is in your example? It seems like you want to dynamically create an array based on the values of another array, but that array is also static? Is this what you really want to do or in reality is the |
Thanks, I don't think I'd tried lifting the expressions from from numba import generated_jit
import numpy as np
@generated_jit
def f(a, b):
A = b.ndim
def specialize(a, b):
new_shape = a.shape[:A] + a.shape[A + 1:]
return np.ones(new_shape)
return specialize
# Should return array with shape (5, 4, 2)
print(f(np.zeros((5, 4, 3, 2)), np.zeros((1, 1))).shape) (i.e. replace |
@bmerry no problem. FWIW right now there's a pass that propagates constants from the typing domain like from numba import generated_jit
import numpy as np
@generated_jit(nopython=True)
def f(a, b):
def specialize(a, b):
return a.shape[:b.ndim] # <-- b.ndim get's rewritten to "2" but it's too late as the generic const-expr rewrite has already happened
return specialize
print(f(np.zeros((5, 4, 3, 2)), np.zeros((1, 1)))) More generally I think what you are asking for is compile time constant folding, something which is not yet implemented. I think a prerequisite for this is constant propagation and a prerequisite for that is having Numba's IR in SSA form, SSA is in progress! RE documentation, pull requests are welcomed. Thanks! |
Ah, that makes more sense now. I was rather perplexed that Is the ordering bug also responsible for the example in my initial bug report not working?
I was only really expecting constants like
In this case I don't have a solid understanding of what does and doesn't work, which makes it tricky to document. Also, if this is a quick bug to fix then there probably isn't too much point documenting the current behaviour. |
ndim
from the type system
ndim
from the type system
Yes and no. Yes in that
Constant-ness is tracked through assignment, and there's some degree of propagation via type inference. For example: from numba import njit
import numpy as np
@njit
def foo(x):
a = x.ndim # const
b = a + 1 # not const
c = a # tracked + propagated as it's a literal
return a + b + c
foo(np.zeros((4,3,2,1)))
foo.inspect_types() gives:
No problem. I think the use of |
I'm trying to write some generic code which I want to compile with numba several times, providing different values for a constant each time (constant for each compilation, similar to an
int
template parameter in C++. In particular, numba only allows constant slices for tuples, and I'd like to use a different slice in each compilation.I've tried several approaches so far.
With numba 0.34, numpy 1.13.1, Python 2.7.12, Ubuntu 16.04 I get this error:
This also fails:
generated_jit
, where the constant n is encoded as an n-dimensional array and the dispatcher function extracts it and uses it in the returned closure. Also fails.The text was updated successfully, but these errors were encountered: