-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Fix hardcoded 255 #6830
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 hardcoded 255 #6830
Conversation
@@ -226,19 +226,15 @@ def adjust_hue_image_tensor(image: torch.Tensor, hue_factor: float) -> torch.Ten | |||
return image | |||
|
|||
orig_dtype = image.dtype | |||
if image.dtype == torch.uint8: | |||
image = image / 255.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of doing the conversion manually, I've opted to use our kernel for this. Note that this also implicitly converts to float32
since the divisor is a float
.
@@ -15,12 +15,6 @@ def _assert_image_tensor(img: Tensor) -> None: | |||
raise TypeError("Tensor is not a torch image.") | |||
|
|||
|
|||
def _assert_threshold(img: Tensor, threshold: float) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was only used once so I inlined it.
test/test_functional_tensor.py
Outdated
F_t.solarize(img, threshold) | ||
|
||
|
||
@pytest.mark.parametrize("device", cpu_and_gpu()) | ||
@pytest.mark.parametrize("threshold", [260]) | ||
def test_solarize_threshold2_upper_bound(threshold, device): | ||
img = torch.randint(0, 256, (3, 12, 23)).to(device) | ||
img = torch.randint(0, 256, (3, 12, 23), dtype=torch.uint8, device=device) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
torch.randint
is a int64
by default which will no longer trigger an error for a threshold of 260
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of just fixing this here, I opted to make the tests more robust in e13613a.
@pmeier Thanks for the PR. Have you made any measurements on the V2 changes to check if there is a performance degradation? There are a few more 255 hardcoded values left but those are in places where we support only uint8. Is there a plan to update them? Finally this change, though correct, has the potential of breaking existing code. Before merging we might need to cherrypick it and bring it on FBcode to see if there is any breakage. |
Not yet. For all ops except for
Nope, I don't see a point. Or do you mean have another look at the kernel whether or not we are doing the wrong thing, i.e. nothing, for other integer dtypes? In that case, yes that would be a good idea. |
There are two places with hardcoded
|
@pmeier As discussed offline, checking only the methods that change significantly will do. No need to benchmark those that just fetch the value from the dictionary. Let's wait for Victor's thoughts on this. @fmassa I was wondering if you could chime in as well. I'm supportive of Philip's change, just wanted to make sure we don't miss something important. The TLDR is, we replace the hardcoded 255 values with the ones for each dtype. Floats and uint8 remain unaffected but other integer types would change. This would align the behaviour of the kernels with |
No changes apart from noise. |
I've imported this PR on FBcode to check if there are any breakages at D40752944 |
I ran all the tests internally and it seems the change didn't break anything. There are a lot of pre-existing failures and skipped tests, so we can't be 100% sure. But it looks like it's mostly OK. @pmeier Do we need to update the PR to cover your recently changes on the 2 kernels? |
…into fix-hardcoded-255
Hi @datumbox I'm supportive of this change. We didn't have good support for other dtypes before, so assuming either |
@pmeier Looks like we should be good to go once you finish the remaining hardcoded values. Ping me when you are happy to do one final review and merge. I've already ported this internally and it looks there are no issues. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, I highlighted 2 places were we should measure performance.
return (1 if image.is_floating_point() else 255) - image # type: ignore[no-any-return] | ||
else: # signed integer dtypes | ||
# We can't use `Tensor.bitwise_not` here, since we want to retain the leading zero bit that encodes the sign | ||
return image.bitwise_xor((1 << _num_value_bits(image.dtype)) - 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you provide benchmarks for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[--------------------------- invert_image_tensor ---------------------------]
| main | fix-hardcoded-255
1 threads: ------------------------------------------------------------------
(3, 512, 512), float32, cpu | 61 (+- 0) us | 57 (+- 0) us
(3, 512, 512), uint8, cpu | 17 (+- 0) us | 17 (+- 0) us
(3, 512, 512), int32, cpu | 78 (+- 0) us | 63 (+- 0) us
(5, 3, 512, 512), float32, cpu | 461 (+- 33) us | 445 (+- 31) us
(5, 3, 512, 512), uint8, cpu | 98 (+- 1) us | 79 (+- 1) us
(5, 3, 512, 512), int32, cpu | 538 (+- 67) us | 514 (+- 8) us
Times are in microseconds (us).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! I like it when bug/code-quality fixing leads to speed improvements. What more can we ask? 😄
There was some offline discussion whether or not we want to remove the implicit assumption that floating point images have the maximum value
|
Let's not adopt anything that slows us down.
What the heck! Well sounds good to me. Shall we try the trick in other places too? Here are some places that we could replace it:
Finally from what I understand, none of the changes you make here are expected to cause speed regressions, can you confirm? |
As discussed offline, there are probably a lot more implicit assumptions on the floating point range than what I detailed above. We agreed to just put a comment on the value inside the
Will do so in a follow-up PR since this is unrelated to this PR.
Nope, perf should be the same. For some ops I posted benchmarks and they all show either no difference or even an improvement. |
Summary: * fix prototype kernels * fix stable kernels * fix tests * make test more robust * improve invert for signed integers * improve invert * fix posterize * Revert "assume that integer images are [0, 255] in equalize (#6859)" This reverts commit 436ff9a. * fix solarize in AA * fix resize * Revert "fix resize" This reverts commit 5f33f4a. * add comment to float max value Reviewed By: datumbox Differential Revision: D41020539 fbshipit-source-id: 1c618ead36a0ae4d93b4ebf07186fd39bd85d915 Co-authored-by: Vasilis Vryniotis <[email protected]>
Fixes #6825.
cc @vfdev-5 @datumbox @bjuncek