Skip to content

Commit

Permalink
Merge pull request #1914 from janhq/chore/cherry-pick-amd-hw
Browse files Browse the repository at this point in the history
chore: cherry pick AMD hardware API support
  • Loading branch information
vansangpfiev authored Feb 3, 2025
2 parents c2d8f61 + 763026e commit cbfe6c7
Show file tree
Hide file tree
Showing 33 changed files with 9,777 additions and 44 deletions.
28 changes: 27 additions & 1 deletion docs/docs/architecture/cortex-db.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import TabItem from "@theme/TabItem";
This document outlines the architecture of the database designed to store and manage various types of entities and their associated metadata.

## Table Structure
### schema Table
The `schema` table is designed to hold schema version for cortex database. Below is the structure of the table:

| Column Name | Data Type | Description |
|--------------------|-----------|---------------------------------------------------------|
| schema_version | INTEGER | A unique schema version for database. |

### models Table
The `models` table is designed to hold metadata about various AI models. Below is the structure of the table:

Expand All @@ -22,4 +29,23 @@ The `models` table is designed to hold metadata about various AI models. Below i
| model_id | TEXT | A unique identifier for each model (Primary Key). |
| author_repo_id | TEXT | The identifier for the repository where the model is stored. |
| branch_name | TEXT | The branch name in the repository that contains the model. |
| path_to_model_yaml | TEXT | The file path to the YAML configuration file for the model. |
| path_to_model_yaml | TEXT | The file path to the YAML configuration file for the model. |
| model_alias | TEXT | The optional alias or friendly name for the model. |
| model_format | TEXT | The format or type of the model (e.g., TensorFlow, PyTorch, GGUF, etc.). |
| model_source | TEXT | The source or origin of the model (e.g., a URL or file path). |
| status | TEXT | Current status of the model (e.g., "downloaded", "downloadable").. |
| engine | TEXT | The name or identifier of the engine or framework used for running or deploying the model.. |
| metadata | TEXT | Additional metadata or information about the model, such as a JSON string containing various attributes or properties. |

### hardware Table
The `hardware` table is designed to hold metadata about hardware information. Below is the structure of the table:

| Column Name | Data Type | Description |
|--------------------|-----------|---------------------------------------------------------|
| uuid | TEXT | the primary key for the table, meaning that each row must have a unique value in this column. |
| type | TEXT | The type of hardware. |
| hardware_id | INTEGER | An integer value representing the hardware ID. |
| software_id | INTEGER | An integer value representing the software ID associated with the hardware. |
| activated | INTEGER | A boolean value (0 or 1) indicating whether the hardware is activated or not. |
| priority | INTEGER | An integer value representing the priority associated with the hardware. |

1 change: 1 addition & 0 deletions engine/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*.so
*.so.*
*.dylib
!**/libvulkan.so

# Executables
*.exe
Expand Down
2 changes: 2 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ endif()

add_subdirectory(cli)



