Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions common/arg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,21 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
params.cache_ram_mib = value;
}
).set_env("LLAMA_ARG_CACHE_RAM").set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_CLI}));
add_opt(common_arg(
{"--cache-disk"}, "DIR",
"a disk tier for the prompt cache: an entry evicted from --cache-ram is written here instead of dropped, "
"and read back when a prompt matches it (default: none)",
[](common_params & params, const std::string & value) {
params.cache_disk_dir = value;
}
).set_env("LLAMA_ARG_CACHE_DISK").set_examples({LLAMA_EXAMPLE_SERVER}));
add_opt(common_arg(
{"--cache-disk-mib"}, "N",
string_format("the prompt cache disk tier's size limit in MiB (default: %d, 0 - no limit)", params.cache_disk_mib),
[](common_params & params, int value) {
params.cache_disk_mib = value;
}
).set_env("LLAMA_ARG_CACHE_DISK_MIB").set_examples({LLAMA_EXAMPLE_SERVER}));
add_opt(common_arg(
{"-kvu", "--kv-unified"},
{"-no-kvu", "--no-kv-unified"},
Expand Down
2 changes: 2 additions & 0 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,8 @@ struct common_params {
int32_t n_ctx_checkpoints = 32; // max number of context checkpoints per slot
int32_t checkpoint_min_step = 8192; // minimum spacing between context checkpoints
int32_t cache_ram_mib = 8192; // -1 = no limit, 0 - disable, 1 = 1 MiB, etc.
std::string cache_disk_dir; // the prompt cache's disk tier, empty = none
int32_t cache_disk_mib = 0; // the disk tier's size limit in MiB, 0 = no limit

std::string hostname = "127.0.0.1";
std::string public_path = ""; // NOLINT
Expand Down
6 changes: 6 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
llama_add_compile_flags()

# Model-free scheduling validator: compile the same traversal used by server-context.
# Model-free: the disk tier's instance custody and bounded reads (llama.cpp#124).
add_executable(test-server-spill-dir test-server-spill-dir.cpp ../tools/server/server-spill-dir.cpp)
target_compile_features(test-server-spill-dir PRIVATE cxx_std_17)
add_test(NAME test-server-spill-dir COMMAND test-server-spill-dir)
set_tests_properties(test-server-spill-dir PROPERTIES LABELS "main")

add_executable(test-server-prefill test-server-prefill.cpp)
target_compile_features(test-server-prefill PRIVATE cxx_std_17)
add_test(NAME test-server-prefill COMMAND test-server-prefill)
Expand Down
83 changes: 83 additions & 0 deletions tests/test-server-spill-dir.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// what this catches: the prompt cache's disk tier deleting another engine's files (Codex's review of
// llama.cpp#124: two owners of one --cache-disk DIR deleted each other's spills), sweeping a live owner's
// directory, touching files that are not instance directories, and sizing a read from a corrupt file's own
// length claim (an out-of-memory). Model-free.
#include "../tools/server/server-spill-dir.h"

#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <memory>

namespace fs = std::filesystem;

static void require(bool condition, const char * message) {
if (!condition) {
std::cerr << "FAILED: " << message << '\n';
std::exit(1);
}
}

static void two_owners_never_touch_each_other() {
const fs::path base = fs::temp_directory_path() / ("spill-dir-test-" + std::to_string(std::rand()));
fs::remove_all(base);
fs::create_directories(base);
{ std::ofstream(base / "foreign.state") << "not ours"; } // a file in DIR that is no instance's
fs::create_directories(base / "notes"); // a directory that is no instance's
// an abandoned instance: its final marker exists and nobody holds its lock
fs::create_directories(base / "instance-1-1");
{ std::ofstream(base / "instance-1-1.owner") << ""; }
{ std::ofstream(base / "instance-1-1" / "0.state") << "stale"; }
// an instance directory with NO marker (a claim before its marker, or anything unproven): untouched,
// and a sweeper never creates a marker for it (Codex on #124)
fs::create_directories(base / "instance-2-2");
// a claim in progress: its .tmp marker exists (a live owner would hold it): never judged
{ std::ofstream(base / "instance-3-3.owner.tmp") << ""; }

std::error_code ec;
auto a = std::make_unique<server_spill_dir>();
require(a->claim(base, ec) == 1 && a->ok(), "the first owner claims its directory and sweeps the one abandoned instance");
{ std::ofstream(a->path() / "0.state") << "a's spill"; }
server_spill_dir b;
require(b.claim(base, ec) == 0 && b.ok(), "a second live owner sweeps nothing: the first one's marker is locked");
require(a->path() != b.path(), "each owner has its own directory");
require(fs::exists(a->path() / "0.state"), "the second owner never deletes the first owner's spill");
require(fs::exists(base / "foreign.state") && fs::exists(base / "notes"), "nothing that is not an instance is touched");
require(!fs::exists(base / "instance-1-1") && !fs::exists(base / "instance-1-1.owner"), "an abandoned instance and its marker are swept");
require(fs::exists(base / "instance-2-2") && !fs::exists(base / "instance-2-2.owner"),
"a directory without a marker is never swept, and no sweeper creates a marker for it");
require(fs::exists(base / "instance-3-3.owner.tmp"), "a claim in progress (.tmp) is never judged");
const fs::path a_dir = a->path();
a.reset();
require(!fs::exists(a_dir), "an owner's directory leaves with it");
require(b.ok() && fs::exists(b.path()), "and the other owner's stays");
fs::remove_all(base);
}

static void a_corrupt_length_is_refused_before_allocation() {
const fs::path p = fs::temp_directory_path() / ("spill-block-" + std::to_string(std::rand()));
{
FILE * f = std::fopen(p.string().c_str(), "wb");
require(server_spill_write_block(f, std::vector<uint8_t>(1000, 7)), "write a block");
const uint64_t lie = 1ull << 62; // a corrupt length: far beyond the file
std::fwrite(&lie, sizeof(lie), 1, f);
std::fclose(f);
}
FILE * f = std::fopen(p.string().c_str(), "rb");
uint64_t remaining = fs::file_size(p);
std::vector<uint8_t> block;
require(server_spill_read_block(f, block, remaining) && block.size() == 1000 && block[999] == 7, "a sound block reads back");
require(!server_spill_read_block(f, block, remaining), "a length beyond the file is refused");
require(block.size() == 1000, "and nothing was resized from its claim");
std::fclose(f);
fs::remove(p);
}

int main() {
two_owners_never_touch_each_other();
a_corrupt_length_is_refused_before_allocation();
std::cout << "test-server-spill-dir: ok\n";
return 0;
}
1 change: 1 addition & 0 deletions tools/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_library(${TARGET} STATIC
server-chat.cpp
server-chat.h
server-task.cpp
server-spill-dir.cpp
server-task.h
server-queue.cpp
server-queue.h
Expand Down
15 changes: 11 additions & 4 deletions tools/server/server-context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,9 @@ struct server_slot {

server_prompt prompt;

bool prompt_save(server_prompt_cache & prompt_cache) const {
// `next` / `next_lora`: the request about to load from the cache after this save, when there is one
bool prompt_save(server_prompt_cache & prompt_cache, const server_tokens * next = nullptr,
const lora_active_set * next_lora = nullptr) const {
if (prompt.tokens.size() == 0) {
return false;
}
Expand All @@ -330,7 +332,7 @@ struct server_slot {
SRV_TRC(" - saving prompt with length %d, total state size = %.3f MiB (draft: %.3f MiB)\n",
(int) prompt.tokens.size(), cur_size / (1024.0 * 1024.0), cur_size_dft / (1024.0 * 1024.0));

auto * cur = prompt_cache.alloc(prompt, lora_active(lora), cur_size_tgt, cur_size_dft);
auto * cur = prompt_cache.alloc(prompt, lora_active(lora), cur_size_tgt, cur_size_dft, next, next_lora);
if (cur == nullptr) {
return false;
}
Expand Down Expand Up @@ -1553,6 +1555,11 @@ struct server_context_impl {
SRV_TRC("%s", "use `--cache-ram 0` to disable the prompt cache\n");

prompt_cache = std::make_unique<server_prompt_cache>(params_base.cache_ram_mib, n_ctx);
if (!params_base.cache_disk_dir.empty() && !prompt_cache->set_disk(params_base.cache_disk_dir, params_base.cache_disk_mib, page_io.get(), &page_io_bytes,
server_test_faults::enabled ? server_test_faults::from_env().page_io_gate : std::string())) {
SRV_ERR("%s", "the prompt cache disk tier could not be set up, refusing to start without it\n");
return false;
}
} else {
SRV_TRC("%s", "prompt cache is disabled - use `--cache-ram N` to enable it\n");
}
Expand Down Expand Up @@ -1847,10 +1854,10 @@ struct server_context_impl {

const int64_t t_start = ggml_time_us();

ret->prompt_save(*prompt_cache);

const auto task_lora = task.params.lora.empty() ? params_base.lora_adapters
: construct_lora_list(task.params.lora);
const auto next_lora = lora_active(task_lora);
ret->prompt_save(*prompt_cache, &task.tokens, &next_lora);
if (!ret->prompt_load(*prompt_cache, task.tokens, task_lora)) {
ret->prompt_clear();
}
Expand Down
11 changes: 11 additions & 0 deletions tools/server/server-page-io.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ class server_page_admission {
return false;
}

// Admits `bytes` now or refuses: never parks (a caller that cannot wait, e.g. a prompt-cache spill
// that must free RAM now, drops instead of queueing behind the budget).
bool try_admit(uint64_t bytes) {
std::lock_guard<std::mutex> lock(m);
if (closed || !waiting.empty() || !fits(bytes)) {
return false;
}
in_flight += bytes;
return true;
}

void release(uint64_t bytes) {
std::vector<std::function<void()>> run;
{
Expand Down
63 changes: 63 additions & 0 deletions tools/server/server-spill-dir.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// The disk tier's only platform-specific lines (server-spill-dir.h): an exclusive file lock and the process
// id. Logic lives in the header; this file holds just the OS calls.
#include "server-spill-dir.h"

#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#else
#include <fcntl.h>
#include <sys/file.h>
#include <unistd.h>
#endif

bool server_owner_lock::try_take(const std::filesystem::path & path, bool create) {
release();
#if defined(_WIN32)
// no read/write sharing: a second open of a file another process holds open fails (delete sharing
// lets the owner rename its own marker)
HANDLE h = CreateFileW(path.wstring().c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_DELETE, nullptr,
create ? CREATE_NEW : OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (h == INVALID_HANDLE_VALUE) {
return false;
}
handle = (intptr_t) h;
return true;
#else
const int fd = ::open(path.c_str(), O_RDWR | O_CLOEXEC | (create ? (O_CREAT | O_EXCL) : 0), 0600);
if (fd < 0) {
return false;
}
if (::flock(fd, LOCK_EX | LOCK_NB) != 0) {
::close(fd);
return false;
}
handle = fd;
return true;
#endif
}

void server_owner_lock::release() {
if (handle == -1) {
return;
}
#if defined(_WIN32)
CloseHandle((HANDLE) handle);
#else
::close((int) handle); // closing releases the flock
#endif
handle = -1;
}

uint64_t server_process_id() {
#if defined(_WIN32)
return (uint64_t) GetCurrentProcessId();
#else
return (uint64_t) ::getpid();
#endif
}
Loading