summaryrefslogtreecommitdiffstats
path: root/llm.c
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2026-08-18 17:45:10 +0800
committerSadeep Madurange <sadeep@asciimx.com>2026-08-18 17:45:10 +0800
commit82680bc3350807c532d0aeb714cb5360da9293f0 (patch)
treec34a1965430d6019e45fc184cb6d3a86bc1ccd50 /llm.c
parent75db6d795d689e28560aa7e6b3d471c89983191a (diff)
downloadlex-82680bc3350807c532d0aeb714cb5360da9293f0.tar.gz
Use syslog, move the sock to dexd subdirectory.
Diffstat (limited to 'llm.c')
-rw-r--r--llm.c32
1 files changed, 20 insertions, 12 deletions
diff --git a/llm.c b/llm.c
index 4d3dc58..e979929 100644
--- a/llm.c
+++ b/llm.c
@@ -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;
}