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
@@ -0,0 +1,402 @@
1
+ # Copyright 2024 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
+ import unittest
17
+ from absl.testing import parameterized
18
+ import jax
19
+ import jax.numpy as jnp
20
+ import numpy as np
21
+
22
+ import brainstate
23
+
24
+
25
+ class TestClipGradNorm(parameterized.TestCase):
26
+ """Comprehensive tests for clip_grad_norm function."""
27
+
28
+ def setUp(self):
29
+ """Set up test fixtures."""
30
+ # Enable 64-bit precision for more accurate testing
31
+ jax.config.update("jax_enable_x64", True)
32
+
33
+ def test_simple_dict_clipping(self):
34
+ """Test basic gradient clipping with dictionary structure."""
35
+ grads = {
36
+ 'w': jnp.array([3.0, 4.0]),
37
+ 'b': jnp.array([12.0])
38
+ }
39
+
40
+ # Test with return_norm=True
41
+ clipped_grads, norm = brainstate.nn.clip_grad_norm(grads, max_norm=5.0, return_norm=True)
42
+
43
+ # Expected L2 norm: sqrt(3^2 + 4^2 + 12^2) = sqrt(9 + 16 + 144) = sqrt(169) = 13
44
+ self.assertAlmostEqual(norm, 13.0, places=5)
45
+
46
+ # Check clipped values: should be scaled by 5/13
47
+ scale = 5.0 / 13.0
48
+ np.testing.assert_array_almost_equal(
49
+ clipped_grads['w'],
50
+ jnp.array([3.0, 4.0]) * scale,
51
+ decimal=5
52
+ )
53
+ np.testing.assert_array_almost_equal(
54
+ clipped_grads['b'],
55
+ jnp.array([12.0]) * scale,
56
+ decimal=5
57
+ )
58
+
59
+ def test_return_norm_parameter(self):
60
+ """Test the return_norm parameter behavior."""
61
+ grads = {
62
+ 'w': jnp.array([3.0, 4.0]),
63
+ 'b': jnp.array([12.0])
64
+ }
65
+
66
+ # Test with return_norm=False (default)
67
+ clipped_grads_only = brainstate.nn.clip_grad_norm(grads, max_norm=5.0, return_norm=False)
68
+ self.assertIsInstance(clipped_grads_only, dict)
69
+ self.assertIn('w', clipped_grads_only)
70
+ self.assertIn('b', clipped_grads_only)
71
+
72
+ # Test with return_norm=True
73
+ result = brainstate.nn.clip_grad_norm(grads, max_norm=5.0, return_norm=True)
74
+ self.assertIsInstance(result, tuple)
75
+ self.assertEqual(len(result), 2)
76
+ clipped_grads, norm = result
77
+
78
+ # Values should be the same regardless of return_norm
79
+ np.testing.assert_array_almost_equal(
80
+ clipped_grads_only['w'],
81
+ clipped_grads['w'],
82
+ decimal=7
83
+ )
84
+ np.testing.assert_array_almost_equal(
85
+ clipped_grads_only['b'],
86
+ clipped_grads['b'],
87
+ decimal=7
88
+ )
89
+
90
+ def test_nested_structure_clipping(self):
91
+ """Test gradient clipping with nested PyTree structures."""
92
+ grads = {
93
+ 'layer1': {
94
+ 'weight': jnp.array([[1.0, 2.0], [3.0, 4.0]]),
95
+ 'bias': jnp.array([5.0, 6.0])
96
+ },
97
+ 'layer2': {
98
+ 'weight': jnp.array([[7.0, 8.0]]),
99
+ 'bias': jnp.array([9.0])
100
+ }
101
+ }
102
+
103
+ # Calculate expected norm
104
+ flat = jnp.arange(1.0, 10.0)
105
+ expected_norm = jnp.linalg.norm(flat)
106
+
107
+ max_norm = 10.0
108
+ clipped_grads, norm = brainstate.nn.clip_grad_norm(grads, max_norm=max_norm, return_norm=True)
109
+
110
+ self.assertAlmostEqual(norm, expected_norm, places=5)
111
+
112
+ # Since norm > max_norm, gradients should be scaled
113
+ scale = max_norm / expected_norm
114
+ np.testing.assert_array_almost_equal(
115
+ clipped_grads['layer1']['weight'],
116
+ grads['layer1']['weight'] * scale,
117
+ decimal=5
118
+ )
119
+
120
+ def test_no_clipping_when_under_max(self):
121
+ """Test that gradients are unchanged when norm is below max_norm."""
122
+ grads = {
123
+ 'w': jnp.array([1.0, 2.0]),
124
+ 'b': jnp.array([2.0])
125
+ }
126
+
127
+ # L2 norm = sqrt(1 + 4 + 4) = 3
128
+ max_norm = 5.0
129
+ clipped_grads, norm = brainstate.nn.clip_grad_norm(grads, max_norm=max_norm, return_norm=True)
130
+
131
+ self.assertAlmostEqual(norm, 3.0, places=5)
132
+
133
+ # Gradients should be unchanged
134
+ np.testing.assert_array_almost_equal(
135
+ clipped_grads['w'], grads['w'], decimal=7
136
+ )
137
+ np.testing.assert_array_almost_equal(
138
+ clipped_grads['b'], grads['b'], decimal=7
139
+ )
140
+
141
+ @parameterized.parameters(
142
+ (1, 'L1'), # L1 norm
143
+ (2, 'L2'), # L2 norm (default)
144
+ (2.0, 'L2'), # L2 norm with float
145
+ (3, 'L3'), # L3 norm
146
+ ('inf', 'Linf'), # Infinity norm
147
+ (jnp.inf, 'Linf'), # Infinity norm with jnp.inf
148
+ )
149
+ def test_different_norm_types(self, norm_type, norm_name):
150
+ """Test gradient clipping with different norm types."""
151
+ grads = {
152
+ 'param': jnp.array([[-2.0, 3.0], [1.0, -4.0]])
153
+ }
154
+
155
+ max_norm = 3.0
156
+ clipped_grads, computed_norm = brainstate.nn.clip_grad_norm(
157
+ grads, max_norm=max_norm, norm_type=norm_type, return_norm=True
158
+ )
159
+
160
+ # Compute expected norm
161
+ flat_grads = grads['param'].ravel()
162
+ if norm_type == 'inf' or norm_type == jnp.inf:
163
+ expected_norm = jnp.max(jnp.abs(flat_grads))
164
+ else:
165
+ expected_norm = jnp.linalg.norm(flat_grads, ord=norm_type)
166
+
167
+ self.assertAlmostEqual(computed_norm, expected_norm, places=5)
168
+
169
+ # Check scaling
170
+ if expected_norm > max_norm:
171
+ scale = max_norm / expected_norm
172
+ np.testing.assert_array_almost_equal(
173
+ clipped_grads['param'],
174
+ grads['param'] * scale,
175
+ decimal=5
176
+ )
177
+ else:
178
+ np.testing.assert_array_almost_equal(
179
+ clipped_grads['param'],
180
+ grads['param'],
181
+ decimal=5
182
+ )
183
+
184
+ def test_zero_gradients(self):
185
+ """Test handling of zero gradients."""
186
+ grads = {
187
+ 'w': jnp.zeros((3, 4)),
188
+ 'b': jnp.zeros(4)
189
+ }
190
+
191
+ clipped_grads, norm = brainstate.nn.clip_grad_norm(grads, max_norm=1.0, return_norm=True)
192
+
193
+ self.assertAlmostEqual(norm, 0.0, places=7)
194
+ np.testing.assert_array_equal(clipped_grads['w'], grads['w'])
195
+ np.testing.assert_array_equal(clipped_grads['b'], grads['b'])
196
+
197
+ def test_single_tensor_input(self):
198
+ """Test with a single tensor instead of a PyTree."""
199
+ grad = jnp.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
200
+
201
+ max_norm = 5.0
202
+ clipped_grad, norm = brainstate.nn.clip_grad_norm(grad, max_norm=max_norm, return_norm=True)
203
+
204
+ expected_norm = jnp.linalg.norm(grad.ravel())
205
+ self.assertAlmostEqual(norm, expected_norm, places=5)
206
+
207
+ scale = max_norm / expected_norm
208
+ np.testing.assert_array_almost_equal(
209
+ clipped_grad,
210
+ grad * scale,
211
+ decimal=5
212
+ )
213
+
214
+ def test_list_structure(self):
215
+ """Test gradient clipping with list structure."""
216
+ grads = [
217
+ jnp.array([1.0, 2.0]),
218
+ jnp.array([[3.0, 4.0], [5.0, 6.0]]),
219
+ jnp.array([7.0])
220
+ ]
221
+
222
+ max_norm = 10.0
223
+ clipped_grads, norm = brainstate.nn.clip_grad_norm(grads, max_norm=max_norm, return_norm=True)
224
+
225
+ # Check structure is preserved
226
+ self.assertIsInstance(clipped_grads, list)
227
+ self.assertEqual(len(clipped_grads), 3)
228
+
229
+ # Check norm computation
230
+ flat = jnp.arange(1.0, 8.0)
231
+ expected_norm = jnp.linalg.norm(flat)
232
+ self.assertAlmostEqual(norm, expected_norm, places=5)
233
+
234
+ def test_tuple_structure(self):
235
+ """Test gradient clipping with tuple structure."""
236
+ grads = (
237
+ jnp.array([3.0, 4.0]),
238
+ jnp.array([5.0])
239
+ )
240
+
241
+ max_norm = 5.0
242
+ clipped_grads, norm = brainstate.nn.clip_grad_norm(grads, max_norm=max_norm, return_norm=True)
243
+
244
+ # Check structure is preserved
245
+ self.assertIsInstance(clipped_grads, tuple)
246
+ self.assertEqual(len(clipped_grads), 2)
247
+
248
+ # Check norm: sqrt(9 + 16 + 25) = sqrt(50) ≈ 7.07
249
+ expected_norm = jnp.sqrt(50.0)
250
+ self.assertAlmostEqual(norm, expected_norm, places=5)
251
+
252
+ def test_max_norm_as_array(self):
253
+ """Test using JAX array for max_norm parameter."""
254
+ grads = {'w': jnp.array([6.0, 8.0])}
255
+ max_norm = jnp.array(5.0)
256
+
257
+ clipped_grads, norm = brainstate.nn.clip_grad_norm(grads, max_norm=max_norm, return_norm=True)
258
+
259
+ # norm = sqrt(36 + 64) = 10
260
+ self.assertAlmostEqual(norm, 10.0, places=5)
261
+
262
+ # Should be scaled by 5/10 = 0.5
263
+ np.testing.assert_array_almost_equal(
264
+ clipped_grads['w'],
265
+ jnp.array([3.0, 4.0]),
266
+ decimal=5
267
+ )
268
+
269
+ def test_none_norm_type(self):
270
+ """Test that None norm_type defaults to L2 norm."""
271
+ grads = {'param': jnp.array([3.0, 4.0])}
272
+
273
+ # Test with explicit None
274
+ clipped1, norm1 = brainstate.nn.clip_grad_norm(grads, max_norm=10.0, norm_type=None, return_norm=True)
275
+
276
+ # Test with default (should be same as L2)
277
+ clipped2, norm2 = brainstate.nn.clip_grad_norm(grads, max_norm=10.0, norm_type=2.0, return_norm=True)
278
+
279
+ self.assertAlmostEqual(norm1, norm2, places=7)
280
+ np.testing.assert_array_almost_equal(
281
+ clipped1['param'], clipped2['param'], decimal=7
282
+ )
283
+
284
+ def test_very_large_gradients(self):
285
+ """Test clipping very large gradients."""
286
+ grads = {
287
+ 'huge': jnp.array([1e10, 1e10, 1e10])
288
+ }
289
+
290
+ max_norm = 1.0
291
+ clipped_grads, norm = brainstate.nn.clip_grad_norm(grads, max_norm=max_norm, return_norm=True)
292
+
293
+ # Check that clipped norm is approximately max_norm
294
+ clipped_norm = jnp.linalg.norm(clipped_grads['huge'])
295
+ self.assertAlmostEqual(clipped_norm, max_norm, places=5)
296
+
297
+ def test_very_small_gradients(self):
298
+ """Test handling very small gradients (numerical stability)."""
299
+ grads = {
300
+ 'tiny': jnp.array([1e-10, 1e-10, 1e-10])
301
+ }
302
+
303
+ max_norm = 1.0
304
+ clipped_grads, norm = brainstate.nn.clip_grad_norm(grads, max_norm=max_norm, return_norm=True)
305
+
306
+ # Should not be clipped
307
+ np.testing.assert_array_almost_equal(
308
+ clipped_grads['tiny'], grads['tiny'], decimal=15
309
+ )
310
+
311
+ def test_mixed_shapes(self):
312
+ """Test with mixed tensor shapes in PyTree."""
313
+ grads = {
314
+ 'scalar': jnp.array(2.0),
315
+ 'vector': jnp.array([3.0, 4.0]),
316
+ 'matrix': jnp.array([[1.0, 2.0], [3.0, 4.0]]),
317
+ 'tensor3d': jnp.ones((2, 3, 4))
318
+ }
319
+
320
+ max_norm = 10.0
321
+ clipped_grads, norm = brainstate.nn.clip_grad_norm(grads, max_norm=max_norm, return_norm=True)
322
+
323
+ # Check all shapes are preserved
324
+ self.assertEqual(clipped_grads['scalar'].shape, ())
325
+ self.assertEqual(clipped_grads['vector'].shape, (2,))
326
+ self.assertEqual(clipped_grads['matrix'].shape, (2, 2))
327
+ self.assertEqual(clipped_grads['tensor3d'].shape, (2, 3, 4))
328
+
329
+ def test_gradient_clipping_invariants(self):
330
+ """Test mathematical invariants of gradient clipping."""
331
+ grads = {
332
+ 'w1': jnp.array([[1.0, 2.0], [3.0, 4.0]]),
333
+ 'w2': jnp.array([5.0, 6.0])
334
+ }
335
+
336
+ max_norm = 5.0
337
+ clipped_grads, original_norm = brainstate.nn.clip_grad_norm(grads, max_norm=max_norm, return_norm=True)
338
+
339
+ # Compute norm of clipped gradients
340
+ clipped_flat = jnp.concatenate([g.ravel() for g in jax.tree.leaves(clipped_grads)])
341
+ clipped_norm = jnp.linalg.norm(clipped_flat)
342
+
343
+ # Clipped norm should be min(original_norm, max_norm)
344
+ expected_clipped_norm = jnp.minimum(original_norm, max_norm)
345
+ self.assertAlmostEqual(clipped_norm, expected_clipped_norm, places=5)
346
+
347
+ @parameterized.parameters(
348
+ (0.5,),
349
+ (1.0,),
350
+ (2.0,),
351
+ (5.0,),
352
+ (10.0,),
353
+ )
354
+ def test_different_max_norms(self, max_norm):
355
+ """Test gradient clipping with various max_norm values."""
356
+ grads = {'param': jnp.array([6.0, 8.0])} # norm = 10
357
+
358
+ clipped_grads, norm = brainstate.nn.clip_grad_norm(grads, max_norm=max_norm, return_norm=True)
359
+
360
+ self.assertAlmostEqual(norm, 10.0, places=5)
361
+
362
+ # Check clipped norm
363
+ clipped_norm = jnp.linalg.norm(clipped_grads['param'])
364
+ if max_norm < 10.0:
365
+ self.assertAlmostEqual(clipped_norm, max_norm, places=5)
366
+ else:
367
+ self.assertAlmostEqual(clipped_norm, 10.0, places=5)
368
+
369
+ def test_empty_pytree(self):
370
+ """Test handling of empty PyTree."""
371
+ grads = {}
372
+
373
+ # Test with return_norm=True
374
+ clipped_grads, norm = brainstate.nn.clip_grad_norm(grads, max_norm=1.0, return_norm=True)
375
+ self.assertEqual(clipped_grads, {})
376
+ self.assertAlmostEqual(norm, 0.0, places=7)
377
+
378
+ # Test with return_norm=False
379
+ clipped_grads_only = brainstate.nn.clip_grad_norm(grads, max_norm=1.0, return_norm=False)
380
+ self.assertEqual(clipped_grads_only, {})
381
+
382
+ def test_pytree_with_none_leaves(self):
383
+ """Test PyTree containing None values (should be filtered out)."""
384
+ grads = {
385
+ 'w': jnp.array([3.0, 4.0]),
386
+ 'b': None, # This should be filtered by jax.tree.leaves
387
+ 'c': jnp.array([5.0])
388
+ }
389
+
390
+ # This test depends on how the function handles None values
391
+ # JAX typically filters them out
392
+ try:
393
+ clipped_grads, norm = brainstate.nn.clip_grad_norm(grads, max_norm=5.0, return_norm=True)
394
+ # If it works, check that None is preserved in structure
395
+ self.assertIn('b', clipped_grads)
396
+ except:
397
+ # Expected if None values cause issues
398
+ pass
399
+
400
+
401
+ if __name__ == '__main__':
402
+ unittest.main()