Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove internal libs from tests #2864

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions cmake/winml.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ add_dependencies(winml_lib_image winml_api_native)
add_dependencies(winml_lib_image winml_api_native_internal)

# Link libraries
target_link_libraries(winml_lib_image PRIVATE wil)
target_link_libraries(winml_lib_image PRIVATE wil winml_lib_common)
if (onnxruntime_USE_DML)
target_add_dml(winml_lib_image)
endif(onnxruntime_USE_DML)
Expand Down Expand Up @@ -360,11 +360,37 @@ add_dependencies(winml_lib_api winml_api_native)
add_dependencies(winml_lib_api winml_api_native_internal)

# Link libraries
target_link_libraries(winml_lib_api PRIVATE wil)
target_link_libraries(winml_lib_api PRIVATE wil winml_lib_telemetry)
if (onnxruntime_USE_DML)
target_add_dml(winml_lib_api)
endif(onnxruntime_USE_DML)

###########################
# Add winml_lib_common
###########################

add_library(winml_lib_common STATIC
${winml_lib_common_dir}/CommonDeviceHelpers.cpp
)

set_target_properties(winml_lib_common PROPERTIES CXX_STANDARD 17)
set_target_properties(winml_lib_common PROPERTIES CXX_STANDARD_REQUIRED ON)
target_compile_options(winml_lib_common PRIVATE /GR- /await /bigobj /wd4238)
target_link_libraries(winml_lib_common PRIVATE wil)
target_include_directories(winml_lib_common PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/winml_api)
target_compile_definitions(winml_lib_common PRIVATE
ONNX_NAMESPACE=onnx
ONNX_ML
LOTUS_LOG_THRESHOLD=2
LOTUS_ENABLE_STDERR_LOGGING
PLATFORM_WINDOWS
_SCL_SECURE_NO_WARNINGS)
add_dependencies(winml_lib_common winml_sdk_cppwinrt)

target_include_directories(winml_lib_common PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/winml_api) # windows machine learning generated component headers
target_include_directories(winml_lib_common PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/winml_api/comp_generated) # windows machine learning generated component headers
target_include_directories(winml_lib_common PRIVATE ${winml_lib_api_dir})
target_include_directories(winml_lib_common PRIVATE ${CMAKE_CURRENT_BINARY_DIR})

