lih403474-byte opened a new issue, #19971:
URL: https://github.com/apache/tvm/issues/19971
### Expected behavior
The ONNX model should be successfully imported by the TVM Relax ONNX
frontend.
The model contains dynamic shape calculation subgraphs before `Reshape`.
The shape graph operators, such as:
- `Shape`
- `Gather`
- `Slice`
- `Unsqueeze`
- `Concat`
should be converted into Relax `ShapeExpr` / `PrimValue` instead of normal
tensor operations.
---
### Actual behavior
TVM Relax ONNX frontend fails during import.
Error:
```
InternalError:
Concat expects all input tensors to have same ndim.
However, the input contains tensors with ndim 3 and 2
```
The failure happens during `Concat` conversion.
The problematic `Concat` belongs to a dynamic shape construction subgraph,
but it is handled as a normal tensor concatenation operation.
---
### Environment
- TVM: `0.25.dev0` (local source tree)
- PyTorch: `2.12.0+cu130`
- Python: `3.10.20`
- OS: `Ubuntu 22.04 64-bit`
- Target: `llvm` (CPU)
- Frontend: from tvm.relax.frontend.onnx import from_onnx
---
### Steps to reproduce
Load an ONNX model containing a dynamic reshape pattern:
```python
import onnx
from tvm.relax.frontend.onnx import from_onnx
model = onnx.load("model.onnx")
mod = from_onnx(model)
```
The import fails during `Concat` conversion:
```python
InternalError(
'Concat expects all input tensors to have same ndim.
However, the input contains tensors with ndim 3 and 2'
)
```
---
### Analysis
The failure occurs for ONNX graphs containing dynamic reshape patterns.
Example pattern:
```
Input tensor
|
Shape
|
Gather / Slice
|
Unsqueeze
|
Concat
|
Reshape
```
The ONNX graph generates the target shape of `Reshape` dynamically.
These intermediate operators do not perform tensor computation. They only
construct the shape argument required by `Reshape`.
The expected Relax representation is:
```
ShapeExpr
|
PrimValue
|
ShapeExpr
|
Concat
|
ShapeExpr
```
The current issue is that the shape graph `Concat` is incorrectly handled as
tensor `Concat`, causing the dimension mismatch error.
---
### Root cause
The same ONNX operators can appear in both tensor computation graphs and
shape computation graphs.
Tensor graph example:
```
Conv
|
Concat
|
Relu
```
Shape graph example:
```
Shape
|
Gather
|
Unsqueeze
|
Concat
|
Reshape
```
The frontend needs data-flow based detection to distinguish these two cases.
--
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]