[Relax][ONNX] Add Optional and MatMulInteger16 frontend support#18950
[Relax][ONNX] Add Optional and MatMulInteger16 frontend support#18950Aharrypotter wants to merge 6 commits intoapache:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds support for the MatMulInteger16, Optional, OptionalHasElement, and OptionalGetElement ONNX operators to the Relax frontend. It introduces a sentinel object for empty optionals, updates attribute parsing to handle type protocols, and includes comprehensive unit tests for the new functionality. Feedback suggests tightening input validation for OptionalHasElement to strictly adhere to the ONNX specification and improving test robustness by programmatically inspecting the Relax IR structure instead of relying on string matching.
| tvm_model = from_onnx(model, opset=18, keep_params_in_input=True) | ||
|
|
||
| assert 'R.const(False, "bool")' in str(tvm_model) |
There was a problem hiding this comment.
This test can be made more robust by inspecting the Relax IR programmatically instead of using a string search. This ensures that the structure is exactly as expected and is not sensitive to formatting changes in the string representation of the IR.
| tvm_model = from_onnx(model, opset=18, keep_params_in_input=True) | |
| assert 'R.const(False, "bool")' in str(tvm_model) | |
| tvm_model = from_onnx(model, opset=18, keep_params_in_input=True) | |
| main_func = tvm_model["main"] | |
| assert isinstance(main_func.body, relax.Block) | |
| assert len(main_func.body.bindings) == 1 | |
| binding = main_func.body.bindings[0] | |
| assert isinstance(binding, relax.VarBinding) | |
| assert isinstance(binding.value, relax.Constant) | |
| assert not binding.value.data.numpy() | |
| assert binding.value.struct_info.dtype == "bool" |
There was a problem hiding this comment.
Addressed. I replaced the string-based IR assertion with programmatic Relax IR inspection so the test now checks the expected constant/binding structure directly and is no longer sensitive to textual IR formatting changes.
|
PTAL @tlopex |
tlopex
left a comment
There was a problem hiding this comment.
Some issues found:
-
Please add a guard in
from_onnxfor empty Optional graph outputs. Right now, if a graph output is an empty Optional,outputscan contain an_EmptyOptionalobject instead of arelax.Expr, andemit_outputwill fail with an opaque internal error. It would be better to raise a clear user-facing error here. -
The
_is_empty_optionalbranch for the"Optional"op looks dead right now, since"Optional"is already handled earlier viareturn_tuple_ops. Please consider keeping only one mechanism here — either remove"Optional"fromreturn_tuple_opsand let_is_empty_optionalhandle it, or remove the dead branch.
a99caf0 to
456e244
Compare
456e244 to
e18e018
Compare
|
Addressed, thanks for the suggestions. I added an explicit guard in I also removed the redundant Additionally, I added a regression test ( cc @tlopex |
Summary
This PR adds Relax ONNX frontend support for:
OptionalOptionalHasElementOptionalGetElementMatMulInteger16from thecom.microsoftdomainThe implementation follows existing TVM ONNX frontend patterns and keeps Optional handling explicit through an empty-Optional sentinel during import.
Changes
Optional,OptionalHasElement, andOptionalGetElementMatMulInteger16TYPE_PROTOMatMulInteger16in the ONNX converter mapTests
Added or updated tests in
tests/python/relax/test_frontend_onnx.pyto cover:MatMulInteger16MatMulInteger16MatMulInteger16OptionalHasElementtypeattribute rejection for emptyOptionalOptionalGetElementrejectionValidation
Validated with:
python -m ruff check python/tvm/relax/frontend/onnx/onnx_frontend.py tests/python/relax/test_frontend_onnx.pypython -m pytest -n 1 tests/python/relax/test_frontend_onnx.py -k "optional or matmulinteger16" -vResult:
13 passedThis PR completes the ONNX
MatMulInteger16andOptionalwork tracked in #18945.