Skip to content

Commit 19d7f35

Browse files
committed
Make get_norm_layer repr test tolerant of PyTorch bias= field
PyTorch >= 2.13 adds an optional 'bias=' token to GroupNorm/InstanceNorm __repr__, breaking the exact-string match in test_norm_layer. Normalize the repr by stripping the bias= field so the test passes on PyTorch versions with or without it (backward- and forward-compatible). Signed-off-by: Hans Johnson <hans-johnson@uiowa.edu>
1 parent e33941c commit 19d7f35

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

tests/networks/layers/test_get_layers.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,26 @@
1111

1212
from __future__ import annotations
1313

14+
import re
1415
import unittest
1516

1617
from parameterized import parameterized
1718

1819
from monai.networks.layers import get_act_layer, get_dropout_layer, get_norm_layer
1920

21+
22+
def _strip_bias_field(text: str) -> str:
23+
"""Strip the optional PyTorch >= 2.13 ``, bias=True|False`` repr fragment.
24+
25+
Args:
26+
text: Layer string representation to normalize.
27+
28+
Returns:
29+
The representation with any ``, bias=True|False`` removed.
30+
"""
31+
return re.sub(r",\s*bias=(?:True|False)", "", text)
32+
33+
2034
TEST_CASE_NORM = [
2135
[{"name": ("group", {"num_groups": 1})}, "GroupNorm(1, 1, eps=1e-05, affine=True)"],
2236
[
@@ -41,7 +55,7 @@ class TestGetLayers(unittest.TestCase):
4155
@parameterized.expand(TEST_CASE_NORM)
4256
def test_norm_layer(self, input_param, expected):
4357
layer = get_norm_layer(**input_param)
44-
self.assertEqual(f"{layer}", expected)
58+
self.assertEqual(_strip_bias_field(f"{layer}"), _strip_bias_field(expected))
4559

4660
@parameterized.expand(TEST_CASE_ACT)
4761
def test_act_layer(self, input_param, expected):

0 commit comments

Comments
 (0)