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

Commit a64af00

Browse files
namchuaisangjanai
andauthored
fix: load engine linux (#1790)
* fix: load engine linux * fix linux --------- Co-authored-by: vansangpfiev <[email protected]>
1 parent 4c39bdb commit a64af00

20 files changed

+293
-116
lines changed

engine/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ file(APPEND "${CMAKE_CURRENT_BINARY_DIR}/cortex_openapi.h"
142142
add_executable(${TARGET_NAME} main.cc
143143
${CMAKE_CURRENT_SOURCE_DIR}/utils/cpuid/cpu_info.cc
144144
${CMAKE_CURRENT_SOURCE_DIR}/utils/file_logger.cc
145+
${CMAKE_CURRENT_SOURCE_DIR}/utils/dylib_path_manager.cc
145146
${CMAKE_CURRENT_SOURCE_DIR}/extensions/remote-engine/remote_engine.cc
146147
${CMAKE_CURRENT_SOURCE_DIR}/extensions/remote-engine/openai_engine.cc
147148
${CMAKE_CURRENT_SOURCE_DIR}/extensions/remote-engine/anthropic_engine.cc

engine/cli/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ find_package(lfreist-hwinfo CONFIG REQUIRED)
7575
add_executable(${TARGET_NAME} main.cc
7676
${CMAKE_CURRENT_SOURCE_DIR}/../utils/cpuid/cpu_info.cc
7777
${CMAKE_CURRENT_SOURCE_DIR}/../utils/file_logger.cc
78+
${CMAKE_CURRENT_SOURCE_DIR}/../utils/dylib_path_manager.cc
7879
${CMAKE_CURRENT_SOURCE_DIR}/command_line_parser.cc
7980
${CMAKE_CURRENT_SOURCE_DIR}/../services/config_service.cc
8081
${CMAKE_CURRENT_SOURCE_DIR}/../services/download_service.cc

engine/cli/command_line_parser.cc

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ constexpr const auto kSubcommands = "Subcommands";
4848
CommandLineParser::CommandLineParser()
4949
: app_("\nCortex.cpp CLI\n"),
5050
download_service_{std::make_shared<DownloadService>()},
51-
model_service_{ModelService(download_service_)},
52-
engine_service_{EngineService(download_service_)} {}
51+
dylib_path_manager_{std::make_shared<cortex::DylibPathManager>()},
52+
engine_service_{std::make_shared<EngineService>(download_service_,
53+
dylib_path_manager_)} {
54+
supported_engines_ = engine_service_->GetSupportedEngineNames().value();
55+
}
5356

5457
bool CommandLineParser::SetupCommand(int argc, char** argv) {
5558
app_.usage("Usage:\n" + commands::GetCortexBinary() +
@@ -60,8 +63,6 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) {
6063

6164
SetupCommonCommands();
6265

63-
SetupInferenceCommands();
64-
6566
SetupModelCommands();
6667

6768
SetupEngineCommands();
@@ -176,17 +177,11 @@ void CommandLineParser::SetupCommonCommands() {
176177
return;
177178
commands::RunCmd rc(cml_data_.config.apiServerHost,
178179
std::stoi(cml_data_.config.apiServerPort),
179-
cml_data_.model_id, download_service_);
180+
cml_data_.model_id, engine_service_);
180181
rc.Exec(cml_data_.run_detach, run_settings_);
181182
});
182183
}
183184

184-
void CommandLineParser::SetupInferenceCommands() {
185-
// auto embeddings_cmd = app_.add_subcommand(
186-
// "embeddings", "Creates an embedding vector representing the input text");
187-
// embeddings_cmd->group(kInferenceGroup);
188-
}
189-
190185
void CommandLineParser::SetupModelCommands() {
191186
// Models group commands
192187
auto models_cmd =
@@ -476,7 +471,7 @@ void CommandLineParser::SetupEngineCommands() {
476471
list_engines_cmd->callback([this]() {
477472
if (std::exchange(executed_, true))
478473
return;
479-
commands::EngineListCmd command;
474+
auto command = commands::EngineListCmd(engine_service_);
480475
command.Exec(cml_data_.config.apiServerHost,
481476
std::stoi(cml_data_.config.apiServerPort));
482477
});
@@ -493,9 +488,9 @@ void CommandLineParser::SetupEngineCommands() {
493488
CLI_LOG(install_cmd->help());
494489
}
495490
});
496-
for (const auto& engine : engine_service_.kSupportEngines) {
497-
std::string engine_name{engine};
498-
EngineInstall(install_cmd, engine_name, cml_data_.engine_version,
491+
492+
for (const auto& engine : supported_engines_) {
493+
EngineInstall(install_cmd, engine, cml_data_.engine_version,
499494
cml_data_.engine_src);
500495
}
501496

@@ -512,9 +507,8 @@ void CommandLineParser::SetupEngineCommands() {
512507
}
513508
});
514509
uninstall_cmd->group(kSubcommands);
515-
for (auto& engine : engine_service_.kSupportEngines) {
516-
std::string engine_name{engine};
517-
EngineUninstall(uninstall_cmd, engine_name);
510+
for (const auto& engine : supported_engines_) {
511+
EngineUninstall(uninstall_cmd, engine);
518512
}
519513

520514
auto engine_upd_cmd = engines_cmd->add_subcommand("update", "Update engine");
@@ -529,9 +523,8 @@ void CommandLineParser::SetupEngineCommands() {
529523
}
530524
});
531525
engine_upd_cmd->group(kSubcommands);
532-
for (auto& engine : engine_service_.kSupportEngines) {
533-
std::string engine_name{engine};
534-
EngineUpdate(engine_upd_cmd, engine_name);
526+
for (const auto& engine : supported_engines_) {
527+
EngineUpdate(engine_upd_cmd, engine);
535528
}
536529

537530
auto engine_use_cmd =
@@ -547,9 +540,8 @@ void CommandLineParser::SetupEngineCommands() {
547540
}
548541
});
549542
engine_use_cmd->group(kSubcommands);
550-
for (auto& engine : engine_service_.kSupportEngines) {
551-
std::string engine_name{engine};
552-
EngineUse(engine_use_cmd, engine_name);
543+
for (const auto& engine : supported_engines_) {
544+
EngineUse(engine_use_cmd, engine);
553545
}
554546

555547
auto engine_load_cmd = engines_cmd->add_subcommand("load", "Load engine");
@@ -564,9 +556,8 @@ void CommandLineParser::SetupEngineCommands() {
564556
}
565557
});
566558
engine_load_cmd->group(kSubcommands);
567-
for (auto& engine : engine_service_.kSupportEngines) {
568-
std::string engine_name{engine};
569-
EngineLoad(engine_load_cmd, engine_name);
559+
for (const auto& engine : supported_engines_) {
560+
EngineLoad(engine_load_cmd, engine);
570561
}
571562

572563
auto engine_unload_cmd =
@@ -582,9 +573,8 @@ void CommandLineParser::SetupEngineCommands() {
582573
}
583574
});
584575
engine_unload_cmd->group(kSubcommands);
585-
for (auto& engine : engine_service_.kSupportEngines) {
586-
std::string engine_name{engine};
587-
EngineUnload(engine_unload_cmd, engine_name);
576+
for (const auto& engine : supported_engines_) {
577+
EngineUnload(engine_unload_cmd, engine);
588578
}
589579

590580
EngineGet(engines_cmd);
@@ -756,7 +746,7 @@ void CommandLineParser::EngineInstall(CLI::App* parent,
756746
return;
757747
try {
758748
commands::EngineInstallCmd(
759-
download_service_, cml_data_.config.apiServerHost,
749+
engine_service_, cml_data_.config.apiServerHost,
760750
std::stoi(cml_data_.config.apiServerPort), cml_data_.show_menu)
761751
.Exec(engine_name, version, src);
762752
} catch (const std::exception& e) {
@@ -878,20 +868,19 @@ void CommandLineParser::EngineGet(CLI::App* parent) {
878868
}
879869
});
880870

881-
for (auto& engine : engine_service_.kSupportEngines) {
882-
std::string engine_name{engine};
883-
std::string desc = "Get " + engine_name + " status";
871+
for (const auto& engine : supported_engines_) {
872+
std::string desc = "Get " + engine + " status";
884873

885-
auto engine_get_cmd = get_cmd->add_subcommand(engine_name, desc);
874+
auto engine_get_cmd = get_cmd->add_subcommand(engine, desc);
886875
engine_get_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
887-
" engines get " + engine_name + " [options]");
876+
" engines get " + engine + " [options]");
888877
engine_get_cmd->group(kEngineGroup);
889-
engine_get_cmd->callback([this, engine_name] {
878+
engine_get_cmd->callback([this, engine] {
890879
if (std::exchange(executed_, true))
891880
return;
892881
commands::EngineGetCmd().Exec(cml_data_.config.apiServerHost,
893882
std::stoi(cml_data_.config.apiServerPort),
894-
engine_name);
883+
engine);
895884
});
896885
}
897886
}

