brainstate 0.1.10__py2.py3-none-any.whl → 0.2.0__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 +130 -19
  2. brainstate/_compatible_import.py +201 -9
  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} +10 -20
  7. brainstate/_state.py +94 -47
  8. brainstate/_state_test.py +1 -1
  9. brainstate/_utils.py +1 -1
  10. brainstate/environ.py +1279 -347
  11. brainstate/environ_test.py +1187 -26
  12. brainstate/graph/__init__.py +6 -13
  13. brainstate/graph/_node.py +240 -0
  14. brainstate/graph/_node_test.py +589 -0
  15. brainstate/graph/{_graph_operation.py → _operation.py} +632 -746
  16. brainstate/graph/_operation_test.py +1147 -0
  17. brainstate/mixin.py +1209 -141
  18. brainstate/mixin_test.py +991 -51
  19. brainstate/nn/__init__.py +74 -72
  20. brainstate/nn/_activations.py +587 -295
  21. brainstate/nn/_activations_test.py +109 -86
  22. brainstate/nn/_collective_ops.py +393 -274
  23. brainstate/nn/_collective_ops_test.py +746 -15
  24. brainstate/nn/_common.py +114 -66
  25. brainstate/nn/_common_test.py +154 -0
  26. brainstate/nn/_conv.py +1652 -143
  27. brainstate/nn/_conv_test.py +838 -227
  28. brainstate/nn/_delay.py +15 -28
  29. brainstate/nn/_delay_test.py +25 -20
  30. brainstate/nn/_dropout.py +359 -167
  31. brainstate/nn/_dropout_test.py +429 -52
  32. brainstate/nn/_dynamics.py +14 -90
  33. brainstate/nn/_dynamics_test.py +1 -12
  34. brainstate/nn/_elementwise.py +492 -313
  35. brainstate/nn/_elementwise_test.py +806 -145
  36. brainstate/nn/_embedding.py +369 -19
  37. brainstate/nn/_embedding_test.py +156 -0
  38. brainstate/nn/{_fixedprob.py → _event_fixedprob.py} +10 -16
  39. brainstate/nn/{_fixedprob_test.py → _event_fixedprob_test.py} +6 -5
  40. brainstate/nn/{_linear_mv.py → _event_linear.py} +2 -2
  41. brainstate/nn/{_linear_mv_test.py → _event_linear_test.py} +6 -5
  42. brainstate/nn/_exp_euler.py +200 -38
  43. brainstate/nn/_exp_euler_test.py +350 -8
  44. brainstate/nn/_linear.py +391 -71
  45. brainstate/nn/_linear_test.py +427 -59
  46. brainstate/nn/_metrics.py +1070 -0
  47. brainstate/nn/_metrics_test.py +611 -0
  48. brainstate/nn/_module.py +10 -3
  49. brainstate/nn/_module_test.py +1 -1
  50. brainstate/nn/_normalizations.py +688 -329
  51. brainstate/nn/_normalizations_test.py +663 -37
  52. brainstate/nn/_paddings.py +1020 -0
  53. brainstate/nn/_paddings_test.py +723 -0
  54. brainstate/nn/_poolings.py +1404 -342
  55. brainstate/nn/_poolings_test.py +828 -92
  56. brainstate/nn/{_rate_rnns.py → _rnns.py} +446 -54
  57. brainstate/nn/_rnns_test.py +593 -0
  58. brainstate/nn/_utils.py +132 -5
  59. brainstate/nn/_utils_test.py +402 -0
  60. brainstate/{init/_random_inits.py → nn/init.py} +301 -45
  61. brainstate/{init/_random_inits_test.py → nn/init_test.py} +51 -20
  62. brainstate/random/__init__.py +247 -1
  63. brainstate/random/_rand_funs.py +668 -346
  64. brainstate/random/_rand_funs_test.py +74 -1
  65. brainstate/random/_rand_seed.py +541 -76
  66. brainstate/random/_rand_seed_test.py +1 -1
  67. brainstate/random/_rand_state.py +601 -393
  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 +1 -1
  72. brainstate/{augment → transform}/_autograd.py +360 -113
  73. brainstate/{augment → transform}/_autograd_test.py +2 -2
  74. brainstate/transform/_conditions.py +316 -0
  75. brainstate/{compile → transform}/_conditions_test.py +11 -11
  76. brainstate/{compile → transform}/_error_if.py +22 -20
  77. brainstate/{compile → transform}/_error_if_test.py +1 -1
  78. brainstate/transform/_eval_shape.py +145 -0
  79. brainstate/{augment → transform}/_eval_shape_test.py +1 -1
  80. brainstate/{compile → transform}/_jit.py +99 -46
  81. brainstate/{compile → transform}/_jit_test.py +3 -3
  82. brainstate/{compile → transform}/_loop_collect_return.py +219 -80
  83. brainstate/{compile → transform}/_loop_collect_return_test.py +1 -1
  84. brainstate/{compile → transform}/_loop_no_collection.py +133 -34
  85. brainstate/{compile → transform}/_loop_no_collection_test.py +2 -2
  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 +78 -25
  91. brainstate/{augment → transform}/_random.py +65 -45
  92. brainstate/{compile → transform}/_unvmap.py +102 -5
  93. brainstate/transform/_util.py +286 -0
  94. brainstate/typing.py +594 -61
  95. brainstate/typing_test.py +780 -0
  96. brainstate/util/__init__.py +9 -32
  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} +161 -27
  102. brainstate/util/_pretty_repr_test.py +696 -0
  103. brainstate/util/filter.py +557 -81
  104. brainstate/util/filter_test.py +912 -0
  105. brainstate/util/struct.py +769 -382
  106. brainstate/util/struct_test.py +602 -0
  107. {brainstate-0.1.10.dist-info → brainstate-0.2.0.dist-info}/METADATA +34 -17
  108. brainstate-0.2.0.dist-info/RECORD +111 -0
  109. brainstate/augment/__init__.py +0 -30
  110. brainstate/augment/_eval_shape.py +0 -99
  111. brainstate/augment/_mapping.py +0 -1060
  112. brainstate/augment/_mapping_test.py +0 -597
  113. brainstate/compile/__init__.py +0 -38
  114. brainstate/compile/_ad_checkpoint.py +0 -204
  115. brainstate/compile/_conditions.py +0 -256
  116. brainstate/compile/_make_jaxpr.py +0 -888
  117. brainstate/compile/_make_jaxpr_test.py +0 -156
  118. brainstate/compile/_util.py +0 -147
  119. brainstate/functional/__init__.py +0 -27
  120. brainstate/graph/_graph_node.py +0 -244
  121. brainstate/graph/_graph_node_test.py +0 -73
  122. brainstate/graph/_graph_operation_test.py +0 -563
  123. brainstate/init/__init__.py +0 -26
  124. brainstate/init/_base.py +0 -52
  125. brainstate/init/_generic.py +0 -244
  126. brainstate/init/_regular_inits.py +0 -105
  127. brainstate/init/_regular_inits_test.py +0 -50
  128. brainstate/nn/_inputs.py +0 -608
  129. brainstate/nn/_ltp.py +0 -28
  130. brainstate/nn/_neuron.py +0 -705
  131. brainstate/nn/_neuron_test.py +0 -161
  132. brainstate/nn/_others.py +0 -46
  133. brainstate/nn/_projection.py +0 -486
  134. brainstate/nn/_rate_rnns_test.py +0 -63
  135. brainstate/nn/_readout.py +0 -209
  136. brainstate/nn/_readout_test.py +0 -53
  137. brainstate/nn/_stp.py +0 -236
  138. brainstate/nn/_synapse.py +0 -505
  139. brainstate/nn/_synapse_test.py +0 -131
  140. brainstate/nn/_synaptic_projection.py +0 -423
  141. brainstate/nn/_synouts.py +0 -162
  142. brainstate/nn/_synouts_test.py +0 -57
  143. brainstate/nn/metrics.py +0 -388
  144. brainstate/optim/__init__.py +0 -38
  145. brainstate/optim/_base.py +0 -64
  146. brainstate/optim/_lr_scheduler.py +0 -448
  147. brainstate/optim/_lr_scheduler_test.py +0 -50
  148. brainstate/optim/_optax_optimizer.py +0 -152
  149. brainstate/optim/_optax_optimizer_test.py +0 -53
  150. brainstate/optim/_sgd_optimizer.py +0 -1104
  151. brainstate/random/_random_for_unit.py +0 -52
  152. brainstate/surrogate.py +0 -1957
  153. brainstate/transform.py +0 -23
  154. brainstate/util/caller.py +0 -98
  155. brainstate/util/others.py +0 -540
  156. brainstate/util/pretty_pytree.py +0 -945
  157. brainstate/util/pretty_pytree_test.py +0 -159
  158. brainstate/util/pretty_table.py +0 -2954
  159. brainstate/util/scaling.py +0 -258
  160. brainstate-0.1.10.dist-info/RECORD +0 -130
  161. {brainstate-0.1.10.dist-info → brainstate-0.2.0.dist-info}/WHEEL +0 -0
  162. {brainstate-0.1.10.dist-info → brainstate-0.2.0.dist-info}/licenses/LICENSE +0 -0
  163. {brainstate-0.1.10.dist-info → brainstate-0.2.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,723 @@
1
+ # Copyright 2025 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
+ """
17
+ Comprehensive tests for padding layers.
18
+ """
19
+
20
+ import unittest
21
+
22
+ import jax
23
+ import jax.numpy as jnp
24
+ import numpy as np
25
+
26
+ import brainstate
27
+ from brainstate.nn._paddings import _format_padding
28
+
29
+
30
+ class TestPaddingFormatting(unittest.TestCase):
31
+ """Test the _format_padding helper function."""
32
+
33
+ def test_format_padding_1d_int(self):
34
+ """Test formatting integer padding for 1D."""
35
+ result = _format_padding(2, 1)
36
+ self.assertEqual(result, [(2, 2)])
37
+
38
+ def test_format_padding_2d_int(self):
39
+ """Test formatting integer padding for 2D."""
40
+
41
+ result = _format_padding(3, 2)
42
+ self.assertEqual(result, [(3, 3), (3, 3)])
43
+
44
+ def test_format_padding_3d_int(self):
45
+ """Test formatting integer padding for 3D."""
46
+
47
+ result = _format_padding(1, 3)
48
+ self.assertEqual(result, [(1, 1), (1, 1), (1, 1)])
49
+
50
+ def test_format_padding_1d_tuple_symmetric(self):
51
+ """Test formatting symmetric tuple padding for 1D."""
52
+
53
+ result = _format_padding([2], 1)
54
+ self.assertEqual(result, [(2, 2)])
55
+
56
+ def test_format_padding_2d_tuple_symmetric(self):
57
+ """Test formatting symmetric tuple padding for 2D."""
58
+
59
+ result = _format_padding([2, 3], 2)
60
+ self.assertEqual(result, [(2, 2), (3, 3)])
61
+
62
+ def test_format_padding_1d_tuple_asymmetric(self):
63
+ """Test formatting asymmetric tuple padding for 1D."""
64
+
65
+ result = _format_padding([1, 2], 1)
66
+ self.assertEqual(result, [(1, 2)])
67
+
68
+ def test_format_padding_2d_tuple_asymmetric(self):
69
+ """Test formatting asymmetric tuple padding for 2D."""
70
+
71
+ result = _format_padding([1, 2, 3, 4], 2)
72
+ self.assertEqual(result, [(1, 2), (3, 4)])
73
+
74
+ def test_format_padding_invalid_length(self):
75
+ """Test that invalid padding length raises ValueError."""
76
+
77
+ with self.assertRaises(ValueError):
78
+ _format_padding([1, 2, 3], 2) # Should be 2 or 4 elements for 2D
79
+
80
+
81
+ class TestReflectionPad1d(unittest.TestCase):
82
+ """Test ReflectionPad1d class."""
83
+
84
+ def test_reflection_pad1d_2d_input(self):
85
+ """Test ReflectionPad1d with 2D input (length, channels)."""
86
+ pad = brainstate.nn.ReflectionPad1d(2)
87
+ x = jnp.array([[1, 2], [3, 4], [5, 6]]) # (3, 2)
88
+ output = pad(x)
89
+ self.assertEqual(output.shape, (7, 2)) # 3 + 2*2 = 7
90
+ # Check reflection pattern for first channel
91
+ expected_first_channel = jnp.array([5, 3, 1, 3, 5, 3, 1])
92
+ np.testing.assert_array_equal(output[:, 0], expected_first_channel)
93
+
94
+ def test_reflection_pad1d_3d_input(self):
95
+ """Test ReflectionPad1d with 3D input (batch, length, channels)."""
96
+ pad = brainstate.nn.ReflectionPad1d(1)
97
+ x = jnp.ones((2, 4, 3))
98
+ output = pad(x)
99
+ self.assertEqual(output.shape, (2, 6, 3)) # 4 + 2*1 = 6
100
+
101
+ def test_reflection_pad1d_asymmetric(self):
102
+ """Test ReflectionPad1d with asymmetric padding."""
103
+ pad = brainstate.nn.ReflectionPad1d([1, 2])
104
+ x = jnp.ones((2, 5, 3))
105
+ output = pad(x)
106
+ self.assertEqual(output.shape, (2, 8, 3)) # 5 + 1 + 2 = 8
107
+
108
+ def test_reflection_pad1d_with_in_size(self):
109
+ """Test ReflectionPad1d with in_size parameter."""
110
+ pad = brainstate.nn.ReflectionPad1d(2, in_size=(10, 3))
111
+ self.assertEqual(pad.out_size, (14, 3))
112
+
113
+ def test_reflection_pad1d_invalid_shape(self):
114
+ """Test that ReflectionPad1d raises error for invalid input shape."""
115
+ pad = brainstate.nn.ReflectionPad1d(2)
116
+ x = jnp.ones((2, 3, 4, 5)) # 4D input
117
+ with self.assertRaises(ValueError):
118
+ pad(x)
119
+
120
+
121
+ class TestReflectionPad2d(unittest.TestCase):
122
+ """Test ReflectionPad2d class."""
123
+
124
+ def test_reflection_pad2d_3d_input(self):
125
+ """Test ReflectionPad2d with 3D input (height, width, channels)."""
126
+ pad = brainstate.nn.ReflectionPad2d(1)
127
+ x = jnp.ones((4, 4, 3))
128
+ output = pad(x)
129
+ self.assertEqual(output.shape, (6, 6, 3))
130
+
131
+ def test_reflection_pad2d_4d_input(self):
132
+ """Test ReflectionPad2d with 4D input (batch, height, width, channels)."""
133
+ pad = brainstate.nn.ReflectionPad2d(2)
134
+ x = jnp.ones((2, 3, 3, 1))
135
+ output = pad(x)
136
+ self.assertEqual(output.shape, (2, 7, 7, 1))
137
+
138
+ def test_reflection_pad2d_different_hw_padding(self):
139
+ """Test ReflectionPad2d with different height and width padding."""
140
+ pad = brainstate.nn.ReflectionPad2d([1, 2])
141
+ x = jnp.ones((2, 4, 4, 3))
142
+ output = pad(x)
143
+ self.assertEqual(output.shape, (2, 6, 8, 3)) # 4+2*1, 4+2*2
144
+
145
+ def test_reflection_pad2d_asymmetric(self):
146
+ """Test ReflectionPad2d with asymmetric padding."""
147
+ pad = brainstate.nn.ReflectionPad2d([1, 2, 3, 4]) # left, right, top, bottom
148
+ x = jnp.ones((2, 5, 5, 3))
149
+ output = pad(x)
150
+ # For 2D: first pair is height (top/bottom), second is width (left/right)
151
+ # Height: 5+1+2=8, Width: 5+3+4=12
152
+ self.assertEqual(output.shape, (2, 8, 12, 3))
153
+
154
+ def test_reflection_pad2d_with_in_size(self):
155
+ """Test ReflectionPad2d with in_size parameter."""
156
+ pad = brainstate.nn.ReflectionPad2d([1, 2], in_size=(10, 10, 3))
157
+ self.assertEqual(pad.out_size, (12, 14, 3))
158
+
159
+
160
+ class TestReflectionPad3d(unittest.TestCase):
161
+ """Test ReflectionPad3d class."""
162
+
163
+ def test_reflection_pad3d_4d_input(self):
164
+ """Test ReflectionPad3d with 4D input (depth, height, width, channels)."""
165
+ pad = brainstate.nn.ReflectionPad3d(1)
166
+ x = jnp.ones((3, 4, 4, 2))
167
+ output = pad(x)
168
+ self.assertEqual(output.shape, (5, 6, 6, 2))
169
+
170
+ def test_reflection_pad3d_5d_input(self):
171
+ """Test ReflectionPad3d with 5D input (batch, depth, height, width, channels)."""
172
+ pad = brainstate.nn.ReflectionPad3d(2)
173
+ x = jnp.ones((1, 3, 3, 3, 1))
174
+ output = pad(x)
175
+ self.assertEqual(output.shape, (1, 7, 7, 7, 1))
176
+
177
+ def test_reflection_pad3d_different_padding(self):
178
+ """Test ReflectionPad3d with different padding for each dimension."""
179
+ pad = brainstate.nn.ReflectionPad3d([1, 2, 3])
180
+ x = jnp.ones((2, 4, 4, 4, 3))
181
+ output = pad(x)
182
+ self.assertEqual(output.shape, (2, 6, 8, 10, 3))
183
+
184
+ def test_reflection_pad3d_with_in_size(self):
185
+ """Test ReflectionPad3d with in_size parameter."""
186
+ pad = brainstate.nn.ReflectionPad3d(1, in_size=(1, 8, 8, 8, 3))
187
+ self.assertEqual(pad.out_size, (1, 10, 10, 10, 3))
188
+
189
+
190
+ class TestReplicationPad1d(unittest.TestCase):
191
+ """Test ReplicationPad1d class."""
192
+
193
+ def test_replication_pad1d_2d_input(self):
194
+ """Test ReplicationPad1d with 2D input."""
195
+ pad = brainstate.nn.ReplicationPad1d(2)
196
+ x = jnp.array([[1, 2], [3, 4], [5, 6]])
197
+ output = pad(x)
198
+ self.assertEqual(output.shape, (7, 2))
199
+ # Check that edges are replicated
200
+ np.testing.assert_array_equal(output[0, :], x[0, :])
201
+ np.testing.assert_array_equal(output[1, :], x[0, :])
202
+ np.testing.assert_array_equal(output[-1, :], x[-1, :])
203
+ np.testing.assert_array_equal(output[-2, :], x[-1, :])
204
+
205
+ def test_replication_pad1d_3d_input(self):
206
+ """Test ReplicationPad1d with 3D input."""
207
+ pad = brainstate.nn.ReplicationPad1d([1, 3])
208
+ x = jnp.ones((2, 4, 3))
209
+ output = pad(x)
210
+ self.assertEqual(output.shape, (2, 8, 3))
211
+
212
+ def test_replication_pad1d_with_in_size(self):
213
+ """Test ReplicationPad1d with in_size parameter."""
214
+ pad = brainstate.nn.ReplicationPad1d(3, in_size=(2, 10, 5))
215
+ self.assertEqual(pad.out_size, (2, 16, 5))
216
+
217
+
218
+ class TestReplicationPad2d(unittest.TestCase):
219
+ """Test ReplicationPad2d class."""
220
+
221
+ def test_replication_pad2d_3d_input(self):
222
+ """Test ReplicationPad2d with 3D input."""
223
+ pad = brainstate.nn.ReplicationPad2d(1)
224
+ x = jnp.arange(12).reshape(3, 4, 1)
225
+ output = pad(x)
226
+ self.assertEqual(output.shape, (5, 6, 1))
227
+ # Check corners are replicated correctly
228
+ self.assertEqual(output[0, 0, 0], x[0, 0, 0])
229
+ self.assertEqual(output[-1, -1, 0], x[-1, -1, 0])
230
+
231
+ def test_replication_pad2d_4d_input(self):
232
+ """Test ReplicationPad2d with 4D input."""
233
+ pad = brainstate.nn.ReplicationPad2d([2, 1])
234
+ x = jnp.ones((2, 5, 5, 3))
235
+ output = pad(x)
236
+ self.assertEqual(output.shape, (2, 9, 7, 3))
237
+
238
+ def test_replication_pad2d_asymmetric(self):
239
+ """Test ReplicationPad2d with asymmetric padding."""
240
+ pad = brainstate.nn.ReplicationPad2d([1, 3, 2, 4])
241
+ x = jnp.ones((1, 4, 4, 2))
242
+ output = pad(x)
243
+ # Height: 4+1+3=8, Width: 4+2+4=10
244
+ self.assertEqual(output.shape, (1, 8, 10, 2))
245
+
246
+
247
+ class TestReplicationPad3d(unittest.TestCase):
248
+ """Test ReplicationPad3d class."""
249
+
250
+ def test_replication_pad3d_4d_input(self):
251
+ """Test ReplicationPad3d with 4D input."""
252
+ pad = brainstate.nn.ReplicationPad3d(1)
253
+ x = jnp.ones((3, 3, 3, 2))
254
+ output = pad(x)
255
+ self.assertEqual(output.shape, (5, 5, 5, 2))
256
+
257
+ def test_replication_pad3d_5d_input(self):
258
+ """Test ReplicationPad3d with 5D input."""
259
+ pad = brainstate.nn.ReplicationPad3d([1, 2, 3])
260
+ x = jnp.ones((2, 4, 4, 4, 1))
261
+ output = pad(x)
262
+ self.assertEqual(output.shape, (2, 6, 8, 10, 1))
263
+
264
+
265
+ class TestZeroPad1d(unittest.TestCase):
266
+ """Test ZeroPad1d class."""
267
+
268
+ def test_zero_pad1d_2d_input(self):
269
+ """Test ZeroPad1d with 2D input."""
270
+ pad = brainstate.nn.ZeroPad1d(2)
271
+ x = jnp.ones((3, 2))
272
+ output = pad(x)
273
+ self.assertEqual(output.shape, (7, 2))
274
+ # Check that padding is zeros
275
+ np.testing.assert_array_equal(output[:2, :], jnp.zeros((2, 2)))
276
+ np.testing.assert_array_equal(output[-2:, :], jnp.zeros((2, 2)))
277
+
278
+ def test_zero_pad1d_3d_input(self):
279
+ """Test ZeroPad1d with 3D input."""
280
+ pad = brainstate.nn.ZeroPad1d([3, 1])
281
+ x = jnp.ones((1, 5, 3))
282
+ output = pad(x)
283
+ self.assertEqual(output.shape, (1, 9, 3))
284
+ # Check padding values
285
+ np.testing.assert_array_equal(output[0, :3, :], jnp.zeros((3, 3)))
286
+ np.testing.assert_array_equal(output[0, -1:, :], jnp.zeros((1, 3)))
287
+
288
+ def test_zero_pad1d_with_in_size(self):
289
+ """Test ZeroPad1d with in_size parameter."""
290
+ pad = brainstate.nn.ZeroPad1d(5, in_size=(20, 10))
291
+ self.assertEqual(pad.out_size, (30, 10))
292
+
293
+
294
+ class TestZeroPad2d(unittest.TestCase):
295
+ """Test ZeroPad2d class."""
296
+
297
+ def test_zero_pad2d_3d_input(self):
298
+ """Test ZeroPad2d with 3D input."""
299
+ pad = brainstate.nn.ZeroPad2d(1)
300
+ x = jnp.ones((3, 3, 2))
301
+ output = pad(x)
302
+ self.assertEqual(output.shape, (5, 5, 2))
303
+ # Check borders are zero
304
+ np.testing.assert_array_equal(output[0, :, :], jnp.zeros((5, 2)))
305
+ np.testing.assert_array_equal(output[-1, :, :], jnp.zeros((5, 2)))
306
+ np.testing.assert_array_equal(output[:, 0, :], jnp.zeros((5, 2)))
307
+ np.testing.assert_array_equal(output[:, -1, :], jnp.zeros((5, 2)))
308
+
309
+ def test_zero_pad2d_4d_input(self):
310
+ """Test ZeroPad2d with 4D input."""
311
+ pad = brainstate.nn.ZeroPad2d([2, 3])
312
+ x = jnp.ones((2, 4, 4, 3))
313
+ output = pad(x)
314
+ self.assertEqual(output.shape, (2, 8, 10, 3))
315
+
316
+ def test_zero_pad2d_asymmetric(self):
317
+ """Test ZeroPad2d with asymmetric padding."""
318
+ pad = brainstate.nn.ZeroPad2d([1, 2, 3, 4])
319
+ x = jnp.ones((1, 5, 5, 1))
320
+ output = pad(x)
321
+ # Height: 5+1+2=8, Width: 5+3+4=12
322
+ self.assertEqual(output.shape, (1, 8, 12, 1))
323
+
324
+
325
+ class TestZeroPad3d(unittest.TestCase):
326
+ """Test ZeroPad3d class."""
327
+
328
+ def test_zero_pad3d_4d_input(self):
329
+ """Test ZeroPad3d with 4D input."""
330
+ pad = brainstate.nn.ZeroPad3d(1)
331
+ x = jnp.ones((2, 2, 2, 3))
332
+ output = pad(x)
333
+ self.assertEqual(output.shape, (4, 4, 4, 3))
334
+ # Check that corners are zero
335
+ self.assertEqual(output[0, 0, 0, 0], 0)
336
+ self.assertEqual(output[-1, -1, -1, 0], 0)
337
+
338
+ def test_zero_pad3d_5d_input(self):
339
+ """Test ZeroPad3d with 5D input."""
340
+ pad = brainstate.nn.ZeroPad3d([1, 1, 2])
341
+ x = jnp.ones((1, 3, 3, 3, 2))
342
+ output = pad(x)
343
+ self.assertEqual(output.shape, (1, 5, 5, 7, 2))
344
+
345
+
346
+ class TestConstantPad1d(unittest.TestCase):
347
+ """Test ConstantPad1d class."""
348
+
349
+ def test_constant_pad1d_default_value(self):
350
+ """Test ConstantPad1d with default value (0)."""
351
+ pad = brainstate.nn.ConstantPad1d(2)
352
+ x = jnp.ones((3, 2))
353
+ output = pad(x)
354
+ self.assertEqual(output.shape, (7, 2))
355
+ np.testing.assert_array_equal(output[:2, :], jnp.zeros((2, 2)))
356
+
357
+ def test_constant_pad1d_custom_value(self):
358
+ """Test ConstantPad1d with custom value."""
359
+ pad = brainstate.nn.ConstantPad1d(1, value=3.14)
360
+ x = jnp.ones((2, 4, 3))
361
+ output = pad(x)
362
+ self.assertEqual(output.shape, (2, 6, 3))
363
+ np.testing.assert_allclose(output[:, 0, :], 3.14)
364
+ np.testing.assert_allclose(output[:, -1, :], 3.14)
365
+
366
+ def test_constant_pad1d_asymmetric(self):
367
+ """Test ConstantPad1d with asymmetric padding."""
368
+ pad = brainstate.nn.ConstantPad1d([2, 3], value=-1)
369
+ x = jnp.zeros((1, 5, 2))
370
+ output = pad(x)
371
+ self.assertEqual(output.shape, (1, 10, 2))
372
+ np.testing.assert_array_equal(output[0, :2, :], -jnp.ones((2, 2)))
373
+ np.testing.assert_array_equal(output[0, -3:, :], -jnp.ones((3, 2)))
374
+
375
+ def test_constant_pad1d_with_in_size(self):
376
+ """Test ConstantPad1d with in_size parameter."""
377
+ pad = brainstate.nn.ConstantPad1d(4, value=2.5, in_size=(15, 8))
378
+ self.assertEqual(pad.out_size, (23, 8))
379
+
380
+
381
+ class TestConstantPad2d(unittest.TestCase):
382
+ """Test ConstantPad2d class."""
383
+
384
+ def test_constant_pad2d_default_value(self):
385
+ """Test ConstantPad2d with default value."""
386
+ pad = brainstate.nn.ConstantPad2d(1)
387
+ x = jnp.ones((3, 3, 1))
388
+ output = pad(x)
389
+ self.assertEqual(output.shape, (5, 5, 1))
390
+ # Check borders are zero
391
+ self.assertEqual(output[0, 0, 0], 0)
392
+
393
+ def test_constant_pad2d_custom_value(self):
394
+ """Test ConstantPad2d with custom value."""
395
+ pad = brainstate.nn.ConstantPad2d([1, 2], value=5.0)
396
+ x = jnp.zeros((2, 4, 4, 3))
397
+ output = pad(x)
398
+ self.assertEqual(output.shape, (2, 6, 8, 3))
399
+ np.testing.assert_array_equal(output[0, 0, :, 0], 5 * jnp.ones(8))
400
+ np.testing.assert_array_equal(output[0, :, 0, 0], 5 * jnp.ones(6))
401
+
402
+ def test_constant_pad2d_negative_value(self):
403
+ """Test ConstantPad2d with negative padding value."""
404
+ pad = brainstate.nn.ConstantPad2d(2, value=-2.5)
405
+ x = jnp.ones((1, 2, 2, 1))
406
+ output = pad(x)
407
+ self.assertEqual(output.shape, (1, 6, 6, 1))
408
+ self.assertEqual(output[0, 0, 0, 0], -2.5)
409
+
410
+
411
+ class TestConstantPad3d(unittest.TestCase):
412
+ """Test ConstantPad3d class."""
413
+
414
+ def test_constant_pad3d_default_value(self):
415
+ """Test ConstantPad3d with default value."""
416
+ pad = brainstate.nn.ConstantPad3d(1)
417
+ x = jnp.ones((2, 2, 2, 1))
418
+ output = pad(x)
419
+ self.assertEqual(output.shape, (4, 4, 4, 1))
420
+
421
+ def test_constant_pad3d_custom_value(self):
422
+ """Test ConstantPad3d with custom value."""
423
+ pad = brainstate.nn.ConstantPad3d([1, 2, 3], value=10)
424
+ x = jnp.zeros((1, 3, 3, 3, 2))
425
+ output = pad(x)
426
+ self.assertEqual(output.shape, (1, 5, 7, 9, 2))
427
+ # Check that padding has correct value
428
+ self.assertEqual(output[0, 0, 0, 0, 0], 10)
429
+ self.assertEqual(output[0, -1, -1, -1, 0], 10)
430
+
431
+ def test_constant_pad3d_asymmetric(self):
432
+ """Test ConstantPad3d with asymmetric padding."""
433
+ pad = brainstate.nn.ConstantPad3d([1, 2, 3, 4, 5, 6], value=7)
434
+ x = jnp.ones((1, 2, 2, 2, 1))
435
+ output = pad(x)
436
+ self.assertEqual(output.shape, (1, 5, 9, 13, 1))
437
+
438
+
439
+ class TestCircularPad1d(unittest.TestCase):
440
+ """Test CircularPad1d class."""
441
+
442
+ def test_circular_pad1d_2d_input(self):
443
+ """Test CircularPad1d with 2D input."""
444
+ pad = brainstate.nn.CircularPad1d(2)
445
+ x = jnp.array([[1], [2], [3], [4], [5]])
446
+ output = pad(x)
447
+ self.assertEqual(output.shape, (9, 1))
448
+ # Check circular pattern
449
+ expected = jnp.array([[4], [5], [1], [2], [3], [4], [5], [1], [2]])
450
+ np.testing.assert_array_equal(output, expected)
451
+
452
+ def test_circular_pad1d_3d_input(self):
453
+ """Test CircularPad1d with 3D input."""
454
+ pad = brainstate.nn.CircularPad1d(1)
455
+ x = jnp.arange(12).reshape(2, 3, 2)
456
+ output = pad(x)
457
+ self.assertEqual(output.shape, (2, 5, 2))
458
+ # Check wrapping for first batch
459
+ np.testing.assert_array_equal(output[0, 0, :], x[0, -1, :])
460
+ np.testing.assert_array_equal(output[0, -1, :], x[0, 0, :])
461
+
462
+ def test_circular_pad1d_asymmetric(self):
463
+ """Test CircularPad1d with asymmetric padding."""
464
+ pad = brainstate.nn.CircularPad1d([1, 2])
465
+ x = jnp.array([[[1], [2], [3]]])
466
+ output = pad(x)
467
+ self.assertEqual(output.shape, (1, 6, 1))
468
+ expected = jnp.array([[[3], [1], [2], [3], [1], [2]]])
469
+ np.testing.assert_array_equal(output, expected)
470
+
471
+ def test_circular_pad1d_with_in_size(self):
472
+ """Test CircularPad1d with in_size parameter."""
473
+ pad = brainstate.nn.CircularPad1d(3, in_size=(2, 10, 4))
474
+ self.assertEqual(pad.out_size, (2, 16, 4))
475
+
476
+
477
+ class TestCircularPad2d(unittest.TestCase):
478
+ """Test CircularPad2d class."""
479
+
480
+ def test_circular_pad2d_3d_input(self):
481
+ """Test CircularPad2d with 3D input."""
482
+ pad = brainstate.nn.CircularPad2d(1)
483
+ x = jnp.arange(9).reshape(3, 3, 1)
484
+ output = pad(x)
485
+ self.assertEqual(output.shape, (5, 5, 1))
486
+ # Check corners wrap correctly
487
+ self.assertEqual(output[0, 0, 0], x[-1, -1, 0])
488
+ self.assertEqual(output[0, -1, 0], x[-1, 0, 0])
489
+ self.assertEqual(output[-1, 0, 0], x[0, -1, 0])
490
+ self.assertEqual(output[-1, -1, 0], x[0, 0, 0])
491
+
492
+ def test_circular_pad2d_4d_input(self):
493
+ """Test CircularPad2d with 4D input."""
494
+ pad = brainstate.nn.CircularPad2d([1, 2])
495
+ x = jnp.ones((2, 3, 3, 2))
496
+ output = pad(x)
497
+ self.assertEqual(output.shape, (2, 5, 7, 2))
498
+
499
+ def test_circular_pad2d_different_padding(self):
500
+ """Test CircularPad2d with different padding for height and width."""
501
+ pad = brainstate.nn.CircularPad2d([2, 1])
502
+ x = jnp.arange(16).reshape(1, 4, 4, 1)
503
+ output = pad(x)
504
+ self.assertEqual(output.shape, (1, 8, 6, 1))
505
+
506
+
507
+ class TestCircularPad3d(unittest.TestCase):
508
+ """Test CircularPad3d class."""
509
+
510
+ def test_circular_pad3d_4d_input(self):
511
+ """Test CircularPad3d with 4D input."""
512
+ pad = brainstate.nn.CircularPad3d(1)
513
+ x = jnp.arange(8).reshape(2, 2, 2, 1)
514
+ output = pad(x)
515
+ self.assertEqual(output.shape, (4, 4, 4, 1))
516
+ # Check that values wrap around
517
+ self.assertEqual(output[0, 0, 0, 0], x[-1, -1, -1, 0])
518
+ self.assertEqual(output[-1, -1, -1, 0], x[0, 0, 0, 0])
519
+
520
+ def test_circular_pad3d_5d_input(self):
521
+ """Test CircularPad3d with 5D input."""
522
+ pad = brainstate.nn.CircularPad3d([1, 1, 2])
523
+ x = jnp.ones((1, 2, 2, 2, 3))
524
+ output = pad(x)
525
+ self.assertEqual(output.shape, (1, 4, 4, 6, 3))
526
+
527
+ def test_circular_pad3d_asymmetric(self):
528
+ """Test CircularPad3d with asymmetric padding."""
529
+ pad = brainstate.nn.CircularPad3d([1, 0, 0, 1, 1, 2])
530
+ x = jnp.ones((1, 3, 3, 3, 1))
531
+ output = pad(x)
532
+ self.assertEqual(output.shape, (1, 4, 4, 6, 1))
533
+
534
+
535
+ class TestPaddingIntegration(unittest.TestCase):
536
+ """Integration tests for padding layers."""
537
+
538
+ def test_all_1d_paddings_same_interface(self):
539
+ """Test that all 1D padding classes have the same interface."""
540
+ padding_classes = [
541
+ brainstate.nn.ReflectionPad1d,
542
+ brainstate.nn.ReplicationPad1d,
543
+ brainstate.nn.ZeroPad1d,
544
+ brainstate.nn.ConstantPad1d,
545
+ brainstate.nn.CircularPad1d
546
+ ]
547
+
548
+ x = jnp.ones((2, 10, 3))
549
+ for PadClass in padding_classes:
550
+ if PadClass == brainstate.nn.ConstantPad1d:
551
+ pad = PadClass(2, value=0)
552
+ else:
553
+ pad = PadClass(2)
554
+ output = pad(x)
555
+ self.assertEqual(output.shape, (2, 14, 3))
556
+
557
+ def test_all_2d_paddings_same_interface(self):
558
+ """Test that all 2D padding classes have the same interface."""
559
+ padding_classes = [
560
+ brainstate.nn.ReflectionPad2d,
561
+ brainstate.nn.ReplicationPad2d,
562
+ brainstate.nn.ZeroPad2d,
563
+ brainstate.nn.ConstantPad2d,
564
+ brainstate.nn.CircularPad2d
565
+ ]
566
+
567
+ x = jnp.ones((2, 8, 8, 3))
568
+ for PadClass in padding_classes:
569
+ if PadClass == brainstate.nn.ConstantPad2d:
570
+ pad = PadClass([1, 2], value=0)
571
+ else:
572
+ pad = PadClass([1, 2])
573
+ output = pad(x)
574
+ self.assertEqual(output.shape, (2, 10, 12, 3))
575
+
576
+ def test_all_3d_paddings_same_interface(self):
577
+ """Test that all 3D padding classes have the same interface."""
578
+ padding_classes = [
579
+ brainstate.nn.ReflectionPad3d,
580
+ brainstate.nn.ReplicationPad3d,
581
+ brainstate.nn.ZeroPad3d,
582
+ brainstate.nn.ConstantPad3d,
583
+ brainstate.nn.CircularPad3d
584
+ ]
585
+
586
+ x = jnp.ones((1, 4, 4, 4, 2))
587
+ for PadClass in padding_classes:
588
+ if PadClass == brainstate.nn.ConstantPad3d:
589
+ pad = PadClass([1, 2, 3], value=0)
590
+ else:
591
+ pad = PadClass([1, 2, 3])
592
+ output = pad(x)
593
+ self.assertEqual(output.shape, (1, 6, 8, 10, 2))
594
+
595
+ def test_in_size_out_size_consistency(self):
596
+ """Test that in_size and out_size are consistent with actual output."""
597
+ test_cases = [
598
+ (brainstate.nn.ReflectionPad1d(2), (10, 3)),
599
+ (brainstate.nn.ReplicationPad2d([1, 2]), (8, 8, 3)),
600
+ (brainstate.nn.ZeroPad3d(1), (4, 4, 4, 2)),
601
+ (brainstate.nn.ConstantPad1d([1, 3], value=0), (2, 10, 5)),
602
+ (brainstate.nn.CircularPad2d([2, 2]), (1, 6, 6, 1)),
603
+ ]
604
+
605
+ for pad, in_size in test_cases:
606
+ pad_with_size = type(pad)(
607
+ pad.padding[0][0] if hasattr(pad, 'padding') else 1,
608
+ in_size=in_size
609
+ )
610
+ x = jnp.ones(in_size)
611
+ output = pad_with_size(x)
612
+ self.assertEqual(output.shape, pad_with_size.out_size)
613
+
614
+ def test_gradient_flow(self):
615
+ """Test that gradients flow correctly through padding layers."""
616
+ def loss_fn(x, pad_layer):
617
+ output = pad_layer(x)
618
+ return jnp.sum(output ** 2)
619
+
620
+ x = jnp.ones((2, 4, 3))
621
+ pad = brainstate.nn.ReflectionPad1d(2)
622
+
623
+ grad_fn = jax.grad(loss_fn, argnums=0)
624
+ grad = grad_fn(x, pad)
625
+
626
+ # Gradient should be non-zero
627
+ self.assertGreater(jnp.sum(jnp.abs(grad)), 0)
628
+ # Gradient should have same shape as input
629
+ self.assertEqual(grad.shape, x.shape)
630
+
631
+ def test_jit_compilation(self):
632
+ """Test that padding layers work with JIT compilation."""
633
+ @jax.jit
634
+ def apply_padding(x):
635
+ pad = brainstate.nn.ZeroPad2d(2)
636
+ return pad(x)
637
+
638
+ x = jnp.ones((1, 4, 4, 3))
639
+ output = apply_padding(x)
640
+ self.assertEqual(output.shape, (1, 8, 8, 3))
641
+
642
+ def test_vmap_compatibility(self):
643
+ """Test that padding layers work with vmap."""
644
+ pad = brainstate.nn.CircularPad1d(1)
645
+
646
+ # Create batch of different inputs
647
+ x = jnp.arange(30).reshape(5, 6, 1)
648
+
649
+ # Apply padding with vmap
650
+ vmapped_pad = jax.vmap(pad.update)
651
+ output = vmapped_pad(x)
652
+
653
+ self.assertEqual(output.shape, (5, 8, 1))
654
+
655
+ # Check that each batch element is padded correctly
656
+ for i in range(5):
657
+ single_output = pad(x[i])
658
+ np.testing.assert_array_equal(output[i], single_output)
659
+
660
+
661
+ class TestEdgeCases(unittest.TestCase):
662
+ """Test edge cases and error conditions."""
663
+
664
+ def test_zero_padding(self):
665
+ """Test padding with size 0."""
666
+ pad = brainstate.nn.ZeroPad1d(0)
667
+ x = jnp.ones((3, 5, 2))
668
+ output = pad(x)
669
+ np.testing.assert_array_equal(output, x)
670
+
671
+ def test_large_padding(self):
672
+ """Test with very large padding."""
673
+ pad = brainstate.nn.ConstantPad1d(100, value=3.14)
674
+ x = jnp.ones((1, 2, 1))
675
+ output = pad(x)
676
+ self.assertEqual(output.shape, (1, 202, 1))
677
+
678
+ def test_reflection_pad_boundary_smaller_than_padding(self):
679
+ """Test reflection padding when padding is larger than input."""
680
+ # This might fail for reflection padding if input is too small
681
+ pad = brainstate.nn.ReflectionPad1d(3)
682
+ x = jnp.ones((2, 2)) # Length 2, padding 3
683
+ # Reflection requires input size > padding size
684
+ # This should work fine with JAX's pad implementation
685
+ output = pad(x)
686
+ self.assertEqual(output.shape, (8, 2))
687
+
688
+ def test_different_dtypes(self):
689
+ """Test padding with different data types."""
690
+ dtypes = [jnp.float32, jnp.int32]
691
+ pad = brainstate.nn.ZeroPad1d(1)
692
+
693
+ for dtype in dtypes:
694
+ x = jnp.ones((2, 3, 1), dtype=dtype)
695
+ output = pad(x)
696
+ # JAX may truncate float64 to float32 depending on config
697
+ if dtype == jnp.float64:
698
+ # Check if it's either float64 or float32 (truncated)
699
+ self.assertIn(output.dtype, [jnp.float32, jnp.float64])
700
+ else:
701
+ self.assertEqual(output.dtype, dtype)
702
+ self.assertEqual(output.shape, (2, 5, 1))
703
+
704
+ # Test float64 separately with proper handling
705
+ try:
706
+ x = jnp.ones((2, 3, 1), dtype=jnp.float64)
707
+ output = pad(x)
708
+ # Output dtype might be float32 if JAX_ENABLE_X64 is not set
709
+ self.assertIn(str(output.dtype), ['float32', 'float64'])
710
+ self.assertEqual(output.shape, (2, 5, 1))
711
+ except:
712
+ pass # Float64 not available
713
+
714
+ def test_empty_batch(self):
715
+ """Test padding with empty batch dimension."""
716
+ pad = brainstate.nn.CircularPad2d(1)
717
+ x = jnp.ones((0, 4, 4, 3))
718
+ output = pad(x)
719
+ self.assertEqual(output.shape, (0, 6, 6, 3))
720
+
721
+
722
+ if __name__ == '__main__':
723
+ unittest.main()