brainstate 0.0.2.post20240913__py2.py3-none-any.whl → 0.0.2.post20241009__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 (50) hide show
  1. brainstate/__init__.py +4 -2
  2. brainstate/_module.py +102 -67
  3. brainstate/_state.py +2 -2
  4. brainstate/_visualization.py +47 -0
  5. brainstate/environ.py +116 -9
  6. brainstate/environ_test.py +56 -0
  7. brainstate/functional/_activations.py +134 -56
  8. brainstate/functional/_activations_test.py +331 -0
  9. brainstate/functional/_normalization.py +21 -10
  10. brainstate/init/_generic.py +4 -2
  11. brainstate/mixin.py +1 -1
  12. brainstate/nn/__init__.py +7 -2
  13. brainstate/nn/_base.py +2 -2
  14. brainstate/nn/_connections.py +4 -4
  15. brainstate/nn/_dynamics.py +5 -5
  16. brainstate/nn/_elementwise.py +9 -9
  17. brainstate/nn/_embedding.py +3 -3
  18. brainstate/nn/_normalizations.py +3 -3
  19. brainstate/nn/_others.py +2 -2
  20. brainstate/nn/_poolings.py +6 -6
  21. brainstate/nn/_rate_rnns.py +1 -1
  22. brainstate/nn/_readout.py +1 -1
  23. brainstate/nn/_synouts.py +1 -1
  24. brainstate/nn/event/__init__.py +25 -0
  25. brainstate/nn/event/_misc.py +34 -0
  26. brainstate/nn/event/csr.py +312 -0
  27. brainstate/nn/event/csr_test.py +118 -0
  28. brainstate/nn/event/fixed_probability.py +276 -0
  29. brainstate/nn/event/fixed_probability_test.py +127 -0
  30. brainstate/nn/event/linear.py +220 -0
  31. brainstate/nn/event/linear_test.py +111 -0
  32. brainstate/nn/metrics.py +390 -0
  33. brainstate/optim/__init__.py +5 -1
  34. brainstate/optim/_optax_optimizer.py +208 -0
  35. brainstate/optim/_optax_optimizer_test.py +14 -0
  36. brainstate/random/__init__.py +24 -0
  37. brainstate/{random.py → random/_rand_funs.py} +7 -1596
  38. brainstate/random/_rand_seed.py +169 -0
  39. brainstate/random/_rand_state.py +1491 -0
  40. brainstate/{_random_for_unit.py → random/_random_for_unit.py} +1 -1
  41. brainstate/{random_test.py → random/random_test.py} +208 -191
  42. brainstate/transform/_jit.py +1 -1
  43. brainstate/transform/_jit_test.py +19 -0
  44. brainstate/transform/_make_jaxpr.py +1 -1
  45. {brainstate-0.0.2.post20240913.dist-info → brainstate-0.0.2.post20241009.dist-info}/METADATA +1 -1
  46. brainstate-0.0.2.post20241009.dist-info/RECORD +87 -0
  47. brainstate-0.0.2.post20240913.dist-info/RECORD +0 -70
  48. {brainstate-0.0.2.post20240913.dist-info → brainstate-0.0.2.post20241009.dist-info}/LICENSE +0 -0
  49. {brainstate-0.0.2.post20240913.dist-info → brainstate-0.0.2.post20241009.dist-info}/WHEEL +0 -0
  50. {brainstate-0.0.2.post20240913.dist-info → brainstate-0.0.2.post20241009.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,169 @@
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 contextlib import contextmanager
17
+
18
+ import jax
19
+ import numpy as np
20
+
21
+ from brainstate.typing import SeedOrKey
22
+ from ._rand_state import RandomState, DEFAULT
23
+
24
+ __all__ = [
25
+ 'seed', 'default_rng', 'split_key', 'split_keys', 'seed_context',
26
+ ]
27
+
28
+
29
+ def split_key():
30
+ """Create a new seed from the current seed.
31
+
32
+ This function is useful for the consistency with JAX's random paradigm.
33
+
34
+ Returns
35
+ -------
36
+ key : jax.random.PRNGKey
37
+ A new random key.
38
+ """
39
+ return DEFAULT.split_key()
40
+
41
+
42
+ def split_keys(n: int):
43
+ """Create multiple seeds from the current seed. This is used
44
+ internally by `pmap` and `vmap` to ensure that random numbers
45
+ are different in parallel threads.
46
+
47
+ Parameters
48
+ ----------
49
+ n : int
50
+ The number of seeds to generate.
51
+
52
+ Returns
53
+ -------
54
+ keys : jax.random.PRNGKey
55
+ A tuple of JAX random keys.
56
+ """
57
+ return DEFAULT.split_keys(n)
58
+
59
+
60
+ def clone_rng(seed_or_key=None, clone: bool = True) -> RandomState:
61
+ """Clone the random state according to the given setting.
62
+
63
+ Args:
64
+ seed_or_key: The seed (an integer) or the random key.
65
+ clone: Bool. Whether clone the default random state.
66
+
67
+ Returns:
68
+ The random state.
69
+ """
70
+ if seed_or_key is None:
71
+ return DEFAULT.clone() if clone else DEFAULT
72
+ else:
73
+ return RandomState(seed_or_key)
74
+
75
+
76
+ def default_rng(seed_or_key=None) -> RandomState:
77
+ """
78
+ Get the default random state.
79
+
80
+ Args:
81
+ seed_or_key: The seed (an integer) or the jax random key.
82
+
83
+ Returns:
84
+ The random state.
85
+ """
86
+ if seed_or_key is None:
87
+ return DEFAULT
88
+ else:
89
+ return RandomState(seed_or_key)
90
+
91
+
92
+ def seed(seed_or_key: SeedOrKey = None):
93
+ """Sets a new random seed.
94
+
95
+ Parameters
96
+ ----------
97
+ seed_or_key: int, optional
98
+ The random seed (an integer) or jax random key.
99
+ """
100
+ with jax.ensure_compile_time_eval():
101
+ _set_numpy_seed = True
102
+ if seed_or_key is None:
103
+ seed_or_key = np.random.randint(0, 100000)
104
+ _set_numpy_seed = False
105
+
106
+ # numpy random seed
107
+ if _set_numpy_seed:
108
+ try:
109
+ if np.size(seed_or_key) == 1: # seed
110
+ np.random.seed(seed_or_key)
111
+ elif np.size(seed_or_key) == 2: # jax random key
112
+ np.random.seed(seed_or_key[0])
113
+ else:
114
+ raise ValueError(f"seed_or_key should be an integer or a tuple of two integers.")
115
+ except jax.errors.TracerArrayConversionError:
116
+ pass
117
+
118
+ # jax random seed
119
+ DEFAULT.seed(seed_or_key)
120
+
121
+
122
+ @contextmanager
123
+ def seed_context(seed_or_key: SeedOrKey):
124
+ """
125
+ A context manager that sets the random seed for the duration of the block.
126
+
127
+ Examples:
128
+
129
+ >>> import brainstate as bst
130
+ >>> print(bst.random.rand(2))
131
+ [0.57721865 0.9820676 ]
132
+ >>> print(bst.random.rand(2))
133
+ [0.8511752 0.95312667]
134
+ >>> with bst.random.seed_context(42):
135
+ ... print(bst.random.rand(2))
136
+ [0.95598125 0.4032725 ]
137
+ >>> with bst.random.seed_context(42):
138
+ ... print(bst.random.rand(2))
139
+ [0.95598125 0.4032725 ]
140
+
141
+ .. note::
142
+
143
+ The context manager does not only set the seed for the AX random state, but also for the numpy random state.
144
+
145
+ Args:
146
+ seed_or_key: The seed (an integer) or jax random key.
147
+
148
+ """
149
+ old_jrand_key = DEFAULT.value
150
+ old_np_state = np.random.get_state()
151
+ try:
152
+ # step 1: set the seed of numpy random state
153
+ try:
154
+ if np.size(seed_or_key) == 1: # seed
155
+ np.random.seed(seed_or_key)
156
+ elif np.size(seed_or_key) == 2: # jax random key
157
+ np.random.seed(seed_or_key[0])
158
+ else:
159
+ raise ValueError(f"seed_or_key should be an integer or a tuple of two integers.")
160
+ except jax.errors.TracerArrayConversionError:
161
+ pass
162
+
163
+ # step 2: set the seed of jax random state
164
+ DEFAULT.seed(seed_or_key)
165
+ yield
166
+ finally:
167
+ # restore the random state
168
+ np.random.set_state(old_np_state)
169
+ DEFAULT.seed(old_jrand_key)