[Relax][ONNX] Add roi_pool op and MaxRoiPool frontend support by LudovicoYIN · Pull Request #18952 · apache/tvm

medium

The current implementation computes bin boundaries twice: once in _sample (within the pooled compute) 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 -inf is used as a sentinel value in _sample. This avoids recomputing the bounds.

With this change, the _is_empty function (lines 70-72) is no longer needed and can be removed.

Note: This change in logic assumes that the input data does 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 return 0. Given that roi_pool is typically used on feature maps from CNNs, this assumption is likely safe.

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]
),
)
return te.compute(
(num_roi, channel, pooled_size_h, pooled_size_w),
lambda i, c, ph, pw: tvm.tirx.if_then_else(
pooled[i, c, ph, pw] == neg_inf, zero, pooled[i, c, ph, pw]
),
)