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

+148
View File
@@ -0,0 +1,148 @@
/* lora/slots.cpp -- see slots.h. */
#include "lora/slots.h"
#include "rad_internal.h"
namespace rad {
namespace lora {
void SlotTable::reset(int n, const std::atomic<uint64_t>* epoch) {
std::lock_guard<std::mutex> lk(mu_);
slots_.assign(n > 0 ? (size_t)n : 0, Slot{});
refs_.clear();
epoch_ = epoch;
}
int SlotTable::size() const {
std::lock_guard<std::mutex> lk(mu_);
return (int)slots_.size();
}
int SlotTable::reserve(const std::string& name, int* slot, bool* cooling, std::string* err) {
std::lock_guard<std::mutex> lk(mu_);
*slot = -1;
*cooling = false;
const uint64_t t = now();
for (size_t i = 0; i < slots_.size(); ++i) {
const Slot& x = slots_[i];
if (x.uid != 0 && x.name == name) {
if (!x.draining) {
*err = "an adapter named '" + name + "' is already loaded";
return RAD_E_INVAL;
}
/* The old adapter's requests are still running on its slot, and a request naming
* the new one would be told apart from them only by the uid: load it once the old one
* is gone (POST /v1/unload_lora_adapter says how many remain). */
const auto it = refs_.find(x.uid);
const int n = it == refs_.end() ? 0 : it->second;
*err = fmt("an adapter named '%s' is being unloaded: %d request%s still use%s it", name.c_str(),
n, n == 1 ? "" : "s", n == 1 ? "s" : "");
return RAD_E_STATE;
}
}
for (size_t i = 0; i < slots_.size(); ++i) {
const Slot& x = slots_[i];
if (x.uid != 0) continue;
if (x.used && t < x.freed_at + kSlotCoolRounds) { *cooling = true; continue; }
*slot = (int)i;
return RAD_OK;
}
*err = *cooling ? std::string("the free slot was unloaded a moment ago and a step on the card may "
"still read it; retry")
: fmt("all %zu adapter slots are in use (--max-loras)", slots_.size());
return RAD_E_FULL;
}
uint64_t SlotTable::publish(int slot, const std::string& name, const std::string& dir) {
std::lock_guard<std::mutex> lk(mu_);
if (slot < 0 || (size_t)slot >= slots_.size()) return 0;
Slot& s = slots_[(size_t)slot];
s.name = name;
s.dir = dir;
s.uid = next_uid_++;
s.used = true;
s.draining = false;
return s.uid;
}
int SlotTable::acquire(const std::string& name, int32_t* slot, uint64_t* uid,
std::shared_ptr<void>* hold) {
uint64_t u = 0;
{
std::lock_guard<std::mutex> lk(mu_);
for (size_t i = 0; i < slots_.size() && !u; ++i) {
const Slot& x = slots_[i];
if (x.uid == 0 || x.draining || x.name != name) continue;
u = x.uid;
*slot = (int32_t)i;
++refs_[u];
}
}
if (!u) return RAD_E_NOTFOUND;
*uid = u;
/* Made outside the lock: the deleter takes it, and runs at once if the allocation fails. */
*hold = std::shared_ptr<void>(nullptr, [this, u](void*) { release(u); });
return RAD_OK;
}
void SlotTable::free_slot(Slot& s, const char* how) {
RAD_INFO("lora: '%s' unloaded%s (uid %llu)", s.name.c_str(), how, (unsigned long long)s.uid);
s.uid = 0;
s.name.clear();
s.dir.clear();
s.draining = false;
s.freed_at = now();
}
void SlotTable::release(uint64_t uid) {
std::lock_guard<std::mutex> lk(mu_);
auto it = refs_.find(uid);
if (it == refs_.end()) return;
if (--it->second > 0) return;
refs_.erase(it);
/* The last request of a draining adapter: the unload it was waiting for happens now. */
for (Slot& s : slots_)
if (s.uid == uid && s.draining) free_slot(s, " -- its last request finished");
}
int SlotTable::unload(const std::string& name, int* in_flight, std::string* err) {
std::lock_guard<std::mutex> lk(mu_);
*in_flight = 0;
for (Slot& x : slots_) {
if (x.uid == 0 || x.name != name) continue;
const auto it = refs_.find(x.uid);
const int n = it == refs_.end() ? 0 : it->second;
if (n > 0) {
if (!x.draining)
RAD_INFO("lora: '%s' draining -- %d request%s still use%s it, new ones are refused",
name.c_str(), n, n == 1 ? "" : "s", n == 1 ? "s" : "");
x.draining = true;
*in_flight = n;
return RAD_OK;
}
free_slot(x, "");
return RAD_OK;
}
*err = "no adapter named '" + name + "' is loaded";
return RAD_E_NOTFOUND;
}
std::vector<std::string> SlotTable::names() const {
std::lock_guard<std::mutex> lk(mu_);
std::vector<std::string> out;
for (const Slot& s : slots_)
if (s.uid != 0 && !s.draining) out.push_back(s.name);
return out;
}
int SlotTable::in_flight(const std::string& name) const {
std::lock_guard<std::mutex> lk(mu_);
for (const Slot& s : slots_) {
if (s.uid == 0 || s.name != name) continue;
const auto it = refs_.find(s.uid);
return it == refs_.end() ? 0 : it->second;
}
return 0;
}
} /* namespace lora */
} /* namespace rad */
+92
View File
@@ -0,0 +1,92 @@
/* lora/slots.h -- which adapter each runtime-LoRA slot holds, who is using it, and when a slot may
* be written again. Host bookkeeping only: the engine copies the adapter's bytes (engine_lora.cpp)
* and this table decides which slot, publishes the name, hands out holds and frees slots.
*
* A SLOT'S LIFE:
*
* free --load--> loaded --unload, no request uses it--> free (cooling)
* | ^
* +--unload, requests use it--> draining ---+ the last of them finishes
*
* A request holds the adapter it names from admission until its reply is done (the server keeps
* the hold in the request's Generation), so an adapter is never unloaded under a request. An
* unload of an adapter in use DRAINS it: from that moment no new request can name it (it is gone
* from the names, and acquire answers as for a name never loaded), the requests already holding
* it finish normally, and the last hold to go frees the slot. A load of the same name waits for
* that rather than racing it.
*
* COOLING: a freed slot is written again only once the step loop has gone round three times
* since (`epoch` below counts its rounds): the step after a request's last one may already be on
* the card, issued ahead, and reads the slot through the step batch until it runs.
*
* Thread-safe: every call takes the table's lock. Loads are serialised by the caller (one at a
* time between reserve and publish), which is what keeps a reserved slot from being handed out
* twice. */
#pragma once
#include <atomic>
#include <cstdint>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
namespace rad {
namespace lora {
class SlotTable {
public:
/* `n` empty slots; `epoch` is the step loop's round counter, read when a slot is freed and
* when a load looks for one. It must outlive the table and every hold the table handed out. */
void reset(int n, const std::atomic<uint64_t>* epoch);
int size() const;
/* A free slot for `name`, or why not:
* RAD_E_INVAL an adapter of that name is loaded
* RAD_E_STATE an adapter of that name is draining (*err says how many requests remain)
* RAD_E_FULL no slot is free; *cooling says one would be after the step loop has gone
* round a few more times, which the caller may wait for */
int reserve(const std::string& name, int* slot, bool* cooling, std::string* err);
/* The adapter in `slot` is on the card: from now on a request may name it. Returns its uid,
* fresh for every load (never 0), which keys its prefix-cache blocks. */
uint64_t publish(int slot, const std::string& name, const std::string& dir);
/* The slot, uid and a hold for a loaded adapter that is not draining; RAD_E_NOTFOUND
* otherwise. The adapter is not freed while the hold lives. */
int acquire(const std::string& name, int32_t* slot, uint64_t* uid, std::shared_ptr<void>* hold);
/* Unpublish `name`. RAD_OK with *in_flight 0: the slot is free (cooling). RAD_OK with
* *in_flight > 0: those requests still use it, so it is draining and frees itself when the
* last of them finishes; a second unload answers the same with the count as it is then.
* RAD_E_NOTFOUND: nothing of that name is loaded. */
int unload(const std::string& name, int* in_flight, std::string* err);
/* The loaded adapters a request may name, in slot order: not the draining ones. */
std::vector<std::string> names() const;
/* Requests holding the adapter `name` (draining or not), 0 for none or no such adapter. */
int in_flight(const std::string& name) const;
private:
struct Slot {
std::string name, dir;
uint64_t uid = 0; /* 0: free */
bool used = false; /* ever held an adapter, so `freed_at` means something */
bool draining = false;
uint64_t freed_at = 0; /* `epoch` when it was last freed */
};
void free_slot(Slot& s, const char* how); /* under mu_ */
void release(uint64_t uid); /* a hold's deleter */
uint64_t now() const { return epoch_ ? epoch_->load(std::memory_order_acquire) : 0; }
mutable std::mutex mu_;
std::vector<Slot> slots_;
std::map<uint64_t, int> refs_; /* uid -> holds */
uint64_t next_uid_ = 1;
const std::atomic<uint64_t>* epoch_ = nullptr;
};
/* Step-loop rounds a freed slot waits before it is written again (see COOLING above). */
inline constexpr uint64_t kSlotCoolRounds = 3;
} /* namespace lora */
} /* namespace rad */