From 63e6d0139453c27b9620e2561b252fbb397af260 Mon Sep 17 00:00:00 2001 From: baKon Date: Fri, 16 Oct 2026 09:49:32 -0500 Subject: [PATCH] kld: --kld-lora-sources PREFIX, an adapter for some documents and the base for the rest, in the same steps --- core/config.cpp | 4 ++++ core/engine_kld.cpp | 7 +++++-- core/kld.h | 1 + core/rad_core.h | 1 + tests/config_test.cpp | 2 ++ 5 files changed, 13 insertions(+), 2 deletions(-) diff --git a/core/config.cpp b/core/config.cpp index ce7fb18f..475f94b8 100644 --- a/core/config.cpp +++ b/core/config.cpp @@ -236,6 +236,8 @@ static const Flag g_flags[] = { { "--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" }, + { "--kld-lora-sources", "PREFIX", "--kld-lora only for documents whose source starts with PREFIX; the\n" + " rest run as the base, in the same steps (a mixed batch)" }, { "--debug-graph", nullptr, "print the entire declared graph and exit-quality detail" }, { "--debug-selection", nullptr, "print the selection table" }, @@ -782,6 +784,7 @@ int config_parse(int argc, char** argv, Config* c) { 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("--kld-lora-sources")) { if (!(v = need(i))) return RAD_E_INVAL; c->kld_lora_sources = 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; @@ -1036,6 +1039,7 @@ const ShowRow kShow[] = { { "--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)"); } }, + { "--kld-lora-sources", [](const Config& c) { return or_none(c.kld_lora_sources, "every document"); } }, { "--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 5d6b5cfc..d402fff3 100644 --- a/core/engine_kld.cpp +++ b/core/engine_kld.cpp @@ -124,8 +124,11 @@ 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; + /* --kld-lora-sources: the other documents run as the base, in the same steps. */ + const bool via = cfg_.kld_lora_sources.empty() || + kld_->source(d).rfind(cfg_.kld_lora_sources, 0) == 0; + r->lora_slot = via ? lora_slot : -1; + r->lora_uid = via ? lora_uid : 0; r->sink = sinks[d].get(); sinks[d]->id = r->id; reqs[d] = std::move(r); diff --git a/core/kld.h b/core/kld.h index 63ad3c02..cd2aafdf 100644 --- a/core/kld.h +++ b/core/kld.h @@ -78,6 +78,7 @@ public: const std::string& model); size_t n_docs() const { return docs_.size(); } + const std::string& source(size_t d) const { return docs_[d].source; } /* Document d's tokens, and the request id the engine submits it under: d + 1. */ std::vector tokens(size_t d) const; int64_t n_rows() const { return n_rows_; } diff --git a/core/rad_core.h b/core/rad_core.h index 00827170..e962d5b6 100644 --- a/core/rad_core.h +++ b/core/rad_core.h @@ -817,6 +817,7 @@ struct Config { 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_lora_sources; /* --kld-lora-sources PREFIX: only documents whose source starts so */ 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 74371e62..dd969c0d 100644 --- a/tests/config_test.cpp +++ b/tests/config_test.cpp @@ -413,6 +413,8 @@ const std::map>& legal_values() { { "--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" } }, + { "--kld-lora-sources", { "doc/", "--kld-lora", "a", "--lora", "a=/adapters/a", + "--kld-ref", "/r" } }, }; return v; }