Skip to content

Commit abe2e31

Browse files
Fix CUDA_VISIBLE_DEVICES setting ignored (#7408)
Fixes #7407 ### Description Move `optional import cucim` inside the function to avoid using all GPUs. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [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: YunLiu <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent facf176 commit abe2e31

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

monai/metrics/utils.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@
3838
binary_erosion, _ = optional_import("scipy.ndimage.morphology", name="binary_erosion")
3939
distance_transform_edt, _ = optional_import("scipy.ndimage.morphology", name="distance_transform_edt")
4040
distance_transform_cdt, _ = optional_import("scipy.ndimage.morphology", name="distance_transform_cdt")
41-
cucim_binary_erosion, has_cucim_binary_erosion = optional_import("cucim.skimage.morphology", name="binary_erosion")
42-
cucim_distance_transform_edt, has_cucim_distance_transform_edt = optional_import(
43-
"cucim.core.operations.morphology", name="distance_transform_edt"
44-
)
4541

4642
__all__ = [
4743
"ignore_background",
@@ -179,6 +175,8 @@ def get_mask_edges(
179175
always_return_as_numpy: whether to a numpy array regardless of the input type.
180176
If False, return the same type as inputs.
181177
"""
178+
# move in the funciton to avoid using all the GPUs
179+
cucim_binary_erosion, has_cucim_binary_erosion = optional_import("cucim.skimage.morphology", name="binary_erosion")
182180
if seg_pred.shape != seg_gt.shape:
183181
raise ValueError(f"seg_pred and seg_gt should have same shapes, got {seg_pred.shape} and {seg_gt.shape}.")
184182
converter: Any

tests/test_set_visible_devices.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ def test_visible_devices(self):
3535
)
3636
self.assertEqual(num_gpus_before, num_gpus_after)
3737

38+
# test import monai won't affect setting CUDA_VISIBLE_DEVICES
39+
num_gpus_after_monai = self.run_process_and_get_exit_code(
40+
'python -c "import os; import torch; import monai; '
41+
+ "os.environ['CUDA_VISIBLE_DEVICES'] = '0'; exit(torch.cuda.device_count())\""
42+
)
43+
self.assertEqual(num_gpus_after_monai, 1)
44+
3845

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

0 commit comments

Comments
 (0)