brainstate 0.1.10__py2.py3-none-any.whl → 0.2.1__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. brainstate/__init__.py +169 -58
  2. brainstate/_compatible_import.py +340 -148
  3. brainstate/_compatible_import_test.py +681 -0
  4. brainstate/_deprecation.py +210 -0
  5. brainstate/_deprecation_test.py +2319 -0
  6. brainstate/{util/error.py → _error.py} +45 -55
  7. brainstate/_state.py +1652 -1605
  8. brainstate/_state_test.py +52 -52
  9. brainstate/_utils.py +47 -47
  10. brainstate/environ.py +1495 -563
  11. brainstate/environ_test.py +1223 -62
  12. brainstate/graph/__init__.py +22 -29
  13. brainstate/graph/_node.py +240 -0
  14. brainstate/graph/_node_test.py +589 -0
  15. brainstate/graph/{_graph_operation.py → _operation.py} +1624 -1738
  16. brainstate/graph/_operation_test.py +1147 -0
  17. brainstate/mixin.py +1433 -365
  18. brainstate/mixin_test.py +1017 -77
  19. brainstate/nn/__init__.py +137 -135
  20. brainstate/nn/_activations.py +1100 -808
  21. brainstate/nn/_activations_test.py +354 -331
  22. brainstate/nn/_collective_ops.py +633 -514
  23. brainstate/nn/_collective_ops_test.py +774 -43
  24. brainstate/nn/_common.py +226 -178
  25. brainstate/nn/_common_test.py +154 -0
  26. brainstate/nn/_conv.py +2010 -501
  27. brainstate/nn/_conv_test.py +849 -238
  28. brainstate/nn/_delay.py +575 -588
  29. brainstate/nn/_delay_test.py +243 -238
  30. brainstate/nn/_dropout.py +618 -426
  31. brainstate/nn/_dropout_test.py +477 -100
  32. brainstate/nn/_dynamics.py +1267 -1343
  33. brainstate/nn/_dynamics_test.py +67 -78
  34. brainstate/nn/_elementwise.py +1298 -1119
  35. brainstate/nn/_elementwise_test.py +830 -169
  36. brainstate/nn/_embedding.py +408 -58
  37. brainstate/nn/_embedding_test.py +156 -0
  38. brainstate/nn/{_fixedprob.py → _event_fixedprob.py} +233 -239
  39. brainstate/nn/{_fixedprob_test.py → _event_fixedprob_test.py} +115 -114
  40. brainstate/nn/{_linear_mv.py → _event_linear.py} +83 -83
  41. brainstate/nn/{_linear_mv_test.py → _event_linear_test.py} +121 -120
  42. brainstate/nn/_exp_euler.py +254 -92
  43. brainstate/nn/_exp_euler_test.py +377 -35
  44. brainstate/nn/_linear.py +744 -424
  45. brainstate/nn/_linear_test.py +475 -107
  46. brainstate/nn/_metrics.py +1070 -0
  47. brainstate/nn/_metrics_test.py +611 -0
  48. brainstate/nn/_module.py +384 -377
  49. brainstate/nn/_module_test.py +40 -40
  50. brainstate/nn/_normalizations.py +1334 -975
  51. brainstate/nn/_normalizations_test.py +699 -73
  52. brainstate/nn/_paddings.py +1020 -0
  53. brainstate/nn/_paddings_test.py +723 -0
  54. brainstate/nn/_poolings.py +2239 -1177
  55. brainstate/nn/_poolings_test.py +953 -217
  56. brainstate/nn/{_rate_rnns.py → _rnns.py} +946 -554
  57. brainstate/nn/_rnns_test.py +593 -0
  58. brainstate/nn/_utils.py +216 -89
  59. brainstate/nn/_utils_test.py +402 -0
  60. brainstate/{init/_random_inits.py → nn/init.py} +809 -553
  61. brainstate/{init/_random_inits_test.py → nn/init_test.py} +180 -149
  62. brainstate/random/__init__.py +270 -24
  63. brainstate/random/_rand_funs.py +3938 -3616
  64. brainstate/random/_rand_funs_test.py +640 -567
  65. brainstate/random/_rand_seed.py +675 -210
  66. brainstate/random/_rand_seed_test.py +48 -48
  67. brainstate/random/_rand_state.py +1617 -1409
  68. brainstate/random/_rand_state_test.py +551 -0
  69. brainstate/transform/__init__.py +59 -0
  70. brainstate/transform/_ad_checkpoint.py +176 -0
  71. brainstate/{compile → transform}/_ad_checkpoint_test.py +49 -49
  72. brainstate/{augment → transform}/_autograd.py +1025 -778
  73. brainstate/{augment → transform}/_autograd_test.py +1289 -1289
  74. brainstate/transform/_conditions.py +316 -0
  75. brainstate/{compile → transform}/_conditions_test.py +220 -220
  76. brainstate/{compile → transform}/_error_if.py +94 -92
  77. brainstate/{compile → transform}/_error_if_test.py +52 -52
  78. brainstate/transform/_eval_shape.py +145 -0
  79. brainstate/{augment → transform}/_eval_shape_test.py +38 -38
  80. brainstate/{compile → transform}/_jit.py +399 -346
  81. brainstate/{compile → transform}/_jit_test.py +143 -143
  82. brainstate/{compile → transform}/_loop_collect_return.py +675 -536
  83. brainstate/{compile → transform}/_loop_collect_return_test.py +58 -58
  84. brainstate/{compile → transform}/_loop_no_collection.py +283 -184
  85. brainstate/{compile → transform}/_loop_no_collection_test.py +50 -50
  86. brainstate/transform/_make_jaxpr.py +2016 -0
  87. brainstate/transform/_make_jaxpr_test.py +1510 -0
  88. brainstate/transform/_mapping.py +529 -0
  89. brainstate/transform/_mapping_test.py +194 -0
  90. brainstate/{compile → transform}/_progress_bar.py +255 -202
  91. brainstate/{augment → transform}/_random.py +171 -151
  92. brainstate/{compile → transform}/_unvmap.py +256 -159
  93. brainstate/transform/_util.py +286 -0
  94. brainstate/typing.py +837 -304
  95. brainstate/typing_test.py +780 -0
  96. brainstate/util/__init__.py +27 -50
  97. brainstate/util/_others.py +1025 -0
  98. brainstate/util/_others_test.py +962 -0
  99. brainstate/util/_pretty_pytree.py +1301 -0
  100. brainstate/util/_pretty_pytree_test.py +675 -0
  101. brainstate/util/{pretty_repr.py → _pretty_repr.py} +462 -328
  102. brainstate/util/_pretty_repr_test.py +696 -0
  103. brainstate/util/filter.py +945 -469
  104. brainstate/util/filter_test.py +912 -0
  105. brainstate/util/struct.py +910 -523
  106. brainstate/util/struct_test.py +602 -0
  107. {brainstate-0.1.10.dist-info → brainstate-0.2.1.dist-info}/METADATA +108 -91
  108. brainstate-0.2.1.dist-info/RECORD +111 -0
  109. {brainstate-0.1.10.dist-info → brainstate-0.2.1.dist-info}/licenses/LICENSE +202 -202
  110. brainstate/augment/__init__.py +0 -30
  111. brainstate/augment/_eval_shape.py +0 -99
  112. brainstate/augment/_mapping.py +0 -1060
  113. brainstate/augment/_mapping_test.py +0 -597
  114. brainstate/compile/__init__.py +0 -38
  115. brainstate/compile/_ad_checkpoint.py +0 -204
  116. brainstate/compile/_conditions.py +0 -256
  117. brainstate/compile/_make_jaxpr.py +0 -888
  118. brainstate/compile/_make_jaxpr_test.py +0 -156
  119. brainstate/compile/_util.py +0 -147
  120. brainstate/functional/__init__.py +0 -27
  121. brainstate/graph/_graph_node.py +0 -244
  122. brainstate/graph/_graph_node_test.py +0 -73
  123. brainstate/graph/_graph_operation_test.py +0 -563
  124. brainstate/init/__init__.py +0 -26
  125. brainstate/init/_base.py +0 -52
  126. brainstate/init/_generic.py +0 -244
  127. brainstate/init/_regular_inits.py +0 -105
  128. brainstate/init/_regular_inits_test.py +0 -50
  129. brainstate/nn/_inputs.py +0 -608
  130. brainstate/nn/_ltp.py +0 -28
  131. brainstate/nn/_neuron.py +0 -705
  132. brainstate/nn/_neuron_test.py +0 -161
  133. brainstate/nn/_others.py +0 -46
  134. brainstate/nn/_projection.py +0 -486
  135. brainstate/nn/_rate_rnns_test.py +0 -63
  136. brainstate/nn/_readout.py +0 -209
  137. brainstate/nn/_readout_test.py +0 -53
  138. brainstate/nn/_stp.py +0 -236
  139. brainstate/nn/_synapse.py +0 -505
  140. brainstate/nn/_synapse_test.py +0 -131
  141. brainstate/nn/_synaptic_projection.py +0 -423
  142. brainstate/nn/_synouts.py +0 -162
  143. brainstate/nn/_synouts_test.py +0 -57
  144. brainstate/nn/metrics.py +0 -388
  145. brainstate/optim/__init__.py +0 -38
  146. brainstate/optim/_base.py +0 -64
  147. brainstate/optim/_lr_scheduler.py +0 -448
  148. brainstate/optim/_lr_scheduler_test.py +0 -50
  149. brainstate/optim/_optax_optimizer.py +0 -152
  150. brainstate/optim/_optax_optimizer_test.py +0 -53
  151. brainstate/optim/_sgd_optimizer.py +0 -1104
  152. brainstate/random/_random_for_unit.py +0 -52
  153. brainstate/surrogate.py +0 -1957
  154. brainstate/transform.py +0 -23
  155. brainstate/util/caller.py +0 -98
  156. brainstate/util/others.py +0 -540
  157. brainstate/util/pretty_pytree.py +0 -945
  158. brainstate/util/pretty_pytree_test.py +0 -159
  159. brainstate/util/pretty_table.py +0 -2954
  160. brainstate/util/scaling.py +0 -258
  161. brainstate-0.1.10.dist-info/RECORD +0 -130
  162. {brainstate-0.1.10.dist-info → brainstate-0.2.1.dist-info}/WHEEL +0 -0
  163. {brainstate-0.1.10.dist-info → brainstate-0.2.1.dist-info}/top_level.txt +0 -0
