brainstate 0.0.1__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- brainstate/__init__.py +45 -0
- brainstate/_module.py +1466 -0
- brainstate/_module_test.py +133 -0
- brainstate/_state.py +378 -0
- brainstate/_state_test.py +41 -0
- brainstate/_utils.py +21 -0
- brainstate/environ.py +375 -0
- brainstate/functional/__init__.py +25 -0
- brainstate/functional/_activations.py +754 -0
- brainstate/functional/_normalization.py +69 -0
- brainstate/functional/_spikes.py +90 -0
- brainstate/init/__init__.py +26 -0
- brainstate/init/_base.py +36 -0
- brainstate/init/_generic.py +175 -0
- brainstate/init/_random_inits.py +489 -0
- brainstate/init/_regular_inits.py +109 -0
- brainstate/math/__init__.py +21 -0
- brainstate/math/_einops.py +787 -0
- brainstate/math/_einops_parsing.py +169 -0
- brainstate/math/_einops_parsing_test.py +126 -0
- brainstate/math/_einops_test.py +346 -0
- brainstate/math/_misc.py +298 -0
- brainstate/math/_misc_test.py +58 -0
- brainstate/mixin.py +373 -0
- brainstate/mixin_test.py +73 -0
- brainstate/nn/__init__.py +68 -0
- brainstate/nn/_base.py +248 -0
- brainstate/nn/_connections.py +686 -0
- brainstate/nn/_dynamics.py +406 -0
- brainstate/nn/_elementwise.py +1437 -0
- brainstate/nn/_misc.py +132 -0
- brainstate/nn/_normalizations.py +389 -0
- brainstate/nn/_others.py +100 -0
- brainstate/nn/_poolings.py +1228 -0
- brainstate/nn/_poolings_test.py +231 -0
- brainstate/nn/_projection/__init__.py +32 -0
- brainstate/nn/_projection/_align_post.py +528 -0
- brainstate/nn/_projection/_align_pre.py +599 -0
- brainstate/nn/_projection/_delta.py +241 -0
- brainstate/nn/_projection/_utils.py +17 -0
- brainstate/nn/_projection/_vanilla.py +101 -0
- brainstate/nn/_rate_rnns.py +393 -0
- brainstate/nn/_readout.py +130 -0
- brainstate/nn/_synouts.py +166 -0
- brainstate/nn/functional/__init__.py +25 -0
- brainstate/nn/functional/_activations.py +754 -0
- brainstate/nn/functional/_normalization.py +69 -0
- brainstate/nn/functional/_spikes.py +90 -0
- brainstate/nn/init/__init__.py +26 -0
- brainstate/nn/init/_base.py +36 -0
- brainstate/nn/init/_generic.py +175 -0
- brainstate/nn/init/_random_inits.py +489 -0
- brainstate/nn/init/_regular_inits.py +109 -0
- brainstate/nn/surrogate.py +1740 -0
- brainstate/optim/__init__.py +23 -0
- brainstate/optim/_lr_scheduler.py +486 -0
- brainstate/optim/_lr_scheduler_test.py +36 -0
- brainstate/optim/_sgd_optimizer.py +1148 -0
- brainstate/random.py +5148 -0
- brainstate/random_test.py +576 -0
- brainstate/surrogate.py +1740 -0
- brainstate/transform/__init__.py +36 -0
- brainstate/transform/_autograd.py +585 -0
- brainstate/transform/_autograd_test.py +1183 -0
- brainstate/transform/_control.py +665 -0
- brainstate/transform/_controls_test.py +220 -0
- brainstate/transform/_jit.py +239 -0
- brainstate/transform/_jit_error.py +158 -0
- brainstate/transform/_jit_test.py +102 -0
- brainstate/transform/_make_jaxpr.py +573 -0
- brainstate/transform/_make_jaxpr_test.py +133 -0
- brainstate/transform/_progress_bar.py +113 -0
- brainstate/typing.py +69 -0
- brainstate/util.py +747 -0
- brainstate-0.0.1.dist-info/LICENSE +202 -0
- brainstate-0.0.1.dist-info/METADATA +101 -0
- brainstate-0.0.1.dist-info/RECORD +79 -0
- brainstate-0.0.1.dist-info/WHEEL +6 -0
- brainstate-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1183 @@
|
|
1
|
+
# Copyright 2024 BDP Ecosystem Limited. All Rights Reserved.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
# ==============================================================================
|
15
|
+
|
16
|
+
# -*- coding: utf-8 -*-
|
17
|
+
|
18
|
+
import unittest
|
19
|
+
from pprint import pprint
|
20
|
+
|
21
|
+
import jax
|
22
|
+
import jax.numpy as jnp
|
23
|
+
import pytest
|
24
|
+
|
25
|
+
import brainstate as bc
|
26
|
+
from brainstate.transform._autograd import _jacfwd
|
27
|
+
|
28
|
+
|
29
|
+
class TestPureFuncGrad(unittest.TestCase):
|
30
|
+
def test_grad_pure_func_1(self):
|
31
|
+
def call(a, b, c): return jnp.sum(a + b + c)
|
32
|
+
|
33
|
+
bc.random.seed(1)
|
34
|
+
a = jnp.ones(10)
|
35
|
+
b = bc.random.randn(10)
|
36
|
+
c = bc.random.uniform(size=10)
|
37
|
+
f_grad = bc.transform.grad(call, argnums=[0, 1, 2])
|
38
|
+
grads = f_grad(a, b, c)
|
39
|
+
|
40
|
+
for g in grads: assert (g == 1.).all()
|
41
|
+
|
42
|
+
def test_grad_pure_func_2(self):
|
43
|
+
def call(a, b, c): return jnp.sum(a + b + c)
|
44
|
+
|
45
|
+
bc.random.seed(1)
|
46
|
+
a = jnp.ones(10)
|
47
|
+
b = bc.random.randn(10)
|
48
|
+
c = bc.random.uniform(size=10)
|
49
|
+
f_grad = bc.transform.grad(call)
|
50
|
+
assert (f_grad(a, b, c) == 1.).all()
|
51
|
+
|
52
|
+
def test_grad_pure_func_aux1(self):
|
53
|
+
def call(a, b, c):
|
54
|
+
return jnp.sum(a + b + c), (jnp.sin(100), jnp.exp(0.1))
|
55
|
+
|
56
|
+
bc.random.seed(1)
|
57
|
+
f_grad = bc.transform.grad(call, argnums=[0, 1, 2])
|
58
|
+
with pytest.raises(TypeError):
|
59
|
+
f_grad(jnp.ones(10), bc.random.randn(10), bc.random.uniform(size=10))
|
60
|
+
|
61
|
+
def test_grad_pure_func_aux2(self):
|
62
|
+
def call(a, b, c):
|
63
|
+
return jnp.sum(a + b + c), (jnp.sin(100), jnp.exp(0.1))
|
64
|
+
|
65
|
+
bc.random.seed(1)
|
66
|
+
f_grad = bc.transform.grad(call, argnums=[0, 1, 2], has_aux=True)
|
67
|
+
grads, aux = f_grad(jnp.ones(10), bc.random.randn(10), bc.random.uniform(size=10))
|
68
|
+
for g in grads: assert (g == 1.).all()
|
69
|
+
assert aux[0] == jnp.sin(100)
|
70
|
+
assert aux[1] == jnp.exp(0.1)
|
71
|
+
|
72
|
+
def test_grad_pure_func_return1(self):
|
73
|
+
def call(a, b, c): return jnp.sum(a + b + c)
|
74
|
+
|
75
|
+
bc.random.seed(1)
|
76
|
+
a = jnp.ones(10)
|
77
|
+
b = bc.random.randn(10)
|
78
|
+
c = bc.random.uniform(size=10)
|
79
|
+
f_grad = bc.transform.grad(call, return_value=True)
|
80
|
+
grads, returns = f_grad(a, b, c)
|
81
|
+
assert (grads == 1.).all()
|
82
|
+
assert returns == jnp.sum(a + b + c)
|
83
|
+
|
84
|
+
def test_grad_func_return_aux1(self):
|
85
|
+
def call(a, b, c):
|
86
|
+
return jnp.sum(a + b + c), (jnp.sin(100), jnp.exp(0.1))
|
87
|
+
|
88
|
+
bc.random.seed(1)
|
89
|
+
a = jnp.ones(10)
|
90
|
+
b = bc.random.randn(10)
|
91
|
+
c = bc.random.uniform(size=10)
|
92
|
+
f_grad = bc.transform.grad(call, return_value=True, has_aux=True)
|
93
|
+
grads, returns, aux = f_grad(a, b, c)
|
94
|
+
assert (grads == 1.).all()
|
95
|
+
assert returns == jnp.sum(a + b + c)
|
96
|
+
assert aux[0] == jnp.sin(100)
|
97
|
+
assert aux[1] == jnp.exp(0.1)
|
98
|
+
|
99
|
+
|
100
|
+
class TestObjectFuncGrad(unittest.TestCase):
|
101
|
+
def test_grad_ob1(self):
|
102
|
+
class Test(bc.Module):
|
103
|
+
def __init__(self):
|
104
|
+
super(Test, self).__init__()
|
105
|
+
|
106
|
+
self.a = bc.ParamState(jnp.ones(10))
|
107
|
+
self.b = bc.ParamState(bc.random.randn(10))
|
108
|
+
self.c = bc.ParamState(bc.random.uniform(size=10))
|
109
|
+
|
110
|
+
def __call__(self):
|
111
|
+
return jnp.sum(self.a.value + self.b.value + self.c.value)
|
112
|
+
|
113
|
+
bc.random.seed(0)
|
114
|
+
|
115
|
+
t = Test()
|
116
|
+
f_grad = bc.transform.grad(t, grad_vars={'a': t.a, 'b': t.b, 'c': t.c})
|
117
|
+
grads = f_grad()
|
118
|
+
for g in grads.values():
|
119
|
+
assert (g == 1.).all()
|
120
|
+
|
121
|
+
t = Test()
|
122
|
+
f_grad = bc.transform.grad(t, grad_vars=[t.a, t.b])
|
123
|
+
grads = f_grad()
|
124
|
+
for g in grads: assert (g == 1.).all()
|
125
|
+
|
126
|
+
t = Test()
|
127
|
+
f_grad = bc.transform.grad(t, grad_vars=t.a)
|
128
|
+
grads = f_grad()
|
129
|
+
assert (grads == 1.).all()
|
130
|
+
|
131
|
+
def test_grad_ob_aux(self):
|
132
|
+
class Test(bc.Module):
|
133
|
+
def __init__(self):
|
134
|
+
super(Test, self).__init__()
|
135
|
+
self.a = bc.ParamState(jnp.ones(10))
|
136
|
+
self.b = bc.ParamState(bc.random.randn(10))
|
137
|
+
self.c = bc.ParamState(bc.random.uniform(size=10))
|
138
|
+
|
139
|
+
def __call__(self):
|
140
|
+
return jnp.sum(self.a.value + self.b.value + self.c.value), (jnp.sin(100), jnp.exp(0.1))
|
141
|
+
|
142
|
+
bc.random.seed(0)
|
143
|
+
t = Test()
|
144
|
+
f_grad = bc.transform.grad(t, grad_vars=[t.a, t.b], has_aux=True)
|
145
|
+
grads, aux = f_grad()
|
146
|
+
for g in grads: assert (g == 1.).all()
|
147
|
+
assert aux[0] == jnp.sin(100)
|
148
|
+
assert aux[1] == jnp.exp(0.1)
|
149
|
+
|
150
|
+
t = Test()
|
151
|
+
f_grad = bc.transform.grad(t, grad_vars=t.a, has_aux=True)
|
152
|
+
grads, aux = f_grad()
|
153
|
+
assert (grads == 1.).all()
|
154
|
+
assert aux[0] == jnp.sin(100)
|
155
|
+
assert aux[1] == jnp.exp(0.1)
|
156
|
+
|
157
|
+
def test_grad_ob_return(self):
|
158
|
+
class Test(bc.Module):
|
159
|
+
def __init__(self):
|
160
|
+
super(Test, self).__init__()
|
161
|
+
self.a = bc.ParamState(jnp.ones(10))
|
162
|
+
self.b = bc.ParamState(bc.random.randn(10))
|
163
|
+
self.c = bc.ParamState(bc.random.uniform(size=10))
|
164
|
+
|
165
|
+
def __call__(self):
|
166
|
+
return jnp.sum(self.a.value + self.b.value + self.c.value)
|
167
|
+
|
168
|
+
bc.random.seed(0)
|
169
|
+
t = Test()
|
170
|
+
f_grad = bc.transform.grad(t, grad_vars=[t.a, t.b], return_value=True)
|
171
|
+
grads, returns = f_grad()
|
172
|
+
for g in grads: assert (g == 1.).all()
|
173
|
+
assert returns == t()
|
174
|
+
|
175
|
+
t = Test()
|
176
|
+
f_grad = bc.transform.grad(t, grad_vars=t.a, return_value=True)
|
177
|
+
grads, returns = f_grad()
|
178
|
+
assert (grads == 1.).all()
|
179
|
+
assert returns == t()
|
180
|
+
|
181
|
+
def test_grad_ob_aux_return(self):
|
182
|
+
class Test(bc.Module):
|
183
|
+
def __init__(self):
|
184
|
+
super(Test, self).__init__()
|
185
|
+
self.a = bc.ParamState(jnp.ones(10))
|
186
|
+
self.b = bc.ParamState(bc.random.randn(10))
|
187
|
+
self.c = bc.ParamState(bc.random.uniform(size=10))
|
188
|
+
|
189
|
+
def __call__(self):
|
190
|
+
return jnp.sum(self.a.value + self.b.value + self.c.value), (jnp.sin(100), jnp.exp(0.1))
|
191
|
+
|
192
|
+
bc.random.seed(0)
|
193
|
+
t = Test()
|
194
|
+
f_grad = bc.transform.grad(t, grad_vars=[t.a, t.b], has_aux=True, return_value=True)
|
195
|
+
grads, returns, aux = f_grad()
|
196
|
+
for g in grads: assert (g == 1.).all()
|
197
|
+
assert returns == jnp.sum(t.a.value + t.b.value + t.c.value)
|
198
|
+
assert aux[0] == jnp.sin(100)
|
199
|
+
assert aux[1] == jnp.exp(0.1)
|
200
|
+
|
201
|
+
t = Test()
|
202
|
+
f_grad = bc.transform.grad(t, grad_vars=t.a, has_aux=True, return_value=True)
|
203
|
+
grads, returns, aux = f_grad()
|
204
|
+
assert (grads == 1.).all()
|
205
|
+
assert returns == jnp.sum(t.a.value + t.b.value + t.c.value)
|
206
|
+
assert aux[0] == jnp.sin(100)
|
207
|
+
assert aux[1] == jnp.exp(0.1)
|
208
|
+
|
209
|
+
def test_grad_ob_argnums(self):
|
210
|
+
class Test(bc.Module):
|
211
|
+
def __init__(self):
|
212
|
+
super(Test, self).__init__()
|
213
|
+
bc.random.seed()
|
214
|
+
self.a = bc.ParamState(jnp.ones(10))
|
215
|
+
self.b = bc.ParamState(bc.random.randn(10))
|
216
|
+
self.c = bc.ParamState(bc.random.uniform(size=10))
|
217
|
+
|
218
|
+
def __call__(self, d):
|
219
|
+
return jnp.sum(self.a.value + self.b.value + self.c.value + 2 * d)
|
220
|
+
|
221
|
+
bc.random.seed(0)
|
222
|
+
|
223
|
+
t = Test()
|
224
|
+
f_grad = bc.transform.grad(t, t.states(), argnums=0)
|
225
|
+
var_grads, arg_grads = f_grad(bc.random.random(10))
|
226
|
+
for g in var_grads.values(): assert (g == 1.).all()
|
227
|
+
assert (arg_grads == 2.).all()
|
228
|
+
|
229
|
+
t = Test()
|
230
|
+
f_grad = bc.transform.grad(t, t.states(), argnums=[0])
|
231
|
+
var_grads, arg_grads = f_grad(bc.random.random(10))
|
232
|
+
for g in var_grads.values(): assert (g == 1.).all()
|
233
|
+
assert (arg_grads[0] == 2.).all()
|
234
|
+
|
235
|
+
t = Test()
|
236
|
+
f_grad = bc.transform.grad(t, argnums=0)
|
237
|
+
arg_grads = f_grad(bc.random.random(10))
|
238
|
+
assert (arg_grads == 2.).all()
|
239
|
+
|
240
|
+
t = Test()
|
241
|
+
f_grad = bc.transform.grad(t, argnums=[0])
|
242
|
+
arg_grads = f_grad(bc.random.random(10))
|
243
|
+
assert (arg_grads[0] == 2.).all()
|
244
|
+
|
245
|
+
def test_grad_ob_argnums_aux(self):
|
246
|
+
class Test(bc.Module):
|
247
|
+
def __init__(self):
|
248
|
+
super(Test, self).__init__()
|
249
|
+
self.a = bc.ParamState(jnp.ones(10))
|
250
|
+
self.b = bc.ParamState(bc.random.randn(10))
|
251
|
+
self.c = bc.ParamState(bc.random.uniform(size=10))
|
252
|
+
|
253
|
+
def __call__(self, d):
|
254
|
+
return jnp.sum(self.a.value + self.b.value + self.c.value + 2 * d), (jnp.sin(100), jnp.exp(0.1))
|
255
|
+
|
256
|
+
bc.random.seed(0)
|
257
|
+
|
258
|
+
t = Test()
|
259
|
+
f_grad = bc.transform.grad(t, grad_vars=t.states(), argnums=0, has_aux=True)
|
260
|
+
(var_grads, arg_grads), aux = f_grad(bc.random.random(10))
|
261
|
+
for g in var_grads.values(): assert (g == 1.).all()
|
262
|
+
assert (arg_grads == 2.).all()
|
263
|
+
assert aux[0] == jnp.sin(100)
|
264
|
+
assert aux[1] == jnp.exp(0.1)
|
265
|
+
|
266
|
+
t = Test()
|
267
|
+
f_grad = bc.transform.grad(t, grad_vars=t.states(), argnums=[0], has_aux=True)
|
268
|
+
(var_grads, arg_grads), aux = f_grad(bc.random.random(10))
|
269
|
+
for g in var_grads.values(): assert (g == 1.).all()
|
270
|
+
assert (arg_grads[0] == 2.).all()
|
271
|
+
assert aux[0] == jnp.sin(100)
|
272
|
+
assert aux[1] == jnp.exp(0.1)
|
273
|
+
|
274
|
+
t = Test()
|
275
|
+
f_grad = bc.transform.grad(t, argnums=0, has_aux=True)
|
276
|
+
arg_grads, aux = f_grad(bc.random.random(10))
|
277
|
+
assert (arg_grads == 2.).all()
|
278
|
+
assert aux[0] == jnp.sin(100)
|
279
|
+
assert aux[1] == jnp.exp(0.1)
|
280
|
+
|
281
|
+
t = Test()
|
282
|
+
f_grad = bc.transform.grad(t, argnums=[0], has_aux=True)
|
283
|
+
arg_grads, aux = f_grad(bc.random.random(10))
|
284
|
+
assert (arg_grads[0] == 2.).all()
|
285
|
+
assert aux[0] == jnp.sin(100)
|
286
|
+
assert aux[1] == jnp.exp(0.1)
|
287
|
+
|
288
|
+
def test_grad_ob_argnums_return(self):
|
289
|
+
class Test(bc.Module):
|
290
|
+
def __init__(self):
|
291
|
+
super(Test, self).__init__()
|
292
|
+
|
293
|
+
self.a = bc.ParamState(jnp.ones(10))
|
294
|
+
self.b = bc.ParamState(bc.random.randn(10))
|
295
|
+
self.c = bc.ParamState(bc.random.uniform(size=10))
|
296
|
+
|
297
|
+
def __call__(self, d):
|
298
|
+
return jnp.sum(self.a.value + self.b.value + self.c.value + 2 * d)
|
299
|
+
|
300
|
+
bc.random.seed(0)
|
301
|
+
|
302
|
+
t = Test()
|
303
|
+
f_grad = bc.transform.grad(t, t.states(), argnums=0, return_value=True)
|
304
|
+
d = bc.random.random(10)
|
305
|
+
(var_grads, arg_grads), loss = f_grad(d)
|
306
|
+
for g in var_grads.values():
|
307
|
+
assert (g == 1.).all()
|
308
|
+
assert (arg_grads == 2.).all()
|
309
|
+
assert loss == t(d)
|
310
|
+
|
311
|
+
t = Test()
|
312
|
+
f_grad = bc.transform.grad(t, t.states(), argnums=[0], return_value=True)
|
313
|
+
d = bc.random.random(10)
|
314
|
+
(var_grads, arg_grads), loss = f_grad(d)
|
315
|
+
for g in var_grads.values():
|
316
|
+
assert (g == 1.).all()
|
317
|
+
assert (arg_grads[0] == 2.).all()
|
318
|
+
assert loss == t(d)
|
319
|
+
|
320
|
+
t = Test()
|
321
|
+
f_grad = bc.transform.grad(t, argnums=0, return_value=True)
|
322
|
+
d = bc.random.random(10)
|
323
|
+
arg_grads, loss = f_grad(d)
|
324
|
+
assert (arg_grads == 2.).all()
|
325
|
+
assert loss == t(d)
|
326
|
+
|
327
|
+
t = Test()
|
328
|
+
f_grad = bc.transform.grad(t, argnums=[0], return_value=True)
|
329
|
+
d = bc.random.random(10)
|
330
|
+
arg_grads, loss = f_grad(d)
|
331
|
+
assert (arg_grads[0] == 2.).all()
|
332
|
+
assert loss == t(d)
|
333
|
+
|
334
|
+
def test_grad_ob_argnums_aux_return(self):
|
335
|
+
class Test(bc.Module):
|
336
|
+
def __init__(self):
|
337
|
+
super(Test, self).__init__()
|
338
|
+
self.a = bc.ParamState(jnp.ones(10))
|
339
|
+
self.b = bc.ParamState(bc.random.randn(10))
|
340
|
+
self.c = bc.ParamState(bc.random.uniform(size=10))
|
341
|
+
|
342
|
+
def __call__(self, d):
|
343
|
+
return jnp.sum(self.a.value + self.b.value + self.c.value + 2 * d), (jnp.sin(100), jnp.exp(0.1))
|
344
|
+
|
345
|
+
bc.random.seed(0)
|
346
|
+
|
347
|
+
t = Test()
|
348
|
+
f_grad = bc.transform.grad(t, grad_vars=t.states(), argnums=0, has_aux=True, return_value=True)
|
349
|
+
d = bc.random.random(10)
|
350
|
+
(var_grads, arg_grads), loss, aux = f_grad(d)
|
351
|
+
for g in var_grads.values(): assert (g == 1.).all()
|
352
|
+
assert (arg_grads == 2.).all()
|
353
|
+
assert aux[0] == jnp.sin(100)
|
354
|
+
assert aux[1] == jnp.exp(0.1)
|
355
|
+
assert loss == t(d)[0]
|
356
|
+
|
357
|
+
t = Test()
|
358
|
+
f_grad = bc.transform.grad(t, grad_vars=t.states(), argnums=[0], has_aux=True, return_value=True)
|
359
|
+
d = bc.random.random(10)
|
360
|
+
(var_grads, arg_grads), loss, aux = f_grad(d)
|
361
|
+
for g in var_grads.values(): assert (g == 1.).all()
|
362
|
+
assert (arg_grads[0] == 2.).all()
|
363
|
+
assert aux[0] == jnp.sin(100)
|
364
|
+
assert aux[1] == jnp.exp(0.1)
|
365
|
+
assert loss == t(d)[0]
|
366
|
+
|
367
|
+
t = Test()
|
368
|
+
f_grad = bc.transform.grad(t, argnums=0, has_aux=True, return_value=True)
|
369
|
+
d = bc.random.random(10)
|
370
|
+
arg_grads, loss, aux = f_grad(d)
|
371
|
+
assert (arg_grads == 2.).all()
|
372
|
+
assert aux[0] == jnp.sin(100)
|
373
|
+
assert aux[1] == jnp.exp(0.1)
|
374
|
+
assert loss == t(d)[0]
|
375
|
+
|
376
|
+
t = Test()
|
377
|
+
f_grad = bc.transform.grad(t, argnums=[0], has_aux=True, return_value=True)
|
378
|
+
d = bc.random.random(10)
|
379
|
+
arg_grads, loss, aux = f_grad(d)
|
380
|
+
assert (arg_grads[0] == 2.).all()
|
381
|
+
assert aux[0] == jnp.sin(100)
|
382
|
+
assert aux[1] == jnp.exp(0.1)
|
383
|
+
assert loss == t(d)[0]
|
384
|
+
|
385
|
+
|
386
|
+
class TestPureFuncJacobian(unittest.TestCase):
|
387
|
+
def test1(self):
|
388
|
+
jac, aux = _jacfwd(lambda x: (x ** 3, [x ** 2]), has_aux=True)(3.)
|
389
|
+
self.assertTrue(jax.numpy.allclose(jac, jax.jacfwd(lambda x: x ** 3)(3.)))
|
390
|
+
self.assertTrue(aux[0] == 9.)
|
391
|
+
|
392
|
+
def test_jacfwd_and_aux_nested(self):
|
393
|
+
def f(x):
|
394
|
+
jac, aux = _jacfwd(lambda x: (x ** 3, [x ** 3]), has_aux=True)(x)
|
395
|
+
return aux[0]
|
396
|
+
|
397
|
+
f2 = lambda x: x ** 3
|
398
|
+
|
399
|
+
self.assertEqual(_jacfwd(f)(4.), _jacfwd(f2)(4.))
|
400
|
+
self.assertEqual(jax.jit(_jacfwd(f))(4.), _jacfwd(f2)(4.))
|
401
|
+
self.assertEqual(jax.jit(_jacfwd(jax.jit(f)))(4.), _jacfwd(f2)(4.))
|
402
|
+
|
403
|
+
self.assertEqual(_jacfwd(f)(jnp.asarray(4.)), _jacfwd(f2)(jnp.asarray(4.)))
|
404
|
+
self.assertEqual(jax.jit(_jacfwd(f))(jnp.asarray(4.)), _jacfwd(f2)(jnp.asarray(4.)))
|
405
|
+
self.assertEqual(jax.jit(_jacfwd(jax.jit(f)))(jnp.asarray(4.)), _jacfwd(f2)(jnp.asarray(4.)))
|
406
|
+
|
407
|
+
def f(x):
|
408
|
+
jac, aux = _jacfwd(lambda x: (x ** 3, [x ** 3]), has_aux=True)(x)
|
409
|
+
return aux[0] * jnp.sin(x)
|
410
|
+
|
411
|
+
f2 = lambda x: x ** 3 * jnp.sin(x)
|
412
|
+
|
413
|
+
self.assertEqual(_jacfwd(f)(4.), _jacfwd(f2)(4.))
|
414
|
+
self.assertEqual(jax.jit(_jacfwd(f))(4.), _jacfwd(f2)(4.))
|
415
|
+
self.assertEqual(jax.jit(_jacfwd(jax.jit(f)))(4.), _jacfwd(f2)(4.))
|
416
|
+
|
417
|
+
self.assertEqual(_jacfwd(f)(jnp.asarray(4.)), _jacfwd(f2)(jnp.asarray(4.)))
|
418
|
+
self.assertEqual(jax.jit(_jacfwd(f))(jnp.asarray(4.)), _jacfwd(f2)(jnp.asarray(4.)))
|
419
|
+
self.assertEqual(jax.jit(_jacfwd(jax.jit(f)))(jnp.asarray(4.)), _jacfwd(f2)(jnp.asarray(4.)))
|
420
|
+
|
421
|
+
def test_jacrev1(self):
|
422
|
+
def f1(x, y):
|
423
|
+
r = jnp.asarray([x[0] * y[0], 5 * x[2] * y[1], 4 * x[1] ** 2 - 2 * x[2], x[2] * jnp.sin(x[0])])
|
424
|
+
return r
|
425
|
+
|
426
|
+
br = bc.transform.jacrev(f1)(jnp.array([1., 2., 3.]), jnp.array([10., 5.]))
|
427
|
+
jr = jax.jacrev(f1)(jnp.array([1., 2., 3.]), jnp.array([10., 5.]))
|
428
|
+
assert (br == jr).all()
|
429
|
+
|
430
|
+
br = bc.transform.jacrev(f1, argnums=(0, 1))(jnp.array([1., 2., 3.]), jnp.array([10., 5.]))
|
431
|
+
jr = jax.jacrev(f1, argnums=(0, 1))(jnp.array([1., 2., 3.]), jnp.array([10., 5.]))
|
432
|
+
assert (br[0] == jr[0]).all()
|
433
|
+
assert (br[1] == jr[1]).all()
|
434
|
+
|
435
|
+
def test_jacrev2(self):
|
436
|
+
print()
|
437
|
+
|
438
|
+
def f2(x, y):
|
439
|
+
r1 = jnp.asarray([x[0] * y[0], 5 * x[2] * y[1]])
|
440
|
+
r2 = jnp.asarray([4 * x[1] ** 2 - 2 * x[2], x[2] * jnp.sin(x[0])])
|
441
|
+
return r1, r2
|
442
|
+
|
443
|
+
jr = jax.jacrev(f2)(jnp.array([1., 2., 3.]), jnp.array([10., 5.]))
|
444
|
+
pprint(jr)
|
445
|
+
|
446
|
+
br = bc.transform.jacrev(f2)(jnp.array([1., 2., 3.]), jnp.array([10., 5.]))
|
447
|
+
pprint(br)
|
448
|
+
assert jnp.array_equal(br[0], jr[0])
|
449
|
+
assert jnp.array_equal(br[1], jr[1])
|
450
|
+
|
451
|
+
br = bc.transform.jacrev(f2)(jnp.array([1., 2., 3.]), jnp.array([10., 5.]))
|
452
|
+
pprint(br)
|
453
|
+
assert jnp.array_equal(br[0], jr[0])
|
454
|
+
assert jnp.array_equal(br[1], jr[1])
|
455
|
+
|
456
|
+
def f2(x, y):
|
457
|
+
r1 = jnp.asarray([x[0] * y[0], 5 * x[2] * y[1]])
|
458
|
+
r2 = jnp.asarray([4 * x[1] ** 2 - 2 * x[2], x[2] * jnp.sin(x[0])])
|
459
|
+
return r1, r2
|
460
|
+
|
461
|
+
br = bc.transform.jacrev(f2)(jnp.array([1., 2., 3.]), jnp.array([10., 5.]))
|
462
|
+
pprint(br)
|
463
|
+
assert jnp.array_equal(br[0], jr[0])
|
464
|
+
assert jnp.array_equal(br[1], jr[1])
|
465
|
+
|
466
|
+
br = bc.transform.jacrev(f2)(jnp.array([1., 2., 3.]), jnp.array([10., 5.]))
|
467
|
+
pprint(br)
|
468
|
+
assert jnp.array_equal(br[0], jr[0])
|
469
|
+
assert jnp.array_equal(br[1], jr[1])
|
470
|
+
|
471
|
+
def test_jacrev3(self):
|
472
|
+
print()
|
473
|
+
|
474
|
+
def f3(x, y):
|
475
|
+
r1 = jnp.asarray([x[0] * y[0], 5 * x[2] * y[1]])
|
476
|
+
r2 = jnp.asarray([4 * x[1] ** 2 - 2 * x[2], x[2] * jnp.sin(x[0])])
|
477
|
+
return r1, r2
|
478
|
+
|
479
|
+
jr = jax.jacrev(f3, argnums=(0, 1))(jnp.array([1., 2., 3.]), jnp.array([10., 5.]))
|
480
|
+
pprint(jr)
|
481
|
+
|
482
|
+
br = bc.transform.jacrev(f3, argnums=(0, 1))(jnp.array([1., 2., 3.]), jnp.array([10., 5.]))
|
483
|
+
pprint(br)
|
484
|
+
assert jnp.array_equal(br[0][0], jr[0][0])
|
485
|
+
assert jnp.array_equal(br[0][1], jr[0][1])
|
486
|
+
assert jnp.array_equal(br[1][0], jr[1][0])
|
487
|
+
assert jnp.array_equal(br[1][1], jr[1][1])
|
488
|
+
|
489
|
+
br = bc.transform.jacrev(f3, argnums=(0, 1))(jnp.array([1., 2., 3.]), jnp.array([10., 5.]))
|
490
|
+
pprint(br)
|
491
|
+
assert jnp.array_equal(br[0][0], jr[0][0])
|
492
|
+
assert jnp.array_equal(br[0][1], jr[0][1])
|
493
|
+
assert jnp.array_equal(br[1][0], jr[1][0])
|
494
|
+
assert jnp.array_equal(br[1][1], jr[1][1])
|
495
|
+
|
496
|
+
def f3(x, y):
|
497
|
+
r1 = jnp.asarray([x[0] * y[0], 5 * x[2] * y[1]])
|
498
|
+
r2 = jnp.asarray([4 * x[1] ** 2 - 2 * x[2], x[2] * jnp.sin(x[0])])
|
499
|
+
return r1, r2
|
500
|
+
|
501
|
+
br = bc.transform.jacrev(f3, argnums=(0, 1))(jnp.array([1., 2., 3.]), jnp.array([10., 5.]))
|
502
|
+
pprint(br)
|
503
|
+
assert jnp.array_equal(br[0][0], jr[0][0])
|
504
|
+
assert jnp.array_equal(br[0][1], jr[0][1])
|
505
|
+
assert jnp.array_equal(br[1][0], jr[1][0])
|
506
|
+
assert jnp.array_equal(br[1][1], jr[1][1])
|
507
|
+
|
508
|
+
br = bc.transform.jacrev(f3, argnums=(0, 1))(jnp.array([1., 2., 3.]), jnp.array([10., 5.]))
|
509
|
+
pprint(br)
|
510
|
+
assert jnp.array_equal(br[0][0], jr[0][0])
|
511
|
+
assert jnp.array_equal(br[0][1], jr[0][1])
|
512
|
+
assert jnp.array_equal(br[1][0], jr[1][0])
|
513
|
+
assert jnp.array_equal(br[1][1], jr[1][1])
|
514
|
+
|
515
|
+
def test_jacrev_aux1(self):
|
516
|
+
x = jnp.array([1., 2., 3.])
|
517
|
+
y = jnp.array([10., 5.])
|
518
|
+
|
519
|
+
def f1(x, y):
|
520
|
+
a = 4 * x[1] ** 2 - 2 * x[2]
|
521
|
+
r = jnp.asarray([x[0] * y[0], 5 * x[2] * y[1], a, x[2] * jnp.sin(x[0])])
|
522
|
+
return r, a
|
523
|
+
|
524
|
+
f2 = lambda *args: f1(*args)[0]
|
525
|
+
jr = jax.jacrev(f2)(x, y) # jax jacobian
|
526
|
+
pprint(jr)
|
527
|
+
grads, aux = bc.transform.jacrev(f1, has_aux=True)(x, y)
|
528
|
+
assert (grads == jr).all()
|
529
|
+
assert aux == (4 * x[1] ** 2 - 2 * x[2])
|
530
|
+
|
531
|
+
jr = jax.jacrev(f2, argnums=(0, 1))(x, y) # jax jacobian
|
532
|
+
pprint(jr)
|
533
|
+
grads, aux = bc.transform.jacrev(f1, argnums=(0, 1), has_aux=True)(x, y)
|
534
|
+
assert (grads[0] == jr[0]).all()
|
535
|
+
assert (grads[1] == jr[1]).all()
|
536
|
+
assert aux == (4 * x[1] ** 2 - 2 * x[2])
|
537
|
+
|
538
|
+
def test_jacrev_return_aux1(self):
|
539
|
+
with bc.environ.context(precision=64):
|
540
|
+
|
541
|
+
def f1(x, y):
|
542
|
+
a = 4 * x[1] ** 2 - 2 * x[2]
|
543
|
+
r = jnp.asarray([x[0] * y[0], 5 * x[2] * y[1], a, x[2] * jnp.sin(x[0])])
|
544
|
+
return r, a
|
545
|
+
|
546
|
+
_x = jnp.array([1., 2., 3.])
|
547
|
+
_y = jnp.array([10., 5.])
|
548
|
+
_r, _a = f1(_x, _y)
|
549
|
+
f2 = lambda *args: f1(*args)[0]
|
550
|
+
_g1 = jax.jacrev(f2)(_x, _y) # jax jacobian
|
551
|
+
pprint(_g1)
|
552
|
+
_g2 = jax.jacrev(f2, argnums=(0, 1))(_x, _y) # jax jacobian
|
553
|
+
pprint(_g2)
|
554
|
+
|
555
|
+
grads, vec, aux = bc.transform.jacrev(f1, return_value=True, has_aux=True)(_x, _y)
|
556
|
+
assert (grads == _g1).all()
|
557
|
+
assert aux == _a
|
558
|
+
assert (vec == _r).all()
|
559
|
+
|
560
|
+
grads, vec, aux = bc.transform.jacrev(f1, return_value=True, argnums=(0, 1), has_aux=True)(_x, _y)
|
561
|
+
assert (grads[0] == _g2[0]).all()
|
562
|
+
assert (grads[1] == _g2[1]).all()
|
563
|
+
assert aux == _a
|
564
|
+
assert (vec == _r).all()
|
565
|
+
|
566
|
+
|
567
|
+
|
568
|
+
class TestClassFuncJacobian(unittest.TestCase):
|
569
|
+
def test_jacrev1(self):
|
570
|
+
def f1(x, y):
|
571
|
+
r = jnp.asarray([x[0] * y[0], 5 * x[2] * y[1], 4 * x[1] ** 2 - 2 * x[2], x[2] * jnp.sin(x[0])])
|
572
|
+
return r
|
573
|
+
|
574
|
+
_x = jnp.array([1., 2., 3.])
|
575
|
+
_y = jnp.array([10., 5.])
|
576
|
+
|
577
|
+
class Test(bc.Module):
|
578
|
+
def __init__(self):
|
579
|
+
super(Test, self).__init__()
|
580
|
+
self.x = bc.State(jnp.array([1., 2., 3.]))
|
581
|
+
self.y = bc.State(jnp.array([10., 5.]))
|
582
|
+
|
583
|
+
def __call__(self, ):
|
584
|
+
a = self.x.value[0] * self.y.value[0]
|
585
|
+
b = 5 * self.x.value[2] * self.y.value[1]
|
586
|
+
c = 4 * self.x.value[1] ** 2 - 2 * self.x.value[2]
|
587
|
+
d = self.x.value[2] * jnp.sin(self.x.value[0])
|
588
|
+
r = jnp.asarray([a, b, c, d])
|
589
|
+
return r
|
590
|
+
|
591
|
+
_jr = jax.jacrev(f1)(_x, _y)
|
592
|
+
t = Test()
|
593
|
+
br = bc.transform.jacrev(t, grad_vars=t.x)()
|
594
|
+
self.assertTrue((br == _jr).all())
|
595
|
+
|
596
|
+
_jr = jax.jacrev(f1, argnums=(0, 1))(_x, _y)
|
597
|
+
t = Test()
|
598
|
+
br = bc.transform.jacrev(t, grad_vars=[t.x, t.y])()
|
599
|
+
self.assertTrue((br[0] == _jr[0]).all())
|
600
|
+
self.assertTrue((br[1] == _jr[1]).all())
|
601
|
+
#
|
602
|
+
# def test_jacfwd1(self):
|
603
|
+
# def f1(x, y):
|
604
|
+
# r = jnp.asarray([x[0] * y[0], 5 * x[2] * y[1], 4 * x[1] ** 2 - 2 * x[2], x[2] * jnp.sin(x[0])])
|
605
|
+
# return r
|
606
|
+
#
|
607
|
+
# _x = jnp.array([1., 2., 3.])
|
608
|
+
# _y = jnp.array([10., 5.])
|
609
|
+
#
|
610
|
+
# class Test(bst.Module):
|
611
|
+
# def __init__(self):
|
612
|
+
# super(Test, self).__init__()
|
613
|
+
# self.x = jnp.Variable(jnp.array([1., 2., 3.]))
|
614
|
+
# self.y = jnp.Variable(jnp.array([10., 5.]))
|
615
|
+
#
|
616
|
+
# def __call__(self, ):
|
617
|
+
# a = self.x[0] * self.y[0]
|
618
|
+
# b = 5 * self.x[2] * self.y[1]
|
619
|
+
# c = 4 * self.x[1] ** 2 - 2 * self.x[2]
|
620
|
+
# d = self.x[2] * jnp.sin(self.x[0])
|
621
|
+
# r = jnp.asarray([a, b, c, d])
|
622
|
+
# return r
|
623
|
+
#
|
624
|
+
# _jr = jax.jacfwd(f1)(_x, _y)
|
625
|
+
# t = Test()
|
626
|
+
# br = bst.transform.jacfwd(t, grad_vars=t.x)()
|
627
|
+
# self.assertTrue((br == _jr).all())
|
628
|
+
#
|
629
|
+
# _jr = jax.jacfwd(f1, argnums=(0, 1))(_x, _y)
|
630
|
+
# t = Test()
|
631
|
+
# br = bst.transform.jacfwd(t, grad_vars=[t.x, t.y])()
|
632
|
+
# self.assertTrue((br[0] == _jr[0]).all())
|
633
|
+
# self.assertTrue((br[1] == _jr[1]).all())
|
634
|
+
#
|
635
|
+
# def test_jacrev2(self):
|
636
|
+
# def f1(x, y):
|
637
|
+
# r = jnp.asarray([x[0] * y[0], 5 * x[2] * y[1], 4 * x[1] ** 2 - 2 * x[2], x[2] * jnp.sin(x[0])])
|
638
|
+
# return r
|
639
|
+
#
|
640
|
+
# _x = jnp.array([1., 2., 3.])
|
641
|
+
# _y = jnp.array([10., 5.])
|
642
|
+
#
|
643
|
+
# class Test(bst.Module):
|
644
|
+
# def __init__(self):
|
645
|
+
# super(Test, self).__init__()
|
646
|
+
# self.x = jnp.Variable(jnp.array([1., 2., 3.]))
|
647
|
+
#
|
648
|
+
# def __call__(self, y):
|
649
|
+
# a = self.x[0] * y[0]
|
650
|
+
# b = 5 * self.x[2] * y[1]
|
651
|
+
# c = 4 * self.x[1] ** 2 - 2 * self.x[2]
|
652
|
+
# d = self.x[2] * jnp.sin(self.x[0])
|
653
|
+
# r = jnp.asarray([a, b, c, d])
|
654
|
+
# return r
|
655
|
+
#
|
656
|
+
# _jr = jax.jacrev(f1)(_x, _y)
|
657
|
+
# t = Test()
|
658
|
+
# br = bst.transform.jacrev(t, grad_vars=t.x)(_y)
|
659
|
+
# self.assertTrue((br == _jr).all())
|
660
|
+
#
|
661
|
+
# _jr = jax.jacrev(f1, argnums=(0, 1))(_x, _y)
|
662
|
+
# t = Test()
|
663
|
+
# var_grads, arg_grads = bst.transform.jacrev(t, grad_vars=t.x, argnums=0)(_y)
|
664
|
+
# print(var_grads, )
|
665
|
+
# print(arg_grads, )
|
666
|
+
# self.assertTrue((var_grads == _jr[0]).all())
|
667
|
+
# self.assertTrue((arg_grads == _jr[1]).all())
|
668
|
+
#
|
669
|
+
# def test_jacfwd2(self):
|
670
|
+
# def f1(x, y):
|
671
|
+
# r = jnp.asarray([x[0] * y[0], 5 * x[2] * y[1], 4 * x[1] ** 2 - 2 * x[2], x[2] * jnp.sin(x[0])])
|
672
|
+
# return r
|
673
|
+
#
|
674
|
+
# _x = jnp.array([1., 2., 3.])
|
675
|
+
# _y = jnp.array([10., 5.])
|
676
|
+
#
|
677
|
+
# class Test(bst.Module):
|
678
|
+
# def __init__(self):
|
679
|
+
# super(Test, self).__init__()
|
680
|
+
# self.x = jnp.Variable(jnp.array([1., 2., 3.]))
|
681
|
+
#
|
682
|
+
# def __call__(self, y):
|
683
|
+
# a = self.x[0] * y[0]
|
684
|
+
# b = 5 * self.x[2] * y[1]
|
685
|
+
# c = 4 * self.x[1] ** 2 - 2 * self.x[2]
|
686
|
+
# d = self.x[2] * jnp.sin(self.x[0])
|
687
|
+
# r = jnp.asarray([a, b, c, d])
|
688
|
+
# return r
|
689
|
+
#
|
690
|
+
# _jr = jax.jacfwd(f1)(_x, _y)
|
691
|
+
# t = Test()
|
692
|
+
# br = bst.transform.jacfwd(t, grad_vars=t.x)(_y)
|
693
|
+
# self.assertTrue((br == _jr).all())
|
694
|
+
#
|
695
|
+
# _jr = jax.jacfwd(f1, argnums=(0, 1))(_x, _y)
|
696
|
+
# t = Test()
|
697
|
+
# var_grads, arg_grads = bst.transform.jacfwd(t, grad_vars=t.x, argnums=0)(_y)
|
698
|
+
# print(var_grads, )
|
699
|
+
# print(arg_grads, )
|
700
|
+
# self.assertTrue((var_grads == _jr[0]).all())
|
701
|
+
# self.assertTrue((arg_grads == _jr[1]).all())
|
702
|
+
#
|
703
|
+
# def test_jacrev_aux1(self):
|
704
|
+
# jnp.enable_x64()
|
705
|
+
#
|
706
|
+
# def f1(x, y):
|
707
|
+
# r = jnp.asarray([x[0] * y[0], 5 * x[2] * y[1], 4 * x[1] ** 2 - 2 * x[2], x[2] * jnp.sin(x[0])])
|
708
|
+
# return r
|
709
|
+
#
|
710
|
+
# _x = jnp.array([1., 2., 3.])
|
711
|
+
# _y = jnp.array([10., 5.])
|
712
|
+
#
|
713
|
+
# class Test(bst.Module):
|
714
|
+
# def __init__(self):
|
715
|
+
# super(Test, self).__init__()
|
716
|
+
# self.x = jnp.Variable(jnp.array([1., 2., 3.]))
|
717
|
+
#
|
718
|
+
# def __call__(self, y):
|
719
|
+
# a = self.x[0] * y[0]
|
720
|
+
# b = 5 * self.x[2] * y[1]
|
721
|
+
# c = 4 * self.x[1] ** 2 - 2 * self.x[2]
|
722
|
+
# d = self.x[2] * jnp.sin(self.x[0])
|
723
|
+
# r = jnp.asarray([a, b, c, d])
|
724
|
+
# return r, (c, d)
|
725
|
+
#
|
726
|
+
# _jr = jax.jacrev(f1)(_x, _y)
|
727
|
+
# t = Test()
|
728
|
+
# br, _ = bst.transform.jacrev(t, grad_vars=t.x, has_aux=True)(_y)
|
729
|
+
# self.assertTrue((br == _jr).all())
|
730
|
+
#
|
731
|
+
# t = Test()
|
732
|
+
# _jr = jax.jacrev(f1, argnums=(0, 1))(_x, _y)
|
733
|
+
# _aux = t(_y)[1]
|
734
|
+
# (var_grads, arg_grads), aux = bst.transform.jacrev(t, grad_vars=t.x, argnums=0, has_aux=True)(_y)
|
735
|
+
# print(var_grads, )
|
736
|
+
# print(arg_grads, )
|
737
|
+
# self.assertTrue((var_grads == _jr[0]).all())
|
738
|
+
# self.assertTrue((arg_grads == _jr[1]).all())
|
739
|
+
# self.assertTrue(jnp.array_equal(aux, _aux))
|
740
|
+
#
|
741
|
+
# jnp.disable_x64()
|
742
|
+
#
|
743
|
+
# def test_jacfwd_aux1(self):
|
744
|
+
# jnp.enable_x64()
|
745
|
+
#
|
746
|
+
# def f1(x, y):
|
747
|
+
# r = jnp.asarray([x[0] * y[0], 5 * x[2] * y[1], 4 * x[1] ** 2 - 2 * x[2], x[2] * jnp.sin(x[0])])
|
748
|
+
# return r
|
749
|
+
#
|
750
|
+
# _x = jnp.array([1., 2., 3.])
|
751
|
+
# _y = jnp.array([10., 5.])
|
752
|
+
#
|
753
|
+
# class Test(bst.Module):
|
754
|
+
# def __init__(self):
|
755
|
+
# super(Test, self).__init__()
|
756
|
+
# self.x = jnp.Variable(jnp.array([1., 2., 3.]))
|
757
|
+
#
|
758
|
+
# def __call__(self, y):
|
759
|
+
# a = self.x[0] * y[0]
|
760
|
+
# b = 5 * self.x[2] * y[1]
|
761
|
+
# c = 4 * self.x[1] ** 2 - 2 * self.x[2]
|
762
|
+
# d = self.x[2] * jnp.sin(self.x[0])
|
763
|
+
# r = jnp.asarray([a, b, c, d])
|
764
|
+
# return r, (c, d)
|
765
|
+
#
|
766
|
+
# _jr = jax.jacfwd(f1)(_x, _y)
|
767
|
+
# t = Test()
|
768
|
+
# br, (c, d) = bst.transform.jacfwd(t, grad_vars=t.x, has_aux=True)(_y)
|
769
|
+
# # print(_jr)
|
770
|
+
# # print(br)
|
771
|
+
# a = (br == _jr)
|
772
|
+
# self.assertTrue(a.all())
|
773
|
+
#
|
774
|
+
# t = Test()
|
775
|
+
# _jr = jax.jacfwd(f1, argnums=(0, 1))(_x, _y)
|
776
|
+
# _aux = t(_y)[1]
|
777
|
+
# (var_grads, arg_grads), aux = bst.transform.jacfwd(t, grad_vars=t.x, argnums=0, has_aux=True)(_y)
|
778
|
+
# print(var_grads, )
|
779
|
+
# print(arg_grads, )
|
780
|
+
# self.assertTrue((var_grads == _jr[0]).all())
|
781
|
+
# self.assertTrue((arg_grads == _jr[1]).all())
|
782
|
+
# self.assertTrue(jnp.array_equal(aux, _aux))
|
783
|
+
#
|
784
|
+
# jnp.disable_x64()
|
785
|
+
#
|
786
|
+
# def test_jacrev_return_aux1(self):
|
787
|
+
# jnp.enable_x64()
|
788
|
+
#
|
789
|
+
# def f1(x, y):
|
790
|
+
# r = jnp.asarray([x[0] * y[0], 5 * x[2] * y[1], 4 * x[1] ** 2 - 2 * x[2], x[2] * jnp.sin(x[0])])
|
791
|
+
# return r
|
792
|
+
#
|
793
|
+
# _x = jnp.array([1., 2., 3.])
|
794
|
+
# _y = jnp.array([10., 5.])
|
795
|
+
#
|
796
|
+
# class Test(bst.Module):
|
797
|
+
# def __init__(self):
|
798
|
+
# super(Test, self).__init__()
|
799
|
+
# self.x = jnp.Variable(jnp.array([1., 2., 3.]))
|
800
|
+
#
|
801
|
+
# def __call__(self, y):
|
802
|
+
# a = self.x[0] * y[0]
|
803
|
+
# b = 5 * self.x[2] * y[1]
|
804
|
+
# c = 4 * self.x[1] ** 2 - 2 * self.x[2]
|
805
|
+
# d = self.x[2] * jnp.sin(self.x[0])
|
806
|
+
# r = jnp.asarray([a, b, c, d])
|
807
|
+
# return r, (c, d)
|
808
|
+
#
|
809
|
+
# _jr = jax.jacrev(f1)(_x, _y)
|
810
|
+
# t = Test()
|
811
|
+
# br, _ = bst.transform.jacrev(t, grad_vars=t.x, has_aux=True)(_y)
|
812
|
+
# self.assertTrue((br == _jr).all())
|
813
|
+
#
|
814
|
+
# t = Test()
|
815
|
+
# _jr = jax.jacrev(f1, argnums=(0, 1))(_x, _y)
|
816
|
+
# _val, _aux = t(_y)
|
817
|
+
# (var_grads, arg_grads), value, aux = bst.transform.jacrev(t, grad_vars=t.x, argnums=0, has_aux=True, return_value=True)(_y)
|
818
|
+
# print(var_grads, )
|
819
|
+
# print(arg_grads, )
|
820
|
+
# self.assertTrue((var_grads == _jr[0]).all())
|
821
|
+
# self.assertTrue((arg_grads == _jr[1]).all())
|
822
|
+
# self.assertTrue(jnp.array_equal(aux, _aux))
|
823
|
+
# self.assertTrue(jnp.array_equal(value, _val))
|
824
|
+
#
|
825
|
+
# jnp.disable_x64()
|
826
|
+
#
|
827
|
+
# def test_jacfwd_return_aux1(self):
|
828
|
+
# jnp.enable_x64()
|
829
|
+
#
|
830
|
+
# def f1(x, y):
|
831
|
+
# r = jnp.asarray([x[0] * y[0], 5 * x[2] * y[1], 4 * x[1] ** 2 - 2 * x[2], x[2] * jnp.sin(x[0])])
|
832
|
+
# return r
|
833
|
+
#
|
834
|
+
# _x = jnp.array([1., 2., 3.])
|
835
|
+
# _y = jnp.array([10., 5.])
|
836
|
+
#
|
837
|
+
# class Test(bst.Module):
|
838
|
+
# def __init__(self):
|
839
|
+
# super(Test, self).__init__()
|
840
|
+
# self.x = jnp.Variable(jnp.array([1., 2., 3.]))
|
841
|
+
#
|
842
|
+
# def __call__(self, y):
|
843
|
+
# a = self.x[0] * y[0]
|
844
|
+
# b = 5 * self.x[2] * y[1]
|
845
|
+
# c = 4 * self.x[1] ** 2 - 2 * self.x[2]
|
846
|
+
# d = self.x[2] * jnp.sin(self.x[0])
|
847
|
+
# r = jnp.asarray([a, b, c, d])
|
848
|
+
# return r, (c, d)
|
849
|
+
#
|
850
|
+
# _jr = jax.jacfwd(f1)(_x, _y)
|
851
|
+
# t = Test()
|
852
|
+
# br, _ = bst.transform.jacfwd(t, grad_vars=t.x, has_aux=True)(_y)
|
853
|
+
# self.assertTrue((br == _jr).all())
|
854
|
+
#
|
855
|
+
# t = Test()
|
856
|
+
# _jr = jax.jacfwd(f1, argnums=(0, 1))(_x, _y)
|
857
|
+
# _val, _aux = t(_y)
|
858
|
+
# (var_grads, arg_grads), value, aux = bst.transform.jacfwd(t, grad_vars=t.x, argnums=0, has_aux=True, return_value=True)(_y)
|
859
|
+
# print(_val, )
|
860
|
+
# print('_aux: ', _aux, 'aux: ', aux)
|
861
|
+
# print(var_grads, )
|
862
|
+
# print(arg_grads, )
|
863
|
+
# self.assertTrue((var_grads == _jr[0]).all())
|
864
|
+
# self.assertTrue((arg_grads == _jr[1]).all())
|
865
|
+
# self.assertTrue(jnp.array_equal(aux, _aux))
|
866
|
+
# self.assertTrue(jnp.array_equal(value, _val))
|
867
|
+
#
|
868
|
+
# jnp.disable_x64()
|
869
|
+
#
|
870
|
+
#
|
871
|
+
# class TestPureFuncVectorGrad(unittest.TestCase):
|
872
|
+
# def test1(self):
|
873
|
+
# f = lambda x: 3 * x ** 2
|
874
|
+
# _x = jnp.ones(10)
|
875
|
+
# pprint(bst.transform.vector_grad(f, argnums=0)(_x))
|
876
|
+
#
|
877
|
+
# def test2(self):
|
878
|
+
# def f(x, y):
|
879
|
+
# dx = x ** 2 + y ** 2 + 10
|
880
|
+
# return dx
|
881
|
+
#
|
882
|
+
# _x = jnp.ones(5)
|
883
|
+
# _y = jnp.ones(5)
|
884
|
+
#
|
885
|
+
# g = bst.transform.vector_grad(f, argnums=0)(_x, _y)
|
886
|
+
# pprint(g)
|
887
|
+
# self.assertTrue(jnp.array_equal(g, 2 * _x))
|
888
|
+
#
|
889
|
+
# g = bst.transform.vector_grad(f, argnums=(0,))(_x, _y)
|
890
|
+
# self.assertTrue(jnp.array_equal(g[0], 2 * _x))
|
891
|
+
#
|
892
|
+
# g = bst.transform.vector_grad(f, argnums=(0, 1))(_x, _y)
|
893
|
+
# pprint(g)
|
894
|
+
# self.assertTrue(jnp.array_equal(g[0], 2 * _x))
|
895
|
+
# self.assertTrue(jnp.array_equal(g[1], 2 * _y))
|
896
|
+
#
|
897
|
+
# def test3(self):
|
898
|
+
# def f(x, y):
|
899
|
+
# dx = x ** 2 + y ** 2 + 10
|
900
|
+
# dy = x ** 3 + y ** 3 - 10
|
901
|
+
# return dx, dy
|
902
|
+
#
|
903
|
+
# _x = jnp.ones(5)
|
904
|
+
# _y = jnp.ones(5)
|
905
|
+
#
|
906
|
+
# g = bst.transform.vector_grad(f, argnums=0)(_x, _y)
|
907
|
+
# # pprint(g)
|
908
|
+
# self.assertTrue(jnp.array_equal(g, 2 * _x + 3 * _x ** 2))
|
909
|
+
#
|
910
|
+
# g = bst.transform.vector_grad(f, argnums=(0,))(_x, _y)
|
911
|
+
# self.assertTrue(jnp.array_equal(g[0], 2 * _x + 3 * _x ** 2))
|
912
|
+
#
|
913
|
+
# g = bst.transform.vector_grad(f, argnums=(0, 1))(_x, _y)
|
914
|
+
# # pprint(g)
|
915
|
+
# self.assertTrue(jnp.array_equal(g[0], 2 * _x + 3 * _x ** 2))
|
916
|
+
# self.assertTrue(jnp.array_equal(g[1], 2 * _y + 3 * _y ** 2))
|
917
|
+
#
|
918
|
+
# def test4_2d(self):
|
919
|
+
# def f(x, y):
|
920
|
+
# dx = x ** 2 + y ** 2 + 10
|
921
|
+
# return dx
|
922
|
+
#
|
923
|
+
# _x = jnp.ones((5, 5))
|
924
|
+
# _y = jnp.ones((5, 5))
|
925
|
+
#
|
926
|
+
# g = bst.transform.vector_grad(f, argnums=0)(_x, _y)
|
927
|
+
# pprint(g)
|
928
|
+
# self.assertTrue(jnp.array_equal(g, 2 * _x))
|
929
|
+
#
|
930
|
+
# g = bst.transform.vector_grad(f, argnums=(0,))(_x, _y)
|
931
|
+
# self.assertTrue(jnp.array_equal(g[0], 2 * _x))
|
932
|
+
#
|
933
|
+
# g = bst.transform.vector_grad(f, argnums=(0, 1))(_x, _y)
|
934
|
+
# pprint(g)
|
935
|
+
# self.assertTrue(jnp.array_equal(g[0], 2 * _x))
|
936
|
+
# self.assertTrue(jnp.array_equal(g[1], 2 * _y))
|
937
|
+
#
|
938
|
+
# def test_aux1(self):
|
939
|
+
# def f(x, y):
|
940
|
+
# dx = x ** 2 + y ** 2 + 10
|
941
|
+
# dy = x ** 3 + y ** 3 - 10
|
942
|
+
# return dx, dy
|
943
|
+
#
|
944
|
+
# _x = jnp.ones(5)
|
945
|
+
# _y = jnp.ones(5)
|
946
|
+
#
|
947
|
+
# g, aux = bst.transform.vector_grad(f, has_aux=True)(_x, _y)
|
948
|
+
# pprint(g, )
|
949
|
+
# pprint(aux)
|
950
|
+
# self.assertTrue(jnp.array_equal(g, 2 * _x))
|
951
|
+
# self.assertTrue(jnp.array_equal(aux, _x ** 3 + _y ** 3 - 10))
|
952
|
+
#
|
953
|
+
# def test_return1(self):
|
954
|
+
# def f(x, y):
|
955
|
+
# dx = x ** 2 + y ** 2 + 10
|
956
|
+
# return dx
|
957
|
+
#
|
958
|
+
# _x = jnp.ones(5)
|
959
|
+
# _y = jnp.ones(5)
|
960
|
+
#
|
961
|
+
# g, value = bst.transform.vector_grad(f, return_value=True)(_x, _y)
|
962
|
+
# pprint(g, )
|
963
|
+
# pprint(value)
|
964
|
+
# self.assertTrue(jnp.array_equal(g, 2 * _x))
|
965
|
+
# self.assertTrue(jnp.array_equal(value, _x ** 2 + _y ** 2 + 10))
|
966
|
+
#
|
967
|
+
# def test_return_aux1(self):
|
968
|
+
# def f(x, y):
|
969
|
+
# dx = x ** 2 + y ** 2 + 10
|
970
|
+
# dy = x ** 3 + y ** 3 - 10
|
971
|
+
# return dx, dy
|
972
|
+
#
|
973
|
+
# _x = jnp.ones(5)
|
974
|
+
# _y = jnp.ones(5)
|
975
|
+
#
|
976
|
+
# g, value, aux = bst.transform.vector_grad(f, has_aux=True, return_value=True)(_x, _y)
|
977
|
+
# print('grad', g)
|
978
|
+
# print('value', value)
|
979
|
+
# print('aux', aux)
|
980
|
+
# self.assertTrue(jnp.array_equal(g, 2 * _x))
|
981
|
+
# self.assertTrue(jnp.array_equal(value, _x ** 2 + _y ** 2 + 10))
|
982
|
+
# self.assertTrue(jnp.array_equal(aux, _x ** 3 + _y ** 3 - 10))
|
983
|
+
#
|
984
|
+
#
|
985
|
+
# class TestClassFuncVectorGrad(unittest.TestCase):
|
986
|
+
# def test1(self):
|
987
|
+
# class Test(bst.Module):
|
988
|
+
# def __init__(self):
|
989
|
+
# super(Test, self).__init__()
|
990
|
+
# self.x = jnp.Variable(jnp.ones(5))
|
991
|
+
# self.y = jnp.Variable(jnp.ones(5))
|
992
|
+
#
|
993
|
+
# def __call__(self, *args, **kwargs):
|
994
|
+
# return self.x ** 2 + self.y ** 2 + 10
|
995
|
+
#
|
996
|
+
# t = Test()
|
997
|
+
#
|
998
|
+
# g = bst.transform.vector_grad(t, grad_vars=t.x)()
|
999
|
+
# self.assertTrue(jnp.array_equal(g, 2 * t.x))
|
1000
|
+
#
|
1001
|
+
# g = bst.transform.vector_grad(t, grad_vars=(t.x,))()
|
1002
|
+
# self.assertTrue(jnp.array_equal(g[0], 2 * t.x))
|
1003
|
+
#
|
1004
|
+
# g = bst.transform.vector_grad(t, grad_vars=(t.x, t.y))()
|
1005
|
+
# self.assertTrue(jnp.array_equal(g[0], 2 * t.x))
|
1006
|
+
# self.assertTrue(jnp.array_equal(g[1], 2 * t.y))
|
1007
|
+
#
|
1008
|
+
#
|
1009
|
+
# def vgrad(f, *x):
|
1010
|
+
# y, vjp_fn = jax.vjp(f, *x)
|
1011
|
+
# return vjp_fn(jnp.ones(y.shape).value)[0]
|
1012
|
+
#
|
1013
|
+
#
|
1014
|
+
# class TestDebug(parameterized.TestCase):
|
1015
|
+
# def test_debug1(self):
|
1016
|
+
# a = bst.random.RandomState()
|
1017
|
+
#
|
1018
|
+
# def f(b):
|
1019
|
+
# print(a.value)
|
1020
|
+
# return a + b + a.random()
|
1021
|
+
#
|
1022
|
+
# f = bst.transform.vector_grad(f, argnums=0)
|
1023
|
+
# f(1.)
|
1024
|
+
#
|
1025
|
+
# with jax.disable_jit():
|
1026
|
+
# f(1.)
|
1027
|
+
#
|
1028
|
+
# @parameterized.product(
|
1029
|
+
# grad_fun=[bst.transform.grad, bst.transform.vector_grad]
|
1030
|
+
# )
|
1031
|
+
# def test_print_info1(self, grad_fun):
|
1032
|
+
# file = tempfile.TemporaryFile(mode='w+')
|
1033
|
+
#
|
1034
|
+
# @functools.partial(grad_fun, argnums=0)
|
1035
|
+
# def f2(a, b):
|
1036
|
+
# print('compiling f2 ...', file=file)
|
1037
|
+
# return a + b
|
1038
|
+
#
|
1039
|
+
# @functools.partial(grad_fun, argnums=0)
|
1040
|
+
# def f1(a):
|
1041
|
+
# print('compiling f1 ...', file=file)
|
1042
|
+
# return f2(a, 1.)
|
1043
|
+
#
|
1044
|
+
# expect_res = '''
|
1045
|
+
# compiling f1 ...
|
1046
|
+
# compiling f2 ...
|
1047
|
+
# compiling f1 ...
|
1048
|
+
# compiling f2 ...
|
1049
|
+
# '''
|
1050
|
+
#
|
1051
|
+
# print(f1(1.))
|
1052
|
+
# file.seek(0)
|
1053
|
+
# self.assertTrue(file.read().strip() == expect_res.strip())
|
1054
|
+
#
|
1055
|
+
# file = tempfile.TemporaryFile(mode='w+')
|
1056
|
+
# with jax.disable_jit():
|
1057
|
+
# expect_res = '''
|
1058
|
+
# compiling f1 ...
|
1059
|
+
# compiling f2 ...
|
1060
|
+
# '''
|
1061
|
+
# self.assertTrue(f1(1.) == 0.)
|
1062
|
+
# file.seek(0)
|
1063
|
+
# self.assertTrue(file.read().strip() == expect_res.strip())
|
1064
|
+
#
|
1065
|
+
# @parameterized.product(
|
1066
|
+
# grad_fun=[bst.transform.grad, bst.transform.vector_grad]
|
1067
|
+
# )
|
1068
|
+
# def test_print_info2(self, grad_fun):
|
1069
|
+
# file = tempfile.TemporaryFile(mode='w+')
|
1070
|
+
#
|
1071
|
+
# @functools.partial(grad_fun, argnums=0)
|
1072
|
+
# def f1(a):
|
1073
|
+
# @functools.partial(grad_fun, argnums=0)
|
1074
|
+
# def f2(a, b):
|
1075
|
+
# print('compiling f2 ...', file=file)
|
1076
|
+
# return a + b
|
1077
|
+
#
|
1078
|
+
# print('compiling f1 ...', file=file)
|
1079
|
+
# return f2(a, 1.)
|
1080
|
+
#
|
1081
|
+
# expect_res = '''
|
1082
|
+
# compiling f1 ...
|
1083
|
+
# compiling f2 ...
|
1084
|
+
# compiling f1 ...
|
1085
|
+
# compiling f2 ...
|
1086
|
+
# compiling f2 ...
|
1087
|
+
# '''
|
1088
|
+
# self.assertTrue(f1(1.) == 0.)
|
1089
|
+
# file.seek(0)
|
1090
|
+
# self.assertTrue(file.read().strip() == expect_res.strip())
|
1091
|
+
#
|
1092
|
+
# file = tempfile.TemporaryFile(mode='w+')
|
1093
|
+
# with jax.disable_jit():
|
1094
|
+
# expect_res = '''
|
1095
|
+
# compiling f1 ...
|
1096
|
+
# compiling f2 ...
|
1097
|
+
# '''
|
1098
|
+
# self.assertTrue(f1(1.) == 0.)
|
1099
|
+
# file.seek(0)
|
1100
|
+
# # print(file.read().strip())
|
1101
|
+
# self.assertTrue(file.read().strip() == expect_res.strip())
|
1102
|
+
#
|
1103
|
+
# def test_debug_correctness1(self):
|
1104
|
+
# def test_f():
|
1105
|
+
# a = jnp.Variable(jnp.ones(2))
|
1106
|
+
# b = jnp.Variable(jnp.zeros(2))
|
1107
|
+
#
|
1108
|
+
# @bst.transform.vector_grad(argnums=0)
|
1109
|
+
# def f1(c):
|
1110
|
+
# a.value += 1
|
1111
|
+
# b.value += 10
|
1112
|
+
# return a * b * c
|
1113
|
+
#
|
1114
|
+
# return a, b, f1(1.)
|
1115
|
+
#
|
1116
|
+
# r1 = test_f()
|
1117
|
+
# print(r1)
|
1118
|
+
#
|
1119
|
+
# with jax.disable_jit():
|
1120
|
+
# r2 = test_f()
|
1121
|
+
# print(r2)
|
1122
|
+
# self.assertTrue(jnp.allclose(r1[0], r2[0]))
|
1123
|
+
# self.assertTrue(jnp.allclose(r1[1], r2[1]))
|
1124
|
+
# self.assertTrue(jnp.allclose(r1[2], r2[2]))
|
1125
|
+
#
|
1126
|
+
# def f1(c, a, b):
|
1127
|
+
# a += 1
|
1128
|
+
# b += 10
|
1129
|
+
# return a * b * c
|
1130
|
+
#
|
1131
|
+
# r3 = vgrad(f1, 1., jnp.ones(2).value, jnp.zeros(2).value)
|
1132
|
+
# self.assertTrue(jnp.allclose(r1[2], r3))
|
1133
|
+
#
|
1134
|
+
# def _bench_f2(self, dd):
|
1135
|
+
# a = jnp.Variable(jnp.ones(2))
|
1136
|
+
# b = jnp.Variable(jnp.zeros(2))
|
1137
|
+
#
|
1138
|
+
#
|
1139
|
+
# def run_fun(d):
|
1140
|
+
# @bst.transform.vector_grad(argnums=0)
|
1141
|
+
# def f1(c):
|
1142
|
+
# a.value += d
|
1143
|
+
# b.value += 10
|
1144
|
+
# return a * b * c
|
1145
|
+
#
|
1146
|
+
# return a, b, f1(1.)
|
1147
|
+
#
|
1148
|
+
# return run_fun(dd)
|
1149
|
+
#
|
1150
|
+
# def test_debug_correctness2(self):
|
1151
|
+
# r1 = self._bench_f2(1.)
|
1152
|
+
# print(r1)
|
1153
|
+
#
|
1154
|
+
# with jax.disable_jit():
|
1155
|
+
# r2 = self._bench_f2(1.)
|
1156
|
+
# print(r2)
|
1157
|
+
#
|
1158
|
+
# self.assertTrue(jnp.allclose(r1[0], r2[0]))
|
1159
|
+
# self.assertTrue(jnp.allclose(r1[1], r2[1]))
|
1160
|
+
# self.assertTrue(jnp.allclose(r1[2], r2[2]))
|
1161
|
+
#
|
1162
|
+
# def test_cache1(self):
|
1163
|
+
# file = tempfile.TemporaryFile(mode='w+')
|
1164
|
+
#
|
1165
|
+
# def f(a, b):
|
1166
|
+
# print('compiling f ...', file=file)
|
1167
|
+
# return a + b
|
1168
|
+
#
|
1169
|
+
# grad1 = bst.transform.grad(f)(1., 2.) # call "f" twice, one for Variable finding, one for compiling
|
1170
|
+
# grad2 = bst.transform.vector_grad(f)(1., 2.) # call "f" once for compiling
|
1171
|
+
#
|
1172
|
+
# file.seek(0)
|
1173
|
+
# print(file.read().strip())
|
1174
|
+
#
|
1175
|
+
# expect_res = '''
|
1176
|
+
# compiling f ...
|
1177
|
+
# compiling f ...
|
1178
|
+
# compiling f ...
|
1179
|
+
# '''
|
1180
|
+
# file.seek(0)
|
1181
|
+
# self.assertTrue(file.read().strip() == expect_res.strip())
|
1182
|
+
#
|
1183
|
+
#
|