Repository navigation
fix: load configs with yaml.safe_load and validate the GPU list - #7
Open
Zhongqi Miao (zhmiao) wants to merge 2 commits into
Open
Zhongqi Miao (zhmiao) wants to merge 2 commits into
Zhongqi Miao (zhmiao) wants to merge 2 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
microsoft/Biodiversitykept its own copy of this training code inPW_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):
yaml.FullLoaderBiodiversity#628: insecure YAML deserialization throughyaml.FullLoader.What
main.pyonly. Items 1 and 2 are identical to the hub version; item 3 fixes a crash the hub version also has.Config loading:
yaml.load(f, Loader=yaml.FullLoader)becomesyaml.safe_load(f). A config file can no longer construct arbitrary Python objects.--gpusparsing:gpus = Noneand then calledNone.split(',').typer.BadParameterwith a clear message instead of aValueErrortraceback.Trainer accelerator: the Trainer used to hardcode
accelerator='gpu'. WhengpusisNone(no CUDA, or an empty list), it now usesaccelerator='cpu',devices='auto'. Without this, settinggpus = Noneonly moved the crash from the parsing step topl.Trainer(...).How it was tested
main()was run throughpl.Trainer(...)construction, with the dataset, learner, and cropping step stubbed andTrainer.fitreplaced by a probe that prints the selected accelerator and device IDs.requirements.txtplus the fix(deps): make a fresh pip install importable with PytorchWildlife 1.3.1 #8 additions,CUDA_VISIBLE_DEVICES="".--gpus 0AttributeError: 'NoneType' object has no attribute 'split'MisconfigurationException: No supported gpu backend found!--gpus ""AttributeErrorMisconfigurationException--gpus 0[0][0][0]--gpus " 0 , 1 "ValueError[0, 1][0, 1]--gpus ""/" "ValueError: invalid literal for int()TypeError: Device IDs ... you passed None--gpus a,bValueError: invalid literal for int()BadParameter: Invalid GPU list 'a,b'. ...BadParameterOther checks:
configs/config.yamlgives the same dict undersafe_loadandFullLoader.python main.py --helpexits 0.Note: separate install problem (not fixed here)
A fresh
pip install -r requirements.txtcannot importPytorchWildlife1.3.1, its final release:soundfileandlibrosaat package load but does not declare them.yolov5dependency importspkg_resources, which setuptools removed in 82.0.0.I will open a separate PR for that. It needs a
setuptools<82pin, which is a trade-off reviewers should decide on its own.