Skip to content

fix(inference): run classifier inference under torch.no_grad - #11

Open
Zhongqi Miao (zhmiao) wants to merge 1 commit into
microsoft:mainfrom
zhmiao:fix/inference-no-grad
Open

Zhongqi Miao (zhmiao) wants to merge 1 commit into
microsoft:mainfrom
zhmiao:fix/inference-no-grad

Conversation

@zhmiao

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

Copy link
Copy Markdown
Contributor

What

Run single-image and batch inference in the ResNet and TIMM classifier wrappers under torch.no_grad(), and move each batch's logits to the CPU as it is produced.

Files:

  • src/models/classification/resnet_base/base_classifier.py
  • src/models/classification/timm_base/base_classifier.py

Why

Without no_grad, every forward pass keeps its autograd graph, and the batch loop held every batch's logits on the GPU until the end. GPU memory grows with the number of images.

This is the same bug fixed in the retired PytorchWildlife package by -Step- (@StepFPV) in microsoft/Biodiversity#662. PytorchWildlife is retired, so the fix is ported here, where the classifier code now lives.

Training (src/models/plain_resnet.py) does not call these methods and is unchanged.

Verification

A CPU check with a tiny stand-in network calls single_image_classification and batch_image_classification on both wrappers and reads requires_grad on the logits passed to results_generation:

Path origin/main this branch
resnet single requires_grad=True False
resnet batch requires_grad=True False
timm single requires_grad=True False
timm batch requires_grad=True False

GPU check on the real PlainResNetInference (ResNet-50, 5 classes, random weights, torch 2.11.0+cu128, RTX 6000 Ada). batch_image_classification over 256 random 224x224 JPEGs, default batch size 32, peak memory from torch.cuda.max_memory_allocated():

origin/main this branch
logits requires_grad (single / batch) True / True False / False
peak GPU memory, batch run 21,210 MiB 448 MiB

On main, peak memory grows with the number of images, because each batch keeps its autograd graph until torch.cat. Not run with released weights; the change does not touch weight loading or results_generation.

Single-image and batch inference in the ResNet and TIMM classifier
wrappers ran without torch.no_grad(), so every forward pass kept its
autograd graph. The batch path also held all logits on the GPU until
the end of the loop. On large folders this grows GPU memory without
bound.

Wrap both inference paths in torch.no_grad() and move each batch's
logits to the CPU as they are produced. Training code
(src/models/plain_resnet.py) does not call these methods and is
unchanged.

Ported from microsoft/Biodiversity#662 by @StepFPV, which fixed the
same code in the retired PytorchWildlife package.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: cf871bd7-3efa-4dad-8dd6-c61e46669a42
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