Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilizing subprocess for nnUNet training. #7576

Merged
merged 13 commits into from
Apr 1, 2024
53 changes: 20 additions & 33 deletions monai/apps/nnunet/nnunetv2_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from monai.apps.nnunet.utils import analyze_data, create_new_data_copy, create_new_dataset_json
from monai.bundle import ConfigParser
from monai.utils import ensure_tuple, optional_import
from monai.utils.misc import run_cmd

load_pickle, _ = optional_import("batchgenerators.utilities.file_and_folder_operations", name="load_pickle")
join, _ = optional_import("batchgenerators.utilities.file_and_folder_operations", name="join")
Expand Down Expand Up @@ -522,38 +523,32 @@ def train_single_model(self, config: Any, fold: int, gpu_id: tuple | list | int
kwargs.pop("export_validation_probabilities")
logger.warning("please specify the `export_validation_probabilities` in the __init__ of `nnUNetV2Runner`.")

cmd = self.train_single_model_command(config, fold, gpu_id, kwargs)
run_cmd(cmd, shell=True)
mingxin-zheng marked this conversation as resolved.
Show resolved Hide resolved

def train_single_model_command(self, config, fold, gpu_id, kwargs):
if isinstance(gpu_id, (tuple, list)):
if len(gpu_id) > 1:
gpu_ids_str = ""
for _i in range(len(gpu_id)):
gpu_ids_str += f"{gpu_id[_i]},"
os.environ["CUDA_VISIBLE_DEVICES"] = gpu_ids_str[:-1]
device_setting = f"CUDA_VISIBLE_DEVICES={gpu_ids_str[:-1]}"
else:
os.environ["CUDA_VISIBLE_DEVICES"] = f"{gpu_id[0]}"
device_setting = f"CUDA_VISIBLE_DEVICES={gpu_id[0]}"
else:
os.environ["CUDA_VISIBLE_DEVICES"] = f"{gpu_id}"

from nnunetv2.run.run_training import run_training
device_setting = f"CUDA_VISIBLE_DEVICES={gpu_id}"
num_gpus = 1 if isinstance(gpu_id, int) or len(gpu_id) == 1 else len(gpu_id)

if isinstance(gpu_id, int) or len(gpu_id) == 1:
run_training(
dataset_name_or_id=self.dataset_name_or_id,
configuration=config,
fold=fold,
trainer_class_name=self.trainer_class_name,
export_validation_probabilities=self.export_validation_probabilities,
**kwargs,
)
else:
run_training(
dataset_name_or_id=self.dataset_name_or_id,
configuration=config,
fold=fold,
num_gpus=len(gpu_id),
trainer_class_name=self.trainer_class_name,
export_validation_probabilities=self.export_validation_probabilities,
**kwargs,
)
cmd = (
f"{device_setting} nnUNetv2_train "
+ f"{self.dataset_name_or_id} {config} {fold} "
+ f"-tr {self.trainer_class_name} -num_gpus {num_gpus}"
)
if self.export_validation_probabilities:
cmd += " --npz"
for _key, _value in kwargs.items():
cmd += f" --{_key} {_value}"
dongyang0122 marked this conversation as resolved.
Show resolved Hide resolved
return cmd

def train(
self,
Expand Down Expand Up @@ -637,15 +632,7 @@ def train_parallel_cmd(
if _config in ensure_tuple(configs):
for _i in range(self.num_folds):
the_device = gpu_id_for_all[_index % n_devices] # type: ignore
cmd = (
"python -m monai.apps.nnunet nnUNetV2Runner train_single_model "
+ f"--input_config '{self.input_config_or_dict}' --work_dir '{self.work_dir}' "
+ f"--config '{_config}' --fold {_i} --gpu_id {the_device} "
+ f"--trainer_class_name {self.trainer_class_name} "
+ f"--export_validation_probabilities {self.export_validation_probabilities}"
)
for _key, _value in kwargs.items():
cmd += f" --{_key} {_value}"
cmd = self.train_single_model_command(_config, _i, the_device, kwargs)
all_cmds[-1][the_device].append(cmd)
_index += 1
return all_cmds
Expand Down
3 changes: 2 additions & 1 deletion tests/test_set_visible_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import os
import unittest

from tests.utils import skip_if_no_cuda
from tests.utils import SkipIfAtLeastPyTorchVersion, skip_if_no_cuda


class TestVisibleDevices(unittest.TestCase):
Expand All @@ -25,6 +25,7 @@ def run_process_and_get_exit_code(code_to_execute):
return int(bin(value).replace("0b", "").rjust(16, "0")[:8], 2)

@skip_if_no_cuda
@SkipIfAtLeastPyTorchVersion((2, 2, 1))
def test_visible_devices(self):
num_gpus_before = self.run_process_and_get_exit_code(
'python -c "import os; import torch; '
Expand Down
Loading