Skip to content
Open
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
16 changes: 15 additions & 1 deletion cmake/external/onnxruntime_external_deps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,21 @@ if (CPUINFO_SUPPORTED)
# https://github.com/pytorch/cpuinfo/pull/324
${Patch_EXECUTABLE} -p1 < ${PROJECT_SOURCE_DIR}/patches/cpuinfo/patch_vcpkg_arm64ec_support.patch &&
# https://github.com/pytorch/cpuinfo/pull/348
${Patch_EXECUTABLE} -p1 < ${PROJECT_SOURCE_DIR}/patches/cpuinfo/win_arm_fp16_detection_fallback.patch
${Patch_EXECUTABLE} -p1 < ${PROJECT_SOURCE_DIR}/patches/cpuinfo/win_arm_fp16_detection_fallback.patch &&
# https://github.com/microsoft/onnxruntime/issues/10038
${Patch_EXECUTABLE} -p1 < ${PROJECT_SOURCE_DIR}/patches/cpuinfo/fix_missing_sysfs_fallback.patch
Comment on lines +376 to +378
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix_missing_sysfs_fallback.patch modifies Linux-only sources (src/linux/processors.c), but it’s being applied unconditionally in the “Windows ARM64/ARM64EC” cpuinfo patch chain. That increases the chance of Windows builds breaking in the future if the Linux patch stops applying cleanly (even though the fix is Linux-specific). Consider applying this patch only under the Linux branch (or gating it by CMAKE_SYSTEM_NAME STREQUAL "Linux").

Suggested change
${Patch_EXECUTABLE} -p1 < ${PROJECT_SOURCE_DIR}/patches/cpuinfo/win_arm_fp16_detection_fallback.patch &&
# https://github.com/microsoft/onnxruntime/issues/10038
${Patch_EXECUTABLE} -p1 < ${PROJECT_SOURCE_DIR}/patches/cpuinfo/fix_missing_sysfs_fallback.patch
${Patch_EXECUTABLE} -p1 < ${PROJECT_SOURCE_DIR}/patches/cpuinfo/win_arm_fp16_detection_fallback.patch

Copilot uses AI. Check for mistakes.
FIND_PACKAGE_ARGS NAMES cpuinfo
)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
message(STATUS "Applying sysfs fallback patch for cpuinfo on Linux")
onnxruntime_fetchcontent_declare(
pytorch_cpuinfo
URL ${DEP_URL_pytorch_cpuinfo}
URL_HASH SHA1=${DEP_SHA1_pytorch_cpuinfo}
EXCLUDE_FROM_ALL
PATCH_COMMAND
# https://github.com/microsoft/onnxruntime/issues/10038
${Patch_EXECUTABLE} -p1 < ${PROJECT_SOURCE_DIR}/patches/cpuinfo/fix_missing_sysfs_fallback.patch
FIND_PACKAGE_ARGS NAMES cpuinfo
)
else()
Expand Down
83 changes: 83 additions & 0 deletions cmake/patches/cpuinfo/fix_missing_sysfs_fallback.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
diff --git a/src/linux/processors.c b/src/linux/processors.c
index 47bee76..d0c5569 100644
--- a/src/linux/processors.c
+++ b/src/linux/processors.c
@@ -2,0 +3 @@
+#include <unistd.h>
@@ -291,0 +293,22 @@
+static uint32_t cpuinfo_linux_get_max_processor_from_sysconf(
+ uint32_t max_processors_count,
+ const char* processor_list_name) {
+ const long nproc = sysconf(_SC_NPROCESSORS_ONLN);
+ if (nproc <= 0) {
+ cpuinfo_log_warning(
+ "failed to query online processors from sysconf(_SC_NPROCESSORS_ONLN) for %s",
+ processor_list_name);
+ return UINT32_MAX;
+ }
+
+ uint32_t max_processor = (uint32_t)(nproc - 1);
+ if ((uint64_t)nproc > (uint64_t)max_processors_count) {
+ cpuinfo_log_warning(
+ "online processors count %ld exceeds system limit %" PRIu32 ": truncating to the latter",
+ nproc,
+ max_processors_count);
+ max_processor = max_processors_count - 1;
+ }
+ return max_processor;
+}
+
@@ -301 +324 @@
- return UINT32_MAX;
+ return cpuinfo_linux_get_max_processor_from_sysconf(max_processors_count, POSSIBLE_CPULIST_FILENAME);
@@ -323 +346 @@
- return UINT32_MAX;
+ return cpuinfo_linux_get_max_processor_from_sysconf(max_processors_count, PRESENT_CPULIST_FILENAME);
@@ -357,0 +381,31 @@
+static bool cpuinfo_linux_detect_processors_from_sysconf(
+ uint32_t max_processors_count,
+ uint32_t* processor0_flags,
+ uint32_t processor_struct_size,
+ uint32_t detected_flag,
+ const char* processor_list_name) {
+ const long nproc = sysconf(_SC_NPROCESSORS_ONLN);
+ if (nproc <= 0) {
+ cpuinfo_log_warning(
+ "failed to query online processors from sysconf(_SC_NPROCESSORS_ONLN) for %s",
+ processor_list_name);
+ return false;
+ }
+
+ uint32_t processors_count = (uint32_t)nproc;
+ if ((uint64_t)nproc > (uint64_t)max_processors_count) {
+ cpuinfo_log_warning(
+ "online processors count %ld exceeds system limit %" PRIu32 ": truncating to the latter",
+ nproc,
+ max_processors_count);
+ processors_count = max_processors_count;
+ }
+
+ for (uint32_t processor = 0; processor < processors_count; processor++) {
+ *((uint32_t*)((uintptr_t)processor0_flags + processor_struct_size * processor)) |= detected_flag;
+ }
+ cpuinfo_log_warning(
+ "falling back to sysconf(_SC_NPROCESSORS_ONLN) = %ld for %s", nproc, processor_list_name);
+ return true;
+}
+
@@ -373 +427,6 @@
- return false;
+ return cpuinfo_linux_detect_processors_from_sysconf(
+ max_processors_count,
+ processor0_flags,
+ processor_struct_size,
+ possible_flag,
+ POSSIBLE_CPULIST_FILENAME);
@@ -392 +451,6 @@
- return false;
+ return cpuinfo_linux_detect_processors_from_sysconf(
+ max_processors_count,
+ processor0_flags,
+ processor_struct_size,
+ present_flag,
+ PRESENT_CPULIST_FILENAME);
12 changes: 11 additions & 1 deletion onnxruntime/core/platform/posix/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,17 @@ class PosixEnv : public Env {
PosixEnv() {
cpuinfo_available_ = cpuinfo_initialize();
if (!cpuinfo_available_) {
LOGS_DEFAULT(INFO) << "cpuinfo_initialize failed";
// PosixEnv may be constructed before the logging system is initialized
// (e.g. via a static Env::Default() reference in the Python bindings).
// Using LOGS_DEFAULT here would crash with "Attempt to use DefaultLogger
// but none has been registered". Fall back to stderr when no logger exists.
if (logging::LoggingManager::HasDefaultLogger()) {
LOGS_DEFAULT(WARNING) << "cpuinfo_initialize failed. "
"May cause CPU EP performance degradation due to undetected CPU features.";
} else {
std::cerr << "onnxruntime warning: cpuinfo_initialize failed. "
"May cause CPU EP performance degradation due to undetected CPU features.\n";
}
}
}
bool cpuinfo_available_{false};
Expand Down
Loading
Loading