-
Notifications
You must be signed in to change notification settings - Fork 7.2k
ToDtype CV-CUDA Backend #9278
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
Open
justincdavis
wants to merge
3
commits into
pytorch:main
Choose a base branch
from
justincdavis:feat/dtype_cvcuda
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
ToDtype CV-CUDA Backend #9278
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2619,7 +2619,32 @@ def test_kernel(self, kernel, make_input, input_dtype, output_dtype, device, sca | |
| scale=scale, | ||
| ) | ||
|
|
||
| @pytest.mark.parametrize("make_input", [make_image_tensor, make_image, make_video]) | ||
| @pytest.mark.parametrize( | ||
| ("kernel", "input_type"), | ||
| [ | ||
| (F.to_dtype_image, torch.Tensor), | ||
| (F.to_dtype_video, tv_tensors.Video), | ||
| pytest.param( | ||
| F._misc._to_dtype_image_cvcuda, | ||
| None, | ||
| marks=pytest.mark.needs_cvcuda, | ||
| ), | ||
| ], | ||
| ) | ||
| def test_functional_signature(self, kernel, input_type): | ||
| if kernel is F._misc._to_dtype_image_cvcuda: | ||
| input_type = _import_cvcuda().Tensor | ||
| check_functional_kernel_signature_match(F.to_dtype, kernel=kernel, input_type=input_type) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding this test! |
||
|
|
||
| @pytest.mark.parametrize( | ||
| "make_input", | ||
| [ | ||
| make_image_tensor, | ||
| make_image, | ||
| make_video, | ||
| pytest.param(make_image_cvcuda, marks=pytest.mark.needs_cvcuda), | ||
| ], | ||
| ) | ||
| @pytest.mark.parametrize("input_dtype", [torch.float32, torch.float64, torch.uint8]) | ||
| @pytest.mark.parametrize("output_dtype", [torch.float32, torch.float64, torch.uint8]) | ||
| @pytest.mark.parametrize("device", cpu_and_cuda()) | ||
|
|
@@ -2634,7 +2659,14 @@ def test_functional(self, make_input, input_dtype, output_dtype, device, scale): | |
|
|
||
| @pytest.mark.parametrize( | ||
| "make_input", | ||
| [make_image_tensor, make_image, make_bounding_boxes, make_segmentation_mask, make_video], | ||
| [ | ||
| make_image_tensor, | ||
| make_image, | ||
| make_bounding_boxes, | ||
| make_segmentation_mask, | ||
| make_video, | ||
| pytest.param(make_image_cvcuda, marks=pytest.mark.needs_cvcuda), | ||
| ], | ||
| ) | ||
| @pytest.mark.parametrize("input_dtype", [torch.float32, torch.float64, torch.uint8]) | ||
| @pytest.mark.parametrize("output_dtype", [torch.float32, torch.float64, torch.uint8]) | ||
|
|
@@ -2680,25 +2712,69 @@ def fn(value): | |
|
|
||
| return torch.tensor(tree_map(fn, image.tolist())).to(dtype=output_dtype, device=image.device) | ||
|
|
||
| def _get_dtype_conversion_atol_cvcuda(self, input_dtype, output_dtype): | ||
| in_bits = torch.iinfo(input_dtype).bits if not input_dtype.is_floating_point else None | ||
| out_bits = torch.iinfo(output_dtype).bits if not output_dtype.is_floating_point else None | ||
| narrows_bits = in_bits is not None and out_bits is not None and out_bits < in_bits | ||
|
|
||
| # int->int with narrowing bits, allow atol=1 for rounding diffs | ||
| if narrows_bits: | ||
| atol = 1 | ||
| # float->int check for same diff, rounding error on float | ||
| elif input_dtype.is_floating_point and not output_dtype.is_floating_point: | ||
| atol = 1 | ||
| # if generating a float value from an int, allow small rounding error | ||
| elif not input_dtype.is_floating_point and output_dtype.is_floating_point: | ||
| atol = 1e-7 | ||
| # all other cases, should be exact | ||
| # uint8 -> uint16 promotion would be here | ||
| else: | ||
| atol = 0 | ||
|
|
||
| return atol | ||
|
|
||
| @pytest.mark.parametrize("input_dtype", [torch.float32, torch.float64, torch.uint8, torch.uint16]) | ||
| @pytest.mark.parametrize("output_dtype", [torch.float32, torch.float64, torch.uint8, torch.uint16]) | ||
| @pytest.mark.parametrize("device", cpu_and_cuda()) | ||
| @pytest.mark.parametrize("scale", (True, False)) | ||
| def test_image_correctness(self, input_dtype, output_dtype, device, scale): | ||
| @pytest.mark.parametrize( | ||
| "make_input", | ||
| [ | ||
| make_image, | ||
| pytest.param(make_image_cvcuda, marks=pytest.mark.needs_cvcuda), | ||
| ], | ||
| ) | ||
| @pytest.mark.parametrize("fn", [F.to_dtype, transform_cls_to_functional(transforms.ToDtype)]) | ||
| def test_image_correctness(self, input_dtype, output_dtype, device, scale, make_input, fn): | ||
| if input_dtype.is_floating_point and output_dtype == torch.int64: | ||
| pytest.xfail("float to int64 conversion is not supported") | ||
| if input_dtype == torch.uint8 and output_dtype == torch.uint16 and device == "cuda": | ||
| pytest.xfail("uint8 to uint16 conversion is not supported on cuda") | ||
| if ( | ||
| input_dtype == torch.uint16 | ||
| and output_dtype == torch.uint8 | ||
| and not scale | ||
| and make_input is make_image_cvcuda | ||
| ): | ||
| pytest.xfail("uint16 to uint8 conversion without scale is not supported for CV-CUDA.") | ||
|
|
||
| input = make_image(dtype=input_dtype, device=device) | ||
| input = make_input(dtype=input_dtype, device=device) | ||
| out = fn(input, dtype=output_dtype, scale=scale) | ||
|
|
||
| if make_input is make_image_cvcuda: | ||
| input = F.cvcuda_to_tensor(input) | ||
| out = F.cvcuda_to_tensor(out) | ||
|
|
||
| out = F.to_dtype(input, dtype=output_dtype, scale=scale) | ||
| expected = self.reference_convert_dtype_image_tensor(input, dtype=output_dtype, scale=scale) | ||
|
|
||
| if input_dtype.is_floating_point and not output_dtype.is_floating_point and scale: | ||
| torch.testing.assert_close(out, expected, atol=1, rtol=0) | ||
| else: | ||
| torch.testing.assert_close(out, expected) | ||
| atol, rtol = None, None | ||
| if make_input is make_image_cvcuda: | ||
| atol = self._get_dtype_conversion_atol_cvcuda(input_dtype, output_dtype) | ||
| rtol = 0 | ||
| elif input_dtype.is_floating_point and not output_dtype.is_floating_point and scale: | ||
| atol, rtol = 1, 0 | ||
|
|
||
| torch.testing.assert_close(out, expected, atol=atol, rtol=rtol) | ||
|
|
||
| def was_scaled(self, inpt): | ||
| # this assumes the target dtype is float | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Uh oh!
There was an error while loading. Please reload this page.