Skip to content

fix: wrap classifier batch inference in torch.no_grad() to stop memory leak - #662

Open
-Step- (StepFPV) wants to merge 1 commit into
microsoft:mainfrom
StepFPV:fix/timm-classifier-memory-leak
Open

fix: wrap classifier batch inference in torch.no_grad() to stop memory leak#662
-Step- (StepFPV) wants to merge 1 commit into
microsoft:mainfrom
StepFPV:fix/timm-classifier-memory-leak

Conversation

@StepFPV

Copy link
Copy Markdown

What

batch_image_classification() in both classifier families ran the forward pass without torch.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 final torch.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_classification isn'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.

…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>
@StepFPV

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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.

Memory usage issues with TIMM classifier

1 participant