onnxruntime: Broken multithreading inference session Onnxruntime-directml >= 1.18
Describe the issue
With the new version 1.18 it seems that trying to use different InferenceSession using the same DirectML device, all threads remain stalled without giving any exception or error
To reproduce
Thread 1
AI_model_loaded = onnx_load(AI_model_path)
AI_model = onnxruntime_inferenceSession(
path_or_bytes = AI_model_loaded.SerializeToString(),
providers = [('DmlExecutionProvider', {"device_id": "0"})]
)
onnx_input = {AI_model.get_inputs()[0].name: image}
onnx_output = AI_model.run(None, onnx_input)[0]
Thread n (where n can be any number)
AI_model_loaded = onnx_load(AI_model_path)
AI_model = onnxruntime_inferenceSession(
path_or_bytes = AI_model_loaded.SerializeToString(),
providers = [('DmlExecutionProvider', {"device_id": "0"})]
)
onnx_input = {AI_model.get_inputs()[0].name: image}
onnx_output = AI_model.run(None, onnx_input)[0]
Urgency
No response
Platform
Windows
OS Version
10
ONNX Runtime Installation
Released Package
ONNX Runtime Version or Commit ID
1.18.0
ONNX Runtime API
Python
Architecture
X64
Execution Provider
DirectML
Execution Provider Library Version
1.18.0
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 1
- Comments: 31 (4 by maintainers)
Hi, tested 1.21.0 Release Candidates #23885
the problem is still present, please fix the problem, is almost a year 😦
Applying stale label due to no activity in 30 days
@skottmckay onnxruntime 1.22 release and the problem is still present
@skottmckay Could you share your thoughts on this? It’s been preventing projects from being updated for over a year.
Tested the RC diretcml 1.20.0.dev20241022005
the problem has not been solved, in fact, it have gotten worse and now with 1.20 the gpu driver crash
This problem is significant, so most of us will remain on version 1.17.3. Please fix it.
Hi @henryruhs
Thank you. I tried the solution using Semaphore and it works, but the performance is in line with using only one Thread.
Hopefully they will fix the problem with the next release.
Not sure if this helps, but I have this method to work around it.
Sorry, but implement
has_execution_provideryourself 😃Same here on Windows, versions 1.16.0 to 1.17.3 work fine over multiple threads, however 1.18.0 gives
Windows fatal exception: access violationwith the following stack trace produced by my own Windows SEH handler:Sorry - I’m not a DML expert. @fdwr any ideas what changed between 1.17.3 and 1.18 that might cause this?
Hi @PatriceVignola @smk2007 @fdwr @liuyunms
There are some news about this issue?
@henryruhs Sorry, I don’t have a patch to provide. I have been a user of the python bindings for a while. The only real info I have to go on for the true cause is the comment by @liuyunms:
I have written my own pybind11 wrapper using the C++ API, which appears to run without issue, and also seems to run with better parallelism – I guess I’m releasing the GIL for longer?
Here’s the rough bit of code that does the job for me, until the official python wrapper is fixed: https://gist.github.com/saulthu/c60a8f1f10352e98a986e57205cedd49
I’m forced to use my own C++ Pybind11 wrapper since this official Python wrapper is broken for multithreading. 😟
Edit: Here’s the rough bit of code that does the job for me, until the official python wrapper is fixed: https://gist.github.com/saulthu/c60a8f1f10352e98a986e57205cedd49
yeah, the performance hit is something I am aware of.
@zhangxiang1993
Hi, I can confirm that the problem is also present on 1.19 nightly (python 3.11)
@PatriceVignola @smk2007 @fdwr
Hi, sorry to bother, there are some news for this problem? Actually testing 1.18.1 and the problem is still present 😦
Thank you
We’ve noted the issue with GPU resource contention due to multiple threads. This usage pattern is not recommended as it makes multiple threads request all of the GPU resources, and can cause contention. Also, the allocator in python API (both CUDA and DML) is explicitly not thread safe because it initializes the allocator as a global singleton due it living outside of the session.
We’re investigating the recent failure and will address it. Meanwhile, please avoid this pattern to prevent GPU contention.