-
Notifications
You must be signed in to change notification settings - Fork 13.5k
server : support unified cache across slots #16736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
src/llama-context.cpp
Outdated
|
|
||
| uint32_t llama_context::n_ctx_per_seq() const { | ||
| return cparams.n_ctx / cparams.n_seq_max; | ||
| return cparams.kv_unified ? cparams.n_ctx : cparams.n_ctx / cparams.n_seq_max; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this value be capped when using unified cache to avoid exceeding the model context length? I think it could be set to min(n_ctx_train, n_ctx), or add a parameter to allow the user to change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we can cap it to n_ctx_train. The only use case for n_ctx > n_ctx_train that comes to mind is self-extend, but lately this technique seems less relevant.
We can also cap it for the non-unified case?
| return cparams.kv_unified ? cparams.n_ctx : cparams.n_ctx / cparams.n_seq_max; | |
| return stdd:min(n_ctx_train, cparams.kv_unified ? cparams.n_ctx : cparams.n_ctx / cparams.n_seq_max); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also cap it for the non-unified case?
What would happen to the leftover slots? I may be misunderstanding the way split cache works, but my assumption would be that these slots would never be used, and it would be wasted memory. So if that's capped, it should be done at context creation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, we should do the capping at context creation in the llama_context constructor. Currently we have some additional logic for this in llama-model:
Lines 19708 to 19724 in 7863fcc
| const auto padding = llama_kv_cache::get_padding(cparams); | |
| uint32_t n_ctx_per_stream = cparams.n_ctx; | |
| if (!cparams.kv_unified) { | |
| n_ctx_per_stream = (cparams.n_ctx + cparams.n_seq_max - 1)/cparams.n_seq_max; | |
| n_ctx_per_stream = GGML_PAD(n_ctx_per_stream, padding); | |
| cparams.n_ctx = n_ctx_per_stream*cparams.n_seq_max; | |
| } else { | |
| n_ctx_per_stream = GGML_PAD(n_ctx_per_stream, padding); | |
| cparams.n_ctx = n_ctx_per_stream; | |
| } | |
| LLAMA_LOG_DEBUG("%s: n_ctx = %u (padded)\n", __func__, cparams.n_ctx); | |
Since we no longer need the padding logic (as of #16148 and related) we should simplify this.
I'll push a separate PR for this and then will come back to polishing this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now rebased on top of the changes in #16812. The result is that we determine the KV cache size during context creation and there should be no leftover KV cells.
Note that since we now cap the context size to the training context size, the user code is recommended to query llama_n_ctx and llama_n_ctx_seq after creating the llama_context in order to obtain the actual context size. I'll add comments in llama.h to reflect this.
Will try to clean-up this PR next and will open it for review when ready.
55bb9db to
6369fe0
Compare
6369fe0 to
ac261be
Compare
| if (cparams.n_ctx_seq > hparams.n_ctx_train) { | ||
| LLAMA_LOG_WARN("%s: n_ctx_seq (%u) > n_ctx_train (%u) -- possible training context overflow\n", | ||
| __func__, cparams.n_ctx_seq, hparams.n_ctx_train); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This branch should not be reached due to the capping above on line 117. But keeping it in case the capping logic gets changed in the future.
ac261be to
0ba88d3
Compare
0ba88d3 to
4e9e319
Compare
|
Ready for review. I've marked some TODOs for follow-up PRs since I think the current implementation is quite basic and at the same time gets us 90% on the way to the ideal logic. Will improve the rest of the cases from |
ref #4130 (reply in thread)
Current logic in this PR (subject to change):
-kvu, share the entire context-c Namong all parallel slots of the server-np N-np Nargument is still utilized to control the max number of parallel jobs, but it is no longer used to change the per-slot contextllama_contextnow caps then_ctx_seqto a maximum ofhparams.n_ctx_trainExample:
TODO:
Think about instead of purging, to move the slot into host-memory cache. Not sure that this is really needed thanks to the existing logic from server : host-memory prompt caching #16391Future improvements: