brainstate 0.2.1__py2.py3-none-any.whl → 0.2.2__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.
- brainstate/__init__.py +167 -169
- brainstate/_compatible_import.py +340 -340
- brainstate/_compatible_import_test.py +681 -681
- brainstate/_deprecation.py +210 -210
- brainstate/_deprecation_test.py +2297 -2319
- brainstate/_error.py +45 -45
- brainstate/_state.py +2157 -1652
- brainstate/_state_test.py +1129 -52
- brainstate/_utils.py +47 -47
- brainstate/environ.py +1495 -1495
- brainstate/environ_test.py +1223 -1223
- brainstate/graph/__init__.py +22 -22
- brainstate/graph/_node.py +240 -240
- brainstate/graph/_node_test.py +589 -589
- brainstate/graph/_operation.py +1620 -1624
- brainstate/graph/_operation_test.py +1147 -1147
- brainstate/mixin.py +1447 -1433
- brainstate/mixin_test.py +1017 -1017
- brainstate/nn/__init__.py +146 -137
- brainstate/nn/_activations.py +1100 -1100
- brainstate/nn/_activations_test.py +354 -354
- brainstate/nn/_collective_ops.py +635 -633
- brainstate/nn/_collective_ops_test.py +774 -774
- brainstate/nn/_common.py +226 -226
- brainstate/nn/_common_test.py +134 -154
- brainstate/nn/_conv.py +2010 -2010
- brainstate/nn/_conv_test.py +849 -849
- brainstate/nn/_delay.py +575 -575
- brainstate/nn/_delay_test.py +243 -243
- brainstate/nn/_dropout.py +618 -618
- brainstate/nn/_dropout_test.py +480 -477
- brainstate/nn/_dynamics.py +870 -1267
- brainstate/nn/_dynamics_test.py +53 -67
- brainstate/nn/_elementwise.py +1298 -1298
- brainstate/nn/_elementwise_test.py +829 -829
- brainstate/nn/_embedding.py +408 -408
- brainstate/nn/_embedding_test.py +156 -156
- brainstate/nn/_event_fixedprob.py +233 -233
- brainstate/nn/_event_fixedprob_test.py +115 -115
- brainstate/nn/_event_linear.py +83 -83
- brainstate/nn/_event_linear_test.py +121 -121
- brainstate/nn/_exp_euler.py +254 -254
- brainstate/nn/_exp_euler_test.py +377 -377
- brainstate/nn/_linear.py +744 -744
- brainstate/nn/_linear_test.py +475 -475
- brainstate/nn/_metrics.py +1070 -1070
- brainstate/nn/_metrics_test.py +611 -611
- brainstate/nn/_module.py +391 -384
- brainstate/nn/_module_test.py +40 -40
- brainstate/nn/_normalizations.py +1334 -1334
- brainstate/nn/_normalizations_test.py +699 -699
- brainstate/nn/_paddings.py +1020 -1020
- brainstate/nn/_paddings_test.py +722 -722
- brainstate/nn/_poolings.py +2239 -2239
- brainstate/nn/_poolings_test.py +952 -952
- brainstate/nn/_rnns.py +946 -946
- brainstate/nn/_rnns_test.py +592 -592
- brainstate/nn/_utils.py +216 -216
- brainstate/nn/_utils_test.py +401 -401
- brainstate/nn/init.py +809 -809
- brainstate/nn/init_test.py +180 -180
- brainstate/random/__init__.py +270 -270
- brainstate/random/{_rand_funs.py → _fun.py} +3938 -3938
- brainstate/random/{_rand_funs_test.py → _fun_test.py} +638 -640
- brainstate/random/_impl.py +672 -0
- brainstate/random/{_rand_seed.py → _seed.py} +675 -675
- brainstate/random/{_rand_seed_test.py → _seed_test.py} +48 -48
- brainstate/random/{_rand_state.py → _state.py} +1320 -1617
- brainstate/random/{_rand_state_test.py → _state_test.py} +551 -551
- brainstate/transform/__init__.py +56 -59
- brainstate/transform/_ad_checkpoint.py +176 -176
- brainstate/transform/_ad_checkpoint_test.py +49 -49
- brainstate/transform/_autograd.py +1025 -1025
- brainstate/transform/_autograd_test.py +1289 -1289
- brainstate/transform/_conditions.py +316 -316
- brainstate/transform/_conditions_test.py +220 -220
- brainstate/transform/_error_if.py +94 -94
- brainstate/transform/_error_if_test.py +52 -52
- brainstate/transform/_find_state.py +200 -0
- brainstate/transform/_find_state_test.py +84 -0
- brainstate/transform/_jit.py +399 -399
- brainstate/transform/_jit_test.py +143 -143
- brainstate/transform/_loop_collect_return.py +675 -675
- brainstate/transform/_loop_collect_return_test.py +58 -58
- brainstate/transform/_loop_no_collection.py +283 -283
- brainstate/transform/_loop_no_collection_test.py +50 -50
- brainstate/transform/_make_jaxpr.py +2176 -2016
- brainstate/transform/_make_jaxpr_test.py +1634 -1510
- brainstate/transform/_mapping.py +607 -529
- brainstate/transform/_mapping_test.py +104 -194
- brainstate/transform/_progress_bar.py +255 -255
- brainstate/transform/_unvmap.py +256 -256
- brainstate/transform/_util.py +286 -286
- brainstate/typing.py +837 -837
- brainstate/typing_test.py +780 -780
- brainstate/util/__init__.py +27 -27
- brainstate/util/_others.py +1024 -1024
- brainstate/util/_others_test.py +962 -962
- brainstate/util/_pretty_pytree.py +1301 -1301
- brainstate/util/_pretty_pytree_test.py +675 -675
- brainstate/util/_pretty_repr.py +462 -462
- brainstate/util/_pretty_repr_test.py +696 -696
- brainstate/util/filter.py +945 -945
- brainstate/util/filter_test.py +911 -911
- brainstate/util/struct.py +910 -910
- brainstate/util/struct_test.py +602 -602
- {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/METADATA +108 -108
- brainstate-0.2.2.dist-info/RECORD +111 -0
- {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/licenses/LICENSE +202 -202
- brainstate/transform/_eval_shape.py +0 -145
- brainstate/transform/_eval_shape_test.py +0 -38
- brainstate/transform/_random.py +0 -171
- brainstate-0.2.1.dist-info/RECORD +0 -111
- {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/WHEEL +0 -0
- {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/top_level.txt +0 -0
brainstate/nn/_utils_test.py
CHANGED
@@ -1,402 +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__':
|
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
402
|
unittest.main()
|