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