[Relax][ONNX] Add GridSample ONNX frontend integration#18932
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 full support for the ONNX 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 GridSample ONNX operator conversion to Relax, supporting various modes, padding, and alignment options. It also includes new parameterized tests to ensure correctness. The review suggests enhancing ONNX spec compliance by explicitly handling the "linear" mode for GridSample and adding a corresponding test case. Additionally, a redundant test for default attributes was identified and recommended for removal.
| method = attr.get("mode", b"bilinear") | ||
| if isinstance(method, bytes): | ||
| method = method.decode("ascii") |
There was a problem hiding this comment.
The ONNX GridSample spec defines "linear" as a valid mode, which is equivalent to "bilinear" for 4-D inputs. The current implementation does not handle this case and would pass "linear" to relax.op.image.grid_sample, which does not support it.
To improve ONNX spec compliance, I suggest adding a translation from "linear" to "bilinear". This will make the converter more robust for models that use mode="linear".
You've correctly noted the discrepancy between the ONNX spec and onnxruntime's behavior regarding the default mode. Keeping "bilinear" as the default in attr.get is a reasonable pragmatic choice, but handling explicit "linear" is important for broader model support.
| method = attr.get("mode", b"bilinear") | |
| if isinstance(method, bytes): | |
| method = method.decode("ascii") | |
| method = attr.get("mode", b"bilinear") | |
| if isinstance(method, bytes): | |
| method = method.decode("ascii") | |
| if method == "linear": | |
| method = "bilinear" |
| tvm_selected[:min_rows], ort_selected[:min_rows], rtol=1e-5, atol=1e-5 | ||
| ) | ||
|
|
||
| @pytest.mark.parametrize("mode", ["bilinear", "nearest"]) |
There was a problem hiding this comment.
To test the recommended handling of mode="linear", please add it to the parameterization for this test. This will ensure the frontend correctly maps "linear" to "bilinear" as per the ONNX spec for 4D inputs.
| @pytest.mark.parametrize("mode", ["bilinear", "nearest"]) | |
| @pytest.mark.parametrize("mode", ["bilinear", "nearest", "linear"]) |
| def test_grid_sample_defaults(): | ||
| """Test GridSample with explicit default attributes to verify correct handling: | ||
| mode defaults to 'bilinear', padding_mode defaults to 'zeros', | ||
| align_corners defaults to 0. | ||
| """ | ||
| # Only testing 2D (NCHW) as that's what TVM currently supports | ||
| x_shape = [1, 3, 4, 4] | ||
| grid_shape = [1, 2, 2, 2] | ||
| out_shape = [x_shape[0], x_shape[1], grid_shape[1], grid_shape[2]] | ||
|
|
||
| node = helper.make_node( | ||
| "GridSample", | ||
| inputs=["X", "grid"], | ||
| outputs=["Y"], | ||
| mode="bilinear", | ||
| padding_mode="zeros", | ||
| align_corners=0, | ||
| ) | ||
|
|
||
| graph = helper.make_graph( | ||
| [node], | ||
| "grid_sample_defaults_test", | ||
| inputs=[ | ||
| helper.make_tensor_value_info("X", TensorProto.FLOAT, x_shape), | ||
| helper.make_tensor_value_info("grid", TensorProto.FLOAT, grid_shape), | ||
| ], | ||
| outputs=[ | ||
| helper.make_tensor_value_info("Y", TensorProto.FLOAT, out_shape), | ||
| ], | ||
| ) | ||
|
|
||
| grid_data = np.random.uniform(-1, 1, grid_shape).astype("float32") | ||
| x_data = np.random.uniform(-1, 1, x_shape).astype("float32") | ||
|
|
||
| model = helper.make_model(graph, producer_name="grid_sample_defaults_test") | ||
| check_correctness( | ||
| model, | ||
| inputs={"grid": grid_data, "X": x_data}, | ||
| opset=16, | ||
| ) |
There was a problem hiding this comment.
This test case for default attributes is redundant. The parameter combination it tests (mode='bilinear', padding_mode='zeros', align_corners=0) is already covered by the parameterized test_grid_sample.
To avoid test duplication and improve test suite conciseness, I recommend removing this test function.
Signed-off-by: OmarAzizi <oalazizi75@gmail.com>
|
Summary
GridSamplefrontend integration for Relax, which was previously commented out in the converter map.GridSampleconverter class that handles ONNX→TVM grid shape transpose ([N, H_out, W_out, 2]→[N, 2, H_out, W_out]) and mode/padding attribute mapping.grid_sampleRelax op (relax.op.image.grid_sample), which already exists, keeping the change minimal and focused on the frontend layer.Closes part of #18928
Notes for Maintainers
The ONNX spec defines the default mode as
"linear", but onnxruntime only accepts"bilinear". I've set the converter default to"bilinear"— happy to add a"linear"→"bilinear"translation if needed for spec compliance. (Edit: This was addressed)Test Plan
python3 -m pytest -q tests/python/relax/test_frontend_onnx.py -k 'grid_sample'