Enable loading in half precision #73
Open
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.
I noticed there already were functions for setting autocast dtype depending on GPU capability. However, they were restricted to compute major version >= 7 whereas my GPU with compute 5.2 can also run models in float16, and in fact it is the only way to run SAM3 because it crashes with OOM if loading in float32 with 4 GB VRAM. I don't know if compute 6.x devices are good with float16 so the detection rule could be tuned further if they have problems.
I moved dtype detection into load_model, save it with the sam3_model and subsequently use it to autocast accordingly. Loading the video model with
.half()is the key change to reduce memory use, but the autocasts are necessary to prevent tensor type mismatch errors during processing. I hope I caught them for all use cases...The interactive detection dialog doesn't use the same loaded model so I added separate detection for it.