Files
lemonade/test/cpp/test_runtime_config_backend_validation.cpp

182 lines
7.3 KiB
C++

#include <lemon/backends/llamacpp/llamacpp.h>
#include <lemon/backends/thenoise/thenoise_server.h>
#include <lemon/backends/whispercpp/whispercpp.h>
#include <lemon/runtime_config.h>
#include <nlohmann/json.hpp>
#include <cstdio>
#include <filesystem>
#include <stdexcept>
#include <string>
using lemon::RuntimeConfig;
using nlohmann::json;
namespace {
int failures = 0;
// A config that never named a setting has to answer with its default. Reaching for a
// missing key through a const object reads past the end, and the model store reads
// both of these on every rebuild.
void expect_absent_key_answers_default(const std::string& key, bool answered) {
if (answered) {
std::printf("[FAIL] an absent %s did not answer its default\n", key.c_str());
++failures;
} else {
std::printf("[PASS] an absent %s answers its default\n", key.c_str());
}
}
void expect_accepted(const std::string& section, const std::string& key, const json& value) {
RuntimeConfig config(json::object());
try {
config.set({{section, {{key, value}}}});
std::printf("[PASS] %s.%s is accepted\n", section.c_str(), key.c_str());
} catch (const std::invalid_argument& error) {
std::printf("[FAIL] %s.%s was rejected: %s\n", section.c_str(), key.c_str(), error.what());
++failures;
}
}
void expect_rejected(const std::string& section, const std::string& key, const json& value,
const std::string& expected_message) {
RuntimeConfig config(json::object());
try {
config.set({{section, {{key, value}}}});
std::printf("[FAIL] %s.%s was accepted\n", section.c_str(), key.c_str());
++failures;
} catch (const std::invalid_argument& error) {
const std::string message = error.what();
if (message.find(expected_message) == std::string::npos) {
std::printf("[FAIL] %s.%s rejected with unexpected message: %s\n",
section.c_str(), key.c_str(), message.c_str());
++failures;
} else {
std::printf("[PASS] %s.%s is rejected\n", section.c_str(), key.c_str());
}
}
}
void expect_unknown_key(const std::string& section, const std::string& key, const json& value) {
expect_rejected(section, key, value, "Unknown key: '" + section + "." + key + "'");
}
void expect_args_variant(const lemon::BackendDescriptor& descriptor,
const std::string& variant, const std::string& args) {
const std::string key = variant + "_args";
const json defaults = descriptor.config_defaults();
if (!defaults.contains(key) || defaults.at(key) != "") {
std::printf("[FAIL] %s.%s missing from defaults\n", descriptor.recipe.c_str(), key.c_str());
++failures;
} else {
std::printf("[PASS] %s.%s in defaults\n", descriptor.recipe.c_str(), key.c_str());
}
RuntimeConfig config(json::object());
try {
config.set({{descriptor.effective_config_section(), {{key, args}}}});
const json resolved = config.recipe_options(variant);
const std::string option_key = descriptor.recipe + "_args";
if (resolved.value(option_key, std::string{}) == args) {
std::printf("[PASS] %s.%s resolves to %s\n",
descriptor.recipe.c_str(), key.c_str(), option_key.c_str());
} else {
std::printf("[FAIL] %s.%s does not resolve to %s\n",
descriptor.recipe.c_str(), key.c_str(), option_key.c_str());
++failures;
}
} catch (const std::exception& error) {
std::printf("[FAIL] %s.%s could not be configured: %s\n",
descriptor.recipe.c_str(), key.c_str(), error.what());
++failures;
}
}
} // namespace
int main() {
{
RuntimeConfig config(json::object());
expect_absent_key_answers_default("disable_model_filtering", config.disable_model_filtering());
expect_absent_key_answers_default("enable_dgpu_gtt", config.enable_dgpu_gtt());
}
try {
RuntimeConfig::validate_backend_choice("llamacpp", "system");
#ifdef __linux__
std::puts("[PASS] llamacpp system backend is accepted on Linux");
#else
std::puts("[FAIL] llamacpp system backend was accepted on an unsupported platform");
++failures;
#endif
} catch (const std::invalid_argument& error) {
#ifdef __linux__
std::printf("[FAIL] llamacpp system backend was rejected on Linux: %s\n", error.what());
++failures;
#else
std::puts("[PASS] llamacpp system backend is rejected on unsupported platforms");
#endif
}
expect_args_variant(lemon::backends::llamacpp::descriptor, "cuda", "--threads 4");
expect_args_variant(lemon::backends::whispercpp::descriptor, "vulkan", "--convert");
expect_args_variant(lemon::backends::whispercpp::descriptor, "rocm", "--convert");
// _bin / _args keys must name a backend variant the section actually has.
expect_unknown_key("flm", "flm_bin", "builtin");
expect_unknown_key("flm", "random_bin", "builtin");
expect_unknown_key("flm", "foo_args", "");
expect_unknown_key("llamacpp", "vulkan_bin_extra", "builtin");
expect_unknown_key("llamacpp", "vulkan_args_extra", "");
expect_unknown_key("llamacpp", "npu_bin", "builtin");
expect_unknown_key("whispercpp", "cuda_args", "");
expect_accepted("llamacpp", "vulkan_bin", "builtin");
expect_accepted("llamacpp", "cuda_bin", "b8664");
expect_accepted("llamacpp", "vulkan_args", "--no-mmap");
expect_accepted("llamacpp", "cuda_args", "--no-mmap");
expect_accepted("llamacpp", "args", "--no-mmap");
expect_accepted("flm", "npu_bin", "builtin");
expect_accepted("flm", "args", "");
expect_accepted("ryzenai", "server_bin", "latest");
expect_accepted("hrx", "hrx_bin", "builtin");
expect_accepted("thenoise", "rocm_bin", "builtin");
expect_accepted("thenoise", "cuda_bin", "builtin");
#if defined(__linux__) && defined(__x86_64__)
try {
const auto cuda = lemon::backends::TheNoiseServer::get_install_params("cuda", "test-tag");
if (cuda.repo == "lemonade-sdk/thenoise" &&
cuda.filename == "test-tag-cuda-x64.tar.gz") {
std::puts("[PASS] TheNoise CUDA uses the shared Linux release asset");
} else {
std::puts("[FAIL] TheNoise CUDA release asset name is incorrect");
++failures;
}
} catch (const std::exception& e) {
std::printf("[FAIL] TheNoise CUDA install params failed: %s\n", e.what());
++failures;
}
#else
try {
(void)lemon::backends::TheNoiseServer::get_install_params("cuda", "test-tag");
std::puts("[FAIL] TheNoise CUDA is accepted outside Linux x86_64");
++failures;
} catch (const std::runtime_error&) {
std::puts("[PASS] TheNoise CUDA is rejected outside Linux x86_64");
}
#endif
expect_rejected("llamacpp", "vulkan_bin", 1, "'llamacpp.vulkan_bin' must be a string");
expect_rejected("llamacpp", "vulkan_args", 1, "'llamacpp.vulkan_args' must be a string");
const std::string missing_bin =
(std::filesystem::temp_directory_path() / "lemonade-does-not-exist" / "llama-server")
.string();
expect_rejected("llamacpp", "vulkan_bin", missing_bin,
"'llamacpp.vulkan_bin' path does not exist");
return failures == 0 ? 0 : 1;
}