Only manually load DLLs if onnxruntime.dll is not already loaded. #1800
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:

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:




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.)