fix: wrap classifier batch inference in torch.no_grad() to stop memory leak - #662
Open
-Step- (StepFPV) wants to merge 1 commit into
Open
fix: wrap classifier batch inference in torch.no_grad() to stop memory leak#662-Step- (StepFPV) wants to merge 1 commit into
-Step- (StepFPV) wants to merge 1 commit into
Conversation
…y leak batch_image_classification() in both the TIMM-based (DFNE/Deepfaune) and ResNet-based (Amazon/Opossum/Serengeti) classifiers ran the forward pass without torch.no_grad(), so PyTorch retained the autograd graph for every batch. Memory grew unbounded across a run (reported: ~300GB RAM/VRAM on ~3000 images, crash before completion). Also move each batch's logits to CPU right after inference instead of holding them all on GPU until the final torch.cat(...).cpu(), so peak VRAM no longer scales with dataset size. Fixes microsoft#609 Diagnosis and fix were done with AI assistance (Claude Code). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Author
|
@microsoft-github-policy-service agree |
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.
What
batch_image_classification()in both classifier families ran the forward pass withouttorch.no_grad():PytorchWildlife/models/classification/timm_base/base_classifier.py(DFNE / Deepfaune)PytorchWildlife/models/classification/resnet_base/base_classifier.py(Amazon / Opossum / Serengeti)Since gradients were tracked, PyTorch retained the autograd graph for every batch appended to
total_logits, so memory grew unbounded over a run instead of staying flat.Also moved each batch's logits to CPU right after inference (
.cpu()per batch) instead of holding them all on GPU until the finaltorch.cat(...).cpu(), so peak VRAM no longer scales with dataset size.Why
Fixes #609 — reported ~300GB RAM/VRAM growth and a crash before completing inference on ~3000 images. Root cause and reproduction were already diagnosed in the issue; this PR applies the same fix to both classifier base classes since they share the identical pattern.
Testing
Confirmed both loops are functionally unchanged (same batch order, same result construction) — only the gradient-tracking and CPU-transfer timing changed. Verified
python -c "import ast; ast.parse(open(f).read())"on both files.Scope kept to the reported bug only (
batch_image_classification);single_image_classificationisn't part of the leak (no accumulation across a loop) so left untouched to keep this PR focused.Disclosure
Diagnosis and implementation were done with AI assistance (Claude Code), reviewed and submitted by me.
Are you willing to submit a PR?
Yes — this is it.