gemini-code-assist[bot] commented on code in PR #19816:
URL: https://github.com/apache/tvm/pull/19816#discussion_r3429053322
##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -4994,15 +4994,29 @@ def _impl_v16(cls, bb, inputs, attr, params):
align_corners = bool(attr.get("align_corners", 0))
- # ONNX grid shape: [N, H_out, W_out, 2]
- # TVM grid shape: [N, 2, H_out, W_out]
- grid = relax.op.permute_dims(grid, [0, 3, 1, 2])
+ if hasattr(data.struct_info, "ndim"):
+ ndim = data.struct_info.ndim
+ else:
+ ndim = len(data.struct_info.shape)
Review Comment:

Instead of manually checking `hasattr(data.struct_info, "ndim")` and falling
back to `len(data.struct_info.shape)`, it is more robust and idiomatic to use
the existing helper function `_get_known_tensor_rank(data)`. This helper is
used throughout the codebase and safely handles various expression types (e.g.,
`relax.Constant`, `relax.ShapeExpr`, `relax.PrimValue`, and
`relax.TensorStructInfo`) while avoiding potential `AttributeError` or
`TypeError` if `struct_info` or `shape` is not fully defined.
```python
ndim = _get_known_tensor_rank(data)
```
##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -4994,15 +4994,29 @@ def _impl_v16(cls, bb, inputs, attr, params):
align_corners = bool(attr.get("align_corners", 0))
- # ONNX grid shape: [N, H_out, W_out, 2]
- # TVM grid shape: [N, 2, H_out, W_out]
- grid = relax.op.permute_dims(grid, [0, 3, 1, 2])
+ if hasattr(data.struct_info, "ndim"):
+ ndim = data.struct_info.ndim
+ else:
+ ndim = len(data.struct_info.shape)
Review Comment:

Directly accessing `data.struct_info` can lead to an `AttributeError` if
`struct_info` is `None`. It is safer and more consistent with the rest of the
codebase to use the helper function `_get_known_tensor_rank(data)` to retrieve
the rank of the input tensor.
```suggestion
ndim = _get_known_tensor_rank(data)
if ndim is None:
raise ValueError("GridSample requires a statically known input
rank.")
```
--
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]