engine/cli/command_line_parser.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "CLI/CLI.hpp"
66
#include "commands/hardware_list_cmd.h"
77
#include "services/engine_service.h"
8-
#include "services/model_service.h"
98
#include "utils/config_yaml_utils.h"
109

1110
class CommandLineParser {
@@ -16,8 +15,6 @@ class CommandLineParser {
1615
private:
1716
void SetupCommonCommands();
1817

19-
void SetupInferenceCommands();
20-
2118
void SetupModelCommands();
2219

2320
void SetupEngineCommands();
@@ -47,8 +44,9 @@ class CommandLineParser {
4744

4845
CLI::App app_;
4946
std::shared_ptr<DownloadService> download_service_;
50-
EngineService engine_service_;
51-
ModelService model_service_;
47+
std::shared_ptr<cortex::DylibPathManager> dylib_path_manager_;
48+
std::shared_ptr<EngineService> engine_service_;
49+
std::vector<std::string> supported_engines_;
5250

5351
struct CmlData {
5452
std::string model_id;

engine/cli/commands/engine_install_cmd.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ bool EngineInstallCmd::Exec(const std::string& engine,
1212
const std::string& src) {
1313
// Handle local install, if fails, fallback to remote install
1414
if (!src.empty()) {
15-
auto res = engine_service_.UnzipEngine(engine, version, src);
15+
auto res = engine_service_->UnzipEngine(engine, version, src);
1616
if (res.has_error()) {
1717
CLI_LOG(res.error());
1818
return false;

engine/cli/commands/engine_install_cmd.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace commands {
77

88
class EngineInstallCmd {
99
public:
10-
explicit EngineInstallCmd(std::shared_ptr<DownloadService> download_service,
10+
explicit EngineInstallCmd(std::shared_ptr<EngineService> engine_service,
1111
const std::string& host, int port, bool show_menu)
12-
: engine_service_{EngineService(download_service)},
12+
: engine_service_{engine_service},
1313
host_(host),
1414
port_(port),
1515
show_menu_(show_menu),
@@ -21,7 +21,7 @@ class EngineInstallCmd {
2121
const std::string& src = "");
2222

2323
private:
24-
EngineService engine_service_;
24+
std::shared_ptr<EngineService> engine_service_;
2525
std::string host_;
2626
int port_;
2727
bool show_menu_;

engine/cli/commands/engine_list_cmd.cc

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// clang-format on
1414

1515
namespace commands {
16-
1716
bool EngineListCmd::Exec(const std::string& host, int port) {
1817
// Start server if server is not started yet
1918
if (!commands::IsServerAlive(host, port)) {
@@ -38,15 +37,10 @@ bool EngineListCmd::Exec(const std::string& host, int port) {
3837
return false;
3938
}
4039

41-
std::vector<std::string> engines = {
42-
kLlamaEngine,
43-
kOnnxEngine,
44-
kTrtLlmEngine,
45-
};
46-
4740
std::unordered_map<std::string, std::vector<EngineVariantResponse>>
4841
engine_map;
4942

43+
auto engines = engine_service_->GetSupportedEngineNames().value();
5044
for (const auto& engine : engines) {
5145
auto installed_variants = result.value()[engine];
5246
for (const auto& variant : installed_variants) {

engine/cli/commands/engine_list_cmd.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
#pragma once
22

33
#include <string>
4+
#include "services/engine_service.h"
45

56
namespace commands {
67
class EngineListCmd {
78
public:
9+
explicit EngineListCmd(std::shared_ptr<EngineService> engine_service)
10+
: engine_service_{engine_service} {}
11+
812
bool Exec(const std::string& host, int port);
13+
14+
private:
15+
std::shared_ptr<EngineService> engine_service_;
916
};
1017

1118
} // namespace commands

engine/cli/commands/run_cmd.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ void RunCmd::Exec(bool run_detach,
9494

9595
// Check if engine existed. If not, download it
9696
{
97-
auto is_engine_ready = engine_service_.IsEngineReady(mc.engine);
97+
auto is_engine_ready = engine_service_->IsEngineReady(mc.engine);
9898
if (is_engine_ready.has_error()) {
9999
throw std::runtime_error(is_engine_ready.error());
100100
}
101101

102102
if (!is_engine_ready.value()) {
103103
CTL_INF("Engine " << mc.engine
104104
<< " is not ready. Proceed to install..");
105-
if (!EngineInstallCmd(download_service_, host_, port_, false)
105+
if (!EngineInstallCmd(engine_service_, host_, port_, false)
106106
.Exec(mc.engine)) {
107107
return;
108108
} else {

engine/cli/commands/run_cmd.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ std::optional<std::string> SelectLocalModel(std::string host, int port,
1212
class RunCmd {
1313
public:
1414
explicit RunCmd(std::string host, int port, std::string model_handle,
15-
std::shared_ptr<DownloadService> download_service)
15+
std::shared_ptr<EngineService> engine_service)
1616
: host_{std::move(host)},
1717
port_{port},
1818
model_handle_{std::move(model_handle)},
19-
download_service_(download_service),
20-
engine_service_{EngineService(download_service)} {};
19+
engine_service_{engine_service} {};
2120

2221
void Exec(bool chat_flag,
2322
const std::unordered_map<std::string, std::string>& options);
@@ -26,8 +25,6 @@ class RunCmd {
2625
std::string host_;
2726
int port_;
2827
std::string model_handle_;
29-
30-
std::shared_ptr<DownloadService> download_service_;
31-
EngineService engine_service_;
28+
std::shared_ptr<EngineService> engine_service_;
3229
};
3330
} // namespace commands

engine/cli/commands/server_start_cmd.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ bool ServerStartCmd::Exec(const std::string& host, int port,
112112
return false;
113113
} else if (pid == 0) {
114114
// Some engines requires to add lib search path before process being created
115-
EngineService().RegisterEngineLibPath();
115+
auto download_srv = std::make_shared<DownloadService>();
116+
auto dylib_path_mng = std::make_shared<cortex::DylibPathManager>();
117+
EngineService(download_srv, dylib_path_mng).RegisterEngineLibPath();
116118

117119
std::string p = cortex_utils::GetCurrentPath() + "/" + exe;
118120
execl(p.c_str(), exe.c_str(), "--start-server", "--config_file_path",
@@ -131,5 +133,4 @@ bool ServerStartCmd::Exec(const std::string& host, int port,
131133
#endif
132134
return true;
133135
}
134-
135136
}; // namespace commands

engine/controllers/engines.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#include "utils/archive_utils.h"
44
#include "utils/cortex_utils.h"
55
#include "utils/engine_constants.h"
6-
#include "utils/http_util.h"
76
#include "utils/logging_utils.h"
87
#include "utils/string_utils.h"
8+
99
namespace {
1010
// Need to change this after we rename repositories
1111
std::string NormalizeEngine(const std::string& engine) {
@@ -24,8 +24,8 @@ void Engines::ListEngine(
2424
const HttpRequestPtr& req,
2525
std::function<void(const HttpResponsePtr&)>&& callback) const {
2626
Json::Value ret;
27-
auto engine_names = engine_service_->GetSupportedEngineNames().value();
28-
for (const auto& engine : engine_names) {
27+
auto engines = engine_service_->GetSupportedEngineNames().value();
28+
for (const auto& engine : engines) {
2929
auto installed_engines =
3030
engine_service_->GetInstalledEngineVariants(engine);
3131
if (installed_engines.has_error()) {
@@ -37,6 +37,7 @@ void Engines::ListEngine(
3737
}
3838
ret[engine] = variants;
3939
}
40+
4041
// Add remote engine
4142
auto remote_engines = engine_service_->GetEngines();
4243
if (remote_engines.has_value()) {
@@ -49,7 +50,6 @@ void Engines::ListEngine(
4950
}
5051
}
5152
}
52-
5353
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
5454
resp->setStatusCode(k200OK);
5555
callback(resp);

0 commit comments

Comments
 (0)