Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
f5b4d8a
add apple device_discovery.cc
edgchen1 Jun 11, 2025
f58ea4d
log discovered devices across platforms
edgchen1 Jun 12, 2025
d7b598d
save work - linux CPU discovery
edgchen1 Jun 13, 2025
f34b674
Merge remote-tracking branch 'origin/main' into edgchen1/device_disco…
edgchen1 Jun 17, 2025
6101b99
support parsing hex string to int
edgchen1 Jun 18, 2025
a82a10c
save work - linux GPU impl
edgchen1 Jun 18, 2025
64e0a82
fix Linux GPU device_id
edgchen1 Jun 18, 2025
3bba325
Rename onnxruntime/core/platform/device_discovery.cc to device_discov…
edgchen1 Jun 18, 2025
0594d5a
finish renaming
edgchen1 Jun 18, 2025
757d27a
add TODO for vendor name
edgchen1 Jun 18, 2025
3481e99
Merge remote-tracking branch 'origin/main' into edgchen1/device_disco…
edgchen1 Jun 18, 2025
34141a1
add default impl, add todo comment for apple
edgchen1 Jun 18, 2025
849b365
Merge remote-tracking branch 'origin/main' into edgchen1/device_disco…
edgchen1 Jun 27, 2025
cc25212
clean up code for linux gpu discovery
edgchen1 Jun 27, 2025
04b0758
add card_idx to linux gpu metadata
edgchen1 Jun 28, 2025
f03f229
update OrtKeyValuePairs - add copy/move, make data members private, k…
edgchen1 Jun 28, 2025
c089033
save work - apple cpu discovery
edgchen1 Jun 28, 2025
55c3a8c
fix up cpu vendor detection
edgchen1 Jul 1, 2025
231d347
add check for non-zero detected vendor id
edgchen1 Jul 1, 2025
9e686ca
fix formatting
edgchen1 Jul 1, 2025
b474e4f
Merge remote-tracking branch 'origin/main' into edgchen1/device_disco…
edgchen1 Jul 1, 2025
c53921d
fix build error
edgchen1 Jul 1, 2025
48c6bb4
hardcoded apple device discovery
edgchen1 Jul 2, 2025
6aa11ab
fix build issue in onnxruntime_pybind_state.cc
edgchen1 Jul 2, 2025
537ab69
Merge remote-tracking branch 'origin/main' into edgchen1/device_disco…
edgchen1 Jul 24, 2025
f926cdc
enable logging before ORT logging is up
edgchen1 Jul 25, 2025
5b8764e
debug - dump /proc/cpuinfo file lines as they are read
edgchen1 Jul 25, 2025
d0f9653
make /proc/cpuinfo vendor field not mandatory, remove debug output
edgchen1 Jul 26, 2025
920c821
Merge remote-tracking branch 'origin/main' into edgchen1/device_disco…
edgchen1 Jul 26, 2025
a56c2ff
update comment
edgchen1 Jul 26, 2025
8ef49c2
Merge remote-tracking branch 'origin/main' into edgchen1/device_disco…
edgchen1 Jul 28, 2025
5b721ec
add some ARM CPU vendor detection support
edgchen1 Jul 28, 2025
c31e336
fix build issues
edgchen1 Jul 29, 2025
9ae765a
replace /proc/cpuinfo parsing with cpuinfo library usage
edgchen1 Jul 30, 2025
53dbbf9
try enabling vendor id check in test
edgchen1 Jul 30, 2025
d9a7a5a
add endif comment, remove constexpr on FindCpuVendorInfo() because st…
edgchen1 Jul 30, 2025
46d60a3
Merge remote-tracking branch 'origin/main' into edgchen1/device_disco…
edgchen1 Aug 14, 2025
8dfeb2f
Merge remote-tracking branch 'origin/main' into edgchen1/device_disco…
edgchen1 Aug 14, 2025
1ddd31a
disable DeviceDiscoveryTest.HasCpuDevice for WASM
edgchen1 Aug 14, 2025
e1e1014
debugging - dump android logs if java test fails
edgchen1 Aug 14, 2025
3ad0927
disable gpu device discovery via sysfs on Android. there are permissi…
edgchen1 Aug 14, 2025
6adf61f
format
edgchen1 Aug 14, 2025
100228f
use ExitStack to dump Android logs
edgchen1 Aug 14, 2025
b8b5db0
Merge remote-tracking branch 'origin/main' into edgchen1/device_disco…
edgchen1 Aug 14, 2025
1afaa3e
add get CPU device helper function, remove ANDROID handling from linu…
edgchen1 Aug 15, 2025
cdca7a9
remove TODO about extending hex parsing to other types. largely hypot…
edgchen1 Aug 15, 2025
35b3796
remove commented out test
edgchen1 Aug 15, 2025
30370bc
add comment about std::fum_chars not accepting 0x prefix
edgchen1 Aug 15, 2025
c3138a7
linux - log GPU discovery error instead of throwing exception
edgchen1 Aug 18, 2025
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
41 changes: 33 additions & 8 deletions cmake/onnxruntime_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set(onnxruntime_common_src_patterns
"${ONNXRUNTIME_ROOT}/core/platform/check_intel.h"
"${ONNXRUNTIME_ROOT}/core/platform/check_intel.cc"
"${ONNXRUNTIME_ROOT}/core/platform/device_discovery.h"
"${ONNXRUNTIME_ROOT}/core/platform/device_discovery.cc"
"${ONNXRUNTIME_ROOT}/core/platform/device_discovery_common.cc"
"${ONNXRUNTIME_ROOT}/core/platform/env.h"
"${ONNXRUNTIME_ROOT}/core/platform/env.cc"
"${ONNXRUNTIME_ROOT}/core/platform/env_time.h"
Expand All @@ -32,26 +32,38 @@ set(onnxruntime_common_src_patterns

if(WIN32)
list(APPEND onnxruntime_common_src_patterns
"${ONNXRUNTIME_ROOT}/core/platform/windows/*.h"
"${ONNXRUNTIME_ROOT}/core/platform/windows/*.cc"
"${ONNXRUNTIME_ROOT}/core/platform/windows/debug_alloc.cc"
"${ONNXRUNTIME_ROOT}/core/platform/windows/debug_alloc.h"
"${ONNXRUNTIME_ROOT}/core/platform/windows/dll_load_error.cc"
"${ONNXRUNTIME_ROOT}/core/platform/windows/dll_load_error.h"
"${ONNXRUNTIME_ROOT}/core/platform/windows/env_time.cc"
"${ONNXRUNTIME_ROOT}/core/platform/windows/env.cc"
"${ONNXRUNTIME_ROOT}/core/platform/windows/env.h"
"${ONNXRUNTIME_ROOT}/core/platform/windows/hardware_core_enumerator.cc"
"${ONNXRUNTIME_ROOT}/core/platform/windows/hardware_core_enumerator.h"
"${ONNXRUNTIME_ROOT}/core/platform/windows/stacktrace.cc"
"${ONNXRUNTIME_ROOT}/core/platform/windows/telemetry.cc"
"${ONNXRUNTIME_ROOT}/core/platform/windows/telemetry.h"
"${ONNXRUNTIME_ROOT}/core/platform/windows/logging/*.h"
"${ONNXRUNTIME_ROOT}/core/platform/windows/logging/*.cc"
)

else()
list(APPEND onnxruntime_common_src_patterns
"${ONNXRUNTIME_ROOT}/core/platform/posix/*.h"
"${ONNXRUNTIME_ROOT}/core/platform/posix/*.cc"
"${ONNXRUNTIME_ROOT}/core/platform/posix/env_time.cc"
"${ONNXRUNTIME_ROOT}/core/platform/posix/env.cc"
"${ONNXRUNTIME_ROOT}/core/platform/posix/stacktrace.cc"
)

# logging files
if (onnxruntime_USE_SYSLOG)
list(APPEND onnxruntime_common_src_patterns
"${ONNXRUNTIME_ROOT}/core/platform/posix/logging/*.h"
"${ONNXRUNTIME_ROOT}/core/platform/posix/logging/*.cc"
)
endif()

if (CMAKE_SYSTEM_NAME STREQUAL "Android")
if (ANDROID)
list(APPEND onnxruntime_common_src_patterns
"${ONNXRUNTIME_ROOT}/core/platform/android/logging/*.h"
"${ONNXRUNTIME_ROOT}/core/platform/android/logging/*.cc"
Expand All @@ -66,6 +78,21 @@ else()
endif()
endif()

# platform-specific device discovery files
if (WIN32)
list(APPEND onnxruntime_common_src_patterns
"${ONNXRUNTIME_ROOT}/core/platform/windows/device_discovery.cc")
elseif (LINUX)
list(APPEND onnxruntime_common_src_patterns
"${ONNXRUNTIME_ROOT}/core/platform/linux/device_discovery.cc")
elseif (APPLE)
list(APPEND onnxruntime_common_src_patterns
"${ONNXRUNTIME_ROOT}/core/platform/apple/device_discovery.cc")
else()
list(APPEND onnxruntime_common_src_patterns
"${ONNXRUNTIME_ROOT}/core/platform/device_discovery_default.cc")
endif()

if(onnxruntime_target_platform STREQUAL "ARM64EC")
if (MSVC)
link_directories("$ENV{VCINSTALLDIR}/Tools/MSVC/$ENV{VCToolsVersion}/lib/ARM64EC")
Expand Down Expand Up @@ -216,8 +243,6 @@ endif()

if (RISCV64 OR ARM64 OR ARM OR X86 OR X64 OR X86_64)
# Link cpuinfo if supported
# Using it mainly in ARM with Android.
# Its functionality in detecting x86 cpu features are lacking, so is support for Windows.
if (CPUINFO_SUPPORTED)
onnxruntime_add_include_to_target(onnxruntime_common cpuinfo::cpuinfo)
list(APPEND onnxruntime_EXTERNAL_LIBRARIES cpuinfo::cpuinfo ${ONNXRUNTIME_CLOG_TARGET_NAME})
Expand Down
23 changes: 20 additions & 3 deletions include/onnxruntime/core/common/parse_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,30 @@ template <typename T>
std::enable_if_t<detail::ParseWithFromChars<T>, bool>
TryParseStringWithClassicLocale(std::string_view str, T& value) {
T parsed_value{};
const auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), parsed_value);

if (ec != std::errc{}) {
std::from_chars_result conversion_result{};
if constexpr (std::is_integral_v<T> && std::is_unsigned_v<T>) {
// For unsigned integral types, also handle hex values, i.e., those beginning with "0x".
// std::from_chars() does not accept the "0x" prefix.
const bool has_hex_prefix = str.size() >= 2 &&
str[0] == '0' &&
(str[1] == 'x' || str[1] == 'X');

if (has_hex_prefix) {
str = str.substr(2);
}

const int base = has_hex_prefix ? 16 : 10;
conversion_result = std::from_chars(str.data(), str.data() + str.size(), parsed_value, base);
} else {
conversion_result = std::from_chars(str.data(), str.data() + str.size(), parsed_value);
}

if (conversion_result.ec != std::errc{}) {
return false;
}

if (ptr != str.data() + str.size()) {
if (conversion_result.ptr != str.data() + str.size()) {
return false;
}

Expand Down
67 changes: 30 additions & 37 deletions onnxruntime/core/common/cpuid_info.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "core/common/cpuid_info.h"

#include <iostream>
#include <optional>

#include "core/common/logging/logging.h"
#include "core/common/logging/severity.h"
#include "core/platform/check_intel.h"
Expand Down Expand Up @@ -51,6 +55,14 @@

#endif // _WIN32

#if defined(__APPLE__)
#if defined(CPUIDINFO_ARCH_ARM)

#include <sys/sysctl.h>

#endif // defined(CPUIDINFO_ARCH_ARM)
#endif // defined(__APPLE__)

#if defined(CPUINFO_SUPPORTED)
#include <cpuinfo.h>
#if defined(CPUIDINFO_ARCH_ARM)
Expand All @@ -74,6 +86,14 @@ void decodeMIDR(uint32_t midr, uint32_t uarch[1]);

namespace onnxruntime {

void CPUIDInfo::LogEarlyWarning(std::string_view message) {
if (logging::LoggingManager::HasDefaultLogger()) {
LOGS_DEFAULT(WARNING) << message;
} else {
std::cerr << "onnxruntime cpuid_info warning: " << message << "\n";
}
}

#if defined(CPUIDINFO_ARCH_X86)

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

vendor_ = GetX86Vendor(data);
vendor_id_ = GetVendorId(vendor_);

int num_IDs = data[0];
if (num_IDs >= 1) {
GetCPUID(1, data);
Expand Down Expand Up @@ -158,24 +175,8 @@ void CPUIDInfo::X86Init() {
}
}

std::string CPUIDInfo::GetX86Vendor(int32_t* data) {
char vendor[sizeof(int32_t) * 3 + 1]{};
*reinterpret_cast<int*>(vendor + 0) = data[1];
*reinterpret_cast<int*>(vendor + 4) = data[3];
*reinterpret_cast<int*>(vendor + 8) = data[2];
return vendor;
}

#endif // defined(CPUIDINFO_ARCH_X86)

uint32_t CPUIDInfo::GetVendorId(const std::string& vendor) {
if (vendor == "GenuineIntel") return 0x8086;
if (vendor == "AuthenticAMD") return 0x1022;
if (vendor.find("Qualcomm") == 0) return 'Q' | ('C' << 8) | ('O' << 16) | ('M' << 24);
if (vendor.find("NV") == 0) return 0x10DE;
return 0;
}

#if defined(CPUIDINFO_ARCH_ARM)

#if defined(__linux__)
Expand Down Expand Up @@ -228,10 +229,6 @@ void CPUIDInfo::ArmLinuxInit() {
#elif defined(_WIN32) // ^ defined(__linux__)

void CPUIDInfo::ArmWindowsInit() {
// Get the ARM vendor string from the registry
vendor_ = GetArmWindowsVendor();
vendor_id_ = GetVendorId(vendor_);

// Read MIDR and ID_AA64ISAR1_EL1 register values from Windows registry
// There should be one per CPU
std::vector<uint64_t> midr_values{}, id_aa64isar1_el1_values{};
Expand Down Expand Up @@ -323,15 +320,6 @@ void CPUIDInfo::ArmWindowsInit() {
#endif // defined(CPUINFO_SUPPORTED)
}

std::string CPUIDInfo::GetArmWindowsVendor() {
const int MAX_VALUE_NAME = 256;
const CHAR vendorKey[] = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
CHAR vendorVal[MAX_VALUE_NAME] = "";
unsigned long vendorSize = sizeof(char) * MAX_VALUE_NAME;
::RegGetValueA(HKEY_LOCAL_MACHINE, vendorKey, "Vendor Identifier", RRF_RT_REG_SZ | RRF_ZEROONFAILURE, nullptr, &vendorVal, &vendorSize);
return vendorVal;
}

#elif defined(__APPLE__) // ^ defined(_WIN32)

void CPUIDInfo::ArmAppleInit() {
Expand Down Expand Up @@ -376,16 +364,21 @@ uint32_t CPUIDInfo::GetCurrentCoreIdx() const {
}

CPUIDInfo::CPUIDInfo() {
#ifdef CPUIDINFO_ARCH_X86
X86Init();
#elif defined(CPUIDINFO_ARCH_ARM)
#if defined(CPUINFO_SUPPORTED)
pytorch_cpuinfo_init_ = cpuinfo_initialize();
if (!pytorch_cpuinfo_init_) {
LOGS_DEFAULT(WARNING) << "Failed to initialize PyTorch cpuinfo library. May cause CPU EP performance degradation "
"due to undetected CPU features.";
LogEarlyWarning(
"Failed to initialize PyTorch cpuinfo library. May cause CPU EP performance degradation due to undetected CPU "
"features.");
}
#endif // defined(CPUINFO_SUPPORTED)

// Note: This should be run after cpuinfo initialization if cpuinfo is enabled.
VendorInfoInit();

#ifdef CPUIDINFO_ARCH_X86
X86Init();
#elif defined(CPUIDINFO_ARCH_ARM)
#if defined(__linux__)
ArmLinuxInit();
#elif defined(_WIN32)
Expand Down
64 changes: 33 additions & 31 deletions onnxruntime/core/common/cpuid_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,40 @@ class CPUIDInfo {
}

private:
// Log function that uses ORT logging if available or writes to stderr.
// This enables us to log even before ORT logging has been initialized.
static void LogEarlyWarning(std::string_view message);

CPUIDInfo();

void VendorInfoInit();

#if defined(CPUIDINFO_ARCH_X86)

void X86Init();

#elif defined(CPUIDINFO_ARCH_ARM)

#if defined(__linux__)

void ArmLinuxInit();

#elif defined(_WIN32)

void ArmWindowsInit();

#elif defined(__APPLE__)

void ArmAppleInit();

#endif

#endif // defined(CPUIDINFO_ARCH_ARM)

#if defined(CPUINFO_SUPPORTED)
bool pytorch_cpuinfo_init_{false};
#endif // defined(CPUINFO_SUPPORTED)

bool has_amx_bf16_{false};
bool has_avx_{false};
bool has_avx2_{false};
Expand Down Expand Up @@ -132,37 +165,6 @@ class CPUIDInfo {

std::string vendor_;
uint32_t vendor_id_;

uint32_t GetVendorId(const std::string& vendor);

#if defined(CPUIDINFO_ARCH_X86)

void X86Init();
std::string GetX86Vendor(int32_t* data);

#elif defined(CPUIDINFO_ARCH_ARM)

#if defined(CPUINFO_SUPPORTED)
// Now the following var is only used in ARM build, but later on we may expand the usage.
bool pytorch_cpuinfo_init_{false};
#endif // defined(CPUINFO_SUPPORTED)

#if defined(__linux__)

void ArmLinuxInit();

#elif defined(_WIN32)

void ArmWindowsInit();
std::string GetArmWindowsVendor();

#elif defined(__APPLE__)

void ArmAppleInit();

#endif

#endif // defined(CPUIDINFO_ARCH_ARM)
};

} // namespace onnxruntime
Loading
Loading