aboutsummaryrefslogtreecommitdiff
path: root/tools/server/models.c
blob: 7420542d0f95516fb46adcbce4ca9be1292f2541 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* Model load/lifecycle, plus tokenization and inference dispatch.
 *
 * Each loaded model owns resolved serving metadata for the operator-chosen
 * label.
 *
 * Transformer behavior comes from model files through ffwd_config.
 * Server-only facts missing from those files, such as output API family and
 * Matryoshka floor, come from the load spec.
 *
 * tokenize_* and model_ffwd_* helpers are the request handlers' single dispatch
 * point, hiding the compiled backend choice behind one signature.
 */

#include "server_internal.h"
#include "util.h"

#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

loaded_model *loaded_model_for_label(http_server *s, const char *label) {
    if (!label)
        return NULL;
    for (int i = 0; i < s->n_models; i++) {
        if (s->models[i].info && !strcmp(label, s->models[i].info->id))
            return &s->models[i];
    }
    return NULL;
}

static model_kind model_kind_from_public(ffwd_server_model_kind_t kind) {
    switch (kind) {
    case FFWD_SERVER_MODEL_CONTEXTUAL:
        return MODEL_KIND_CONTEXTUAL;
    case FFWD_SERVER_MODEL_LATE:
        return MODEL_KIND_LATE;
    case FFWD_SERVER_MODEL_STANDARD:
    default:
        return MODEL_KIND_STANDARD;
    }
}

static embedding_api_t embedding_api_from_public(ffwd_server_embedding_api_t api) {
    return api == FFWD_SERVER_API_PERPLEXITY ? FFWD_API_PERPLEXITY : FFWD_API_OPENAI;
}

static int read_optional_text_file(const char *model_dir, const char *relative, char **out) {
    char path[1024];
    int n = snprintf(path, sizeof(path), "%s/%s", model_dir, relative);
    if (n < 0 || (size_t)n >= sizeof(path)) {
        server_log("ffwd-server: model path too long");
        return -1;
    }
    FILE *f = fopen(path, "rb");
    if (!f && errno == ENOENT)
        return 0;
    if (!f) {
        server_log("ffwd-server: failed to open %s", path);
        return -1;
    }
    if (fseek(f, 0, SEEK_END) != 0) {
        fclose(f);
        return -1;
    }
    long sz = ftell(f);
    if (sz < 0 || (size_t)sz == SIZE_MAX || fseek(f, 0, SEEK_SET) != 0) {
        fclose(f);
        return -1;
    }
    char *buf = (char *)malloc((size_t)sz + 1);
    if (!buf) {
        fclose(f);
        return -1;
    }
    if (fread(buf, 1, (size_t)sz, f) != (size_t)sz) {
        free(buf);
        fclose(f);
        return -1;
    }
    fclose(f);
    buf[sz] = '\0';
    *out = buf;
    return 1;
}

static int read_query_instruct_from_files(const char *model_dir, char **out) {
    char *buf = NULL;
    int rc = read_optional_text_file(model_dir, "config_sentence_transformers.json", &buf);
    if (rc <= 0)
        return rc;
    yyjson_doc *doc = yyjson_read(buf, strlen(buf), 0);
    free(buf);
    if (!doc) {
        server_log("ffwd-server: invalid config_sentence_transformers.json: %s", model_dir);
        return -1;
    }
    yyjson_val *root = yyjson_doc_get_root(doc);
    yyjson_val *prompts = yyjson_obj_get(root, "prompts");
    yyjson_val *query = prompts ? yyjson_obj_get(prompts, "query") : NULL;
    if (query && !yyjson_is_str(query)) {
        yyjson_doc_free(doc);
        server_log("ffwd-server: prompts.query must be a string: %s", model_dir);
        return -1;
    }
    const char *qs = yyjson_get_str(query);
    if (qs && qs[0]) {
        *out = xstrdup(qs);
        yyjson_doc_free(doc);
        return *out ? 1 : -1;
    }
    yyjson_doc_free(doc);
    return 0;
}

static int init_model_info(model_info **out, const ffwd_server_model_spec_t *spec) {
    model_info *info = xcalloc(1, sizeof(*info));
    info->id = xstrdup(spec->id);
    info->kind = model_kind_from_public(spec->kind);
    info->api = embedding_api_from_public(spec->api);
    info->min_dim = spec->min_dim;
    if (spec->query_instruct) {
        info->query_instruct = xstrdup(spec->query_instruct);
    } else if (read_query_instruct_from_files(spec->path, &info->query_instruct) < 0) {
        free(info->id);
        free(info);
        return -1;
    }
    if (!info->id || (spec->query_instruct && !info->query_instruct)) {
        free(info->query_instruct);
        free(info->id);
        free(info);
        return -1;
    }
    *out = info;
    return 0;
}

