Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions python/tvm/relax/frontend/onnx/onnx_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -4135,6 +4135,19 @@ class Flatten(OnnxOpConverter):
def _impl_v13(cls, bb, inputs, attr, params):
axis = attr.get("axis", 1)
data_shape = list(inputs[0].ty.shape)
rank = len(data_shape)

# ONNX Flatten spec: "The value for axis must be in the range [-r, r], where r
# is the rank of the input tensor. Negative value means counting dimensions from
# the back." Normalize negative axis and validate the range, matching onnxruntime
# which rejects out-of-range axis with a ShapeInferenceError.
if axis < 0:
axis += rank
if not 0 <= axis <= rank:
raise ValueError(
f"Flatten axis {attr.get('axis', 1)} is out of range [-{rank}, {rank}] "
f"for an input of rank {rank}"
)

if axis == 0:
new_shape = (1, -1)
Expand Down