From ed23a1571fe6911eba9d4727bef30c82df7be06d Mon Sep 17 00:00:00 2001 From: Onur Gündüz Date: Sat, 4 Jul 2026 02:37:58 +0200 Subject: server: per-model input token cap, auto-truncate, and 429 admission --- tools/server/test_models.c | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'tools/server/test_models.c') diff --git a/tools/server/test_models.c b/tools/server/test_models.c index 3dce62b..48a2da1 100644 --- a/tools/server/test_models.c +++ b/tools/server/test_models.c @@ -1,4 +1,5 @@ -/* test_models.c - resolving a served model by its request label. */ +/* test_models.c - resolving a served model by its request label, and the + * per-input token cap with its truncation helper. */ #include "server_internal.h" #include "test_util.h" @@ -20,7 +21,46 @@ static void test_loaded_model_lookup(void) { TEST_ASSERT(loaded_model_for_label(&s, "compiled-name") == NULL); } +static void test_model_item_token_cap(void) { + model_info info; + memset(&info, 0, sizeof(info)); + + /* No model cap: the API item cap applies, then the batch budget. */ + TEST_ASSERT(model_item_token_cap(&info, 0) == FFWD_API_MAX_ITEM_TOKENS); + TEST_ASSERT(model_item_token_cap(&info, 16384) == 16384); + + /* Model cap below both: it wins. */ + info.max_seq_tokens = 512; + TEST_ASSERT(model_item_token_cap(&info, 16384) == 512); + + /* Batch budget below the model cap: the budget wins. */ + info.max_seq_tokens = 32768; + TEST_ASSERT(model_item_token_cap(&info, 16384) == 16384); + TEST_ASSERT(model_item_token_cap(&info, 0) == 32768); +} + +static void test_truncate_token_buf(void) { + int ids[6] = {101, 7, 8, 9, 10, 102}; + token_buf t = {.ids = ids, .n_tokens = 6}; + + /* Under the cap: untouched. */ + truncate_token_buf(&t, 6, 1); + TEST_ASSERT(t.n_tokens == 6 && ids[5] == 102); + + /* keep_tail moves the final special token to the new end. */ + truncate_token_buf(&t, 4, 1); + TEST_ASSERT(t.n_tokens == 4); + TEST_ASSERT(ids[0] == 101 && ids[1] == 7 && ids[2] == 8 && ids[3] == 102); + + /* Plain cut when the layout has no required tail. */ + truncate_token_buf(&t, 2, 0); + TEST_ASSERT(t.n_tokens == 2); + TEST_ASSERT(ids[0] == 101 && ids[1] == 7); +} + int main(void) { test_loaded_model_lookup(); + test_model_item_token_cap(); + test_truncate_token_buf(); return TEST_REPORT("models"); } -- cgit v1.3.1