Archived
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:
1 parent
582cda0a53
commit
91127395ee
10 files changed
+495
-93
No files matched your search
+3
-1
@@ -2086,7 +2086,9 @@ int Engine::run_server() {
|
||||
o.lora_load = [this](const std::string& n, const std::string& d, std::string* e) {
|
||||
return lora_load(n, d, e);
|
||||
};
|
||||
o.lora_unload = [this](const std::string& n, std::string* e) { return lora_unload(n, e); };
|
||||
o.lora_unload = [this](const std::string& n, std::string* e, int* in_flight) {
|
||||
return lora_unload(n, e, in_flight);
|
||||
};
|
||||
}
|
||||
|
||||
/* Media, when the program declared an encoder. The processor's geometry comes from the
|
||||
|
||||
+15
-13
@@ -31,6 +31,7 @@
|
||||
#include "text/tokenizer.h"
|
||||
#include "format/radfile.h"
|
||||
#include "format/table_copy.h"
|
||||
#include "lora/slots.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
@@ -141,24 +142,25 @@ private:
|
||||
int lora_start();
|
||||
public:
|
||||
int lora_load(const std::string& name, const std::string& dir, std::string* err);
|
||||
/* Unpublish an adapter. Refused (RAD_E_STATE) while a request holds it; its slot is reused
|
||||
* only once the step loop has gone round three times since, so no step still on the card can
|
||||
* read bytes a later load is writing (the next step is issued before the last is read back). */
|
||||
int lora_unload(const std::string& name, std::string* err);
|
||||
/* RAD_OK with the slot, the uid of its load and a HOLD -- the adapter cannot be unloaded while
|
||||
* the hold lives (the server keeps it in the request's Generation, which outlives the request
|
||||
* in the scheduler). RAD_E_NOTFOUND for a name no slot holds. */
|
||||
/* Unpublish an adapter (lora::SlotTable::unload). Idle, its slot is freed at once; in use, it
|
||||
* DRAINS: *in_flight says how many requests still hold it, no new request may name it, and
|
||||
* the last of them frees the slot. A freed slot is reused only once the step loop has gone
|
||||
* round three times since, so no step still on the card can read bytes a later load is
|
||||
* writing (the next step is issued before the last is read back). */
|
||||
int lora_unload(const std::string& name, std::string* err, int* in_flight);
|
||||
/* RAD_OK with the slot, the uid of its load and a HOLD -- the adapter is not freed while the
|
||||
* hold lives (the server keeps it in the request's Generation, which outlives the request in
|
||||
* the scheduler). RAD_E_NOTFOUND for a name no slot holds, or one that is draining. */
|
||||
int lora_acquire(const std::string& name, int32_t* slot, uint64_t* uid,
|
||||
std::shared_ptr<void>* hold);
|
||||
std::vector<std::string> lora_names() const;
|
||||
private:
|
||||
struct LoraSlotState { std::string name, dir; uint64_t uid = 0; bool used = false; uint64_t freed_at = 0; };
|
||||
std::vector<LoraSlotState> lora_slots_;
|
||||
uint64_t lora_next_uid_ = 1;
|
||||
std::map<uint64_t, int> lora_refs_;
|
||||
mutable std::mutex lora_mu_;
|
||||
std::set<std::string> lora_targets_;
|
||||
std::atomic<uint64_t> lora_epoch_{0}; /* step-loop iterations, for slot reuse */
|
||||
lora::SlotTable lora_table_; /* reads lora_epoch_, so declared after it */
|
||||
/* Serialises loads: a slot is reserved, filled on every rank and published by one load at a
|
||||
* time. Requests and unloads go to the table alone and never wait for a load's copy. */
|
||||
std::mutex lora_mu_;
|
||||
std::set<std::string> lora_targets_;
|
||||
/* Resolves --embedding-placement, --vision-tower-placement and --ngram-placement into Config
|
||||
* before the card is split (engine_bringup.cpp describes how auto chooses). `wfs` is every
|
||||
* rank's footprint, read for its towers. `host_late` is the pinned host memory startup
|
||||
|
||||
+16
-72
@@ -77,7 +77,7 @@ int Engine::lora_targets() {
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
}
|
||||
lora_slots_.assign((size_t)cfg_.lora.slots(), LoraSlotState{});
|
||||
lora_table_.reset(cfg_.lora.slots(), &lora_epoch_);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -115,43 +115,25 @@ int Engine::lora_start() {
|
||||
}
|
||||
|
||||
int Engine::lora_load(const std::string& name, const std::string& dir, std::string* err) {
|
||||
std::unique_lock<std::mutex> lk(lora_mu_);
|
||||
std::lock_guard<std::mutex> lk(lora_mu_);
|
||||
if (!cfg_.lora.enabled()) { *err = "the engine was started without adapters"; return RAD_E_STATE; }
|
||||
if (name.empty()) { *err = "an adapter needs a name"; return RAD_E_INVAL; }
|
||||
int slot = -1;
|
||||
bool cooling = false;
|
||||
/* A SLOT FREED A MOMENT AGO IS WAITED FOR HERE, not handed back as a retry. It is reusable once
|
||||
* the step loop has gone round three times (a pass issued ahead may still read it), and the
|
||||
* loop goes round idle or busy, so that is milliseconds -- an unload followed at once by a load
|
||||
* (a hot swap) would otherwise fail almost every time under traffic. The lock is let go while
|
||||
* waiting; the step loop's counter is an atomic and needs none of it. */
|
||||
* (a hot swap) would otherwise fail almost every time under traffic. Only other loads wait on
|
||||
* lora_mu_ meanwhile; requests and unloads go to the table, and the step loop's counter is an
|
||||
* atomic. */
|
||||
const auto give_up = std::chrono::steady_clock::now() + std::chrono::seconds(2);
|
||||
int slot = -1;
|
||||
for (;;) {
|
||||
slot = -1;
|
||||
cooling = false;
|
||||
const uint64_t now = lora_epoch_.load(std::memory_order_acquire);
|
||||
for (size_t i = 0; i < lora_slots_.size(); ++i) {
|
||||
const LoraSlotState& x = lora_slots_[i];
|
||||
if (x.uid != 0 && x.name == name) {
|
||||
*err = "an adapter named '" + name + "' is already loaded";
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (x.uid != 0 || slot >= 0) continue;
|
||||
if (x.used && now < x.freed_at + 3) { cooling = true; continue; }
|
||||
slot = (int)i;
|
||||
}
|
||||
if (slot >= 0 || !cooling || stopping_.load(std::memory_order_acquire) ||
|
||||
bool cooling = false;
|
||||
const int s = lora_table_.reserve(name, &slot, &cooling, err);
|
||||
if (s >= 0) break;
|
||||
if (s != RAD_E_FULL || !cooling || stopping_.load(std::memory_order_acquire) ||
|
||||
std::chrono::steady_clock::now() >= give_up)
|
||||
break;
|
||||
lk.unlock();
|
||||
return s;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(2));
|
||||
lk.lock();
|
||||
}
|
||||
if (slot < 0) {
|
||||
*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)", lora_slots_.size());
|
||||
return RAD_E_FULL;
|
||||
}
|
||||
|
||||
/* ---- pack, per rank (host only) */
|
||||
@@ -222,11 +204,7 @@ int Engine::lora_load(const std::string& name, const std::string& dir, std::stri
|
||||
}
|
||||
}
|
||||
|
||||
LoraSlotState& ls = lora_slots_[(size_t)slot];
|
||||
ls.name = name;
|
||||
ls.dir = dir;
|
||||
ls.uid = lora_next_uid_++;
|
||||
ls.used = true;
|
||||
lora_table_.publish(slot, name, dir);
|
||||
const lora::AdapterInfo& a = infos[0];
|
||||
RAD_INFO("lora: '%s' in slot %d -- %zu modules, rank %lld, alpha %g%s, base %s", name.c_str(),
|
||||
slot, a.modules.size(), (long long)a.max_module_rank, a.alpha, a.rslora ? " (rsLoRA)" : "",
|
||||
@@ -236,49 +214,15 @@ int Engine::lora_load(const std::string& name, const std::string& dir, std::stri
|
||||
|
||||
int Engine::lora_acquire(const std::string& name, int32_t* slot, uint64_t* uid,
|
||||
std::shared_ptr<void>* hold) {
|
||||
std::lock_guard<std::mutex> lk(lora_mu_);
|
||||
for (size_t i = 0; i < lora_slots_.size(); ++i)
|
||||
if (lora_slots_[i].uid != 0 && lora_slots_[i].name == name) {
|
||||
const uint64_t u = lora_slots_[i].uid;
|
||||
*slot = (int32_t)i;
|
||||
*uid = u;
|
||||
++lora_refs_[u];
|
||||
*hold = std::shared_ptr<void>(nullptr, [this, u](void*) {
|
||||
std::lock_guard<std::mutex> g(lora_mu_);
|
||||
auto it = lora_refs_.find(u);
|
||||
if (it != lora_refs_.end() && --it->second <= 0) lora_refs_.erase(it);
|
||||
});
|
||||
return RAD_OK;
|
||||
}
|
||||
return RAD_E_NOTFOUND;
|
||||
return lora_table_.acquire(name, slot, uid, hold);
|
||||
}
|
||||
|
||||
int Engine::lora_unload(const std::string& name, std::string* err) {
|
||||
std::lock_guard<std::mutex> lk(lora_mu_);
|
||||
for (LoraSlotState& x : lora_slots_) {
|
||||
if (x.uid == 0 || x.name != name) continue;
|
||||
auto it = lora_refs_.find(x.uid);
|
||||
if (it != lora_refs_.end() && it->second > 0) {
|
||||
*err = fmt("%d request%s still use%s '%s'", it->second, it->second == 1 ? "" : "s",
|
||||
it->second == 1 ? "s" : "", name.c_str());
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
RAD_INFO("lora: '%s' unloaded (uid %llu)", name.c_str(), (unsigned long long)x.uid);
|
||||
x.uid = 0;
|
||||
x.name.clear();
|
||||
x.freed_at = lora_epoch_.load(std::memory_order_acquire);
|
||||
return RAD_OK;
|
||||
}
|
||||
*err = "no adapter named '" + name + "' is loaded";
|
||||
return RAD_E_NOTFOUND;
|
||||
int Engine::lora_unload(const std::string& name, std::string* err, int* in_flight) {
|
||||
return lora_table_.unload(name, in_flight, err);
|
||||
}
|
||||
|
||||
std::vector<std::string> Engine::lora_names() const {
|
||||
std::lock_guard<std::mutex> lk(lora_mu_);
|
||||
std::vector<std::string> out;
|
||||
for (const LoraSlotState& s : lora_slots_)
|
||||
if (s.uid != 0) out.push_back(s.name);
|
||||
return out;
|
||||
return lora_table_.names();
|
||||
}
|
||||
|
||||
} /* namespace rad */
|
||||
@@ -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 */
|
||||
@@ -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 */
|
||||
+12
-1
@@ -1832,17 +1832,28 @@ HttpResponse Server::handle_lora_admin(const HttpRequest& req, bool load) {
|
||||
return fail(400, "invalid_request_error", "lora_name must be non-empty and not the served model's name");
|
||||
std::string err;
|
||||
int s;
|
||||
int in_flight = 0;
|
||||
if (load) {
|
||||
if (!j.contains("lora_path") || !j["lora_path"].is_string())
|
||||
return fail(400, "invalid_request_error", "load_lora_adapter needs lora_path");
|
||||
s = opt_.lora_load(name, j["lora_path"].get<std::string>(), &err);
|
||||
} else {
|
||||
s = opt_.lora_unload(name, &err);
|
||||
s = opt_.lora_unload(name, &err, &in_flight);
|
||||
}
|
||||
/* 409 for a load of a name that is still draining: the old adapter's requests are running. */
|
||||
if (s == RAD_E_NOTFOUND) return fail(404, "not_found_error", err);
|
||||
if (s == RAD_E_STATE) return fail(409, "conflict_error", err);
|
||||
if (s == RAD_E_FULL) return fail(503, "service_unavailable", err);
|
||||
if (s < 0) return fail(400, "invalid_request_error", err.empty() ? std::string(rad_strerror(s)) : err);
|
||||
/* AN UNLOAD OF AN ADAPTER IN USE DRAINS IT (lora/slots.h): accepted, not done. New requests
|
||||
* naming it are already refused as an unknown model and it is off /v1/models; the requests
|
||||
* counted here finish normally, and the last of them frees the slot. Asking again answers
|
||||
* the same with the count as it is then, and a 404 once it is gone. */
|
||||
if (!load && in_flight > 0) {
|
||||
metrics_.observe_http(202);
|
||||
return HttpResponse::json_body(202, json{ { "action", "draining" }, { "lora_name", name },
|
||||
{ "in_flight", in_flight } }.dump());
|
||||
}
|
||||
metrics_.observe_http(200);
|
||||
return HttpResponse::json_body(200, json{ { "status", "ok" }, { "lora_name", name },
|
||||
{ "action", load ? "loaded" : "unloaded" } }.dump());
|
||||
|
||||
@@ -119,13 +119,16 @@ struct ServerOptions {
|
||||
* clock only mean something under load: queried during a long prefill, it tells a link parked
|
||||
* below its maximum from one at rest. */
|
||||
std::function<std::string()> diagnostics_json;
|
||||
/* RUNTIME LoRA (Engine::lora_resolve): a request's `model` naming a loaded adapter gets that
|
||||
* adapter's slot and load uid. Unset with LoRA off, and then any `model` is served by the base. */
|
||||
/* RUNTIME LoRA (Engine::lora_acquire): a request's `model` naming a loaded adapter gets that
|
||||
* adapter's slot, load uid and a hold on it. Unset with LoRA off, and then any `model` is
|
||||
* served by the base. `lora_names` lists the adapters a request may name (not the draining
|
||||
* ones). */
|
||||
std::function<int(const std::string&, int32_t*, uint64_t*, std::shared_ptr<void>*)> lora_acquire;
|
||||
std::function<std::vector<std::string>()> lora_names;
|
||||
/* POST /v1/load_lora_adapter and /v1/unload_lora_adapter (vLLM's names and bodies). */
|
||||
/* POST /v1/load_lora_adapter and /v1/unload_lora_adapter (vLLM's names and bodies). An unload
|
||||
* of an adapter requests still use drains it: RAD_OK with *in_flight > 0 (Engine::lora_unload). */
|
||||
std::function<int(const std::string&, const std::string&, std::string*)> lora_load;
|
||||
std::function<int(const std::string&, std::string*)> lora_unload;
|
||||
std::function<int(const std::string&, std::string*, int*)> lora_unload;
|
||||
|
||||
std::string api_key;
|
||||
bool cors = true;
|
||||
|
||||
+9
-1
@@ -1145,7 +1145,15 @@ all-reduce, so it works at any `--tp`.
|
||||
A and B for every targeted weight; startup prints the size. With free slots, adapters load and
|
||||
unload while serving: `POST /v1/load_lora_adapter {"lora_name": "x", "lora_path": "/dir"}` and
|
||||
`POST /v1/unload_lora_adapter {"lora_name": "x"}` (vLLM's routes; loopback-only without
|
||||
`--api-key`). An unload is refused (409) while a request uses the adapter.
|
||||
`--api-key`).
|
||||
- An unload of an idle adapter answers 200 and frees its slot. An unload of an adapter requests
|
||||
are still using **drains** it and answers 202
|
||||
`{"action": "draining", "lora_name": "x", "in_flight": N}`: from then on a request naming `x`
|
||||
gets the 404 an unknown model gets and `/v1/models` no longer lists it, the N requests finish
|
||||
normally, and the last of them frees the slot. Asking again while it drains answers 202 with the
|
||||
count as it is then, and 404 once it is gone. Loading the same name while it drains answers 409;
|
||||
load it again once the unload answers 404. A freed slot is reused a few steps later (a step
|
||||
already on the card may still read it), which a load waits for by itself.
|
||||
- `--max-lora-rank R` (default 16) is the rank every slot is sized for. A smaller rank is padded,
|
||||
a larger one refused.
|
||||
- `--lora-target-modules q_proj,k_proj,...` names what adapters may target. Default: what the
|
||||
|
||||
+89
-1
@@ -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;
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in new issue
Block a user