Skip to content

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

Description

@Shirley4042

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:

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triagePRs or issues that need to be investigated by maintainers to find the right assignees to address ittype: bug

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions