Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

qwen36, qwen38: max_tokens is a ceiling, not a target (#1641)

JustVugg5c6a3189a22e
diff --git a/c/qwen36.c b/c/qwen36.c
index 001d5e92..81a2ea31 100644
--- a/c/qwen36.c
+++ b/c/qwen36.c
@@ -3147,14 +3147,36 @@ static void hits_emit(Model *m){
}
static double tm_sum(int idx){ return (g_tm_dec[idx]+g_tm_pre[idx])/1e3; } /* ms -> s */
+/* The generation budget a request gets. max_tokens is a CEILING, not a
+ * target (#260/#382, the rule GLM and DeepSeek V4 already apply): the prompt
+ * must fit with room for one token (none for a read-only logprobs request,
+ * docs/brio.md), and the budget is then clamped to what the context can hold.
+ * Returns the budget, or -1 when the PROMPT does not fit. Refusing when
+ * prompt + budget exceeded the context (#1641) turned the gateway's default
+ * output budget -- 8192 here, the whole default context -- into a 400 on
+ * every message of `coli chat` and on every request without max_tokens. */
+static int qwen36_serve_budget(int np, int max_tok, int max_ctx, int read_only){
+ if (np < 1) return -1;
+ int room = max_ctx - np;
+ if (read_only) return room < 0 ? -1 : (max_tok < room ? max_tok : room);
+ if (room < 1) return -1;
+ return max_tok > room ? room : max_tok;
+}
+
static void serve_one(Model *m, ServeReq *q){
int *ids=NULL, np=0;
encode_text(q->payload, &ids, &np); /* payload is raw prompt text; qwen36 adds no BOS */
int max_ctx = qwen36_max_ctx();
- if(np<1 || np+q->max_tok>max_ctx){
+ int budget = qwen36_serve_budget(np, q->max_tok, max_ctx, q->logprobs > 0);
+ if(budget < 0){
printf("ERROR %s CONTEXT_EXCEEDED prompt_tokens=%d requested=%d capacity=%d\n",q->id,np,q->max_tok,max_ctx);
fflush(stdout); free(ids); return;
}
+ if(budget < q->max_tok){
+ fprintf(stderr,"[serve] max_tokens %d clamped to %d (context %d - prompt %d); raise Q36_MAXT for longer answers\n",
+ q->max_tok, budget, max_ctx, np);
+ q->max_tok = budget;
+ }
printf("ACCEPT %s %d\n",q->id,np); fflush(stdout);
m->max_t = np + q->max_tok;
/* Grow the cache BEFORE deciding, so the decision sees the state that will
diff --git a/c/qwen38.c b/c/qwen38.c
index 9eea6cfb..eb4e2c64 100644
--- a/c/qwen38.c
+++ b/c/qwen38.c
@@ -1595,6 +1595,22 @@ static void serve_hits(Model *m){
printf("HITS %d %d %s\n",rows,E,hex); fflush(stdout); free(hex); free(bm);
}
+/* The generation budget a request gets. max_tokens is a CEILING, not a
+ * target (#260/#382, the rule GLM and DeepSeek V4 already apply): the prompt
+ * must fit with room for one token (none for a read-only logprobs request,
+ * docs/brio.md), and the budget is then clamped to what the context can hold.
+ * Returns the budget, or -1 when the PROMPT does not fit. Refusing when
+ * prompt + budget exceeded the context (#1641) turned the gateway's default
+ * output budget -- 8192 here, the whole default context -- into a 400 on
+ * every message of `coli chat` and on every request without max_tokens. */
+static int q38_serve_budget(int np, int max_tok, int max_ctx, int read_only){
+ if (np < 1) return -1;
+ int room = max_ctx - np;
+ if (read_only) return room < 0 ? -1 : (max_tok < room ? max_tok : room);
+ if (room < 1) return -1;
+ return max_tok > room ? room : max_tok;
+}
+
static int serve_one(Model *m, ServeReq *q){
int *ids=NULL, np=0;
encode_text_n(q->payload,(size_t)q->plen,&ids,&np); /* byte-counted prompt; qwen38 adds no BOS */
@@ -1619,11 +1635,16 @@ static int serve_one(Model *m, ServeReq *q){
}
int max_ctx=m->kv_cap;
/* max_tokens=0 in modalita jev: leggere il prompt e fermarsi (serve_codec.h) */
- int tok_min = (q->logprobs > 0) ? 0 : 1;
- if(np<1 || np>max_ctx || q->max_tok<tok_min || q->max_tok>max_ctx-np){
+ int budget = q38_serve_budget(np, q->max_tok, max_ctx, q->logprobs > 0);
+ if(budget < 0){
printf("ERROR %s CONTEXT_EXCEEDED prompt_tokens=%d requested=%d capacity=%d\n",q->id,np,q->max_tok,max_ctx);
fflush(stdout); free(ids); return 0;
}
+ if(budget < q->max_tok){
+ fprintf(stderr,"[serve] max_tokens %d clamped to %d (context %d - prompt %d); raise Q38_MAXT for longer answers\n",
+ q->max_tok, budget, max_ctx, np);
+ q->max_tok = budget;
+ }
printf("ACCEPT %s %d\n",q->id,np); fflush(stdout);
double request_started=now_s();
uint64_t hits_before=m->hits, misses_before=m->miss;
diff --git a/c/tests/test_qwen36_ctx.c b/c/tests/test_qwen36_ctx.c
index 8a906ed3..c2f2e6ef 100644
--- a/c/tests/test_qwen36_ctx.c
+++ b/c/tests/test_qwen36_ctx.c
@@ -125,10 +125,27 @@ static void case_ceiling(void) {
"unset Q36_MAXT falls back to the conservative default");
}
+/* #1641: max_tokens is a ceiling, not a target. The gateway's default budget
+ * (8192, the whole default context) used to be refused on every request
+ * without max_tokens and on every `coli chat` message; now it is clamped to
+ * the room the context leaves, and only a prompt that does not fit is refused.
+ * A read-only logprobs request (max_tokens 0) may fill the context exactly. */
+static void case_budget(void) {
+ ck(qwen36_serve_budget(2, 8192, 8192, 0) == 8190, "default budget clamped to the room left by the prompt");
+ ck(qwen36_serve_budget(100, 50, 8192, 0) == 50, "a budget that fits is untouched");
+ ck(qwen36_serve_budget(8191, 1, 8192, 0) == 1, "one token of room is enough");
+ ck(qwen36_serve_budget(8192, 1, 8192, 0) == -1, "a prompt that leaves no room is refused");
+ ck(qwen36_serve_budget(9000, 1, 8192, 0) == -1, "a prompt longer than the context is refused");
+ ck(qwen36_serve_budget(0, 1, 8192, 0) == -1, "an empty prompt is refused");
+ ck(qwen36_serve_budget(8192, 0, 8192, 1) == 0, "read-only: the prompt may fill the context");
+ ck(qwen36_serve_budget(8193, 0, 8192, 1) == -1, "read-only: past the context is still refused");
+}
+
int main(void) {
case_layout();
case_growth();
case_ceiling();
+ case_budget();
if (fails) { printf("FAILED %d\n", fails); return 1; }
printf("OK test_qwen36_ctx\n");
return 0;
diff --git a/c/tests/test_qwen38_serve_framing.c b/c/tests/test_qwen38_serve_framing.c
index 3f77ffb2..cf95dc37 100644
--- a/c/tests/test_qwen38_serve_framing.c
+++ b/c/tests/test_qwen38_serve_framing.c
@@ -53,6 +53,19 @@ int main(void){
in=input_with("SUBMIT bad 0 0 1 nan 1\n\n");CHECK(in!=NULL);
CHECK(serve_read_req(in,out,&q,NULL)==0);
fclose(in);fclose(out);
- puts("qwen38 serve framing: mode, decode rate, submit, busy drain, stop, cancel: ok");
+ /* #1641: max_tokens is a ceiling. The gateway's default budget (8192, the
+ * whole default context) used to be refused on every request without
+ * max_tokens and on every `coli chat` message; now it is clamped to what
+ * the context holds, and only a prompt that does not fit is refused. */
+ CHECK(q38_serve_budget(2, 8192, 8192, 0)==8190); /* clamped to the room left */
+ CHECK(q38_serve_budget(100, 50, 8192, 0)==50); /* fits: untouched */
+ CHECK(q38_serve_budget(8191, 1, 8192, 0)==1); /* exactly one token of room */
+ CHECK(q38_serve_budget(8192, 1, 8192, 0)==-1); /* the prompt leaves no room */
+ CHECK(q38_serve_budget(9000, 1, 8192, 0)==-1); /* the prompt does not fit */
+ CHECK(q38_serve_budget(0, 1, 8192, 0)==-1); /* empty prompt */
+ CHECK(q38_serve_budget(8192, 0, 8192, 1)==0); /* read-only: the prompt may fill the context */
+ CHECK(q38_serve_budget(8193, 0, 8192, 1)==-1);
+ CHECK(q38_serve_budget(10, 0, 8192, 1)==0);
+ puts("qwen38 serve framing: mode, decode rate, submit, busy drain, stop, cancel, max_tokens ceiling: ok");
return 0;
}
diff --git a/docs/api.md b/docs/api.md
index 76f39fac..5d1bdc60 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -25,7 +25,11 @@ Implemented endpoints are `GET /v1/models`, `GET /v1/models/{model}`,
`POST /v1/chat/completions`, and legacy `POST /v1/completions`. Chat and
completion requests support JSON responses, SSE streaming, usage counts,
`max_tokens`/`max_completion_tokens`, `temperature`, `top_p`, and up to four
-custom `stop` sequences. Stop sequences are removed from the response and end
+custom `stop` sequences. `max_tokens` is a ceiling, not a target: when the
+prompt leaves less room than the budget asks for, every engine clamps the
+budget to what the context holds and the reply ends with `finish_reason:
+"length"`; only a prompt that does not fit is refused, with
+`context_length_exceeded` (#260, #1641). Stop sequences are removed from the response and end
generation early in both JSON and streaming modes. The extension
`x_colibri_ignore_leading_stop: true` discards leading stop sequences until
the first non-whitespace response content, which is useful for local templates
diff --git a/docs/qwen38.md b/docs/qwen38.md
index 7d355347..fd4dccc7 100644
--- a/docs/qwen38.md
+++ b/docs/qwen38.md
@@ -101,7 +101,12 @@ blocks are pooled and scored, the best 512 blocks are retained, and a causal
tail of up to three tokens is appended. The main 24-head attention then operates
only on those original tokens. The native model limit is 262,144 tokens;
`Q38_MAXT` defaults to 8,192 and may raise the server limit up to that native
-ceiling when the required RAM is available.
+ceiling when the required RAM is available. `coli serve --ctx N` (and `coli
+chat --ctx N`) reach the engine as `Q38_MAXT=N`, not as `CTX`: that is the
+variable to look for in the engine process's environment. `max_tokens` is a
+ceiling: a budget the prompt leaves no room for is clamped to the room left
+(one `[serve] max_tokens ... clamped` line on stderr), and only a prompt that
+does not fit is refused.
## Memory and speed