brainstate 0.0.2.post20241010__py2.py3-none-any.whl → 0.1.0.post20241122__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.
Files changed (184) hide show
  1. benchmark/COBA_2005.py +125 -0
  2. benchmark/CUBA_2005.py +149 -0
  3. brainstate/__init__.py +31 -11
  4. brainstate/_state.py +760 -316
  5. brainstate/_state_test.py +41 -12
  6. brainstate/_utils.py +31 -4
  7. brainstate/augment/__init__.py +40 -0
  8. brainstate/augment/_autograd.py +611 -0
  9. brainstate/augment/_autograd_test.py +1193 -0
  10. brainstate/augment/_eval_shape.py +102 -0
  11. brainstate/augment/_eval_shape_test.py +40 -0
  12. brainstate/augment/_mapping.py +525 -0
  13. brainstate/augment/_mapping_test.py +210 -0
  14. brainstate/augment/_random.py +99 -0
  15. brainstate/{transform → compile}/__init__.py +25 -13
  16. brainstate/compile/_ad_checkpoint.py +204 -0
  17. brainstate/compile/_ad_checkpoint_test.py +51 -0
  18. brainstate/compile/_conditions.py +259 -0
  19. brainstate/compile/_conditions_test.py +221 -0
  20. brainstate/compile/_error_if.py +94 -0
  21. brainstate/compile/_error_if_test.py +54 -0
  22. brainstate/compile/_jit.py +314 -0
  23. brainstate/compile/_jit_test.py +143 -0
  24. brainstate/compile/_loop_collect_return.py +516 -0
  25. brainstate/compile/_loop_collect_return_test.py +59 -0
  26. brainstate/compile/_loop_no_collection.py +185 -0
  27. brainstate/compile/_loop_no_collection_test.py +51 -0
  28. brainstate/compile/_make_jaxpr.py +756 -0
  29. brainstate/compile/_make_jaxpr_test.py +134 -0
  30. brainstate/compile/_progress_bar.py +111 -0
  31. brainstate/compile/_unvmap.py +159 -0
  32. brainstate/compile/_util.py +147 -0
  33. brainstate/environ.py +408 -381
  34. brainstate/environ_test.py +34 -32
  35. brainstate/event/__init__.py +27 -0
  36. brainstate/event/_csr.py +316 -0
  37. brainstate/event/_csr_benchmark.py +14 -0
  38. brainstate/event/_csr_test.py +118 -0
  39. brainstate/event/_fixed_probability.py +708 -0
  40. brainstate/event/_fixed_probability_benchmark.py +128 -0
  41. brainstate/event/_fixed_probability_test.py +131 -0
  42. brainstate/event/_linear.py +359 -0
  43. brainstate/event/_linear_benckmark.py +82 -0
  44. brainstate/event/_linear_test.py +117 -0
  45. brainstate/{nn/event → event}/_misc.py +7 -7
  46. brainstate/event/_xla_custom_op.py +312 -0
  47. brainstate/event/_xla_custom_op_test.py +55 -0
  48. brainstate/functional/_activations.py +521 -511
  49. brainstate/functional/_activations_test.py +300 -300
  50. brainstate/functional/_normalization.py +43 -43
  51. brainstate/functional/_others.py +15 -15
  52. brainstate/functional/_spikes.py +49 -49
  53. brainstate/graph/__init__.py +33 -0
  54. brainstate/graph/_graph_context.py +443 -0
  55. brainstate/graph/_graph_context_test.py +65 -0
  56. brainstate/graph/_graph_convert.py +246 -0
  57. brainstate/graph/_graph_node.py +300 -0
  58. brainstate/graph/_graph_node_test.py +75 -0
  59. brainstate/graph/_graph_operation.py +1746 -0
  60. brainstate/graph/_graph_operation_test.py +724 -0
  61. brainstate/init/_base.py +28 -10
  62. brainstate/init/_generic.py +175 -172
  63. brainstate/init/_random_inits.py +470 -415
  64. brainstate/init/_random_inits_test.py +150 -0
  65. brainstate/init/_regular_inits.py +66 -69
  66. brainstate/init/_regular_inits_test.py +51 -0
  67. brainstate/mixin.py +236 -244
  68. brainstate/mixin_test.py +44 -46
  69. brainstate/nn/__init__.py +26 -51
  70. brainstate/nn/_collective_ops.py +199 -0
  71. brainstate/nn/_dyn_impl/__init__.py +46 -0
  72. brainstate/nn/_dyn_impl/_dynamics_neuron.py +290 -0
  73. brainstate/nn/_dyn_impl/_dynamics_neuron_test.py +162 -0
  74. brainstate/nn/_dyn_impl/_dynamics_synapse.py +315 -0
  75. brainstate/nn/_dyn_impl/_dynamics_synapse_test.py +132 -0
  76. brainstate/nn/_dyn_impl/_inputs.py +154 -0
  77. brainstate/nn/{event/__init__.py → _dyn_impl/_projection_alignpost.py} +8 -8
  78. brainstate/nn/_dyn_impl/_rate_rnns.py +400 -0
  79. brainstate/nn/_dyn_impl/_rate_rnns_test.py +64 -0
  80. brainstate/nn/_dyn_impl/_readout.py +128 -0
  81. brainstate/nn/_dyn_impl/_readout_test.py +54 -0
  82. brainstate/nn/_dynamics/__init__.py +37 -0
  83. brainstate/nn/_dynamics/_dynamics_base.py +631 -0
  84. brainstate/nn/_dynamics/_dynamics_base_test.py +79 -0
  85. brainstate/nn/_dynamics/_projection_base.py +346 -0
  86. brainstate/nn/_dynamics/_state_delay.py +453 -0
  87. brainstate/nn/_dynamics/_synouts.py +161 -0
  88. brainstate/nn/_dynamics/_synouts_test.py +58 -0
  89. brainstate/nn/_elementwise/__init__.py +22 -0
  90. brainstate/nn/_elementwise/_dropout.py +418 -0
  91. brainstate/nn/_elementwise/_dropout_test.py +100 -0
  92. brainstate/nn/_elementwise/_elementwise.py +1122 -0
  93. brainstate/nn/_elementwise/_elementwise_test.py +171 -0
  94. brainstate/nn/_exp_euler.py +97 -0
  95. brainstate/nn/_exp_euler_test.py +36 -0
  96. brainstate/nn/_interaction/__init__.py +41 -0
  97. brainstate/nn/_interaction/_conv.py +499 -0
  98. brainstate/nn/_interaction/_conv_test.py +239 -0
  99. brainstate/nn/_interaction/_embedding.py +59 -0
  100. brainstate/nn/_interaction/_linear.py +582 -0
  101. brainstate/nn/_interaction/_linear_test.py +42 -0
  102. brainstate/nn/_interaction/_normalizations.py +388 -0
  103. brainstate/nn/_interaction/_normalizations_test.py +75 -0
  104. brainstate/nn/_interaction/_poolings.py +1179 -0
  105. brainstate/nn/_interaction/_poolings_test.py +219 -0
  106. brainstate/nn/_module.py +328 -0
  107. brainstate/nn/_module_test.py +211 -0
  108. brainstate/nn/metrics.py +309 -309
  109. brainstate/optim/__init__.py +14 -2
  110. brainstate/optim/_base.py +66 -0
  111. brainstate/optim/_lr_scheduler.py +363 -400
  112. brainstate/optim/_lr_scheduler_test.py +25 -24
  113. brainstate/optim/_optax_optimizer.py +121 -176
  114. brainstate/optim/_optax_optimizer_test.py +41 -1
  115. brainstate/optim/_sgd_optimizer.py +950 -1025
  116. brainstate/random/_rand_funs.py +3269 -3268
  117. brainstate/random/_rand_funs_test.py +568 -0
  118. brainstate/random/_rand_seed.py +149 -117
  119. brainstate/random/_rand_seed_test.py +50 -0
  120. brainstate/random/_rand_state.py +1356 -1321
  121. brainstate/random/_random_for_unit.py +13 -13
  122. brainstate/surrogate.py +1262 -1243
  123. brainstate/{nn/_projection/_utils.py → transform.py} +1 -2
  124. brainstate/typing.py +157 -130
  125. brainstate/util/__init__.py +52 -0
  126. brainstate/util/_caller.py +100 -0
  127. brainstate/util/_dict.py +734 -0
  128. brainstate/util/_dict_test.py +160 -0
  129. brainstate/{nn/_projection/__init__.py → util/_error.py} +9 -13
  130. brainstate/util/_filter.py +178 -0
  131. brainstate/util/_others.py +497 -0
  132. brainstate/util/_pretty_repr.py +208 -0
  133. brainstate/util/_scaling.py +260 -0
  134. brainstate/util/_struct.py +524 -0
  135. brainstate/util/_tracers.py +75 -0
  136. brainstate/{_visualization.py → util/_visualization.py} +16 -16
  137. {brainstate-0.0.2.post20241010.dist-info → brainstate-0.1.0.post20241122.dist-info}/METADATA +11 -11
  138. brainstate-0.1.0.post20241122.dist-info/RECORD +144 -0
  139. {brainstate-0.0.2.post20241010.dist-info → brainstate-0.1.0.post20241122.dist-info}/top_level.txt +1 -0
  140. brainstate/_module.py +0 -1637
  141. brainstate/_module_test.py +0 -207
  142. brainstate/nn/_base.py +0 -251
  143. brainstate/nn/_connections.py +0 -686
  144. brainstate/nn/_dynamics.py +0 -426
  145. brainstate/nn/_elementwise.py +0 -1438
  146. brainstate/nn/_embedding.py +0 -66
  147. brainstate/nn/_misc.py +0 -133
  148. brainstate/nn/_normalizations.py +0 -389
  149. brainstate/nn/_others.py +0 -101
  150. brainstate/nn/_poolings.py +0 -1229
  151. brainstate/nn/_poolings_test.py +0 -231
  152. brainstate/nn/_projection/_align_post.py +0 -546
  153. brainstate/nn/_projection/_align_pre.py +0 -599
  154. brainstate/nn/_projection/_delta.py +0 -241
  155. brainstate/nn/_projection/_vanilla.py +0 -101
  156. brainstate/nn/_rate_rnns.py +0 -410
  157. brainstate/nn/_readout.py +0 -136
  158. brainstate/nn/_synouts.py +0 -166
  159. brainstate/nn/event/csr.py +0 -312
  160. brainstate/nn/event/csr_test.py +0 -118
  161. brainstate/nn/event/fixed_probability.py +0 -276
  162. brainstate/nn/event/fixed_probability_test.py +0 -127
  163. brainstate/nn/event/linear.py +0 -220
  164. brainstate/nn/event/linear_test.py +0 -111
  165. brainstate/random/random_test.py +0 -593
  166. brainstate/transform/_autograd.py +0 -585
  167. brainstate/transform/_autograd_test.py +0 -1181
  168. brainstate/transform/_conditions.py +0 -334
  169. brainstate/transform/_conditions_test.py +0 -220
  170. brainstate/transform/_error_if.py +0 -94
  171. brainstate/transform/_error_if_test.py +0 -55
  172. brainstate/transform/_jit.py +0 -265
  173. brainstate/transform/_jit_test.py +0 -118
  174. brainstate/transform/_loop_collect_return.py +0 -502
  175. brainstate/transform/_loop_no_collection.py +0 -170
  176. brainstate/transform/_make_jaxpr.py +0 -739
  177. brainstate/transform/_make_jaxpr_test.py +0 -131
  178. brainstate/transform/_mapping.py +0 -109
  179. brainstate/transform/_progress_bar.py +0 -111
  180. brainstate/transform/_unvmap.py +0 -143
  181. brainstate/util.py +0 -746
  182. brainstate-0.0.2.post20241010.dist-info/RECORD +0 -87
  183. {brainstate-0.0.2.post20241010.dist-info → brainstate-0.1.0.post20241122.dist-info}/LICENSE +0 -0
  184. {brainstate-0.0.2.post20241010.dist-info → brainstate-0.1.0.post20241122.dist-info}/WHEEL +0 -0
