-
-
Notifications
You must be signed in to change notification settings - Fork 16.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix different devices bug when moving model from GPU to CPU #5110
Conversation
@SamFC10 thanks for the PR! Yes this is probably the best solution of the 3 (thanks for the detailed analysis of options). Does this PR pass your reproduce test? |
Yes it does. To verify : - model = torch.hub.load('ultralytics/yolov5', 'yolov5s', device='cuda:0')
+ model = torch.hub.load('SamFC10/yolov5:fix-devices', 'yolov5s', device='cuda:0') But there is still an issue. While Looking at the source code, functions like |
Latest commit fixes the errors. |
@
Very smart, this is a good idea. Good use of |
Co-authored-by: Jebastin Nadar <njebastin10@gmail.com>
@SamFC10 PR is merged. Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐ |
…ics#5110) * fix different devices bug * extend _apply() instead of to() for a general fix * Only apply if Detect() is last layer Co-authored-by: Jebastin Nadar <njebastin10@gmail.com> * Indent fix * Add comment to yolo.py * Add comment to common.py Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Discovered by @glenn-jocher #4833 (comment)
Simple reproducer:
Problem :
stride
andgrid
are not moved to the correct device. Only tensors that are registered usingregister_buffer
move to the correct device.Possible solutions :
stride
andgrid
as a nn.Parameter() and nn.ParameterList() respectively.stride
andgrid
using register_buffer(). Butgrid
is a list of tensors and register_buffer() only accepts tensors as inputs, so not an ideal solution.to()
function ofModel
andAutoShape
class to manually movestride
andgrid
. This seems to be an easy and straightforward solution.Similar discussion - https://discuss.pytorch.org/t/why-model-to-device-wouldnt-put-tensors-on-a-custom-layer-to-the-same-device/17964
🛠️ PR Summary
Made with ❤️ by Ultralytics Actions
🌟 Summary
Enhanced YOLOv5 model flexibility with improved tensor operation support.
📊 Key Changes
_apply
method inmodels/common.py
andmodels/yolo.py
..to()
,.cpu()
,.cuda()
, and.half()
.Detect()
within the model to correctly handlestride
andgrid
.🎯 Purpose & Impact