@@ -1,238 +1,243 @@
1
- # Copyright 2025 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
-
17
- import unittest
18
-
19
- import jax.numpy as jnp
20
-
21
- import brainstate
22
-
23
- brainstate.environ.set(dt=0.1)
24
-
25
-
26
- class TestDelay(unittest.TestCase):
27
- def test_delay1(self):
28
- a = brainstate.State(brainstate.random.random(10, 20))
29
- delay = brainstate.nn.Delay(a.value)
30
- delay.register_entry('a', 1.)
31
- delay.register_entry('b', 2.)
32
- delay.register_entry('c', None)
33
-
34
- delay.init_state()
35
- with self.assertRaises(KeyError):
36
- delay.register_entry('c', 10.)
37
-
38
- def test_rotation_delay(self):
39
- rotation_delay = brainstate.nn.Delay(jnp.ones((1,)))
40
- t0 = 0.
41
- t1, n1 = 1., 10
42
- t2, n2 = 2., 20
43
-
44
- rotation_delay.register_entry('a', t0)
45
- rotation_delay.register_entry('b', t1)
46
- rotation_delay.register_entry('c2', 1.9)
47
- rotation_delay.register_entry('c', t2)
48
-
49
- rotation_delay.init_state()
50
-
51
- print()
52
- # print(rotation_delay)
53
- # print(rotation_delay.max_length)
54
-
55
- for i in range(100):
56
- brainstate.environ.set(i=i)
57
- rotation_delay.update(jnp.ones((1,)) * i)
58
- # print(i, rotation_delay.at('a'), rotation_delay.at('b'), rotation_delay.at('c2'), rotation_delay.at('c'))
59
- self.assertTrue(jnp.allclose(rotation_delay.at('a'), jnp.ones((1,)) * i))
60
- self.assertTrue(jnp.allclose(rotation_delay.at('b'), jnp.maximum(jnp.ones((1,)) * i - n1, 0.)))
61
- self.assertTrue(jnp.allclose(rotation_delay.at('c'), jnp.maximum(jnp.ones((1,)) * i - n2, 0.)))
62
-
63
- def test_concat_delay(self):
64
- rotation_delay = brainstate.nn.Delay(jnp.ones([1]), delay_method='concat')
65
- t0 = 0.
66
- t1, n1 = 1., 10
67
- t2, n2 = 2., 20
68
-
69
- rotation_delay.register_entry('a', t0)
70
- rotation_delay.register_entry('b', t1)
71
- rotation_delay.register_entry('c', t2)
72
-
73
- rotation_delay.init_state()
74
-
75
- print()
76
- for i in range(100):
77
- brainstate.environ.set(i=i)
78
- rotation_delay.update(jnp.ones((1,)) * i)
79
- print(i, rotation_delay.at('a'), rotation_delay.at('b'), rotation_delay.at('c'))
80
- self.assertTrue(jnp.allclose(rotation_delay.at('a'), jnp.ones((1,)) * i))
81
- self.assertTrue(jnp.allclose(rotation_delay.at('b'), jnp.maximum(jnp.ones((1,)) * i - n1, 0.)))
82
- self.assertTrue(jnp.allclose(rotation_delay.at('c'), jnp.maximum(jnp.ones((1,)) * i - n2, 0.)))
83
- # brainstate.util.clear_buffer_memory()
84
-
85
- def test_jit_erro(self):
86
- rotation_delay = brainstate.nn.Delay(jnp.ones([1]), time=2., delay_method='concat', interp_method='round')
87
- rotation_delay.init_state()
88
-
89
- with brainstate.environ.context(i=0, t=0, jit_error_check=True):
90
- rotation_delay.retrieve_at_time(-2.0)
91
- with self.assertRaises(Exception):
92
- rotation_delay.retrieve_at_time(-2.1)
93
- rotation_delay.retrieve_at_time(-2.01)
94
- with self.assertRaises(Exception):
95
- rotation_delay.retrieve_at_time(-2.09)
96
- with self.assertRaises(Exception):
97
- rotation_delay.retrieve_at_time(0.1)
98
- with self.assertRaises(Exception):
99
- rotation_delay.retrieve_at_time(0.01)
100
-
101
- def test_round_interp(self):
102
- for shape in [(1,), (1, 1), (1, 1, 1)]:
103
- for delay_method in ['rotation', 'concat']:
104
- rotation_delay = brainstate.nn.Delay(jnp.ones(shape), time=2., delay_method=delay_method,
105
- interp_method='round')
106
- t0, n1 = 0.01, 0
107
- t1, n1 = 1.04, 10
108
- t2, n2 = 1.06, 11
109
- rotation_delay.init_state()
110
-
111
- @brainstate.compile.jit
112
- def retrieve(td, i):
113
- with brainstate.environ.context(i=i, t=i * brainstate.environ.get_dt()):
114
- return rotation_delay.retrieve_at_time(td)
115
-
116
- print()
117
- for i in range(100):
118
- t = i * brainstate.environ.get_dt()
119
- with brainstate.environ.context(i=i, t=t):
120
- rotation_delay.update(jnp.ones(shape) * i)
121
- print(i,
122
- retrieve(t - t0, i),
123
- retrieve(t - t1, i),
124
- retrieve(t - t2, i))
125
- self.assertTrue(jnp.allclose(retrieve(t - t0, i), jnp.ones(shape) * i))
126
- self.assertTrue(jnp.allclose(retrieve(t - t1, i), jnp.maximum(jnp.ones(shape) * i - n1, 0.)))
127
- self.assertTrue(jnp.allclose(retrieve(t - t2, i), jnp.maximum(jnp.ones(shape) * i - n2, 0.)))
128
-
129
- def test_linear_interp(self):
130
- for shape in [(1,), (1, 1), (1, 1, 1)]:
131
- for delay_method in ['rotation', 'concat']:
132
- print(shape, delay_method)
133
-
134
- rotation_delay = brainstate.nn.Delay(jnp.ones(shape), time=2., delay_method=delay_method,
135
- interp_method='linear_interp')
136
- t0, n0 = 0.01, 0.1
137
- t1, n1 = 1.04, 10.4
138
- t2, n2 = 1.06, 10.6
139
- rotation_delay.init_state()
140
-
141
- @brainstate.compile.jit
142
- def retrieve(td, i):
143
- with brainstate.environ.context(i=i, t=i * brainstate.environ.get_dt()):
144
- return rotation_delay.retrieve_at_time(td)
145
-
146
- print()
147
- for i in range(100):
148
- t = i * brainstate.environ.get_dt()
149
- with brainstate.environ.context(i=i, t=t):
150
- rotation_delay.update(jnp.ones(shape) * i)
151
- print(i,
152
- retrieve(t - t0, i),
153
- retrieve(t - t1, i),
154
- retrieve(t - t2, i))
155
- self.assertTrue(jnp.allclose(retrieve(t - t0, i), jnp.maximum(jnp.ones(shape) * i - n0, 0.)))
156
- self.assertTrue(jnp.allclose(retrieve(t - t1, i), jnp.maximum(jnp.ones(shape) * i - n1, 0.)))
157
- self.assertTrue(jnp.allclose(retrieve(t - t2, i), jnp.maximum(jnp.ones(shape) * i - n2, 0.)))
158
-
159
- def test_rotation_and_concat_delay(self):
160
- rotation_delay = brainstate.nn.Delay(jnp.ones((1,)))
161
- concat_delay = brainstate.nn.Delay(jnp.ones([1]), delay_method='concat')
162
- t0 = 0.
163
- t1, n1 = 1., 10
164
- t2, n2 = 2., 20
165
-
166
- rotation_delay.register_entry('a', t0)
167
- rotation_delay.register_entry('b', t1)
168
- rotation_delay.register_entry('c', t2)
169
- concat_delay.register_entry('a', t0)
170
- concat_delay.register_entry('b', t1)
171
- concat_delay.register_entry('c', t2)
172
-
173
- rotation_delay.init_state()
174
- concat_delay.init_state()
175
-
176
- print()
177
- for i in range(100):
178
- brainstate.environ.set(i=i)
179
- new = jnp.ones((1,)) * i
180
- rotation_delay.update(new)
181
- concat_delay.update(new)
182
- self.assertTrue(jnp.allclose(rotation_delay.at('a'), concat_delay.at('a'), ))
183
- self.assertTrue(jnp.allclose(rotation_delay.at('b'), concat_delay.at('b'), ))
184
- self.assertTrue(jnp.allclose(rotation_delay.at('c'), concat_delay.at('c'), ))
185
-
186
- def test_delay_2d(self):
187
- with brainstate.environ.context(dt=0.1, i=0):
188
- rotation_delay = brainstate.nn.Delay(jnp.arange(2))
189
- index = (brainstate.random.uniform(0., 10., (2, 2)),
190
- brainstate.random.randint(0, 2, (2, 2)))
191
- rotation_delay.register_entry('a', *index)
192
- rotation_delay.init_state()
193
- data = rotation_delay.at('a')
194
- print(index[0])
195
- print(index[1])
196
- print(data)
197
- assert data.shape == (2, 2)
198
-
199
- def test_delay_time2(self):
200
- with brainstate.environ.context(dt=0.1, i=0):
201
- rotation_delay = brainstate.nn.Delay(jnp.arange(2))
202
- index = (brainstate.random.uniform(0., 10., (2, 2)),
203
- 1)
204
- rotation_delay.register_entry('a', *index)
205
- rotation_delay.init_state()
206
- data = rotation_delay.at('a')
207
- print(index[0])
208
- print(index[1])
209
- print(data)
210
- assert data.shape == (2, 2)
211
-
212
- def test_delay_time3(self):
213
- with brainstate.environ.context(dt=0.1, i=0):
214
- rotation_delay = brainstate.nn.Delay(jnp.zeros((2, 2)))
215
- index = (brainstate.random.uniform(0., 10., (2, 2)),
216
- 1,
217
- brainstate.random.randint(0, 2, (2, 2)))
218
- rotation_delay.register_entry('a', *index)
219
- rotation_delay.init_state()
220
- data = rotation_delay.at('a')
221
- print(index[0])
222
- print(index[1])
223
- print(data)
224
- assert data.shape == (2, 2)
225
-
226
- def test_delay_time4(self):
227
- with brainstate.environ.context(dt=0.1, i=0):
228
- rotation_delay = brainstate.nn.Delay(jnp.zeros((2, 2)))
229
- index = (brainstate.random.uniform(0., 10., (2, 2)),
230
- 1,
231
- brainstate.random.randint(0, 2, (2, 2)))
232
- rotation_delay.register_entry('a', *index)
233
- rotation_delay.init_state()
234
- data = rotation_delay.at('a')
235
- print(index[0])
236
- print(index[1])
237
- print(data)
238
- assert data.shape == (2, 2)
1
+ # Copyright 2025 BrainX 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
+
17
+ import unittest
18
+
19
+ import jax.numpy as jnp
20
+
21
+ import brainstate
22
+
23
+
24
+ class TestDelay(unittest.TestCase):
25
+ def setUp(self):
26
+ brainstate.environ.set(dt=0.1)
27
+
28
+ def tearDown(self):
29
+ brainstate.environ.pop('dt')
30
+
31
+ def test_delay1(self):
32
+ a = brainstate.State(brainstate.random.random(10, 20))
33
+ delay = brainstate.nn.Delay(a.value)
34
+ delay.register_entry('a', 1.)
35
+ delay.register_entry('b', 2.)
36
+ delay.register_entry('c', None)
37
+
38
+ delay.init_state()
39
+ with self.assertRaises(KeyError):
40
+ delay.register_entry('c', 10.)
41
+
42
+ def test_rotation_delay(self):
43
+ rotation_delay = brainstate.nn.Delay(jnp.ones((1,)))
44
+ t0 = 0.
45
+ t1, n1 = 1., 10
46
+ t2, n2 = 2., 20
47
+
48
+ rotation_delay.register_entry('a', t0)
49
+ rotation_delay.register_entry('b', t1)
50
+ rotation_delay.register_entry('c2', 1.9)
51
+ rotation_delay.register_entry('c', t2)
52
+
53
+ rotation_delay.init_state()
54
+
55
+ print()
56
+ # print(rotation_delay)
57
+ # print(rotation_delay.max_length)
58
+
59
+ for i in range(100):
60
+ brainstate.environ.set(i=i)
61
+ rotation_delay.update(jnp.ones((1,)) * i)
62
+ # print(i, rotation_delay.at('a'), rotation_delay.at('b'), rotation_delay.at('c2'), rotation_delay.at('c'))
63
+ self.assertTrue(jnp.allclose(rotation_delay.at('a'), jnp.ones((1,)) * i))
64
+ self.assertTrue(jnp.allclose(rotation_delay.at('b'), jnp.maximum(jnp.ones((1,)) * i - n1, 0.)))
65
+ self.assertTrue(jnp.allclose(rotation_delay.at('c'), jnp.maximum(jnp.ones((1,)) * i - n2, 0.)))
66
+
67
+ def test_concat_delay(self):
68
+ with brainstate.environ.context(dt=0.1) as env:
69
+ rotation_delay = brainstate.nn.Delay(jnp.ones([1]), delay_method='concat')
70
+ t0 = 0.
71
+ t1, n1 = 1., 10
72
+ t2, n2 = 2., 20
73
+
74
+ rotation_delay.register_entry('a', t0)
75
+ rotation_delay.register_entry('b', t1)
76
+ rotation_delay.register_entry('c', t2)
77
+
78
+ rotation_delay.init_state()
79
+
80
+ print()
81
+ for i in range(100):
82
+ brainstate.environ.set(i=i)
83
+ rotation_delay.update(jnp.ones((1,)) * i)
84
+ print(i, rotation_delay.at('a'), rotation_delay.at('b'), rotation_delay.at('c'))
85
+ self.assertTrue(jnp.allclose(rotation_delay.at('a'), jnp.ones((1,)) * i))
86
+ self.assertTrue(jnp.allclose(rotation_delay.at('b'), jnp.maximum(jnp.ones((1,)) * i - n1, 0.)))
87
+ self.assertTrue(jnp.allclose(rotation_delay.at('c'), jnp.maximum(jnp.ones((1,)) * i - n2, 0.)))
88
+ # brainstate.util.clear_buffer_memory()
89
+
90
+ def test_jit_erro(self):
91
+ rotation_delay = brainstate.nn.Delay(jnp.ones([1]), time=2., delay_method='concat', interp_method='round')
92
+ rotation_delay.init_state()
93
+
94
+ with brainstate.environ.context(i=0, t=0, jit_error_check=True):
95
+ rotation_delay.retrieve_at_time(-2.0)
96
+ with self.assertRaises(Exception):
97
+ rotation_delay.retrieve_at_time(-2.1)
98
+ rotation_delay.retrieve_at_time(-2.01)
99
+ with self.assertRaises(Exception):
100
+ rotation_delay.retrieve_at_time(-2.09)
101
+ with self.assertRaises(Exception):
102
+ rotation_delay.retrieve_at_time(0.1)
103
+ with self.assertRaises(Exception):
104
+ rotation_delay.retrieve_at_time(0.01)
105
+
106
+ def test_round_interp(self):
107
+ for shape in [(1,), (1, 1), (1, 1, 1)]:
108
+ for delay_method in ['rotation', 'concat']:
109
+ rotation_delay = brainstate.nn.Delay(jnp.ones(shape), time=2., delay_method=delay_method,
110
+ interp_method='round')
111
+ t0, n1 = 0.01, 0
112
+ t1, n1 = 1.04, 10
113
+ t2, n2 = 1.06, 11
114
+ rotation_delay.init_state()
115
+
116
+ @brainstate.compile.jit
117
+ def retrieve(td, i):
118
+ with brainstate.environ.context(i=i, t=i * brainstate.environ.get_dt()):
119
+ return rotation_delay.retrieve_at_time(td)
120
+
121
+ print()
122
+ for i in range(100):
123
+ t = i * brainstate.environ.get_dt()
124
+ with brainstate.environ.context(i=i, t=t):
125
+ rotation_delay.update(jnp.ones(shape) * i)
126
+ print(i,
127
+ retrieve(t - t0, i),
128
+ retrieve(t - t1, i),
129
+ retrieve(t - t2, i))
130
+ self.assertTrue(jnp.allclose(retrieve(t - t0, i), jnp.ones(shape) * i))
131
+ self.assertTrue(jnp.allclose(retrieve(t - t1, i), jnp.maximum(jnp.ones(shape) * i - n1, 0.)))
132
+ self.assertTrue(jnp.allclose(retrieve(t - t2, i), jnp.maximum(jnp.ones(shape) * i - n2, 0.)))
133
+
134
+ def test_linear_interp(self):
135
+ for shape in [(1,), (1, 1), (1, 1, 1)]:
136
+ for delay_method in ['rotation', 'concat']:
137
+ print(shape, delay_method)
138
+
139
+ rotation_delay = brainstate.nn.Delay(jnp.ones(shape), time=2., delay_method=delay_method,
140
+ interp_method='linear_interp')
141
+ t0, n0 = 0.01, 0.1
142
+ t1, n1 = 1.04, 10.4
143
+ t2, n2 = 1.06, 10.6
144
+ rotation_delay.init_state()
145
+
146
+ @brainstate.compile.jit
147
+ def retrieve(td, i):
148
+ with brainstate.environ.context(i=i, t=i * brainstate.environ.get_dt()):
149
+ return rotation_delay.retrieve_at_time(td)
150
+
151
+ print()
152
+ for i in range(100):
153
+ t = i * brainstate.environ.get_dt()
154
+ with brainstate.environ.context(i=i, t=t):
155
+ rotation_delay.update(jnp.ones(shape) * i)
156
+ print(i,
157
+ retrieve(t - t0, i),
158
+ retrieve(t - t1, i),
159
+ retrieve(t - t2, i))
160
+ self.assertTrue(jnp.allclose(retrieve(t - t0, i), jnp.maximum(jnp.ones(shape) * i - n0, 0.)))
161
+ self.assertTrue(jnp.allclose(retrieve(t - t1, i), jnp.maximum(jnp.ones(shape) * i - n1, 0.)))
162
+ self.assertTrue(jnp.allclose(retrieve(t - t2, i), jnp.maximum(jnp.ones(shape) * i - n2, 0.)))
163
+
164
+ def test_rotation_and_concat_delay(self):
165
+ rotation_delay = brainstate.nn.Delay(jnp.ones((1,)))
166
+ concat_delay = brainstate.nn.Delay(jnp.ones([1]), delay_method='concat')
167
+ t0 = 0.
168
+ t1, n1 = 1., 10
169
+ t2, n2 = 2., 20
170
+
171
+ rotation_delay.register_entry('a', t0)
172
+ rotation_delay.register_entry('b', t1)
173
+ rotation_delay.register_entry('c', t2)
174
+ concat_delay.register_entry('a', t0)
175
+ concat_delay.register_entry('b', t1)
176
+ concat_delay.register_entry('c', t2)
177
+
178
+ rotation_delay.init_state()
179
+ concat_delay.init_state()
180
+
181
+ print()
182
+ for i in range(100):
183
+ brainstate.environ.set(i=i)
184
+ new = jnp.ones((1,)) * i
185
+ rotation_delay.update(new)
186
+ concat_delay.update(new)
187
+ self.assertTrue(jnp.allclose(rotation_delay.at('a'), concat_delay.at('a'), ))
188
+ self.assertTrue(jnp.allclose(rotation_delay.at('b'), concat_delay.at('b'), ))
189
+ self.assertTrue(jnp.allclose(rotation_delay.at('c'), concat_delay.at('c'), ))
190
+
191
+ def test_delay_2d(self):
192
+ with brainstate.environ.context(dt=0.1, i=0):
193
+ rotation_delay = brainstate.nn.Delay(jnp.arange(2))
194
+ index = (brainstate.random.uniform(0., 10., (2, 2)),
195
+ brainstate.random.randint(0, 2, (2, 2)))
196
+ rotation_delay.register_entry('a', *index)
197
+ rotation_delay.init_state()
198
+ data = rotation_delay.at('a')
199
+ print(index[0])
200
+ print(index[1])
201
+ print(data)
202
+ assert data.shape == (2, 2)
203
+
204
+ def test_delay_time2(self):
205
+ with brainstate.environ.context(dt=0.1, i=0):
206
+ rotation_delay = brainstate.nn.Delay(jnp.arange(2))
207
+ index = (brainstate.random.uniform(0., 10., (2, 2)),
208
+ 1)
209
+ rotation_delay.register_entry('a', *index)
210
+ rotation_delay.init_state()
211
+ data = rotation_delay.at('a')
212
+ print(index[0])
213
+ print(index[1])
214
+ print(data)
215
+ assert data.shape == (2, 2)
216
+
217
+ def test_delay_time3(self):
218
+ with brainstate.environ.context(dt=0.1, i=0):
219
+ rotation_delay = brainstate.nn.Delay(jnp.zeros((2, 2)))
220
+ index = (brainstate.random.uniform(0., 10., (2, 2)),
221
+ 1,
222
+ brainstate.random.randint(0, 2, (2, 2)))
223
+ rotation_delay.register_entry('a', *index)
224
+ rotation_delay.init_state()
225
+ data = rotation_delay.at('a')
226
+ print(index[0])
227
+ print(index[1])
228
+ print(data)
229
+ assert data.shape == (2, 2)
230
+
231
+ def test_delay_time4(self):
232
+ with brainstate.environ.context(dt=0.1, i=0):
233
+ rotation_delay = brainstate.nn.Delay(jnp.zeros((2, 2)))
234
+ index = (brainstate.random.uniform(0., 10., (2, 2)),
235
+ 1,
236
+ brainstate.random.randint(0, 2, (2, 2)))
237
+ rotation_delay.register_entry('a', *index)
238
+ rotation_delay.init_state()
239
+ data = rotation_delay.at('a')
240
+ print(index[0])
241
+ print(index[1])
242
+ print(data)
243
+ assert data.shape == (2, 2)