Skip to content

ONNXRuntime Optimization Causes Output Discrepancy with Certain opt_level Settings #23210

@Thrsu

Description

@Thrsu

Describe the issue

When running inference on an ONNX model with ONNXRuntime, discrepancies are observed between the original and optimized outputs. The issue occurs when using optimization levels 0, 2, or 99, but not with opt_level=1. The discrepancy is observed only in the output of all the outputs of the model.

  • Actual Behavior
AssertionError: 
Not equal to tolerance rtol=0.001, atol=0.001

Mismatched elements: 64 / 3630 (1.76%)
Max absolute difference: 0.00350001
Max relative difference: 0.33333337
 x: array([-0.3605, -0.273 , -0.119 , ...,  0.42  ,  0.42  ,  0.42  ],
      dtype=float32)
 y: array([-0.3605, -0.273 , -0.119 , ...,  0.42  ,  0.42  ,  0.42  ],
      dtype=float32)

To reproduce

  1. Download the model
  2. Run the script:
import onnx
import onnxruntime as ort
import numpy as np
from onnxruntime.transformers import optimizer

model_path = "inconsis7.onnx"
optimized_model_path = f"./opt.onnx"
sess_options = ort.SessionOptions()
sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_DISABLE_ALL
this_provider_list = ort.get_available_providers()

original_session = ort.InferenceSession(model_path, sess_options, providers=this_provider_list)
input_data = {"v0_0": np.random.rand(1, 56, 32, 1).astype(np.float16), "v10_0": np.random.rand(1, 56, 32, 1).astype(np.float16)}
output_names = [output.name for output in original_session.get_outputs()]
original_result = original_session.run(output_names, input_data)

optimized_model = optimizer.optimize_model(model_path, opt_level=0, use_gpu=True)
optimized_model.save_model_to_file(optimized_model_path)
optimized_session = ort.InferenceSession(optimized_model_path, providers=this_provider_list)
optimized_result = optimized_session.run(output_names, input_data)
for r1, r2 in zip(original_result, optimized_result):
    np.testing.assert_allclose(r1, r2, atol=1e-3, rtol=1e-3)

Urgency

No response

Platform

Linux

OS Version

Ubuntu 20.04

ONNX Runtime Installation

Built from Source

ONNX Runtime Version or Commit ID

5c1b7cc

ONNX Runtime API

Python

Architecture

X64

Execution Provider

CUDA

Execution Provider Library Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    model:transformerissues related to a transformer model: BERT, GPT2, Hugging Face, Longformer, T5, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions