aboutsummaryrefslogtreecommitdiff
path: root/src/server_internal.h
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-20 20:10:56 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-21 05:57:19 +0200
commit6b969d25ce136eaf695222bd1a10bff1ecac0220 (patch)
treec23afb50c28b9deaddae5c6526a74ee11cf2d865 /src/server_internal.h
parentd4bdb8dae753462f621507d5fc3582e0e53da35c (diff)
downloadffwd-6b969d25ce136eaf695222bd1a10bff1ecac0220.tar.gz
ffwd-6b969d25ce136eaf695222bd1a10bff1ecac0220.zip
all: split Makefile and reorg library/tools
Diffstat (limited to '')
-rw-r--r--tools/server/server_internal.h (renamed from src/server_internal.h)153
1 files changed, 53 insertions, 100 deletions
diff --git a/src/server_internal.h b/tools/server/server_internal.h
index 1f106ca..91b51c5 100644
--- a/src/server_internal.h
+++ b/tools/server/server_internal.h
@@ -1,27 +1,17 @@
-#ifndef EMBED_SERVER_INTERNAL_H
-#define EMBED_SERVER_INTERNAL_H
+#ifndef FFWD_SERVER_INTERNAL_H
+#define FFWD_SERVER_INTERNAL_H
-/* Declarations shared across the embed-server translation units (HTTP framing,
+/* Declarations shared across the ffwd-server translation units (HTTP framing,
* JSON validation, output encoding, scheduling, model lifecycle, endpoint
* handlers). The server is split by concern but compiles as several objects
* linked into one binary; this header is what they share. Not a public API -
* the CLI and library do not include it. */
-#include "embed.h"
+#include "ffwd.h"
#include "server.h"
#include "sbuf.h"
-#include "tokenizer_bpe.h"
-#include "tokenizer_sentencepiece.h"
-#include "tokenizer_wordpiece.h"
-#ifdef USE_MLX
-#include "mlx.h"
-#endif
-#ifdef USE_CUDA
-#include "cuda.h"
-#endif
-
-#include "deps/ae/ae.h"
+#include "ae.h"
#include <cjson/cJSON.h>
#include <pthread.h>
@@ -29,45 +19,43 @@
#include <stddef.h>
#include <stdint.h>
-#define EMBED_HTTP_IO_BUF 8192
-#define EMBED_HTTP_MAX_HEADER (64u * 1024u)
-#define EMBED_HTTP_MAX_BODY (64u * 1024u * 1024u)
-#define EMBED_HTTP_MAX_PATH 255u
-#define EMBED_HTTP_BACKLOG 128
-#define EMBED_HTTP_SETSIZE 10240
-#define EMBED_HTTP_CLIENT_TIMEOUT_MS 30000
-#define EMBED_HTTP_SWEEP_MS 1000
+#define FFWD_HTTP_IO_BUF 8192
+#define FFWD_HTTP_MAX_HEADER (64u * 1024u)
+#define FFWD_HTTP_MAX_BODY (64u * 1024u * 1024u)
+#define FFWD_HTTP_MAX_PATH 255u
+#define FFWD_HTTP_BACKLOG 128
+#define FFWD_HTTP_SETSIZE 10240
+#define FFWD_HTTP_CLIENT_TIMEOUT_MS 30000
+#define FFWD_HTTP_SWEEP_MS 1000
-#define EMBED_API_MAX_STANDARD_INPUTS 512
-#define EMBED_API_MAX_CONTEXT_DOCS 512
-#define EMBED_API_MAX_CONTEXT_CHUNKS 16000
-#define EMBED_API_MAX_RERANK_DOCUMENTS 1000
-#define EMBED_API_MAX_ITEM_TOKENS 32768
-#define EMBED_API_MAX_TOTAL_TOKENS 120000
-#define EMBED_LATE_QUERY_TOKENS 32
-#define EMBED_LATE_MASK_TOKEN_ID 151642
-#define EMBED_LATE_QUERY_PREFIX_ID 151669
-#define EMBED_LATE_DOCUMENT_PREFIX_ID 151670
+#define FFWD_API_MAX_STANDARD_INPUTS 512
+#define FFWD_API_MAX_CONTEXT_DOCS 512
+#define FFWD_API_MAX_CONTEXT_CHUNKS 16000
+#define FFWD_API_MAX_RERANK_DOCUMENTS 1000
+#define FFWD_API_MAX_ITEM_TOKENS 32768
+#define FFWD_API_MAX_TOTAL_TOKENS 120000
+/* FFWD_LATE_* and FFWD_CONTEXT_SEPARATOR_TOKEN_ID now live in ffwd.h, owned
+ * by the tokenizer (ffwd_tok). */
/* Chosen from the L4 scheduler sweep: -b 32 gained ~24% concurrent
* short-request throughput over 8 with no long-document penalty, while 128
* was no faster and inflated long-document tail latency. */
-#define EMBED_SERVER_DEFAULT_BATCH_SIZE 32
-#define EMBED_SERVER_DEFAULT_MAX_BATCH_TOKENS 16384
+#define FFWD_SERVER_DEFAULT_BATCH_SIZE 32
+#define FFWD_SERVER_DEFAULT_MAX_BATCH_TOKENS 16384
/* Microseconds the worker waits for more requests before dispatching a batch.
* CUDA uses a 1 ms window so arrivals group into one launch and keep the GPU
* busy; MLX and CPU dispatch immediately, where waiting only adds latency.
* Override with --batch-wait-us. */
-#define EMBED_SERVER_BATCH_WAIT_US 0
-#define EMBED_SERVER_CUDA_BATCH_WAIT_US 1000
-#define EMBED_SERVER_MICROBATCH_MAX_JOBS 128
-#define EMBED_MLX_MEMORY_BUDGET_PERCENT 90
-#define EMBED_MLX_RESIDENT_MULTIPLIER 2
+#define FFWD_SERVER_BATCH_WAIT_US 0
+#define FFWD_SERVER_CUDA_BATCH_WAIT_US 1000
+#define FFWD_SERVER_MICROBATCH_MAX_JOBS 128
+#define FFWD_MLX_MEMORY_BUDGET_PERCENT 90
+#define FFWD_MLX_RESIDENT_MULTIPLIER 2
typedef enum { MODEL_KIND_STANDARD, MODEL_KIND_CONTEXTUAL, MODEL_KIND_LATE } model_kind;
typedef enum {
- EMBED_API_OPENAI = 0,
- EMBED_API_PERPLEXITY = 1,
+ FFWD_API_OPENAI = 0,
+ FFWD_API_PERPLEXITY = 1,
} embedding_api_t;
typedef struct {
@@ -76,8 +64,8 @@ typedef struct {
int dim;
int min_dim;
int token_dim;
- embed_attention_mode_t attention_mode;
- embed_pooling_mode_t pooling_mode;
+ ffwd_attention_mode_t attention_mode;
+ ffwd_pooling_mode_t pooling_mode;
int normalize_embeddings;
embedding_api_t api;
char *query_instruct;
@@ -86,38 +74,9 @@ typedef struct {
typedef struct {
model_info *info;
char *path;
- embed_tokenizer_t *tok;
- embed_tokenizer_workspace_t *tok_ws;
- /* BERT-family WordPiece tokenizer, selected by file probe in load_one_model.
- * When wp_tok is set, tokenize_one wraps the ids with [CLS]/[SEP] and the
- * Qwen byte-level BPE fields above stay NULL. */
- wordpiece_tokenizer_t *wp_tok;
- wordpiece_workspace_t *wp_tok_ws;
- sentencepiece_tokenizer_t *sp_tok;
- sentencepiece_workspace_t *sp_tok_ws;
- int cls_id;
- int sep_id;
- int context_separator_id;
- int late_mask_id;
- int late_query_prefix_id;
- int late_document_prefix_id;
- int late_skip_ids[64];
- int n_late_skip_ids;
- int append_terminal_token;
- int terminal_token_id;
- int renormalize_truncated;
- embed_model_t *cpu_model;
- embed_workspace_t *cpu_ws;
- embed_late_model_t *cpu_late_model;
- embed_late_workspace_t *cpu_late_ws;
-#ifdef USE_MLX
- embed_mlx_ctx_t *mlx_ctx;
- embed_mlx_late_ctx_t *mlx_late_ctx;
-#endif
-#ifdef USE_CUDA
- embed_cuda_ctx_t *cuda_ctx;
- embed_cuda_late_ctx_t *cuda_late_ctx;
-#endif
+ ffwd_tok_t *tok; /* tokenizer: text -> ids + the model's special tokens */
+ ffwd_t *backend; /* compute handle */
+ int renormalize_truncated; /* serving policy: re-normalize a Matryoshka cut */
} loaded_model;
typedef struct client client;
@@ -136,10 +95,7 @@ typedef struct {
char *api_key;
loaded_model *models;
int n_models;
- int use_mlx;
- int use_cuda;
- int mlx_quantize_bits;
- int mlx_quantize_group_size;
+ ffwd_options_t backend_opts;
pthread_t worker;
pthread_mutex_t mu;
@@ -158,7 +114,7 @@ typedef struct {
typedef struct {
char method[8];
- char path[EMBED_HTTP_MAX_PATH + 1];
+ char path[FFWD_HTTP_MAX_PATH + 1];
char version[16];
char *body;
size_t body_len;
@@ -188,7 +144,7 @@ struct job {
http_server *srv;
client *c;
char method[8];
- char path[EMBED_HTTP_MAX_PATH + 1];
+ char path[FFWD_HTTP_MAX_PATH + 1];
char *body;
size_t body_len;
char *auth;
@@ -222,7 +178,7 @@ typedef struct {
const char *encoding;
int n_inputs;
token_buf *tokens;
- embed_input_t *inputs;
+ ffwd_input_t *inputs;
int total_tokens;
int ready;
} embedding_request;
@@ -230,7 +186,7 @@ typedef struct {
typedef struct {
int *ids;
int n_tokens;
- embed_span_t *spans;
+ ffwd_span_t *spans;
int n_spans;
} contextual_doc;
@@ -247,12 +203,8 @@ typedef struct {
int ready;
} contextual_request;
-typedef struct {
- int *ids;
- int n_tokens;
- int *keep;
- int n_keep;
-} late_tokens;
+/* The server's late_tokens is exactly what ffwd_tokenize_late fills. */
+typedef ffwd_late_tokens_t late_tokens;
typedef struct {
job *j;
@@ -278,8 +230,7 @@ cJSON *parse_json_body(job *j, cJSON *detail);
bool cjson_is_integer(cJSON *item);
const char *encoding_from_root(cJSON *root, cJSON *detail, embedding_api_t api);
int text_type_is_query(cJSON *root, cJSON *detail, const char *query_instruct);
-int dimensions_from_root(
- cJSON *root, cJSON *detail, int min_dim, int max_dim, const char *encoding);
+int dimensions_from_root(cJSON *root, cJSON *detail, int min_dim, int max_dim, const char *encoding);
/* ---- server_http.c: connection lifecycle, header parse, response framing ---- */
int set_nonblock(int fd);
@@ -317,19 +268,21 @@ void embedding_request_free(embedding_request *r);
void contextual_request_free(contextual_request *r);
void late_tokens_free(late_tokens *t);
void rerank_request_free(rerank_request *r);
+/* Thin timing wrappers over libffwd's ffwd_tokenize / ffwd_tokenize_late. */
int tokenize_one(loaded_model *m, job *j, const char *text, token_buf *out);
int tokenize_input(
loaded_model *m, job *j, const char *text, const char *query_instruct, token_buf *out);
int tokenize_late_text(loaded_model *m, job *j, const char *text, int is_query, late_tokens *out);
-int model_embed_batch(loaded_model *m, const embed_input_t *inputs, int batch, float *out);
-int model_embed_spans_batch(loaded_model *m,
- const embed_context_input_t *inputs,
- int batch,
- float *out);
+int model_ffwd_batch(loaded_model *m, const ffwd_input_t *inputs, int batch, float *out);
+int model_ffwd_spans_batch(loaded_model *m,
+ const ffwd_context_input_t *inputs,
+ int batch,
+ float *out);
int inference_batch_accepts_input(
const loaded_model *m, int batch, int packed_tokens, int next_tokens, int max_batch_tokens);
-int configure_loaded_model(loaded_model *m, const embed_config_t *config, int token_dim);
-int load_one_model(http_server *s, const embed_server_model_spec_t *spec);
+int configure_loaded_model(loaded_model *m, const ffwd_config_t *config, int token_dim);
+int finalize_loaded_model(loaded_model *m);
+int load_one_model(http_server *s, const ffwd_server_model_spec_t *spec);
void free_models(http_server *s);
/* ---- server_handlers.c: per-endpoint prepare / batch-group / execute ---- */
@@ -358,4 +311,4 @@ int collect_job_batch(http_server *s, job **jobs, int max_jobs);
void process_job_group(http_server *s, job **jobs, int n_jobs);
void completion_cb(aeEventLoop *loop, int fd, void *clientData, int mask);
-#endif /* EMBED_SERVER_INTERNAL_H */
+#endif /* FFWD_SERVER_INTERNAL_H */