From 626800093688e2c041755dc0d55e60e6a68f6895 Mon Sep 17 00:00:00 2001 From: BachDEV Date: Wed, 1 Apr 2026 02:06:46 +0700 Subject: [PATCH 1/2] refactor: gpu configuration crash on cpu-only systems The code attempts to process the `gpus` string by splitting it, but `gpus` can become `None` if `torch.cuda.is_available()` returns `False`. This will lead to an `AttributeError` when `gpus.split(',')` is called, causing the application to crash on systems without a GPU or where CUDA is not detected. This makes the application unusable in CPU-only environments. Affected files: main.py Signed-off-by: BachDEV <1437214+bachdev@users.noreply.github.com> --- PW_FT_classification/main.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/PW_FT_classification/main.py b/PW_FT_classification/main.py index ec66333a1..174afd6db 100644 --- a/PW_FT_classification/main.py +++ b/PW_FT_classification/main.py @@ -55,8 +55,12 @@ def main( """ # GPU configuration: set up GPUs based on availability and user specification - gpus = gpus if torch.cuda.is_available() else None - gpus = [int(i) for i in gpus.split(',')] + if torch.cuda.is_available(): + gpus = [int(i) for i in gpus.split(',')] + else: + # If no CUDA devices are available, set gpus to None to indicate CPU usage + # PyTorch Lightning Trainer will default to CPU if devices is None + gpus = None # Environment variable setup for numpy multi-threading. It is important to avoid cpu and ram issues. os.environ["OMP_NUM_THREADS"] = str(np_threads) From 0c242344b8a132c02d33ef0c20726d4749a9b899 Mon Sep 17 00:00:00 2001 From: BachDEV Date: Wed, 1 Apr 2026 16:35:31 +0700 Subject: [PATCH 2/2] Update PW_FT_classification/main.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- PW_FT_classification/main.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/PW_FT_classification/main.py b/PW_FT_classification/main.py index 174afd6db..4caa2236e 100644 --- a/PW_FT_classification/main.py +++ b/PW_FT_classification/main.py @@ -56,7 +56,22 @@ def main( # GPU configuration: set up GPUs based on availability and user specification if torch.cuda.is_available(): - gpus = [int(i) for i in gpus.split(',')] + # Sanitize and validate the GPU list provided via CLI + if gpus is None: + parsed_gpus = None + else: + tokens = [token.strip() for token in str(gpus).split(",") if token.strip()] + if not tokens: + # Empty or whitespace-only input: fall back to CPU + parsed_gpus = None + else: + try: + parsed_gpus = [int(token) for token in tokens] + except ValueError as exc: + raise typer.BadParameter( + f"Invalid GPU list '{gpus}'. Expected a comma-separated list of integers, e.g. '0,1,2'." + ) from exc + gpus = parsed_gpus else: # If no CUDA devices are available, set gpus to None to indicate CPU usage # PyTorch Lightning Trainer will default to CPU if devices is None