###########################
# Add winml_dll
Expand Down
3 changes: 1 addition & 2 deletions cmake/winml_unittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
set(WINML_TEST_SRC_DIR ${REPO_ROOT}/winml/test)
set(WINML_TEST_INC_DIR
${REPO_ROOT}/winml/test/common
${REPO_ROOT}/winml/lib/Api.Image/inc
${REPO_ROOT}/winml/lib/Common/inc
${REPO_ROOT}/onnxruntime
${REPO_ROOT}/onnxruntime/core/providers/dml/DmlExecutionProvider/src/External/D3DX12
Expand Down Expand Up @@ -44,7 +43,7 @@ function(add_winml_test)
if (_UT_DEPENDS)
add_dependencies(${_UT_TARGET} ${_UT_DEPENDS})
endif()
target_link_libraries(${_UT_TARGET} PRIVATE ${_UT_LIBS} gtest winml_lib_image ${onnxruntime_EXTERNAL_LIBRARIES} winml_lib_telemetry winml_lib_api onnxruntime)
target_link_libraries(${_UT_TARGET} PRIVATE ${_UT_LIBS} gtest ${onnxruntime_EXTERNAL_LIBRARIES} winml_lib_common onnxruntime)

add_test(NAME ${_UT_TARGET}
COMMAND ${_UT_TARGET}
Expand Down
9 changes: 5 additions & 4 deletions winml/lib/Api.Image/D3DDeviceCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <directxmath.h>
#include <d3d11on12.h>
#include "inc/DeviceHelpers.h"
#include "CommonDeviceHelpers.h"

namespace float32 {
#include "shaders\SurfaceToTensor-SurfaceToTensorBGR8.h"
Expand Down Expand Up @@ -50,8 +51,8 @@ D3DDeviceCache::D3DDeviceCache(Windows::AI::MachineLearning::LearningModelDevice
DXGI_GPU_PREFERENCE preference;
WINML_THROW_IF_FAILED(DeviceHelpers::GetGPUPreference(deviceKind, &preference));

DeviceHelpers::AdapterEnumerationSupport support;
WINML_THROW_IF_FAILED(DeviceHelpers::GetAdapterEnumerationSupport(&support));
CommonDeviceHelpers::AdapterEnumerationSupport support;
WINML_THROW_IF_FAILED(CommonDeviceHelpers::GetAdapterEnumerationSupport(&support));

const char errStr[] = "No hardware adapters available";
if (support.has_dxgi) {
Expand Down Expand Up @@ -130,7 +131,7 @@ D3DDeviceCache::~D3DDeviceCache() {

bool D3DDeviceCache::IsFloat16Supported() {
if (device_ != nullptr) {
return DeviceHelpers::IsFloat16Supported(device_.get());
return CommonDeviceHelpers::IsFloat16Supported(device_.get());
}

return true;
Expand Down Expand Up @@ -671,4 +672,4 @@ void D3DDeviceCache::SyncD3D11DeviceToConverter(_In_ ID3D11Fence* pD3D11Fence) {
bool D3DDeviceCache::SharedHandleInitialized() {
return d3d11_fence_ != nullptr;
}
} // namespace winrt::Windows::AI::MachineLearning::implementation
} // namespace winrt::Windows::AI::MachineLearning::implementation
209 changes: 2 additions & 207 deletions winml/lib/Api.Image/DeviceHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,215 +9,10 @@
#include <d3d11on12.h>
#include <wil/winrt.h>
#include "inc/DeviceHelpers.h"
#include "CommonDeviceHelpers.h"
#include "LearningModelDevice.h"

namespace DeviceHelpers {
constexpr uint32_t c_intelVendorId = 0x8086;
constexpr uint32_t c_nvidiaVendorId = 0x10DE;
constexpr uint32_t c_amdVendorId = 0x1002;
static std::optional<AdapterEnumerationSupport> s_adapterEnumerationSupport;

static bool CheckAdapterFP16Blocked(bool isMcdmAdapter, uint32_t vendorId, uint32_t majorVersion, uint32_t minorVersion) {
switch (vendorId) {
case c_intelVendorId: {
if (isMcdmAdapter) {
return false;
}

// Check Intel GPU driver version
return (majorVersion < 25) || (majorVersion == 25 && minorVersion < 6574) || (majorVersion == 26 && minorVersion < 6572);
}
}
return false;
}

static void ParseDriverVersion(LARGE_INTEGER& version, uint32_t& majorVersion, uint32_t& minorVersion) {
majorVersion = HIWORD(version.HighPart);
minorVersion = LOWORD(version.LowPart);
}

static HRESULT GetDXGIAdapterMetadata(ID3D12Device& device, uint32_t& vendorId, uint32_t& majorVersion, uint32_t& minorVersion) {
winrt::com_ptr<IDXGIFactory4> spFactory;
RETURN_IF_FAILED(CreateDXGIFactory1(IID_PPV_ARGS(spFactory.put())));

winrt::com_ptr<IDXGIAdapter> spAdapter;
RETURN_IF_FAILED(spFactory->EnumAdapterByLuid(device.GetAdapterLuid(), IID_PPV_ARGS(spAdapter.put())));

DXGI_ADAPTER_DESC adapterDesc = {};
RETURN_IF_FAILED(spAdapter->GetDesc(&adapterDesc));

LARGE_INTEGER driverVersion;
RETURN_IF_FAILED(spAdapter->CheckInterfaceSupport(__uuidof(IDXGIDevice), &driverVersion));

vendorId = adapterDesc.VendorId;
ParseDriverVersion(driverVersion, majorVersion, minorVersion);
return S_OK;
}

#ifdef ENABLE_DXCORE
static HRESULT GetDXCoreAdapterMetadata(ID3D12Device& device, bool& isMcdmAdapter, uint32_t& vendorId, uint32_t& majorVersion, uint32_t& minorVersion) {
winrt::com_ptr<IDXCoreAdapterFactory> spFactory;
RETURN_IF_FAILED(DXCoreCreateAdapterFactory(IID_PPV_ARGS(spFactory.put())));

winrt::com_ptr<IDXCoreAdapter> spAdapter;
RETURN_IF_FAILED(spFactory->GetAdapterByLuid(device.GetAdapterLuid(), IID_PPV_ARGS(spAdapter.put())));

if (spAdapter->IsAttributeSupported(DXCORE_ADAPTER_ATTRIBUTE_D3D12_CORE_COMPUTE) &&
(!(spAdapter->IsAttributeSupported(DXCORE_ADAPTER_ATTRIBUTE_D3D12_GRAPHICS) ||
spAdapter->IsAttributeSupported(DXCORE_ADAPTER_ATTRIBUTE_D3D11_GRAPHICS)))) {
isMcdmAdapter = true;
} else {
isMcdmAdapter = false;
}

DXCoreHardwareID hardwareId;
RETURN_IF_FAILED(spAdapter->GetProperty(DXCoreAdapterProperty::HardwareID, &hardwareId));
vendorId = hardwareId.vendorID;

uint64_t rawDriverVersion;
RETURN_IF_FAILED(spAdapter->GetProperty(DXCoreAdapterProperty::DriverVersion, &rawDriverVersion));

LARGE_INTEGER driverVersion;
driverVersion.QuadPart = static_cast<LONGLONG>(rawDriverVersion);
ParseDriverVersion(driverVersion, majorVersion, minorVersion);
return S_OK;
}
#endif

static HRESULT GetD3D12Device(const winrt::Windows::AI::MachineLearning::LearningModelDevice& device, ID3D12Device** outDevice) {
_LUID id;
id.LowPart = device.AdapterId().LowPart;
id.HighPart = device.AdapterId().HighPart;
AdapterEnumerationSupport support;
RETURN_IF_FAILED(GetAdapterEnumerationSupport(&support));

if (support.has_dxgi) {
winrt::com_ptr<IDXGIFactory6> spFactory;
RETURN_IF_FAILED(CreateDXGIFactory1(IID_PPV_ARGS(spFactory.put())));

winrt::com_ptr<IDXGIAdapter1> spAdapter;
RETURN_IF_FAILED(spFactory->EnumAdapterByLuid(id, IID_PPV_ARGS(spAdapter.put())));
RETURN_IF_FAILED(D3D12CreateDevice(spAdapter.get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(outDevice)));
}
#ifdef ENABLE_DXCORE
if (support.has_dxgi == false) {
winrt::com_ptr<IDXCoreAdapterFactory> spFactory;
RETURN_IF_FAILED(DXCoreCreateAdapterFactory(IID_PPV_ARGS(spFactory.put())));

winrt::com_ptr<IDXCoreAdapter> spAdapter;
RETURN_IF_FAILED(spFactory->GetAdapterByLuid(id, IID_PPV_ARGS(spAdapter.put())));
RETURN_IF_FAILED(D3D12CreateDevice(spAdapter.get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(outDevice)));
}
#endif
return S_OK;
}

static HRESULT IsFloat16Blocked(ID3D12Device& device, bool* isBlocked) {
uint32_t vendorId;
uint32_t majorVersion;
uint32_t minorVersion;
bool isMcdmAdapter;
*isBlocked = true;
AdapterEnumerationSupport support;
RETURN_IF_FAILED(GetAdapterEnumerationSupport(&support));
#ifdef ENABLE_DXCORE
if (support.has_dxcore) {
RETURN_IF_FAILED(GetDXCoreAdapterMetadata(device, isMcdmAdapter, vendorId, majorVersion, minorVersion));
*isBlocked = CheckAdapterFP16Blocked(isMcdmAdapter, vendorId, majorVersion, minorVersion);
return S_OK;
}
#endif
RETURN_IF_FAILED(GetDXGIAdapterMetadata(device, vendorId, majorVersion, minorVersion));
isMcdmAdapter = false;
*isBlocked = CheckAdapterFP16Blocked(isMcdmAdapter, vendorId, majorVersion, minorVersion);
return S_OK;
}

bool IsFloat16Supported(const winrt::Windows::AI::MachineLearning::LearningModelDevice& device) {
auto modelImpl = device.as<winmlp::LearningModelDevice>();
if (modelImpl->IsCpuDevice()) {
return true;
}
winrt::com_ptr<ID3D12Device> d3d12Device;
if (FAILED(GetD3D12Device(device, d3d12Device.put()))) {
return false;
}
return IsFloat16Supported(d3d12Device.get());
}

bool IsFloat16Supported(ID3D12Device* device) {
#ifndef USE_DML
WINML_THROW_HR_IF_TRUE_MSG(ERROR_NOT_SUPPORTED, true, "IsFloat16Supported is not implemented for WinML only build.");
return false;
#else
bool isBlocked;
if (FAILED(IsFloat16Blocked(*device, &isBlocked)) || isBlocked) {
return false;
}
winrt::com_ptr<IDMLDevice> dmlDevice;
winrt::check_hresult(DMLCreateDevice(
device,
DML_CREATE_DEVICE_FLAG_NONE,
IID_PPV_ARGS(dmlDevice.put())));

DML_FEATURE_QUERY_TENSOR_DATA_TYPE_SUPPORT float16Query = {DML_TENSOR_DATA_TYPE_FLOAT16};
DML_FEATURE_DATA_TENSOR_DATA_TYPE_SUPPORT float16Data = {};

winrt::check_hresult(dmlDevice->CheckFeatureSupport(
DML_FEATURE_TENSOR_DATA_TYPE_SUPPORT,
sizeof(float16Query),
&float16Query,
sizeof(float16Data),
&float16Data));
return float16Data.IsSupported;
#endif USE_DML
}

// uses Structured Exception Handling (SEH) to detect for delay load failures of target API.
// You cannot mix and match SEH with C++ exception and object unwinding
// In this case we will catch it, and report up to the caller via HRESULT so our callers can use
// C++ exceptions
template <typename TFunc, typename... TArgs>
static HRESULT RunDelayLoadedApi(TFunc& tfunc, TArgs&&... args) {
__try {
return tfunc(std::forward<TArgs>(args)...);
} __except (GetExceptionCode() == VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND) ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
// this could be ok, just let people know that it failed to load
return HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND);
}
}

HRESULT GetAdapterEnumerationSupport(AdapterEnumerationSupport* support) {
if (!s_adapterEnumerationSupport.has_value()) {
// check for support, starting with DXGI
winrt::com_ptr<IDXGIFactory4> dxgiFactory;
#ifdef ENABLE_DXCORE
winrt::com_ptr<IDXCoreAdapterFactory> dxcoreFactory;
// necessary because DXCoreCreateAdapterFactory is overloaded
HRESULT(WINAPI * pDxCoreTestFunc)
(REFIID, void**) = DXCoreCreateAdapterFactory;
#endif
AdapterEnumerationSupport adapterEnumerationSupport = {};

if (SUCCEEDED(RunDelayLoadedApi(CreateDXGIFactory1, IID_PPV_ARGS(dxgiFactory.put())))) {
adapterEnumerationSupport.has_dxgi = true;
}
#ifdef ENABLE_DXCORE
if (SUCCEEDED(RunDelayLoadedApi(pDxCoreTestFunc, IID_PPV_ARGS(dxcoreFactory.put())))) {
adapterEnumerationSupport.has_dxcore = true;
}
#endif

s_adapterEnumerationSupport = adapterEnumerationSupport;

if (!(adapterEnumerationSupport.has_dxgi || adapterEnumerationSupport.has_dxcore)) {
return TYPE_E_CANTLOADLIBRARY;
}
}
*support = s_adapterEnumerationSupport.value();
return S_OK;
}

HRESULT GetDXGIHardwareAdapterWithPreference(DXGI_GPU_PREFERENCE preference, IDXGIAdapter1** ppAdapter) {
winrt::com_ptr<IDXGIFactory6> spFactory;
RETURN_IF_FAILED(CreateDXGIFactory1(IID_PPV_ARGS(spFactory.put())));
Expand Down Expand Up @@ -304,7 +99,7 @@ HRESULT GetDXCoreHardwareAdapterWithPreference(DXGI_GPU_PREFERENCE preference, I
#endif

HRESULT CreateD3D11On12Device(ID3D12Device* device12, ID3D11Device** device11) {
return DeviceHelpers::RunDelayLoadedApi(
return CommonDeviceHelpers::RunDelayLoadedApi(
D3D11On12CreateDevice,
device12, // pointer to d3d12 device
D3D11_CREATE_DEVICE_BGRA_SUPPORT, // required in order to interop with Direct2D
Expand Down
19 changes: 1 addition & 18 deletions winml/lib/Api.Image/inc/DeviceHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,11 @@
#include <dxcore.h>
#endif

//
// Exception information
//
#ifndef FACILITY_VISUALCPP
#define FACILITY_VISUALCPP ((LONG)0x6d)
#endif

#define VcppException(sev, err) ((sev) | (FACILITY_VISUALCPP << 16) | err)

namespace DeviceHelpers {
struct AdapterEnumerationSupport {
bool has_dxgi;
bool has_dxcore;
};

HRESULT GetAdapterEnumerationSupport(AdapterEnumerationSupport* support);
bool IsFloat16Supported(ID3D12Device* device);
bool IsFloat16Supported(const winrt::Windows::AI::MachineLearning::LearningModelDevice& device);
HRESULT CreateD3D11On12Device(ID3D12Device* device12, ID3D11Device** device11);
#ifdef ENABLE_DXCORE
HRESULT GetDXCoreHardwareAdapterWithPreference(DXGI_GPU_PREFERENCE preference, _COM_Outptr_ IDXCoreAdapter** ppAdapter);
#endif
HRESULT GetDXGIHardwareAdapterWithPreference(DXGI_GPU_PREFERENCE preference, _COM_Outptr_ IDXGIAdapter1** adapter);
HRESULT GetGPUPreference(winrt::Windows::AI::MachineLearning::LearningModelDeviceKind deviceKind, DXGI_GPU_PREFERENCE* preference) noexcept;
} // namespace DeviceHelpers
} // namespace DeviceHelpers
Loading