Repository: Project-MONAI/MONAI
File: monai/apps/nnunet/nnunetv2_runner.py (Python)

Code before:
                        files (in addition to predicted segmentations). Needed for finding the best ensemble.
                        Default: False.
                    - continue_training: continue training from latest checkpoint. Default: False.
                    - only_run_validation: True to run the validation only. Requires training to have finished.
                        Default: False.
                    - disable_checkpointing: True to disable checkpointing. Ideal for testing things out and you
                        don't want to flood your hard drive with checkpoints. Default: False.
        """
        if isinstance(gpu_id, str):
            gpu_id = tuple(map(int, gpu_id.replace('"', "").split(",")))

        if isinstance(gpu_id, tuple):
            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]
            else:
                os.environ["CUDA_VISIBLE_DEVICES"] = f"{gpu_id[0]}"
        else:
            os.environ["CUDA_VISIBLE_DEVICES"] = f"{gpu_id}"

        from nnunetv2.run.run_training import run_training

        if isinstance(gpu_id, int):
            run_training(dataset_name_or_id=self.dataset_name_or_id, configuration=config, fold=fold, **kwargs)
        else:
            run_training(
                dataset_name_or_id=self.dataset_name_or_id,
                configuration=config,
                fold=fold,
                num_gpus=len(gpu_id),
                **kwargs,
            )

    def train(
        self,
        configs: tuple | str = (M.N_3D_FULLRES, M.N_2D, M.N_3D_LOWRES, M.N_3D_CASCADE_FULLRES),
        device_ids: tuple | None = None,
        **kwargs: Any,
    ) -> None:
        """
        Run the training for all the models specified by the configurations.
        Note: to set the number of GPUs to use, use ``devices_ids`` instead of the `CUDA_VISIBLE_DEVICES`
        environment variable.

        Args:
            configs: configurations that should be trained.
                Default: ("2d", "3d_fullres", "3d_lowres", "3d_cascade_fullres").
            device_ids: a tuple/list of GPU device IDs to use for the training. Default: None (all available GPUs).
            kwargs: this optional parameter allows you to specify additional arguments defined in the

Review comment (suggestion):
Does the following work to replace L510-513?
```
os.environ["CUDA_VISIBLE_DEVICES"] = ",".join([str(x) for x in gpu_id])
```

Code after:
        from nnunetv2.run.run_training import run_training
            kwargs: this optional parameter allows you to specify additional arguments in
                ``nnunetv2.run.run_training.run_training``. Currently supported args are
                    - trainer_class_name: name of the custom trainer class. Default: "nnUNetTrainer".
                    - plans_identifier: custom plans identifier. Default: "nnUNetPlans".
                    - pretrained_weights: path to nnU-Net checkpoint file to be used as pretrained model. Will only be
                        used when actually training. Beta. Use with caution. Default: False.
                    - use_compressed_data: True to use compressed data for training. Reading compressed data is much
                        more CPU and (potentially) RAM intensive and should only be used if you know what you are
                        doing. Default: False.
                    - continue_training: continue training from latest checkpoint. Default: False.
                    - only_run_validation: True to run the validation only. Requires training to have finished.
                        Default: False.
                    - disable_checkpointing: True to disable checkpointing. Ideal for testing things out and you
                        don't want to flood your hard drive with checkpoints. Default: False.
        """
        if "num_gpus" in kwargs:
            kwargs.pop("num_gpus")
            logger.warning("please use gpu_id to set the GPUs to use")

        if isinstance(gpu_id, tuple) or isinstance(gpu_id, 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]
            else:
                os.environ["CUDA_VISIBLE_DEVICES"] = f"{gpu_id[0]}"
        else:
            os.environ["CUDA_VISIBLE_DEVICES"] = f"{gpu_id}"

        from nnunetv2.run.run_training import run_training

        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,