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

Code before:
  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{};
  const auto device_id_path = sysfs_path / "device" / "device";
  ORT_RETURN_IF_ERROR(ReadValueFromFile(device_id_path, device_id));
  gpu_device.device_id = device_id;

  // metadata
  gpu_device.metadata.Add("card_idx", MakeString(path_info.card_idx));

  if (const auto is_gpu_discrete = IsGpuDiscrete(vendor_id, device_id);
      is_gpu_discrete.has_value()) {
    gpu_device.metadata.Add("Discrete", (*is_gpu_discrete ? "1" : "0"));
  }

  gpu_device.metadata.Add("pci_bus_id", std::filesystem::read_symlink(sysfs_path / "device").filename().string());  // e.g. 0000:65:00.0

  gpu_device.type = OrtHardwareDeviceType_GPU;

  gpu_device_out = std::move(gpu_device);
  return Status::OK();
}

Status GetGpuDevices(std::vector<OrtHardwareDevice>& gpu_devices_out) {
  std::vector<GpuSysfsPathInfo> gpu_sysfs_path_infos{};
  ORT_RETURN_IF_ERROR(DetectGpuSysfsPaths(gpu_sysfs_path_infos));

  std::vector<OrtHardwareDevice> gpu_devices{};
  gpu_devices.reserve(gpu_sysfs_path_infos.size());

  for (const auto& gpu_sysfs_path_info : gpu_sysfs_path_infos) {
    OrtHardwareDevice gpu_device{};
    ORT_RETURN_IF_ERROR(GetGpuDeviceFromSysfs(gpu_sysfs_path_info, gpu_device));
    gpu_devices.emplace_back(std::move(gpu_device));
  }

  gpu_devices_out = std::move(gpu_devices);
  return Status::OK();
}

}  // namespace

Review comment (bug):
is it possible for `std::filesystem::read_symlink` to fail here? if so, could we use the non-throwing overload and have this function return a failure status instead?

we have some tolerance of GPU device discovery failure here, but it doesn't handle exceptions.
https://github.com/microsoft/onnxruntime/blob/a19954cd9a0d666dcb438e364ce1758f3548369c/onnxruntime/core/platform/linux/device_discovery.cc#L176-L183

Code after:
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{};
  const auto device_id_path = sysfs_path / "device" / "device";
  ORT_RETURN_IF_ERROR(ReadValueFromFile(device_id_path, device_id));
  gpu_device.device_id = device_id;

  // metadata
  gpu_device.metadata.Add("card_idx", MakeString(path_info.card_idx));

  if (const auto is_gpu_discrete = IsGpuDiscrete(vendor_id, device_id);
      is_gpu_discrete.has_value()) {
    gpu_device.metadata.Add("Discrete", (*is_gpu_discrete ? "1" : "0"));
  }

  std::optional<std::string> pci_bus_id;
  ORT_RETURN_IF_ERROR(GetPciBusId(sysfs_path, pci_bus_id));
  if (pci_bus_id) {