mirror of
https://github.com/mudler/LocalAI.git
synced 2026-10-28 17:18:49 -04:00
* feat(llama-cpp): add slot prompt-cache save and restore API Add POST /v1/slots/save and POST /v1/slots/restore. They write one llama.cpp slot's prompt cache (KV state) to a file and load it back, so a client can warm a long system prompt in a few hundred milliseconds instead of re-running prefill. Refs #12685. The new SlotCache gRPC call posts the same slot save and restore tasks the llama-server /slots endpoint uses. It is off unless the model sets the new slot_save_path option, so existing models are not affected. The /slots monitoring endpoint is not needed here: the backend has no HTTP server. The file name comes from the caller, so it is checked twice, in Go and again in the backend. It must be one path component: no empty name, "/", "\", "..", NUL, control characters or absolute path. The resolved path, with symbolic links followed, must be a direct child of slot_save_path. The routes are admin only because they write files on the backend host. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(llama-cpp): answer 501 when a backend lacks slot cache A backend that does not implement the SlotCache rpc returns gRPC UNIMPLEMENTED. The API passed that text through, so the client saw "unknown method SlotCache". Map the status to a typed error in core/backend and return 501 with a fixed message instead. Every other error path is unchanged. Make the SlotCache comments in backend.proto backend-neutral: slot is the backend's state slot, filename must stay inside the save directory, and an empty model_identity means the backend does not check it. Only comments change, no field or name. Add tests that the identity sent is the configured model, and for the error mapping. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(llama-cpp): issue slot cache IDs on the server The slot save and restore API let the client pick the file name. Restore could load any file in slot_save_path, and the symlink check ran before llama.cpp opened the file. The backend now issues the name. A save returns a random 128-bit ID (32 lowercase hex characters, read from /dev/urandom) and never overwrites an existing file. Files live in a 0700 directory per model, named from the SHA-256 of the model identity, with a manifest.json that records each save. The manifest is written atomically and a corrupt one stops both save and restore. A restore takes the ID, finds it in the manifest, refuses an entry of another model, builds the path from the entry and checks with lstat that it is a plain file of the recorded size. The directory mode is the main defence against another local user, since llama.cpp still opens the path itself. Each model keeps at most 32 saves and the oldest is deleted first. A file over 8 GiB is deleted after the save, because llama.cpp does not report the size before it writes. Responses carry no host path. The HTTP bodies change: save takes {model, slot}, restore takes {model, slot, id}, and an unknown ID returns 404. In the proto, SlotCacheRequest.filename and SlotCacheResponse.filename become id. SlotCache no longer calls checkModelIdentity, which reads a field this request does not have. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(llama-cpp): copy slot_cache files in prepare.sh grpc-server.cpp includes slot_cache.h, but prepare.sh copies the helper headers into llama.cpp/tools/grpc-server/ one by one and skipped it. The llama-cpp, turboquant and bonsai builds share this step, so they all failed with "slot_cache.h: No such file or directory". Copy slot_cache.h and slot_cache_test.cpp, which CMakeLists.txt builds when LLAMA_GRPC_BUILD_TESTS is on. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * chore(deps): bump LocalAGI to pick up the scheduler start/stop fix LocalAGI's scheduler could hit a nil pointer in run when Start and Stop raced. That made the core/services/agentpool tests panic at random and turned tests-linux red. Move to mudler/LocalAGI@7aa0c8fea0, the merge of mudler/LocalAGI#519 ("make Start and Stop safe to call concurrently"). The new module version also adds a few indirect requirements to go.mod and go.sum. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> --------- Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Co-authored-by: Ettore Di Giacinto <mudler@localai.io> Co-authored-by: Ettore Di Giacinto <mudler@users.noreply.github.com>
149 lines
6.5 KiB
Bash
149 lines
6.5 KiB
Bash
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
|
|
## Patches
|
|
|
|
## Apply patches from the `patches` directory. Runs under set -e so a
|
|
## rejected patch aborts the build here, loudly, instead of surfacing later
|
|
## as a confusing compile error. A missing or empty patches dir is a no-op.
|
|
if [ -d "patches" ]; then
|
|
for patch in $(ls patches); do
|
|
echo "Applying patch $patch"
|
|
patch -d llama.cpp/ -p1 < patches/$patch
|
|
done
|
|
fi
|
|
|
|
## Apple RDMA link fixup.
|
|
|
|
## ggml-rpc hands Apple's librdma to the linker with
|
|
## target_link_options(ggml-rpc PRIVATE "LINKER:-weak_library,..."). Link options are not
|
|
## a usage requirement of a static library, so in our BUILD_SHARED_LIBS=OFF build the flag
|
|
## dies with libggml-rpc.a and every ibv_* symbol transport-apple.cpp reaches for comes out
|
|
## undefined when grpc-server and ggml-rpc-server link. Re-declare the same weak link as
|
|
## INTERFACE so it travels to whoever links the static library.
|
|
##
|
|
## Guarded on the marker so a second prepare.sh over the same checkout is a no-op, and on
|
|
## GGML_RPC_RDMA_APPLE so forks that branched before the Apple RDMA transport (turboquant,
|
|
## bonsai) are left alone.
|
|
RPC_CMAKE=llama.cpp/ggml/src/ggml-rpc/CMakeLists.txt
|
|
if [ -f "$RPC_CMAKE" ] && grep -q "GGML_RPC_RDMA_APPLE" "$RPC_CMAKE" && ! grep -q "LOCALAI_RDMA_IFACE" "$RPC_CMAKE"; then
|
|
echo "==> ggml-rpc carries the Apple RDMA transport, re-declaring its weak librdma link as INTERFACE"
|
|
cat >> "$RPC_CMAKE" <<'EOF'
|
|
|
|
# LOCALAI_RDMA_IFACE: added by backend/cpp/llama-cpp/prepare.sh
|
|
if (GGML_RPC_RDMA AND APPLE AND NOT BUILD_SHARED_LIBS)
|
|
target_link_options(ggml-rpc INTERFACE "LINKER:-weak_library,${RDMA_LIB}")
|
|
endif()
|
|
EOF
|
|
fi
|
|
|
|
for file in $(ls llama.cpp/tools/server/); do
|
|
cp -rfv llama.cpp/tools/server/$file llama.cpp/tools/grpc-server/
|
|
done
|
|
|
|
cp -r CMakeLists.txt llama.cpp/tools/grpc-server/
|
|
cp -r grpc-server.cpp llama.cpp/tools/grpc-server/
|
|
cp -r decision_compat.h llama.cpp/tools/grpc-server/
|
|
cp -r decision_images.h llama.cpp/tools/grpc-server/
|
|
# Model-load diagnostics (included by grpc-server.cpp) and their standalone
|
|
# regression test.
|
|
cp -r model_load_error.h llama.cpp/tools/grpc-server/
|
|
cp -r model_load_error_test.cpp llama.cpp/tools/grpc-server/
|
|
# Shared message-reconstruction helpers (included by grpc-server.cpp) and their
|
|
# unit test (compiled only when -DLLAMA_GRPC_BUILD_TESTS=ON).
|
|
cp -r message_content.h llama.cpp/tools/grpc-server/
|
|
cp -r message_content_test.cpp llama.cpp/tools/grpc-server/
|
|
# Generic passthrough parser staging and its standalone regression test.
|
|
cp -r passthrough_options.h llama.cpp/tools/grpc-server/
|
|
cp -r passthrough_options_test.cpp llama.cpp/tools/grpc-server/
|
|
# TTS request validation (included by grpc-server.cpp) and its standalone
|
|
# regression test.
|
|
cp -r tts_request_options.h llama.cpp/tools/grpc-server/
|
|
cp -r tts_request_options_test.cpp llama.cpp/tools/grpc-server/
|
|
# Thread-count default normalization and its standalone regression test.
|
|
cp -r thread_params.h llama.cpp/tools/grpc-server/
|
|
cp -r thread_params_test.cpp llama.cpp/tools/grpc-server/
|
|
# Slot-count resolution (option over LLAMACPP_PARALLEL) and its standalone
|
|
# regression test.
|
|
cp -r parallel_params.h llama.cpp/tools/grpc-server/
|
|
cp -r parallel_params_test.cpp llama.cpp/tools/grpc-server/
|
|
# moe_cache_mib option parsing and its standalone regression test.
|
|
cp -r moe_cache_option.h llama.cpp/tools/grpc-server/
|
|
cp -r moe_cache_option_test.cpp llama.cpp/tools/grpc-server/
|
|
# Parent-death watcher (included by grpc-server.cpp) and its standalone unit
|
|
# test (run via backend/cpp/run-unit-tests.sh; also buildable under ctest).
|
|
cp -r parent_watch.h llama.cpp/tools/grpc-server/
|
|
cp -r parent_watch_test.cpp llama.cpp/tools/grpc-server/
|
|
# Dead-stream tracker (included by grpc-server.cpp) and its standalone unit
|
|
# test (run via backend/cpp/run-unit-tests.sh; also buildable under ctest).
|
|
cp -r stream_peer.h llama.cpp/tools/grpc-server/
|
|
cp -r stream_peer_test.cpp llama.cpp/tools/grpc-server/
|
|
# Slot cache save/restore helpers (included by grpc-server.cpp) and their
|
|
# unit test (compiled only when -DLLAMA_GRPC_BUILD_TESTS=ON).
|
|
cp -r slot_cache.h llama.cpp/tools/grpc-server/
|
|
cp -r slot_cache_test.cpp llama.cpp/tools/grpc-server/
|
|
cp -rfv llama.cpp/vendor/nlohmann/json.hpp llama.cpp/tools/grpc-server/
|
|
cp -rfv llama.cpp/vendor/cpp-httplib/httplib.h llama.cpp/tools/grpc-server/
|
|
|
|
## Fork-skew probe. Upstream folded common_params::use_mmap / use_mlock /
|
|
## use_direct_io into a single `load_mode` enum (ggml-org/llama.cpp#20834).
|
|
## turboquant and bonsai compile this very same grpc-server.cpp against forks
|
|
## that branched before that change, so the field set is decided from the
|
|
## checkout in front of us rather than from a per-fork build flag: the flavor
|
|
## targets disagree on whether they forward CMAKE_ARGS or EXTRA_CMAKE_ARGS, and
|
|
## probing heals itself the moment a fork rebases past the refactor.
|
|
if grep -q "LLAMA_LOAD_MODE_MMAP" llama.cpp/include/llama.h; then
|
|
echo "==> llama.cpp carries the load-mode enum, using common_params::load_mode"
|
|
LEGACY_LOAD_MODE=0
|
|
else
|
|
echo "==> llama.cpp predates the load-mode enum, using the legacy mmap/mlock/direct-io booleans"
|
|
LEGACY_LOAD_MODE=1
|
|
fi
|
|
if grep -q "server_metrics metrics;" llama.cpp/tools/server/server-task.h; then
|
|
HAS_SERVER_METRICS=1
|
|
else
|
|
HAS_SERVER_METRICS=0
|
|
fi
|
|
if grep -q "mtmd_helper_init_opt" llama.cpp/tools/mtmd/mtmd-helper.h; then
|
|
HAS_MTMD_INIT_OPT=1
|
|
else
|
|
HAS_MTMD_INIT_OPT=0
|
|
fi
|
|
if grep -q "llm_add_n_cpu_ffn_overrides" llama.cpp/common/common.h; then
|
|
HAS_N_CPU_FFN_HELPER=1
|
|
else
|
|
HAS_N_CPU_FFN_HELPER=0
|
|
fi
|
|
# MoE expert GPU cache (--moe-cache-mib). Forks that branched earlier lack the field.
|
|
if grep -q "moe_cache_size" llama.cpp/common/common.h; then
|
|
HAS_MOE_CACHE=1
|
|
else
|
|
HAS_MOE_CACHE=0
|
|
fi
|
|
# Newer servers return template state separately from the request JSON.
|
|
if grep -q "common_chat_session & out_session" llama.cpp/tools/server/server-common.h; then
|
|
HAS_CHAT_SESSION=1
|
|
else
|
|
HAS_CHAT_SESSION=0
|
|
fi
|
|
cat > llama.cpp/tools/grpc-server/llama_compat.h <<EOF
|
|
// Generated by backend/cpp/llama-cpp/prepare.sh. Do not edit.
|
|
#pragma once
|
|
#define LOCALAI_LEGACY_LOAD_MODE ${LEGACY_LOAD_MODE}
|
|
#define LOCALAI_HAS_CHAT_SESSION ${HAS_CHAT_SESSION}
|
|
#define LOCALAI_HAS_SERVER_METRICS ${HAS_SERVER_METRICS}
|
|
#define LOCALAI_HAS_MTMD_INIT_OPT ${HAS_MTMD_INIT_OPT}
|
|
#define LOCALAI_HAS_N_CPU_FFN_HELPER ${HAS_N_CPU_FFN_HELPER}
|
|
#define LOCALAI_HAS_MOE_CACHE ${HAS_MOE_CACHE}
|
|
EOF
|
|
|
|
set +e
|
|
if grep -q "grpc-server" llama.cpp/tools/CMakeLists.txt; then
|
|
echo "grpc-server already added"
|
|
else
|
|
echo "add_subdirectory(grpc-server)" >> llama.cpp/tools/CMakeLists.txt
|
|
fi
|
|
set -e
|