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

Remaining BBox kernel perf optimizations #6896

Merged
merged 8 commits into from
Nov 3, 2022
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions torchvision/prototype/transforms/functional/_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,11 @@ def resize_bounding_box(
) -> Tuple[torch.Tensor, Tuple[int, int]]:
old_height, old_width = spatial_size
new_height, new_width = _compute_resized_output_size(spatial_size, size=size, max_size=max_size)
ratios = torch.tensor((new_width / old_width, new_height / old_height), device=bounding_box.device)
w_ratio = new_width / old_width
h_ratio = new_height / old_height
ratios = torch.tensor([w_ratio, h_ratio, w_ratio, h_ratio], device=bounding_box.device)
return (
bounding_box.reshape(-1, 2, 2).mul(ratios).to(bounding_box.dtype).reshape(bounding_box.shape),
bounding_box.mul(ratios).to(bounding_box.dtype),
Comment on lines +184 to +188
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Improvement:

[------------ resize cpu torch.float32 ------------]
                |        old       |        new     
1 threads: -----------------------------------------
      (128, 4)  |   13 (+-  0) us  |    8 (+-  0) us
6 threads: -----------------------------------------
      (128, 4)  |   13 (+-  0) us  |    8 (+-  0) us

Times are in microseconds (us).

[----------- resize cuda torch.float32 ------------]
                |        old       |        new     
1 threads: -----------------------------------------
      (128, 4)  |   37 (+-  0) us  |   31 (+-  0) us
6 threads: -----------------------------------------
      (128, 4)  |   37 (+-  0) us  |   31 (+-  0) us

Times are in microseconds (us).

[------------- resize cpu torch.uint8 -------------]
                |        old       |        new     
1 threads: -----------------------------------------
      (128, 4)  |   19 (+-  0) us  |   13 (+-  0) us
6 threads: -----------------------------------------
      (128, 4)  |   19 (+-  0) us  |   13 (+-  0) us

Times are in microseconds (us).

[------------ resize cuda torch.uint8 -------------]
                |        old       |        new     
1 threads: -----------------------------------------
      (128, 4)  |   45 (+-  0) us  |   39 (+-  0) us
6 threads: -----------------------------------------
      (128, 4)  |   45 (+-  0) us  |   39 (+-  1) us

Times are in microseconds (us).

(new_height, new_width),
)

Expand Down Expand Up @@ -367,8 +369,7 @@ def _affine_bounding_box_xyxy(
# 3) Reshape transformed points to [N boxes, 4 points, x/y coords]
# and compute bounding box from 4 transformed points:
transformed_points = transformed_points.reshape(-1, 4, 2)
out_bbox_mins, _ = torch.min(transformed_points, dim=1)
out_bbox_maxs, _ = torch.max(transformed_points, dim=1)
out_bbox_mins, out_bbox_maxs = torch.aminmax(transformed_points, dim=1)
out_bboxes = torch.cat([out_bbox_mins, out_bbox_maxs], dim=1)

if expand:
Expand All @@ -388,8 +389,7 @@ def _affine_bounding_box_xyxy(
new_points = torch.matmul(points, transposed_affine_matrix)
tr, _ = torch.min(new_points, dim=0, keepdim=True)
# Translate bounding boxes
out_bboxes[:, 0::2] = out_bboxes[:, 0::2] - tr[:, 0]
out_bboxes[:, 1::2] = out_bboxes[:, 1::2] - tr[:, 1]
out_bboxes.sub_(tr.repeat((1, 2)))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Improvement for both changes:

[-------------------- bbox_rotate cpu -------------------]
                     |      False       |       True     
1 threads: ----------------------------------------------
      torch.float32  |  265 (+- 40) us  |  225 (+-  2) us
      torch.float64  |  261 (+-  1) us  |  241 (+-  1) us
      torch.int32    |  258 (+-  1) us  |  239 (+-  2) us
      torch.int64    |  260 (+-  1) us  |  239 (+-  1) us
6 threads: ----------------------------------------------
      torch.float32  |  466 (+- 10) us  |  405 (+- 20) us
      torch.float64  |  483 (+- 10) us  |  422 (+- 55) us
      torch.int32    |  479 (+- 10) us  |  420 (+- 10) us
      torch.int64    |  482 (+- 18) us  |  422 (+- 10) us

Times are in microseconds (us).

[-------------------- bbox_rotate cpu -------------------]
                     |      False       |       True     
1 threads: ----------------------------------------------
      torch.float32  |  498 (+- 46) us  |  432 (+-  0) us
      torch.float64  |  489 (+-  1) us  |  446 (+-  0) us
      torch.int32    |  503 (+-  0) us  |  459 (+-  3) us
      torch.int64    |  504 (+-  3) us  |  458 (+-  0) us
6 threads: ----------------------------------------------
      torch.float32  |  573 (+-  2) us  |  530 (+-  0) us
      torch.float64  |  600 (+- 20) us  |  554 (+- 20) us
      torch.int32    |  609 (+- 20) us  |  560 (+- 10) us
      torch.int64    |  598 (+- 58) us  |  563 (+- 10) us

Times are in microseconds (us).

# Estimate meta-data for image with inverted=True and with center=[0,0]
affine_vector = _get_inverse_affine_matrix([0.0, 0.0], angle, translate, scale, shear)
new_width, new_height = _FT._compute_affine_output_size(affine_vector, width, height)
Expand Down