This commit is contained in:
Owen Qwen
2026-07-29 17:17:25 -05:00
parent fdd9fcf85e
commit a673afad85
17 changed files with 1071 additions and 11 deletions
+2
View File
@@ -259,6 +259,8 @@ set_tests_properties(test-thread-safety PROPERTIES FIXTURES_REQUIRED test-downlo
llama_build_and_test(test-arg-parser.cpp)
llama_build_and_test(test-longhaul.cpp)
llama_build_and_test(test-token-cache.cpp)
target_include_directories(test-token-cache PRIVATE ${PROJECT_SOURCE_DIR}/src)
if (NOT LLAMA_SANITIZE_ADDRESS AND NOT GGML_SCHED_NO_REALLOC)
# TODO: repair known memory leaks
+8
View File
@@ -160,6 +160,14 @@ static void test(void) {
assert(params.load_mode == LLAMA_LOAD_MODE_LONGHAUL);
assert(params.longhaul_cache_bytes == 2ULL * 1024 * 1024 * 1024);
argv = {"binary_name", "--token-cache-size", "256", "--token-cache-dir", "/tmp/llama-token-cache-test.sqlite3"};
assert(true == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
assert(params.token_cache_size_mib == 256);
assert(params.token_cache_path == "/tmp/llama-token-cache-test.sqlite3");
argv = {"binary_name", "--token-cache-size", "-1"};
assert(false == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
// multi-value args (CSV)
argv = {"binary_name", "--lora", "file1.gguf,\"file2,2.gguf\",\"file3\"\"3\"\".gguf\",file4\".gguf"};
assert(true == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
+69
View File
@@ -0,0 +1,69 @@
#include "llama.h"
#include "../src/llama-token-cache.h"
#include <chrono>
#include <filesystem>
#include <string>
#include <vector>
#undef NDEBUG
#include <cassert>
int main() {
namespace fs = std::filesystem;
const auto defaults = llama_token_cache_default_params();
assert(defaults.capacity_bytes == 5ull * 1024 * 1024 * 1024);
const fs::path root = fs::temp_directory_path() /
("llama-token-cache-test-" + std::to_string(
std::chrono::high_resolution_clock::now().time_since_epoch().count()));
const fs::path db = root / "cache.sqlite3";
llama_token_cache_params params = {
nullptr,
128,
};
// configure copies the path; it must not retain the temporary c_str().
const std::string db_string = db.string();
params.path = db_string.c_str();
assert(llama_token_cache_configure(params));
assert(llama_token_cache_clear());
assert(llama_token_cache_hash("abc") ==
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
const std::vector<uint8_t> first(80, 0x11);
const std::vector<uint8_t> second(80, 0x22);
std::vector<uint8_t> loaded;
llama_token_cache_store(LLAMA_TOKEN_CACHE_KIND_TOKENIZER, "first", first, true);
assert(llama_token_cache_lookup(LLAMA_TOKEN_CACHE_KIND_TOKENIZER, "first", loaded));
assert(loaded == first);
llama_token_cache_store(LLAMA_TOKEN_CACHE_KIND_TEXT, "second", second, true);
loaded.clear();
assert(!llama_token_cache_lookup(LLAMA_TOKEN_CACHE_KIND_TOKENIZER, "first", loaded));
assert(llama_token_cache_lookup(LLAMA_TOKEN_CACHE_KIND_TEXT, "second", loaded));
assert(loaded == second);
const std::vector<uint8_t> async_payload(16, 0x33);
llama_token_cache_store(LLAMA_TOKEN_CACHE_KIND_TEXT, "async", async_payload);
assert(llama_token_cache_flush());
loaded.clear();
assert(llama_token_cache_lookup(LLAMA_TOKEN_CACHE_KIND_TEXT, "async", loaded));
assert(loaded == async_payload);
const auto stats = llama_token_cache_get_stats();
assert(stats.entries == 2);
assert(stats.bytes_used == second.size() + async_payload.size());
assert(stats.evictions >= 1);
assert(stats.writes >= 3);
assert(llama_token_cache_clear());
assert(llama_token_cache_get_stats().entries == 0);
std::error_code ec;
fs::remove_all(root, ec);
return 0;
}