Repository: microsoft/onnxruntime
File: onnxruntime/core/platform/linux/device_discovery.cc (C++)

Code before:
}

std::optional<bool> IsGpuDiscrete(uint16_t vendor_id, uint16_t device_id) {
  ORT_UNUSED_PARAMETER(device_id);

  // Currently, we only assume that all Nvidia GPUs are discrete.

  constexpr auto kNvidiaPciId = 0x10de;
  if (vendor_id == kNvidiaPciId) {
    return true;
  }

  return std::nullopt;
}

Status GetPciBusId(const std::filesystem::path& sysfs_path, std::optional<std::string>& pci_bus_id) {
  constexpr const char* regex_pattern{R"([0-9a-f]+:[0-9a-f]+:[0-9a-f]+[.][0-9a-f]+)"};
  static const std::regex pci_bus_id_regex(regex_pattern);

  std::error_code error_code;
  auto pci_bus_id_path = std::filesystem::canonical(sysfs_path / "device", error_code);  // resolves symlink to PCI bus id, e.g. 0000:65:00.0
  ORT_RETURN_IF_ERROR(ErrorCodeToStatus(error_code));

  auto pci_bus_id_filename = pci_bus_id_path.filename();
  if (!std::regex_match(pci_bus_id_filename.string(), pci_bus_id_regex)) {
    return Status{common::StatusCategory::ONNXRUNTIME, common::StatusCode::FAIL,
                  MakeString("Expected PCI path at \"",
                             pci_bus_id_path.string(),
                             "\" did not match expected pattern of ",
                             regex_pattern,
                             " for the filename \"",
                             pci_bus_id_filename, "\"")};
  }

  pci_bus_id = pci_bus_id_filename.string();
  return Status::OK();
}

Status GetGpuDeviceFromSysfs(const GpuSysfsPathInfo& path_info, OrtHardwareDevice& gpu_device_out) {
  OrtHardwareDevice gpu_device{};
  const auto& sysfs_path = path_info.path;

  // vendor id
  uint16_t vendor_id{};
  const auto vendor_id_path = sysfs_path / "device" / "vendor";
  ORT_RETURN_IF_ERROR(ReadValueFromFile(vendor_id_path, vendor_id));
  gpu_device.vendor_id = vendor_id;

  // TODO vendor name

  // device id

Review comment (bug):
from a failed CI build:
`1: 2026-01-20 20:00:44.635509407 [W:onnxruntime:Default, device_discovery.cc:212 DiscoverDevicesForPlatform] GPU device discovery failed: Expected PCI path at "/sys/devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0004:00/VMBUS:00/5620e0c7-8062-4dce-aeb7-520c7ef76171" did not match expected pattern of [0-9a-f]+:[0-9a-f]+:[0-9a-f]+[.][0-9a-f]+ for the filename ""5620e0c7-8062-4dce-aeb7-520c7ef76171""`

maybe we make the `regex_match` failure return an empty optional instead of failing? good error message though, maybe log that instead.

Code after:
}

std::optional<bool> IsGpuDiscrete(uint16_t vendor_id, uint16_t device_id) {
  ORT_UNUSED_PARAMETER(device_id);

  // Currently, we only assume that all Nvidia GPUs are discrete.

  constexpr auto kNvidiaPciId = 0x10de;
  if (vendor_id == kNvidiaPciId) {
    return true;
  }

  return std::nullopt;
}

Status GetPciBusId(const std::filesystem::path& sysfs_path, std::optional<std::string>& pci_bus_id) {
  constexpr const char* regex_pattern{R"([0-9a-f]+:[0-9a-f]+:[0-9a-f]+[.][0-9a-f]+)"};
  static const std::regex pci_bus_id_regex(regex_pattern);

  std::error_code error_code;
  auto pci_bus_id_path = std::filesystem::canonical(sysfs_path / "device", error_code);  // resolves symlink to PCI bus id, e.g. 0000:65:00.0
  ORT_RETURN_IF_ERROR(ErrorCodeToStatus(error_code));

  auto pci_bus_id_filename = pci_bus_id_path.filename();
  if (std::regex_match(pci_bus_id_filename.string(), pci_bus_id_regex)) {
    pci_bus_id = pci_bus_id_filename.string();
  } else {
    pci_bus_id = {};
    LOGS_DEFAULT(WARNING) << MakeString("Skipping pci_bus_id for PCI path at \"",
                                        pci_bus_id_path.string(),
                                        "\" because filename \"", pci_bus_id_filename, "\" dit not match expected pattern of ",
                                        regex_pattern);
  };

  return Status::OK();
}

Status GetGpuDeviceFromSysfs(const GpuSysfsPathInfo& path_info, OrtHardwareDevice& gpu_device_out) {
  OrtHardwareDevice gpu_device{};
  const auto& sysfs_path = path_info.path;

  // vendor id
  uint16_t vendor_id{};
  const auto vendor_id_path = sysfs_path / "device" / "vendor";
  ORT_RETURN_IF_ERROR(ReadValueFromFile(vendor_id_path, vendor_id));
  gpu_device.vendor_id = vendor_id;

  // TODO vendor name

  // device id
  uint16_t device_id{};