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

Commit cbfe6c7

Browse files
authored
Merge pull request #1914 from janhq/chore/cherry-pick-amd-hw
chore: cherry pick AMD hardware API support
2 parents c2d8f61 + 763026e commit cbfe6c7

33 files changed

+9777
-44
lines changed

docs/docs/architecture/cortex-db.mdx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ import TabItem from "@theme/TabItem";
1414
This document outlines the architecture of the database designed to store and manage various types of entities and their associated metadata.
1515

1616
## Table Structure
17+
### schema Table
18+
The `schema` table is designed to hold schema version for cortex database. Below is the structure of the table:
19+
20+
| Column Name | Data Type | Description |
21+
|--------------------|-----------|---------------------------------------------------------|
22+
| schema_version | INTEGER | A unique schema version for database. |
23+
1724
### models Table
1825
The `models` table is designed to hold metadata about various AI models. Below is the structure of the table:
1926

@@ -22,4 +29,23 @@ The `models` table is designed to hold metadata about various AI models. Below i
2229
| model_id | TEXT | A unique identifier for each model (Primary Key). |
2330
| author_repo_id | TEXT | The identifier for the repository where the model is stored. |
2431
| branch_name | TEXT | The branch name in the repository that contains the model. |
25-
| path_to_model_yaml | TEXT | The file path to the YAML configuration file for the model. |
32+
| path_to_model_yaml | TEXT | The file path to the YAML configuration file for the model. |
33+
| model_alias | TEXT | The optional alias or friendly name for the model. |
34+
| model_format | TEXT | The format or type of the model (e.g., TensorFlow, PyTorch, GGUF, etc.). |
35+
| model_source | TEXT | The source or origin of the model (e.g., a URL or file path). |
36+
| status | TEXT | Current status of the model (e.g., "downloaded", "downloadable").. |
37+
| engine | TEXT | The name or identifier of the engine or framework used for running or deploying the model.. |
38+
| metadata | TEXT | Additional metadata or information about the model, such as a JSON string containing various attributes or properties. |
39+
40+
### hardware Table
41+
The `hardware` table is designed to hold metadata about hardware information. Below is the structure of the table:
42+
43+
| Column Name | Data Type | Description |
44+
|--------------------|-----------|---------------------------------------------------------|
45+
| uuid | TEXT | the primary key for the table, meaning that each row must have a unique value in this column. |
46+
| type | TEXT | The type of hardware. |
47+
| hardware_id | INTEGER | An integer value representing the hardware ID. |
48+
| software_id | INTEGER | An integer value representing the software ID associated with the hardware. |
49+
| activated | INTEGER | A boolean value (0 or 1) indicating whether the hardware is activated or not. |
50+
| priority | INTEGER | An integer value representing the priority associated with the hardware. |
51+

engine/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*.so
3333
*.so.*
3434
*.dylib
35+
!**/libvulkan.so
3536

3637
# Executables
3738
*.exe

engine/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ endif()
6868

6969
add_subdirectory(cli)
7070

71+
72+
7173
find_package(jsoncpp CONFIG REQUIRED)
7274
find_package(Drogon CONFIG REQUIRED)
7375
find_package(yaml-cpp CONFIG REQUIRED)

engine/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,15 @@ ifeq ($(OS),Windows_NT)
7373
@powershell -Command "mkdir -p cortex;"
7474
@powershell -Command "cp build\cortex-server.exe .\cortex\$(DESTINATION_BINARY_SERVER_NAME).exe;"
7575
@powershell -Command "cp build\cortex.exe .\cortex\$(DESTINATION_BINARY_NAME).exe;"
76+
@powershell -Command "cp ..\engine\deps\windows\vulkan-1.dll .\cortex\;"
7677
@powershell -Command "cp ..\.github\patches\windows\msvcp140.dll .\cortex\;"
7778
@powershell -Command "cp ..\.github\patches\windows\vcruntime140_1.dll .\cortex\;"
7879
@powershell -Command "cp ..\.github\patches\windows\vcruntime140.dll .\cortex\;"
7980
else ifeq ($(shell uname -s),Linux)
8081
@mkdir -p cortex; \
8182
cp build/cortex-server cortex/$(DESTINATION_BINARY_SERVER_NAME); \
82-
cp build/cortex cortex/$(DESTINATION_BINARY_NAME);
83+
cp build/cortex cortex/$(DESTINATION_BINARY_NAME); \
84+
cp ../engine/deps/linux/libvulkan.so cortex/libvulkan.so;
8385
else
8486
@mkdir -p cortex; \
8587
cp build/cortex-server cortex/$(DESTINATION_BINARY_SERVER_NAME); \

