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

Code before:

  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
  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

Review comment (suggestion):
nit: [`path::operator<<`](https://en.cppreference.com/w/cpp/filesystem/path/operator_ltltgtgt.html) should handle quoting already, so this could simply be
```suggestion
                  MakeString("Expected PCI path at ",
                             pci_bus_id_path,
                             " did not match expected pattern of ",
                             regex_pattern,
                             " for the filename ",
                             pci_bus_id_filename)};
```

Code after:

  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{};
  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));