From 782ccff207fa23c43d812f0035e8d621907a2b64 Mon Sep 17 00:00:00 2001 From: Hariharan Seshadri Date: Thu, 10 Sep 2020 16:19:21 -0700 Subject: [PATCH] Add dll probe path so that the right DirectML.dll is loaded while running C# tests (#5104) --- .../InferenceTest.cs | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs b/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs index 13ba9f1787..5df43cc06d 100644 --- a/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs +++ b/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs @@ -97,7 +97,17 @@ namespace Microsoft.ML.OnnxRuntime.Tests opt.AppendExecutionProvider_CUDA(0); #endif #if USE_DML + // Explicitly set dll probe path so that the (potentially) stale system DirectML.dll + // doesn't get loaded by the test process when it is eventually delay loaded by onnruntime.dll + // The managed tests binary path already contains the right DirectML.dll, so use that + + var directml_dll_path = AppDomain.CurrentDomain.BaseDirectory; + SetDllDirectory(directml_dll_path); opt.AppendExecutionProvider_DML(0); + + // Restore the default dll search order + SetDllDirectory(null); + #endif #if USE_NGRAPH opt.AppendExecutionProvider_NGraph("CPU"); //TODO: this API should be refined @@ -119,6 +129,13 @@ namespace Microsoft.ML.OnnxRuntime.Tests } } + // Use to set dll probe path so that the right dll(s) is loaded by the test process + // Invoke only to specify Windows specific EPs' dll locations explicitly + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + + [return: MarshalAs(UnmanagedType.Bool)] + static extern bool SetDllDirectory(string lpPathName); + [Fact] public void TestRunOptions() { @@ -1594,7 +1611,7 @@ namespace Microsoft.ML.OnnxRuntime.Tests Assert.Equal(2, modelMetadata.CustomMetadataMap.Keys.Count); Assert.Equal("dummy_value", modelMetadata.CustomMetadataMap["dummy_key"]); - Assert.Equal("{\"session_options\": {\"inter_op_num_threads\": 5, \"intra_op_num_threads\": 2, \"graph_optimization_level\": 99, \"enable_profiling\": 1}}", + Assert.Equal("{\"session_options\": {\"inter_op_num_threads\": 5, \"intra_op_num_threads\": 2, \"graph_optimization_level\": 99, \"enable_profiling\": 1}}", modelMetadata.CustomMetadataMap["ort_config"]); @@ -2119,6 +2136,13 @@ namespace Microsoft.ML.OnnxRuntime.Tests { string modelPath = Path.Combine(Directory.GetCurrentDirectory(), "squeezenet.onnx"); #if USE_DML + // Explicitly set dll probe path so that the (potentially) stale system DirectML.dll + // doesn't get loaded by the test process when it is eventually delay loaded by onnruntime.dll + // The managed tests binary path already contains the right DirectML.dll, so use that + + var directml_dll_path = AppDomain.CurrentDomain.BaseDirectory; + SetDllDirectory(directml_dll_path); + using (var option = new SessionOptions()) { if (!deviceId.HasValue) @@ -2130,6 +2154,9 @@ namespace Microsoft.ML.OnnxRuntime.Tests { option.AppendExecutionProvider_DML(deviceId.Value); } + + // Restore the default dll search order + SetDllDirectory(null); #elif USE_CUDA using (var option = (deviceId.HasValue) ? SessionOptions.MakeSessionOptionWithCudaProvider(deviceId.Value) : @@ -2140,7 +2167,7 @@ namespace Microsoft.ML.OnnxRuntime.Tests option.AppendExecutionProvider_CPU(1); } #else - using (var option = new SessionOptions()) + using (var option = new SessionOptions()) { option.AppendExecutionProvider_CPU(1); #endif @@ -2215,7 +2242,7 @@ namespace Microsoft.ML.OnnxRuntime.Tests public DisposableListTest() { } public DisposableListTest(int count) : base(count) { } -#region IDisposable Support + #region IDisposable Support private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) @@ -2248,6 +2275,6 @@ namespace Microsoft.ML.OnnxRuntime.Tests Dispose(true); GC.SuppressFinalize(this); } -#endregion + #endregion } }