brainstate 0.1.8__py2.py3-none-any.whl → 0.1.10__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 (133) hide show
  1. brainstate/__init__.py +58 -51
  2. brainstate/_compatible_import.py +148 -148
  3. brainstate/_state.py +1605 -1663
  4. brainstate/_state_test.py +52 -52
  5. brainstate/_utils.py +47 -47
  6. brainstate/augment/__init__.py +30 -30
  7. brainstate/augment/_autograd.py +778 -778
  8. brainstate/augment/_autograd_test.py +1289 -1289
  9. brainstate/augment/_eval_shape.py +99 -99
  10. brainstate/augment/_eval_shape_test.py +38 -38
  11. brainstate/augment/_mapping.py +1060 -1060
  12. brainstate/augment/_mapping_test.py +597 -597
  13. brainstate/augment/_random.py +151 -151
  14. brainstate/compile/__init__.py +38 -38
  15. brainstate/compile/_ad_checkpoint.py +204 -204
  16. brainstate/compile/_ad_checkpoint_test.py +49 -49
  17. brainstate/compile/_conditions.py +256 -256
  18. brainstate/compile/_conditions_test.py +220 -220
  19. brainstate/compile/_error_if.py +92 -92
  20. brainstate/compile/_error_if_test.py +52 -52
  21. brainstate/compile/_jit.py +346 -346
  22. brainstate/compile/_jit_test.py +143 -143
  23. brainstate/compile/_loop_collect_return.py +536 -536
  24. brainstate/compile/_loop_collect_return_test.py +58 -58
  25. brainstate/compile/_loop_no_collection.py +184 -184
  26. brainstate/compile/_loop_no_collection_test.py +50 -50
  27. brainstate/compile/_make_jaxpr.py +888 -888
  28. brainstate/compile/_make_jaxpr_test.py +156 -156
  29. brainstate/compile/_progress_bar.py +202 -202
  30. brainstate/compile/_unvmap.py +159 -159
  31. brainstate/compile/_util.py +147 -147
  32. brainstate/environ.py +563 -563
  33. brainstate/environ_test.py +62 -62
  34. brainstate/functional/__init__.py +27 -26
  35. brainstate/graph/__init__.py +29 -29
  36. brainstate/graph/_graph_node.py +244 -244
  37. brainstate/graph/_graph_node_test.py +73 -73
  38. brainstate/graph/_graph_operation.py +1738 -1738
  39. brainstate/graph/_graph_operation_test.py +563 -563
  40. brainstate/init/__init__.py +26 -26
  41. brainstate/init/_base.py +52 -52
  42. brainstate/init/_generic.py +244 -244
  43. brainstate/init/_random_inits.py +553 -553
  44. brainstate/init/_random_inits_test.py +149 -149
  45. brainstate/init/_regular_inits.py +105 -105
  46. brainstate/init/_regular_inits_test.py +50 -50
  47. brainstate/mixin.py +365 -363
  48. brainstate/mixin_test.py +77 -73
  49. brainstate/nn/__init__.py +135 -131
  50. brainstate/{functional → nn}/_activations.py +808 -813
  51. brainstate/{functional → nn}/_activations_test.py +331 -331
  52. brainstate/nn/_collective_ops.py +514 -514
  53. brainstate/nn/_collective_ops_test.py +43 -43
  54. brainstate/nn/_common.py +178 -178
  55. brainstate/nn/_conv.py +501 -501
  56. brainstate/nn/_conv_test.py +238 -238
  57. brainstate/nn/_delay.py +588 -502
  58. brainstate/nn/_delay_test.py +238 -184
  59. brainstate/nn/_dropout.py +426 -426
  60. brainstate/nn/_dropout_test.py +100 -100
  61. brainstate/nn/_dynamics.py +1343 -1343
  62. brainstate/nn/_dynamics_test.py +78 -78
  63. brainstate/nn/_elementwise.py +1119 -1119
  64. brainstate/nn/_elementwise_test.py +169 -169
  65. brainstate/nn/_embedding.py +58 -58
  66. brainstate/nn/_exp_euler.py +92 -92
  67. brainstate/nn/_exp_euler_test.py +35 -35
  68. brainstate/nn/_fixedprob.py +239 -239
  69. brainstate/nn/_fixedprob_test.py +114 -114
  70. brainstate/nn/_inputs.py +608 -608
  71. brainstate/nn/_linear.py +424 -424
  72. brainstate/nn/_linear_mv.py +83 -83
  73. brainstate/nn/_linear_mv_test.py +120 -120
  74. brainstate/nn/_linear_test.py +107 -107
  75. brainstate/nn/_ltp.py +28 -28
  76. brainstate/nn/_module.py +377 -377
  77. brainstate/nn/_module_test.py +40 -40
  78. brainstate/nn/_neuron.py +705 -705
  79. brainstate/nn/_neuron_test.py +161 -161
  80. brainstate/nn/_normalizations.py +975 -918
  81. brainstate/nn/_normalizations_test.py +73 -73
  82. brainstate/{functional → nn}/_others.py +46 -46
  83. brainstate/nn/_poolings.py +1177 -1177
  84. brainstate/nn/_poolings_test.py +217 -217
  85. brainstate/nn/_projection.py +486 -486
  86. brainstate/nn/_rate_rnns.py +554 -554
  87. brainstate/nn/_rate_rnns_test.py +63 -63
  88. brainstate/nn/_readout.py +209 -209
  89. brainstate/nn/_readout_test.py +53 -53
  90. brainstate/nn/_stp.py +236 -236
  91. brainstate/nn/_synapse.py +505 -505
  92. brainstate/nn/_synapse_test.py +131 -131
  93. brainstate/nn/_synaptic_projection.py +423 -423
  94. brainstate/nn/_synouts.py +162 -162
  95. brainstate/nn/_synouts_test.py +57 -57
  96. brainstate/nn/_utils.py +89 -89
  97. brainstate/nn/metrics.py +388 -388
  98. brainstate/optim/__init__.py +38 -38
  99. brainstate/optim/_base.py +64 -64
  100. brainstate/optim/_lr_scheduler.py +448 -448
  101. brainstate/optim/_lr_scheduler_test.py +50 -50
  102. brainstate/optim/_optax_optimizer.py +152 -152
  103. brainstate/optim/_optax_optimizer_test.py +53 -53
  104. brainstate/optim/_sgd_optimizer.py +1104 -1104
  105. brainstate/random/__init__.py +24 -24
  106. brainstate/random/_rand_funs.py +3616 -3616
  107. brainstate/random/_rand_funs_test.py +567 -567
  108. brainstate/random/_rand_seed.py +210 -210
  109. brainstate/random/_rand_seed_test.py +48 -48
  110. brainstate/random/_rand_state.py +1409 -1409
  111. brainstate/random/_random_for_unit.py +52 -52
  112. brainstate/surrogate.py +1957 -1957
  113. brainstate/transform.py +23 -23
  114. brainstate/typing.py +304 -304
  115. brainstate/util/__init__.py +50 -50
  116. brainstate/util/caller.py +98 -98
  117. brainstate/util/error.py +55 -55
  118. brainstate/util/filter.py +469 -469
  119. brainstate/util/others.py +540 -540
  120. brainstate/util/pretty_pytree.py +945 -945
  121. brainstate/util/pretty_pytree_test.py +159 -159
  122. brainstate/util/pretty_repr.py +328 -328
  123. brainstate/util/pretty_table.py +2954 -2954
  124. brainstate/util/scaling.py +258 -258
  125. brainstate/util/struct.py +523 -523
  126. {brainstate-0.1.8.dist-info → brainstate-0.1.10.dist-info}/METADATA +91 -99
  127. brainstate-0.1.10.dist-info/RECORD +130 -0
  128. {brainstate-0.1.8.dist-info → brainstate-0.1.10.dist-info}/WHEEL +1 -1
  129. {brainstate-0.1.8.dist-info → brainstate-0.1.10.dist-info/licenses}/LICENSE +202 -202
  130. brainstate/functional/_normalization.py +0 -81
  131. brainstate/functional/_spikes.py +0 -204
  132. brainstate-0.1.8.dist-info/RECORD +0 -132
  133. {brainstate-0.1.8.dist-info → brainstate-0.1.10.dist-info}/top_level.txt +0 -0
