Fix float out of range in owlvit and owlv2 when using FP16 or lower precision #31657
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.
What does this PR do?
As discussed in #31342 (comment)
The hard coded
-1e6
value used to mask logits is not in range for FP16 which causes avalue cannot be converted to type at::Half without overflow
error. We replace that with the min value of whichever dtype the logits are in.Did a search and only the
owlvit
andowlv2
models are using this, however, we should take note if any future model implementation hardcodes a similar value. There exists a couple more models using -1e7, -1e8, -1e9 and -1e10, but most of them are TensorFlow models, and the rest are either deprecated or language models. Since there has been no issue reports on this so far, I think it is properly handled somewhere so I won't change them.Tests on FP32 showed no numerical difference in logits up to 1e-4 (what the test uses).
The reason of not using
float('-inf')
is that it can be 20% (RTX 3080Ti) to 3x (H100 SXM5) slower than using a predefined value for some reason.Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@amyeroberts