diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2026-08-18 17:45:10 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2026-08-18 17:45:10 +0800 |
| commit | 82680bc3350807c532d0aeb714cb5360da9293f0 (patch) | |
| tree | c34a1965430d6019e45fc184cb6d3a86bc1ccd50 | |
| parent | 75db6d795d689e28560aa7e6b3d471c89983191a (diff) | |
| download | lex-82680bc3350807c532d0aeb714cb5360da9293f0.tar.gz | |
Use syslog, move the sock to dexd subdirectory.
| -rw-r--r-- | llm.c | 32 | ||||
| -rw-r--r-- | main.c | 63 | ||||
| -rw-r--r-- | mem.h | 19 | ||||
| -rwxr-xr-x | test_prompts.sh | 2 |
4 files changed, 76 insertions, 40 deletions
@@ -1,6 +1,7 @@ #include <stdio.h> #include <err.h> #include <string.h> +#include <syslog.h> #include "llm.h" #include "mem.h" @@ -47,9 +48,17 @@ struct llm_ctx { static void llm_log_cb(enum ggml_log_level level, const char *s, void *ud) { (void)ud; - if (level < GGML_LOG_LEVEL_WARN) + + switch (level) { + case GGML_LOG_LEVEL_WARN: + syslog(LOG_WARNING, "%s", s); + return; + case GGML_LOG_LEVEL_ERROR: + syslog(LOG_ERR, "%s", s); return; - fputs(s, stderr); + default: + return; + } } /* Tokenize `text` (already a fully rendered chat-template prompt) into @@ -105,7 +114,7 @@ struct llm_ctx *llm_init(const char *model_path) } if (!llama_model_chat_template(llm->model, NULL)) - fprintf(stderr, "WARN: model has no embedded chat template\n"); + syslog(LOG_WARNING, "model has no embedded chat template"); /* Creation of context allocates KV cache and compute buffers. * Do this once in init, instead of per request. */ @@ -206,14 +215,14 @@ void llm_run(struct llm_ctx *llm, const char *user_prompt, FILE *out) * assumptions about the model's internal chat structure. */ prompt_len = llama_chat_apply_template(NULL, messages, 2, true, NULL, 0); if (prompt_len <= 0) { - fprintf(stderr, "ERROR: Chat template size calculation failed\n"); + syslog(LOG_ERR, "chat template size calculation failed"); return; } prompt = MALLOC((size_t)prompt_len + 1); if (llama_chat_apply_template(NULL, messages, 2, true, prompt, prompt_len + 1) < 0) { - fprintf(stderr, "ERROR: failed to apply chat template\n"); + syslog(LOG_ERR, "failed to apply chat template"); free(prompt); return; } @@ -221,12 +230,12 @@ void llm_run(struct llm_ctx *llm, const char *user_prompt, FILE *out) n_new_tokens = tokenize_prompt(llm->vocab, prompt, prompt_len, &new_tokens); free(prompt); if (n_new_tokens <= 0) { - fprintf(stderr, "ERROR: tokenization failed\n"); + syslog(LOG_ERR, "tokenization failed"); return; } if (n_new_tokens + MAX_TOKENS > N_CTX) { - fprintf(stderr, "ERROR: token count exceeds context size\n"); + syslog(LOG_ERR, "token count exceeds context window"); free(new_tokens); return; } @@ -244,9 +253,8 @@ void llm_run(struct llm_ctx *llm, const char *user_prompt, FILE *out) /* Sanity check */ if (llm->n_cached_tokens > 0 && n_common < llm->n_system_tokens) { - fprintf(stderr, "WARN: Cached prefix is shorter than the " - "system prompt. Chat template may be rendering " - "inconsistently between requests."); + /* Template may be rendering inconsistently between requests */ + syslog(LOG_WARNING, "cached prefix is shorter than the system prompt"); } /* Drop everything in the cache past the common prefix. */ @@ -261,7 +269,7 @@ void llm_run(struct llm_ctx *llm, const char *user_prompt, FILE *out) batch = llama_batch_get_one(new_tokens + n_common, n_new_tokens - n_common); if (llama_decode(llm->ctx, batch) != 0) { - fprintf(stderr, "ERROR: prompt evaluation failed\n"); + syslog(LOG_ERR, "prompt evaluation failed"); free(new_tokens); llm->n_cached_tokens = n_common; return; @@ -299,7 +307,7 @@ void llm_run(struct llm_ctx *llm, const char *user_prompt, FILE *out) batch = llama_batch_get_one(&new_token_id, 1); if (llama_decode(llm->ctx, batch) != 0) { - fprintf(stderr, "ERROR: llama_decode failed!\n"); + syslog(LOG_ERR, "decode() error in generation loop"); break; } @@ -1,14 +1,16 @@ #include <stdio.h> +#include <stdlib.h> #include <err.h> #include <errno.h> #include <string.h> +#include <syslog.h> #include <unistd.h> #include <sys/un.h> #include <sys/socket.h> #include "llm.h" -#define SOCK_PATH "/var/run/lex.sock" +#define SOCK_PATH "/var/run/lexd/sock" #define MODEL_PATH "qwen2.5-3b-instruct-q4_k_m.gguf" static void usage(const char *prog) @@ -27,12 +29,12 @@ static void loop(struct llm_ctx *llm, int lsock) csock = accept(lsock, NULL, NULL); if (csock == -1) { if (errno != EINTR) - warn("accept()"); + syslog(LOG_WARNING, "accept() failed: %m"); continue; } if (!(cfp = fdopen(csock, "r+"))) { - warn("cfp fdopen()"); + syslog(LOG_WARNING, "fdopen() failed for cfp: %m"); close(csock); continue; } @@ -40,7 +42,7 @@ static void loop(struct llm_ctx *llm, int lsock) if (fgets(line, sizeof(line), cfp) != NULL) { len = strcspn(line, "\n"); if (line[len] == '\0' && len == sizeof(line) - 1) { - warnx("request too large"); + syslog(LOG_WARNING, "request too large"); // drain and skip while ((c = fgetc(cfp)) != '\n' && c != EOF) ; @@ -63,8 +65,10 @@ int sock_init(void) struct sockaddr_un addr; sock = socket(AF_UNIX, SOCK_STREAM, 0); - if (sock == -1) - err(1, "socket()"); + if (sock == -1) { + syslog(LOG_ERR, "socket error: %m"); + exit(1); + } memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; @@ -72,11 +76,15 @@ int sock_init(void) unlink(SOCK_PATH); /* remove stale socket from a previous run */ - if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) - err(1, "bind()"); + if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) { + syslog(LOG_ERR, "bind failed: %m"); + exit(1); + } - if (listen(sock, 1) == -1) /* backlog = 1 */ - err(1, "listen()"); + if (listen(sock, 1) == -1) { /* backlog = 1 */ + syslog(LOG_ERR, "listen(): %m"); + exit(1); + } return sock; } @@ -91,6 +99,8 @@ int main(int argc , char *argv[]) prog = argv[0]; model_path = MODEL_PATH; + openlog("lexd", LOG_PID | LOG_NDELAY, LOG_DAEMON); + while ((opt = getopt(argc, argv, "m:")) != -1) { switch (opt) { case 'm': @@ -106,23 +116,34 @@ int main(int argc , char *argv[]) if (argc != 0) usage(prog); - if (unveil(model_path, "r") == -1) - err(1, "unveil %s failed", model_path); - if (unveil(SOCK_PATH, "rwc") == -1) - err(1, "unveil %s failed", SOCK_PATH); - if (unveil(NULL, NULL) == -1) - err(1, "unveil lock failed"); + if (unveil(model_path, "r") == -1) { + syslog(LOG_ERR, "unveil %s: %m", model_path); + exit(1); + } + + if (unveil(SOCK_PATH, "rwc") == -1) { + syslog(LOG_ERR, "unveil %s: %m", SOCK_PATH); + exit(1); + } + + if (unveil(NULL, NULL) == -1) { + syslog(LOG_ERR, "unveil lock failed: %m"); + exit(1); + } sock = sock_init(); - if (!(llm = llm_init(model_path))) - errx(1, "Failed to load model"); + if (!(llm = llm_init(model_path))) { + syslog(LOG_ERR, "failed to load model"); + exit(1); + } /* llm_init() calls mlock, hence pledge() after that. */ - if (pledge("stdio unix", NULL) == -1) - err(1, "secondary pledge failed"); + if (pledge("stdio unix", NULL) == -1) { + syslog(LOG_ERR, "secondary pledge: %m"); + exit(1); + } loop(llm, sock); /* doesn't return */ - return 0; } @@ -3,6 +3,7 @@ #include <err.h> #include <stdlib.h> +#include <syslog.h> #define MALLOC(s) xmalloc((s), __FILE__, __LINE__) #define CALLOC(n, s) xcalloc((n), (s), __FILE__, __LINE__) @@ -12,8 +13,10 @@ static inline void *xmalloc(size_t s, const char *file, int line) { void *p; - if (!(p = malloc(s))) - err(1, "%s:%d: malloc", file, line); + if (!(p = malloc(s))) { + syslog(LOG_ERR, "malloc: %s (line %d)", file, line); + exit(1); + } return p; } @@ -21,8 +24,10 @@ static inline void *xcalloc(size_t n, size_t s, const char *file, int line) { void *p; - if (!(p = calloc(n, s))) - err(1, "%s:%d: calloc", file, line); + if (!(p = calloc(n, s))) { + syslog(LOG_ERR, "calloc: %s (line %d)", file, line); + exit(1); + } return p; } @@ -30,8 +35,10 @@ static inline void *xrealloc(void *ptr, size_t s, const char *file, int line) { void *p; - if (!(p = realloc(ptr, s))) - err(1, "%s:%d: realloc", file, line); + if (!(p = realloc(ptr, s))) { + syslog(LOG_ERR, "realloc: %s (line %d)", file, line); + exit(1); + } return p; } diff --git a/test_prompts.sh b/test_prompts.sh index 0424e62..1de3822 100755 --- a/test_prompts.sh +++ b/test_prompts.sh @@ -2,7 +2,7 @@ # Usage: ./test_prompts.sh <model_path> [prompt_file] -SOCK_PATH="/var/run/lex.sock" +SOCK_PATH="/var/run/lexd/sock" EXECUTABLE="./lex" # Mandatory Model Argument |
