-
Notifications
You must be signed in to change notification settings - Fork 27
/
test_optimize.py
123 lines (111 loc) · 4.8 KB
/
test_optimize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Copyright (C) 2014 ETH Zurich, Institute for Astronomy
"""
Test functions for `hope` module.
"""
from __future__ import print_function, division, absolute_import, unicode_literals
import numpy as np
import hope, itertools, pytest, sys, sysconfig, os, shutil
from test.utilities import random, check, make_test, JENKINS, min_dtypes, dtypes, shapes, setup_module, setup_method, teardown_module
@pytest.mark.parametrize("dtype,shape", itertools.product(dtypes, shapes[1:]))
def test_opt_pow_array(dtype, shape):
def fkt(a, c):
c[:] = a ** 2.0 + a ** 4 + a ** 7
hope.config.optimize = True
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)
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
hope.config.optimize = False
@pytest.mark.parametrize("dtype", dtypes)
def test_opt_pow_scalar(dtype):
def fkt(a, c):
c[0] = a ** 2 + a ** 4 + a ** 7.0
hope.config.optimize = True
hfkt = hope.jit(fkt)
(ao, ah), (co, ch) = random(dtype, []), random(dtype, [1])
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)
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
fkt(ao, co), hfkt(ah, ch)
assert check(co, ch)
hope.config.optimize = False
@pytest.mark.parametrize("dtype,shape", itertools.product(dtypes, shapes[1:]))
def test_opt_neg_pow_array(dtype, shape):
def fkt(a, c):
c[:] = a ** -2 + a ** -4.0 + a ** -7
hope.config.optimize = True
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)
hope.config.optimize = False
@pytest.mark.parametrize("dtype", dtypes)
def test_opt_neg_pow_scalar(dtype):
def fkt(a, c):
c[0] = a ** -2 + a ** -4.0 + a ** -7
hope.config.optimize = True
hfkt = hope.jit(fkt)
(ao, ah), (co, ch) = random(dtype, []), random(dtype, [1])
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)
hope.config.optimize = False
@pytest.mark.parametrize("dtype,shape", itertools.product(dtypes, shapes[1:]))
def test_opt_basic_array(dtype, shape):
def fkt(a, c):
c[:] = (a + a) * a - 1 / a
hope.config.optimize = True
hfkt = hope.jit(fkt)
(ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape)
ao, ah = np.copysign(np.power(np.abs(ao), 1. / 4.), ao).astype(dtype), np.copysign(np.power(np.abs(ah), 1. / 4.), 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)
hope.config.optimize = False
@pytest.mark.parametrize("dtype", dtypes)
def test_opt_basic_scalar(dtype):
def fkt(a, c):
c[0] = (a + a) * a - 1 / a
hope.config.optimize = True
hfkt = hope.jit(fkt)
(ao, ah), (co, ch) = random(dtype, []), random(dtype, [1])
ao, ah = np.copysign(np.power(np.abs(ao), 1. / 4.), ao).astype(dtype), np.copysign(np.power(np.abs(ah), 1. / 4.), 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)
hope.config.optimize = False
@pytest.mark.parametrize("dtype", [float])
def test_opt_basic_scalar(dtype):
def fkt(a, c):
c[0] = a**-2
hope.config.optimize = True
hfkt = hope.jit(fkt)
(ao, ah), (co, ch) = random(dtype, []), random(dtype, [1])
ao, ah = 1. / np.power(np.abs(ao), 1. / 2.).astype(dtype), 1. / np.power(np.abs(ah), 1. / 2.).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)
hope.config.optimize = False
# TODO: add test for supported functions
# TODO: add test where resulting ast is really tested (pow)