New smart_inference_mode()
conditional decorator
#8957
Merged
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.
Applies torch.inference_mode() decorator if torch>=1.9.0 else torch.no_grad() decorator. Material speed improvements observed in detect.py and val.py.
🛠️ PR Summary
Made with ❤️ by Ultralytics Actions
🌟 Summary
Integration of a new
smart_inference_mode
decorator across various Ultralytics YOLOv5 files for improved handling of inference operations.📊 Key Changes
smart_inference_mode
, has been created to wrap functions that previously used thetorch.no_grad()
decorator.detect.py
,export.py
,val.py
,models/common.py
, andmodels/yolo.py
to use the new decorator.utils/torch_utils.py
now usessmart_inference_mode
instead oftorch.no_grad()
._make_grid
function inmodels/yolo.py
now accepts an additional parameter to handle torch version compatibility.🎯 Purpose & Impact
torch.inference_mode
for PyTorch versions >=1.9.0, which provides a more performant context manager for inference operations, potentially speeding up the process.torch.no_grad()
when necessary.torch.no_grad()
calls with a decorator that checks the version and decides the context, the codebase becomes cleaner and more maintainable.These updates aim to ensure that the YOLOv5 codebase remains modern and efficient, aligning with the latest best practices for PyTorch inference, while still supporting older versions of the framework seamlessly. Users should benefit from potential performance improvements during model inference without needing to alter their own usage of the library. 🚀📈