forked from taichi-dev/taichi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_compile.py
43 lines (31 loc) · 878 Bytes
/
benchmark_compile.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
import taichi as ti
from pytest import approx
import autograd.numpy as np
from autograd import grad
@ti.all_archs
def grad_test(tifunc, npfunc=None):
if npfunc is None:
npfunc = tifunc
x = ti.field(ti.f32)
y = ti.field(ti.f32)
ti.root.dense(ti.i, 1).place(x, x.grad, y, y.grad)
@ti.kernel
def func():
for i in x:
y[i] = tifunc(x[i])
v = 0.2
y.grad[0] = 1
x[0] = v
func()
func.grad()
assert y[0] == approx(npfunc(v))
assert x.grad[0] == approx(grad(npfunc)(v))
def test_unary():
import time
t = time.time()
grad_test(lambda x: ti.sqrt(x), lambda x: np.sqrt(x))
grad_test(lambda x: ti.exp(x), lambda x: np.exp(x))
grad_test(lambda x: ti.log(x), lambda x: np.log(x))
ti.core.print_profile_info()
print("Total time {:.3f}s".format(time.time() - t))
test_unary()