Skip to content

Only manually load DLLs if onnxruntime.dll is not already loaded. #1800

Merged
baijumeswani merged 2 commits into
microsoft:mainfrom
chemwolf6922:user/chemwolf6922/allow-winml-loading-its-own-onnxruntime.dll
Jan 20, 2026
Merged

Only manually load DLLs if onnxruntime.dll is not already loaded. #1800
baijumeswani merged 2 commits into
microsoft:mainfrom
chemwolf6922:user/chemwolf6922/allow-winml-loading-its-own-onnxruntime.dll

Conversation

@chemwolf6922
Copy link
Copy Markdown
Contributor

@chemwolf6922 chemwolf6922 commented Sep 30, 2025

Why is this change made

Before this change, when using Windows ML. If the user initialize Windows ML before loading genai, there will be two onnxruntime.dll loaded into the python process, like this:
image
This is because Windows ML will try to load the onnxruntime.dll packed in the Windows App Runtime when it is initialized. But genai will also try to load the dml and ort DLLs packed in the onnxruntime python package. Result in duplicating DLLs.
Since Windows ML is not loading the DLL by path, it won't load the its DLL if genai is loaded first.

What changed

This PR checks if onnxruntime.dll is already loaded before loading the dml and ort DLLs manually. After this change, when running a dml model with Windows ML initialized first, the process' loaded library looks like this:
image
image
When loading WinML after genai, it behaves the same as before. (IMHO, using the DLLs from the Windows App Runtime and dropping the dependency on onnxruntime-winml seems more streamlined.)
image
image

@baijumeswani
Copy link
Copy Markdown
Collaborator

baijumeswani commented Sep 30, 2025

For the winml path, which process/code is responsible for loading onnxruntime.dll?
If the user's python script imports onnxruntime-genai first, would that mean that the onnxruntime.dll will be imported from the python packages instead of from winapp?
Does winapp not use the onnxruntime-winml python package?

@chemwolf6922
Copy link
Copy Markdown
Contributor Author

For the winml path, which process/code is responsible for loading onnxruntime.dll?

The onnxruntime.dll will be loaded when the user creates the winml ep category. Which is the first call to winml.

If the user's python script imports onnxruntime-genai first, would that mean that the onnxruntime.dll will be imported from the python packages instead of from winapp?

Yes, winml won't import a second onnxruntime.dll if the user imports onnxruntime-genai first and loads the onnxruntime.dll from the ort python package.

Does winapp not use the onnxruntime-winml python package?

I'm not sure if any production app is using python winml currently. The user needs to carefully install the onnxruntime-winml that matches the winml/wasdk's version to get the exact onnxruntime.dll as the packed one. Allowing the user to just use the packed one in winml would make this easier and avoid the unnecessary dependency on onnxruntime-winml.

@baijumeswani
Copy link
Copy Markdown
Collaborator

Is there a plan to improve and streamline the Python WinML user experience? It could be worthwhile to explore a design that is more intuitive and user‑friendly.

@chemwolf6922
Copy link
Copy Markdown
Contributor Author

Is there a plan to improve and streamline the Python WinML user experience? It could be worthwhile to explore a design that is more intuitive and user‑friendly.

The major problem with the Python WinML complication is how the onnxruntime python package works. With its inclusion of all the implementation in the .pyd file. I don't think there is much that we can do w/o major changes to that.
I have a toy project that builds an ort python wheel on top of the C interface: https://pypi.org/project/ortpy/ . But I won't expected that to be a formal solution😂

@baijumeswani baijumeswani enabled auto-merge (squash) January 20, 2026 04:55
@baijumeswani baijumeswani merged commit 886d134 into microsoft:main Jan 20, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants