gemini-code-assist[bot] commented on code in PR #19566:
URL: https://github.com/apache/tvm/pull/19566#discussion_r3242150283
##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -526,8 +538,25 @@ class Div(BinaryBase):
@classmethod
def _impl_v7(cls, bb, inputs, attr, params):
- return cls.base_impl(bb, inputs, attr, params)
+ lhs_dtype = inputs[0].struct_info.dtype
+ rhs_dtype = inputs[1].struct_info.dtype
+
+ try:
+ lhs_code = DataType(lhs_dtype).type_code
+ rhs_code = DataType(rhs_dtype).type_code
+ except (ValueError, TypeError, TVMError):
+ return cls.base_impl(bb, inputs, attr, params)
Review Comment:

Accessing `struct_info.dtype` directly on `inputs[0]` and `inputs[1]` is
risky as `struct_info` could be `None` or a non-tensor type (e.g.,
`TupleStructInfo` or `ShapeStructInfo`), which would raise an `AttributeError`.
These accesses should be moved inside the `try` block, and `AttributeError`
should be added to the caught exceptions to ensure a safe fallback to
`base_impl` if metadata is missing or unexpected.
```suggestion
try:
lhs_code = DataType(inputs[0].struct_info.dtype).type_code
rhs_code = DataType(inputs[1].struct_info.dtype).type_code
except (AttributeError, ValueError, TypeError, TVMError):
return cls.base_impl(bb, inputs, attr, params)
```
##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -72,6 +72,18 @@ def _relax_dtype_is_floating_point(dtype: str) -> bool:
)
+def _const_integer_expr_has_zero(expr: relax.Expr) -> bool | None:
+ """Return whether a constant integer expression contains a zero value.
+
+ Returns None when expression is not statically inspectable.
+ """
+
+ if isinstance(expr, relax.Constant):
+ return bool(_np.any(expr.data.numpy() == 0))
Review Comment:

The helper function `_const_integer_expr_has_zero` should also handle
`relax.PrimValue` expressions, as they can also represent constant integer
divisors in Relax. This ensures that scalar constant zero divisors are also
caught during import validation.
```suggestion
if isinstance(expr, relax.Constant):
return bool(_np.any(expr.data.numpy() == 0))
if isinstance(expr, relax.PrimValue) and isinstance(expr.value,
tirx.IntImm):
return int(expr.value.value) == 0
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]