Skip to content

onnxruntime produces invalid results due to the wrong shape inference for the clip operator #24971

Open
@coffezhou

Description

@coffezhou

Describe the issue

For the following onnx model,

Image
the shape of result should be 1x3x1x1. But onnxruntime produces wrong shape of results, 1x3x1x3. The outputs are as follows:

ONNXRuntime:
 [array([[[[0.0192995 , 0.0192995 , 0.0192995 ]],

        [[0.00301902, 0.00301902, 0.00301902]],

        [[0.        , 0.        , 0.        ]]]], dtype=float32)]
shape:
 (1, 3, 1, 3)

According to the specification of clip operator, the results' shape of clip should be identical to that of the its inputs. So, in this issue, it is strange that why the results' shape is 1x3x1x3.

To reproduce

Environment

OS: Ubuntu 20.04
onnxruntime: 1.23.0.dev20250515001
CUDA: cuda-12.2.2::cuda-toolkit
CUDNN: 9.1.1.17
NVIDIA GPU: GeForce RTX 3080
NVIDIA Driver Version: 535.183.01
Python Version: 3.12.9

Steps to reproduce

This bug can be reproduced by the following code with the model in the attachment.

import sys
import os

import numpy as np
import onnx
import onnxruntime

import pickle
            
def main():
    onnx_model = onnx.load("111.onnx")
    
    with open("inputs.pkl", "rb") as fp:
        inputs = pickle.load(fp)
    
    try:
        ort_session = onnxruntime.InferenceSession(
            onnx_model.SerializeToString(), providers=["CPUExecutionProvider"]
        )
        ort_output = ort_session.run([], inputs)
    except Exception as e:
        print(e)
        sys.exit(1)
    
    print("ONNXRuntime:\n", ort_output)    
    print("shape:\n", ort_output[0].shape)  
    
if __name__ == "__main__":
    
    main()   

testcase.zip

Urgency

No response

Platform

Linux

OS Version

Ubuntu 20.04

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.23.0.dev20250515001

ONNX Runtime API

Python

Architecture

X64

Execution Provider

Default CPU

Execution Provider Library Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions