From e419c1acc34e1612e682d8090b2168f09fddf53a Mon Sep 17 00:00:00 2001 From: baKon Date: Fri, 16 Oct 2026 09:26:40 -0500 Subject: [PATCH] kld: --kld-lora NAME scores a runtime LoRA adapter against a reference --- core/config.cpp | 3 +++ core/engine_kld.cpp | 16 ++++++++++++++++ core/rad_core.h | 1 + tests/config_test.cpp | 1 + 4 files changed, 21 insertions(+) diff --git a/core/config.cpp b/core/config.cpp index dab84a19..ce7fb18f 100644 --- a/core/config.cpp +++ b/core/config.cpp @@ -235,6 +235,7 @@ static const Flag g_flags[] = { " agreement, over the positions and tokens DIR was recorded at" }, { "--kld-corpus", "FILE", "JSONL, one {\"prompt\", \"score_from\"?, \"source\"?} a line; --kld-record only" }, { "--kld-out", "FILE", "the --kld-ref report as JSON, and every position's numbers in FILE.rows" }, + { "--kld-lora", "NAME", "run --kld-record / --kld-ref through the --lora adapter NAME" }, { "--debug-graph", nullptr, "print the entire declared graph and exit-quality detail" }, { "--debug-selection", nullptr, "print the selection table" }, @@ -780,6 +781,7 @@ int config_parse(int argc, char** argv, Config* c) { else if (S("--kld-ref")) { if (!(v = need(i))) return RAD_E_INVAL; c->kld_ref = v; } else if (S("--kld-corpus")) { if (!(v = need(i))) return RAD_E_INVAL; c->kld_corpus = v; } else if (S("--kld-out")) { if (!(v = need(i))) return RAD_E_INVAL; c->kld_out = v; } + else if (S("--kld-lora")) { if (!(v = need(i))) return RAD_E_INVAL; c->kld_lora = v; } else if (S("--debug-graph")) { c->debug_graph = true; c->debug_selection = true; } else if (S("--debug-selection")) c->debug_selection = true; else if (S("--debug-placement")) c->debug_placement = true; @@ -1033,6 +1035,7 @@ const ShowRow kShow[] = { { "--kld-ref", [](const Config& c) { return or_none(c.kld_ref, "none"); } }, { "--kld-corpus", [](const Config& c) { return or_none(c.kld_corpus, "none"); } }, { "--kld-out", [](const Config& c) { return or_none(c.kld_out, "none"); } }, + { "--kld-lora", [](const Config& c) { return or_none(c.kld_lora, "none (the base)"); } }, { "--debug-graph", [](const Config& c) { return onoff(c.debug_graph); } }, { "--debug-selection", [](const Config& c) { return onoff(c.debug_selection); } }, { "--debug-placement", [](const Config& c) { return onoff(c.debug_placement); } }, diff --git a/core/engine_kld.cpp b/core/engine_kld.cpp index 79bdb659..5d6b5cfc 100644 --- a/core/engine_kld.cpp +++ b/core/engine_kld.cpp @@ -97,6 +97,20 @@ int Engine::run_kld() { if (too_long) return RAD_E_FULL; } + /* --kld-lora: every document runs through one runtime LoRA adapter, held for the whole run -- + * how an adapter is scored against a reference recorded from its merged or HF counterpart. */ + int32_t lora_slot = -1; + uint64_t lora_uid = 0; + std::shared_ptr lora_hold; + if (!cfg_.kld_lora.empty() && + lora_acquire(cfg_.kld_lora, &lora_slot, &lora_uid, &lora_hold) != RAD_OK) { + RAD_ERR("kld: --kld-lora '%s' is not a loaded adapter (--lora NAME=PATH)", cfg_.kld_lora.c_str()); + return RAD_E_NOTFOUND; + } + if (lora_slot >= 0) + RAD_INFO("kld: scoring through the runtime LoRA adapter '%s' (slot %d)", cfg_.kld_lora.c_str(), + lora_slot); + /* Submit every document under one hold of the queue, so batching depends only on the corpus * and the flags and a rerun batches identically. */ std::vector> sinks(n); @@ -110,6 +124,8 @@ int Engine::run_kld() { r->ignore_eos = true; r->sp.temp = 0.0f; r->score = true; + r->lora_slot = lora_slot; + r->lora_uid = lora_uid; r->sink = sinks[d].get(); sinks[d]->id = r->id; reqs[d] = std::move(r); diff --git a/core/rad_core.h b/core/rad_core.h index d3f09127..00827170 100644 --- a/core/rad_core.h +++ b/core/rad_core.h @@ -816,6 +816,7 @@ struct Config { * of the two. */ std::string kld_record; /* --kld-record DIR */ std::string kld_ref; /* --kld-ref DIR */ + std::string kld_lora; /* --kld-lora NAME: score through this --lora adapter */ std::string kld_corpus; /* --kld-corpus FILE, read by --kld-record only */ std::string kld_out; /* --kld-out FILE: the report as JSON, rows beside it */ bool kld() const { return !kld_record.empty() || !kld_ref.empty(); } diff --git a/tests/config_test.cpp b/tests/config_test.cpp index 485aec49..74371e62 100644 --- a/tests/config_test.cpp +++ b/tests/config_test.cpp @@ -412,6 +412,7 @@ const std::map>& legal_values() { { "--kld-out", { "/o.json", "--kld-ref", "/r" } }, { "--lora", { "a=/adapters/a" } }, { "--max-loras", { "2" } }, { "--max-lora-rank", { "32" } }, { "--lora-target-modules", { "q_proj,v_proj" } }, + { "--kld-lora", { "a", "--lora", "a=/adapters/a", "--kld-ref", "/r" } }, }; return v; }