lora: an unload of an adapter in use drains it instead of answering 409

POST /v1/unload_lora_adapter for an adapter requests still hold marks it draining and answers
202 {"action": "draining", "lora_name", "in_flight": N}: from then on a request naming it gets the
404 an unknown model gets and /v1/models leaves it out, the N requests finish normally, and the
last hold to go frees the slot, which then cools for three step-loop rounds like any freed slot.
An idle adapter still unloads at once with 200; a second unload while it drains answers 202 with
the count as it is then, and 404 once it is gone; a load of the same name while it drains answers
409 (is being unloaded).

The slots' bookkeeping moves out of Engine into lora::SlotTable (core/lora/slots.h): reserve,
publish, acquire with its hold, unload, names. It has its own lock, so a request's hold and an
unload no longer wait behind a load's device copy; Engine::lora_mu_ now only serialises loads,
which is what keeps a reserved slot from being handed out twice. lora_test drives the table
through both paths, and server_test drives the routes over a real table with a streamed request
held open: 200 idle, 202 draining twice, 404 for a new request and on /v1/models/:id, 409 for a
load while draining, the slot freed when the stream ends, 503 while it cools, 200 after.
This commit is contained in:
baKon committed 2026-10-16 20:52:09 -05:00
1 parent 582cda0a53
commit 91127395ee
10 files changed
+495 -93

No files matched your search

