Repository: Project-MONAI/MONAI
File: tests/networks/nets/test_checkpointunet.py (Python)

Code before:
            y_plain = net_plain(x)

        # checkpointing should not change outputs in eval mode
        self.assertTrue(torch.allclose(y_ckpt, y_plain, atol=1e-6, rtol=1e-5))

    def test_checkpointing_activates_training(self):
        """Ensure checkpointing triggers recomputation under training and gradients propagate."""
        params = dict(
            spatial_dims=2, in_channels=1, out_channels=1, channels=(8, 16, 32), strides=(2, 2), num_res_units=1
        )

        net = CheckpointUNet(**params).to(device)
        net.train()

        x = torch.randn(2, 1, 32, 32, device=device, requires_grad=True)
        y = net(x)
        loss = y.mean()
        loss.backward()

        # gradient flow check
        grad_norm = sum(p.grad.abs().sum() for p in net.parameters() if p.grad is not None)
        self.assertGreater(grad_norm.item(), 0.0)

        # checkpointing should reduce activation memory use; we can't directly assert memory savings
        # but we can confirm no runtime errors and gradients propagate correctly
        self.assertIsNotNone(grad_norm)


if __name__ == "__main__":
    unittest.main()


Review comment (suggestion):
This check will never be reached when `grad_norm` is None because the previous one would raise an exception when evaluating `grad_norm.item()`.

Code after:
    (16, 4, 32, 64, 48),
    (16, 3, 32, 64, 48),
]

CASES = [TEST_CASE_0, TEST_CASE_1, TEST_CASE_2, TEST_CASE_3, TEST_CASE_4, TEST_CASE_5, TEST_CASE_6]


class TestCheckpointUNet(unittest.TestCase):
    @parameterized.expand(CASES)
    def test_shape(self, input_param, input_shape, expected_shape):
        """Validate CheckpointUNet output shapes across configurations.

        Args:
            input_param: Dictionary of UNet constructor arguments.
            input_shape: Tuple specifying input tensor dimensions.
            expected_shape: Tuple specifying expected output tensor dimensions.
        """
        net = CheckpointUNet(**input_param).to(device)
        with eval_mode(net):
            result = net.forward(torch.randn(input_shape).to(device))
            self.assertEqual(result.shape, expected_shape)

    def test_checkpointing_equivalence_eval(self):
        """Confirm eval parity when checkpointing is inactive."""
        params = dict(
            spatial_dims=2, in_channels=1, out_channels=2, channels=(8, 16, 32), strides=(2, 2), num_res_units=1
        )

        x = torch.randn(2, 1, 32, 32, device=device)

        torch.manual_seed(42)
        net_plain = UNet(**params).to(device)

        torch.manual_seed(42)
        net_ckpt = CheckpointUNet(**params).to(device)

        # Both in eval mode disables checkpointing logic
        with eval_mode(net_ckpt), eval_mode(net_plain):
            y_ckpt = net_ckpt(x)
            y_plain = net_plain(x)

        # Check shape equality
        self.assertEqual(y_ckpt.shape, y_plain.shape)

        # Check numerical equivalence
        self.assertTrue(
            torch.allclose(y_ckpt, y_plain, atol=1e-6, rtol=1e-5),
            f"Eval-mode outputs differ: max abs diff={torch.max(torch.abs(y_ckpt - y_plain)).item():.2e}",
        )

    def test_checkpointing_activates_training(self):