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

Commit ee1b5fa

Browse files
committed
feat: update engine interface
1 parent 08fbb8a commit ee1b5fa

File tree

10 files changed

+425
-343
lines changed

10 files changed

+425
-343
lines changed

docs/docs/engines/engine-extension.mdx

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,32 @@ First, create an engine that implements the `EngineI.h` interface. Here's the in
2222
```cpp
2323
class EngineI {
2424
public:
25-
struct EngineLoadOption{};
26-
struct EngineUnloadOption{};
25+
struct RegisterLibraryOption {
26+
std::vector<std::filesystem::path> paths;
27+
};
28+
29+
struct EngineLoadOption {
30+
// engine
31+
std::filesystem::path engine_path;
32+
std::filesystem::path cuda_path;
33+
bool custom_engine_path;
34+
35+
// logging
36+
std::filesystem::path log_path;
37+
int max_log_lines;
38+
trantor::Logger::LogLevel log_level;
39+
};
40+
41+
struct EngineUnloadOption {
42+
bool unload_dll;
43+
};
2744

2845
virtual ~EngineI() {}
2946

47+
virtual void RegisterLibraryPath(RegisterLibraryOption opts) = 0;
48+
3049
virtual void Load(EngineLoadOption opts) = 0;
50+
3151
virtual void Unload(EngineUnloadOption opts) = 0;
3252

3353
// Cortex.llamacpp interface methods
@@ -65,7 +85,71 @@ class EngineI {
6585
};
6686
```
6787
68-
Note that Cortex will call `Load` before loading any models and `Unload` when stopping the engine.
88+
#### Lifecycle Management
89+
90+
##### RegisterLibraryPath
91+
92+
```cpp
93+
virtual void RegisterLibraryPath(RegisterLibraryOption opts) = 0;
94+
```
95+
96+
This method is called during engine initialization to set up dynamic library search paths. For example, in Linux, we still have to use `LD_LIBRARY_PATH` to add CUDA dependencies to the search path.
97+
98+
**Parameters:**
99+
100+
- `opts.paths`: Vector of filesystem paths that the engine should register
101+
102+
**Implementation Requirements:**
103+
104+
- Register provided paths for dynamic library loading
105+
- Handle invalid paths gracefully
106+
- Thread-safe implementation
107+
- No exceptions should escape the method
108+
109+
##### Load
110+
111+
```cpp
112+
virtual void Load(EngineLoadOption opts) = 0;
113+
```
114+
115+
Initializes the engine with the provided configuration options.
116+
117+
**Parameters:**
118+
119+
- `engine_path`: Base path for engine files
120+
- `cuda_path`: Path to CUDA installation
121+
- `custom_engine_path`: Flag for using custom engine location
122+
- `log_path`: Location for log files
123+
- `max_log_lines`: Maximum number of lines per log file
124+
- `log_level`: Logging verbosity level
125+
126+
**Implementation Requirements:**
127+
128+
- Validate all paths before use
129+
- Initialize engine components
130+
- Set up logging configuration
131+
- Handle missing dependencies gracefully
132+
- Clean initialization state in case of failures
133+
134+
##### Unload
135+
136+
```cpp
137+
virtual void Unload(EngineUnloadOption opts) = 0;
138+
```
139+
140+
Performs cleanup and shutdown of the engine.
141+
142+
**Parameters:**
143+
144+
- `unload_dll`: Boolean flag indicating whether to unload dynamic libraries
145+
146+
**Implementation Requirements:**
147+
148+
- Clean up all allocated resources
149+
- Close file handles and connections
150+
- Release memory
151+
- Ensure proper shutdown of running models
152+
- Handle cleanup in a thread-safe manner
69153
70154
### 2. Create a Dynamic Library
71155
@@ -98,7 +182,7 @@ To test your engine locally:
98182
99183
1. Create a directory structure following this hierarchy:
100184
101-
```
185+
```bash
102186
engines/
103187
└── cortex.llamacpp/
104188
└── mac-arm64/
@@ -107,12 +191,12 @@ engines/
107191
└── version.txt
108192
```
109193
110-
2. Configure your engine:
194+
1. Configure your engine:
111195
112196
- Edit the `~/.cortexrc` file to register your engine name
113197
- Add your model with the appropriate engine field in `model.yaml`
114198
115-
3. Testing:
199+
2. Testing:
116200
- Start the engine
117201
- Load your model
118202
- Verify functionality

engine/cli/commands/server_start_cmd.cc

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#include "server_start_cmd.h"
22
#include "commands/cortex_upd_cmd.h"
3+
#include "services/engine_service.h"
34
#include "utils/cortex_utils.h"
4-
#include "utils/engine_constants.h"
55
#include "utils/file_manager_utils.h"
6+
7+
#if defined(_WIN32) || defined(_WIN64)
68
#include "utils/widechar_conv.h"
9+
#endif
710

811
namespace commands {
912

@@ -108,22 +111,9 @@ bool ServerStartCmd::Exec(const std::string& host, int port,
108111
std::cerr << "Could not start server: " << std::endl;
109112
return false;
110113
} else if (pid == 0) {
111-
// No need to configure LD_LIBRARY_PATH for macOS
112-
#if !defined(__APPLE__) || !defined(__MACH__)
113-
const char* name = "LD_LIBRARY_PATH";
114-
auto data = getenv(name);
115-
std::string v;
116-
if (auto g = getenv(name); g) {
117-
v += g;
118-
}
119-
CTL_INF("LD_LIBRARY_PATH: " << v);
120-
auto llamacpp_path = file_manager_utils::GetCudaToolkitPath(kLlamaRepo);
121-
auto trt_path = file_manager_utils::GetCudaToolkitPath(kTrtLlmRepo);
114+
// Some engines requires to add lib search path before process being created
115+
EngineService().RegisterEngineLibPath();
122116

123-
auto new_v = trt_path.string() + ":" + llamacpp_path.string() + ":" + v;
124-
setenv(name, new_v.c_str(), true);
125-
CTL_INF("LD_LIBRARY_PATH: " << getenv(name));
126-
#endif
127117
std::string p = cortex_utils::GetCurrentPath() + "/" + exe;
128118
execl(p.c_str(), exe.c_str(), "--start-server", "--config_file_path",
129119
get_config_file_path().c_str(), "--data_folder_path",

engine/controllers/engines.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ std::string NormalizeEngine(const std::string& engine) {
2323
void Engines::ListEngine(
2424
const HttpRequestPtr& req,
2525
std::function<void(const HttpResponsePtr&)>&& callback) const {
26-
std::vector<std::string> supported_engines{kLlamaEngine, kOnnxEngine,
27-
kTrtLlmEngine};
2826
Json::Value ret;
29-
for (const auto& engine : supported_engines) {
27+
auto engine_names = engine_service_->GetSupportedEngineNames().value();
28+
for (const auto& engine : engine_names) {
3029
auto installed_engines =
3130
engine_service_->GetInstalledEngineVariants(engine);
3231
if (installed_engines.has_error()) {

engine/cortex-common/EngineI.h

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

3+
#include <filesystem>
34
#include <functional>
45
#include <memory>
56

67
#include "json/value.h"
78
#include "trantor/utils/Logger.h"
89
class EngineI {
910
public:
11+
struct RegisterLibraryOption {
12+
std::vector<std::filesystem::path> paths;
13+
};
14+
15+
struct EngineLoadOption {
16+
// engine
17+
std::filesystem::path engine_path;
18+
std::filesystem::path cuda_path;
19+
bool custom_engine_path;
20+
21+
// logging
22+
std::filesystem::path log_path;
23+
int max_log_lines;
24+
trantor::Logger::LogLevel log_level;
25+
};
26+
27+
struct EngineUnloadOption {
28+
bool unload_dll;
29+
};
30+
1031
virtual ~EngineI() {}
1132

33+
/**
34+
* Being called before starting process to register dependencies search paths.
35+
*/
36+
virtual void RegisterLibraryPath(RegisterLibraryOption opts) = 0;
37+
38+
virtual void Load(EngineLoadOption opts) = 0;
39+
40+
virtual void Unload(EngineUnloadOption opts) = 0;
41+
1242
// cortex.llamacpp interface
1343
virtual void HandleChatCompletion(
1444
std::shared_ptr<Json::Value> json_body,

0 commit comments

Comments
 (0)