Fix test breaks in Windows ingestion pipeline (#6476)

* fix various build breaks with Windows build

* fix runtime errors loading libraries from system32

* add build_inbox check to winml_test_common

* use raw string

* cleanup

* fix dll load

Co-authored-by: Sheil Kumar <sheilk@microsoft.com>
This commit is contained in:
Sheil Kumar 2021-01-28 14:37:15 -08:00 committed by GitHub
parent 00afd00059
commit ea2b560055
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 58 deletions

View file

@ -171,6 +171,9 @@ file(GLOB winml_test_common_src CONFIGURE_DEPENDS
"${WINML_TEST_SRC_DIR}/common/*.cpp")
add_library(winml_test_common STATIC ${winml_test_common_src})
target_compile_options(winml_test_common PRIVATE /wd5205) # workaround cppwinrt SDK bug https://github.com/microsoft/cppwinrt/issues/584
if (onnxruntime_WINML_NAMESPACE_OVERRIDE STREQUAL "Windows")
target_compile_definitions(winml_test_common PRIVATE "BUILD_INBOX=1")
endif()
add_dependencies(winml_test_common
onnx
winml_api
@ -192,7 +195,7 @@ add_winml_test(
SOURCES ${winml_test_api_src} ${winml_test_api_redist_only_src}
LIBS winml_test_common
)
target_delayload(winml_test_api d3d11.dll dxgi.dll d3d12.dll api-ms-win-core-file-l1-2-2.dll api-ms-win-core-synch-l1-2-1.dll)
target_delayload(winml_test_api dxgi.dll d3d12.dll api-ms-win-core-file-l1-2-2.dll api-ms-win-core-synch-l1-2-1.dll)
if (onnxruntime_USE_DML)
target_delayload(winml_test_api directml.dll)
endif()
@ -229,9 +232,6 @@ if(onnxruntime_RUN_MODELTEST_IN_DEBUG_MODE)
target_compile_definitions(winml_test_image PUBLIC -DRUN_MODELTEST_IN_DEBUG_MODE)
endif()
target_delayload(winml_test_image d3d12.dll api-ms-win-core-file-l1-2-2.dll api-ms-win-core-synch-l1-2-1.dll)
if (EXISTS ${dxcore_header})
target_delayload(winml_test_image ext-ms-win-dxcore-l1-*.dll)
endif()
get_winml_test_concurrency_src(${WINML_TEST_SRC_DIR} winml_test_concurrency_src)
add_winml_test(

View file

@ -22,36 +22,25 @@ namespace winmla = Windows::AI::MachineLearning::Adapter;
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
static std::string System32Path() {
std::string system32_path;
system32_path.reserve(MAX_PATH);
auto size_system32_path = GetSystemDirectoryA(system32_path.data(), MAX_PATH);
FAIL_FAST_IF(size_system32_path == 0);
return system32_path;
}
static std::string CurrentModulePath() {
char path[MAX_PATH];
FAIL_FAST_IF(0 == GetModuleFileNameA((HINSTANCE)&__ImageBase, path, _countof(path)));
static bool IsCurrentModuleInPath(const std::string& path) {
std::string current_module_path;
current_module_path.reserve(MAX_PATH);
auto size_module_path = GetModuleFileNameA((HINSTANCE)&__ImageBase, current_module_path.data(), MAX_PATH);
FAIL_FAST_IF(size_module_path == 0);
return _strnicmp(path.c_str(), current_module_path.c_str(), path.size()) == 0;
char absolute_path[MAX_PATH];
char* name;
FAIL_FAST_IF(0 == GetFullPathNameA(path, _countof(path), absolute_path, &name));
auto idx = std::distance(absolute_path, name);
auto out_path = std::string(absolute_path);
out_path.resize(idx);
return out_path;
}
Microsoft::WRL::ComPtr<IDMLDevice> CreateDmlDevice(ID3D12Device* d3d12Device) {
std::string directml_dll = "directml.dll";
DWORD flags = 0;
#ifdef BUILD_INBOX
auto system32_path = System32Path();
if (IsCurrentModuleInPath(system32_path))
{
flags |= LOAD_LIBRARY_SEARCH_SYSTEM32;
directml_dll = system32_path + "\\" + directml_dll;
}
#endif
// Dynamically load DML to avoid WinML taking a static dependency on DirectML.dll
wil::unique_hmodule dmlDll(LoadLibraryExA(directml_dll.c_str(), nullptr, flags));
auto directml_dll = CurrentModulePath() + "\\directml.dll";
wil::unique_hmodule dmlDll(LoadLibraryExA(directml_dll.c_str(), nullptr, 0));
THROW_LAST_ERROR_IF(!dmlDll);
auto dmlCreateDevice1Fn = reinterpret_cast<decltype(&DMLCreateDevice1)>(

View file

@ -16,39 +16,30 @@ static bool debug_output_ = false;
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
static std::string System32Path() {
std::string system32_path;
system32_path.reserve(MAX_PATH);
auto size_system32_path = GetSystemDirectoryA(system32_path.data(), MAX_PATH);
FAIL_FAST_IF(size_system32_path == 0);
return system32_path;
}
static std::string CurrentModulePath() {
char path[MAX_PATH];
FAIL_FAST_IF(0 == GetModuleFileNameA((HINSTANCE)&__ImageBase, path, _countof(path)));
static bool IsCurrentModuleInPath(const std::string& path) {
std::string current_module_path;
current_module_path.reserve(MAX_PATH);
auto size_module_path = GetModuleFileNameA((HINSTANCE)&__ImageBase, current_module_path.data(), MAX_PATH);
FAIL_FAST_IF(size_module_path == 0);
return _strnicmp(path.c_str(), current_module_path.c_str(), path.size()) == 0;
char absolute_path[MAX_PATH];
char* name;
FAIL_FAST_IF(0 == GetFullPathNameA(path, _countof(path), absolute_path, &name));
auto idx = std::distance(absolute_path, name);
auto out_path = std::string(absolute_path);
out_path.resize(idx);
return out_path;
}
static HRESULT GetOnnxruntimeLibrary(HMODULE& module) {
#if WINAPI_FAMILY == WINAPI_FAMILY_PC_APP
// Store + Redist (note that this is never built into the inbox dll)
auto out_module = LoadPackagedLibrary(L"onnxruntime.dll", 0);
#else
std::string onnxruntime_dll = "onnxruntime.dll";
DWORD flags = 0;
#ifdef BUILD_INBOX
auto system32_path = System32Path();
if (IsCurrentModuleInPath(system32_path))
{
flags |= LOAD_LIBRARY_SEARCH_SYSTEM32;
onnxruntime_dll = system32_path + "\\" + onnxruntime_dll;
}
auto onnxruntime_dll = CurrentModulePath() + "\\onnxruntime.dll";
auto out_module = LoadLibraryExA(onnxruntime_dll.c_str(), nullptr, 0);
#endif
auto out_module = LoadLibraryExA(onnxruntime_dll.c_str(), nullptr, flags);
#endif
if (out_module == nullptr) {
return HRESULT_FROM_WIN32(GetLastError());
}

View file

@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// #include "dx.h"
// #include <D3dx12.h>
#include "inc/pch.h"
#if USE_DML
#include <DirectML.h>

View file

@ -336,12 +336,14 @@ static void NamedDimensionOverride()
LearningModelSession session(nullptr);
WINML_EXPECT_NO_THROW(session = LearningModelSession(model, device, options));
#ifndef BUILD_INBOX
Experimental::LearningModelSessionExperimental experimental_session(session);
Experimental::LearningModelSessionOptionsExperimental experimental_options = experimental_session.Options();
wfc::IMapView<winrt::hstring, uint32_t> internal_overrides = experimental_options.GetNamedDimensionOverrides();
WINML_EXPECT_EQUAL(internal_overrides.Lookup(L"None"), n);
WINML_EXPECT_EQUAL(internal_overrides.Lookup(L"DimNameThatDoesntExist"), n);
#endif
ILearningModelFeatureDescriptor descriptor = model.InputFeatures().GetAt(0);
TensorFeatureDescriptor tensorDescriptor = nullptr;

View file

@ -33,16 +33,15 @@
#define CREATE_NATIVE_INTERNAL_HEADER() XSTRINGIFY(NATIVE_INTERNAL_HEADER(WINML_ROOT_NS))
#include CREATE_CPPWINRT_COMPONENT_HEADER()
#include CREATE_CPPWINRT_EXPERIMENTAL_COMPONENT_HEADER()
#ifndef BUILD_INBOX
#include CREATE_CPPWINRT_EXPERIMENTAL_COMPONENT_HEADER()
#endif
// WinML Native Headers
#include CREATE_NATIVE_HEADER()
#include CREATE_NATIVE_INTERNAL_HEADER()
#endif
namespace winml = winrt::WINML_ROOT_NS::AI::MachineLearning;
namespace wf = winrt::Windows::Foundation;
namespace wfc = winrt::Windows::Foundation::Collections;