Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 510ae28

Browse files
fix: default GPUs setting (#2029)
Co-authored-by: sangjanai <sang@jan.ai>
1 parent ca2327a commit 510ae28

File tree

4 files changed

+61
-27
lines changed

4 files changed

+61
-27
lines changed

engine/common/hardware_common.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ struct NvidiaAddInfo {
6969
};
7070
struct AmdAddInfo {};
7171
using GPUAddInfo = std::variant<NvidiaAddInfo, AmdAddInfo>;
72+
73+
enum class GpuType {
74+
kGpuTypeOther = 0,
75+
kGpuTypeIntegrated = 1,
76+
kGpuTypeDiscrete = 2,
77+
kGpuTypeVirtual = 3,
78+
kGpuTypeCpu = 4,
79+
kGpuTypeMaxEnum = 0x7FFFFFFF
80+
};
81+
7282
struct GPU {
7383
std::string id;
7484
uint32_t device_id;
@@ -80,6 +90,7 @@ struct GPU {
8090
std::string uuid;
8191
bool is_activated = true;
8292
std::string vendor;
93+
GpuType gpu_type;
8394
};
8495

8596
inline Json::Value ToJson(const std::vector<GPU>& gpus) {

engine/services/hardware_service.cc

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -322,23 +322,39 @@ void HardwareService::UpdateHardwareInfos() {
322322
}
323323
}
324324
CTL_INF("Activated GPUs before: " << debug_b);
325+
auto has_nvidia = [&gpus] {
326+
for (auto const& g : gpus) {
327+
if (g.vendor == cortex::hw::kNvidiaStr) {
328+
return true;
329+
}
330+
}
331+
return false;
332+
}();
333+
325334
for (auto const& gpu : gpus) {
326-
// ignore error
327-
// Note: only support NVIDIA for now, so hardware_id = software_id
328335
if (db_service_->HasHardwareEntry(gpu.uuid)) {
329336
auto res = db_service_->UpdateHardwareEntry(gpu.uuid, std::stoi(gpu.id),
330-
std::stoi(gpu.id));
337+
std::stoi(gpu.id));
331338
if (res.has_error()) {
332339
CTL_WRN(res.error());
333340
}
334341
} else {
335-
auto res =
336-
db_service_->AddHardwareEntry(HwEntry{.uuid = gpu.uuid,
337-
.type = "gpu",
338-
.hardware_id = std::stoi(gpu.id),
339-
.software_id = std::stoi(gpu.id),
340-
.activated = true,
341-
.priority = INT_MAX});
342+
// iGPU should be deactivated by default
343+
// Only activate Nvidia GPUs if both AMD and Nvidia GPUs exists
344+
auto activated = [&gpu, &gpus, has_nvidia] {
345+
if (gpu.gpu_type != cortex::hw::GpuType::kGpuTypeDiscrete)
346+
return false;
347+
if (has_nvidia && gpu.vendor != cortex::hw::kNvidiaStr)
348+
return false;
349+
return true;
350+
};
351+
auto res = db_service_->AddHardwareEntry(
352+
HwEntry{.uuid = gpu.uuid,
353+
.type = "gpu",
354+
.hardware_id = std::stoi(gpu.id),
355+
.software_id = std::stoi(gpu.id),
356+
.activated = activated(),
357+
.priority = INT_MAX});
342358
if (res.has_error()) {
343359
CTL_WRN(res.error());
344360
}

engine/utils/hardware/gpu/vulkan/vulkan_gpu.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,26 @@
2424
#endif
2525

2626
namespace cortex::hw {
27-
constexpr const uint32_t NVIDIA_VENDOR = 0x10DE;
28-
constexpr const uint32_t AMD_VENDOR = 0x1002;
29-
constexpr const uint32_t INTEL_VENDOR = 0x8086;
30-
constexpr const uint32_t ARM_VENDOR = 0x13B5;
27+
constexpr const uint32_t kNvidiaVendor = 0x10DE;
28+
constexpr const uint32_t kAmdVendor = 0x1002;
29+
constexpr const uint32_t kIntelVendor = 0x8086;
30+
constexpr const uint32_t kArmVendor = 0x13B5;
31+
32+
constexpr const auto kAmdStr = "AMD";
33+
constexpr const auto kNvidiaStr = "NVIDIA";
34+
constexpr const auto kIntelStr = "INTEL";
35+
constexpr const auto kArmStr = "ARM";
3136

3237
inline std::string GetVendorStr(uint32_t vendor_id) {
3338
switch (vendor_id) {
34-
case AMD_VENDOR:
35-
return "AMD";
36-
case NVIDIA_VENDOR:
37-
return "NVIDIA";
38-
case INTEL_VENDOR:
39-
return "INTEL";
40-
case ARM_VENDOR:
41-
return "ARM";
39+
case kAmdVendor:
40+
return kAmdStr;
41+
case kNvidiaVendor:
42+
return kNvidiaStr;
43+
case kIntelVendor:
44+
return kIntelStr;
45+
case kArmVendor:
46+
return kArmStr;
4247
default:
4348
return std::to_string(vendor_id);
4449
}
@@ -446,8 +451,8 @@ class VulkanGpu {
446451
#endif
447452
int free_vram_MiB =
448453
total_vram_MiB > used_vram_MiB ? total_vram_MiB - used_vram_MiB : 0;
449-
if (device_properties.vendorID == NVIDIA_VENDOR ||
450-
device_properties.vendorID == AMD_VENDOR) {
454+
if (device_properties.vendorID == kNvidiaVendor ||
455+
device_properties.vendorID == kAmdVendor) {
451456
gpus.emplace_back(cortex::hw::GPU{
452457
.id = std::to_string(id),
453458
.device_id = device_properties.deviceID,
@@ -457,7 +462,8 @@ class VulkanGpu {
457462
.free_vram = free_vram_MiB,
458463
.total_vram = total_vram_MiB,
459464
.uuid = uuid_to_string(device_id_properties.deviceUUID),
460-
.vendor = GetVendorStr(device_properties.vendorID)});
465+
.vendor = GetVendorStr(device_properties.vendorID),
466+
.gpu_type = static_cast<GpuType>(device_properties.deviceType)});
461467
}
462468
id++;
463469
}

engine/utils/hardware/gpu_info.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ inline std::vector<GPU> GetGPUInfo() {
2525
.compute_cap = nvidia_gpus[i].compute_cap.value_or("unknown")};
2626
vulkan_gpus[j].free_vram = std::stoll(nvidia_gpus[i].vram_free);
2727
vulkan_gpus[j].total_vram = std::stoll(nvidia_gpus[i].vram_total);
28-
vulkan_gpus[j].vendor = nvidia_gpus[i].vendor;
28+
vulkan_gpus[j].vendor = nvidia_gpus[i].vendor;
2929
}
3030
}
3131
}
@@ -55,7 +55,8 @@ inline std::vector<GPU> GetGPUInfo() {
5555
.free_vram = std::stoi(n.vram_free),
5656
.total_vram = std::stoi(n.vram_total),
5757
.uuid = n.uuid,
58-
.vendor = n.vendor});
58+
.vendor = n.vendor,
59+
.gpu_type = GpuType::kGpuTypeDiscrete});
5960
}
6061
return res;
6162
}

0 commit comments

Comments
 (0)