static void free_model_info(model_info *info) {
    if (!info)
        return;
    free(info->id);
    free(info->query_instruct);
    free(info);
}

void free_token_bufs(token_buf *t, int n) {
    if (!t)
        return;
    for (int i = 0; i < n; i++)
        free(t[i].ids);
}

void embedding_request_free(embedding_request *r) {
    if (!r)
        return;
    free_token_bufs(r->tokens, r->n_inputs);
    free(r->tokens);
    free(r->inputs);
    if (r->root)
        yyjson_doc_free(r->root);
    memset(r, 0, sizeof(*r));
}

void contextual_request_free(contextual_request *r) {
    if (!r)
        return;
    for (int i = 0; i < r->n_docs; i++) {
        free(r->docs[i].ids);
        free(r->docs[i].spans);
    }
    free(r->docs);
    if (r->root)
        yyjson_doc_free(r->root);
    memset(r, 0, sizeof(*r));
}

void late_tokens_free(late_tokens *t) {
    if (!t)
        return;
    free(t->ids);
    free(t->keep);
    memset(t, 0, sizeof(*t));
}

void rerank_request_free(rerank_request *r) {
    if (!r)
        return;
    late_tokens_free(&r->query);
    for (int i = 0; i < r->n_documents; i++)
        late_tokens_free(&r->documents[i]);
    free(r->documents);
    if (r->root)
        yyjson_doc_free(r->root);
    memset(r, 0, sizeof(*r));
}

/* Largest token count one input may carry for a model: the smallest of the
 * model's positional capacity, the API item cap, and the per-batch token
 * budget. Including the batch budget keeps --max-batch-tokens an honest bound
 * on a single inference launch; without it a lone oversized input would run as
 * a batch bigger than the configured budget. */
int model_item_token_cap(const model_info *info, int max_batch_tokens) {
    int cap = FFWD_API_MAX_ITEM_TOKENS;
    if (info && info->max_seq_tokens > 0 && info->max_seq_tokens < cap)
        cap = info->max_seq_tokens;
    if (max_batch_tokens > 0 && max_batch_tokens < cap)
        cap = max_batch_tokens;
    return cap;
}

/* Cut a tokenized input to cap tokens. When the layout ends with a required
 * special token ([SEP], </s>, or the Qwen terminal), that token moves to the
 * new end so the truncated sequence keeps a valid layout. */
void truncate_token_buf(token_buf *t, int cap, int keep_tail) {
    if (!t || !t->ids || cap <= 0 || t->n_tokens <= cap)
        return;
    if (keep_tail)
        t->ids[cap - 1] = t->ids[t->n_tokens - 1];
    t->n_tokens = cap;
}

/* Thin wrappers that add server per-stage timing to the tokenization job.
 * Request handlers call them unchanged. */
int tokenize_input(
    loaded_model *m, job *j, const char *text, const char *query_instruct, token_buf *out) {
    uint64_t t0 = nstime();
    out->ids = ffwd_tokenize(m->tok, text, query_instruct, &out->n_tokens);
    if (j)
        j->tokenize_ns += nstime() - t0;
    if (!out->ids) {
        memset(out, 0, sizeof(*out));
        return -1;
    }
    return 0;
}

int tokenize_one(loaded_model *m, job *j, const char *text, token_buf *out) {
    return tokenize_input(m, j, text, NULL, out);
}

int tokenize_late_text(loaded_model *m, job *j, const char *text, int is_query, late_tokens *out) {
    uint64_t t0 = nstime();
    int rc = ffwd_tokenize_late(m->tok, text, is_query, out);
    if (j)
        j->tokenize_ns += nstime() - t0;
    return rc;
}

int model_ffwd_batch(loaded_model *m, const ffwd_input_t *inputs, int batch, float *out) {
    return ffwd_encode_batch(m->backend, inputs, batch, out);
}

int model_ffwd_spans_batch(loaded_model *m,
                           const ffwd_context_input_t *inputs,
                           int batch,
                           float *out) {
    return ffwd_encode_spans_batch(m->backend, inputs, batch, out);
}

static int model_uses_dense_batches(const loaded_model *m) {
    return ffwd_uses_dense_batches(m->backend);
}

/* Inputs are sorted by length before chunking. CPU packs real tokens while MLX
 * pads each dense row to the longest input in the chunk. */
