onnxruntime: multiple InferenceSession slowdown inference speed
Describe the bug Multiple InferenceSession slowdown inference speed
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): Linux centos 7
- ONNX Runtime installed from (source or binary): binary
- ONNX Runtime version: 1.10.0
- Python version: python3.6.8
- Visual Studio version (if applicable):
- GCC/Compiler version (if compiling from source):
- CUDA/cuDNN version:
- GPU model and memory:
To Reproduce
use this script, when run_single_sess(), average time is 31ms, when run_two_sess(), average time is 46ms
model is download from https://github.com/onnx/models/tree/master/vision/object_detection_segmentation/tiny-yolov2
cpu info: Intel® Xeon® Platinum 8255C CPU @ 2.50GHz, 10 cores for my docker container
import time
import numpy as np
import onnxruntime as ort
model_path = "ft_local/tinyyolov2-8.onnx"
def run_two_sess():
ort_sess1 = ort.InferenceSession(model_path)
ort_sess2 = ort.InferenceSession(model_path)
warm_up_count = 5
for i in range(warm_up_count):
x = np.random.rand(1, 3, 416, 416).astype("float32")
ort_sess1.run(None, {ort_sess1._inputs_meta[0].name: x})
x = np.random.rand(1, 3, 416, 416).astype("float32")
ort_sess2.run(None, {ort_sess2._inputs_meta[0].name: x})
count = 100
start_total = time.time()
for i in range(count):
x = np.random.rand(1, 3, 416, 416).astype("float32")
start = time.time()
ort_sess1.run(None, {ort_sess1._inputs_meta[0].name: x})
print(f"p1 consumed: {(time.time() - start)*1000}")
x = np.random.rand(1, 3, 416, 416).astype("float32")
start = time.time()
ort_sess2.run(None, {ort_sess2._inputs_meta[0].name: x})
print(f"p2 consumed: {(time.time() - start)*1000}")
end_total = time.time()
print(f"average time: {(end_total - start_total)*1000/count/2}")
def run_single_sess():
ort_sess1 = ort.InferenceSession(model_path)
warm_up_count = 5
for i in range(warm_up_count):
x = np.random.rand(1, 3, 416, 416).astype("float32")
ort_sess1.run(None, {ort_sess1._inputs_meta[0].name: x})
count = 100
start_total = time.time()
for i in range(count):
x = np.random.rand(1, 3, 416, 416).astype("float32")
start = time.time()
ort_sess1.run(None, {ort_sess1._inputs_meta[0].name: x})
print(f"p1 consumed: {(time.time() - start)*1000}")
end_total = time.time()
print(f"average time: {(end_total - start_total)*1000/count}")
if __name__ == "__main__":
run_single_sess()
# run_two_sess
Expected behavior it should cost same time when I use two session, compared with single session
Appreciate for any help
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 15 (6 by maintainers)
You could also try sharing a single threadpool between 2 inference sessions using the global threadpool feature. By default each session has its own separate threadpool. Using 2 processes is better though.