Skip to content

Commit ebcce42

Browse files
authored
Add some device discovery support for non-Windows platforms (microsoft#25228)
### Description <!-- Describe your changes. --> Add some device discovery support for non-Windows platforms. ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> More device discovery support.
1 parent a380e5c commit ebcce42

File tree

15 files changed

+765
-110
lines changed

15 files changed

+765
-110
lines changed

cmake/onnxruntime_common.cmake

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ set(onnxruntime_common_src_patterns
1414
"${ONNXRUNTIME_ROOT}/core/platform/check_intel.h"
1515
"${ONNXRUNTIME_ROOT}/core/platform/check_intel.cc"
1616
"${ONNXRUNTIME_ROOT}/core/platform/device_discovery.h"
17-
"${ONNXRUNTIME_ROOT}/core/platform/device_discovery.cc"
17+
"${ONNXRUNTIME_ROOT}/core/platform/device_discovery_common.cc"
1818
"${ONNXRUNTIME_ROOT}/core/platform/env.h"
1919
"${ONNXRUNTIME_ROOT}/core/platform/env.cc"
2020
"${ONNXRUNTIME_ROOT}/core/platform/env_time.h"
@@ -32,26 +32,38 @@ set(onnxruntime_common_src_patterns
3232

3333
if(WIN32)
3434
list(APPEND onnxruntime_common_src_patterns
35-
"${ONNXRUNTIME_ROOT}/core/platform/windows/*.h"
36-
"${ONNXRUNTIME_ROOT}/core/platform/windows/*.cc"
35+
"${ONNXRUNTIME_ROOT}/core/platform/windows/debug_alloc.cc"
36+
"${ONNXRUNTIME_ROOT}/core/platform/windows/debug_alloc.h"
37+
"${ONNXRUNTIME_ROOT}/core/platform/windows/dll_load_error.cc"
38+
"${ONNXRUNTIME_ROOT}/core/platform/windows/dll_load_error.h"
39+
"${ONNXRUNTIME_ROOT}/core/platform/windows/env_time.cc"
40+
"${ONNXRUNTIME_ROOT}/core/platform/windows/env.cc"
41+
"${ONNXRUNTIME_ROOT}/core/platform/windows/env.h"
42+
"${ONNXRUNTIME_ROOT}/core/platform/windows/hardware_core_enumerator.cc"
43+
"${ONNXRUNTIME_ROOT}/core/platform/windows/hardware_core_enumerator.h"
44+
"${ONNXRUNTIME_ROOT}/core/platform/windows/stacktrace.cc"
45+
"${ONNXRUNTIME_ROOT}/core/platform/windows/telemetry.cc"
46+
"${ONNXRUNTIME_ROOT}/core/platform/windows/telemetry.h"
3747
"${ONNXRUNTIME_ROOT}/core/platform/windows/logging/*.h"
3848
"${ONNXRUNTIME_ROOT}/core/platform/windows/logging/*.cc"
3949
)
4050

4151
else()
4252
list(APPEND onnxruntime_common_src_patterns
43-
"${ONNXRUNTIME_ROOT}/core/platform/posix/*.h"
44-
"${ONNXRUNTIME_ROOT}/core/platform/posix/*.cc"
53+
"${ONNXRUNTIME_ROOT}/core/platform/posix/env_time.cc"
54+
"${ONNXRUNTIME_ROOT}/core/platform/posix/env.cc"
55+
"${ONNXRUNTIME_ROOT}/core/platform/posix/stacktrace.cc"
4556
)
4657

58+
# logging files
4759
if (onnxruntime_USE_SYSLOG)
4860
list(APPEND onnxruntime_common_src_patterns
4961
"${ONNXRUNTIME_ROOT}/core/platform/posix/logging/*.h"
5062
"${ONNXRUNTIME_ROOT}/core/platform/posix/logging/*.cc"
5163
)
5264
endif()
5365

54-
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
66+
if (ANDROID)
5567
list(APPEND onnxruntime_common_src_patterns
5668
"${ONNXRUNTIME_ROOT}/core/platform/android/logging/*.h"
5769
"${ONNXRUNTIME_ROOT}/core/platform/android/logging/*.cc"
@@ -66,6 +78,21 @@ else()
6678
endif()
6779
endif()
6880

81+
# platform-specific device discovery files
82+
if (WIN32)
83+
list(APPEND onnxruntime_common_src_patterns
84+
"${ONNXRUNTIME_ROOT}/core/platform/windows/device_discovery.cc")
85+
elseif (LINUX)
86+
list(APPEND onnxruntime_common_src_patterns
87+
"${ONNXRUNTIME_ROOT}/core/platform/linux/device_discovery.cc")
88+
elseif (APPLE)
89+
list(APPEND onnxruntime_common_src_patterns
90+
"${ONNXRUNTIME_ROOT}/core/platform/apple/device_discovery.cc")
91+
else()
92+
list(APPEND onnxruntime_common_src_patterns
93+
"${ONNXRUNTIME_ROOT}/core/platform/device_discovery_default.cc")
94+
endif()
95+
6996
if(onnxruntime_target_platform STREQUAL "ARM64EC")
7097
if (MSVC)
7198
link_directories("$ENV{VCINSTALLDIR}/Tools/MSVC/$ENV{VCToolsVersion}/lib/ARM64EC")
@@ -216,8 +243,6 @@ endif()
216243

217244
if (RISCV64 OR ARM64 OR ARM OR X86 OR X64 OR X86_64)
218245
# Link cpuinfo if supported
219-
# Using it mainly in ARM with Android.
220-
# Its functionality in detecting x86 cpu features are lacking, so is support for Windows.
221246
if (CPUINFO_SUPPORTED)
222247
onnxruntime_add_include_to_target(onnxruntime_common cpuinfo::cpuinfo)
223248
list(APPEND onnxruntime_EXTERNAL_LIBRARIES cpuinfo::cpuinfo ${ONNXRUNTIME_CLOG_TARGET_NAME})

include/onnxruntime/core/common/parse_string.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,30 @@ template <typename T>
3535
std::enable_if_t<detail::ParseWithFromChars<T>, bool>
3636
TryParseStringWithClassicLocale(std::string_view str, T& value) {
3737
T parsed_value{};
38-
const auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), parsed_value);
3938

40-
if (ec != std::errc{}) {
39+
std::from_chars_result conversion_result{};
40+
if constexpr (std::is_integral_v<T> && std::is_unsigned_v<T>) {
41+
// For unsigned integral types, also handle hex values, i.e., those beginning with "0x".
42+
// std::from_chars() does not accept the "0x" prefix.
43+
const bool has_hex_prefix = str.size() >= 2 &&
44+
str[0] == '0' &&
45+
(str[1] == 'x' || str[1] == 'X');
46+
47+
if (has_hex_prefix) {
48+
str = str.substr(2);
49+
}
50+
51+
const int base = has_hex_prefix ? 16 : 10;
52+
conversion_result = std::from_chars(str.data(), str.data() + str.size(), parsed_value, base);
53+
} else {
54+
conversion_result = std::from_chars(str.data(), str.data() + str.size(), parsed_value);
55+
}
56+
57+
if (conversion_result.ec != std::errc{}) {
4158
return false;
4259
}
4360

44-
if (ptr != str.data() + str.size()) {
61+
if (conversion_result.ptr != str.data() + str.size()) {
4562
return false;
4663
}
4764

onnxruntime/core/common/cpuid_info.cc

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33
#include "core/common/cpuid_info.h"
4+
5+
#include <iostream>
6+
#include <optional>
7+
48
#include "core/common/logging/logging.h"
59
#include "core/common/logging/severity.h"
610
#include "core/platform/check_intel.h"
@@ -51,6 +55,14 @@
5155

5256
#endif // _WIN32
5357

58+
#if defined(__APPLE__)
59+
#if defined(CPUIDINFO_ARCH_ARM)
60+
61+
#include <sys/sysctl.h>
62+
63+
#endif // defined(CPUIDINFO_ARCH_ARM)
64+
#endif // defined(__APPLE__)
65+
5466
#if defined(CPUINFO_SUPPORTED)
5567
#include <cpuinfo.h>
5668
#if defined(CPUIDINFO_ARCH_ARM)
@@ -74,6 +86,14 @@ void decodeMIDR(uint32_t midr, uint32_t uarch[1]);
7486

7587
namespace onnxruntime {
7688

89+
void CPUIDInfo::LogEarlyWarning(std::string_view message) {
90+
if (logging::LoggingManager::HasDefaultLogger()) {
91+
LOGS_DEFAULT(WARNING) << message;
92+
} else {
93+
std::cerr << "onnxruntime cpuid_info warning: " << message << "\n";
94+
}
95+
}
96+
7797
#if defined(CPUIDINFO_ARCH_X86)
7898

7999
static inline void GetCPUID(int function_id, int data[4]) { // NOLINT
@@ -108,9 +128,6 @@ void CPUIDInfo::X86Init() {
108128
int data[4] = {-1};
109129
GetCPUID(0, data);
110130

111-
vendor_ = GetX86Vendor(data);
112-
vendor_id_ = GetVendorId(vendor_);
113-
114131
int num_IDs = data[0];
115132
if (num_IDs >= 1) {
116133
GetCPUID(1, data);
@@ -158,24 +175,8 @@ void CPUIDInfo::X86Init() {
158175
}
159176
}
160177

161-
std::string CPUIDInfo::GetX86Vendor(int32_t* data) {
162-
char vendor[sizeof(int32_t) * 3 + 1]{};
163-
*reinterpret_cast<int*>(vendor + 0) = data[1];
164-
*reinterpret_cast<int*>(vendor + 4) = data[3];
165-
*reinterpret_cast<int*>(vendor + 8) = data[2];
166-
return vendor;
167-
}
168-
169178
#endif // defined(CPUIDINFO_ARCH_X86)
170179

171-
uint32_t CPUIDInfo::GetVendorId(const std::string& vendor) {
172-
if (vendor == "GenuineIntel") return 0x8086;
173-
if (vendor == "AuthenticAMD") return 0x1022;
174-
if (vendor.find("Qualcomm") == 0) return 'Q' | ('C' << 8) | ('O' << 16) | ('M' << 24);
175-
if (vendor.find("NV") == 0) return 0x10DE;
176-
return 0;
177-
}
178-
179180
#if defined(CPUIDINFO_ARCH_ARM)
180181

181182
#if defined(__linux__)
@@ -228,10 +229,6 @@ void CPUIDInfo::ArmLinuxInit() {
228229
#elif defined(_WIN32) // ^ defined(__linux__)
229230

230231
void CPUIDInfo::ArmWindowsInit() {
231-
// Get the ARM vendor string from the registry
232-
vendor_ = GetArmWindowsVendor();
233-
vendor_id_ = GetVendorId(vendor_);
234-
235232
// Read MIDR and ID_AA64ISAR1_EL1 register values from Windows registry
236233
// There should be one per CPU
237234
std::vector<uint64_t> midr_values{}, id_aa64isar1_el1_values{};
@@ -323,15 +320,6 @@ void CPUIDInfo::ArmWindowsInit() {
323320
#endif // defined(CPUINFO_SUPPORTED)
324321
}
325322

326-
std::string CPUIDInfo::GetArmWindowsVendor() {
327-
const int MAX_VALUE_NAME = 256;
328-
const CHAR vendorKey[] = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
329-
CHAR vendorVal[MAX_VALUE_NAME] = "";
330-
unsigned long vendorSize = sizeof(char) * MAX_VALUE_NAME;
331-
::RegGetValueA(HKEY_LOCAL_MACHINE, vendorKey, "Vendor Identifier", RRF_RT_REG_SZ | RRF_ZEROONFAILURE, nullptr, &vendorVal, &vendorSize);
332-
return vendorVal;
333-
}
334-
335323
#elif defined(__APPLE__) // ^ defined(_WIN32)
336324

337325
void CPUIDInfo::ArmAppleInit() {
@@ -376,16 +364,21 @@ uint32_t CPUIDInfo::GetCurrentCoreIdx() const {
376364
}
377365

378366
CPUIDInfo::CPUIDInfo() {
379-
#ifdef CPUIDINFO_ARCH_X86
380-
X86Init();
381-
#elif defined(CPUIDINFO_ARCH_ARM)
382367
#if defined(CPUINFO_SUPPORTED)
383368
pytorch_cpuinfo_init_ = cpuinfo_initialize();
384369
if (!pytorch_cpuinfo_init_) {
385-
LOGS_DEFAULT(WARNING) << "Failed to initialize PyTorch cpuinfo library. May cause CPU EP performance degradation "
386-
"due to undetected CPU features.";
370+
LogEarlyWarning(
371+
"Failed to initialize PyTorch cpuinfo library. May cause CPU EP performance degradation due to undetected CPU "
372+
"features.");
387373
}
388374
#endif // defined(CPUINFO_SUPPORTED)
375+
376+
// Note: This should be run after cpuinfo initialization if cpuinfo is enabled.
377+
VendorInfoInit();
378+
379+
#ifdef CPUIDINFO_ARCH_X86
380+
X86Init();
381+
#elif defined(CPUIDINFO_ARCH_ARM)
389382
#if defined(__linux__)
390383
ArmLinuxInit();
391384
#elif defined(_WIN32)

onnxruntime/core/common/cpuid_info.h

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,40 @@ class CPUIDInfo {
103103
}
104104

105105
private:
106+
// Log function that uses ORT logging if available or writes to stderr.
107+
// This enables us to log even before ORT logging has been initialized.
108+
static void LogEarlyWarning(std::string_view message);
109+
106110
CPUIDInfo();
111+
112+
void VendorInfoInit();
113+
114+
#if defined(CPUIDINFO_ARCH_X86)
115+
116+
void X86Init();
117+
118+
#elif defined(CPUIDINFO_ARCH_ARM)
119+
120+
#if defined(__linux__)
121+
122+
void ArmLinuxInit();
123+
124+
#elif defined(_WIN32)
125+
126+
void ArmWindowsInit();
127+
128+
#elif defined(__APPLE__)
129+
130+
void ArmAppleInit();
131+
132+
#endif
133+
134+
#endif // defined(CPUIDINFO_ARCH_ARM)
135+
136+
#if defined(CPUINFO_SUPPORTED)
137+
bool pytorch_cpuinfo_init_{false};
138+
#endif // defined(CPUINFO_SUPPORTED)
139+
107140
bool has_amx_bf16_{false};
108141
bool has_avx_{false};
109142
bool has_avx2_{false};
@@ -132,37 +165,6 @@ class CPUIDInfo {
132165

133166
std::string vendor_;
134167
uint32_t vendor_id_;
135-
136-
uint32_t GetVendorId(const std::string& vendor);
137-
138-
#if defined(CPUIDINFO_ARCH_X86)
139-
140-
void X86Init();
141-
std::string GetX86Vendor(int32_t* data);
142-
143-
#elif defined(CPUIDINFO_ARCH_ARM)
144-
145-
#if defined(CPUINFO_SUPPORTED)
146-
// Now the following var is only used in ARM build, but later on we may expand the usage.
147-
bool pytorch_cpuinfo_init_{false};
148-
#endif // defined(CPUINFO_SUPPORTED)
149-
150-
#if defined(__linux__)
151-
152-
void ArmLinuxInit();
153-
154-
#elif defined(_WIN32)
155-
156-
void ArmWindowsInit();
157-
std::string GetArmWindowsVendor();
158-
159-
#elif defined(__APPLE__)
160-
161-
void ArmAppleInit();
162-
163-
#endif
164-
165-
#endif // defined(CPUIDINFO_ARCH_ARM)
166168
};
167169

168170
} // namespace onnxruntime

0 commit comments

Comments
 (0)