int inference_batch_accepts_input(
    const loaded_model *m, int batch, int packed_tokens, int next_tokens, int max_batch_tokens) {
    if (batch == 0)
        return 1;
    if (model_uses_dense_batches(m))
        return next_tokens <= max_batch_tokens / (batch + 1);
    return next_tokens <= max_batch_tokens - packed_tokens;
}

int configure_loaded_model(loaded_model *m, const ffwd_config_t *config, int token_dim) {
    if (!m || !m->info || !config || config->hidden_size <= 0) {
        if (m && m->info)
            server_log("ffwd-server: model %s has invalid config", m->info->id);
        return -1;
    }
    if (m->info->kind == MODEL_KIND_LATE) {
        if (token_dim <= 0) {
            server_log("ffwd-server: late model %s is missing token projection metadata",
                       m->info->id);
            return -1;
        }
    } else if (token_dim != 0) {
        server_log("ffwd-server: non-late model %s unexpectedly has token projection metadata",
                   m->info->id);
        return -1;
    }

    int min_dim = m->info->min_dim > 0 ? m->info->min_dim : config->hidden_size;
    if (min_dim <= 0 || min_dim > config->hidden_size) {
        server_log("ffwd-server: model %s has invalid min_dim=%d for dim=%d", m->info->id, min_dim,
                   config->hidden_size);
        return -1;
    }
    m->info->dim = config->hidden_size;
    m->info->min_dim = min_dim;
    m->info->token_dim = token_dim;
    /* Positional capacity: BERT-family positions live in a learned table of
     * max_position_embeddings rows starting at position_id_offset; RoPE models
     * state a trained context length. 0 (absent) means no model-side cap. */
    m->info->max_seq_tokens = 0;
    if (config->max_position_embeddings > 0) {
        int cap = config->max_position_embeddings - config->position_id_offset;
        m->info->max_seq_tokens = cap > 0 ? cap : 0;
    }
    m->info->truncate_keep_tail = config->family == FFWD_FAMILY_BERT || config->append_terminal_token;
    m->info->attention_mode = config->attention_mode;
    m->info->pooling_mode = config->pooling_mode;
    m->info->normalize_embeddings = config->normalize_embeddings;
    m->renormalize_truncated =
        config->normalize_embeddings && config->pooling_mode == FFWD_POOL_LAST_TOKEN;
    return 0;
}

/* Apply the loaded model's config to its serving metadata and validate it. CPU
 * runs this in load_one_model; a GPU build runs it on the inference thread once
 * ffwd_activate has created the context. */
int finalize_loaded_model(loaded_model *m) {
    const ffwd_config_t *config = ffwd_config(m->backend);
    int token_dim = ffwd_token_dim(m->backend);
    ffwd_tok_set_append_terminal(m->tok, config->append_terminal_token);
    return configure_loaded_model(m, config, token_dim);
}

int load_one_model(http_server *s, const ffwd_server_model_spec_t *spec) {
    /* Grow the model list by one entry. */
    loaded_model *nm = realloc(s->models, (size_t)(s->n_models + 1) * sizeof(*s->models));
    if (!nm)
        return -1;
    s->models = nm;
    loaded_model *m = &s->models[s->n_models];
    memset(m, 0, sizeof(*m));
    s->n_models++;

    if (init_model_info(&m->info, spec) != 0)
        return -1;
    const char *path = spec->path;
    m->path = xstrdup(path);
    char err[256];
    /* The tokenizer (file probe, special-token resolution, [CLS]/[SEP] vs Qwen
     * terminal-token layout) is owned by libffwd now. */
    m->tok = ffwd_tok_open(path, m->info->kind == MODEL_KIND_LATE, err, sizeof(err));
    if (!m->tok) {
        server_log("ffwd-server: %s: %s", path, err);
        return -1;
    }

    m->backend =
        ffwd_open(path, m->info->kind == MODEL_KIND_LATE, &s->backend_opts, err, sizeof(err));
    if (!m->backend) {
        server_log("ffwd-server: %s: %s", path, err);
        return -1;
    }
    /* CPU loads the model in open, so its config is ready to finalize now. A GPU
     * build defers the load - and this finalize - to the inference thread. */
    if (ffwd_config(m->backend))
        return finalize_loaded_model(m);
    return 0;
}

void free_models(http_server *s) {
    for (int i = 0; i < s->n_models; i++) {
        loaded_model *m = &s->models[i];
        free_model_info(m->info);
        free(m->path);
        ffwd_tok_free(m->tok);
        ffwd_free(m->backend);
    }
    free(s->models);
    s->models = NULL;
    s->n_models = 0;
}