engine/cli/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,9 @@ set_target_properties(${TARGET_NAME} PROPERTIES
140140
RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}
141141
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
142142
)
143+
144+
if(UNIX AND NOT APPLE)
145+
configure_file("${PROJECT_SOURCE_DIR}/../deps/linux/libvulkan.so" "${CMAKE_BINARY_DIR}/libvulkan.so" COPYONLY)
146+
elseif(MSVC)
147+
configure_file("${PROJECT_SOURCE_DIR}/../deps/windows/vulkan-1.dll" "${CMAKE_BINARY_DIR}/vulkan-1.dll" COPYONLY)
148+
endif()

engine/cli/commands/hardware_list_cmd.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ bool HardwareListCmd::Exec(const std::string& host, int port,
4141
if (!ho.has_value() || ho.value().show_cpu) {
4242
std::cout << "CPU Information:" << std::endl;
4343
Table table;
44-
std::vector<std::string> column_headers{"#", "Arch", "Cores", "Model",
45-
"Instructions"};
44+
std::vector<std::string> column_headers{"#", "Arch", "Cores",
45+
"Model", "Usage", "Instructions"};
4646

4747
Row_t header{column_headers.begin(), column_headers.end()};
4848
table.add_row(header);
@@ -52,6 +52,7 @@ bool HardwareListCmd::Exec(const std::string& host, int port,
5252
row.emplace_back(cpu.arch);
5353
row.emplace_back(std::to_string(cpu.cores));
5454
row.emplace_back(cpu.model);
55+
row.emplace_back(std::to_string(cpu.usage));
5556
std::string insts;
5657
for (auto const& i : cpu.instructions) {
5758
insts += i + " ";
@@ -130,6 +131,7 @@ bool HardwareListCmd::Exec(const std::string& host, int port,
130131
std::get<cortex::hw::NvidiaAddInfo>(gpu.add_info).compute_cap);
131132
row.emplace_back(gpu.is_activated ? "Yes" : "No");
132133
table.add_row({row.begin(), row.end()});
134+
count++;
133135
}
134136

135137
std::cout << table << std::endl;

engine/common/hardware_common.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
2+
#include <assert.h>
23
#include <json/json.h>
34
#include <string>
45
#include <variant>
56
#include <vector>
6-
#include <assert.h>
77

88
namespace cortex::hw {
99

@@ -26,6 +26,7 @@ struct CPU {
2626
int cores;
2727
std::string arch;
2828
std::string model;
29+
float usage;
2930
std::vector<std::string> instructions;
3031
};
3132

@@ -34,6 +35,7 @@ inline Json::Value ToJson(const CPU& cpu) {
3435
res["arch"] = cpu.arch;
3536
res["cores"] = cpu.cores;
3637
res["model"] = cpu.model;
38+
res["usage"] = cpu.usage;
3739
Json::Value insts(Json::arrayValue);
3840
for (auto const& i : cpu.instructions) {
3941
insts.append(i);
@@ -47,11 +49,16 @@ inline CPU FromJson(const Json::Value& root) {
4749
int cores = root["cores"].asInt();
4850
std::string arch = root["arch"].asString();
4951
std::string model = root["model"].asString();
52+
float usage = root["usage"].asFloat();
5053
std::vector<std::string> insts;
5154
for (auto const& i : root["instructions"]) {
5255
insts.emplace_back(i.asString());
5356
}
54-
return {.cores = cores, .arch = arch, .model = model, .instructions = insts};
57+
return {.cores = cores,
58+
.arch = arch,
59+
.model = model,
60+
.usage = usage,
61+
.instructions = insts};
5562
}
5663
} // namespace cpu
5764

@@ -64,6 +71,7 @@ struct AmdAddInfo {};
6471
using GPUAddInfo = std::variant<NvidiaAddInfo, AmdAddInfo>;
6572
struct GPU {
6673
std::string id;
74+
uint32_t device_id;
6775
std::string name;
6876
std::string version;
6977
GPUAddInfo add_info;
@@ -77,7 +85,7 @@ inline Json::Value ToJson(const std::vector<GPU>& gpus) {
7785
Json::Value res(Json::arrayValue);
7886
for (size_t i = 0; i < gpus.size(); i++) {
7987
Json::Value gpu;
80-
gpu["id"] = std::to_string(i);
88+
gpu["id"] = gpus[i].id;
8189
gpu["name"] = gpus[i].name;
8290
gpu["version"] = gpus[i].version;
8391
Json::Value add_info;
@@ -142,7 +150,6 @@ inline OS FromJson(const Json::Value& root) {
142150
}
143151
} // namespace os
144152

145-
146153
struct PowerInfo {
147154
std::string charging_status;
148155
int battery_life;
@@ -165,7 +172,6 @@ inline PowerInfo FromJson(const Json::Value& root) {
165172
}
166173
} // namespace power
167174

168-
169175
namespace {
170176
int64_t ByteToMiB(int64_t b) {
171177
return b / 1024 / 1024;
@@ -214,4 +220,4 @@ inline StorageInfo FromJson(const Json::Value& root) {
214220
.available = root["available"].asInt64()};
215221
}
216222
} // namespace storage
217-
}
223+
} // namespace cortex::hw

engine/database/hardware.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,41 @@ cpp::result<bool, std::string> Hardware::DeleteHardwareEntry(
9898
return cpp::fail(e.what());
9999
}
100100
}
101+
102+
bool Hardware::HasHardwareEntry(const std::string& id) {
103+
try {
104+
SQLite::Statement query(db_,
105+
"SELECT COUNT(*) FROM hardware WHERE uuid = ?");
106+
query.bind(1, id);
107+
if (query.executeStep()) {
108+
return query.getColumn(0).getInt() > 0;
109+
}
110+
return false;
111+
} catch (const std::exception& e) {
112+
CTL_WRN(e.what());
113+
return false;
114+
}
115+
}
116+
117+
cpp::result<bool, std::string> Hardware::UpdateHardwareEntry(const std::string& id,
118+
int hw_id,
119+
int sw_id) const {
120+
try {
121+
SQLite::Statement upd(
122+
db_,
123+
"UPDATE hardware "
124+
"SET hardware_id = ?, software_id = ? "
125+
"WHERE uuid = ?");
126+
upd.bind(1, hw_id);
127+
upd.bind(2, sw_id);
128+
upd.bind(3, id);
129+
if (upd.exec() == 1) {
130+
CTL_INF("Updated: " << id << " " << hw_id << " " << sw_id);
131+
return true;
132+
}
133+
return false;
134+
} catch (const std::exception& e) {
135+
return cpp::fail(e.what());
136+
}
137+
}
101138
} // namespace cortex::db

engine/database/hardware.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,9 @@ class Hardware {
4343
cpp::result<bool, std::string> UpdateHardwareEntry(
4444
const std::string& id, const HardwareEntry& updated_entry);
4545
cpp::result<bool, std::string> DeleteHardwareEntry(const std::string& id);
46+
bool HasHardwareEntry(const std::string& id);
47+
cpp::result<bool, std::string> UpdateHardwareEntry(const std::string& id,
48+
int hw_id,
49+
int sw_id) const;
4650
};
4751
} // namespace cortex::db

engine/deps/linux/libvulkan.so

3.41 MB
Binary file not shown.

0 commit comments

Comments
 (0)