Repository navigation
Conversation
|
our lord and saviour @ggerganov has answered our prayers ! 😄 |
|
!!!! This is exactly what we we're looking for. While I was talking the talk in various PR's and discussions, you were already walking the walk. Kudos ! |
| // TODO: show sample usage | ||
| // | ||
|
|
||
| struct llama_context; |
There was a problem hiding this comment.
It would be really cool if the memory in the llama context and the actual model parameters were in separate structs, so you could initialize a model from a file, then create multiple contexts from it and inference them in parallel. this would be useful if you want to have multiple prompts going at the same time so they can all share the model weights, but have their own k/v memory
There was a problem hiding this comment.
I think it would also be useful if the thread safety of each function was mentioned. For example, can I call llama_eval() and llama_tokenize() concurrently?
There was a problem hiding this comment.
Yes, this will be added via llama_state similar to the way it is implemented in whisper.cpp.
I didn't include it in the first pass of the implementation in order to deal with a smaller change. Will probably add this on Thursday with a second pass. It will not change the existing API - it will simply extend it with new per-state calls.
|
|
||
| for (int i = 0; i < nb; i++) { | ||
| float min = FLT_MAX; | ||
| float max = -FLT_MAX; |
There was a problem hiding this comment.
This went from
float max = std::numeric_limits<float>::min();to
float max = -FLT_MAX;Can't really argue against it, but mentioning it because it's easy to lose these details...
There was a problem hiding this comment.
Is FLT_MAX standard C++? Edit: Yes, part of limits.h
| LLAMA_API llama_token llama_token_bos(); | ||
| LLAMA_API llama_token llama_token_eos(); |
There was a problem hiding this comment.
Suggestion: expand the BOS and EOS acronyms here
There was a problem hiding this comment.
BOS/EOS abbreviations are used by SentencePiece itself (and in other tokenizers). I think the acronyms are probably fine in context here.
| auto lparams = llama_context_default_params(); | ||
|
|
||
| lparams.f16_kv = params.memory_f16; | ||
| lparams.logits_all = params.perplexity; |
There was a problem hiding this comment.
missing parameters:
lparams.n_parts = params.n_parts;
lparams.n_ctx = params.n_ctx;
There was a problem hiding this comment.
They are initialized with defaults from the llama_context_default_params() call above
There was a problem hiding this comment.
Not copying these values from params breaks their respective command line switches (--n_parts and -c).
|
alpaca 30B q4_0 opinion: |
|
|
||
| int main(int argc, char ** argv) { | ||
| ggml_time_init(); | ||
| const int64_t t_main_start_us = ggml_time_us(); |
There was a problem hiding this comment.
the t_main_start_us variable is now unused, and i think it needs to be replaced with a call to llama_reset_timings(ctx)
There was a problem hiding this comment.
actually, ggml_time_init() too. thatone moved to llama_init_from_file(), llama_reset_timings(ctx) should likely be there too.
There was a problem hiding this comment.
t_sample_us and t_predict_us in lines 229-230 are also unused now.
There was a problem hiding this comment.
Should be resolved now
| int n_tokens, | ||
| int n_past, | ||
| int n_threads) { | ||
| if (!llama_eval_internal(*ctx, tokens, n_tokens, n_past, n_threads)) { |
There was a problem hiding this comment.
Evaluation timings are broken. ctx->n_eval should be increased here and this function call should be timed and added to ctx->t_eval_us.
| temp, | ||
| repeat_penalty); | ||
|
|
||
| ctx->t_sample_us += ggml_time_us() - t_start_sample_us; |
There was a problem hiding this comment.
ctx->n_sample should also be increased here.
|
|
||
| if (n_max_tokens < (int) res.size()) { | ||
| fprintf(stderr, "%s: too many tokens\n", __func__); | ||
| return 1; |
There was a problem hiding this comment.
Returning 1 here is ambiguous, it is impossible to tell if there was an error or if only 1 token was returned. Consider returning a negative number to indicate failure (maybe the number of tokens negated?).
There was a problem hiding this comment.
According to the documentation in llama.h it should return -1 on failure. However, may be necessary to rethink or add other API to support cases like the perplexity computation in which a big document has to be tokenized.
There was a problem hiding this comment.
Good catch. Lets fix the perplexity computation on master to avoid blocking the development for too long.
With the new API change that you suggested, we can now query for the number of needed tokens and allocate the necessary memory. Some improvements might also be possible
|
Thank you all for the detailed look! TODO: The perplexity computation needs a small fix - see comment above by @slaren |
fix: Make LLamaState pickleable for disk cache
* Major refactoring - introduce C-style API * Clean up * Add <cassert> * Add <iterator> * Add <algorithm> .... * Fix timing reporting and accumulation * Measure eval time only for single-token calls * Change llama_tokenize return meaning
* Major refactoring - introduce C-style API * Clean up * Add <cassert> * Add <iterator> * Add <algorithm> .... * Fix timing reporting and accumulation * Measure eval time only for single-token calls * Change llama_tokenize return meaning
* cuda: WIP MMA FA * Use MMA for TG also when quantized --------- Co-authored-by: Iwan Kawrakow <[email protected]>
* Major refactoring - introduce C-style API * Clean up * Add <cassert> * Add <iterator> * Add <algorithm> .... * Fix timing reporting and accumulation * Measure eval time only for single-token calls * Change llama_tokenize return meaning
* Major refactoring - introduce C-style API * Clean up * Add <cassert> * Add <iterator> * Add <algorithm> .... * Fix timing reporting and accumulation * Measure eval time only for single-token calls * Change llama_tokenize return meaning
Qwen MTP and multi-seq optimization with new OV ops
pr-set : pin Inkling 1 + 2 via the upstream draft ggml-org#32187
…n fair share of the step, an interactive batch size (-ib) that applies only while a decoder waits, a shortest-remaining skew, the prefill/decode duty cycle, and the KV admission of a whole task
A batch is assembled once per step and cannot be joined mid-flight, so the policy that
decides WHICH prompt tokens enter that step also decides how long every other slot waits.
* LLAMA_SERVER_PREFILL_FAIR (a kill-switch, default on) gives the slots that still have
prompt tokens a max-min fair share of the window: every active prompt is admitted to
every step, and a slot that needs less returns the difference to the pool.
* LLAMA_SERVER_PREFILL_SKEW (default 1 = the plain share) weights the slot with the
shortest remaining prompt N:1 against the others; every other slot still advances.
* -ib / --interactive-batch-size (r306; env LLAMA_ARG_INTERACTIVE_BATCH_SIZE) caps the
prompt tokens admitted per step WHILE a decoder is waiting -- the interactive bound on
a decoding slot's inter-token latency and on a newcomer's wait for admission. -1 = one
n_ubatch (the default), 0 = no bound, N = explicit. A step with NOTHING decoding takes
the whole -b window instead (r306): with no decoder waiting the cap bounds nothing, and
below -ub a smaller step only re-pays the step's fixed cost for nothing. It was
LLAMA_SERVER_PREFILL_CHUNK, env-only and applied to EVERY step, which cost the field
shape half of its uncontended prefill (1454 vs 3036 t/s on the same 65536 tokens); that
env var is kept as the option's deprecated alias, with one WARN.
* LLAMA_SERVER_PREFILL_DUTY (default 16) leaves up to K-1 steps decode-only and puts the
prompt into one step per cycle, BOUNDED in time by the previous prefill step's own
duration, so a decoder gets a burst at the decode-only step time instead of one token
per slow step.
New at r365 (contributor PR ggml-org#370, briansp2020): the same policy now also decides whether a
TASK is admitted at all, because with a unified KV cache -- the delivery's DEFAULT server,
since -np -1 resolves to 4 slots and kv_unified = true -- every slot may use the whole
context, so "a slot is free" says nothing about whether the prompt fits. Two prompts whose
sum exceeded the context were launched together and the failure landed in DECODE, where
nothing can recover it: the retry loop can only clear IDLE slots (try_clear_idle_slots skips
a processing one), so it halved n_batch to 1 and aborted a running slot (measured on r364 at
-c 8192 -np 4 --kv-unified with two ~4.8k-token prompts: both HTTP 500 "Context size has
been exceeded", and with a speculator "speculative batch index N is not inside the current
sub-batch"; the same two prompts alone: both 200).
* server_slot_sched gains an injected `admit` oracle (std::function<bool(const
server_task &)>) and server_slot_plan a `why` (none / no_slot / no_capacity). select()
asks the oracle ONCE, before a candidate is chosen, so the predicate is a whole-request
go/no-go and the scheduler learns nothing about cells, reserves or margins; a refusal
travels the existing "plan.id_slot == -1 -> queue_tasks.defer()" path, so there is no
second defer site, pop_deferred_task() and /metrics' n_tasks_deferred cover it for free,
and a prompt pinned to an idle id_slot is refused by the same path.
* The server context owns the accounting (kv_admit_ok): each RUNNING slot is charged
max(its prompt, its task's tokens) plus a reply reserve. Idle slots are not charged (the
launch path parks the residents and then clears them for want of room) and neither is a
realtime slot waiting for audio (is_processing() but no batch work). With nothing
running a task is always admitted, so a prompt that fits the context alone is never
starved.
* LLAMA_SERVER_KV_ADMIT_RESERVE (default min(n_ctx / n_parallel, n_ctx_slot()) cells, the
per-sequence share a non-unified cache would give each slot) is the reply space charged
per request, capped by the request's own n_predict. LLAMA_SERVER_KV_ADMIT=0 restores
the pre-r365 server exactly. Inert without --kv-unified.
* A release wakes exactly one deferred task, so a task that is re-offered and refused
again would wait for an unrelated request: update_slots() re-offers one when NO slot is
processing -- the condition that makes the wake provably spin-free, since the popped
task is then always admitted.
* A task that was never launched is answered at termination: server_context::terminate()
drains the deferred queue (server_queue::take_deferred_tasks) and aborts the running
slots. Without it a SIGTERM with a deferred task pending never returned -- the exit
hung in HIP's teardown because server_http_context::join() waits for an HTTP worker
waiting on a result nothing produced. PRE-EXISTING (the stock "no free slot" deferral
escaped it because the release path launches the task and then aborts it).
Measured (r365, one R9700, 4B Qwen3.5 Q8_0, -c 8192 -np 4 --kv-unified): two copies of
prompts/prose-rdna-boosts.txt (5246 tokens, cache_prompt false) answer 500/500 on
LLAMA_SERVER_KV_ADMIT=0 and 200/200 with it, at 2.1 s then 4.0 s (serialised), and BOTH
answers are 1dfd5ced6f3e -- the same text the prompt gives when it runs alone, the deferred
one re-prefilling all 5246 tokens. A pair that fits is untouched (no deferral, text equal
to the kill-switch arm), four overlapping prompts drain in order, the reserve census reads
2048 at -c 8192 / 4096 at -c 16384 / 256 with LLAMA_SERVER_KV_ADMIT_RESERVE=256 / +n_predict
when the request caps it, a non-unified server defers nothing, SIGTERM with three pending
deferrals exits in 0.1 s (it never returned before), and a waiting task adds no CPU while
the server idles at 0 ticks after the drain. Gate: scripts/gate-kv-admit.sh (MANIFESTS.md
gate 46), whose --self-test runs every arm on the kill switch and rejects it on 12 checks.
Knobs: ENVIRONMENT.md 29 / DIAGNOSTIC.md 29. Record: WORKLOG.md 261126.001, INVARIANTS.md
r365, patches/README.md block 16.
…n fair share of the step, an interactive batch size (-ib) that applies only while a decoder waits, a shortest-remaining skew, the prefill/decode duty cycle, and the KV admission of a whole task
A batch is assembled once per step and cannot be joined mid-flight, so the policy that
decides WHICH prompt tokens enter that step also decides how long every other slot waits.
* LLAMA_SERVER_PREFILL_FAIR (a kill-switch, default on) gives the slots that still have
prompt tokens a max-min fair share of the window: every active prompt is admitted to
every step, and a slot that needs less returns the difference to the pool.
* LLAMA_SERVER_PREFILL_SKEW (default 1 = the plain share) weights the slot with the
shortest remaining prompt N:1 against the others; every other slot still advances.
* -ib / --interactive-batch-size (r306; env LLAMA_ARG_INTERACTIVE_BATCH_SIZE) caps the
prompt tokens admitted per step WHILE a decoder is waiting -- the interactive bound on
a decoding slot's inter-token latency and on a newcomer's wait for admission. -1 = one
n_ubatch (the default), 0 = no bound, N = explicit. A step with NOTHING decoding takes
the whole -b window instead (r306): with no decoder waiting the cap bounds nothing, and
below -ub a smaller step only re-pays the step's fixed cost for nothing. It was
LLAMA_SERVER_PREFILL_CHUNK, env-only and applied to EVERY step, which cost the field
shape half of its uncontended prefill (1454 vs 3036 t/s on the same 65536 tokens); that
env var is kept as the option's deprecated alias, with one WARN.
* LLAMA_SERVER_PREFILL_DUTY (default 16) leaves up to K-1 steps decode-only and puts the
prompt into one step per cycle, BOUNDED in time by the previous prefill step's own
duration, so a decoder gets a burst at the decode-only step time instead of one token
per slow step.
New at r365 (contributor PR ggml-org#370, briansp2020): the same policy now also decides whether a
TASK is admitted at all, because with a unified KV cache -- the delivery's DEFAULT server,
since -np -1 resolves to 4 slots and kv_unified = true -- every slot may use the whole
context, so "a slot is free" says nothing about whether the prompt fits. Two prompts whose
sum exceeded the context were launched together and the failure landed in DECODE, where
nothing can recover it: the retry loop can only clear IDLE slots (try_clear_idle_slots skips
a processing one), so it halved n_batch to 1 and aborted a running slot (measured on r364 at
-c 8192 -np 4 --kv-unified with two ~4.8k-token prompts: both HTTP 500 "Context size has
been exceeded", and with a speculator "speculative batch index N is not inside the current
sub-batch"; the same two prompts alone: both 200).
* server_slot_sched gains an injected `admit` oracle (std::function<bool(const
server_task &)>) and server_slot_plan a `why` (none / no_slot / no_capacity). select()
asks the oracle ONCE, before a candidate is chosen, so the predicate is a whole-request
go/no-go and the scheduler learns nothing about cells, reserves or margins; a refusal
travels the existing "plan.id_slot == -1 -> queue_tasks.defer()" path, so there is no
second defer site, pop_deferred_task() and /metrics' n_tasks_deferred cover it for free,
and a prompt pinned to an idle id_slot is refused by the same path.
* The server context owns the accounting (kv_admit_ok): each RUNNING slot is charged
max(its prompt, its task's tokens) plus a reply reserve. Idle slots are not charged (the
launch path parks the residents and then clears them for want of room) and neither is a
realtime slot waiting for audio (is_processing() but no batch work). With nothing
running a task is always admitted, so a prompt that fits the context alone is never
starved.
* LLAMA_SERVER_KV_ADMIT_RESERVE (default min(n_ctx / n_parallel, n_ctx_slot()) cells, the
per-sequence share a non-unified cache would give each slot) is the reply space charged
per request, capped by the request's own n_predict. LLAMA_SERVER_KV_ADMIT=0 restores
the pre-r365 server exactly. Inert without --kv-unified.
* A release wakes exactly one deferred task, so a task that is re-offered and refused
again would wait for an unrelated request: update_slots() re-offers one when NO slot is
processing -- the condition that makes the wake provably spin-free, since the popped
task is then always admitted.
* A task that was never launched is answered at termination: server_context::terminate()
drains the deferred queue (server_queue::take_deferred_tasks) and aborts the running
slots. Without it a SIGTERM with a deferred task pending never returned -- the exit
hung in HIP's teardown because server_http_context::join() waits for an HTTP worker
waiting on a result nothing produced. PRE-EXISTING (the stock "no free slot" deferral
escaped it because the release path launches the task and then aborts it).
Measured (r365, one R9700, 4B Qwen3.5 Q8_0, -c 8192 -np 4 --kv-unified): two copies of
prompts/prose-rdna-boosts.txt (5246 tokens, cache_prompt false) answer 500/500 on
LLAMA_SERVER_KV_ADMIT=0 and 200/200 with it, at 2.1 s then 4.0 s (serialised), and BOTH
answers are 1dfd5ced6f3e -- the same text the prompt gives when it runs alone, the deferred
one re-prefilling all 5246 tokens. A pair that fits is untouched (no deferral, text equal
to the kill-switch arm), four overlapping prompts drain in order, the reserve census reads
2048 at -c 8192 / 4096 at -c 16384 / 256 with LLAMA_SERVER_KV_ADMIT_RESERVE=256 / +n_predict
when the request caps it, a non-unified server defers nothing, SIGTERM with three pending
deferrals exits in 0.1 s (it never returned before), and a waiting task adds no CPU while
the server idles at 0 ticks after the drain. Gate: scripts/gate-kv-admit.sh (MANIFESTS.md
gate 46), whose --self-test runs every arm on the kill switch and rejects it on 12 checks.
Knobs: ENVIRONMENT.md 29 / DIAGNOSTIC.md 29. Record: WORKLOG.md 261126.001, INVARIANTS.md
r365, patches/README.md block 16.
…n fair share of the step, an interactive batch size (-ib) that applies only while a decoder waits, a shortest-remaining skew, the prefill/decode duty cycle, and the KV admission of a whole task
A batch is assembled once per step and cannot be joined mid-flight, so the policy that
decides WHICH prompt tokens enter that step also decides how long every other slot waits.
* LLAMA_SERVER_PREFILL_FAIR (a kill-switch, default on) gives the slots that still have
prompt tokens a max-min fair share of the window: every active prompt is admitted to
every step, and a slot that needs less returns the difference to the pool.
* LLAMA_SERVER_PREFILL_SKEW (default 1 = the plain share) weights the slot with the
shortest remaining prompt N:1 against the others; every other slot still advances.
* -ib / --interactive-batch-size (r306; env LLAMA_ARG_INTERACTIVE_BATCH_SIZE) caps the
prompt tokens admitted per step WHILE a decoder is waiting -- the interactive bound on
a decoding slot's inter-token latency and on a newcomer's wait for admission. -1 = one
n_ubatch (the default), 0 = no bound, N = explicit. A step with NOTHING decoding takes
the whole -b window instead (r306): with no decoder waiting the cap bounds nothing, and
below -ub a smaller step only re-pays the step's fixed cost for nothing. It was
LLAMA_SERVER_PREFILL_CHUNK, env-only and applied to EVERY step, which cost the field
shape half of its uncontended prefill (1454 vs 3036 t/s on the same 65536 tokens); that
env var is kept as the option's deprecated alias, with one WARN.
* LLAMA_SERVER_PREFILL_DUTY (default 16) leaves up to K-1 steps decode-only and puts the
prompt into one step per cycle, BOUNDED in time by the previous prefill step's own
duration, so a decoder gets a burst at the decode-only step time instead of one token
per slow step.
New at r365 (contributor PR ggml-org#370, briansp2020): the same policy now also decides whether a
TASK is admitted at all, because with a unified KV cache -- the delivery's DEFAULT server,
since -np -1 resolves to 4 slots and kv_unified = true -- every slot may use the whole
context, so "a slot is free" says nothing about whether the prompt fits. Two prompts whose
sum exceeded the context were launched together and the failure landed in DECODE, where
nothing can recover it: the retry loop can only clear IDLE slots (try_clear_idle_slots skips
a processing one), so it halved n_batch to 1 and aborted a running slot (measured on r364 at
-c 8192 -np 4 --kv-unified with two ~4.8k-token prompts: both HTTP 500 "Context size has
been exceeded", and with a speculator "speculative batch index N is not inside the current
sub-batch"; the same two prompts alone: both 200).
* server_slot_sched gains an injected `admit` oracle (std::function<bool(const
server_task &)>) and server_slot_plan a `why` (none / no_slot / no_capacity). select()
asks the oracle ONCE, before a candidate is chosen, so the predicate is a whole-request
go/no-go and the scheduler learns nothing about cells, reserves or margins; a refusal
travels the existing "plan.id_slot == -1 -> queue_tasks.defer()" path, so there is no
second defer site, pop_deferred_task() and /metrics' n_tasks_deferred cover it for free,
and a prompt pinned to an idle id_slot is refused by the same path.
* The server context owns the accounting (kv_admit_ok): each RUNNING slot is charged
max(its prompt, its task's tokens) plus a reply reserve. Idle slots are not charged (the
launch path parks the residents and then clears them for want of room) and neither is a
realtime slot waiting for audio (is_processing() but no batch work). With nothing
running a task is always admitted, so a prompt that fits the context alone is never
starved.
* LLAMA_SERVER_KV_ADMIT_RESERVE (default min(n_ctx / n_parallel, n_ctx_slot()) cells, the
per-sequence share a non-unified cache would give each slot) is the reply space charged
per request, capped by the request's own n_predict. LLAMA_SERVER_KV_ADMIT=0 restores
the pre-r365 server exactly. Inert without --kv-unified.
* A release wakes exactly one deferred task, so a task that is re-offered and refused
again would wait for an unrelated request: update_slots() re-offers one when NO slot is
processing -- the condition that makes the wake provably spin-free, since the popped
task is then always admitted.
* A task that was never launched is answered at termination: server_context::terminate()
drains the deferred queue (server_queue::take_deferred_tasks) and aborts the running
slots. Without it a SIGTERM with a deferred task pending never returned -- the exit
hung in HIP's teardown because server_http_context::join() waits for an HTTP worker
waiting on a result nothing produced. PRE-EXISTING (the stock "no free slot" deferral
escaped it because the release path launches the task and then aborts it).
Measured (r365, one R9700, 4B Qwen3.5 Q8_0, -c 8192 -np 4 --kv-unified): two copies of
prompts/prose-rdna-boosts.txt (5246 tokens, cache_prompt false) answer 500/500 on
LLAMA_SERVER_KV_ADMIT=0 and 200/200 with it, at 2.1 s then 4.0 s (serialised), and BOTH
answers are 1dfd5ced6f3e -- the same text the prompt gives when it runs alone, the deferred
one re-prefilling all 5246 tokens. A pair that fits is untouched (no deferral, text equal
to the kill-switch arm), four overlapping prompts drain in order, the reserve census reads
2048 at -c 8192 / 4096 at -c 16384 / 256 with LLAMA_SERVER_KV_ADMIT_RESERVE=256 / +n_predict
when the request caps it, a non-unified server defers nothing, SIGTERM with three pending
deferrals exits in 0.1 s (it never returned before), and a waiting task adds no CPU while
the server idles at 0 ticks after the drain. Gate: scripts/gate-kv-admit.sh (MANIFESTS.md
gate 46), whose --self-test runs every arm on the kill switch and rejects it on 12 checks.
Knobs: ENVIRONMENT.md 29 / DIAGNOSTIC.md 29. Record: WORKLOG.md 261126.001, INVARIANTS.md
r365, patches/README.md block 16.
…n fair share of the step, an interactive batch size (-ib) that applies only while a decoder waits, a shortest-remaining skew, the prefill/decode duty cycle, and the KV admission of a whole task
A batch is assembled once per step and cannot be joined mid-flight, so the policy that
decides WHICH prompt tokens enter that step also decides how long every other slot waits.
* LLAMA_SERVER_PREFILL_FAIR (a kill-switch, default on) gives the slots that still have
prompt tokens a max-min fair share of the window: every active prompt is admitted to
every step, and a slot that needs less returns the difference to the pool.
* LLAMA_SERVER_PREFILL_SKEW (default 1 = the plain share) weights the slot with the
shortest remaining prompt N:1 against the others; every other slot still advances.
* -ib / --interactive-batch-size (r306; env LLAMA_ARG_INTERACTIVE_BATCH_SIZE) caps the
prompt tokens admitted per step WHILE a decoder is waiting -- the interactive bound on
a decoding slot's inter-token latency and on a newcomer's wait for admission. -1 = one
n_ubatch (the default), 0 = no bound, N = explicit. A step with NOTHING decoding takes
the whole -b window instead (r306): with no decoder waiting the cap bounds nothing, and
below -ub a smaller step only re-pays the step's fixed cost for nothing. It was
LLAMA_SERVER_PREFILL_CHUNK, env-only and applied to EVERY step, which cost the field
shape half of its uncontended prefill (1454 vs 3036 t/s on the same 65536 tokens); that
env var is kept as the option's deprecated alias, with one WARN.
* LLAMA_SERVER_PREFILL_DUTY (default 16) leaves up to K-1 steps decode-only and puts the
prompt into one step per cycle, BOUNDED in time by the previous prefill step's own
duration, so a decoder gets a burst at the decode-only step time instead of one token
per slow step.
New at r365 (contributor PR ggml-org#370, briansp2020): the same policy now also decides whether a
TASK is admitted at all, because with a unified KV cache -- the delivery's DEFAULT server,
since -np -1 resolves to 4 slots and kv_unified = true -- every slot may use the whole
context, so "a slot is free" says nothing about whether the prompt fits. Two prompts whose
sum exceeded the context were launched together and the failure landed in DECODE, where
nothing can recover it: the retry loop can only clear IDLE slots (try_clear_idle_slots skips
a processing one), so it halved n_batch to 1 and aborted a running slot (measured on r364 at
-c 8192 -np 4 --kv-unified with two ~4.8k-token prompts: both HTTP 500 "Context size has
been exceeded", and with a speculator "speculative batch index N is not inside the current
sub-batch"; the same two prompts alone: both 200).
* server_slot_sched gains an injected `admit` oracle (std::function<bool(const
server_task &)>) and server_slot_plan a `why` (none / no_slot / no_capacity). select()
asks the oracle ONCE, before a candidate is chosen, so the predicate is a whole-request
go/no-go and the scheduler learns nothing about cells, reserves or margins; a refusal
travels the existing "plan.id_slot == -1 -> queue_tasks.defer()" path, so there is no
second defer site, pop_deferred_task() and /metrics' n_tasks_deferred cover it for free,
and a prompt pinned to an idle id_slot is refused by the same path.
* The server context owns the accounting (kv_admit_ok): each RUNNING slot is charged
max(its prompt, its task's tokens) plus a reply reserve. Idle slots are not charged (the
launch path parks the residents and then clears them for want of room) and neither is a
realtime slot waiting for audio (is_processing() but no batch work). With nothing
running a task is always admitted, so a prompt that fits the context alone is never
starved.
* LLAMA_SERVER_KV_ADMIT_RESERVE (default min(n_ctx / n_parallel, n_ctx_slot()) cells, the
per-sequence share a non-unified cache would give each slot) is the reply space charged
per request, capped by the request's own n_predict. LLAMA_SERVER_KV_ADMIT=0 restores
the pre-r365 server exactly. Inert without --kv-unified.
* A release wakes exactly one deferred task, so a task that is re-offered and refused
again would wait for an unrelated request: update_slots() re-offers one when NO slot is
processing -- the condition that makes the wake provably spin-free, since the popped
task is then always admitted.
* A task that was never launched is answered at termination: server_context::terminate()
drains the deferred queue (server_queue::take_deferred_tasks) and aborts the running
slots. Without it a SIGTERM with a deferred task pending never returned -- the exit
hung in HIP's teardown because server_http_context::join() waits for an HTTP worker
waiting on a result nothing produced. PRE-EXISTING (the stock "no free slot" deferral
escaped it because the release path launches the task and then aborts it).
Measured (r365, one R9700, 4B Qwen3.5 Q8_0, -c 8192 -np 4 --kv-unified): two copies of
prompts/prose-rdna-boosts.txt (5246 tokens, cache_prompt false) answer 500/500 on
LLAMA_SERVER_KV_ADMIT=0 and 200/200 with it, at 2.1 s then 4.0 s (serialised), and BOTH
answers are 1dfd5ced6f3e -- the same text the prompt gives when it runs alone, the deferred
one re-prefilling all 5246 tokens. A pair that fits is untouched (no deferral, text equal
to the kill-switch arm), four overlapping prompts drain in order, the reserve census reads
2048 at -c 8192 / 4096 at -c 16384 / 256 with LLAMA_SERVER_KV_ADMIT_RESERVE=256 / +n_predict
when the request caps it, a non-unified server defers nothing, SIGTERM with three pending
deferrals exits in 0.1 s (it never returned before), and a waiting task adds no CPU while
the server idles at 0 ticks after the drain. Gate: scripts/gate-kv-admit.sh (MANIFESTS.md
gate 46), whose --self-test runs every arm on the kill switch and rejects it on 12 checks.
Knobs: ENVIRONMENT.md 29 / DIAGNOSTIC.md 29. Record: WORKLOG.md 261126.001, INVARIANTS.md
r365, patches/README.md block 16.
This is pretty big change, but it has to happen in order to allow building more examples by reusing the code properly. It will also allow other programming languages to interface with the code.
ggml_quantize_q4_0()andggml_quantize_q4_1()fromutilstoggmlllama.handllama.cppthat implement most of the model handling that was previously inmain.cppllamalibraryllamalibrary, exposed through the APIPlease test that everything is working. I haven't tested the
perplexityparameter at all.Sorry if this conflicts with your ongoing work!