diff --git a/abi/rad_clef.h b/abi/rad_clef.h index 6aba09e7..0168361c 100644 --- a/abi/rad_clef.h +++ b/abi/rad_clef.h @@ -22,7 +22,11 @@ extern "C" { enum { RAD_CLEF_MAX_QUESTIONS = 64, RAD_CLEF_MAX_OPTIONS = 512, /* every question's options together */ - RAD_CLEF_MAX_LEX = 8192 /* every option's tokens together */ + RAD_CLEF_MAX_LEX = 8192, /* every option's tokens together */ + /* Prompt tokens. The head gathers every row of a prompt into buffers of its own, so this, + * not the deployment's context, sizes them: at the model's own 262144 they would take + * 4.5 GiB of the card for prompts that never pass a fifth of it. */ + RAD_CLEF_MAX_PROMPT = 32768 }; /* The header: word 0 is the engine's (RadBatch::plugin_words), the rest the counts the layout is diff --git a/arch/common/rad_block_clef.h b/arch/common/rad_block_clef.h index f0fa3035..6e3ffd9c 100644 --- a/arch/common/rad_block_clef.h +++ b/arch/common/rad_block_clef.h @@ -151,7 +151,10 @@ inline int ClefHead::declare(RadBuilder* b, const RadModelMeta* meta, const RadB if (E % 2) return RAD_E_INVAL; bs = rad_kv_block_size(b, kv_trunk); if (bs <= 0) return RAD_E_STATE; - nblk = (g.max_ctx + bs - 1) / bs; + /* Rows a request may hold: the context, or the head's own bound where that is less (the + * server refuses a longer prompt before it is admitted). */ + const int64_t rows = g.max_ctx < RAD_CLEF_MAX_PROMPT ? g.max_ctx : RAD_CLEF_MAX_PROMPT; + nblk = (rows + bs - 1) / bs; NM = nblk * bs; n_vocab = g.n_vocab; layer = bind_layer; diff --git a/core/server/clef.cpp b/core/server/clef.cpp index 296f006b..4289508f 100644 --- a/core/server/clef.cpp +++ b/core/server/clef.cpp @@ -182,12 +182,17 @@ int parse_clef_request(const std::string& body, const OaiDeps& d, const OaiLimit const std::vector suffix = ids("\n<|im_end|>\n<|im_start|>assistant\n\n\n\n\nJOINT SCHEMA DECISIONS:"); const int64_t n = (int64_t)(prefix.size() + state.size() + schema.size() + suffix.size()); - if (lim.max_ctx > 0 && n > lim.max_ctx) { - ApiError e = err_bad("state", "the prompt is " + std::to_string(n) + " tokens; this " - "deployment's context holds " + std::to_string(lim.max_ctx)); + /* The context, or the head's own bound where that is less (abi/rad_clef.h). */ + const int64_t cap = lim.max_ctx > 0 && lim.max_ctx < RAD_CLEF_MAX_PROMPT ? lim.max_ctx + : RAD_CLEF_MAX_PROMPT; + if (n > cap) { + ApiError e = err_bad("state", "the prompt is " + std::to_string(n) + " tokens; " + + (cap == lim.max_ctx ? "this deployment's context holds " + : "clef's head takes at most ") + + std::to_string(cap)); e.code = "context_length_exceeded"; e.n_prompt_tokens = n; - e.n_ctx = lim.max_ctx; + e.n_ctx = cap; *err = e; return RAD_E_INVAL; } diff --git a/tests/clef_test.cpp b/tests/clef_test.cpp index 708205b4..997563de 100644 --- a/tests/clef_test.cpp +++ b/tests/clef_test.cpp @@ -134,6 +134,12 @@ TEST(bad_requests_are_400s) { /* The whole prompt must fit; nothing is cut. */ CHECK(parse(tok, kBody, &r, &e, 100) < 0); CHECK_EQ(e.code, "context_length_exceeded"); + /* And it fits the head's own bound, whatever the context: a byte a token here. */ + const std::string huge = R"({"state": ")" + std::string(RAD_CLEF_MAX_PROMPT, 'x') + + R"(", "questions": {"q": {"type": "noul"}}})"; + CHECK(parse(tok, huge, &r, &e, 0) < 0); + CHECK_EQ(e.code, "context_length_exceeded"); + CHECK_EQ(e.n_ctx, (int64_t)RAD_CLEF_MAX_PROMPT); } TEST(answers_are_softmaxes_in_the_releases_shape) {