Skip to content

Commit

Permalink
fix linux
Browse files Browse the repository at this point in the history
  • Loading branch information
sangjanai authored and namchuai committed Dec 13, 2024
1 parent 68b92f8 commit 159b390
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 37 deletions.
2 changes: 1 addition & 1 deletion engine/cli/command_line_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ void CommandLineParser::SetupEngineCommands() {
list_engines_cmd->callback([this]() {
if (std::exchange(executed_, true))
return;
commands::EngineListCmd command;
auto command = commands::EngineListCmd(engine_service_);
command.Exec(cml_data_.config.apiServerHost,
std::stoi(cml_data_.config.apiServerPort));
});
Expand Down
9 changes: 2 additions & 7 deletions engine/cli/commands/engine_list_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// clang-format on

namespace commands {

bool EngineListCmd::Exec(const std::string& host, int port) {
// Start server if server is not started yet
if (!commands::IsServerAlive(host, port)) {
Expand All @@ -32,21 +31,17 @@ bool EngineListCmd::Exec(const std::string& host, int port) {
.host = host + ":" + std::to_string(port),
.pathParams = {"v1", "engines"},
};
CTL_INF("NamH URL: " << url.ToFullPath());
auto result = curl_utils::SimpleGetJson(url.ToFullPath());
if (result.has_error()) {
CTL_ERR(result.error());
return false;
}

std::vector<std::string> engines = {
kLlamaEngine,
kOnnxEngine,
kTrtLlmEngine,
};

std::unordered_map<std::string, std::vector<EngineVariantResponse>>
engine_map;

auto engines = engine_service_->GetSupportedEngineNames().value();
for (const auto& engine : engines) {
auto installed_variants = result.value()[engine];
for (const auto& variant : installed_variants) {
Expand Down
7 changes: 7 additions & 0 deletions engine/cli/commands/engine_list_cmd.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#pragma once

#include <string>
#include "services/engine_service.h"

namespace commands {
class EngineListCmd {
public:
explicit EngineListCmd(std::shared_ptr<EngineService> engine_service)
: engine_service_{engine_service} {}

bool Exec(const std::string& host, int port);

private:
std::shared_ptr<EngineService> engine_service_;
};

} // namespace commands
6 changes: 3 additions & 3 deletions engine/controllers/engines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ void Engines::ListEngine(
const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback) const {
Json::Value ret;
for (const auto& engine :
engine_service_->GetSupportedEngineNames().value()) {
auto engines = engine_service_->GetSupportedEngineNames().value();
for (const auto& engine : engines) {
auto installed_engines =
engine_service_->GetInstalledEngineVariants(engine);
if (installed_engines.has_error()) {
Expand All @@ -37,6 +37,7 @@ void Engines::ListEngine(
}
ret[engine] = variants;
}

// Add remote engine
auto remote_engines = engine_service_->GetEngines();
if (remote_engines.has_value()) {
Expand All @@ -49,7 +50,6 @@ void Engines::ListEngine(
}
}
}

auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
resp->setStatusCode(k200OK);
callback(resp);
Expand Down
15 changes: 3 additions & 12 deletions engine/cortex-common/EngineI.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@
#include "trantor/utils/Logger.h"
class EngineI {
public:
struct RegisterLibraryOption {
std::vector<std::filesystem::path> paths;
};

struct EngineLoadOption {
// engine
std::filesystem::path engine_path;
std::filesystem::path cuda_path;
bool custom_engine_path;
std::filesystem::path deps_path;
bool is_custom_engine_path;

// logging
std::filesystem::path log_path;
Expand All @@ -25,16 +21,11 @@ class EngineI {
};

struct EngineUnloadOption {
bool unload_dll;
// place holder for now
};

virtual ~EngineI() {}

/**
* Being called before starting process to register dependencies search paths.
*/
virtual void RegisterLibraryPath(RegisterLibraryOption opts) = 0;

virtual void Load(EngineLoadOption opts) = 0;

virtual void Unload(EngineUnloadOption opts) = 0;
Expand Down
22 changes: 10 additions & 12 deletions engine/services/engine_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ cpp::result<void, std::string> EngineService::LoadEngine(
try {
auto cuda_path = file_manager_utils::GetCudaToolkitPath(ne);

#if defined(_WIN32) || defined(_WIN64)
// register deps
std::vector<std::filesystem::path> paths{};
paths.push_back(std::move(cuda_path));
Expand All @@ -730,24 +731,22 @@ cpp::result<void, std::string> EngineService::LoadEngine(
} else {
CTL_DBG("Registered lib paths for: " << ne);
}
#endif

auto dylib =
std::make_unique<cortex_cpp::dylib>(engine_dir_path.string(), "engine");

auto config = file_manager_utils::GetCortexConfig();

auto log_path =
std::filesystem::path(config.logFolderPath) /
std::filesystem::path(
config.logLlamaCppPath); // for now seems like we use same log path
auto log_path = std::filesystem::path(config.logFolderPath) /
std::filesystem::path(config.logLlamaCppPath);

// init
auto func = dylib->get_function<EngineI*()>("get_engine");
auto engine_obj = func();
auto load_opts = EngineI::EngineLoadOption{
.engine_path = engine_dir_path,
.cuda_path = file_manager_utils::GetCudaToolkitPath(ne),
.custom_engine_path = custom_engine_path,
.deps_path = cuda_path,
.is_custom_engine_path = custom_engine_path,
.log_path = log_path,
.max_log_lines = config.maxLogLines,
.log_level = logging_utils_helper::global_log_level,
Expand All @@ -773,7 +772,7 @@ void EngineService::RegisterEngineLibPath() {
try {
auto engine_dir_path_res = GetEngineDirPath(engine);
if (engine_dir_path_res.has_error()) {
CTL_ERR(
CTL_WRN(
"Could not get engine dir path: " << engine_dir_path_res.error());
continue;
}
Expand All @@ -794,8 +793,9 @@ void EngineService::RegisterEngineLibPath() {

auto reg_result = dylib_path_manager_->RegisterPath(ne, paths);
if (reg_result.has_error()) {
CTL_WRN("Failed register lib path for " << engine);
} else {
CTL_DBG("Register lib path for: " << engine);
CTL_DBG("Registered lib path for " << engine);
}

} catch (const std::exception& e) {
Expand Down Expand Up @@ -863,9 +863,7 @@ cpp::result<void, std::string> EngineService::UnloadEngine(
CTL_DBG("Unregistered lib paths for: " << ne);
}
auto* e = std::get<EngineI*>(engines_[ne].engine);
auto unload_opts = EngineI::EngineUnloadOption{
.unload_dll = true,
};
auto unload_opts = EngineI::EngineUnloadOption{};
e->Unload(unload_opts);
delete e;
engines_.erase(ne);
Expand Down
8 changes: 6 additions & 2 deletions engine/utils/dylib_path_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ cpp::result<void, std::string> DylibPathManager::RegisterPath(
// Get current LD_LIBRARY_PATH
const char* current_path = getenv(kLdLibraryPath);
std::string current_paths = current_path ? current_path : "";
CTL_DBG("Current paths: " << current_paths);

// Add new paths
for (const auto& path : paths) {
Expand All @@ -64,12 +65,15 @@ cpp::result<void, std::string> DylibPathManager::RegisterPath(
if (!current_paths.empty()) {
new_path << ":" << current_paths;
}

CTL_DBG("New paths: " << new_path.str());
// Set the new LD_LIBRARY_PATH
if (setenv(kLdLibraryPath, new_path.str().c_str(), 1) != 0) {
CTL_ERR("Failed to set path!!!");
return cpp::fail("Failed to set " + std::string(kLdLibraryPath));
}

CTL_DBG("After set path: " << getenv(kLdLibraryPath));

dylib_map_[key] = std::move(dylib_paths);
#endif

Expand All @@ -92,7 +96,7 @@ cpp::result<void, std::string> DylibPathManager::Unregister(
}
}

#else
#elif defined(__linux__)
// For Linux, we need to rebuild LD_LIBRARY_PATH without the removed paths
const char* current_path = getenv(kLdLibraryPath);
if (current_path) {
Expand Down

0 comments on commit 159b390

Please sign in to comment.