[Relax][ONNX][Torch] Add roi_align support and frontend integration#18936
[Relax][ONNX][Torch] Add roi_align support and frontend integration#18936tlopex merged 2 commits intoapache:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces comprehensive support for the ROIAlign operator across the Relax framework, integrating it with both ONNX and PyTorch frontends. The changes enable users to leverage ROIAlign functionality within Relax, ensuring accurate and consistent behavior by addressing different coordinate transformation modes and aligned semantics. This completes a previously tracked work item, expanding Relax's capabilities in computer vision tasks. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces the ROIAlign operator to TVM Relax, including its attributes, Python and C++ implementations, legalization rules, and comprehensive tests across ONNX and PyTorch frontends. Review comments suggest improving a misleading error message in the ONNX frontend's RoiAlign converter, ensuring consistency in the default sampling_ratio value in the PyTorch frontend, and addressing potential test fragility due to a dynamically generated variable name in one of the tests.
| elif coordinate_transformation_mode != "output_half_pixel": | ||
| raise NotImplementedError( | ||
| "RoiAlign only supports coordinate_transformation_mode " | ||
| "'half_pixel' and 'output_half_pixel'" | ||
| ) |
There was a problem hiding this comment.
The NotImplementedError message is slightly misleading. It states that RoiAlign only supports 'half_pixel' and 'output_half_pixel', but the else branch implicitly handles 'output_half_pixel' by setting aligned = False. The error should only be raised if coordinate_transformation_mode is neither 'half_pixel' nor 'output_half_pixel'.
| elif coordinate_transformation_mode != "output_half_pixel": | |
| raise NotImplementedError( | |
| "RoiAlign only supports coordinate_transformation_mode " | |
| "'half_pixel' and 'output_half_pixel'" | |
| ) | |
| elif coordinate_transformation_mode == "output_half_pixel": | |
| aligned = False | |
| else: | |
| raise NotImplementedError( | |
| "RoiAlign only supports coordinate_transformation_mode " | |
| "'half_pixel' and 'output_half_pixel'" | |
| ) |
| sampling_ratio = args[5] if len(args) > 5 else -1 | ||
| aligned = args[6] if len(args) > 6 else False |
There was a problem hiding this comment.
The sampling_ratio default value is set to -1 in the roi_align function signature, but here it's 0 in the attr.get call. It's better to be consistent with the Relax op's default value.
| sampling_ratio = args[5] if len(args) > 5 else -1 | |
| aligned = args[6] if len(args) > 6 else False | |
| sampling_ratio = args[5] if len(args) > 5 else -1 |
| input_1: R.Tensor((1, 1, 4, 4), dtype="float32"), | ||
| rois: R.Tensor((1, 5), dtype="float32"), | ||
| ) -> R.Tuple(R.Tensor((1, 1, 1, 1), dtype="float32")): | ||
| R.func_attr({"tir_var_lower_bound": {"s86": 2}}) |
There was a problem hiding this comment.
The tir_var_lower_bound attribute s86 seems to be a dynamically generated variable name. It's generally better to use a more stable or descriptive name for variables in expected IR modules, especially if they are part of assertions. This can prevent test failures if the internal naming convention changes slightly.
tlopex
left a comment
There was a problem hiding this comment.
Overall looks good, please have a look at the comments
tests/python/relax/test_op_vision.py
Outdated
|
|
||
| _ROI_ALIGN_PYTHON_SPEC = importlib.util.spec_from_file_location( | ||
| "roi_align_python", | ||
| Path(__file__).resolve().parents[3] / "python/tvm/topi/testing/roi_align_python.py", |
There was a problem hiding this comment.
This import is a bit indirect. Since roi_align_python.py is already under tvm.topi.testing, it would be cleaner to import roi_align_nchw_python directly instead of loading it via importlib and a file path.
There was a problem hiding this comment.
I removed the indirect importlib-based loading from test_op_vision.py.
tests/python/relax/test_op_vision.py
Outdated
| tvm.testing.assert_allclose(result.numpy(), expected, rtol=1e-5, atol=1e-5) | ||
|
|
||
|
|
||
| def test_roi_align_legalize_e2e_aligned(): |
There was a problem hiding this comment.
Since e2e tests add too much CI pressure, I suggest do not include those e2e tests here
There was a problem hiding this comment.
I removed the roi_align e2e tests from test_op_vision.py to keep the op-level test coverage lighter. The end-to-end numerical coverage is still exercised through the ONNX and PyTorch frontend tests, including the aligned path.
|
@tlopex. Thanks for the review. I've addressed the comments by simplifying the op-level tests and cleaning up the frontend handling. |
Summary
Add Relax
roi_alignsupport and wire it through the ONNX and PyTorch frontends.Changes
relax.vision.roi_align, including attrs, Python wrapper, struct info inference, and legalizationroi_aligncompute and keep both legacy and aligned ROIAlign semanticsRoiAlign, includingcoordinate_transformation_modehandling foroutput_half_pixelandhalf_pixeltorchvision.ops.roi_alignin the exported-program frontend, including thealignedflagmin=1.0clampValidation
pytest tests/python/relax/test_op_vision.py -k roi_alignpytest tests/python/relax/test_tvmscript_parser_op_vision.py -k roi_alignpytest tests/python/relax/test_frontend_onnx.py -k roi_alignpytest tests/python/relax/test_frontend_from_exported_program.py -k roi_alignThis PR completes the Relax/ONNX/Torch roi_align work tracked in #18928.