Skip to content

Commit 1420ad0

Browse files
fix(llama-cpp): normalize batch threads
The updated llama.cpp creates its batch threadpool during model initialization, before the context-level fallback can replace the -1 sentinel. Resolve that sentinel from the inference thread count so model loading does not overflow the threadpool allocation.\n\nAssisted-by: Codex:gpt-5.4
1 parent ad247ec commit 1420ad0

5 files changed

Lines changed: 41 additions & 0 deletions

File tree

backend/cpp/llama-cpp/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,9 @@ if(LLAMA_GRPC_BUILD_TESTS)
120120
target_include_directories(tts_request_options_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
121121
target_compile_features(tts_request_options_test PRIVATE cxx_std_17)
122122
add_test(NAME tts_request_options_test COMMAND tts_request_options_test)
123+
124+
add_executable(thread_params_test thread_params_test.cpp thread_params.h)
125+
target_include_directories(thread_params_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
126+
target_compile_features(thread_params_test PRIVATE cxx_std_17)
127+
add_test(NAME thread_params_test COMMAND thread_params_test)
123128
endif()

backend/cpp/llama-cpp/grpc-server.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "arg.h"
5454
#include "chat-auto-parser.h"
5555
#include "llama_compat.h" // fork-skew switches, generated by prepare.sh
56+
#include "thread_params.h"
5657
#include "message_content.h"
5758
#include "passthrough_options.h"
5859
#include "tts_request_options.h"
@@ -1412,6 +1413,12 @@ static void params_parse(server_context& /*ctx_server*/, const backend::ModelOpt
14121413
passthrough_draft_gpu_layers);
14131414
}
14141415

1416+
// The library initializer now creates both threadpools before the server
1417+
// can apply llama_context's fallback for the -1 batch-thread sentinel.
1418+
params.cpuparams_batch.n_threads = llama_grpc::resolve_batch_threads(
1419+
params.cpuparams_batch.n_threads,
1420+
params.cpuparams.n_threads);
1421+
14151422
#ifndef LOCALAI_LLAMA_CPP_NO_SCORE_TASK
14161423
// Score-task suffix forking: reserve seq ids (and recurrent-state cells)
14171424
// beyond the slots so one scoring call decodes all candidate tails in a

backend/cpp/llama-cpp/prepare.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ cp -r passthrough_options_test.cpp llama.cpp/tools/grpc-server/
3232
# regression test.
3333
cp -r tts_request_options.h llama.cpp/tools/grpc-server/
3434
cp -r tts_request_options_test.cpp llama.cpp/tools/grpc-server/
35+
# Thread-count default normalization and its standalone regression test.
36+
cp -r thread_params.h llama.cpp/tools/grpc-server/
37+
cp -r thread_params_test.cpp llama.cpp/tools/grpc-server/
3538
# Parent-death watcher (included by grpc-server.cpp) and its standalone unit
3639
# test (run via backend/cpp/run-unit-tests.sh; also buildable under ctest).
3740
cp -r parent_watch.h llama.cpp/tools/grpc-server/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
5+
namespace llama_grpc {
6+
7+
inline int32_t resolve_batch_threads(int32_t batch_threads, int32_t inference_threads) {
8+
return batch_threads < 0 ? inference_threads : batch_threads;
9+
}
10+
11+
} // namespace llama_grpc
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "thread_params.h"
2+
3+
#include <cstdio>
4+
5+
int main() {
6+
if (llama_grpc::resolve_batch_threads(-1, 4) != 4) {
7+
std::fprintf(stderr, "default batch threads did not inherit inference threads\n");
8+
return 1;
9+
}
10+
if (llama_grpc::resolve_batch_threads(2, 4) != 2) {
11+
std::fprintf(stderr, "explicit batch threads were overwritten\n");
12+
return 1;
13+
}
14+
return 0;
15+
}

0 commit comments

Comments
 (0)