onnxruntime: ONNXRuntime Using GPU Slower Than Using CPU?

Describe the bug A clear and concise description of what the bug is.

I have been using ONNXRuntime for a while and I found the ONNXRuntime GPU instance is usually 5-200 times slower than the CPU instance. I just wonder what is the reason behind this, or there is something wrong with my implementation.

The CPU is just an Intel E5 CPU and the GPU is a V100 card.

Urgency If there are particular important use cases blocked by this or strict project-related timelines, please share more information and dates. If there are no hard deadlines, please specify none.

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): 18.04.3
  • ONNX Runtime installed from (source or binary): binary
  • ONNX Runtime version: 1.0.0
  • Python version: 3.6.8
  • Visual Studio version (if applicable):
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version: 10.0/7.6.3
  • GPU model and memory: V100

To Reproduce Describe steps/code to reproduce the behavior:

I am using the official model logreg_iris.onnx as an example.

import onnxruntime
import numpy as np
import time
from onnxruntime.datasets import get_example

example_model = get_example("logreg_iris.onnx")
sess = onnxruntime.InferenceSession(example_model)

input_name = sess.get_inputs()[0].name
input_shape = sess.get_inputs()[0].shape
input_type = sess.get_inputs()[0].type

print(input_name, input_shape, input_type)

output_name = sess.get_outputs()[0].name
output_shape = sess.get_outputs()[0].shape
output_type = sess.get_outputs()[0].type

print(output_name, output_shape, output_type)

x = np.random.random(input_shape)
x = x.astype(np.float32)

print(sess.get_providers())

sess.set_providers(['CPUExecutionProvider'])
start_time = time.time()
for i in range(1000):
    result = sess.run([output_name], {input_name: x})
end_time = time.time()
time_elapsed = end_time - start_time
print(time_elapsed)


sess.set_providers(['CUDAExecutionProvider'])
start_time = time.time()
for i in range(1000):
    result = sess.run([output_name], {input_name: x})
end_time = time.time()
time_elapsed = end_time - start_time
print(time_elapsed)

Got the following outputs:

root@79d922d8c004:/mnt# python onnx_test.py 
float_input [3, 2] tensor(float)
label [3] tensor(int64)
['CUDAExecutionProvider', 'CPUExecutionProvider']
0.011708498001098633
1.7355647087097168

Expected behavior A clear and concise description of what you expected to happen.

I expect the GPU instance should be faster than CPU instance. But it is not. Is it due to memory copy between host and device?

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Add any other context about the problem here. If the issue is about a particular model, please share the model details as well to facilitate debugging.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

A few notes:

  1. The model only contains traditional ML operators, and there are no CUDA implementations of those ops.
  2. If you add an initial call to sess.Run prior to doing the timing the CPU and CUDA times are much closer, so there’s some work in the first call that is cached.
  3. There’s a TODO in the code about not calling a per-Run initialization method on unused execution providers. That’s the cause of the CUDA run being slower as that (unnecessary) setup is expensive relative to the extremely small model which is taking less than a millisecond in total to run.

So for this model, as none of the nodes can run on CUDA, adding the CUDA execution provider adds some overhead with no benefit from doing so, and that overhead is significant given the very small model.

I had the same issue while trying to build yolov5 onnxruntime on Nvidia Jetson Xaviernx. But turns out that the detection is slow only for the first frame, so while running the detection on further frames, it works 5 times faster than CPU detection.

If you set the session log severity level to VERBOSE, it’ll print which nodes were assigned to GPU and which ones to CPU. Let us know what this prints. It might explain the behavior. We don’t have CUDA implementations for all ops.