-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[Relax][ONNX] Add roi_pool op and MaxRoiPool frontend support #18952
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -163,6 +163,7 @@ | |
| multibox_transform_loc, | ||
| non_max_suppression, | ||
| roi_align, | ||
| roi_pool, | ||
| ) | ||
|
|
||
|
|
||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,3 +20,4 @@ | |
| from .multibox_transform_loc import * | ||
| from .nms import * | ||
| from .roi_align import * | ||
| from .roi_pool import * | ||
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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """ROI Pool operator""" | ||
|
|
||
| from ..base import Expr | ||
| from . import _ffi_api | ||
|
|
||
|
|
||
| def roi_pool( | ||
| data: Expr, | ||
| rois: Expr, | ||
| pooled_size: int | tuple[int, int] | list[int], | ||
| spatial_scale: float, | ||
| layout: str = "NCHW", | ||
| ): | ||
| """ROI Pool operator. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| data : relax.Expr | ||
| 4-D input tensor. | ||
|
|
||
| rois : relax.Expr | ||
| 2-D input tensor with shape `(num_roi, 5)` in | ||
| `[batch_idx, x1, y1, x2, y2]` format. | ||
|
|
||
| pooled_size : Union[int, Tuple[int, int], List[int]] | ||
| Output pooled size. | ||
|
|
||
| spatial_scale : float | ||
| Ratio of input feature map height (or width) to raw image height (or width). | ||
|
|
||
| layout : str, optional | ||
| Layout of the input data. Currently only `NCHW` is supported. | ||
|
|
||
| Returns | ||
| ------- | ||
| result : relax.Expr | ||
| The computed result. | ||
| """ | ||
| if isinstance(pooled_size, int): | ||
| pooled_size = (pooled_size, pooled_size) | ||
| return _ffi_api.roi_pool(data, rois, pooled_size, spatial_scale, layout) |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,3 +20,4 @@ | |
| from .multibox_transform_loc import * | ||
| from .nms import * | ||
| from .roi_align import * | ||
| from .roi_pool import * | ||
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 |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| # pylint: disable=invalid-name | ||
| """ROI Pool operator""" | ||
|
|
||
| import tvm | ||
| from tvm import te | ||
|
|
||
|
|
||
| def roi_pool_nchw(data, rois, pooled_size, spatial_scale): | ||
| """ROI pool operator in NCHW layout.""" | ||
| _, channel, height, width = data.shape | ||
| num_roi, _ = rois.shape | ||
|
|
||
| if isinstance(pooled_size, int): | ||
| pooled_size_h = pooled_size_w = pooled_size | ||
| else: | ||
| pooled_size_h, pooled_size_w = pooled_size | ||
|
|
||
| zero = tvm.tirx.const(0.0, data.dtype) | ||
| roi_dtype = rois.dtype | ||
|
|
||
| neg_inf = tvm.tirx.const(float("-inf"), data.dtype) | ||
|
|
||
| def _bin_bounds(i, ph, pw): | ||
| roi = rois[i] | ||
| roi_start_w = te.round(roi[1] * spatial_scale).astype("int32") | ||
| roi_start_h = te.round(roi[2] * spatial_scale).astype("int32") | ||
| roi_end_w = te.round(roi[3] * spatial_scale).astype("int32") | ||
| roi_end_h = te.round(roi[4] * spatial_scale).astype("int32") | ||
|
|
||
| roi_h = te.max(roi_end_h - roi_start_h + 1, tvm.tirx.const(1, "int32")) | ||
| roi_w = te.max(roi_end_w - roi_start_w + 1, tvm.tirx.const(1, "int32")) | ||
|
|
||
| bin_h = tvm.tirx.Cast(roi_dtype, roi_h) / tvm.tirx.const(float(pooled_size_h), roi_dtype) | ||
| bin_w = tvm.tirx.Cast(roi_dtype, roi_w) / tvm.tirx.const(float(pooled_size_w), roi_dtype) | ||
|
|
||
| hstart = te.floor(tvm.tirx.Cast(roi_dtype, ph) * bin_h).astype("int32") | ||
| wstart = te.floor(tvm.tirx.Cast(roi_dtype, pw) * bin_w).astype("int32") | ||
| hend = te.ceil(tvm.tirx.Cast(roi_dtype, ph + 1) * bin_h).astype("int32") | ||
| wend = te.ceil(tvm.tirx.Cast(roi_dtype, pw + 1) * bin_w).astype("int32") | ||
|
|
||
| hstart = te.min(te.max(hstart + roi_start_h, 0), height) | ||
| hend = te.min(te.max(hend + roi_start_h, 0), height) | ||
| wstart = te.min(te.max(wstart + roi_start_w, 0), width) | ||
| wend = te.min(te.max(wend + roi_start_w, 0), width) | ||
| return hstart, hend, wstart, wend | ||
|
|
||
| def _sample(i, c, ph, pw): | ||
| roi = rois[i] | ||
| batch_index = roi[0].astype("int32") | ||
| hstart, hend, wstart, wend = _bin_bounds(i, ph, pw) | ||
| valid = tvm.tirx.all(hstart <= rh, rh < hend, wstart <= rw, rw < wend) | ||
| return tvm.tirx.if_then_else(valid, data[batch_index, c, rh, rw], neg_inf) | ||
|
|
||
| def _is_empty(i, ph, pw): | ||
| hstart, hend, wstart, wend = _bin_bounds(i, ph, pw) | ||
| return tvm.tirx.any(hend <= hstart, wend <= wstart) | ||
|
|
||
| rh = te.reduce_axis((0, height), name="rh") | ||
| rw = te.reduce_axis((0, width), name="rw") | ||
| pooled = te.compute( | ||
| (num_roi, channel, pooled_size_h, pooled_size_w), | ||
| lambda i, c, ph, pw: te.max(_sample(i, c, ph, pw), axis=[rh, rw]), | ||
| tag="pool,roi_pool_nchw", | ||
| ) | ||
|
|
||
| return te.compute( | ||
| (num_roi, channel, pooled_size_h, pooled_size_w), | ||
| lambda i, c, ph, pw: tvm.tirx.if_then_else( | ||
| _is_empty(i, ph, pw), zero, pooled[i, c, ph, pw] | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def roi_pool(data, rois, pooled_size, spatial_scale, layout="NCHW"): | ||
| """ROI pool operator.""" | ||
| if layout == "NCHW": | ||
| return roi_pool_nchw(data, rois, pooled_size, spatial_scale) | ||
| raise ValueError(f"Unsupported layout for roi_pool: {layout}") | ||
Oops, something went wrong.
Oops, something went wrong.
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.
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.
The current implementation computes bin boundaries twice: once in
_sample(within thepooledcompute) and again in_is_empty(in the final compute). This is inefficient.This can be optimized by checking if the result of the max-pooling is
-inf, which indicates an empty bin, as-infis used as a sentinel value in_sample. This avoids recomputing the bounds.With this change, the
_is_emptyfunction (lines 70-72) is no longer needed and can be removed.Note: This change in logic assumes that the input
datadoes not contain-inf. If a non-empty bin contains only values less than or equal to-inf, the original code would return-inf, while this suggested change would return0. Given thatroi_poolis typically used on feature maps from CNNs, this assumption is likely safe.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.
I kept the explicit empty-bin check here because using -inf as the final sentinel could mis-handle valid non-empty bins whose max value is actually -inf. This is a bit more work, but it preserves the intended semantics without assuming anything about the input values.