Skip to content

Commit 6f8e63f

Browse files
authored
6697 run default command formatting (#6698)
Fixes #6697 ### 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: Wenqi Li <wenqil@nvidia.com>
1 parent c6c7ba9 commit 6f8e63f

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

monai/apps/auto3dseg/auto_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def set_device_info(
525525
self.device_setting["MN_START_METHOD"] = mn_start_method
526526

527527
if cmd_prefix is None:
528-
cmd_prefix = os.environ.get("CMD_PREFIX")
528+
cmd_prefix = os.environ.get("CMD_PREFIX", "")
529529
self.device_setting["CMD_PREFIX"] = cmd_prefix
530530

531531
if cmd_prefix is not None:

monai/apps/auto3dseg/bundle_gen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def _run_cmd(self, cmd: str, devices_info: str = "") -> subprocess.CompletedProc
251251
cmd, nnodes=1, nproc_per_node=self.device_setting["n_devices"], env=ps_environ, check=True
252252
)
253253
else:
254-
return run_cmd(cmd.split(), env=ps_environ, check=True)
254+
return run_cmd(cmd.split(), run_cmd_verbose=True, env=ps_environ, check=True)
255255

256256
def train(
257257
self, train_params: None | dict = None, device_setting: None | dict = None

monai/apps/auto3dseg/ensemble_builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ def __init__(
453453
"n_devices": torch.cuda.device_count(),
454454
"NUM_NODES": int(os.environ.get("NUM_NODES", 1)),
455455
"MN_START_METHOD": os.environ.get("MN_START_METHOD", "bcprun"),
456-
"CMD_PREFIX": os.environ.get("CMD_PREFIX"), # type: ignore
456+
"CMD_PREFIX": os.environ.get("CMD_PREFIX", ""), # type: ignore
457457
}
458458

459459
def set_ensemble_method(self, ensemble_method_name: str = "AlgoEnsembleBestByFold", **kwargs: Any) -> None:

monai/auto3dseg/utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -390,14 +390,14 @@ def list_to_python_fire_arg_str(args: list) -> str:
390390

391391

392392
def check_and_set_optional_args(params: dict) -> str:
393-
""" """
393+
"""convert `params` into '--key_1=value_1 --key_2=value_2 ...'"""
394394
cmd_mod_opt = ""
395395
for k, v in params.items():
396396
if isinstance(v, dict):
397397
raise ValueError("Nested dict is not supported.")
398398
elif isinstance(v, list):
399399
v = list_to_python_fire_arg_str(v)
400-
cmd_mod_opt += f" --{k} {str(v)}"
400+
cmd_mod_opt += f" --{k}={v}"
401401
return cmd_mod_opt
402402

403403

@@ -422,7 +422,8 @@ def _prepare_cmd_default(cmd: str, cmd_prefix: str | None = None, **kwargs: Any)
422422
"""
423423
params = kwargs.copy()
424424

425-
cmd_prefix = cmd_prefix or "python"
425+
if not cmd_prefix or "None" in cmd_prefix: # defaulting to 'python'
426+
cmd_prefix = "python"
426427

427428
if not cmd_prefix.endswith(" "):
428429
cmd_prefix += " " # ensure a space after the command prefix so that the script can be appended
@@ -498,7 +499,7 @@ def _run_cmd_torchrun(cmd: str, **kwargs: Any) -> subprocess.CompletedProcess:
498499
raise ValueError(f"Missing required argument {arg} for torchrun.")
499500
torchrun_list += [f"--{arg}", str(params.pop(arg))]
500501
torchrun_list += cmd_list
501-
return run_cmd(torchrun_list, **params)
502+
return run_cmd(torchrun_list, run_cmd_verbose=True, **params)
502503

503504

504505
def _run_cmd_bcprun(cmd: str, **kwargs: Any) -> subprocess.CompletedProcess:
@@ -520,4 +521,4 @@ def _run_cmd_bcprun(cmd: str, **kwargs: Any) -> subprocess.CompletedProcess:
520521
raise ValueError(f"Missing required argument {arg} for bcprun.")
521522
cmd_list += [f"-{arg}", str(params.pop(arg))]
522523
cmd_list.extend(["-c", cmd])
523-
return run_cmd(cmd_list, **params)
524+
return run_cmd(cmd_list, run_cmd_verbose=True, **params)

monai/utils/misc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,10 @@ def run_cmd(cmd_list: list[str], **kwargs: Any) -> subprocess.CompletedProcess:
841841
debug = MONAIEnvVars.debug()
842842
kwargs["capture_output"] = kwargs.get("capture_output", debug)
843843

844+
if kwargs.pop("run_cmd_verbose", False):
845+
import monai
846+
847+
monai.apps.utils.get_logger("run_cmd").info(f"{cmd_list}")
844848
try:
845849
return subprocess.run(cmd_list, **kwargs)
846850
except subprocess.CalledProcessError as e:

0 commit comments

Comments
 (0)