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.
Files changed (115) hide show
  1. brainstate/__init__.py +167 -169
  2. brainstate/_compatible_import.py +340 -340
  3. brainstate/_compatible_import_test.py +681 -681
  4. brainstate/_deprecation.py +210 -210
  5. brainstate/_deprecation_test.py +2297 -2319
  6. brainstate/_error.py +45 -45
  7. brainstate/_state.py +2157 -1652
  8. brainstate/_state_test.py +1129 -52
  9. brainstate/_utils.py +47 -47
  10. brainstate/environ.py +1495 -1495
  11. brainstate/environ_test.py +1223 -1223
  12. brainstate/graph/__init__.py +22 -22
  13. brainstate/graph/_node.py +240 -240
  14. brainstate/graph/_node_test.py +589 -589
  15. brainstate/graph/_operation.py +1620 -1624
  16. brainstate/graph/_operation_test.py +1147 -1147
  17. brainstate/mixin.py +1447 -1433
  18. brainstate/mixin_test.py +1017 -1017
  19. brainstate/nn/__init__.py +146 -137
  20. brainstate/nn/_activations.py +1100 -1100
  21. brainstate/nn/_activations_test.py +354 -354
  22. brainstate/nn/_collective_ops.py +635 -633
  23. brainstate/nn/_collective_ops_test.py +774 -774
  24. brainstate/nn/_common.py +226 -226
  25. brainstate/nn/_common_test.py +134 -154
  26. brainstate/nn/_conv.py +2010 -2010
  27. brainstate/nn/_conv_test.py +849 -849
  28. brainstate/nn/_delay.py +575 -575
  29. brainstate/nn/_delay_test.py +243 -243
  30. brainstate/nn/_dropout.py +618 -618
  31. brainstate/nn/_dropout_test.py +480 -477
  32. brainstate/nn/_dynamics.py +870 -1267
  33. brainstate/nn/_dynamics_test.py +53 -67
  34. brainstate/nn/_elementwise.py +1298 -1298
  35. brainstate/nn/_elementwise_test.py +829 -829
  36. brainstate/nn/_embedding.py +408 -408
  37. brainstate/nn/_embedding_test.py +156 -156
  38. brainstate/nn/_event_fixedprob.py +233 -233
  39. brainstate/nn/_event_fixedprob_test.py +115 -115
  40. brainstate/nn/_event_linear.py +83 -83
  41. brainstate/nn/_event_linear_test.py +121 -121
  42. brainstate/nn/_exp_euler.py +254 -254
  43. brainstate/nn/_exp_euler_test.py +377 -377
  44. brainstate/nn/_linear.py +744 -744
  45. brainstate/nn/_linear_test.py +475 -475
  46. brainstate/nn/_metrics.py +1070 -1070
  47. brainstate/nn/_metrics_test.py +611 -611
  48. brainstate/nn/_module.py +391 -384
  49. brainstate/nn/_module_test.py +40 -40
  50. brainstate/nn/_normalizations.py +1334 -1334
  51. brainstate/nn/_normalizations_test.py +699 -699
  52. brainstate/nn/_paddings.py +1020 -1020
  53. brainstate/nn/_paddings_test.py +722 -722
  54. brainstate/nn/_poolings.py +2239 -2239
  55. brainstate/nn/_poolings_test.py +952 -952
  56. brainstate/nn/_rnns.py +946 -946
  57. brainstate/nn/_rnns_test.py +592 -592
  58. brainstate/nn/_utils.py +216 -216
  59. brainstate/nn/_utils_test.py +401 -401
  60. brainstate/nn/init.py +809 -809
  61. brainstate/nn/init_test.py +180 -180
  62. brainstate/random/__init__.py +270 -270
  63. brainstate/random/{_rand_funs.py → _fun.py} +3938 -3938
  64. brainstate/random/{_rand_funs_test.py → _fun_test.py} +638 -640
  65. brainstate/random/_impl.py +672 -0
  66. brainstate/random/{_rand_seed.py → _seed.py} +675 -675
  67. brainstate/random/{_rand_seed_test.py → _seed_test.py} +48 -48
  68. brainstate/random/{_rand_state.py → _state.py} +1320 -1617
  69. brainstate/random/{_rand_state_test.py → _state_test.py} +551 -551
  70. brainstate/transform/__init__.py +56 -59
  71. brainstate/transform/_ad_checkpoint.py +176 -176
  72. brainstate/transform/_ad_checkpoint_test.py +49 -49
  73. brainstate/transform/_autograd.py +1025 -1025
  74. brainstate/transform/_autograd_test.py +1289 -1289
  75. brainstate/transform/_conditions.py +316 -316
  76. brainstate/transform/_conditions_test.py +220 -220
  77. brainstate/transform/_error_if.py +94 -94
  78. brainstate/transform/_error_if_test.py +52 -52
  79. brainstate/transform/_find_state.py +200 -0
  80. brainstate/transform/_find_state_test.py +84 -0
  81. brainstate/transform/_jit.py +399 -399
  82. brainstate/transform/_jit_test.py +143 -143
  83. brainstate/transform/_loop_collect_return.py +675 -675
  84. brainstate/transform/_loop_collect_return_test.py +58 -58
  85. brainstate/transform/_loop_no_collection.py +283 -283
  86. brainstate/transform/_loop_no_collection_test.py +50 -50
  87. brainstate/transform/_make_jaxpr.py +2176 -2016
  88. brainstate/transform/_make_jaxpr_test.py +1634 -1510
  89. brainstate/transform/_mapping.py +607 -529
  90. brainstate/transform/_mapping_test.py +104 -194
  91. brainstate/transform/_progress_bar.py +255 -255
  92. brainstate/transform/_unvmap.py +256 -256
  93. brainstate/transform/_util.py +286 -286
  94. brainstate/typing.py +837 -837
  95. brainstate/typing_test.py +780 -780
  96. brainstate/util/__init__.py +27 -27
  97. brainstate/util/_others.py +1024 -1024
  98. brainstate/util/_others_test.py +962 -962
  99. brainstate/util/_pretty_pytree.py +1301 -1301
  100. brainstate/util/_pretty_pytree_test.py +675 -675
  101. brainstate/util/_pretty_repr.py +462 -462
  102. brainstate/util/_pretty_repr_test.py +696 -696
  103. brainstate/util/filter.py +945 -945
  104. brainstate/util/filter_test.py +911 -911
  105. brainstate/util/struct.py +910 -910
  106. brainstate/util/struct_test.py +602 -602
  107. {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/METADATA +108 -108
  108. brainstate-0.2.2.dist-info/RECORD +111 -0
  109. {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/licenses/LICENSE +202 -202
  110. brainstate/transform/_eval_shape.py +0 -145
  111. brainstate/transform/_eval_shape_test.py +0 -38
  112. brainstate/transform/_random.py +0 -171
  113. brainstate-0.2.1.dist-info/RECORD +0 -111
  114. {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/WHEEL +0 -0
  115. {brainstate-0.2.1.dist-info → brainstate-0.2.2.dist-info}/top_level.txt +0 -0
@@ -1,723 +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__':
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
723
  unittest.main()