apache / apache/tvm

[Bug] [Relax ONNX front]ONNX frontend import failed with shape graph Concat error

Open
#19,971 4 comments 0 reactions 0 assignees View on GitHub
needs-triage type: bug
Dominant language
Python
Stars
13.7k
Forks
4k
Avg merge
2d 1h
Merged PRs (30d)
112

Description

### 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.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by running the supplied from_onnx reproduction with a model containing the Shape/Gather/Slice/Unsqueeze/Concat/Reshape pattern, then trace the Relax ONNX frontend's Concat conversion. Compare its handling of tensor and shape-graph inputs. Done means dynamic shape operators produce ShapeExpr/PrimValue values and the model imports without the ndim mismatch, while ordinary tensor Concat remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.