2024-01-10 04:29:58 +00:00
|
|
|
#include "dyn_ext_server.h"
|
2023-11-29 19:00:37 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2023-12-18 20:05:59 +00:00
|
|
|
#ifdef __linux__
|
2023-11-29 19:00:37 +00:00
|
|
|
#include <dlfcn.h>
|
2024-01-13 00:28:00 +00:00
|
|
|
#define LOAD_LIBRARY(lib, flags) dlopen(lib, flags)
|
2023-11-29 19:00:37 +00:00
|
|
|
#define LOAD_SYMBOL(handle, sym) dlsym(handle, sym)
|
2023-12-23 19:35:44 +00:00
|
|
|
#define LOAD_ERR() strdup(dlerror())
|
2023-11-29 19:00:37 +00:00
|
|
|
#define UNLOAD_LIBRARY(handle) dlclose(handle)
|
2023-12-18 20:05:59 +00:00
|
|
|
#elif _WIN32
|
2023-11-29 19:00:37 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#define LOAD_LIBRARY(lib, flags) LoadLibrary(lib)
|
|
|
|
#define LOAD_SYMBOL(handle, sym) GetProcAddress(handle, sym)
|
|
|
|
#define UNLOAD_LIBRARY(handle) FreeLibrary(handle)
|
2024-02-16 01:15:09 +00:00
|
|
|
#define LOAD_ERR() ({\
|
|
|
|
LPSTR messageBuffer = NULL; \
|
|
|
|
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \
|
|
|
|
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL); \
|
|
|
|
char *resp = strdup(messageBuffer); \
|
|
|
|
LocalFree(messageBuffer); \
|
|
|
|
resp; \
|
|
|
|
})
|
2023-12-18 20:05:59 +00:00
|
|
|
#else
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#define LOAD_LIBRARY(lib, flags) dlopen(lib, flags)
|
|
|
|
#define LOAD_SYMBOL(handle, sym) dlsym(handle, sym)
|
2023-12-23 19:35:44 +00:00
|
|
|
#define LOAD_ERR() strdup(dlerror())
|
2023-12-18 20:05:59 +00:00
|
|
|
#define UNLOAD_LIBRARY(handle) dlclose(handle)
|
2023-11-29 19:00:37 +00:00
|
|
|
#endif
|
|
|
|
|
2024-01-10 04:29:58 +00:00
|
|
|
void dyn_init(const char *libPath, struct dynamic_llama_server *s,
|
2023-12-20 18:36:01 +00:00
|
|
|
ext_server_resp_t *err) {
|
2023-11-29 19:00:37 +00:00
|
|
|
int i = 0;
|
|
|
|
struct lookup {
|
|
|
|
char *s;
|
|
|
|
void **p;
|
|
|
|
} l[] = {
|
|
|
|
{"llama_server_init", (void *)&s->llama_server_init},
|
|
|
|
{"llama_server_start", (void *)&s->llama_server_start},
|
|
|
|
{"llama_server_stop", (void *)&s->llama_server_stop},
|
|
|
|
{"llama_server_completion", (void *)&s->llama_server_completion},
|
|
|
|
{"llama_server_completion_next_result",
|
|
|
|
(void *)&s->llama_server_completion_next_result},
|
|
|
|
{"llama_server_completion_cancel",
|
|
|
|
(void *)&s->llama_server_completion_cancel},
|
|
|
|
{"llama_server_release_task_result",
|
|
|
|
(void *)&s->llama_server_release_task_result},
|
|
|
|
{"llama_server_tokenize", (void *)&s->llama_server_tokenize},
|
|
|
|
{"llama_server_detokenize", (void *)&s->llama_server_detokenize},
|
|
|
|
{"llama_server_embedding", (void *)&s->llama_server_embedding},
|
|
|
|
{"llama_server_release_json_resp",
|
|
|
|
(void *)&s->llama_server_release_json_resp},
|
|
|
|
{"", NULL},
|
|
|
|
};
|
|
|
|
|
2024-01-13 00:28:00 +00:00
|
|
|
printf("loading library %s\n", libPath);
|
2024-01-19 19:37:02 +00:00
|
|
|
s->handle = LOAD_LIBRARY(libPath, RTLD_LOCAL|RTLD_NOW);
|
2023-11-29 19:00:37 +00:00
|
|
|
if (!s->handle) {
|
|
|
|
err->id = -1;
|
2023-12-23 19:35:44 +00:00
|
|
|
char *msg = LOAD_ERR();
|
2023-12-20 18:36:01 +00:00
|
|
|
snprintf(err->msg, err->msg_len,
|
2023-12-23 19:35:44 +00:00
|
|
|
"Unable to load dynamic server library: %s", msg);
|
|
|
|
free(msg);
|
2023-11-29 19:00:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; l[i].p != NULL; i++) {
|
|
|
|
*l[i].p = LOAD_SYMBOL(s->handle, l[i].s);
|
|
|
|
if (!l[i].p) {
|
|
|
|
UNLOAD_LIBRARY(s->handle);
|
|
|
|
err->id = -1;
|
2023-12-23 19:35:44 +00:00
|
|
|
char *msg = LOAD_ERR();
|
2023-11-29 19:00:37 +00:00
|
|
|
snprintf(err->msg, err->msg_len, "symbol lookup for %s failed: %s",
|
2023-12-23 19:35:44 +00:00
|
|
|
l[i].s, msg);
|
|
|
|
free(msg);
|
2023-11-29 19:00:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-10 04:29:58 +00:00
|
|
|
inline void dyn_llama_server_init(struct dynamic_llama_server s,
|
2023-12-20 18:36:01 +00:00
|
|
|
ext_server_params_t *sparams,
|
|
|
|
ext_server_resp_t *err) {
|
2023-11-29 19:00:37 +00:00
|
|
|
s.llama_server_init(sparams, err);
|
|
|
|
}
|
|
|
|
|
2024-01-10 04:29:58 +00:00
|
|
|
inline void dyn_llama_server_start(struct dynamic_llama_server s) {
|
2023-11-29 19:00:37 +00:00
|
|
|
s.llama_server_start();
|
|
|
|
}
|
|
|
|
|
2024-01-10 04:29:58 +00:00
|
|
|
inline void dyn_llama_server_stop(struct dynamic_llama_server s) {
|
2023-11-29 19:00:37 +00:00
|
|
|
s.llama_server_stop();
|
|
|
|
}
|
|
|
|
|
2024-01-10 04:29:58 +00:00
|
|
|
inline void dyn_llama_server_completion(struct dynamic_llama_server s,
|
2023-12-20 18:36:01 +00:00
|
|
|
const char *json_req,
|
|
|
|
ext_server_resp_t *resp) {
|
2023-11-29 19:00:37 +00:00
|
|
|
s.llama_server_completion(json_req, resp);
|
|
|
|
}
|
|
|
|
|
2024-01-10 04:29:58 +00:00
|
|
|
inline void dyn_llama_server_completion_next_result(
|
2023-12-20 18:36:01 +00:00
|
|
|
struct dynamic_llama_server s, const int task_id,
|
2023-11-29 19:00:37 +00:00
|
|
|
ext_server_task_result_t *result) {
|
|
|
|
s.llama_server_completion_next_result(task_id, result);
|
|
|
|
}
|
|
|
|
|
2024-01-10 04:29:58 +00:00
|
|
|
inline void dyn_llama_server_completion_cancel(
|
2023-12-20 18:36:01 +00:00
|
|
|
struct dynamic_llama_server s, const int task_id, ext_server_resp_t *err) {
|
2023-11-29 19:00:37 +00:00
|
|
|
s.llama_server_completion_cancel(task_id, err);
|
|
|
|
}
|
2024-01-10 04:29:58 +00:00
|
|
|
inline void dyn_llama_server_release_task_result(
|
2023-12-20 18:36:01 +00:00
|
|
|
struct dynamic_llama_server s, ext_server_task_result_t *result) {
|
2023-11-29 19:00:37 +00:00
|
|
|
s.llama_server_release_task_result(result);
|
|
|
|
}
|
|
|
|
|
2024-01-10 04:29:58 +00:00
|
|
|
inline void dyn_llama_server_tokenize(struct dynamic_llama_server s,
|
2023-12-20 18:36:01 +00:00
|
|
|
const char *json_req,
|
|
|
|
char **json_resp,
|
|
|
|
ext_server_resp_t *err) {
|
2023-11-29 19:00:37 +00:00
|
|
|
s.llama_server_tokenize(json_req, json_resp, err);
|
|
|
|
}
|
|
|
|
|
2024-01-10 04:29:58 +00:00
|
|
|
inline void dyn_llama_server_detokenize(struct dynamic_llama_server s,
|
2023-12-20 18:36:01 +00:00
|
|
|
const char *json_req,
|
|
|
|
char **json_resp,
|
|
|
|
ext_server_resp_t *err) {
|
2023-11-29 19:00:37 +00:00
|
|
|
s.llama_server_detokenize(json_req, json_resp, err);
|
|
|
|
}
|
|
|
|
|
2024-01-10 04:29:58 +00:00
|
|
|
inline void dyn_llama_server_embedding(struct dynamic_llama_server s,
|
2023-12-20 18:36:01 +00:00
|
|
|
const char *json_req,
|
|
|
|
char **json_resp,
|
|
|
|
ext_server_resp_t *err) {
|
2023-11-29 19:00:37 +00:00
|
|
|
s.llama_server_embedding(json_req, json_resp, err);
|
|
|
|
}
|
|
|
|
|
2024-01-10 04:29:58 +00:00
|
|
|
inline void dyn_llama_server_release_json_resp(
|
2023-12-20 18:36:01 +00:00
|
|
|
struct dynamic_llama_server s, char **json_resp) {
|
2023-11-29 19:00:37 +00:00
|
|
|
s.llama_server_release_json_resp(json_resp);
|
|
|
|
}
|