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,546 +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
- from typing import Optional, Union
17
-
18
-
19
- import brainunit as u
20
- from brainstate._module import (register_delay_of_target,
21
- Projection,
22
- Module,
23
- Dynamics,
24
- ReceiveInputProj,
25
- ExtendedUpdateWithBA)
26
- from brainstate._utils import set_module_as
27
- from brainstate.mixin import (Mode, JointTypes, DelayedInitializer, BindCondData, AlignPost, UpdateReturn)
28
- from ._utils import is_instance
29
-
30
- __all__ = [
31
- 'HalfProjAlignPostMg', 'FullProjAlignPostMg',
32
- 'HalfProjAlignPost', 'FullProjAlignPost',
33
- ]
34
-
35
-
36
- def get_post_repr(out_label, syn, out):
37
- return f'{out_label} // {syn.identifier} // {out.identifier}'
38
-
39
-
40
- def align_post_add_bef_update(
41
- out_label: str,
42
- syn_desc,
43
- out_desc,
44
- post: JointTypes[ReceiveInputProj, ExtendedUpdateWithBA],
45
- proj_name: str
46
- ):
47
- # synapse and output initialization
48
- _post_repr = get_post_repr(out_label, syn_desc, out_desc)
49
- if not post.has_before_update(_post_repr):
50
- syn_cls = syn_desc()
51
- out_cls = out_desc()
52
-
53
- # synapse and output initialization
54
- post.add_input_fun(proj_name, out_cls, label=out_label)
55
- post.add_before_update(_post_repr, _AlignPost(syn_cls, out_cls))
56
- syn = post.get_before_update(_post_repr).syn
57
- out = post.get_before_update(_post_repr).out
58
- return syn, out
59
-
60
-
61
- class _AlignPost(Module):
62
- def __init__(
63
- self,
64
- syn: Module,
65
- out: JointTypes[Dynamics, BindCondData]
66
- ):
67
- super().__init__()
68
- self.syn = syn
69
- self.out = out
70
-
71
- def update(self, *args, **kwargs):
72
- self.out.bind_cond(self.syn(*args, **kwargs))
73
-
74
-
75
- @set_module_as('brainstate.nn')
76
- class HalfProjAlignPostMg(Projection):
77
- r"""
78
- Defining the half part of synaptic projection with the align-post reduction and the automatic synapse merging.
79
-
80
- The ``half-part`` means that the model only needs to provide half information needed for a projection,
81
- including ``comm`` -> ``syn`` -> ``out`` -> ``post``. Therefore, the model's ``update`` function needs
82
- the manual providing of the spiking input.
83
-
84
- The ``align-post`` means that the synaptic variables have the same dimension as the post-synaptic neuron group.
85
-
86
- The ``merging`` means that the same delay model is shared by all synapses, and the synapse model with same
87
- parameters (such like time constants) will also share the same synaptic variables.
88
-
89
- All align-post projection models prefer to use the event-driven computation mode. This means that the
90
- ``comm`` model should be the event-driven model.
91
-
92
- **Code Examples**
93
-
94
- To define an E/I balanced network model.
95
-
96
- .. code-block:: python
97
-
98
- import brainstate as bp
99
- import brainstate.math as bm
100
-
101
- class EINet(bp.DynSysGroup):
102
- def __init__(self):
103
- super().__init__()
104
- self.N = bp.dyn.LifRef(4000, V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.,
105
- V_initializer=bp.init.Normal(-55., 2.))
106
- self.delay = bp.VarDelay(self.N.spike, entries={'I': None})
107
- self.E = bp.dyn.HalfProjAlignPostMg(comm=bp.dnn.EventJitFPHomoLinear(3200, 4000, prob=0.02, weight=0.6),
108
- syn=bp.dyn.Expon.desc(size=4000, tau=5.),
109
- out=bp.dyn.COBA.desc(E=0.),
110
- post=self.N)
111
- self.I = bp.dyn.HalfProjAlignPostMg(comm=bp.dnn.EventJitFPHomoLinear(800, 4000, prob=0.02, weight=6.7),
112
- syn=bp.dyn.Expon.desc(size=4000, tau=10.),
113
- out=bp.dyn.COBA.desc(E=-80.),
114
- post=self.N)
115
-
116
- def update(self, input):
117
- spk = self.delay.at('I')
118
- self.E(spk[:3200])
119
- self.I(spk[3200:])
120
- self.delay(self.N(input))
121
- return self.N.spike.value
122
-
123
- model = EINet()
124
- indices = bm.arange(1000)
125
- spks = bm.for_loop(lambda i: model.step_run(i, 20.), indices)
126
- bp.visualize.raster_plot(indices, spks, show=True)
127
-
128
- Args:
129
- comm: The synaptic communication.
130
- syn: The synaptic dynamics.
131
- out: The synaptic output.
132
- post: The post-synaptic neuron group.
133
- out_label: str. The prefix of the output function.
134
- name: str. The projection name.
135
- mode: Mode. The computing mode.
136
- """
137
-
138
- _invisible_nodes = ['syn', 'out', 'post']
139
-
140
- def __init__(
141
- self,
142
- comm: Module,
143
- syn: DelayedInitializer[AlignPost],
144
- out: DelayedInitializer[BindCondData],
145
- post: JointTypes[ReceiveInputProj, ExtendedUpdateWithBA],
146
- out_label: Optional[str] = None,
147
- name: Optional[str] = None,
148
- mode: Optional[Mode] = None,
149
- ):
150
- super().__init__(name=name, mode=mode)
151
-
152
- # synaptic models
153
- is_instance(syn, DelayedInitializer[AlignPost])
154
- is_instance(out, DelayedInitializer[BindCondData])
155
- is_instance(post, JointTypes[ReceiveInputProj, ExtendedUpdateWithBA])
156
-
157
- # synapse and output initialization
158
- syn, out = align_post_add_bef_update(out_label, syn_desc=syn, out_desc=out, post=post, proj_name=self.name)
159
-
160
- # references
161
- self.post = post
162
- self.syn = syn
163
- self.out = out
164
- self.comm = comm
165
-
166
- def update(self, x):
167
- current = self.comm(x)
168
- self.syn.align_post_input_add(current) # synapse post current
169
- return current
170
-
171
-
172
- @set_module_as('brainstate.nn')
173
- class FullProjAlignPostMg(Projection):
174
- """Full-chain synaptic projection with the align-post reduction and the automatic synapse merging.
175
-
176
- The ``full-chain`` means that the model needs to provide all information needed for a projection,
177
- including ``pre`` -> ``delay`` -> ``comm`` -> ``syn`` -> ``out`` -> ``post``.
178
-
179
- The ``align-post`` means that the synaptic variables have the same dimension as the post-synaptic neuron group.
180
-
181
- The ``merging`` means that the same delay model is shared by all synapses, and the synapse model with same
182
- parameters (such like time constants) will also share the same synaptic variables.
183
-
184
- All align-post projection models prefer to use the event-driven computation mode. This means that the
185
- ``comm`` model should be the event-driven model.
186
-
187
- Moreover, it's worth noting that ``FullProjAlignPostMg`` has a different updating order with all align-pre
188
- projection models. The updating order of align-post projections is ``spikes`` -> ``comm`` -> ``syn`` -> ``out``.
189
- While, the updating order of all align-pre projection models is usually ``spikes`` -> ``syn`` -> ``comm`` -> ``out``.
190
-
191
- **Code Examples**
192
-
193
- To define an E/I balanced network model.
194
-
195
- .. code-block:: python
196
-
197
- import brainstate as bp
198
- import brainstate.math as bm
199
-
200
- class EINet(bp.DynSysGroup):
201
- def __init__(self):
202
- super().__init__()
203
- ne, ni = 3200, 800
204
- self.E = bp.dyn.LifRef(ne, V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.,
205
- V_initializer=bp.init.Normal(-55., 2.))
206
- self.I = bp.dyn.LifRef(ni, V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.,
207
- V_initializer=bp.init.Normal(-55., 2.))
208
- self.E2E = bp.dyn.FullProjAlignPostMg(pre=self.E,
209
- delay=0.1,
210
- comm=bp.dnn.EventJitFPHomoLinear(ne, ne, prob=0.02, weight=0.6),
211
- syn=bp.dyn.Expon.desc(size=ne, tau=5.),
212
- out=bp.dyn.COBA.desc(E=0.),
213
- post=self.E)
214
- self.E2I = bp.dyn.FullProjAlignPostMg(pre=self.E,
215
- delay=0.1,
216
- comm=bp.dnn.EventJitFPHomoLinear(ne, ni, prob=0.02, weight=0.6),
217
- syn=bp.dyn.Expon.desc(size=ni, tau=5.),
218
- out=bp.dyn.COBA.desc(E=0.),
219
- post=self.I)
220
- self.I2E = bp.dyn.FullProjAlignPostMg(pre=self.I,
221
- delay=0.1,
222
- comm=bp.dnn.EventJitFPHomoLinear(ni, ne, prob=0.02, weight=6.7),
223
- syn=bp.dyn.Expon.desc(size=ne, tau=10.),
224
- out=bp.dyn.COBA.desc(E=-80.),
225
- post=self.E)
226
- self.I2I = bp.dyn.FullProjAlignPostMg(pre=self.I,
227
- delay=0.1,
228
- comm=bp.dnn.EventJitFPHomoLinear(ni, ni, prob=0.02, weight=6.7),
229
- syn=bp.dyn.Expon.desc(size=ni, tau=10.),
230
- out=bp.dyn.COBA.desc(E=-80.),
231
- post=self.I)
232
-
233
- def update(self, inp):
234
- self.E2E()
235
- self.E2I()
236
- self.I2E()
237
- self.I2I()
238
- self.E(inp)
239
- self.I(inp)
240
- return self.E.spike
241
-
242
- model = EINet()
243
- indices = bm.arange(1000)
244
- spks = bm.for_loop(lambda i: model.step_run(i, 20.), indices)
245
- bp.visualize.raster_plot(indices, spks, show=True)
246
-
247
- Args:
248
- pre: The pre-synaptic neuron group.
249
- delay: The synaptic delay.
250
- comm: The synaptic communication.
251
- syn: The synaptic dynamics.
252
- out: The synaptic output.
253
- post: The post-synaptic neuron group.
254
- name: str. The projection name.
255
- mode: Mode. The computing mode.
256
- """
257
-
258
- _invisible_nodes = ['syn', 'out', 'post', 'pre', 'delay']
259
-
260
- def __init__(
261
- self,
262
- pre: JointTypes[ExtendedUpdateWithBA, UpdateReturn],
263
- delay: Union[None, int, float],
264
- comm: Module,
265
- syn: DelayedInitializer[AlignPost],
266
- out: DelayedInitializer[BindCondData],
267
- post: JointTypes[ReceiveInputProj, ExtendedUpdateWithBA],
268
- out_label: Optional[str] = None,
269
- name: Optional[str] = None,
270
- mode: Optional[Mode] = None,
271
- ):
272
- super().__init__(name=name, mode=mode)
273
-
274
- # synaptic models
275
- is_instance(pre, JointTypes[ExtendedUpdateWithBA, UpdateReturn])
276
- is_instance(comm, Module)
277
- is_instance(syn, DelayedInitializer[AlignPost])
278
- is_instance(out, DelayedInitializer[BindCondData])
279
- is_instance(post, JointTypes[ReceiveInputProj, ExtendedUpdateWithBA])
280
- self.comm = comm
281
-
282
- # delay initialization
283
- if delay is not None:
284
- if isinstance(delay, u.Quantity):
285
- has_delay = delay.mantissa > 0.
286
- else:
287
- has_delay = delay > 0.
288
- if has_delay:
289
- delay_cls = register_delay_of_target(pre)
290
- delay_cls.register_entry(self.name, delay)
291
- self.delay = delay_cls
292
- self.has_delay = True
293
- else:
294
- self.delay = None
295
- self.has_delay = False
296
- else:
297
- self.delay = None
298
- self.has_delay = False
299
-
300
- # synapse and output initialization
301
- syn, out = align_post_add_bef_update(out_label, syn_desc=syn, out_desc=out, post=post, proj_name=self.name)
302
-
303
- # references
304
- self.pre = pre
305
- self.post = post
306
- self.syn = syn
307
- self.out = out
308
-
309
- def update(self):
310
- if self.has_delay:
311
- x = self.delay.at(self.name)
312
- else:
313
- x = self.pre.update_return()
314
- current = self.comm(x)
315
- self.syn.align_post_input_add(current) # synapse post current
316
- return current
317
-
318
-
319
- @set_module_as('brainstate.nn')
320
- class HalfProjAlignPost(Projection):
321
- """Defining the half-part of synaptic projection with the align-post reduction.
322
-
323
- The ``half-part`` means that the model only needs to provide half information needed for a projection,
324
- including ``comm`` -> ``syn`` -> ``out`` -> ``post``. Therefore, the model's ``update`` function needs
325
- the manual providing of the spiking input.
326
-
327
- The ``align-post`` means that the synaptic variables have the same dimension as the post-synaptic neuron group.
328
-
329
- All align-post projection models prefer to use the event-driven computation mode. This means that the
330
- ``comm`` model should be the event-driven model.
331
-
332
- To simulate an E/I balanced network:
333
-
334
- .. code-block::
335
-
336
- class EINet(bp.DynSysGroup):
337
- def __init__(self):
338
- super().__init__()
339
- self.N = bp.dyn.LifRef(4000, V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.,
340
- V_initializer=bp.init.Normal(-55., 2.))
341
- self.delay = bp.VarDelay(self.N.spike, entries={'I': None})
342
- self.E = bp.dyn.HalfProjAlignPost(comm=bp.dnn.EventJitFPHomoLinear(3200, 4000, prob=0.02, weight=0.6),
343
- syn=bp.dyn.Expon(size=4000, tau=5.),
344
- out=bp.dyn.COBA(E=0.),
345
- post=self.N)
346
- self.I = bp.dyn.HalfProjAlignPost(comm=bp.dnn.EventJitFPHomoLinear(800, 4000, prob=0.02, weight=6.7),
347
- syn=bp.dyn.Expon(size=4000, tau=10.),
348
- out=bp.dyn.COBA(E=-80.),
349
- post=self.N)
350
-
351
- def update(self, input):
352
- spk = self.delay.at('I')
353
- self.E(spk[:3200])
354
- self.I(spk[3200:])
355
- self.delay(self.N(input))
356
- return self.N.spike.value
357
-
358
- model = EINet()
359
- indices = bm.arange(1000)
360
- spks = bm.for_loop(lambda i: model.step_run(i, 20.), indices)
361
- bp.visualize.raster_plot(indices, spks, show=True)
362
-
363
-
364
- Args:
365
- comm: The synaptic communication.
366
- syn: The synaptic dynamics.
367
- out: The synaptic output.
368
- post: The post-synaptic neuron group.
369
- name: str. The projection name.
370
- mode: Mode. The computing mode.
371
- """
372
-
373
- _invisible_nodes = ['out', 'post']
374
-
375
- def __init__(
376
- self,
377
- comm: Module,
378
- syn: AlignPost,
379
- out: BindCondData,
380
- post: ReceiveInputProj,
381
- out_label: Optional[str] = None,
382
- name: Optional[str] = None,
383
- mode: Optional[Mode] = None,
384
- ):
385
- super().__init__(name=name, mode=mode)
386
-
387
- # synaptic models
388
- is_instance(comm, Module)
389
- is_instance(syn, AlignPost)
390
- is_instance(out, BindCondData)
391
- is_instance(post, Module)
392
- self.comm = comm
393
- self.syn = syn
394
-
395
- # synapse and output initialization
396
- post.add_input_fun(self.name, out, label=out_label)
397
-
398
- # reference
399
- self.post = post
400
- self.out = out
401
-
402
- def update(self, x):
403
- current = self.comm(x)
404
- g = self.syn(current)
405
- self.out.bind_cond(g) # synapse post current
406
- return current
407
-
408
-
409
- @set_module_as('brainstate.nn')
410
- class FullProjAlignPost(Projection):
411
- """Full-chain synaptic projection with the align-post reduction.
412
-
413
- The ``full-chain`` means that the model needs to provide all information needed for a projection,
414
- including ``pre`` -> ``delay`` -> ``comm`` -> ``syn`` -> ``out`` -> ``post``.
415
-
416
- The ``align-post`` means that the synaptic variables have the same dimension as the post-synaptic neuron group.
417
-
418
- All align-post projection models prefer to use the event-driven computation mode. This means that the
419
- ``comm`` model should be the event-driven model.
420
-
421
- Moreover, it's worth noting that ``FullProjAlignPost`` has a different updating order with all align-pre
422
- projection models. The updating order of align-post projections is ``spikes`` -> ``comm`` -> ``syn`` -> ``out``.
423
- While, the updating order of all align-pre projection models is usually ``spikes`` -> ``syn`` -> ``comm`` -> ``out``.
424
-
425
- To simulate and define an E/I balanced network model:
426
-
427
- .. code-block:: python
428
-
429
- class EINet(bp.DynSysGroup):
430
- def __init__(self):
431
- super().__init__()
432
- ne, ni = 3200, 800
433
- self.E = bp.dyn.LifRef(ne, V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.,
434
- V_initializer=bp.init.Normal(-55., 2.))
435
- self.I = bp.dyn.LifRef(ni, V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.,
436
- V_initializer=bp.init.Normal(-55., 2.))
437
- self.E2E = bp.dyn.FullProjAlignPost(pre=self.E,
438
- delay=0.1,
439
- comm=bp.dnn.EventJitFPHomoLinear(ne, ne, prob=0.02, weight=0.6),
440
- syn=bp.dyn.Expon(size=ne, tau=5.),
441
- out=bp.dyn.COBA(E=0.),
442
- post=self.E)
443
- self.E2I = bp.dyn.FullProjAlignPost(pre=self.E,
444
- delay=0.1,
445
- comm=bp.dnn.EventJitFPHomoLinear(ne, ni, prob=0.02, weight=0.6),
446
- syn=bp.dyn.Expon(size=ni, tau=5.),
447
- out=bp.dyn.COBA(E=0.),
448
- post=self.I)
449
- self.I2E = bp.dyn.FullProjAlignPost(pre=self.I,
450
- delay=0.1,
451
- comm=bp.dnn.EventJitFPHomoLinear(ni, ne, prob=0.02, weight=6.7),
452
- syn=bp.dyn.Expon(size=ne, tau=10.),
453
- out=bp.dyn.COBA(E=-80.),
454
- post=self.E)
455
- self.I2I = bp.dyn.FullProjAlignPost(pre=self.I,
456
- delay=0.1,
457
- comm=bp.dnn.EventJitFPHomoLinear(ni, ni, prob=0.02, weight=6.7),
458
- syn=bp.dyn.Expon(size=ni, tau=10.),
459
- out=bp.dyn.COBA(E=-80.),
460
- post=self.I)
461
-
462
- def update(self, inp):
463
- self.E2E()
464
- self.E2I()
465
- self.I2E()
466
- self.I2I()
467
- self.E(inp)
468
- self.I(inp)
469
- return self.E.spike
470
-
471
- model = EINet()
472
- indices = bm.arange(1000)
473
- spks = bm.for_loop(lambda i: model.step_run(i, 20.), indices)
474
- bp.visualize.raster_plot(indices, spks, show=True)
475
-
476
-
477
- Args:
478
- pre: The pre-synaptic neuron group.
479
- delay: The synaptic delay.
480
- comm: The synaptic communication.
481
- syn: The synaptic dynamics.
482
- out: The synaptic output.
483
- post: The post-synaptic neuron group.
484
- name: str. The projection name.
485
- mode: Mode. The computing mode.
486
- """
487
-
488
- _invisible_nodes = ['post', 'pre', 'delay']
489
-
490
- def __init__(
491
- self,
492
- pre: UpdateReturn,
493
- delay: Union[None, int, float],
494
- comm: Module,
495
- syn: AlignPost,
496
- out: BindCondData,
497
- post: ReceiveInputProj,
498
- out_label: Optional[str] = None,
499
- name: Optional[str] = None,
500
- mode: Optional[Mode] = None,
501
- ):
502
- super().__init__(name=name, mode=mode)
503
-
504
- # synaptic models
505
- is_instance(pre, UpdateReturn)
506
- is_instance(comm, Module)
507
- is_instance(syn, JointTypes[Dynamics, AlignPost])
508
- is_instance(out, JointTypes[Dynamics, BindCondData])
509
- is_instance(post, ReceiveInputProj)
510
- self.comm = comm
511
- self.syn = syn
512
- self.out = out
513
-
514
- # delay initialization
515
- if delay is not None:
516
- if isinstance(delay, u.Quantity):
517
- has_delay = delay.mantissa > 0.
518
- else:
519
- has_delay = delay > 0.
520
- if has_delay:
521
- delay_cls = register_delay_of_target(pre)
522
- delay_cls.register_entry(self.name, delay)
523
- self.delay = delay_cls
524
- self.has_delay = True
525
- else:
526
- self.delay = None
527
- self.has_delay = False
528
- else:
529
- self.delay = None
530
- self.has_delay = False
531
-
532
- # synapse and output initialization
533
- post.add_input_fun(self.name, out, label=out_label)
534
-
535
- # references
536
- self.post = post
537
- self.pre = pre
538
-
539
- def update(self):
540
- if self.has_delay:
541
- x = self.delay.at(self.name)
542
- else:
543
- x = self.pre.update_return()
544
- g = self.syn(self.comm(x))
545
- self.out.bind_cond(g) # synapse post current
546
- return g