-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_tensortrax_vs_autograd.py
165 lines (134 loc) · 5 KB
/
benchmark_tensortrax_vs_autograd.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
r"""
_
| | ████████╗██████╗ █████╗ ██╗ ██╗
| |_ ___ _ __ ___ ___ _ __╚══██╔══╝██╔══██╗██╔══██╗╚██╗██╔╝
| __/ _ \ '_ \/ __|/ _ \| '__| ██║ ██████╔╝███████║ ╚███╔╝
| || __/ | | \__ \ (_) | | ██║ ██╔══██╗██╔══██║ ██╔██╗
\__\___|_| |_|___/\___/|_| ██║ ██║ ██║██║ ██║██╔╝ ██╗
╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝
"""
from timeit import timeit
import matplotlib.pyplot as plt
import numpy as np
from autograd import jacobian
from autograd import numpy as anp
import tensortrax as tr
import tensortrax.math as tm
def fun_tensortrax(C):
return tm.trace(C) - tm.log(tm.linalg.det(C))
def det_autograd(A):
return (
A[0, 0] * A[1, 1] * A[2, 2]
+ A[0, 1] * A[1, 2] * A[2, 0]
+ A[0, 2] * A[1, 0] * A[2, 1]
- A[2, 0] * A[1, 1] * A[0, 2]
- A[2, 1] * A[1, 2] * A[0, 0]
- A[2, 2] * A[1, 0] * A[0, 1]
)
def fun_autograd(C):
Csym = (anp.einsum("ij...->ji...", C) + C) / 2
return anp.trace(Csym) - anp.log(det_autograd(Csym))
def pre_tensortrax(n, **kwargs):
np.random.seed(4519245)
F = np.eye(3).reshape(3, 3, 1) + (np.random.rand(3, 3, n) - 0.5) / 10
C = np.einsum("ki...,kj...->ij...", F, F)
stress = tr.gradient(fun_tensortrax, **kwargs)
elasticity = tr.hessian(fun_tensortrax, **kwargs)
return C, stress, elasticity
def pre_autograd(n, **kwargs):
np.random.seed(4519245)
F = anp.eye(3).reshape(3, 3, 1) + (anp.random.rand(3, 3, n) - 0.5) / 10
C = anp.einsum("ki...,kj...->ij...", F, F)
def reduce(fun):
def inner(C):
return anp.sum(fun(C), -1)
return inner
stress = jacobian(reduce(fun_autograd))
elasticity = jacobian(reduce(jacobian(reduce(fun_autograd))))
return C, stress, elasticity
tensors = 2 ** np.arange(0, 21, 2)
time_gradient_tensortrax = []
time_hessian_tensortrax = []
time_gradient_autograd = []
time_hessian_autograd = []
kwargs = dict(ntrax=1, sym=True, parallel=False)
number = 1
print("Tensortrax Benchmark (Comparison with Autograd)")
print("===============================================")
print("")
print(f"tensortrax {tr.__version__}")
print("")
for i, n in enumerate(tensors):
c, stress, elasticity = pre_tensortrax(n, **kwargs)
C, Stress, Elasticity = pre_autograd(n, **kwargs)
if n < 10000:
s = stress(c)
e = elasticity(c)
S = Stress(C)
E = Elasticity(C)
assert np.allclose(s, S)
assert np.allclose(e, E)
del s
del e
del S
del E
time_gradient_tensortrax.append(timeit(lambda: stress(c), number=number) / number)
time_hessian_tensortrax.append(
timeit(lambda: elasticity(c), number=number) / number
)
time_gradient_autograd.append(timeit(lambda: Stress(C), number=number) / number)
time_hessian_autograd.append(timeit(lambda: Elasticity(C), number=number) / number)
print(f"...Evaluate timings... {i+1}/{len(tensors)}")
print("")
print("| Tensors | Gradient (Tensortrax) in s | Gradient (Autograd) in s | Speedup |")
print("| ------- | -------------------------- | ------------------------ | ------- |")
for n, t_grad_trax, t_grad_autograd in zip(
tensors, time_gradient_tensortrax, time_gradient_autograd
):
speedup = t_grad_autograd / t_grad_trax
print(
f"| {n:7d} | {t_grad_trax:26.5f} | {t_grad_autograd:24.5f} | x{speedup:6.2f} |"
)
print("")
print("")
print("| Tensors | Hessian (Tensortrax) in s | Hessian (Autograd) in s | Speedup |")
print("| ------- | -------------------------- | ------------------------ | ------- |")
for n, t_hess_trax, t_hess_autograd in zip(
tensors, time_hessian_tensortrax, time_hessian_autograd
):
speedup = t_hess_autograd / t_hess_trax
print(
f"| {n:7d} | {t_hess_trax:26.5f} | {t_hess_autograd:24.5f} | x{speedup:6.2f} |"
)
plt.figure()
plt.title(r"Strain Energy Function $\psi(C) = \mathrm{tr}(C) - \ln(\det(C))$")
plt.loglog(
tensors,
time_gradient_tensortrax,
"C0",
label=r"Gradient (Tensortrax) $\partial \psi~/~\partial C$",
)
plt.loglog(
tensors,
time_gradient_autograd,
"C0--",
label=r"Gradient (Autograd) $\partial \psi~/~\partial C$",
)
plt.loglog(
tensors,
time_hessian_tensortrax,
"C1",
label=r"Hessian (Tensortrax) $\partial^2 \psi~/~\partial C \partial C$",
)
plt.loglog(
tensors,
time_hessian_autograd,
"C1--",
label=r"Hessian (Autograd) $\partial^2 \psi~/~\partial C \partial C$",
)
plt.xlabel(r"Number of input tensors $\longrightarrow$")
plt.ylabel(r"Runtime in s $\longrightarrow$")
plt.legend()
plt.tight_layout()
plt.savefig("benchmark_tensortrax_vs_autograd.svg", transparent=False)
plt.savefig("benchmark_tensortrax_vs_autograd.png", transparent=False)