@@ -1,514 +1,514 @@
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 collections import namedtuple
17
- from typing import Callable, TypeVar, Tuple, Any, Dict
18
-
19
- import jax
20
-
21
- from brainstate._state import catch_new_states
22
- from brainstate._utils import set_module_as
23
- from brainstate.augment import vmap, vmap_new_states
24
- from brainstate.graph import nodes
25
- from brainstate.random import set_key, split_key
26
- from brainstate.typing import Filter
27
- from ._module import Module
28
-
29
- # the maximum order
30
- MAX_ORDER = 10
31
-
32
- # State Load Results
33
- StateLoadResult = namedtuple('StateLoadResult', ['missing_keys', 'unexpected_keys'])
34
-
35
- T = TypeVar('T', bound=Module)
36
-
37
- __all__ = [
38
- 'MAX_ORDER',
39
- 'call_order',
40
- 'call_all_functions',
41
- 'vmap_call_all_functions',
42
- 'init_all_states',
43
- 'vmap_init_all_states',
44
- 'reset_all_states',
45
- 'load_all_states',
46
- 'save_all_states',
47
- 'assign_state_values',
48
- ]
49
-
50
-
51
- @set_module_as('brainstate.nn')
52
- def call_order(level: int = 0, check_order_boundary: bool = True):
53
- """The decorator for indicating the resetting level.
54
-
55
- The function takes an optional integer argument level with a default value of 0.
56
-
57
- The lower the level, the earlier the function is called.
58
-
59
- >>> import brainstate as brainstate
60
- >>> brainstate.nn.call_order(0)
61
- >>> brainstate.nn.call_order(-1)
62
- >>> brainstate.nn.call_order(-2)
63
-
64
- Parameters
65
- ----------
66
- level: int
67
- The call order level.
68
- check_order_boundary: bool
69
- Whether check the boundary of function call order. If True,
70
- the order that not in [0, 10) will raise a ValueError.
71
-
72
- Returns
73
- -------
74
- The function to warp.
75
- """
76
- if check_order_boundary and (level < 0 or level >= MAX_ORDER):
77
- raise ValueError(f'"call_order" must be an integer in [0, {MAX_ORDER}). but we got {level}.')
78
-
79
- def wrap(fun: Callable):
80
- fun.call_order = level
81
- return fun
82
-
83
- return wrap
84
-
85
-
86
- @set_module_as('brainstate.nn')
87
- def call_all_functions(
88
- target: T,
89
- fun_name: str,
90
- args: Tuple[Any, ...] | Any = (),
91
- kwargs: Dict[str, Any] | None = None,
92
- node_to_exclude: Filter = None,
93
- fun_if_not_exist: str = 'raise',
94
- ) -> T:
95
- """
96
- Call a specified function on all nodes of a target module, respecting call order if defined.
97
-
98
- This function iterates through all nodes of the target module, calling a specified function
99
- on each node. It respects the call order of functions if defined, and provides options for
100
- handling cases where the specified function does not exist on a node.
101
-
102
- Parameters
103
- -----------
104
- target : T
105
- The target module on which to call functions.
106
- fun_name : str
107
- The name of the function to call on each node.
108
- args : Tuple[Any, ...] | Any, optional
109
- Positional arguments to pass to the called function. Default is an empty tuple.
110
- kwargs : Dict[str, Any] | None, optional
111
- Keyword arguments to pass to the called function. Default is None.
112
- node_to_exclude : Filter, optional
113
- A filter function to exclude certain nodes from the function call.
114
- fun_if_not_exist : str, optional
115
- Specifies behavior when the function doesn't exist on a node. Options are:
116
-
117
- - 'raise': Raise an exception (default)
118
- - 'pass' or 'none': Skip the node and continue
119
-
120
- Returns
121
- --------
122
- T
123
- The target module after calling the specified function on all applicable nodes.
124
-
125
- Raises
126
- -------
127
- AssertionError
128
- If fun_name is not a string or kwargs is not a dictionary.
129
- ValueError
130
- If fun_if_not_exist is not one of the allowed values.
131
- AttributeError
132
- If the specified function doesn't exist on a node and fun_if_not_exist is 'raise'.
133
- """
134
- assert isinstance(fun_name, str), f'fun_name must be a string, but got {fun_name}.'
135
-
136
- args = (args,) if not isinstance(args, tuple) else args
137
- kwargs = kwargs or {}
138
- assert isinstance(kwargs, dict), f'kwargs must be a dict, but got {kwargs}.'
139
-
140
- all_nodes = nodes(target).filter(Module)
141
- if node_to_exclude is not None:
142
- all_nodes -= all_nodes.filter(node_to_exclude)
143
-
144
- nodes_with_order = []
145
- for node in all_nodes.values():
146
- try:
147
- fun = getattr(node, fun_name)
148
- except AttributeError as e:
149
- if fun_if_not_exist == 'raise':
150
- raise
151
- elif fun_if_not_exist in ('pass', 'none'):
152
- continue
153
- else:
154
- raise ValueError(
155
- f'fun_if_not_exist must be one of ["raise", "pass", "none"], but got {fun_if_not_exist}.')
156
-
157
- assert callable(fun), f'{fun_name} must be a callable function, but got {fun}.'
158
- if hasattr(fun, 'call_order'):
159
- nodes_with_order.append(node)
160
- else:
161
- fun(*args, **kwargs)
162
-
163
- for node in sorted(nodes_with_order, key=lambda x: getattr(x, fun_name).call_order):
164
- getattr(node, fun_name)(*args, **kwargs)
165
-
166
- return target
167
-
168
-
169
- def vmap_call_all_functions(
170
- target: T,
171
- fun_name: str,
172
- args: Tuple[Any, ...] | Any = (),
173
- kwargs: Dict[str, Any] | None = None,
174
- axis_size: int = None,
175
- node_to_exclude: Filter = None,
176
- tag: str | None = None,
177
- fun_if_not_exist: str = 'raise',
178
- ) -> T:
179
- """
180
- Apply vectorized mapping (vmap) to call a specified function on all nodes of a target module.
181
-
182
- This function vectorizes the process of calling a specified function across multiple instances
183
- of the target module, effectively batching the operation.
184
-
185
- Parameters
186
- -----------
187
- target : T
188
- The target module on which to call functions.
189
- fun_name : str
190
- The name of the function to call on each node.
191
- args : Tuple[Any, ...] | Any, optional
192
- Positional arguments to pass to the called function. Default is an empty tuple.
193
- kwargs : Dict[str, Any] | None, optional
194
- Keyword arguments to pass to the called function. Default is None.
195
- axis_size : int, optional
196
- The size of the batch axis for vmap. Must be a positive integer.
197
- node_to_exclude : Filter, optional
198
- A filter function to exclude certain nodes from the function call.
199
- tag : str | None, optional
200
- A tag to be used for catching new states.
201
- fun_if_not_exist : str, optional
202
- Specifies behavior when the function doesn't exist on a node. Options are:
203
-
204
- - 'raise': Raise an exception (default)
205
- - 'pass' or 'none': Skip the node and continue
206
-
207
- Returns
208
- --------
209
- T
210
- The target module after applying the vectorized function call on all applicable nodes.
211
-
212
- Raises
213
- -------
214
- AssertionError
215
- If axis_size is not specified or is not a positive integer.
216
- """
217
- assert axis_size is not None and axis_size > 0, f"axis_size must be a positive integer, got {axis_size}"
218
-
219
- if not isinstance(args, tuple):
220
- args = (args,)
221
- kwargs = kwargs or {}
222
- assert isinstance(kwargs, dict), f'kwargs must be a dict, but got {kwargs}.'
223
-
224
- @vmap(out_axes=0, axis_size=axis_size)
225
- def vmapped_fn(key):
226
- set_key(key)
227
- with catch_new_states(tag) as inner_catcher:
228
- call_all_functions(
229
- target,
230
- fun_name=fun_name,
231
- args=args,
232
- kwargs=kwargs,
233
- node_to_exclude=node_to_exclude,
234
- fun_if_not_exist=fun_if_not_exist
235
- )
236
- values = inner_catcher.get_state_values()
237
- return values
238
-
239
- with catch_new_states(tag) as outer_catcher:
240
- values = vmapped_fn(split_key(axis_size))
241
- states = outer_catcher.get_states()
242
- for state, value in zip(states, values):
243
- state.value = value
244
-
245
- return target
246
-
247
-
248
- @set_module_as('brainstate.nn')
249
- def init_all_states(
250
- target: T,
251
- *init_args,
252
- node_to_exclude: Filter = None,
253
- **init_kwargs,
254
- ) -> T:
255
- """
256
- Initialize all states for the given target module and its submodules.
257
-
258
- This function initializes the states of the target module and all its submodules,
259
- respecting any call order decorators that may be present on the init_state methods.
260
-
261
- Parameters
262
- ----------
263
- target : T
264
- The target module whose states are to be initialized.
265
- init_args : Tuple[Any, ...] | Any, optional
266
- Positional arguments to be passed to each init_state method.
267
- If a single non-tuple argument is provided, it will be wrapped in a tuple.
268
- init_kwargs : Dict[str, Any] | None, optional
269
- Keyword arguments to be passed to each init_state method.
270
- If None, an empty dictionary will be used.
271
- node_to_exclude : Filter, optional
272
- A filter function or predicate to exclude certain nodes from initialization.
273
-
274
- Returns
275
- -------
276
- T
277
- The target module with all states initialized.
278
-
279
- Raises
280
- ------
281
- AssertionError
282
- If init_kwargs is provided but is not a dictionary.
283
- """
284
- return call_all_functions(target, 'init_state', init_args, init_kwargs, node_to_exclude)
285
-
286
-
287
- @set_module_as('brainstate.nn')
288
- def vmap_init_all_states(
289
- target: T,
290
- *init_args: Tuple[Any, ...] | Any,
291
- axis_size: int = None,
292
- node_to_exclude: Filter = None,
293
- state_to_exclude: Filter = None,
294
- state_tag: str | None = None,
295
- **init_kwargs: Dict[str, Any] | None
296
- ) -> T:
297
- """
298
- Initialize all vmap states for the given target module.
299
-
300
- This function applies vectorized mapping (vmap) to initialize states across multiple
301
- instances of the target module, effectively batching the initialization process.
302
-
303
- Parameters
304
- -----------
305
- target : T
306
- The target module whose states are to be initialized.
307
- init_args : Tuple[Any, ...] | Any, optional
308
- Positional arguments to be passed to the init_all_states function. Default is an empty tuple.
309
- init_kwargs : Dict[str, Any] | None, optional
310
- Keyword arguments to be passed to the init_all_states function. Default is None.
311
- axis_size : int, optional
312
- The size of the batch axis for vmap. This must be specified and should be greater than 0.
313
- node_to_exclude : Filter, optional
314
- A filter to exclude certain nodes from initialization.
315
- state_tag : str | None, optional
316
- A tag to be used for catching new states.
317
-
318
- Returns
319
- --------
320
- T
321
- The target module with initialized states.
322
-
323
- Raises
324
- -------
325
- AssertionError
326
- If axis_size is not specified or is not greater than 0.
327
- If init_kwargs is not a dictionary.
328
- """
329
-
330
- # return vmap_call_all_functions(
331
- # target,
332
- # 'init_state',
333
- # args=init_args,
334
- # kwargs=init_kwargs,
335
- # axis_size=axis_size,
336
- # node_to_exclude=node_to_exclude,
337
- # tag=tag,
338
- # )
339
-
340
- def init_fn():
341
- init_all_states(
342
- target,
343
- *init_args,
344
- **init_kwargs,
345
- node_to_exclude=node_to_exclude,
346
- )
347
- return
348
-
349
- vmap_new_states(init_fn, state_tag=state_tag, axis_size=axis_size, state_to_exclude=state_to_exclude)()
350
- return target
351
-
352
-
353
- @set_module_as('brainstate.nn')
354
- def reset_all_states(
355
- target: T,
356
- reset_args: Tuple[Any, ...] | Any = (),
357
- reset_kwargs: Dict[str, Any] | None = None,
358
- node_to_exclude: Filter = None,
359
- ) -> T:
360
- """
361
- Reset all states for the given target module and its submodules.
362
-
363
- This function resets the states of the target module and all its submodules,
364
- respecting any call order decorators that may be present on the reset_state methods.
365
-
366
- Parameters
367
- ----------
368
- target : T
369
- The target module whose states are to be reset.
370
- reset_args : Tuple[Any, ...] | Any, optional
371
- Positional arguments to be passed to each reset_state method.
372
- If a single non-tuple argument is provided, it will be wrapped in a tuple.
373
- reset_kwargs : Dict[str, Any] | None, optional
374
- Keyword arguments to be passed to each reset_state method.
375
- If None, an empty dictionary will be used.
376
- node_to_exclude : Filter, optional
377
- A filter function or predicate to exclude certain nodes from reset.
378
-
379
- Returns
380
- -------
381
- T
382
- The target module with all states reset.
383
-
384
- Raises
385
- ------
386
- AssertionError
387
- If init_kwargs is provided but is not a dictionary.
388
- """
389
- return call_all_functions(
390
- target,
391
- fun_name='reset_state',
392
- args=reset_args,
393
- kwargs=reset_kwargs,
394
- node_to_exclude=node_to_exclude
395
- )
396
-
397
-
398
- def vmap_reset_all_states(
399
- target: T,
400
- reset_args: Tuple[Any, ...] | Any = (),
401
- reset_kwargs: Dict[str, Any] | None = None,
402
- axis_size: int = None,
403
- node_to_exclude: Filter = None,
404
- tag: str | None = None,
405
- ) -> T:
406
- """
407
- Reset all vmap states for the given target module.
408
-
409
- This function applies vectorized mapping (vmap) to reset states across multiple
410
- instances of the target module, effectively batching the reset process.
411
-
412
- Parameters
413
- -----------
414
- target : T
415
- The target module whose states are to be reset.
416
- reset_args : Tuple[Any, ...] | Any, optional
417
- Positional arguments to be passed to the reset_all_states function. Default is an empty tuple.
418
- reset_kwargs : Dict[str, Any] | None, optional
419
- Keyword arguments to be passed to the reset_all_states function. Default is None.
420
- axis_size : int, optional
421
- The size of the batch axis for vmap. This must be specified and should be greater than 0.
422
- node_to_exclude : Filter, optional
423
- A filter to exclude certain nodes from reset.
424
- tag : str | None, optional
425
- A tag to be used for catching new states.
426
-
427
- Returns
428
- --------
429
- T
430
- The target module with reset states.
431
-
432
- Raises
433
- -------
434
- AssertionError
435
- If axis_size is not specified or is not greater than 0.
436
- If reset_kwargs is not a dictionary.
437
- """
438
- return vmap_call_all_functions(
439
- target,
440
- fun_name='reset_state',
441
- args=reset_args,
442
- kwargs=reset_kwargs,
443
- axis_size=axis_size,
444
- node_to_exclude=node_to_exclude,
445
- tag=tag,
446
- )
447
-
448
-
449
- @set_module_as('brainstate.nn')
450
- def load_all_states(target: Module, state_dict: Dict, **kwargs):
451
- """
452
- Copy parameters and buffers from :attr:`state_dict` into
453
- this module and its descendants.
454
-
455
- Args:
456
- target: Module. The dynamical system to load its states.
457
- state_dict: dict. A dict containing parameters and persistent buffers.
458
-
459
- Returns
460
- -------
461
- ``NamedTuple`` with ``missing_keys`` and ``unexpected_keys`` fields:
462
-
463
- * **missing_keys** is a list of str containing the missing keys
464
- * **unexpected_keys** is a list of str containing the unexpected keys
465
- """
466
- missing_keys = []
467
- unexpected_keys = []
468
- for path, node in nodes(target).items():
469
- r = node.load_state(state_dict[path], **kwargs)
470
- if r is not None:
471
- missing, unexpected = r
472
- missing_keys.extend([f'{path}.{key}' for key in missing])
473
- unexpected_keys.extend([f'{path}.{key}' for key in unexpected])
474
- return StateLoadResult(missing_keys, unexpected_keys)
475
-
476
-
477
- @set_module_as('brainstate.nn')
478
- def save_all_states(target: Module, **kwargs) -> Dict:
479
- """
480
- Save all states in the ``target`` as a dictionary for later disk serialization.
481
-
482
- Args:
483
- target: Module. The node to save its states.
484
-
485
- Returns
486
- Dict. The state dict for serialization.
487
- """
488
- return {key: node.save_state(**kwargs) for key, node in target.nodes().items()}
489
-
490
-
491
- @set_module_as('brainstate.nn')
492
- def assign_state_values(target: Module, *state_by_abs_path: Dict):
493
- """
494
- Assign state values according to the given state dictionary.
495
-
496
- Parameters
497
- ----------
498
- target: Module
499
- The target module.
500
- state_by_abs_path: dict
501
- The state dictionary which is accessed by the "absolute" accessing method.
502
-
503
- """
504
- all_states = dict()
505
- for state in state_by_abs_path:
506
- all_states.update(state)
507
- variables = target.states()
508
- keys1 = set(all_states.keys())
509
- keys2 = set(variables.keys())
510
- for key in keys2.intersection(keys1):
511
- variables[key].value = jax.numpy.asarray(all_states[key])
512
- unexpected_keys = list(keys1 - keys2)
513
- missing_keys = list(keys2 - keys1)
514
- return unexpected_keys, missing_keys
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 collections import namedtuple
17
+ from typing import Callable, TypeVar, Tuple, Any, Dict
18
+
19
+ import jax
20
+
21
+ from brainstate._state import catch_new_states
22
+ from brainstate._utils import set_module_as
23
+ from brainstate.augment import vmap, vmap_new_states
24
+ from brainstate.graph import nodes
25
+ from brainstate.random import set_key, split_key
26
+ from brainstate.typing import Filter
27
+ from ._module import Module
28
+
29
+ # the maximum order
30
+ MAX_ORDER = 10
31
+
32
+ # State Load Results
33
+ StateLoadResult = namedtuple('StateLoadResult', ['missing_keys', 'unexpected_keys'])
34
+
35
+ T = TypeVar('T', bound=Module)
36
+
37
+ __all__ = [
38
+ 'MAX_ORDER',
39
+ 'call_order',
40
+ 'call_all_functions',
41
+ 'vmap_call_all_functions',
42
+ 'init_all_states',
43
+ 'vmap_init_all_states',
44
+ 'reset_all_states',
45
+ 'load_all_states',
46
+ 'save_all_states',
47
+ 'assign_state_values',
48
+ ]
49
+
50
+
51
+ @set_module_as('brainstate.nn')
52
+ def call_order(level: int = 0, check_order_boundary: bool = True):
53
+ """The decorator for indicating the resetting level.
54
+
55
+ The function takes an optional integer argument level with a default value of 0.
56
+
57
+ The lower the level, the earlier the function is called.
58
+
59
+ >>> import brainstate as brainstate
60
+ >>> brainstate.nn.call_order(0)
61
+ >>> brainstate.nn.call_order(-1)
62
+ >>> brainstate.nn.call_order(-2)
63
+
64
+ Parameters
65
+ ----------
66
+ level: int
67
+ The call order level.
68
+ check_order_boundary: bool
69
+ Whether check the boundary of function call order. If True,
70
+ the order that not in [0, 10) will raise a ValueError.
71
+
72
+ Returns
73
+ -------
74
+ The function to warp.
75
+ """
76
+ if check_order_boundary and (level < 0 or level >= MAX_ORDER):
77
+ raise ValueError(f'"call_order" must be an integer in [0, {MAX_ORDER}). but we got {level}.')
78
+
79
+ def wrap(fun: Callable):
80
+ fun.call_order = level
81
+ return fun
82
+
83
+ return wrap
84
+
85
+
86
+ @set_module_as('brainstate.nn')
87
+ def call_all_functions(
88
+ target: T,
89
+ fun_name: str,
90
+ args: Tuple[Any, ...] | Any = (),
91
+ kwargs: Dict[str, Any] | None = None,
92
+ node_to_exclude: Filter = None,
93
+ fun_if_not_exist: str = 'raise',
94
+ ) -> T:
95
+ """
96
+ Call a specified function on all nodes of a target module, respecting call order if defined.
97
+
98
+ This function iterates through all nodes of the target module, calling a specified function
99
+ on each node. It respects the call order of functions if defined, and provides options for
100
+ handling cases where the specified function does not exist on a node.
101
+
102
+ Parameters
103
+ -----------
104
+ target : T
105
+ The target module on which to call functions.
106
+ fun_name : str
107
+ The name of the function to call on each node.
108
+ args : Tuple[Any, ...] | Any, optional
109
+ Positional arguments to pass to the called function. Default is an empty tuple.
110
+ kwargs : Dict[str, Any] | None, optional
111
+ Keyword arguments to pass to the called function. Default is None.
112
+ node_to_exclude : Filter, optional
113
+ A filter function to exclude certain nodes from the function call.
114
+ fun_if_not_exist : str, optional
115
+ Specifies behavior when the function doesn't exist on a node. Options are:
116
+
117
+ - 'raise': Raise an exception (default)
118
+ - 'pass' or 'none': Skip the node and continue
119
+
120
+ Returns
121
+ --------
122
+ T
123
+ The target module after calling the specified function on all applicable nodes.
124
+
125
+ Raises
126
+ -------
127
+ AssertionError
128
+ If fun_name is not a string or kwargs is not a dictionary.
129
+ ValueError
130
+ If fun_if_not_exist is not one of the allowed values.
131
+ AttributeError
132
+ If the specified function doesn't exist on a node and fun_if_not_exist is 'raise'.
133
+ """
134
+ assert isinstance(fun_name, str), f'fun_name must be a string, but got {fun_name}.'
135
+
136
+ args = (args,) if not isinstance(args, tuple) else args
137
+ kwargs = kwargs or {}
138
+ assert isinstance(kwargs, dict), f'kwargs must be a dict, but got {kwargs}.'
139
+
140
+ all_nodes = nodes(target).filter(Module)
141
+ if node_to_exclude is not None:
142
+ all_nodes -= all_nodes.filter(node_to_exclude)
143
+
144
+ nodes_with_order = []
145
+ for node in all_nodes.values():
146
+ try:
147
+ fun = getattr(node, fun_name)
148
+ except AttributeError as e:
149
+ if fun_if_not_exist == 'raise':
150
+ raise
151
+ elif fun_if_not_exist in ('pass', 'none'):
152
+ continue
153
+ else:
154
+ raise ValueError(
155
+ f'fun_if_not_exist must be one of ["raise", "pass", "none"], but got {fun_if_not_exist}.')
156
+
157
+ assert callable(fun), f'{fun_name} must be a callable function, but got {fun}.'
158
+ if hasattr(fun, 'call_order'):
159
+ nodes_with_order.append(node)
160
+ else:
161
+ fun(*args, **kwargs)
162
+
163
+ for node in sorted(nodes_with_order, key=lambda x: getattr(x, fun_name).call_order):
164
+ getattr(node, fun_name)(*args, **kwargs)
165
+
166
+ return target
167
+
168
+
169
+ def vmap_call_all_functions(
170
+ target: T,
171
+ fun_name: str,
172
+ args: Tuple[Any, ...] | Any = (),
173
+ kwargs: Dict[str, Any] | None = None,
174
+ axis_size: int = None,
175
+ node_to_exclude: Filter = None,
176
+ tag: str | None = None,
177
+ fun_if_not_exist: str = 'raise',
178
+ ) -> T:
179
+ """
180
+ Apply vectorized mapping (vmap) to call a specified function on all nodes of a target module.
181
+
182
+ This function vectorizes the process of calling a specified function across multiple instances
183
+ of the target module, effectively batching the operation.
184
+
185
+ Parameters
186
+ -----------
187
+ target : T
188
+ The target module on which to call functions.
189
+ fun_name : str
190
+ The name of the function to call on each node.
191
+ args : Tuple[Any, ...] | Any, optional
192
+ Positional arguments to pass to the called function. Default is an empty tuple.
193
+ kwargs : Dict[str, Any] | None, optional
194
+ Keyword arguments to pass to the called function. Default is None.
195
+ axis_size : int, optional
196
+ The size of the batch axis for vmap. Must be a positive integer.
197
+ node_to_exclude : Filter, optional
198
+ A filter function to exclude certain nodes from the function call.
199
+ tag : str | None, optional
200
+ A tag to be used for catching new states.
201
+ fun_if_not_exist : str, optional
202
+ Specifies behavior when the function doesn't exist on a node. Options are:
203
+
204
+ - 'raise': Raise an exception (default)
205
+ - 'pass' or 'none': Skip the node and continue
206
+
207
+ Returns
208
+ --------
209
+ T
210
+ The target module after applying the vectorized function call on all applicable nodes.
211
+
212
+ Raises
213
+ -------
214
+ AssertionError
215
+ If axis_size is not specified or is not a positive integer.
216
+ """
217
+ assert axis_size is not None and axis_size > 0, f"axis_size must be a positive integer, got {axis_size}"
218
+
219
+ if not isinstance(args, tuple):
220
+ args = (args,)
221
+ kwargs = kwargs or {}
222
+ assert isinstance(kwargs, dict), f'kwargs must be a dict, but got {kwargs}.'
223
+
224
+ @vmap(out_axes=0, axis_size=axis_size)
225
+ def vmapped_fn(key):
226
+ set_key(key)
227
+ with catch_new_states(tag) as inner_catcher:
228
+ call_all_functions(
229
+ target,
230
+ fun_name=fun_name,
231
+ args=args,
232
+ kwargs=kwargs,
233
+ node_to_exclude=node_to_exclude,
234
+ fun_if_not_exist=fun_if_not_exist
235
+ )
236
+ values = inner_catcher.get_state_values()
237
+ return values
238
+
239
+ with catch_new_states(tag) as outer_catcher:
240
+ values = vmapped_fn(split_key(axis_size))
241
+ states = outer_catcher.get_states()
242
+ for state, value in zip(states, values):
243
+ state.value = value
244
+
245
+ return target
246
+
247
+
248
+ @set_module_as('brainstate.nn')
249
+ def init_all_states(
250
+ target: T,
251
+ *init_args,
252
+ node_to_exclude: Filter = None,
253
+ **init_kwargs,
254
+ ) -> T:
255
+ """
256
+ Initialize all states for the given target module and its submodules.
257
+
258
+ This function initializes the states of the target module and all its submodules,
259
+ respecting any call order decorators that may be present on the init_state methods.
260
+
261
+ Parameters
262
+ ----------
263
+ target : T
264
+ The target module whose states are to be initialized.
265
+ init_args : Tuple[Any, ...] | Any, optional
266
+ Positional arguments to be passed to each init_state method.
267
+ If a single non-tuple argument is provided, it will be wrapped in a tuple.
268
+ init_kwargs : Dict[str, Any] | None, optional
269
+ Keyword arguments to be passed to each init_state method.
270
+ If None, an empty dictionary will be used.
271
+ node_to_exclude : Filter, optional
272
+ A filter function or predicate to exclude certain nodes from initialization.
273
+
274
+ Returns
275
+ -------
276
+ T
277
+ The target module with all states initialized.
278
+
279
+ Raises
280
+ ------
281
+ AssertionError
282
+ If init_kwargs is provided but is not a dictionary.
283
+ """
284
+ return call_all_functions(target, 'init_state', init_args, init_kwargs, node_to_exclude)
285
+
286
+
287
+ @set_module_as('brainstate.nn')
288
+ def vmap_init_all_states(
289
+ target: T,
290
+ *init_args: Tuple[Any, ...] | Any,
291
+ axis_size: int = None,
292
+ node_to_exclude: Filter = None,
293
+ state_to_exclude: Filter = None,
294
+ state_tag: str | None = None,
295
+ **init_kwargs: Dict[str, Any] | None
296
+ ) -> T:
297
+ """
298
+ Initialize all vmap states for the given target module.
299
+
300
+ This function applies vectorized mapping (vmap) to initialize states across multiple
301
+ instances of the target module, effectively batching the initialization process.
302
+
303
+ Parameters
304
+ -----------
305
+ target : T
306
+ The target module whose states are to be initialized.
307
+ init_args : Tuple[Any, ...] | Any, optional
308
+ Positional arguments to be passed to the init_all_states function. Default is an empty tuple.
309
+ init_kwargs : Dict[str, Any] | None, optional
310
+ Keyword arguments to be passed to the init_all_states function. Default is None.
311
+ axis_size : int, optional
312
+ The size of the batch axis for vmap. This must be specified and should be greater than 0.
313
+ node_to_exclude : Filter, optional
314
+ A filter to exclude certain nodes from initialization.
315
+ state_tag : str | None, optional
316
+ A tag to be used for catching new states.
317
+
318
+ Returns
319
+ --------
320
+ T
321
+ The target module with initialized states.
322
+
323
+ Raises
324
+ -------
325
+ AssertionError
326
+ If axis_size is not specified or is not greater than 0.
327
+ If init_kwargs is not a dictionary.
328
+ """
329
+
330
+ # return vmap_call_all_functions(
331
+ # target,
332
+ # 'init_state',
333
+ # args=init_args,
334
+ # kwargs=init_kwargs,
335
+ # axis_size=axis_size,
336
+ # node_to_exclude=node_to_exclude,
337
+ # tag=tag,
338
+ # )
339
+
340
+ def init_fn():
341
+ init_all_states(
342
+ target,
343
+ *init_args,
344
+ **init_kwargs,
345
+ node_to_exclude=node_to_exclude,
346
+ )
347
+ return
348
+
349
+ vmap_new_states(init_fn, state_tag=state_tag, axis_size=axis_size, state_to_exclude=state_to_exclude)()
350
+ return target
351
+
352
+
353
+ @set_module_as('brainstate.nn')
354
+ def reset_all_states(
355
+ target: T,
356
+ reset_args: Tuple[Any, ...] | Any = (),
357
+ reset_kwargs: Dict[str, Any] | None = None,
358
+ node_to_exclude: Filter = None,
359
+ ) -> T:
360
+ """
361
+ Reset all states for the given target module and its submodules.
362
+
363
+ This function resets the states of the target module and all its submodules,
364
+ respecting any call order decorators that may be present on the reset_state methods.
365
+
366
+ Parameters
367
+ ----------
368
+ target : T
369
+ The target module whose states are to be reset.
370
+ reset_args : Tuple[Any, ...] | Any, optional
371
+ Positional arguments to be passed to each reset_state method.
372
+ If a single non-tuple argument is provided, it will be wrapped in a tuple.
373
+ reset_kwargs : Dict[str, Any] | None, optional
374
+ Keyword arguments to be passed to each reset_state method.
375
+ If None, an empty dictionary will be used.
376
+ node_to_exclude : Filter, optional
377
+ A filter function or predicate to exclude certain nodes from reset.
378
+
379
+ Returns
380
+ -------
381
+ T
382
+ The target module with all states reset.
383
+
384
+ Raises
385
+ ------
386
+ AssertionError
387
+ If init_kwargs is provided but is not a dictionary.
388
+ """
389
+ return call_all_functions(
390
+ target,
391
+ fun_name='reset_state',
392
+ args=reset_args,
393
+ kwargs=reset_kwargs,
394
+ node_to_exclude=node_to_exclude
395
+ )
396
+
397
+
398
+ def vmap_reset_all_states(
399
+ target: T,
400
+ reset_args: Tuple[Any, ...] | Any = (),
401
+ reset_kwargs: Dict[str, Any] | None = None,
402
+ axis_size: int = None,
403
+ node_to_exclude: Filter = None,
404
+ tag: str | None = None,
405
+ ) -> T:
406
+ """
407
+ Reset all vmap states for the given target module.
408
+
409
+ This function applies vectorized mapping (vmap) to reset states across multiple
410
+ instances of the target module, effectively batching the reset process.
411
+
412
+ Parameters
413
+ -----------
414
+ target : T
415
+ The target module whose states are to be reset.
416
+ reset_args : Tuple[Any, ...] | Any, optional
417
+ Positional arguments to be passed to the reset_all_states function. Default is an empty tuple.
418
+ reset_kwargs : Dict[str, Any] | None, optional
419
+ Keyword arguments to be passed to the reset_all_states function. Default is None.
420
+ axis_size : int, optional
421
+ The size of the batch axis for vmap. This must be specified and should be greater than 0.
422
+ node_to_exclude : Filter, optional
423
+ A filter to exclude certain nodes from reset.
424
+ tag : str | None, optional
425
+ A tag to be used for catching new states.
426
+
427
+ Returns
428
+ --------
429
+ T
430
+ The target module with reset states.
431
+
432
+ Raises
433
+ -------
434
+ AssertionError
435
+ If axis_size is not specified or is not greater than 0.
436
+ If reset_kwargs is not a dictionary.
437
+ """
438
+ return vmap_call_all_functions(
439
+ target,
440
+ fun_name='reset_state',
441
+ args=reset_args,
442
+ kwargs=reset_kwargs,
443
+ axis_size=axis_size,
444
+ node_to_exclude=node_to_exclude,
445
+ tag=tag,
446
+ )
447
+
448
+
449
+ @set_module_as('brainstate.nn')
450
+ def load_all_states(target: Module, state_dict: Dict, **kwargs):
451
+ """
452
+ Copy parameters and buffers from :attr:`state_dict` into
453
+ this module and its descendants.
454
+
455
+ Args:
456
+ target: Module. The dynamical system to load its states.
457
+ state_dict: dict. A dict containing parameters and persistent buffers.
458
+
459
+ Returns
460
+ -------
461
+ ``NamedTuple`` with ``missing_keys`` and ``unexpected_keys`` fields:
462
+
463
+ * **missing_keys** is a list of str containing the missing keys
464
+ * **unexpected_keys** is a list of str containing the unexpected keys
465
+ """
466
+ missing_keys = []
467
+ unexpected_keys = []
468
+ for path, node in nodes(target).items():
469
+ r = node.load_state(state_dict[path], **kwargs)
470
+ if r is not None:
471
+ missing, unexpected = r
472
+ missing_keys.extend([f'{path}.{key}' for key in missing])
473
+ unexpected_keys.extend([f'{path}.{key}' for key in unexpected])
474
+ return StateLoadResult(missing_keys, unexpected_keys)
475
+
476
+
477
+ @set_module_as('brainstate.nn')
478
+ def save_all_states(target: Module, **kwargs) -> Dict:
479
+ """
480
+ Save all states in the ``target`` as a dictionary for later disk serialization.
481
+
482
+ Args:
483
+ target: Module. The node to save its states.
484
+
485
+ Returns
486
+ Dict. The state dict for serialization.
487
+ """
488
+ return {key: node.save_state(**kwargs) for key, node in target.nodes().items()}
489
+
490
+
491
+ @set_module_as('brainstate.nn')
492
+ def assign_state_values(target: Module, *state_by_abs_path: Dict):
493
+ """
494
+ Assign state values according to the given state dictionary.
495
+
496
+ Parameters
497
+ ----------
498
+ target: Module
499
+ The target module.
500
+ state_by_abs_path: dict
501
+ The state dictionary which is accessed by the "absolute" accessing method.
502
+
503
+ """
504
+ all_states = dict()
505
+ for state in state_by_abs_path:
506
+ all_states.update(state)
507
+ variables = target.states()
508
+ keys1 = set(all_states.keys())
509
+ keys2 = set(variables.keys())
510
+ for key in keys2.intersection(keys1):
511
+ variables[key].value = jax.numpy.asarray(all_states[key])
512
+ unexpected_keys = list(keys1 - keys2)
513
+ missing_keys = list(keys2 - keys1)
514
+ return unexpected_keys, missing_keys