Public channel for discussing Numba usage. Don't post confidential info here! Consider posting questions to: https://numba.discourse.group/ !
nditer
currently be used to implement a version of broadcast_to
?
I have a fn that will not compile. But have checked the dtypes for where it fails and they are correct when not using @njit
. Here is the fn.
@njit(float64[:](int64, float64[:], float64[:], float64[:], float64[:], int64, float64[:], float64[:]))
def _density_donoho(j, c_coef, d_coef, x_array, y_array, len_signal, t, phi_t):
"""
Compute the signal's density (Linear estimator)
:param j: scaling parameter [int]
:param c_coef: scaling coefficients C [1D array]
:param x_array: density index [1D array]
:return: density values [1D array]
"""
k_lim_ = int(2. ** -j * len_signal)
z_array = np.empty(shape=(len(x_array), len(y_array)))
cpt = 0.
perc = 0.
for i in prange(0, len(x_array)):
for ii in prange(0, len(y_array)):
sum_ = 0.
for k in prange(-k_lim_, k_lim_):
for k2 in prange(-k_lim_, k_lim_):
p1 = _derived_father_wavelet(x_array[i], j, k, t, phi_t)
p2 = _derived_father_wavelet(y_array[ii], j, k2, t, phi_t)
print(k, k2, k_lim_)
cc = c_coef[k + k_lim_][k2 + k_lim_] # line for error below
sum_ = sum_ + cc * p1 * p2
The error relates to the 2nd last line above. And shows, I think, that the first array index is represented as a float64
, but when not in @njit
mode it shows as a int64
>>> getitem(float64, int64)
There are 22 candidate implementations:
- Of which 22 did not match due to:
Overload of function 'getitem': File: <numerous>: Line N/A.
With argument(s): '(float64, int64)':
No match.
Can numba change the type of the argument?
@njit(parallel=True)
def gen_ddt():
ddt = [[0 for _ in range(65536)] for _ in range(65536)]
sbox = [s16(i) for i in range(0,65536)]
print('done sbox')
for di in prange(1000):
for do in prange(1000):
ddt[di ^ do][sbox[di] ^ sbox[do]] += 1
return ddt
def gen_ddt_single():
ddt = [[0 for _ in range(65536)] for _ in range(65536)]
sbox = [s16(i) for i in range(0,65536)]
print('done sbox')
for di in range(1000):
for do in range(1000):
ddt[di ^ do][sbox[di] ^ sbox[do]] += 1
return ddt
z_array = np.empty(shape=(len(x_array), len(y_array)))
after some processing to fill these arrays up the original code used this z_array[z_array < 0] = 0
to set all <0 values in all arrays to 0. This does not work with numba. It gives this error TypeError: unsupported array index type array(bool, 2d, C) in [array(bool, 2d, C)]
. This is understandable. But is there a better way than looping thru the arrays and using the same expression on each of the sub arrays?
import numpy as np
import numba
from numba import jit,njit
from numba import int32, float32 ,float64 # import the types
from numba.experimental import jitclass
spec = [
('w', float64[:,:]), # a simple scalar field
('b', float64[:,:]), # an array field
]
@jitclass(spec)
class Linear:
def __init__(self,input_shape,output_shape):
self.w = np.ascontiguousarray(
np.random.rand(input_shape,output_shape)
)
self.b = np.ascontiguousarray(
np.random.rand(1,output_shape)
)
def forward(self,x):
return x@self.w+ self.b
l = Linear(4,5)
x = np.random.rand(10,4)
l.forward(x)
<string>:3: NumbaPerformanceWarning: '@' is faster on contiguous arrays, called on (array(float64, 2d, C), array(float64, 2d, A))
<string>:3: NumbaPerformanceWarning: '@' is faster on contiguous arrays, called on (array(float64, 2d, C), array(float64, 2d, A))
float64[:,;]
to float64[:,::1]
does that get rid of the error?
_debug_print=True
in nrtdynmod.py
, the values printed by NRT_Incref
make no sense (e.g. I am getting *** NRT_Incref 393217 [...]
), while NRT_Decref
prints more reasonable values. (e.g. ** NRT_Decref 2 [...]
). Is this normal?
%zu
for a value I believe to be of type signed int. changing the Format to %hhi
does little to solve the problem)
Hi, all. I am a newbie of numba and have been looking chance to contirbute to numba. I was tring to fix issue6949 in the pasted weekend, which is my first try, but i am stuck now and asking for some help.
The result of array inpalce binop(such as +=) is ircorrect when aliases of the lhs in the function expression existed. For example (provided in #6949):
@jit
def self_addition(x):
x += x.T
return x
x = np.array([[1,2],[3,4]])
self_addition(x)
> array([[2, 5],[8, 8]])
In this case, x
will be temporarily changed to [[2,5], [3,4]]
since the first row will be calculated first when matrix addition is performed. Therefore, x.T
will be [[2,3], [5,4]]
(the correct value is [[1,3], [2,4]])
when the second row is calculated. I am still not sure which pass cased this error , does anyone know anything about this?
from numba import cuda
from time import sleep
import numpy as np
@cuda.jit
def report_progress(progress):
for i in range(1000000):
cuda.atomic.inc(progress, 0, 10000000)
cuda.nanosleep(1000)
progress = cuda.managed_array(1, dtype=np.uint64)
progress[0] = 0
report_progress[1, 1](progress)
val = 0
count = 0
while val < 1000000:
sleep(0.001)
val = progress[0]
print(f"{count}: {val}")
count += 1
Gives
0: 369
1: 1119
2: 1825
...
1251: 998488
1252: 999286
1253: 1000000
for me on RTX 8000