From 2a016225367d7a7ec4bd8b75a3653b0b93b97720 Mon Sep 17 00:00:00 2001 From: Sheil Kumar Date: Tue, 21 Nov 2023 08:47:56 -0800 Subject: [PATCH] Hide NPU Adapter selection behind macro (#18515) Hide NPU Adapter selection behind macro --------- Co-authored-by: Sheil Kumar --- .../core/providers/dml/dml_provider_factory.h | 4 ++++ .../providers/dml/dml_provider_factory.cc | 19 ++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/onnxruntime/core/providers/dml/dml_provider_factory.h b/include/onnxruntime/core/providers/dml/dml_provider_factory.h index cf3ddc3f125f9..7d7f05193f486 100644 --- a/include/onnxruntime/core/providers/dml/dml_provider_factory.h +++ b/include/onnxruntime/core/providers/dml/dml_provider_factory.h @@ -37,9 +37,13 @@ enum OrtDmlPerformancePreference { }; enum OrtDmlDeviceFilter : uint32_t { +#ifdef ENABLE_NPU_ADAPTER_ENUMERATION Any = 0xffffffff, Gpu = 1 << 0, Npu = 1 << 1, +#else + Gpu = 1 << 0, +#endif }; inline OrtDmlDeviceFilter operator~(OrtDmlDeviceFilter a) { return (OrtDmlDeviceFilter) ~(int)a; } diff --git a/onnxruntime/core/providers/dml/dml_provider_factory.cc b/onnxruntime/core/providers/dml/dml_provider_factory.cc index d587424fe01f8..33f1f59e07f3f 100644 --- a/onnxruntime/core/providers/dml/dml_provider_factory.cc +++ b/onnxruntime/core/providers/dml/dml_provider_factory.cc @@ -118,6 +118,7 @@ static bool IsGPU(IDXCoreAdapter* compute_adapter) { return compute_adapter->IsAttributeSupported(DXCORE_ADAPTER_ATTRIBUTE_D3D12_GRAPHICS); } +#ifdef ENABLE_NPU_ADAPTER_ENUMERATION static bool IsNPU(IDXCoreAdapter* compute_adapter) { // Only considering hardware adapters if (!IsHardwareAdapter(compute_adapter)) { @@ -125,6 +126,7 @@ static bool IsNPU(IDXCoreAdapter* compute_adapter) { } return !(compute_adapter->IsAttributeSupported(DXCORE_ADAPTER_ATTRIBUTE_D3D12_GRAPHICS)); } +#endif enum class DeviceType { GPU, NPU, BadDevice }; @@ -134,10 +136,12 @@ static DeviceType FilterAdapterTypeQuery(IDXCoreAdapter* adapter, OrtDmlDeviceFi return DeviceType::GPU; } +#ifdef ENABLE_NPU_ADAPTER_ENUMERATION auto allow_npus = (filter & OrtDmlDeviceFilter::Npu) == OrtDmlDeviceFilter::Npu; if (IsNPU(adapter) && allow_npus) { return DeviceType::NPU; } +#endif return DeviceType::BadDevice; } @@ -216,6 +220,7 @@ static void SortHeterogenousDXCoreAdapterList( return; } +#ifdef ENABLE_NPU_ADAPTER_ENUMERATION // When considering both GPUs and NPUs sort them by performance preference // of Default (Gpus first), HighPerformance (GPUs first), or LowPower (NPUs first) auto keep_npus = (filter & OrtDmlDeviceFilter::Npu) == OrtDmlDeviceFilter::Npu; @@ -223,6 +228,7 @@ static void SortHeterogenousDXCoreAdapterList( if (!keep_npus || only_npus) { return; } +#endif struct SortingPolicy { // default is false because GPUs are considered higher priority in @@ -322,23 +328,26 @@ static std::optional ParsePerformancePreference(con static std::optional ParseFilter(const ProviderOptions& provider_options) { static const std::string Filter = "filter"; - static const std::string Any = "any"; static const std::string Gpu = "gpu"; +#ifdef ENABLE_NPU_ADAPTER_ENUMERATION + static const std::string Any = "any"; static const std::string Npu = "npu"; +#endif auto preference_it = provider_options.find(Filter); if (preference_it != provider_options.end()) { - if (preference_it->second == Any) { - return OrtDmlDeviceFilter::Any; - } - if (preference_it->second == Gpu) { return OrtDmlDeviceFilter::Gpu; } +#ifdef ENABLE_NPU_ADAPTER_ENUMERATION + if (preference_it->second == Any) { + return OrtDmlDeviceFilter::Any; + } if (preference_it->second == Npu) { return OrtDmlDeviceFilter::Npu; } +#endif ORT_THROW("Invalid Filter provided for DirectML EP device selection."); }