server: run sampling in a threadpool #24914
Draft
ngxson wants to merge 4 commits into
Draft
Conversation
ngxson
commented
Jun 22, 2026
| slot.task->params.sampling.preserved_tokens.find(token) != slot.task->params.sampling.preserved_tokens.end(); | ||
| }; | ||
|
|
||
| std::vector<sampling_task> smpl_tasks; |
Collaborator
Author
There was a problem hiding this comment.
point for discussion: should sampling_task be a member of server_slot ?
ggerganov
reviewed
Jun 24, 2026
Comment on lines
+474
to
+475
| int sampling_n_threads = -1; // number of threads for sampling, used by server | ||
|
|
Member
There was a problem hiding this comment.
Can put it in common_params_sampling
Comment on lines
+3804
to
+3809
| threadpool.run_all<sampling_task>(smpl_tasks, [](sampling_task & task) { | ||
| if (task.slot) { | ||
| task.sampled_id = common_sampler_sample(task.slot->smpl.get(), | ||
| task.slot->ctx_tgt, task.tok_idx); | ||
| } | ||
| }); |
Member
There was a problem hiding this comment.
This is problematic because common_sampler_sample calls llama_context API that is not thread-safe.
Probably need a new overload for sampling from multiple samplers: common_sampler_sample(std::vector<common_sampler *> smpls, ...). And multi-thread just the llama_sampler_apply parts.
Collaborator
Author
There was a problem hiding this comment.
hmm ok I think that will requires having a common_threadpool that can be shared across multiple sampling calls, so that we can also reuse it for MTP sampling if needed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Ref discussion: #24843
server_threadpoolthat spawn n-1 threads (plus one main thread)--threads-samplingargument to select the number of threadsBenchmark: RTX 5060 Ti + 12th Gen Intel(R) Core(TM) i7-12700KF
10 concurrent requests --> around ~5% speed improvement
Requirements