def test_slice_1d_3(dtype): def fkt(a, b): a[2:] = b[1:9] (ao, ah), (b, _) = random(dtype, [10]), random(dtype, [10]) fkt(ao, b), hope.jit(fkt)(ah, b) assert check(ao, ah) fkt(ao, b), hope.jit(fkt)(ah, b) assert check(ao, ah)
def test_negative_slice_1d_both(): def fkt(a): a[1:-1] = 1 ah = np.zeros(10) hope.jit(fkt)(ah) assert np.all(ah[1:-1] == 1) assert ah[-1] == 0 assert ah[0] == 0
def test_index_2d(dtype): def fkt(a, b): a[4, 2] = b[3, 4] (ao, ah), (b, _) = random(dtype, [5, 5]), random(dtype, [5, 5]) fkt(ao, b), hope.jit(fkt)(ah, b) assert check(ao, ah) fkt(ao, b), hope.jit(fkt)(ah, b) assert check(ao, ah)
def test_index_1d(dtype): def fkt(a, b): a[5] = b[3] (ao, ah), (b, _) = random(dtype, [10]), random(dtype, [10]) fkt(ao, b), hope.jit(fkt)(ah, b) assert check(ao, ah) fkt(ao, b), hope.jit(fkt)(ah, b) assert check(ao, ah)
def test_assignment(dtype): def fkt(a, b): a[:] = b (ao, ah), (b, _) = random(dtype, [10]), random(dtype, [10]) fkt(ao, b), hope.jit(fkt)(ah, b) assert check(ao, ah) fkt(ao, b), hope.jit(fkt)(ah, b) assert check(ao, ah)
def test_slice_1d_2(dtype): def test(a, b): a[:5] = b[3:8] (ao, ah), (b, _) = random(dtype, [10]), random(dtype, [10]) test(ao, b), hope.jit(test)(ah, b) assert check(ao, ah) test(ao, b), hope.jit(test)(ah, b) assert check(ao, ah)
def test_index_2d_2(dtype): def fkt(a): a[1:4, 1:4] = 1 (ao, ah) = random(dtype, [5, 5]) fkt(ao), hope.jit(fkt)(ah) assert check(ao, ah) fkt(ao), hope.jit(fkt)(ah) assert check(ao, ah)
def test_create_array_invalid_args(): def fkt(shape): return np.empty(shape, 0) hfkt = hope.jit(fkt) with pytest.raises(UnsupportedFeatureException): hfkt(5) def fkt_order(shape): return np.empty(shape, order="c") hfkt = hope.jit(fkt_order) with pytest.raises(NotImplementedError): hfkt(5)
def test_create_ones_array(): def fkt(shape): return np.ones(shape) hfkt = hope.jit(fkt) assert fkt(5).size == hfkt(5).size def fkt_dtype(shape): return np.ones(shape, dtype=np.float64) hfkt = hope.jit(fkt_dtype) co, ch = fkt(5), hfkt(5) assert co.size == ch.size assert co.dtype == np.float64 assert ch.dtype == np.float64
def test_fkt_call_scalar_jit_multi(dtype): hfkt = hope.jit(fkt_call_scalar_jit_multi_fkt) (a, _), (c, _) = random(dtype, []), random(dtype, []) c = hfkt(a) assert check(c, a + 1) c = hfkt(a) assert check(c, a + 1)
def test_func_uint32(dtype): def fkt(a): return np.uint32(a) hfkt = hope.jit(fkt) ao, ah = random(dtype, []) co, ch = fkt(ao), hfkt(ah) assert type(co) == type(ch)
def test_return_arrayscalarcpy(dtype): def fkt(a): b = a[2] return b ao, ah = random(dtype, [10]) ro, rh = fkt(ao), hope.jit(fkt)(ah) assert check(ro, rh)
def test_cross_div(dtypea, dtypeb, dtypec): if dtypea == np.int8 and dtypeb == np.int8: pytest.skip("Different behaviour in c++ and python for int8 / int8".format(dtypea, dtypeb)) def fkt(a, b, c): c[:] = a / b hfkt = hope.jit(fkt) (ao, ah), (bo, bh), (co, ch) = random(dtypea, [10]), random(dtypeb, [10]), random(dtypec, [10]) ao, ah, bo, bh = ao.astype(np.float64), ah.astype(np.float64), bo.astype(np.float64), bh.astype(np.float64) ao, ah = ( np.copysign(np.power(np.abs(ao), 1.0 / 4.0), ao).astype(dtypea), np.copysign(np.power(np.abs(ah), 1.0 / 4.0), ah).astype(dtypea), ) bo, bh = ( np.copysign(np.power(np.abs(bo), 1.0 / 4.0), bo).astype(dtypeb), np.copysign(np.power(np.abs(bh), 1.0 / 4.0), bh).astype(dtypeb), ) if np.count_nonzero(bo == 0) > 0: bo[bo == 0] += 1 if np.count_nonzero(bh == 0) > 0: bh[bh == 0] += 1 fkt(ao, bo, co), hfkt(ah, bh, ch) assert check(co, ch) fkt(ao, bo, co), hfkt(ah, bh, ch) assert check(co, ch)
def test_func_sum_invalid_args(): a = np.random.random(1) b = np.empty_like(a) def fkt(a, b): return np.sum(a, b) hfkt = hope.jit(fkt) with pytest.raises(UnsupportedFeatureException): hfkt(a, b) def fkt_axis(a): return np.sum(a, axis=0) hfkt = hope.jit(fkt_axis) with pytest.raises(UnsupportedFeatureException): hfkt(a)
def test_np_func_invalid_args(): def fkt(a, b): return np.log(a, b) hfkt = hope.jit(fkt) a = np.random.random(1) b = np.empty_like(a) with pytest.raises(UnsupportedFeatureException): hfkt(a, b) def fkt_kwargs(a, b): return np.log(a, out=b) hfkt = hope.jit(fkt_kwargs) with pytest.raises(UnsupportedFeatureException): hfkt(a, b)
def test_false(): def fkt(): if False: return 1 else: return 0 hfkt = hope.jit(fkt) ro, rh = fkt(), hfkt() assert check(ro, rh)
def test_func_log(dtype, shape): def fkt(a): return np.log(a) hfkt = hope.jit(fkt) a, b = np.abs(random(dtype, shape)) co = fkt(a) ch = hfkt(b) assert check(co, ch)
def test_call_scalar_jit_fun_return(dtype): def fkt(a): return fkt_call_scalar_jit_fun_return_callback(a) hfkt = hope.jit(fkt) (ao, ah), (co, ch) = random(dtype, []), random(dtype, []) co, ch = fkt(ao), hfkt(ah) assert check(co, ch) co, ch = fkt(ao), hfkt(ah) assert check(co, ch)
def test_unsupported_np_func(): def fkt(a): return np.alen(a) hfkt = hope.jit(fkt) a = np.random.random(1) with pytest.raises(UnsupportedFeatureException): hfkt(a)
def test_func_exp(dtype, shape): def fkt(a): return np.exp(a) hfkt = hope.jit(fkt) ao, ah = np.log(np.abs(random(dtype, shape))).astype(dtype) co = fkt(ao) ch = hfkt(ah) assert check(co, ch)
def test_func_arcsin(dtype, shape): def fkt(a): return np.arcsin(a) hfkt = hope.jit(fkt) a = 2 * np.random.random(shape) - 1 b = copy.deepcopy(a) co = fkt(a) ch = hfkt(b) assert check(co, ch)
def test_func_arccos(dtype, shape): def fkt(a): return np.arccos(a) hfkt = hope.jit(fkt) a = 2 * np.random.random(shape) - 1 b = copy.deepcopy(a) co = fkt(a) ch = hfkt(b) assert check(co, ch)
def test_binary_minus(dtype, shape): def fkt(a, b, c): c[:] = a - b hfkt = hope.jit(fkt) (ao, ah), (bo, bh), (co, ch) = random(dtype, shape), random(dtype, shape), random(dtype, shape) ao, ah, bo, bh = (ao / 2.).astype(dtype), (ah / 2.).astype(dtype), (bo / 2.).astype(dtype), (bh / 2.).astype(dtype) ro, rh = fkt(ao, bo, co), hfkt(ah, bh, ch) assert check(co, ch) ro, rh = fkt(ao, bo, co), hfkt(ah, bh, ch) assert check(co, ch)
def test_func_cosh(dtype, shape): def fkt(a): return np.cosh(a) hfkt = hope.jit(fkt) a = np.log(np.abs(random(dtype, shape)[0]) / 2.) b = copy.deepcopy(a) co = fkt(a) ch = hfkt(b) assert check(co, ch)
def test_binary_rshift(dtype, shape): def fkt(a, b, c): c[:] = a >> b hfkt = hope.jit(fkt) (ao, ah), (bo, bh), (co, ch) = random(dtype, shape), random(dtype, shape), random(dtype, shape) bo, bh = (bo % (np.dtype(dtype).itemsize * 8)).astype(dtype), (bh % (np.dtype(dtype).itemsize * 8)).astype(dtype) fkt(ao, bo, co), hfkt(ah, bh, ch) assert check(co, ch) fkt(ao, bo, co), hfkt(ah, bh, ch) assert check(co, ch)
def test_call_local_fun(dtype, shape): def fkt(a, b, c): fkt_call_local_fun_callback(a, b) c[:] = a hfkt = hope.jit(fkt) (ao, ah), (bo, bh), (co, ch) = random(dtype, shape), random(dtype, shape), random(dtype, shape) fkt(ao, bo, co), hfkt(ah, bh, ch) assert check(co, ch) fkt(ao, bo, co), hfkt(ah, bh, ch) assert check(co, ch)
def test_func_sqrt(dtype, shape): def fkt(a): return np.sqrt(a) hfkt = hope.jit(fkt) (ao, ah) = random(dtype, shape) ao, ah = np.abs(ao), np.abs(ah) co = fkt(ao) ch = hfkt(ah) assert check(co, ch)
def test_blocks_2(): def fkt(): i = 1 i += 1 i = 3 return i hfkt = hope.jit(fkt) ro, rh = fkt(), hfkt() assert check(ro, rh)
def test_augmented_rshift(dtype, shape): def fkt(a, c): c[:] >>= a hfkt = hope.jit(fkt) (ao, ah), (co, ch) = random(dtype, [10]), random(dtype, [10]) ao, ah = (ao % (np.dtype(dtype).itemsize * 8)).astype(dtype), (ah % (np.dtype(dtype).itemsize * 8)).astype(dtype) fkt(ao, co), hfkt(ah, ch) assert check(co, ch) fkt(ao, co), hfkt(ah, ch) assert check(co, ch)
def test_func_sum_var(dtype, shape): def fkt(a): return np.sum(a) hfkt = hope.jit(fkt) ao, ah = random(dtype, shape) ao, ah = ao / 1200, ah / 1200 co, ch = fkt(ao), hfkt(ah) assert check(co, ch) co, ch = fkt(ao), hfkt(ah) assert check(co, ch)
def test_augmented_plus(dtype, shape): def fkt(a, c): c[:] += a hfkt = hope.jit(fkt) (ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape) ao, ah, co, ch = (ao / 4.).astype(dtype), (ah / 4.).astype(dtype), (co / 2.).astype(dtype), (ch / 2.).astype(dtype) ro, rh = fkt(ao, co), hfkt(ah, ch) assert check(co, ch) ro, rh = fkt(ao, co), hfkt(ah, ch) assert check(co, ch)
def test_augmented_minus(dtype, shape): def fkt(a, c): c[:] -= a hfkt = hope.jit(fkt) (ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape) ao, ah, co, ch = (ao / 4.).astype(dtype), (ah / 4.).astype(dtype), (co / 2.).astype(dtype), (ch / 2.).astype(dtype) ro, rh = fkt(ao, co), hfkt(ah, ch) assert check(co, ch) ro, rh = fkt(ao, co), hfkt(ah, ch) assert check(co, ch)
def test_binary_plus(dtype, shape): def fkt(a, b, c): c[:] = a + b hfkt = hope.jit(fkt) (ao, ah), (bo, bh), (co, ch) = random(dtype, shape), random(dtype, shape), random(dtype, shape) ao, ah, bo, bh = (ao / 2.).astype(dtype), (ah / 2.).astype(dtype), (bo / 2.).astype(dtype), (bh / 2.).astype(dtype) ro, rh = fkt(ao, bo, co), hfkt(ah, bh, ch) assert check(co, ch) ro, rh = fkt(ao, bo, co), hfkt(ah, bh, ch) assert check(co, ch)
def test_func_interp_bounds(dtype): def fkt(x, y, x0, y0): y0[:] = np.interp(x0, x, y) hfkt = hope.jit(fkt) x = np.arange(5, 15).astype(dtype) y = -x x0 = np.arange(0, 20).astype(dtype) yo, yh = np.zeros_like(x0), np.zeros_like(x0) fkt(x, y, x0, yo), hfkt(x, y, x0, yh) assert check(yo, yh)
def test_func_sum_dtype(dtype, shape): def fkt(a): return np.sum(a, dtype=np.float64) hfkt = hope.jit(fkt) ao, ah = random(dtype, shape) ao, ah = ao / 1200, ah / 1200 co, ch = fkt(ao), hfkt(ah) assert check(co, ch) assert co.dtype == np.float64 assert ch.dtype == np.float64
def test_ufig_star(dtype): b = 3.5 a = 1. / np.sqrt(2.**(1. / (b - 1.)) - 1.) r50 = 2 center = np.array([10.141, 10.414]) dims = np.array([20, 20]) # coefficients generated by http://keisan.casio.com/has10/SpecExec.cgi?id=system/2006/1280624821, 7th order x1D = np.array([ \ 0.5 - 0.9491079123427585245262 / 2 \ , 0.5 - 0.7415311855993944398639 / 2 \ , 0.5 - 0.4058451513773971669066 / 2 \ , 0.5 \ , 0.5 + 0.4058451513773971669066 / 2 \ , 0.5 + 0.7415311855993944398639 / 2 \ , 0.5 + 0.9491079123427585245262 / 2 \ ], dtype=dtype) w1D = np.array([ \ 0.1294849661688696932706 / 2 \ , 0.2797053914892766679015 / 2 \ , 0.38183005050511894495 / 2 \ , 0.4179591836734693877551 / 2 \ , 0.38183005050511894495 / 2 \ , 0.2797053914892766679015 / 2 \ , 0.1294849661688696932706 / 2 \ ], dtype=dtype) w2D = np.outer(w1D, w1D) def fkt_pdf(density, dims, center, w2D, r50, b, a): for x in range(dims[0]): for y in range(dims[1]): dr = np.sqrt((x - center[0])**2 + (y - center[1])**2) density[x, y] = np.sum(w2D * 2 * (b - 1) / (2 * np.pi * (r50 * a)**2) * (1 + (dr / (r50 * a))**2)**(-b)) return density hope.config.optimize = True hpdf = hope.jit(fkt_pdf) density = np.zeros((dims[0], dims[1]), dtype=dtype) fkt_pdf(density, dims, center, w2D, r50, b, a) hdensity = np.zeros((dims[0], dims[1]), dtype=dtype) hpdf(hdensity, dims, center, w2D, r50, b, a) hope.config.optimize = False # print("going to sleep") # import time # time.sleep(10) assert check(density, hdensity) if np.all(hdensity == 1): print("asdf") else: print("else")
def run(dtype, shape): hfkt = hope.jit(fkt) (ao, ah), (bo, bh), (co, ch) = random(dtype, shape), random(dtype, shape), random(dtype, shape) ro, rh = fkt(ao, bo, co), hfkt(ah, bh, ch) for a, b in [(ao, ah), (bo, bh), (co, ch), (ro, rh)]: if not a is None or not b is None: assert check(a, b) ro, rh = fkt(ao, bo, co), hfkt(ah, bh, ch) for a, b in [(ao, ah), (bo, bh), (co, ch), (ro, rh)]: if not a is None or not b is None: assert check(a, b)
def test_func_sum_expr(dtype, shape): def fkt(a, b): return np.sum(a + b) hfkt = hope.jit(fkt) (ao, ah), (bo, bh) = random(dtype, shape), random(dtype, shape) ao, ah = np.abs(ao) / 2400, np.abs(ah) / 2400 bo, bh = np.abs(bo) / 2400, np.abs(bh) / 2400 co, ch = fkt(ao, bo), hfkt(ah, bh) assert check(co, ch) co, ch = fkt(ao, bo), hfkt(ah, bh) assert check(co, ch)
def test_binary_pow(dtype, shape): def fkt(a, c): c[:] = a**2 hfkt = hope.jit(fkt) (ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape) ao, ah = np.copysign(np.sqrt(np.abs(ao)), ao).astype(dtype), np.copysign( np.sqrt(np.abs(ah)), ah).astype(dtype) fkt(ao, co), hfkt(ah, ch) assert check(co, ch) fkt(ao, co), hfkt(ah, ch) assert check(co, ch)
def test_binary_floordiv(dtype, shape): def fkt(a, b, c): c[:] = a // b hfkt = hope.jit(fkt) (ao, ah), (bo, bh), (co, ch) = random(dtype, shape), random(dtype, shape), random(dtype, shape) if np.count_nonzero(bo == 0) > 0: bo[bo == 0] += 1 if np.count_nonzero(bh == 0) > 0: bh[bh == 0] += 1 ro, rh = fkt(ao, bo, co), hfkt(ah, bh, ch) if dtype in [np.float32, np.float64, float]: co[co < 1. / (np.finfo(dtype).max * np.finfo(dtype).resolution)] /= np.finfo(dtype).resolution ch[ch < 1. / (np.finfo(dtype).max * np.finfo(dtype).resolution)] /= np.finfo(dtype).resolution assert check(co, ch)
def test_augmented_rshift(dtype, shape): def fkt(a, c): c[:] >>= a hfkt = hope.jit(fkt) (ao, ah), (co, ch) = random(dtype, [10]), random(dtype, [10]) ao, ah = (ao % (np.dtype(dtype).itemsize * 8)).astype(dtype), ( ah % (np.dtype(dtype).itemsize * 8)).astype(dtype) fkt(ao, co), hfkt(ah, ch) assert check(co, ch) fkt(ao, co), hfkt(ah, ch) assert check(co, ch)
def test_augmented_mod(dtype, shape): def fkt(a, c): c[:] %= a hfkt = hope.jit(fkt) (ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape) if np.count_nonzero(ao == 0) > 0: ao[ao == 0] += 1 if np.count_nonzero(ah == 0) > 0: ah[ah == 0] += 1 fkt(ao, co), hfkt(ah, ch) assert check(co, ch) fkt(ao, co), hfkt(ah, ch) assert check(co, ch)
def test_augmented_floordiv(dtype, shape): def fkt(a, c): c[:] //= a hfkt = hope.jit(fkt) (ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape) if np.count_nonzero(ao == 0) > 0: ao[ao == 0] += 1 if np.count_nonzero(ah == 0) > 0: ah[ah == 0] += 1 ro, rh = fkt(ao, co), hfkt(ah, ch) if dtype in [np.float32, np.float64, float]: co[co < 1. / (np.finfo(dtype).max * np.finfo(dtype).resolution)] /= np.finfo(dtype).resolution ch[ch < 1. / (np.finfo(dtype).max * np.finfo(dtype).resolution)] /= np.finfo(dtype).resolution assert check(co.astype(dtype), ch.astype(dtype))
def test_cross_add_scalar(dtype): def fkt(a, b, c): c[:] = a + b hfkt = hope.jit(fkt) dtypeo = np.float64 if dtype == float else dtype (ao, ah), (bo, bh), (co, ch) = random(dtypeo, [100]), random(dtypeo, []), random(dtypeo, [100]) ao, ah = (ao / 2.).astype(dtype), (ah / 2.).astype(dtype) bo, bh = (bo / 2.).astype(dtype), (bh / 2.).astype(dtype) fkt(ao, bo, co), hfkt(ah, bh, ch) assert check(co, ch) fkt(ao, bo, co), hfkt(ah, bh, ch) assert check(co, ch)
def test_cross_add_dtype(dtypea, dtypeb, dtypec): def fkt(a, b, c): c[:] = a + b hfkt = hope.jit(fkt) dtypeao = np.float64 if dtypea == float else dtypea dtypebo = np.float64 if dtypeb == float else dtypeb (ao, ah), (bo, bh), (co, ch) = random(dtypeao, [100]), random(dtypebo, [100]), random(dtypec, [100]) ao, ah = (ao / 2.).astype(dtypea), (ah / 2.).astype(dtypea) bo, bh = (bo / 2.).astype(dtypeb), (bh / 2.).astype(dtypeb) fkt(ao.astype(dtypea), bo.astype(dtypeb), co), hfkt(ah.astype(dtypea), bh.astype(dtypeb), ch) assert check(co, ch) fkt(ao.astype(dtypea), bo.astype(dtypeb), co), hfkt(ah.astype(dtypea), bh.astype(dtypeb), ch) assert check(co, ch)
def test_func_sum_in_if(dtype, shape): def fkt(a): if True: val = np.sum(a) else: val = 1 return val hfkt = hope.jit(fkt) ao, ah = random(dtype, shape) ao, ah = ao / 1200, ah / 1200 co, ch = fkt(ao), hfkt(ah) assert check(co, ch)
def test_merged_slice_2(dtype): def fkt(a, c): a[1:] = c[:-1] + c[1:] c[:] = a hfkt = hope.jit(fkt) (ao, ah), (co, ch) = random(dtype, [5]), random(dtype, [5]) ao, ah = (ao / 4.).astype(dtype), (ah / 4.).astype(dtype) co, ch = (co / 4.).astype(dtype), (ch / 4.).astype(dtype) fkt(ao, co), hfkt(ah, ch) assert check(co, ch) fkt(ao, co), hfkt(ah, ch) assert check(co, ch)
def test_binary_rshift(dtype, shape): def fkt(a, b, c): c[:] = a >> b hfkt = hope.jit(fkt) (ao, ah), (bo, bh), (co, ch) = random(dtype, shape), random(dtype, shape), random( dtype, shape) bo, bh = (bo % (np.dtype(dtype).itemsize * 8)).astype(dtype), ( bh % (np.dtype(dtype).itemsize * 8)).astype(dtype) fkt(ao, bo, co), hfkt(ah, bh, ch) assert check(co, ch) fkt(ao, bo, co), hfkt(ah, bh, ch) assert check(co, ch)
def test_merged_slice(dtype): def fkt(a, b, c): for i in range(10): c[:, i] = a[i, :] hfkt = hope.jit(fkt) (ao, ah), (bo, bh), (co, ch) = random(dtype, [10, 10]), random( dtype, [10, 10]), random(dtype, [10, 10]) ao, ah, bo, bh = (ao / 2.).astype(dtype), (ah / 2.).astype(dtype), ( bo / 2.).astype(dtype), (bh / 2.).astype(dtype) fkt(ao, bo, co), hfkt(ah, bh, ch) assert check(co, ch) fkt(ao, bo, co), hfkt(ah, bh, ch) assert check(co, ch)
def test_return_arr_expr(dtype, shape): def fkt(a, c): return a**-2 + a**-4.0 + a**-7 hfkt = hope.jit(fkt) (ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape) ao, ah = np.copysign(np.power(np.abs(ao), 1. / 8.), ao).astype(dtype), np.copysign( np.power(np.abs(ah), 1. / 8.), ah).astype(dtype) if np.count_nonzero(ao == 0) > 0: ao[ao == 0] += 1 if np.count_nonzero(ah == 0) > 0: ah[ah == 0] += 1 fkt(ao, co), hfkt(ah, ch) assert check(co, ch) fkt(ao, co), hfkt(ah, ch) assert check(co, ch)