+89 -1
View File
@@ -1,4 +1,5 @@
/* lora_test.cpp -- core/lora: sites from the declared Program, and a PEFT adapter packed per rank.
/* lora_test.cpp -- core/lora: sites from the declared Program, a PEFT adapter packed per rank, and
* the adapter slots' bookkeeping (slots.h).
*
* HOST ONLY, AND IT NEVER REACHES THE DEVICE LAYER. No kernel plugin is loaded: a Registry that
* loads one brackets the dlopen with the device backend's code mark, which in a HIP build starts the
@@ -18,6 +19,7 @@
#include "build/rad_build.h"
#include "lora/lora.h"
#include "lora/slots.h"
#include "plugin/registry.h"
#include "rad_plugin.h"
@@ -30,6 +32,7 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <atomic>
#include <filesystem>
#include <fstream>
#include <functional>
@@ -1215,6 +1218,91 @@ TEST(adapter_info_and_scales) {
CHECK(ri.content_hash != info.content_hash);
}
/* ================================================================== the slot table
* lora/slots.h: which adapter a slot holds, the holds requests take on it, and draining. */
/* An unload with no request on the adapter frees its slot at once, and the slot is written again
* only after the step loop has gone round kSlotCoolRounds times. */
TEST(an_idle_adapter_unloads_at_once_and_its_slot_cools) {
std::atomic<uint64_t> epoch{10};
lora::SlotTable t;
t.reset(1, &epoch);
int slot = -1, n = -1;
bool cooling = true;
std::string err;
CHECK_OK(t.reserve("a", &slot, &cooling, &err));
CHECK_EQ(slot, 0);
CHECK(!cooling);
const uint64_t u1 = t.publish(slot, "a", "/a");
CHECK(u1 != 0);
CHECK_EQ(t.reserve("a", &slot, &cooling, &err), RAD_E_INVAL); /* already loaded */
CHECK_OK(t.unload("a", &n, &err));
CHECK_EQ(n, 0);
CHECK(t.names().empty());
CHECK_EQ(t.unload("a", &n, &err), RAD_E_NOTFOUND);
CHECK_EQ(t.reserve("b", &slot, &cooling, &err), RAD_E_FULL);
CHECK(cooling);
epoch += lora::kSlotCoolRounds;
CHECK_OK(t.reserve("b", &slot, &cooling, &err));
CHECK_EQ(slot, 0);
const uint64_t u2 = t.publish(slot, "b", "/b");
CHECK(u2 != 0 && u2 != u1); /* a fresh uid: its prefix-cache blocks start cold */
}
/* An unload of an adapter requests hold drains it: new requests cannot name it, the ones holding
* it keep it, a second unload reports the count as it is then, a load of the same name is refused,
* and the last hold to go frees the slot -- which then cools like any freed slot. A hold copied
* into every choice of one request counts once. */
TEST(an_adapter_in_use_drains_and_its_last_request_frees_it) {
std::atomic<uint64_t> epoch{0};
lora::SlotTable t;
t.reset(1, &epoch);
int slot = -1, n = -1;
bool cooling = false;
std::string err;
CHECK_OK(t.reserve("a", &slot, &cooling, &err));
const uint64_t u = t.publish(slot, "a", "/a");
epoch += 10;
int32_t s1 = -1, s2 = -1;
uint64_t v1 = 0, v2 = 0;
std::shared_ptr<void> h1, h2;
CHECK_OK(t.acquire("a", &s1, &v1, &h1));
CHECK_OK(t.acquire("a", &s2, &v2, &h2));
CHECK_EQ(s1, 0);
CHECK_EQ(v1, u);
std::shared_ptr<void> h2_choice = h2; /* a second choice of the same request */
CHECK_EQ(t.in_flight("a"), 2);
CHECK_OK(t.unload("a", &n, &err));
CHECK_EQ(n, 2);
CHECK(t.names().empty());
std::shared_ptr<void> h3;
int32_t s3 = -1;
uint64_t v3 = 0;
CHECK_EQ(t.acquire("a", &s3, &v3, &h3), RAD_E_NOTFOUND); /* as for a name never loaded */
CHECK(!h3);
CHECK_EQ(t.reserve("a", &slot, &cooling, &err), RAD_E_STATE);
CHECK(has(err, "being unloaded"));
CHECK_OK(t.unload("a", &n, &err)); /* asked again: the same */
CHECK_EQ(n, 2);
h1.reset();
CHECK_OK(t.unload("a", &n, &err));
CHECK_EQ(n, 1);
h2.reset();
CHECK_EQ(t.in_flight("a"), 1); /* the other choice still holds */
h2_choice.reset();
CHECK_EQ(t.in_flight("a"), 0);
CHECK_EQ(t.unload("a", &n, &err), RAD_E_NOTFOUND); /* gone, by itself */
CHECK_EQ(t.reserve("a", &slot, &cooling, &err), RAD_E_FULL);
CHECK(cooling);
epoch += lora::kSlotCoolRounds;
CHECK_OK(t.reserve("a", &slot, &cooling, &err));
CHECK(t.publish(slot, "a", "/a2") != u);
}
TEST(zz_remove_the_temporary_adapters) {
if (!g_tmp.empty()) {
std::error_code ec;
+104
View File
@@ -6,6 +6,7 @@
*/
#include "server_fakes.h"
#include "lora/slots.h"
#include "server/liveview.h"
#include "server/metrics.h"
#include "text/chat.h"
@@ -2451,6 +2452,109 @@ TEST(models_and_health_answer) {
CHECK(json::parse(h.body)["status"].get<std::string>() == "ok");
}
/* RUNTIME LoRA admin, over a real slot table (lora/slots.h) the way Engine wires it: a request in
* flight holds its adapter, an unload then drains it (202, new requests 404 as an unknown model,
* off /v1/models, a load of the name 409), the request's end frees it, and an idle unload is 200.
* One slot, so the reload also waits out the cooling. */
TEST(an_adapter_in_use_drains_on_unload_and_frees_itself_when_its_last_request_ends) {
std::atomic<uint64_t> epoch{100};
lora::SlotTable table;
table.reset(1, &epoch);
Fixture f(Fixture::Tweak{ [&](ServerOptions& o, Deps&) {
o.lora_acquire = [&](const std::string& m, int32_t* s, uint64_t* u, std::shared_ptr<void>* h) {
return table.acquire(m, s, u, h);
};
o.lora_names = [&] { return table.names(); };
o.lora_load = [&](const std::string& n, const std::string& d, std::string* e) {
int slot = -1;
bool cooling = false;
const int st = table.reserve(n, &slot, &cooling, e);
if (st < 0) return st;
table.publish(slot, n, d);
return (int)RAD_OK;
};
o.lora_unload = [&](const std::string& n, std::string* e, int* in_flight) {
return table.unload(n, in_flight, e);
};
} });
auto listed = [&](const char* id) {
const json all = json::parse(f.get("/v1/models").body);
for (const json& m : all["data"])
if (m.value("id", std::string()) == id) return true;
return false;
};
const std::string load = R"({"lora_name":"ad","lora_path":"/adapters/ad"})";
const std::string unload = R"({"lora_name":"ad"})";
const std::string chat_ad = R"({"model":"ad","messages":[{"role":"user","content":"hi"}]})";
CHECK_EQ(f.post("/v1/load_lora_adapter", load).status, 200);
CHECK(listed("ad"));
/* A request through the adapter, held open: the stream never finishes on its own. */
f.sched.reply = "abcdefgh";
f.sched.hang = true;
std::atomic<bool> gone{false};
{
HttpRequest q;
q.method = "POST";
q.path = "/v1/chat/completions";
q.body = R"({"model":"ad","messages":[{"role":"user","content":"hi"}],"stream":true})";
q.closed = [&gone] { return gone.load(); };
HttpResponse r = f.srv->router().dispatch(q);
CHECK_EQ(r.status, 200);
for (int i = 0; i < 2; ++i) { std::string c; r.next(c); }
{
std::lock_guard<std::mutex> lk(f.sched.mu);
CHECK_EQ(f.sched.seen.back().lora_slot, 0);
CHECK(f.sched.seen.back().lora_uid != 0);
}
/* Unloaded while in use: 202, draining, the one request counted. */
HttpResponse u = f.post("/v1/unload_lora_adapter", unload);
CHECK_EQ(u.status, 202);
const json uj = json::parse(u.body);
CHECK_EQ(uj.value("action", std::string()), std::string("draining"));
CHECK_EQ(uj.value("lora_name", std::string()), std::string("ad"));
CHECK_EQ(uj.value("in_flight", 0), 1);
/* Asked again: the same, with the count as it is now. */
HttpResponse u2 = f.post("/v1/unload_lora_adapter", unload);
CHECK_EQ(u2.status, 202);
CHECK_EQ(json::parse(u2.body).value("in_flight", 0), 1);
/* A new request naming it is told what an unknown model is told, and it is not listed. */
HttpResponse n = f.post("/v1/chat/completions", chat_ad);
CHECK_EQ(n.status, 404);
CHECK_EQ(json::parse(n.body)["error"].value("message", std::string()),
std::string("no such model or adapter: ad (served: test-model)"));
CHECK(!listed("ad"));
CHECK_EQ(f.get("/v1/models/ad").status, 404);
/* A load of the same name waits for the drain. */
HttpResponse l = f.post("/v1/load_lora_adapter", load);
CHECK_EQ(l.status, 409);
CHECK(has(l.body, "being unloaded"));
/* The request ends (its client goes away), and with it the last hold. */
gone.store(true);
std::string c;
while (r.next(c)) {}
r.on_close(false);
}
f.sched.join_all();
CHECK_EQ(table.in_flight("ad"), 0);
CHECK_EQ(f.post("/v1/unload_lora_adapter", unload).status, 404); /* freed by itself */
/* The slot cools before it is written again, then the name loads afresh; idle, it unloads at
* once. */
CHECK_EQ(f.post("/v1/load_lora_adapter", load).status, 503);
epoch += lora::kSlotCoolRounds;
CHECK_EQ(f.post("/v1/load_lora_adapter", load).status, 200);
CHECK(listed("ad"));
HttpResponse idle = f.post("/v1/unload_lora_adapter", unload);
CHECK_EQ(idle.status, 200);
CHECK_EQ(json::parse(idle.body).value("action", std::string()), std::string("unloaded"));
CHECK(!listed("ad"));
}
TEST(the_model_entry_carries_the_context_length_the_server_was_started_with) {
/* Both routes a client sizes its window from: the list, and the entry it fetches by id. */
Fixture f(true, 262144, "/models/m.rad");