find_package(jsoncpp CONFIG REQUIRED)
find_package(Drogon CONFIG REQUIRED)
find_package(yaml-cpp CONFIG REQUIRED)
Expand Down
4 changes: 3 additions & 1 deletion engine/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ ifeq ($(OS),Windows_NT)
@powershell -Command "mkdir -p cortex;"
@powershell -Command "cp build\cortex-server.exe .\cortex\$(DESTINATION_BINARY_SERVER_NAME).exe;"
@powershell -Command "cp build\cortex.exe .\cortex\$(DESTINATION_BINARY_NAME).exe;"
@powershell -Command "cp ..\engine\deps\windows\vulkan-1.dll .\cortex\;"
@powershell -Command "cp ..\.github\patches\windows\msvcp140.dll .\cortex\;"
@powershell -Command "cp ..\.github\patches\windows\vcruntime140_1.dll .\cortex\;"
@powershell -Command "cp ..\.github\patches\windows\vcruntime140.dll .\cortex\;"
else ifeq ($(shell uname -s),Linux)
@mkdir -p cortex; \
cp build/cortex-server cortex/$(DESTINATION_BINARY_SERVER_NAME); \
cp build/cortex cortex/$(DESTINATION_BINARY_NAME);
cp build/cortex cortex/$(DESTINATION_BINARY_NAME); \
cp ../engine/deps/linux/libvulkan.so cortex/libvulkan.so;
else
@mkdir -p cortex; \
cp build/cortex-server cortex/$(DESTINATION_BINARY_SERVER_NAME); \
Expand Down
6 changes: 6 additions & 0 deletions engine/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,9 @@ set_target_properties(${TARGET_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

if(UNIX AND NOT APPLE)
configure_file("${PROJECT_SOURCE_DIR}/../deps/linux/libvulkan.so" "${CMAKE_BINARY_DIR}/libvulkan.so" COPYONLY)
elseif(MSVC)
configure_file("${PROJECT_SOURCE_DIR}/../deps/windows/vulkan-1.dll" "${CMAKE_BINARY_DIR}/vulkan-1.dll" COPYONLY)
endif()
6 changes: 4 additions & 2 deletions engine/cli/commands/hardware_list_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ bool HardwareListCmd::Exec(const std::string& host, int port,
if (!ho.has_value() || ho.value().show_cpu) {
std::cout << "CPU Information:" << std::endl;
Table table;
std::vector<std::string> column_headers{"#", "Arch", "Cores", "Model",
"Instructions"};
std::vector<std::string> column_headers{"#", "Arch", "Cores",
"Model", "Usage", "Instructions"};

Row_t header{column_headers.begin(), column_headers.end()};
table.add_row(header);
Expand All @@ -52,6 +52,7 @@ bool HardwareListCmd::Exec(const std::string& host, int port,
row.emplace_back(cpu.arch);
row.emplace_back(std::to_string(cpu.cores));
row.emplace_back(cpu.model);
row.emplace_back(std::to_string(cpu.usage));
std::string insts;
for (auto const& i : cpu.instructions) {
insts += i + " ";
Expand Down Expand Up @@ -130,6 +131,7 @@ bool HardwareListCmd::Exec(const std::string& host, int port,
std::get<cortex::hw::NvidiaAddInfo>(gpu.add_info).compute_cap);
row.emplace_back(gpu.is_activated ? "Yes" : "No");
table.add_row({row.begin(), row.end()});
count++;
}

std::cout << table << std::endl;
Expand Down
18 changes: 12 additions & 6 deletions engine/common/hardware_common.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once
#include <assert.h>
#include <json/json.h>
#include <string>
#include <variant>
#include <vector>
#include <assert.h>

namespace cortex::hw {

Expand All @@ -26,6 +26,7 @@ struct CPU {
int cores;
std::string arch;
std::string model;
float usage;
std::vector<std::string> instructions;
};

Expand All @@ -34,6 +35,7 @@ inline Json::Value ToJson(const CPU& cpu) {
res["arch"] = cpu.arch;
res["cores"] = cpu.cores;
res["model"] = cpu.model;
res["usage"] = cpu.usage;
Json::Value insts(Json::arrayValue);
for (auto const& i : cpu.instructions) {
insts.append(i);
Expand All @@ -47,11 +49,16 @@ inline CPU FromJson(const Json::Value& root) {
int cores = root["cores"].asInt();
std::string arch = root["arch"].asString();
std::string model = root["model"].asString();
float usage = root["usage"].asFloat();
std::vector<std::string> insts;
for (auto const& i : root["instructions"]) {
insts.emplace_back(i.asString());
}
return {.cores = cores, .arch = arch, .model = model, .instructions = insts};
return {.cores = cores,
.arch = arch,
.model = model,
.usage = usage,
.instructions = insts};
}
} // namespace cpu

Expand All @@ -64,6 +71,7 @@ struct AmdAddInfo {};
using GPUAddInfo = std::variant<NvidiaAddInfo, AmdAddInfo>;
struct GPU {
std::string id;
uint32_t device_id;
std::string name;
std::string version;
GPUAddInfo add_info;
Expand All @@ -77,7 +85,7 @@ inline Json::Value ToJson(const std::vector<GPU>& gpus) {
Json::Value res(Json::arrayValue);
for (size_t i = 0; i < gpus.size(); i++) {
Json::Value gpu;
gpu["id"] = std::to_string(i);
gpu["id"] = gpus[i].id;
gpu["name"] = gpus[i].name;
gpu["version"] = gpus[i].version;
Json::Value add_info;
Expand Down Expand Up @@ -142,7 +150,6 @@ inline OS FromJson(const Json::Value& root) {
}
} // namespace os


struct PowerInfo {
std::string charging_status;
int battery_life;
Expand All @@ -165,7 +172,6 @@ inline PowerInfo FromJson(const Json::Value& root) {
}
} // namespace power


namespace {
int64_t ByteToMiB(int64_t b) {
return b / 1024 / 1024;
Expand Down Expand Up @@ -214,4 +220,4 @@ inline StorageInfo FromJson(const Json::Value& root) {
.available = root["available"].asInt64()};
}
} // namespace storage
}
} // namespace cortex::hw
37 changes: 37 additions & 0 deletions engine/database/hardware.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,41 @@ cpp::result<bool, std::string> Hardware::DeleteHardwareEntry(
return cpp::fail(e.what());
}
}

bool Hardware::HasHardwareEntry(const std::string& id) {
try {
SQLite::Statement query(db_,
"SELECT COUNT(*) FROM hardware WHERE uuid = ?");
query.bind(1, id);
if (query.executeStep()) {
return query.getColumn(0).getInt() > 0;
}
return false;
} catch (const std::exception& e) {
CTL_WRN(e.what());
return false;
}
}

cpp::result<bool, std::string> Hardware::UpdateHardwareEntry(const std::string& id,
int hw_id,
int sw_id) const {
try {
SQLite::Statement upd(
db_,
"UPDATE hardware "
"SET hardware_id = ?, software_id = ? "
"WHERE uuid = ?");
upd.bind(1, hw_id);
upd.bind(2, sw_id);
upd.bind(3, id);
if (upd.exec() == 1) {
CTL_INF("Updated: " << id << " " << hw_id << " " << sw_id);
return true;
}
return false;
} catch (const std::exception& e) {
return cpp::fail(e.what());
}
}
} // namespace cortex::db
4 changes: 4 additions & 0 deletions engine/database/hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,9 @@ class Hardware {
cpp::result<bool, std::string> UpdateHardwareEntry(
const std::string& id, const HardwareEntry& updated_entry);
cpp::result<bool, std::string> DeleteHardwareEntry(const std::string& id);
bool HasHardwareEntry(const std::string& id);
cpp::result<bool, std::string> UpdateHardwareEntry(const std::string& id,
int hw_id,
int sw_id) const;
};
} // namespace cortex::db
Binary file added engine/deps/linux/libvulkan.so
Binary file not shown.
Binary file added engine/deps/windows/vulkan-1.dll
Binary file not shown.
14 changes: 8 additions & 6 deletions engine/services/download_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ void SetUpProxy(CURL* handle, std::shared_ptr<ConfigService> config_service) {
} // namespace

cpp::result<bool, std::string> DownloadService::AddDownloadTask(
DownloadTask& task,
std::optional<OnDownloadTaskSuccessfully> callback) noexcept {
DownloadTask& task, std::optional<OnDownloadTaskSuccessfully> callback,
bool show_progress) noexcept {
std::optional<std::string> dl_err_msg = std::nullopt;
bool has_task_done = false;
for (const auto& item : task.items) {
CLI_LOG("Start downloading: " + item.localPath.filename().string());
auto result = Download(task.id, item);
auto result = Download(task.id, item, show_progress);
if (result.has_error()) {
dl_err_msg = result.error();
break;
Expand Down Expand Up @@ -164,8 +164,8 @@ cpp::result<uint64_t, std::string> DownloadService::GetFileSize(
}

cpp::result<bool, std::string> DownloadService::Download(
const std::string& download_id,
const DownloadItem& download_item) noexcept {
const std::string& download_id, const DownloadItem& download_item,
bool show_progress) noexcept {
CTL_INF("Absolute file output: " << download_item.localPath.string());

auto curl = curl_easy_init();
Expand Down Expand Up @@ -241,7 +241,9 @@ cpp::result<bool, std::string> DownloadService::Download(
SetUpProxy(curl, config_service_);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
if (show_progress) {
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
}
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

if (mode == "ab") {
Expand Down
8 changes: 5 additions & 3 deletions engine/services/download_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ class DownloadService {
* Start download task synchronously.
*/
cpp::result<bool, std::string> AddDownloadTask(
DownloadTask& task, std::optional<OnDownloadTaskSuccessfully> callback =
std::nullopt) noexcept;
DownloadTask& task,
std::optional<OnDownloadTaskSuccessfully> callback = std::nullopt,
bool show_progress = true) noexcept;

/**
* Getting file size for a provided url. Can be used to validating the download url.
Expand All @@ -128,7 +129,8 @@ class DownloadService {

cpp::result<bool, std::string> Download(
const std::string& download_id,
const DownloadItem& download_item) noexcept;
const DownloadItem& download_item,
bool show_progress = true) noexcept;

std::shared_ptr<EventQueue> event_queue_;

Expand Down
Loading

0 comments on commit cbfe6c7

Please sign in to comment.