onnxruntime: [Performance] Celu becomes 4X slower after update to Eigen 3.4

Describe the issue

When running inference on the ONNX model “poc.onnx” using ONNX Runtime, there is a 30% increase in inference time between version 1.14.0 and version 1.17.3.

OS: Ubuntu 20.04.6 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Clang version: 12.0.0 (https://github.com/llvm/llvm-project.git d928dfc6f924f7e8436dcb5bcc51285fd03e869c)
CMake version: version 3.29.2
Libc version: glibc-2.31

Python version: 3.8.10 (default, Nov 22 2023, 10:22:35)  [GCC 9.4.0] (64-bit runtime)
Python platform: Linux-5.4.0-174-generic-x86_64-with-glibc2.29
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Byte Order:                         Little Endian
Address sizes:                      46 bits physical, 48 bits virtual
CPU(s):                             48

To reproduce

poc.onnx.zip

  1. Download “poc.onnx”.
  2. Run the following script.
import time
import onnxruntime
import numpy as np

onnx_model_path = 'poc.onnx'

ort_session = onnxruntime.InferenceSession(onnx_model_path, providers=['CPUExecutionProvider'])
ort_session.get_modelmeta()
inputs = ort_session.get_inputs()

nth = 1000
start = time.time()
for _ in range(nth):
    np_input = np.random.randn(1, 3, 224, 224).astype('f')
    input_ = {inputs[0].name: np_input}
    ort_session.run(None, input_)
end = time.time()

print ('time:', (end - start) / nth)
[1.14.0] time: 0.011553843975067
[1.17.3] time: 0.014802978515625

Urgency

No response

Platform

Linux

OS Version

5.4.0-174-generic-x86_64

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.17.3

ONNX Runtime API

Python

Architecture

X64

Execution Provider

Default CPU

Execution Provider Library Version

No response

Model File

No response

Is this a quantized model?

No

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 19 (12 by maintainers)

Most upvoted comments

@SuhwanSong , I think your benchmark result tells us that the problem is solely in Eigen. We should be able to create a simpler benchmark test case without using ORT code and without using multi threads, and we should be able to see the regression. Then we should report it to Eigen community.

The version of eigen from (good) is 3.3.9.

I created a new PR from the lates main branch with this version and see if it would still work and performance improves.

I found a performance regression (1.7x slower) between the following commits:

The regression seems related to the update to Eigen 3.4 and the removal of Eigen as a git submodule in commit 1a73d61.

Regarding Eigen version 3.4.0, it was needed for c++20 standard. Thus we should keep it as is.

I conducted further tests to analyze CELU performance regressions. Below are the results and testing methodology:

tested two adjacent commits: 7c58d013aa3715f5421f03f90690982625fdae6e and 1a73d618297c080cafff0a67b85c7829a8e629ad

Input Size Baseline (ms) Current (ms) Overhead (%)
1,000 121.52 135.63 111.61
10,000 420.38 551.37 131.16
20,000 750.26 1,014.70 135.25
40,000 1,521.47 2,000.15 131.46
80,000 2,754.10 3,794.72 137.78
160,000 1,567.11 1,923.50 122.74
320,000 2,954.99 4,237.15 143.39
640,000 7,502.74 7,864.26 104.82
1,280,000 13,020.44 14,150.55 108.68
2,560,000 26,198.54 28,384.23 108.34
5,120,000 49,519.31 52,275.60 105.57

Testing Methodology

I created an ONNX model for CELU using the following script:

import onnx
import numpy as np
from onnx import helper, TensorProto
import onnxruntime
import time

def create_celu_onnx_model(input_size, alpha=1.0, model_path="celu_model.onnx"):
    input_tensor = helper.make_tensor_value_info("input", TensorProto.FLOAT, input_size)
    output_tensor = helper.make_tensor_value_info("output", TensorProto.FLOAT, input_size)
    celu_node = helper.make_node("Celu", inputs=["input"], outputs=["output"], alpha=alpha)
    graph = helper.make_graph(nodes=[celu_node], name="CeluGraph", inputs=[input_tensor], outputs=[output_tensor])
    model = helper.make_model(graph, producer_name="celu_example")
    onnx.save(model, model_path)


def test(model_path, input_data, iter_count):
    session = onnxruntime.InferenceSession(model_path, providers=["CPUExecutionProvider"])
    input_name = session.get_inputs()[0].name
    output_name = session.get_outputs()[0].name
    times = []
    for _ in range(iter_count):
        start = time.time()
        session.run([output_name], {input_name: input_data})
        times.append(time.time() - start)
    return {"inference_time": sum(times) / iter_count * 1e6}

print(f'VERSION: {onnxruntime.__version__}')

input_sizes = [1000, 10000, 20000, 40000, 80000, 160000, 320000, 640000, 1280000, 2560000, 5120000]
for input_size in input_sizes:
    create_celu_onnx_model([input_size], model_path=f"celu_model.onnx")
    random_input = np.random.randn(input_size).astype(np.float32)
    result = test(f"celu_model.onnx", random_input, 10000)
    print(f"Input size: {input_size}, Inference time: {result['inference_time']:.2f}ms")

I will use Sigmoid as an example. Now the cost is set to 2.

When I ran it on an Azure machine with an AMD 4th Gen CPU, 64 vCPUs, I got the following result:

-----------------------------------------------------------------------
Benchmark                             Time             CPU   Iterations
-----------------------------------------------------------------------
BM_Sigmoid/100/real_time           91.1 ns         89.9 ns      6986624
BM_Sigmoid/1000/real_time           328 ns          328 ns      2107320
BM_Sigmoid/10000/real_time         2787 ns         2786 ns       247753
BM_Sigmoid/20000/real_time         5598 ns         5491 ns       113177
BM_Sigmoid/40000/real_time        10918 ns        10916 ns        59415
BM_Sigmoid/80000/real_time         8312 ns         8309 ns        69647
BM_Sigmoid/160000/real_time        9342 ns         9339 ns        67763
BM_Sigmoid/320000/real_time       21399 ns        21173 ns        31647
BM_Sigmoid/640000/real_time       40144 ns        40131 ns        17556
BM_Sigmoid/1280000/real_time      58210 ns        50799 ns        10000

When the input size was 1000, it ran in single thread and took about 328 ns. So when the input size increased to 10000, it took 2787 ns, roughly linear. The situation continues until the input was 40000. Then, you see when the input size is 80000, it actually took less than input size = 40000, it was because multithreading kicked in . So, what if we make it more aggressive? We can achieve it by decrease the cost to 1.

Then I got this:

-----------------------------------------------------------------------
Benchmark                             Time             CPU   Iterations
-----------------------------------------------------------------------
BM_Sigmoid/100/real_time           89.5 ns         89.4 ns      6993203
BM_Sigmoid/1000/real_time           332 ns          327 ns      2070997
BM_Sigmoid/10000/real_time         2850 ns         2783 ns       237048
BM_Sigmoid/20000/real_time         5582 ns         5503 ns       101808
BM_Sigmoid/40000/real_time        10906 ns        10905 ns        59913
BM_Sigmoid/80000/real_time        21862 ns        21861 ns        30587
BM_Sigmoid/160000/real_time       11291 ns        11288 ns        60564
BM_Sigmoid/320000/real_time       19114 ns        19108 ns        39017
BM_Sigmoid/640000/real_time       28067 ns        28065 ns        26146
BM_Sigmoid/1280000/real_time      50173 ns        50161 ns        14089

The result stayed the same when input size <= 40000 or input size>=1280000. And, for the others the new one was not really better.

And, here is cost = 4.0f;

-----------------------------------------------------------------------
Benchmark                             Time             CPU   Iterations
-----------------------------------------------------------------------
BM_Sigmoid/100/real_time           89.6 ns         89.6 ns      6992836
BM_Sigmoid/1000/real_time           328 ns          328 ns      2082535
BM_Sigmoid/10000/real_time         2788 ns         2787 ns       247701
BM_Sigmoid/20000/real_time         5550 ns         5547 ns       114148
BM_Sigmoid/40000/real_time         7972 ns         7852 ns       126907
BM_Sigmoid/80000/real_time         6438 ns         6437 ns        92929
BM_Sigmoid/160000/real_time       14342 ns        14172 ns        52282
BM_Sigmoid/320000/real_time       29809 ns        29712 ns        24899
BM_Sigmoid/640000/real_time       46220 ns        46202 ns        15135
BM_Sigmoid/1280000/real_time      63565 ns        63491 ns         9781

So, back to your question, in your case, what’s the avg input size for this operator? We may ran such benchmarks to see how it works. And we can compare the results with different eigen versions. These numbers are more convincing than the numbers from model tests(which contains a lot more extra things).

I found a performance regression (1.7x slower) between the following commits:

  • 7c58d013aa3715f5421f03f90690982625fdae6e (good)
  • 1a73d618297c080cafff0a67b85c7829a8e629ad (first bad commit)

The regression seems related to the update to Eigen 3.4 and the removal of Eigen as a git submodule in commit 1a73d618297c080cafff0a67b85c7829a8e629ad.