@@ -1,1181 +0,0 @@
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
- def f1(x, y):
541
- a = 4 * x[1] ** 2 - 2 * x[2]
542
- r = jnp.asarray([x[0] * y[0], 5 * x[2] * y[1], a, x[2] * jnp.sin(x[0])])
543
- return r, a
544
-
545
- _x = jnp.array([1., 2., 3.])
546
- _y = jnp.array([10., 5.])
547
- _r, _a = f1(_x, _y)
548
- f2 = lambda *args: f1(*args)[0]
549
- _g1 = jax.jacrev(f2)(_x, _y) # jax jacobian
550
- pprint(_g1)
551
- _g2 = jax.jacrev(f2, argnums=(0, 1))(_x, _y) # jax jacobian
552
- pprint(_g2)
553
-
554
- grads, vec, aux = bc.transform.jacrev(f1, return_value=True, has_aux=True)(_x, _y)
555
- assert (grads == _g1).all()
556
- assert aux == _a
557
- assert (vec == _r).all()
558
-
559
- grads, vec, aux = bc.transform.jacrev(f1, return_value=True, argnums=(0, 1), has_aux=True)(_x, _y)
560
- assert (grads[0] == _g2[0]).all()
561
- assert (grads[1] == _g2[1]).all()
562
- assert aux == _a
563
- assert (vec == _r).all()
564
-
565
-
566
- class TestClassFuncJacobian(unittest.TestCase):
567
- def test_jacrev1(self):
568
- def f1(x, y):
569
- 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])])
570
- return r
571
-
572
- _x = jnp.array([1., 2., 3.])
573
- _y = jnp.array([10., 5.])
574
-
575
- class Test(bc.Module):
576
- def __init__(self):
577
- super(Test, self).__init__()
578
- self.x = bc.State(jnp.array([1., 2., 3.]))
579
- self.y = bc.State(jnp.array([10., 5.]))
580
-
581
- def __call__(self, ):
582
- a = self.x.value[0] * self.y.value[0]
583
- b = 5 * self.x.value[2] * self.y.value[1]
584
- c = 4 * self.x.value[1] ** 2 - 2 * self.x.value[2]
585
- d = self.x.value[2] * jnp.sin(self.x.value[0])
586
- r = jnp.asarray([a, b, c, d])
587
- return r
588
-
589
- _jr = jax.jacrev(f1)(_x, _y)
590
- t = Test()
591
- br = bc.transform.jacrev(t, grad_vars=t.x)()
592
- self.assertTrue((br == _jr).all())
593
-
594
- _jr = jax.jacrev(f1, argnums=(0, 1))(_x, _y)
595
- t = Test()
596
- br = bc.transform.jacrev(t, grad_vars=[t.x, t.y])()
597
- self.assertTrue((br[0] == _jr[0]).all())
598
- self.assertTrue((br[1] == _jr[1]).all())
599
- #
600
- # def test_jacfwd1(self):
601
- # def f1(x, y):
602
- # 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])])
603
- # return r
604
- #
605
- # _x = jnp.array([1., 2., 3.])
606
- # _y = jnp.array([10., 5.])
607
- #
608
- # class Test(bst.Module):
609
- # def __init__(self):
610
- # super(Test, self).__init__()
611
- # self.x = jnp.Variable(jnp.array([1., 2., 3.]))
612
- # self.y = jnp.Variable(jnp.array([10., 5.]))
613
- #
614
- # def __call__(self, ):
615
- # a = self.x[0] * self.y[0]
616
- # b = 5 * self.x[2] * self.y[1]
617
- # c = 4 * self.x[1] ** 2 - 2 * self.x[2]
618
- # d = self.x[2] * jnp.sin(self.x[0])
619
- # r = jnp.asarray([a, b, c, d])
620
- # return r
621
- #
622
- # _jr = jax.jacfwd(f1)(_x, _y)
623
- # t = Test()
624
- # br = bst.transform.jacfwd(t, grad_vars=t.x)()
625
- # self.assertTrue((br == _jr).all())
626
- #
627
- # _jr = jax.jacfwd(f1, argnums=(0, 1))(_x, _y)
628
- # t = Test()
629
- # br = bst.transform.jacfwd(t, grad_vars=[t.x, t.y])()
630
- # self.assertTrue((br[0] == _jr[0]).all())
631
- # self.assertTrue((br[1] == _jr[1]).all())
632
- #
633
- # def test_jacrev2(self):
634
- # def f1(x, y):
635
- # 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])])
636
- # return r
637
- #
638
- # _x = jnp.array([1., 2., 3.])
639
- # _y = jnp.array([10., 5.])
640
- #
641
- # class Test(bst.Module):
642
- # def __init__(self):
643
- # super(Test, self).__init__()
644
- # self.x = jnp.Variable(jnp.array([1., 2., 3.]))
645
- #
646
- # def __call__(self, y):
647
- # a = self.x[0] * y[0]
648
- # b = 5 * self.x[2] * y[1]
649
- # c = 4 * self.x[1] ** 2 - 2 * self.x[2]
650
- # d = self.x[2] * jnp.sin(self.x[0])
651
- # r = jnp.asarray([a, b, c, d])
652
- # return r
653
- #
654
- # _jr = jax.jacrev(f1)(_x, _y)
655
- # t = Test()
656
- # br = bst.transform.jacrev(t, grad_vars=t.x)(_y)
657
- # self.assertTrue((br == _jr).all())
658
- #
659
- # _jr = jax.jacrev(f1, argnums=(0, 1))(_x, _y)
660
- # t = Test()
661
- # var_grads, arg_grads = bst.transform.jacrev(t, grad_vars=t.x, argnums=0)(_y)
662
- # print(var_grads, )
663
- # print(arg_grads, )
664
- # self.assertTrue((var_grads == _jr[0]).all())
665
- # self.assertTrue((arg_grads == _jr[1]).all())
666
- #
667
- # def test_jacfwd2(self):
668
- # def f1(x, y):
669
- # 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])])
670
- # return r
671
- #
672
- # _x = jnp.array([1., 2., 3.])
673
- # _y = jnp.array([10., 5.])
674
- #
675
- # class Test(bst.Module):
676
- # def __init__(self):
677
- # super(Test, self).__init__()
678
- # self.x = jnp.Variable(jnp.array([1., 2., 3.]))
679
- #
680
- # def __call__(self, y):
681
- # a = self.x[0] * y[0]
682
- # b = 5 * self.x[2] * y[1]
683
- # c = 4 * self.x[1] ** 2 - 2 * self.x[2]
684
- # d = self.x[2] * jnp.sin(self.x[0])
685
- # r = jnp.asarray([a, b, c, d])
686
- # return r
687
- #
688
- # _jr = jax.jacfwd(f1)(_x, _y)
689
- # t = Test()
690
- # br = bst.transform.jacfwd(t, grad_vars=t.x)(_y)
691
- # self.assertTrue((br == _jr).all())
692
- #
693
- # _jr = jax.jacfwd(f1, argnums=(0, 1))(_x, _y)
694
- # t = Test()
695
- # var_grads, arg_grads = bst.transform.jacfwd(t, grad_vars=t.x, argnums=0)(_y)
696
- # print(var_grads, )
697
- # print(arg_grads, )
698
- # self.assertTrue((var_grads == _jr[0]).all())
699
- # self.assertTrue((arg_grads == _jr[1]).all())
700
- #
701
- # def test_jacrev_aux1(self):
702
- # jnp.enable_x64()
703
- #
704
- # def f1(x, y):
705
- # 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])])
706
- # return r
707
- #
708
- # _x = jnp.array([1., 2., 3.])
709
- # _y = jnp.array([10., 5.])
710
- #
711
- # class Test(bst.Module):
712
- # def __init__(self):
713
- # super(Test, self).__init__()
714
- # self.x = jnp.Variable(jnp.array([1., 2., 3.]))
715
- #
716
- # def __call__(self, y):
717
- # a = self.x[0] * y[0]
718
- # b = 5 * self.x[2] * y[1]
719
- # c = 4 * self.x[1] ** 2 - 2 * self.x[2]
720
- # d = self.x[2] * jnp.sin(self.x[0])
721
- # r = jnp.asarray([a, b, c, d])
722
- # return r, (c, d)
723
- #
724
- # _jr = jax.jacrev(f1)(_x, _y)
725
- # t = Test()
726
- # br, _ = bst.transform.jacrev(t, grad_vars=t.x, has_aux=True)(_y)
727
- # self.assertTrue((br == _jr).all())
728
- #
729
- # t = Test()
730
- # _jr = jax.jacrev(f1, argnums=(0, 1))(_x, _y)
731
- # _aux = t(_y)[1]
732
- # (var_grads, arg_grads), aux = bst.transform.jacrev(t, grad_vars=t.x, argnums=0, has_aux=True)(_y)
733
- # print(var_grads, )
734
- # print(arg_grads, )
735
- # self.assertTrue((var_grads == _jr[0]).all())
736
- # self.assertTrue((arg_grads == _jr[1]).all())
737
- # self.assertTrue(jnp.array_equal(aux, _aux))
738
- #
739
- # jnp.disable_x64()
740
- #
741
- # def test_jacfwd_aux1(self):
742
- # jnp.enable_x64()
743
- #
744
- # def f1(x, y):
745
- # 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])])
746
- # return r
747
- #
748
- # _x = jnp.array([1., 2., 3.])
749
- # _y = jnp.array([10., 5.])
750
- #
751
- # class Test(bst.Module):
752
- # def __init__(self):
753
- # super(Test, self).__init__()
754
- # self.x = jnp.Variable(jnp.array([1., 2., 3.]))
755
- #
756
- # def __call__(self, y):
757
- # a = self.x[0] * y[0]
758
- # b = 5 * self.x[2] * y[1]
759
- # c = 4 * self.x[1] ** 2 - 2 * self.x[2]
760
- # d = self.x[2] * jnp.sin(self.x[0])
761
- # r = jnp.asarray([a, b, c, d])
762
- # return r, (c, d)
763
- #
764
- # _jr = jax.jacfwd(f1)(_x, _y)
765
- # t = Test()
766
- # br, (c, d) = bst.transform.jacfwd(t, grad_vars=t.x, has_aux=True)(_y)
767
- # # print(_jr)
768
- # # print(br)
769
- # a = (br == _jr)
770
- # self.assertTrue(a.all())
771
- #
772
- # t = Test()
773
- # _jr = jax.jacfwd(f1, argnums=(0, 1))(_x, _y)
774
- # _aux = t(_y)[1]
775
- # (var_grads, arg_grads), aux = bst.transform.jacfwd(t, grad_vars=t.x, argnums=0, has_aux=True)(_y)
776
- # print(var_grads, )
777
- # print(arg_grads, )
778
- # self.assertTrue((var_grads == _jr[0]).all())
779
- # self.assertTrue((arg_grads == _jr[1]).all())
780
- # self.assertTrue(jnp.array_equal(aux, _aux))
781
- #
782
- # jnp.disable_x64()
783
- #
784
- # def test_jacrev_return_aux1(self):
785
- # jnp.enable_x64()
786
- #
787
- # def f1(x, y):
788
- # 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])])
789
- # return r
790
- #
791
- # _x = jnp.array([1., 2., 3.])
792
- # _y = jnp.array([10., 5.])
793
- #
794
- # class Test(bst.Module):
795
- # def __init__(self):
796
- # super(Test, self).__init__()
797
- # self.x = jnp.Variable(jnp.array([1., 2., 3.]))
798
- #
799
- # def __call__(self, y):
800
- # a = self.x[0] * y[0]
801
- # b = 5 * self.x[2] * y[1]
802
- # c = 4 * self.x[1] ** 2 - 2 * self.x[2]
803
- # d = self.x[2] * jnp.sin(self.x[0])
804
- # r = jnp.asarray([a, b, c, d])
805
- # return r, (c, d)
806
- #
807
- # _jr = jax.jacrev(f1)(_x, _y)
808
- # t = Test()
809
- # br, _ = bst.transform.jacrev(t, grad_vars=t.x, has_aux=True)(_y)
810
- # self.assertTrue((br == _jr).all())
811
- #
812
- # t = Test()
813
- # _jr = jax.jacrev(f1, argnums=(0, 1))(_x, _y)
814
- # _val, _aux = t(_y)
815
- # (var_grads, arg_grads), value, aux = bst.transform.jacrev(t, grad_vars=t.x, argnums=0, has_aux=True, return_value=True)(_y)
816
- # print(var_grads, )
817
- # print(arg_grads, )
818
- # self.assertTrue((var_grads == _jr[0]).all())
819
- # self.assertTrue((arg_grads == _jr[1]).all())
820
- # self.assertTrue(jnp.array_equal(aux, _aux))
821
- # self.assertTrue(jnp.array_equal(value, _val))
822
- #
823
- # jnp.disable_x64()
824
- #
825
- # def test_jacfwd_return_aux1(self):
826
- # jnp.enable_x64()
827
- #
828
- # def f1(x, y):
829
- # 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])])
830
- # return r
831
- #
832
- # _x = jnp.array([1., 2., 3.])
833
- # _y = jnp.array([10., 5.])
834
- #
835
- # class Test(bst.Module):
836
- # def __init__(self):
837
- # super(Test, self).__init__()
838
- # self.x = jnp.Variable(jnp.array([1., 2., 3.]))
839
- #
840
- # def __call__(self, y):
841
- # a = self.x[0] * y[0]
842
- # b = 5 * self.x[2] * y[1]
843
- # c = 4 * self.x[1] ** 2 - 2 * self.x[2]
844
- # d = self.x[2] * jnp.sin(self.x[0])
845
- # r = jnp.asarray([a, b, c, d])
846
- # return r, (c, d)
847
- #
848
- # _jr = jax.jacfwd(f1)(_x, _y)
849
- # t = Test()
850
- # br, _ = bst.transform.jacfwd(t, grad_vars=t.x, has_aux=True)(_y)
851
- # self.assertTrue((br == _jr).all())
852
- #
853
- # t = Test()
854
- # _jr = jax.jacfwd(f1, argnums=(0, 1))(_x, _y)
855
- # _val, _aux = t(_y)
856
- # (var_grads, arg_grads), value, aux = bst.transform.jacfwd(t, grad_vars=t.x, argnums=0, has_aux=True, return_value=True)(_y)
857
- # print(_val, )
858
- # print('_aux: ', _aux, 'aux: ', aux)
859
- # print(var_grads, )
860
- # print(arg_grads, )
861
- # self.assertTrue((var_grads == _jr[0]).all())
862
- # self.assertTrue((arg_grads == _jr[1]).all())
863
- # self.assertTrue(jnp.array_equal(aux, _aux))
864
- # self.assertTrue(jnp.array_equal(value, _val))
865
- #
866
- # jnp.disable_x64()
867
- #
868
- #
869
- # class TestPureFuncVectorGrad(unittest.TestCase):
870
- # def test1(self):
871
- # f = lambda x: 3 * x ** 2
872
- # _x = jnp.ones(10)
873
- # pprint(bst.transform.vector_grad(f, argnums=0)(_x))
874
- #
875
- # def test2(self):
876
- # def f(x, y):
877
- # dx = x ** 2 + y ** 2 + 10
878
- # return dx
879
- #
880
- # _x = jnp.ones(5)
881
- # _y = jnp.ones(5)
882
- #
883
- # g = bst.transform.vector_grad(f, argnums=0)(_x, _y)
884
- # pprint(g)
885
- # self.assertTrue(jnp.array_equal(g, 2 * _x))
886
- #
887
- # g = bst.transform.vector_grad(f, argnums=(0,))(_x, _y)
888
- # self.assertTrue(jnp.array_equal(g[0], 2 * _x))
889
- #
890
- # g = bst.transform.vector_grad(f, argnums=(0, 1))(_x, _y)
891
- # pprint(g)
892
- # self.assertTrue(jnp.array_equal(g[0], 2 * _x))
893
- # self.assertTrue(jnp.array_equal(g[1], 2 * _y))
894
- #
895
- # def test3(self):
896
- # def f(x, y):
897
- # dx = x ** 2 + y ** 2 + 10
898
- # dy = x ** 3 + y ** 3 - 10
899
- # return dx, dy
900
- #
901
- # _x = jnp.ones(5)
902
- # _y = jnp.ones(5)
903
- #
904
- # g = bst.transform.vector_grad(f, argnums=0)(_x, _y)
905
- # # pprint(g)
906
- # self.assertTrue(jnp.array_equal(g, 2 * _x + 3 * _x ** 2))
907
- #
908
- # g = bst.transform.vector_grad(f, argnums=(0,))(_x, _y)
909
- # self.assertTrue(jnp.array_equal(g[0], 2 * _x + 3 * _x ** 2))
910
- #
911
- # g = bst.transform.vector_grad(f, argnums=(0, 1))(_x, _y)
912
- # # pprint(g)
913
- # self.assertTrue(jnp.array_equal(g[0], 2 * _x + 3 * _x ** 2))
914
- # self.assertTrue(jnp.array_equal(g[1], 2 * _y + 3 * _y ** 2))
915
- #
916
- # def test4_2d(self):
917
- # def f(x, y):
918
- # dx = x ** 2 + y ** 2 + 10
919
- # return dx
920
- #
921
- # _x = jnp.ones((5, 5))
922
- # _y = jnp.ones((5, 5))
923
- #
924
- # g = bst.transform.vector_grad(f, argnums=0)(_x, _y)
925
- # pprint(g)
926
- # self.assertTrue(jnp.array_equal(g, 2 * _x))
927
- #
928
- # g = bst.transform.vector_grad(f, argnums=(0,))(_x, _y)
929
- # self.assertTrue(jnp.array_equal(g[0], 2 * _x))
930
- #
931
- # g = bst.transform.vector_grad(f, argnums=(0, 1))(_x, _y)
932
- # pprint(g)
933
- # self.assertTrue(jnp.array_equal(g[0], 2 * _x))
934
- # self.assertTrue(jnp.array_equal(g[1], 2 * _y))
935
- #
936
- # def test_aux1(self):
937
- # def f(x, y):
938
- # dx = x ** 2 + y ** 2 + 10
939
- # dy = x ** 3 + y ** 3 - 10
940
- # return dx, dy
941
- #
942
- # _x = jnp.ones(5)
943
- # _y = jnp.ones(5)
944
- #
945
- # g, aux = bst.transform.vector_grad(f, has_aux=True)(_x, _y)
946
- # pprint(g, )
947
- # pprint(aux)
948
- # self.assertTrue(jnp.array_equal(g, 2 * _x))
949
- # self.assertTrue(jnp.array_equal(aux, _x ** 3 + _y ** 3 - 10))
950
- #
951
- # def test_return1(self):
952
- # def f(x, y):
953
- # dx = x ** 2 + y ** 2 + 10
954
- # return dx
955
- #
956
- # _x = jnp.ones(5)
957
- # _y = jnp.ones(5)
958
- #
959
- # g, value = bst.transform.vector_grad(f, return_value=True)(_x, _y)
960
- # pprint(g, )
961
- # pprint(value)
962
- # self.assertTrue(jnp.array_equal(g, 2 * _x))
963
- # self.assertTrue(jnp.array_equal(value, _x ** 2 + _y ** 2 + 10))
964
- #
965
- # def test_return_aux1(self):
966
- # def f(x, y):
967
- # dx = x ** 2 + y ** 2 + 10
968
- # dy = x ** 3 + y ** 3 - 10
969
- # return dx, dy
970
- #
971
- # _x = jnp.ones(5)
972
- # _y = jnp.ones(5)
973
- #
974
- # g, value, aux = bst.transform.vector_grad(f, has_aux=True, return_value=True)(_x, _y)
975
- # print('grad', g)
976
- # print('value', value)
977
- # print('aux', aux)
978
- # self.assertTrue(jnp.array_equal(g, 2 * _x))
979
- # self.assertTrue(jnp.array_equal(value, _x ** 2 + _y ** 2 + 10))
980
- # self.assertTrue(jnp.array_equal(aux, _x ** 3 + _y ** 3 - 10))
981
- #
982
- #
983
- # class TestClassFuncVectorGrad(unittest.TestCase):
984
- # def test1(self):
985
- # class Test(bst.Module):
986
- # def __init__(self):
987
- # super(Test, self).__init__()
988
- # self.x = jnp.Variable(jnp.ones(5))
989
- # self.y = jnp.Variable(jnp.ones(5))
990
- #
991
- # def __call__(self, *args, **kwargs):
992
- # return self.x ** 2 + self.y ** 2 + 10
993
- #
994
- # t = Test()
995
- #
996
- # g = bst.transform.vector_grad(t, grad_vars=t.x)()
997
- # self.assertTrue(jnp.array_equal(g, 2 * t.x))
998
- #
999
- # g = bst.transform.vector_grad(t, grad_vars=(t.x,))()
1000
- # self.assertTrue(jnp.array_equal(g[0], 2 * t.x))
1001
- #
1002
- # g = bst.transform.vector_grad(t, grad_vars=(t.x, t.y))()
1003
- # self.assertTrue(jnp.array_equal(g[0], 2 * t.x))
1004
- # self.assertTrue(jnp.array_equal(g[1], 2 * t.y))
1005
- #
1006
- #
1007
- # def vgrad(f, *x):
1008
- # y, vjp_fn = jax.vjp(f, *x)
1009
- # return vjp_fn(jnp.ones(y.shape).value)[0]
1010
- #
1011
- #
1012
- # class TestDebug(parameterized.TestCase):
1013
- # def test_debug1(self):
1014
- # a = bst.random.RandomState()
1015
- #
1016
- # def f(b):
1017
- # print(a.value)
1018
- # return a + b + a.random()
1019
- #
1020
- # f = bst.transform.vector_grad(f, argnums=0)
1021
- # f(1.)
1022
- #
1023
- # with jax.disable_jit():
1024
- # f(1.)
1025
- #
1026
- # @parameterized.product(
1027
- # grad_fun=[bst.transform.grad, bst.transform.vector_grad]
1028
- # )
1029
- # def test_print_info1(self, grad_fun):
1030
- # file = tempfile.TemporaryFile(mode='w+')
1031
- #
1032
- # @functools.partial(grad_fun, argnums=0)
1033
- # def f2(a, b):
1034
- # print('compiling f2 ...', file=file)
1035
- # return a + b
1036
- #
1037
- # @functools.partial(grad_fun, argnums=0)
1038
- # def f1(a):
1039
- # print('compiling f1 ...', file=file)
1040
- # return f2(a, 1.)
1041
- #
1042
- # expect_res = '''
1043
- # compiling f1 ...
1044
- # compiling f2 ...
1045
- # compiling f1 ...
1046
- # compiling f2 ...
1047
- # '''
1048
- #
1049
- # print(f1(1.))
1050
- # file.seek(0)
1051
- # self.assertTrue(file.read().strip() == expect_res.strip())
1052
- #
1053
- # file = tempfile.TemporaryFile(mode='w+')
1054
- # with jax.disable_jit():
1055
- # expect_res = '''
1056
- # compiling f1 ...
1057
- # compiling f2 ...
1058
- # '''
1059
- # self.assertTrue(f1(1.) == 0.)
1060
- # file.seek(0)
1061
- # self.assertTrue(file.read().strip() == expect_res.strip())
1062
- #
1063
- # @parameterized.product(
1064
- # grad_fun=[bst.transform.grad, bst.transform.vector_grad]
1065
- # )
1066
- # def test_print_info2(self, grad_fun):
1067
- # file = tempfile.TemporaryFile(mode='w+')
1068
- #
1069
- # @functools.partial(grad_fun, argnums=0)
1070
- # def f1(a):
1071
- # @functools.partial(grad_fun, argnums=0)
1072
- # def f2(a, b):
1073
- # print('compiling f2 ...', file=file)
1074
- # return a + b
1075
- #
1076
- # print('compiling f1 ...', file=file)
1077
- # return f2(a, 1.)
1078
- #
1079
- # expect_res = '''
1080
- # compiling f1 ...
1081
- # compiling f2 ...
1082
- # compiling f1 ...
1083
- # compiling f2 ...
1084
- # compiling f2 ...
1085
- # '''
1086
- # self.assertTrue(f1(1.) == 0.)
1087
- # file.seek(0)
1088
- # self.assertTrue(file.read().strip() == expect_res.strip())
1089
- #
1090
- # file = tempfile.TemporaryFile(mode='w+')
1091
- # with jax.disable_jit():
1092
- # expect_res = '''
1093
- # compiling f1 ...
1094
- # compiling f2 ...
1095
- # '''
1096
- # self.assertTrue(f1(1.) == 0.)
1097
- # file.seek(0)
1098
- # # print(file.read().strip())
1099
- # self.assertTrue(file.read().strip() == expect_res.strip())
1100
- #
1101
- # def test_debug_correctness1(self):
1102
- # def test_f():
1103
- # a = jnp.Variable(jnp.ones(2))
1104
- # b = jnp.Variable(jnp.zeros(2))
1105
- #
1106
- # @bst.transform.vector_grad(argnums=0)
1107
- # def f1(c):
1108
- # a.value += 1
1109
- # b.value += 10
1110
- # return a * b * c
1111
- #
1112
- # return a, b, f1(1.)
1113
- #
1114
- # r1 = test_f()
1115
- # print(r1)
1116
- #
1117
- # with jax.disable_jit():
1118
- # r2 = test_f()
1119
- # print(r2)
1120
- # self.assertTrue(jnp.allclose(r1[0], r2[0]))
1121
- # self.assertTrue(jnp.allclose(r1[1], r2[1]))
1122
- # self.assertTrue(jnp.allclose(r1[2], r2[2]))
1123
- #
1124
- # def f1(c, a, b):
1125
- # a += 1
1126
- # b += 10
1127
- # return a * b * c
1128
- #
1129
- # r3 = vgrad(f1, 1., jnp.ones(2).value, jnp.zeros(2).value)
1130
- # self.assertTrue(jnp.allclose(r1[2], r3))
1131
- #
1132
- # def _bench_f2(self, dd):
1133
- # a = jnp.Variable(jnp.ones(2))
1134
- # b = jnp.Variable(jnp.zeros(2))
1135
- #
1136
- #
1137
- # def run_fun(d):
1138
- # @bst.transform.vector_grad(argnums=0)
1139
- # def f1(c):
1140
- # a.value += d
1141
- # b.value += 10
1142
- # return a * b * c
1143
- #
1144
- # return a, b, f1(1.)
1145
- #
1146
- # return run_fun(dd)
1147
- #
1148
- # def test_debug_correctness2(self):
1149
- # r1 = self._bench_f2(1.)
1150
- # print(r1)
1151
- #
1152
- # with jax.disable_jit():
1153
- # r2 = self._bench_f2(1.)
1154
- # print(r2)
1155
- #
1156
- # self.assertTrue(jnp.allclose(r1[0], r2[0]))
1157
- # self.assertTrue(jnp.allclose(r1[1], r2[1]))
1158
- # self.assertTrue(jnp.allclose(r1[2], r2[2]))
1159
- #
1160
- # def test_cache1(self):
1161
- # file = tempfile.TemporaryFile(mode='w+')
1162
- #
1163
- # def f(a, b):
1164
- # print('compiling f ...', file=file)
1165
- # return a + b
1166
- #
1167
- # grad1 = bst.transform.grad(f)(1., 2.) # call "f" twice, one for Variable finding, one for compiling
1168
- # grad2 = bst.transform.vector_grad(f)(1., 2.) # call "f" once for compiling
1169
- #
1170
- # file.seek(0)
1171
- # print(file.read().strip())
1172
- #
1173
- # expect_res = '''
1174
- # compiling f ...
1175
- # compiling f ...
1176
- # compiling f ...
1177
- # '''
1178
- # file.seek(0)
1179
- # self.assertTrue(file.read().strip() == expect_res.strip())
1180
- #
1181
- #