Skip to content
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

[prototype] Minor improvements on functional #6832

Merged
merged 5 commits into from
Oct 25, 2022

Conversation

datumbox
Copy link
Contributor

@datumbox datumbox commented Oct 25, 2022

Minor last improvements on the functional kernels. I consider these mostly code-quality improvements rather that speed optimizations.

cc @vfdev-5 @bjuncek @pmeier

Comment on lines -213 to -214
if not (isinstance(image, torch.Tensor)):
raise TypeError("Input img should be Tensor image")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As decided previously, removing the final tensor checks on tensor kernels.

@@ -183,12 +183,8 @@ def clamp_bounding_box(
return convert_format_bounding_box(xyxy_boxes, BoundingBoxFormat.XYXY, format)


def _split_alpha(image: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
return image[..., :-1, :, :], image[..., -1:, :, :]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tensor_split is marginally faster because it avoids the double indexing:

[------------------------------- Convert_color_space_image_tensor cpu torch.uint8 ------------------------------]
                     |  convert_color_space_image_tensor v2 before  |  convert_color_space_image_tensor_ v2 after
1 threads: ------------------------------------------------------------------------------------------------------
      (4, 400, 400)  |                     217                      |                     213                    
6 threads: ------------------------------------------------------------------------------------------------------
      (4, 400, 400)  |                     297                      |                     292                    

Times are in microseconds (us).

[------------------------------ Convert_color_space_image_tensor cuda torch.uint8 ------------------------------]
                     |  convert_color_space_image_tensor v2 before  |  convert_color_space_image_tensor_ v2 after
1 threads: ------------------------------------------------------------------------------------------------------
      (4, 400, 400)  |                     56.4                     |                      52                    
6 threads: ------------------------------------------------------------------------------------------------------
      (4, 400, 400)  |                     56.4                     |                      52                    

Times are in microseconds (us).

[------------------------------ Convert_color_space_image_tensor cpu torch.float32 -----------------------------]
                     |  convert_color_space_image_tensor v2 before  |  convert_color_space_image_tensor_ v2 after
1 threads: ------------------------------------------------------------------------------------------------------
      (4, 400, 400)  |                     236                      |                     232                    
6 threads: ------------------------------------------------------------------------------------------------------
      (4, 400, 400)  |                     325                      |                     321                    

Times are in microseconds (us).

[----------------------------- Convert_color_space_image_tensor cuda torch.float32 -----------------------------]
                     |  convert_color_space_image_tensor v2 before  |  convert_color_space_image_tensor_ v2 after
1 threads: ------------------------------------------------------------------------------------------------------
      (4, 400, 400)  |                     56.9                     |                     52.4                   
6 threads: ------------------------------------------------------------------------------------------------------
      (4, 400, 400)  |                     56.7                     |                     52.4                   

Times are in microseconds (us).

def _strip_alpha(image: torch.Tensor) -> torch.Tensor:
image, alpha = _split_alpha(image)
image, alpha = torch.tensor_split(image, indices=(-1,), dim=-3)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we don't need a private one liner method for something Torch has a built in method.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: I would prefer it since it is a little more expressive. Plus, since we have a _add_alpha function it would be consistent to have the complementary _split_alpha as well. With this change we have three identical, non-trivial calls of torch.tensor_split. At a glance, it is harder to see that these three actually do exactly the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I'll restore `_split_alpha.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restoring this causes more harm than good. JIT is complaining that it's not longer a tuple, which means I would have to split into 2 lines and return as tuple to please it. I don't think it's worth this.

Copy link
Collaborator

@pmeier pmeier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks Vasilis.

@datumbox datumbox merged commit edb3a80 into pytorch:main Oct 25, 2022
@datumbox datumbox deleted the prototype/more_cleanups branch October 25, 2022 13:20
@pmeier pmeier mentioned this pull request Oct 26, 2022
facebook-github-bot pushed a commit that referenced this pull request Oct 27, 2022
Summary:
* Minor improvements on functional.

* Restore `_split_alpha`.

* Revert "Restore `_split_alpha`."

This reverts commit 2286120.

Reviewed By: YosuaMichael

Differential Revision: D40722902

fbshipit-source-id: 3a574939365abd1b74ed3a558b4354b1c40fc883
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants