Skip to content

fix: load configs with yaml.safe_load and validate the GPU list - #7

Open
Zhongqi Miao (zhmiao) wants to merge 2 commits into
microsoft:mainfrom
zhmiao:fix/safe-yaml-and-cpu-gpus
Open

Zhongqi Miao (zhmiao) wants to merge 2 commits into
microsoft:mainfrom
zhmiao:fix/safe-yaml-and-cpu-gpus

Conversation

@zhmiao

@zhmiao Zhongqi Miao (zhmiao) commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Why

microsoft/Biodiversity kept its own copy of this training code in PW_FT_classification/. Two fixes landed there in May 2026, but they never reached this repo. The hub copy is about to be deleted, because the hub is becoming a catalogue and navigation page only. This PR moves both fixes here so they are not lost.

Original fixes by BachDEV (@bachdev):

What

main.py only. Items 1 and 2 are identical to the hub version; item 3 fixes a crash the hub version also has.

  1. Config loading: yaml.load(f, Loader=yaml.FullLoader) becomes yaml.safe_load(f). A config file can no longer construct arbitrary Python objects.

  2. --gpus parsing:

    • The list is parsed only when CUDA is available. Previously a CPU-only machine set gpus = None and then called None.split(',').
    • Empty or whitespace-only input falls back to CPU.
    • A non-integer list raises typer.BadParameter with a clear message instead of a ValueError traceback.
  3. Trainer accelerator: the Trainer used to hardcode accelerator='gpu'. When gpus is None (no CUDA, or an empty list), it now uses accelerator='cpu', devices='auto'. Without this, setting gpus = None only moved the crash from the parsing step to pl.Trainer(...).

How it was tested

main() was run through pl.Trainer(...) construction, with the dataset, learner, and cropping step stubbed and Trainer.fit replaced by a probe that prints the selected accelerator and device IDs.

Case main first commit (b5773c9) this PR (54c60c6)
CPU, --gpus 0 AttributeError: 'NoneType' object has no attribute 'split' MisconfigurationException: No supported gpu backend found! CPU accelerator
CPU, --gpus "" AttributeError MisconfigurationException CPU accelerator
CUDA, --gpus 0 GPU [0] GPU [0] GPU [0]
CUDA, --gpus " 0 , 1 " ValueError GPU [0, 1] GPU [0, 1]
CUDA, --gpus "" / " " ValueError: invalid literal for int() TypeError: Device IDs ... you passed None CPU accelerator
CUDA, --gpus a,b ValueError: invalid literal for int() BadParameter: Invalid GPU list 'a,b'. ... same BadParameter

Other checks:

  • configs/config.yaml gives the same dict under safe_load and FullLoader.
  • python main.py --help exits 0.

Note: separate install problem (not fixed here)

A fresh pip install -r requirements.txt cannot import PytorchWildlife 1.3.1, its final release:

  • It imports soundfile and librosa at package load but does not declare them.
  • Its yolov5 dependency imports pkg_resources, which setuptools removed in 82.0.0.

I will open a separate PR for that. It needs a setuptools<82 pin, which is a trade-off reviewers should decide on its own.

Port two fixes that landed in microsoft/Biodiversity's copy of this code
(PW_FT_classification, PRs #628 and #629) before that copy is removed:

- Load the training config with yaml.safe_load instead of
  yaml.load(Loader=FullLoader), so a config file cannot construct
  arbitrary Python objects.
- Parse --gpus only when CUDA is available. On CPU-only machines the old
  code called None.split(',') and crashed. Empty input now falls back to
  CPU, and a non-integer list raises a typer.BadParameter error.

Original fixes by @bachdev in microsoft/Biodiversity#628 and #629.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: cf871bd7-3efa-4dad-8dd6-c61e46669a42
PR microsoft#7 set gpus=None for CPU-only machines and empty --gpus input, but the
Trainer still hardcoded accelerator='gpu' and passed devices=None:
- CPU-only: MisconfigurationException: No supported gpu backend found!
- CUDA + --gpus '': TypeError: Device IDs ... you passed None
Use accelerator='cpu', devices='auto' when gpus is None.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: b452684d-9b9b-4a61-9af9-28bddaabcf14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant