def test_get_index_fn_0 (self): arr = numpy.ones((4,4,4), dtype=numpy.double) arr[1,2,3] = 0. compiled_fn = jit(restype=double, argtypes=[double[:, :, ::1]], backend='bytecode')(get_index_fn_0) self.assertEqual(compiled_fn(arr), 0.)
def test_vectorized_sum2d(self): usum2d = jit(arg_types=[double[:,:]], ret_type=double)(sum2d) image = numpy.random.rand(10, 10) plain_old_result = sum2d(image) hot_new_result = usum2d(image) self.assertTrue((abs(plain_old_result - hot_new_result) < 1e-9).all())
def test_set_index_fn_0 (self): arr = numpy.ones((4,4,4)) compiled_fn = jit(argtypes=[double[:,:,::1]], backend='bytecode')(set_index_fn_0) self.assertEqual(arr[1,2,3], 1.) compiled_fn(arr) self.assertEqual(arr[1,2,3], 0.)
def add(self, restype=None, argtypes=None): dec = decorators.jit(restype, argtypes, backend='ast') numba_func = dec(self.pyfunc) self.args_restypes.append(list(numba_func.signature.args) + [numba_func.signature.return_type]) self.signatures.append((restype, argtypes, {})) self.translates.append(numba_func)
def test_getattr_data_1(self): test_data = numpy.array([1., 2., 3.]) compiled_fn = jit('d*(d[:])')(get_ndarray_data) result = compiled_fn(test_data) self.assertEqual(result[0], 1.) self.assertEqual(result[1], 2.) self.assertEqual(result[2], 3.)
def test_compiled_for_loop_fn_0(self): test_data = numpy.array([1, 2, 3], dtype = 'l') compiled_for_loop_fn = jit( arg_types = [['l']])(for_loop_fn_0) result = compiled_for_loop_fn(test_data) self.assertEqual(result, 6) self.assertEqual(result, for_loop_fn_0(testdata))
def __init__(self, py_func, signature, targetoptions={}): self.py_func = py_func self.nb_func = jit(target='npyufunc')(py_func) self.signature = signature self.sin, self.sout = parse_signature(signature) self.targetoptions = targetoptions self._sigs = [] self._cres = {}
def test_vectorized_filter2d(self): ufilter2d = jit(argtypes=[double[:, :], double[:, :]], restype=double[:, :])(filter2d) image = numpy.random.random((50, 50)) filt = numpy.random.random((5, 5)) filt /= filt.sum() plain_old_result = filter2d(image, filt) hot_new_result = ufilter2d(image, filt) self.assertTrue((abs(plain_old_result - hot_new_result) < 1e-9).all())
def test_getattr_shape_2_unpack(self): compiler_fn = jit('i%d(d[:,:])' % (_plat_bits // 8)) dim0_fn, dim1_fn = (compiler_fn(fn) for fn in (get_ndarray_2_shape_unpack_0, get_ndarray_2_shape_unpack_1)) test_data2 = numpy.array([[1., 2., 3.], [4., 5., 6.]]) self.assertEqual(dim0_fn(test_data2), 2) self.assertEqual(dim1_fn(test_data2), 3)
def main (*args, **kws): compiled_demo_function = jit( argtypes = ['d', 'd', 'd', [[['d']]]])(demo_function) control_arr = numpy.zeros((5, 5, 2)) demo_function(-1., 1., -1., control_arr) test_arr = numpy.zeros_like(control_arr) compiled_demo_function(-1., 1., -1., test_arr) assert (numpy.abs(control_arr - test_arr) < 1e9).all()
def test_set_index_fn_1 (self): control_arr = numpy.zeros((50, 50, 2), dtype=numpy.double) test_arr = numpy.zeros_like(control_arr) set_index_fn_1(-1., 1., -1., control_arr) argtypes = double, double, double, double[:,:,:] compiled_fn = jit(argtypes=argtypes)(set_index_fn_1) compiled_fn(-1., 1., -1., test_arr) self.assertTrue((numpy.abs(control_arr - test_arr) < 1e9).all())
def test_set_index_fn_1 (self): control_arr = numpy.zeros((50, 50, 2)) test_arr = numpy.zeros_like(control_arr) set_index_fn_1(-1., 1., -1., control_arr) compiled_fn = jit( arg_types = ['d', 'd', 'd', ['d']])(set_index_fn_1) compiled_fn(-1., 1., -1., test_arr) self.assertTrue((numpy.abs(control_arr - test_arr) < 1e9).all())
def _do_test (self, _avg2d): compiled_fn = jit(arg_types = [d[:,:], d[:]])(_avg2d) test_data = numpy.random.random((5,5)) control_result = numpy.zeros((5,)) test_result = control_result[:] _avg2d(test_data, control_result) compiled_fn(test_data, test_result) self.assertTrue((control_result == test_result).all())
def __init__(self, py_func, signature, identity=None, cache=False, targetoptions={}): self.py_func = py_func self.identity = parse_identity(identity) self.nb_func = jit(target='npyufunc', cache=cache)(py_func) self.signature = signature self.sin, self.sout = parse_signature(signature) self.targetoptions = targetoptions self._sigs = [] self._cres = {}
def test_getattr_data_2(self): test_data = numpy.array([[1., 2., 3.], [4., 5., 6.]]) compiled_fn = jit('d*(d[:,:])')(get_ndarray_data) result = compiled_fn(test_data) self.assertEqual(result[0], 1.) self.assertEqual(result[1], 2.) self.assertEqual(result[2], 3.) self.assertEqual(result[3], 4.) self.assertEqual(result[4], 5.) self.assertEqual(result[5], 6.)
def test_vectorized_fbcorr(self): ufbcorr = jit(argtypes=(nd4type, nd4type, nd4type), backend='bytecode')(fbcorr) imgs = np.random.randn(10, 16, 16, 3) filt = np.random.randn(6, 5, 5, 3) old_output = np.zeros((10, 6, 15, 15)) fbcorr(imgs, filt, old_output) new_output = np.zeros((10, 6, 15, 15)) ufbcorr(imgs, filt, new_output) self.assertTrue((abs(old_output - new_output) < 1e-9).all())
def test_vectorized_fbcorr(self): ufbcorr = jit(arg_types=(nd4type, nd4type, nd4type))(fbcorr) imgs = np.random.randn(10, 64, 64, 3) filt = np.random.randn(6, 5, 5, 3) old_output = np.zeros((10, 6, 60, 60)) fbcorr(imgs, filt, old_output) new_output = np.zeros((10, 6, 60, 60)) ufbcorr(imgs, filt, new_output) self.assertTrue((abs(old_output - new_output) < 1e-9).all())
def test_numba(self): jit_matmulcore = jit((float32[:, :], float32[:, :], float32[:,:]))(matmulcore) A = np.arange(16, dtype=np.float32).reshape(4, 4) B = np.arange(16, dtype=np.float32).reshape(4, 4) C = np.zeros(16, dtype=np.float32).reshape(4, 4) Gold = np.matrix(A) * np.matrix(B) jit_matmulcore(A, B, C) self.assertTrue((C == Gold).all())
def add(self, restype=None, argtypes=None, **kwds): """ Add a specialization to the vectorizer. Pass any keyword arguments to numba.jit(). """ dec = decorators.jit(restype, argtypes, **kwds) numba_func = dec(self.pyfunc) self.args_restypes.append(list(numba_func.signature.args) + [numba_func.signature.return_type]) self.signatures.append((restype, argtypes, {})) self.translates.append(numba_func)
def test_numba(): jit_matmulcore = jit(argtypes=[f4[:,:], f4[:,:], f4[:,:]])(matmulcore) A = np.arange(16, dtype=np.float32).reshape(4, 4) B = np.arange(16, dtype=np.float32).reshape(4, 4) C = np.zeros(16, dtype=np.float32).reshape(4, 4) Gold = np.matrix(A) * np.matrix(B) jit_matmulcore(A, B, C) if (C != Gold).any(): raise ValueError
def test_set_index_fn_2 (self): control_arr = numpy.zeros((10, 10), dtype=numpy.double) test_arr = numpy.zeros_like(control_arr) set_index_fn_2(control_arr) argtypes = double[:, :], compiled_fn = jit(argtypes=argtypes)(set_index_fn_2) compiled_fn(test_arr) self.assertTrue((numpy.abs(control_arr - test_arr) < 1e9).all())
from __future__ import print_function, division, absolute_import from numba import double from numba.decorators import jit as jit #@autojit def sum2d(arr): M, N = arr.shape result = 0.0 for i in range(M): for j in range(N): result += arr[i, j] return result csum2d = jit(restype=double, argtypes=[double[:, :]])(sum2d) from numpy import random arr = random.randn(100, 100) import time start = time.time() res = sum2d(arr) duration = time.time() - start print("Result from python is %s in %s (msec)" % (res, duration * 1000)) start = time.time() res = csum2d(arr) duration2 = time.time() - start
def recontruct4DCore(ps, A, B, P0, dummy): n1, n2, n3, n4 = dummy.shape p = numpy.zeros_like(dummy) for j in range(n2): Afact = A[j] * P0 for i in range(n1): for k in range(n3): for l in range(n4): p[i, j, k, l] = ps[i, k, l] * B[j] + Afact return p numbaReconstruct4D = jit(ret_type=numba.d[:, :, :, :], arg_types=[ numba.d[:, :, :], numba.d[:], numba.d[:], numba.d, numba.d[:, :, :, :] ])(recontruct4DCore) def reconstructPressureFromHybrid(ps, A, B, Po): """ Reconstruct the Pressure field on sigma levels, from the surface pressure Input Ps : Surface pressure A,B,Po: Hybrid Convertion Coefficients, such as: p=B.ps+A.Po Ps: surface pressure B,A are 1D : sigma levels Po and Ps must have same units
def test_compiled_for_loop_fn_3(self): compiled_for_loop_fn = jit(argtypes = ['i'], restype = 'i', backend='bytecode')(for_loop_fn_3) result = compiled_for_loop_fn(3) self.assertEqual(result, for_loop_fn_3(3)) self.assertEqual(result, 81)
def test_compiled_for_loop_w_guard_1(self): compiled_for_loop_w_guard = jit(backend='bytecode')(for_loop_w_guard_1) self.assertEqual(compiled_for_loop_w_guard(5.), for_loop_w_guard_1(5.)) self.assertEqual(compiled_for_loop_w_guard(4.), for_loop_w_guard_1(4.))
def __init__(self, py_func, identity=None, targetoptions={}): self.py_func = py_func self.identity = self.parse_identity(identity) self.nb_func = jit(target='npyufunc', **targetoptions)(py_func) self._sigs = [] self._cres = {}
def test_compiled_for_loop_fn_2(self): compiled_for_loop_fn = jit(arg_types=['i'], ret_type='i')(for_loop_fn_2) result = compiled_for_loop_fn(4) self.assertEqual(result, 36) self.assertEqual(result, for_loop_fn_2(4))
def test_compiled_for_loop_w_guard_1(self): compiled_for_loop_w_guard = jit()(for_loop_w_guard_1) self.assertEqual(compiled_for_loop_w_guard(5.), for_loop_w_guard_1(5.)) self.assertEqual(compiled_for_loop_w_guard(4.), for_loop_w_guard_1(4.))
def test_while_loop_fn_4(self): compiled_fn = jit(arg_types = ['l', 'l', 'l'], ret_type = 'l')(while_loop_fn_4) compiled_result = compiled_fn(1, 4, 1) self.assertEqual(compiled_result, while_loop_fn_4(1, 4, 1)) self.assertEqual(compiled_result, 6)
def test_compiled_for_loop_fn_2(self): compiled_for_loop_fn = jit(arg_types = ['i'], ret_type = 'i')(for_loop_fn_2) result = compiled_for_loop_fn(4) self.assertEqual(result, 36) self.assertEqual(result, for_loop_fn_2(4))
def test_call_len(self): testarr = numpy.arange(10.) testfn = jit(arg_types = [double[:]], ret_type = long_)( call_len) self.assertEqual(testfn(testarr), 10)
from numba import d from numba.decorators import jit as jit def sum2d(arr): M, N = arr.shape result = 0.0 for i in range(M): for j in range(N): result += arr[i,j] return result csum2d = jit(ret_type=d, arg_types=[d[:,:]])(sum2d) from numpy import random arr = random.randn(100,100) import time start = time.time() res = sum2d(arr) duration = time.time() - start print "Result from python is %s in %s (msec)" % (res, duration*1000) start = time.time() res = csum2d(arr) duration2 = time.time() - start print "Result from compiled is %s in %s (msec)" % (res, duration2*1000) print "Speed up is %s" % (duration / duration2)
# -*- coding: utf-8 -*- from __future__ import print_function, division, absolute_import from numba import double from numba.decorators import jit as jit def sum2d(arr): M, N = arr.shape result = 0.0 for i in range(M): for j in range(N): result += arr[i, j] return result jitsum2d = jit(sum2d) csum2d = jitsum2d.compile(double(double[:, ::1])) from numpy import random arr = random.randn(100, 100) import time start = time.time() res = sum2d(arr) duration = time.time() - start print("Result from python is %s in %s (msec)" % (res, duration * 1000)) csum2d(arr) # warm up start = time.time() res = csum2d(arr)
# to detect and desugar for loops over range/xrange/arange first. i = 0 z_real = 0. z_imag = 0. while i < max_iters: z_real_n = z_real * z_real - z_imag * z_imag + real_coord z_imag = 2. * z_real * z_imag + imag_coord z_real = z_real_n if (z_real * z_real + z_imag * z_imag) >= 4: return i i += 1 return -1 try: mandel_1c = jit(arg_types=['d', 'd', 'i'], ret_type='i')(mandel_1) except: if __debug__: import traceback as tb tb.print_exc() mandel_1c = None mandel_1c_ast = function(mandel_1) #@jit(arg_types = ['d', 'd', 'd', 'i', [['b']], [[['b']]]]) def mandel_driver_1(min_x, max_x, min_y, nb_iterations, colors, image): nb_colors = len(colors) width = image.shape[0] height = image.shape[1] pixel_size = (max_x - min_x) / width
def test_compiled_for_loop_fn_0(self): test_data = numpy.array([1, 2, 3], dtype='l') compiled_for_loop_fn = jit(arg_types=[['l']])(for_loop_fn_0) result = compiled_for_loop_fn(test_data) self.assertEqual(result, 6) self.assertEqual(result, for_loop_fn_0(testdata))
def test_while_loop_fn_6(self): compiled_fn = jit()(while_loop_fn_6) self.assertEqual(while_loop_fn_6(4.), compiled_fn(4.)) self.assertEqual(while_loop_fn_6(5.), compiled_fn(5.))
def test_compiled_for_loop_fn_3(self): compiled_for_loop_fn = jit(arg_types=['i'], ret_type='i')(for_loop_fn_3) result = compiled_for_loop_fn(3) self.assertEqual(result, for_loop_fn_3(3)) self.assertEqual(result, 81)
def test_if_fn_1(self): if_fn_1c = jit()(if_fn_1) self.assertEqual(if_fn_1c(-1.), 42.) self.assertEqual(if_fn_1c(1.), 22.)
def test_compiled_for_loop_fn_1(self): compiled_for_loop_fn = jit(arg_types=['i', 'i', 'i'], ret_type='i')(for_loop_fn_1) result = compiled_for_loop_fn(1, 4, 1) self.assertEqual(result, 6) self.assertEqual(result, for_loop_fn_1(1, 4, 1))
def test_tuple_fn_0(self): test_arr = numpy.zeros((4, 4, 4)) compiled_fn = jit(arg_types=[['d']])(tuple_fn_0) self.assertEqual(compiled_fn(test_arr), 0.)
def test_compiled_for_loop_fn_2(self): compiled_for_loop_fn = jit(argtypes = ['i'], restype = 'i', backend='bytecode')(for_loop_fn_2) result = compiled_for_loop_fn(4) self.assertEqual(result, 36) self.assertEqual(result, for_loop_fn_2(4))
def test_long(self): func = jit(ret_type=numba.long_, arg_types=[numba.long_])(test_long) self.assertEqual(func(-1), 42) self.assertEqual(func(1), 22)
def test_compiled_for_loop_fn_1(self): compiled_for_loop_fn = jit(argtypes = ['i','i','i'], restype = 'i', backend='bytecode')(for_loop_fn_1) result = compiled_for_loop_fn(1, 4, 1) self.assertEqual(result, 6) self.assertEqual(result, for_loop_fn_1(1, 4, 1))
def test_getattr_shape_1(self): test_data = numpy.array([1., 2., 3.]) compiled_fn = jit(ret_type='i%d*' % (_plat_bits // 8), arg_types=[['d']])(get_ndarray_shape) result = compiled_fn(test_data) self.assertEqual(result[0], 3)
def test_call_zeros_like(self): testarr = numpy.array([1., 2, 3, 4, 5], dtype=numpy.double) testfn = jit(arg_types = [double[:]], ret_type = double[:])( call_zeros_like) self.assertTrue((testfn(testarr) == numpy.zeros_like(testarr)).all())
ctr = 0 for j in range(yy.shape[1]/3): ax = yy[i, j*3] ay = yy[i, j*3+1] az = yy[i, j*3+2] for k in range(j+1, yy.shape[1]/3): bx = yy[i,k*3] by = yy[i,k*3+1] bz = yy[i,k*3+2] tmpx = ax-bx tmpy = ay-by tmpz = az-bz boxhalf = 7.5 if(tmpx < -boxhalf): tmpx = tmpx + boxhalf if(tmpx > boxhalf): tmpx = tmpx - boxhalf if(tmpy < -boxhalf): tmpy = tmpy + boxhalf if(tmpy > boxhalf): tmpy = tmpy - boxhalf if(tmpz < -boxhalf): tmpz = tmpz + boxhalf if(tmpz > boxhalf): tmpz = tmpz - boxhalf dist[i,ctr] = math.sqrt(tmpx*tmpx + tmpy*tmpy + tmpz*tmpz) ctr += 1 return dist jitdist = jit(get_distances) preds = loaded_model.predict(jitdist(coords)/27) preds = mm.inverse_transform(preds) preds = preds.reshape((108,3)) np.savetxt('./dnn.forces.dat', preds, delimiter=' ')
def test_if_fn_1(self): if_fn_1c = jit(backend='bytecode')(if_fn_1) self.assertEqual(if_fn_1c(-1.), 42.) self.assertEqual(if_fn_1c(1.), 22.)
a[1, i][1] *= -1 elif a[0, i][1] > 2 - size: a[0, i][1] = 2 - size a[1, i][1] *= -1 return a np.random.seed(0) a = np.empty([2, N, 2], dtype=float) a[0, :] = -0.5 + np.random.random((N, 2)) # positions a[1, :] = -0.5 + np.random.random((N, 2)) # velocities a[0, :] *= (4 - 2 * size) dt = 1. / 30 step_numba = jit('f8[:,:,:](f8, f8, f8[:,:,:])')(step) gr.setwindow(-2, 2, -2, 2) gr.setviewport(0, 1, 0, 1) gr.setmarkertype(gr.MARKERTYPE_SOLID_CIRCLE) gr.setmarkersize(1.0) start = time.time() t0 = start n = 0 t = 0 worker = 'CPython' while t < 6:
min_ = np.reshape(np.min(data5_1, 1), [np.shape(data5_1)[0], 1]) # if max_.any()!=min_.any(): data5_1 = (data5_1 - min_) / (max_ - min_ + 1.) pred2 = sess.run(prediction, feed_dict={ x: data5_1, keep: 1. }) dBuf_1[i:i + isize, jj:jj + isize] = np.reshape( pred2, [isize, isize]) * 255 # 使用多线程,numba 加速 # jit(compute(0, srcYSize)) try: jit(compute(0, srcYSize), target='gpu') except: jit(compute(0, srcYSize)) print(datetime.datetime.now() - start) print('writer data:', datetime.datetime.now()) target_ds.GetRasterBand(1).WriteArray(dBuf_1, 0, 0) target_ds.FlushCache() # 将数据写入文件 target_ds = None print("计算完成:100.00%", end="\r") # endtime = datetime.datetime.now() print('end time:', datetime.datetime.now()) # print((endtime - starttime).seconds) exit()
def binary_search(a, x): lo = 0 hi = a.shape[0] while lo < hi: mid = (lo + hi) // 2 midval = a[mid] if midval < x: lo = mid + 1 elif midval > x: hi = mid else: return mid return -1 binary_search_numba = jit(binary_search, nopython=True) def extract(all_elems_codes, out, askii_list): MAX_STR = out.shape[0] cur_num_str = 0 i = all_elems_codes.shape[0] - 1 state = 0 cur_end = -1 min_length = 4 count_one = 0 count_two = 0 count_three = 0
def test_while_loop_fn_3(self): compiled_fn = jit(arg_types = ['l'])(while_loop_fn_3) compiled_result = compiled_fn(3) self.assertEqual(compiled_result, while_loop_fn_3(3)) self.assertEqual(compiled_result, 8.)
pred2 = sess.run(pred, feed_dict={x: data_2_1, keep_prob: 1.}) del data_2_1 for row, pred3 in enumerate(sess.run(tf.argmax(pred2, 1))): if pred3: print('pred3', pred3) jj = row * isize if jj + isize >= srcXSize - 1: jj = srcXSize - 1 - isize dBuf_1[i:i + isize, jj:jj + isize] = np.ones([isize, isize], np.uint8) * 255 # 使用多线程,numba 加速 jit(compute(0, srcYSize)) # try: # jit(compute(0,srcYSize),target='cuda') # except: # pass print(datetime.datetime.now() - start) print('writer data:', datetime.datetime.now()) target_ds.GetRasterBand(1).WriteArray(dBuf_1, 0, 0) target_ds.FlushCache() # 将数据写入文件 target_ds = None print("计算完成:100.00%", end="\r") # endtime = datetime.datetime.now() print('end time:', datetime.datetime.now()) # print((endtime - starttime).seconds)
def test_while_loop_fn_5(self): compiled_fn = jit(arg_types = ['d', 'd'])(while_loop_fn_5) compiled_result = compiled_fn(3, 4) self.assertEqual(compiled_result, while_loop_fn_5(3, 4)) self.assertEqual(compiled_result, 18.)
def __init__(self, py_func, signature, targetoptions={}): self.py_func = py_func self.nb_func = jit(target='npyufunc')(py_func) self.signature = signature self.sin, self.sout = parse_signature(signature) self.targetoptions = targetoptions
def _do_test(self, function, arg_types, *args, **kws): _jit = (jit(arg_types = arg_types) if arg_types is not None else jit()) compiled_fn = _jit(function) self.assertEqual(compiled_fn(*args, **kws), function(*args, **kws))
def __init__(self, py_func, targetoptions={}): self.py_func = py_func self.nb_func = jit(target='npyufunc', **targetoptions)(py_func)
def test_int(self): func = jit(ret_type=numba.int_, arg_types=[numba.int_])(test_int) self.assertEqual(func(-1), 42) self.assertEqual(func(1), 22)
for i in range(nx): for j in range(ny): if (((i * dx - 0.5)**2 + (j * dy - 0.5)**2 <= 0.1) & ((i * dx - 0.5)**2 + (j * dy - 0.5)**2 >= 0.05)): ui[i, j] = 1 def diff_step(u, ui): for i in range(1, nx - 1): for j in range(1, ny - 1): uxx = (ui[i + 1, j] - 2 * ui[i, j] + ui[i - 1, j]) / (dx * dx) uyy = (ui[i, j + 1] - 2 * ui[i, j] + ui[i, j - 1]) / (dy * dy) u[i, j] = ui[i, j] + dt * a * (uxx + uyy) diff_step_numba = jit('void(f8[:,:], f8[:,:])')(diff_step) now = perf_counter() t = 0 worker = 'CPython' for m in range(timesteps): gr.clearws() start = now if t > 5: diff_step_numba(u, ui) worker = 'Numba' else: diff_step(u, ui)
def __init__(self, py_func, identity=None, targetoptions={}): self.py_func = py_func self.identity = parse_identity(identity) self.nb_func = jit(target='npyufunc', **targetoptions)(py_func) self._sigs = [] self._cres = {}
def test_getattr_ndim_2(self): test_data2 = numpy.array([[1., 2., 3.], [4., 5., 6.]]) compiled_fn2 = jit(ret_type='i', arg_types=[[['d']]])(get_ndarray_ndim) self.assertEqual(compiled_fn2(test_data2), 2)