From 555c20cdf01cd24337723482fd8dea0f88947e2e Mon Sep 17 00:00:00 2001 From: Balamurali Date: Thu, 15 Aug 2024 01:01:03 -0700 Subject: [PATCH] NACLLoss memory management (#8020) Fixes # . ### Description Calling contiguous after applying the permute option to work with view operation in apply_filter (https://github.com/Project-MONAI/MONAI/blob/59a7211070538586369afd4a01eca0a7fe2e742e/monai/networks/layers/simplelayers.py#L293). ### Types of changes - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Balamurali Signed-off-by: bala93 Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> Co-authored-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/losses/nacl_loss.py | 4 ++-- tests/test_nacl_loss.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/monai/losses/nacl_loss.py b/monai/losses/nacl_loss.py index 3303e89bce..27a712d308 100644 --- a/monai/losses/nacl_loss.py +++ b/monai/losses/nacl_loss.py @@ -95,11 +95,11 @@ def get_constr_target(self, mask: torch.Tensor) -> torch.Tensor: rmask: torch.Tensor if self.dim == 2: - oh_labels = F.one_hot(mask.to(torch.int64), num_classes=self.nc).contiguous().permute(0, 3, 1, 2).float() + oh_labels = F.one_hot(mask.to(torch.int64), num_classes=self.nc).permute(0, 3, 1, 2).contiguous().float() rmask = self.svls_layer(oh_labels) if self.dim == 3: - oh_labels = F.one_hot(mask.to(torch.int64), num_classes=self.nc).contiguous().permute(0, 4, 1, 2, 3).float() + oh_labels = F.one_hot(mask.to(torch.int64), num_classes=self.nc).permute(0, 4, 1, 2, 3).contiguous().float() rmask = self.svls_layer(oh_labels) return rmask diff --git a/tests/test_nacl_loss.py b/tests/test_nacl_loss.py index 51ec275cf4..704bbdb9b1 100644 --- a/tests/test_nacl_loss.py +++ b/tests/test_nacl_loss.py @@ -47,6 +47,7 @@ TEST_CASES = [ [{"classes": 3, "dim": 2}, {"inputs": inputs, "targets": targets}, 1.1442], + [{"classes": 3, "dim": 2}, {"inputs": inputs.repeat(4, 1, 1, 1), "targets": targets.repeat(4, 1, 1)}, 1.1442], [{"classes": 3, "dim": 2, "kernel_ops": "gaussian"}, {"inputs": inputs, "targets": targets}, 1.1433], [{"classes": 3, "dim": 2, "kernel_ops": "gaussian", "sigma": 0.5}, {"inputs": inputs, "targets": targets}, 1.1469], [{"classes": 3, "dim": 2, "distance_type": "l2"}, {"inputs": inputs, "targets": targets}, 1.1269],