Archived
radiance 1.8.0
- video: MiniMax-H3 and FastVideo's FastH3 8-Step V2, a video with its stereo soundtrack from text on OpenAI's /v1/videos, block fp8 on two cards - music: MiniMax-Music3, songs from lyrics and a description on /v1/audio/speech - speech: Breeze-TTS-2 on /v1/audio/speech and vLLM-Omni's realtime WebSocket - runtime LoRA adapters beside the base model, picked per request by `model` (baKon, #105) - several agent streams keep their cached history: --checkpoint-slots auto, the new default, sizes the snapshot store by --max-num-seqs (#108) - the Swift 1.5 27B W4 releases convert and serve: compressed-tensors AWQ and GPTQ int4, AutoRound, ModelOpt NVFP4 (#110) - the image's host tests pass where perf_event_open is refused, as in BuildKit's sandbox (#106) - plugin ABI 21: out-of-tree plugins must be rebuilt
This commit is contained in:
1 parent
91127395ee
commit
7a7707a955
179 files changed
+37542
-1892
No files matched your search
+5
-9
@@ -2,11 +2,9 @@
|
||||
* --lora-target-modules). Read by declare (which sites and pools exist), by the adapter manager
|
||||
* (what may be loaded into them) and by nothing else.
|
||||
*
|
||||
* OFF UNLESS ASKED FOR: with no adapter named and no --max-loras, `enabled()` is false, declare
|
||||
* appends nothing, the program has no pool and no extra op, and the engine computes exactly what it
|
||||
* computed before this file existed. */
|
||||
* Off unless asked for: with no adapter named and no --max-loras, `enabled()` is false, declare
|
||||
* appends nothing, the program has no pool and no extra op, and no step carries an adapter slot. */
|
||||
#pragma once
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -20,11 +18,9 @@ struct LoraConfig {
|
||||
int max_loras = 0;
|
||||
/* The rank every slot is sized for; an adapter of lower rank is zero-padded into it. */
|
||||
int max_rank = 16;
|
||||
/* HF module leaf names (q_proj, gate_proj, in_proj_qkv, ...) the sites are built for. Empty:
|
||||
* the union of the startup adapters' target_modules, or -- with no startup adapter -- every
|
||||
* LoRA-capable trunk linear (`all_linear`). */
|
||||
std::set<std::string> targets;
|
||||
bool all_linear = false;
|
||||
/* HF module leaf names (q_proj, gate_proj, in_proj_qkv, ...) the sites are built for, sorted
|
||||
* and unique. Empty: the union of the startup adapters' target_modules. */
|
||||
std::vector<std::string> targets;
|
||||
|
||||
bool enabled() const { return max_loras > 0 || !adapters.empty(); }
|
||||
int slots() const {
|
||||
|
||||
+57
-16
@@ -28,6 +28,38 @@ bool is_coded(uint32_t dt) {
|
||||
return dt == RAD_F8E4M3 || dt == RAD_F8E5M2 || dt == RAD_I8;
|
||||
}
|
||||
|
||||
/* The block of a scale stream of `blocks` groups over `ext` elements, as the kernel libraries
|
||||
* read one (libref/ref_gemm.h's grid_block): the power of two whose ceiling division gives
|
||||
* `blocks`, else the plain ceiling; one group is the whole extent. */
|
||||
int64_t grid_block(int64_t ext, int64_t blocks) {
|
||||
if (ext <= 0) return 1;
|
||||
if (blocks <= 1) return ext;
|
||||
const int64_t lo = (ext + blocks - 1) / blocks;
|
||||
int64_t b = 1;
|
||||
while (b < lo) b <<= 1;
|
||||
return (ext + b - 1) / b == blocks ? b : lo;
|
||||
}
|
||||
|
||||
/* The scale grid of a coded activation the GEMM `reads`: the read set's one f32 buffer beside
|
||||
* the codes (reads[0]), [rows, groups] or one scale a row, over the codes' full width. The
|
||||
* GEMM's own `group` is the weight's (an MXFP4 weight's 32-wide exponent block reads an
|
||||
* activation scaled per 128), and a GEMM may read a column span of the activation, so neither
|
||||
* its `group` nor its K gives the activation's grid. 0 when the stream is not one f32 buffer. */
|
||||
int64_t activation_grid(const Program& P, const std::vector<rad_buf>& reads) {
|
||||
const RadBufDecl& a = P.buffers[reads[0]].decl;
|
||||
const RadBufDecl* s = nullptr;
|
||||
for (size_t i = 1; i < reads.size(); ++i) {
|
||||
const RadBufDecl& d = P.buffers[reads[i]].decl;
|
||||
if (d.dtype != RAD_F32) continue;
|
||||
if (s) return 0;
|
||||
s = &d;
|
||||
}
|
||||
if (!s || a.rank < 1 || s->rank < 1) return 0;
|
||||
const int64_t width = a.shape[a.rank - 1];
|
||||
const int64_t groups = s->rank >= 2 ? s->shape[s->rank - 1] : 1;
|
||||
return groups > 0 ? grid_block(width, groups) : 0;
|
||||
}
|
||||
|
||||
rad_buf decl_persist(Builder& b, const std::string& name, uint32_t dtype, uint32_t rank,
|
||||
const int64_t* shape) {
|
||||
RadBufDecl d{};
|
||||
@@ -44,6 +76,16 @@ rad_buf decl_persist(Builder& b, const std::string& name, uint32_t dtype, uint32
|
||||
int lora_declare(Builder& b, const LoraDeclare& d) {
|
||||
if (!d.cfg || !d.cfg->enabled()) return RAD_OK;
|
||||
Program& P = b.program();
|
||||
/* Adapters are chosen per request and applied on the token steps of a model that writes
|
||||
* logits; a generator's passes and an embedding model's requests carry no adapter. */
|
||||
if (P.is_pass_driven() || P.pooled_buf != RAD_NULL_HANDLE ||
|
||||
P.logits_buf == RAD_NULL_HANDLE) {
|
||||
RAD_ERR("lora: runtime LoRA adapters serve a model that generates text, and this one %s",
|
||||
P.is_pass_driven() ? "is an image, audio or video generator"
|
||||
: P.pooled_buf != RAD_NULL_HANDLE ? "makes embeddings"
|
||||
: "declares no logits");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
const int S = d.cfg->slots();
|
||||
const int max_rank = d.cfg->max_rank;
|
||||
const int64_t max_tok = P.ctx.max_tok;
|
||||
@@ -53,10 +95,8 @@ int lora_declare(Builder& b, const LoraDeclare& d) {
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
|
||||
/* EVERY READER OF A TARGETED WEIGHT CARRIES THE DELTA, OR STARTUP REFUSES. A targeted weight
|
||||
* that some op multiplies without the adapter is a model that is the base in one place and the
|
||||
* adapter in another, and the text it writes is wrong in a way no test of the targeted GEMM
|
||||
* would show. */
|
||||
/* Every reader of a targeted weight carries the delta, or startup refuses: a weight some op
|
||||
* multiplies without the adapter would be the base in one place and the adapter in another. */
|
||||
std::vector<lora::UnsupportedUse> bad;
|
||||
int s = lora::find_unsupported_uses(P, d.targets, &bad);
|
||||
if (s < 0) return s;
|
||||
@@ -110,10 +150,10 @@ int lora_declare(Builder& b, const LoraDeclare& d) {
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- t = A.x, one f32 plane per capacity R and lane. PERSIST and not TRANSIENT: the ops that
|
||||
* use it are issued inside the GEMM's issue, at the GEMM's position, while the buffer plan would
|
||||
* place a transient by the appended ops' own position at the end of the program -- and share
|
||||
* its bytes with whatever is live there. */
|
||||
/* ---- t = A.x, one f32 plane per capacity R and lane. Persistent, not transient: the ops that
|
||||
* use it run at the GEMM's position, but the buffer plan would place a transient at the
|
||||
* appended ops' own position, the end of the program, and share its bytes with buffers live
|
||||
* mid-program. */
|
||||
std::map<int64_t, int32_t> t_by_r;
|
||||
P.lora_t.clear();
|
||||
for (const lora::Site& x : sites) {
|
||||
@@ -136,14 +176,13 @@ int lora_declare(Builder& b, const LoraDeclare& d) {
|
||||
const uint32_t gh = x.gemm;
|
||||
if (gh == 0 || gh >= P.ops.size()) return RAD_E_STATE;
|
||||
if (!b.op_resolved(gh)) continue; /* never handed to the plugin, so never issued */
|
||||
/* Copied out first: decl_op below appends to P.ops and moves every OpInfo. */
|
||||
/* Copied out first: decl_op below appends to P.ops, which may move every OpInfo. */
|
||||
const OpInfo& g = P.ops[gh];
|
||||
const std::string gop = g.op;
|
||||
const std::string rkey = g.ranged_key;
|
||||
const int64_t lo = g.range_lo, hi = g.range_hi;
|
||||
long long gk = 0, gn = 0, gg = 0;
|
||||
long long gk = 0, gn = 0;
|
||||
const bool has_k = g.base.get_i("K", &gk), has_n = g.base.get_i("N", &gn);
|
||||
const bool has_group = g.base.get_i("group", &gg) && gg > 0;
|
||||
const int oa = opd_index(g, "a"), oas = opd_index(g, "a_scale"), oy = opd_index(g, "y");
|
||||
const std::vector<rad_buf> reads = b.op_read_set(gh), writes = b.op_write_set(gh);
|
||||
|
||||
@@ -167,6 +206,12 @@ int lora_declare(Builder& b, const LoraDeclare& d) {
|
||||
const uint32_t y_dt = P.buffers[writes[0]].decl.dtype;
|
||||
const bool coded = is_coded(a_dt);
|
||||
if (coded && oas < 0) { refuse("a coded activation and no `a_scale` operand"); continue; }
|
||||
const int64_t a_grid = coded ? activation_grid(P, reads) : 0;
|
||||
if (coded && a_grid <= 0) {
|
||||
refuse("a coded activation whose read set holds no single f32 scale stream beside the "
|
||||
"codes, so the grid lora_shrink reads it at is not known at declare");
|
||||
continue;
|
||||
}
|
||||
|
||||
RadParam sp[6];
|
||||
int n_sp = 0;
|
||||
@@ -174,7 +219,7 @@ int lora_declare(Builder& b, const LoraDeclare& d) {
|
||||
sp[n_sp++] = RAD_INT("K", x.K_r);
|
||||
sp[n_sp++] = RAD_INT("R", x.R);
|
||||
sp[n_sp++] = RAD_INT("S", S);
|
||||
if (coded && has_group) sp[n_sp++] = RAD_INT("group", gg);
|
||||
if (coded) sp[n_sp++] = RAD_INT("group", a_grid);
|
||||
sp[n_sp++] = RAD_STR("dtype", rad_dtype_name(a_dt));
|
||||
const rad_op hs = b.decl_op("lora_shrink", sp, n_sp, nullptr, 0);
|
||||
|
||||
@@ -200,10 +245,6 @@ int lora_declare(Builder& b, const LoraDeclare& d) {
|
||||
P.ops[he].anchor = (int32_t)gh;
|
||||
P.ops[gh].lora_site = (int32_t)P.lora_sites.size();
|
||||
P.lora_sites.push_back(is);
|
||||
|
||||
x.lora_shrink = hs; x.lora_expand = he;
|
||||
const LoraPool& lp = P.lora_pools[(size_t)x.pool];
|
||||
x.pool_a = lp.a; x.pool_b = lp.b; x.pool_scale = lp.scale;
|
||||
}
|
||||
if (n_refused) return RAD_E_UNSUPPORTED;
|
||||
|
||||
|
||||
+3
-3
@@ -2,9 +2,9 @@
|
||||
* its program, build the sites (lora.h), declare the adapter pools and the two ops each targeted
|
||||
* GEMM is followed by, and record what the issue path needs (Program::lora_sites).
|
||||
*
|
||||
* Runs inside rad_tools_declare, after rad_arch_declare and before the sampler, on the real
|
||||
* declare AND on every sizing declare -- identically, because a sizing declare must produce the
|
||||
* same op list (rad_builder.cpp checks it op by op). */
|
||||
* Runs inside rad_tools_declare, after rad_arch_declare and before the sampler, identically on the
|
||||
* real declare and on every sizing declare, which must produce the same op list (rad_builder.cpp
|
||||
* checks it op by op). */
|
||||
#pragma once
|
||||
#include "lora/config.h"
|
||||
#include "lora/lora.h"
|
||||
|
||||
+39
-52
@@ -130,7 +130,7 @@ struct MapIndex {
|
||||
}
|
||||
};
|
||||
|
||||
/* THE predicate, over the maps already found. */
|
||||
/* weight_targeted(), over the maps already found. */
|
||||
bool targeted_core(const Program& prog, const WeightInfo& w,
|
||||
const std::vector<const RadNameMap*>& maps, const TargetSet& targets) {
|
||||
if (targets.empty() || maps.empty()) return false;
|
||||
@@ -190,11 +190,6 @@ rad_weight b_weight(const OpInfo& op) {
|
||||
return op.weights.empty() ? 0 : op.weights[0];
|
||||
}
|
||||
|
||||
void fnv(uint64_t* h, const void* p, size_t n) {
|
||||
const uint8_t* b = static_cast<const uint8_t*>(p);
|
||||
for (size_t i = 0; i < n; ++i) { *h ^= b[i]; *h *= 1099511628211ull; }
|
||||
}
|
||||
|
||||
bool read_file(const std::string& path, std::string* out) {
|
||||
std::ifstream f(path, std::ios::binary);
|
||||
if (!f) return false;
|
||||
@@ -339,11 +334,9 @@ int make_site(const Program& prog, const MapIndex& mi, const TargetSet& targets,
|
||||
Site s;
|
||||
s.gemm = op_index;
|
||||
s.op = op.op;
|
||||
s.w = wh;
|
||||
s.weight = W.name;
|
||||
s.shard = W.decl.shard;
|
||||
s.max_rank = max_rank;
|
||||
s.res_unchecked = op.op == "gemm_nt_bias";
|
||||
|
||||
if (W.decl.rank != 2)
|
||||
return fail(err, RAD_E_SHAPE, fmt("weight '%s' is rank %u; a LoRA site needs a matrix",
|
||||
@@ -425,8 +418,8 @@ int make_site(const Program& prog, const MapIndex& mi, const TargetSet& targets,
|
||||
int64_t N_full = 0;
|
||||
for (int64_t t : s.part_total) N_full = (t > 0 && N_full >= 0) ? N_full + t : -1;
|
||||
|
||||
/* ---- THE LOADER'S OWN FUNCTION AGREES, or this is not a site. weight_share is what puts the
|
||||
* base weight's bytes on this rank; asked for a plain [N_full, K_full] entry it must name the
|
||||
/* ---- the loader's own cut must agree, or this is not a site: weight_share, which puts the
|
||||
* base weight's bytes on this rank, must name for a plain [N_full, K_full] entry the
|
||||
* rectangles this geometry names. */
|
||||
if (N_full > 0 && s.K_full > 0) {
|
||||
WeightInfo v = W;
|
||||
@@ -483,7 +476,7 @@ int make_site(const Program& prog, const MapIndex& mi, const TargetSet& targets,
|
||||
srcs.push_back(m->src[k] ? m->src[k] : "");
|
||||
}
|
||||
if (alternatives) {
|
||||
/* COPY with alternatives: the first one the container holds IS the weight, and which one
|
||||
/* COPY with alternatives: the first one the container holds is the weight, and which one
|
||||
* that is is not recorded. Usable only when exactly one alternative is targeted. */
|
||||
std::vector<std::string> hit;
|
||||
for (const std::string& x : srcs) {
|
||||
@@ -580,6 +573,15 @@ bool op_is_site(const Program& prog, const OpInfo& op, rad_weight* b_out, std::s
|
||||
return no(fmt("gemm_nt_q over rotated parts (pb1=%lld pb2=%lld): its input is the "
|
||||
"rotated activation", p1, p2));
|
||||
}
|
||||
if (gq || gqb) {
|
||||
/* At w4a8, w8a8 and w2a8 `a` is quant_act_i8's codes in the int8 WMMA's step order (the
|
||||
* middle dwords of every 16-byte step exchanged, docs/OPS.md), which lora_shrink would
|
||||
* read as the activation's columns. */
|
||||
const char* dt = op.base.get_s("dtype");
|
||||
if (dt && (!std::strcmp(dt, "w4a8") || !std::strcmp(dt, "w8a8") || !std::strcmp(dt, "w2a8")))
|
||||
return no(fmt("%s at %s reads quant_act_i8's codes in the int8 WMMA's step order, not "
|
||||
"in the activation's column order", op.op.c_str(), dt));
|
||||
}
|
||||
const WeightInfo& w = prog.weights[b];
|
||||
if (gq || gqb) {
|
||||
long long k = 0;
|
||||
@@ -607,7 +609,7 @@ int find_unsupported_uses(const Program& prog, const TargetSet& targets,
|
||||
const bool site = op_is_site(prog, op, &b, &why);
|
||||
for (rad_weight h : op.weights) {
|
||||
if (!targeted_idx(prog, mi, h, targets)) continue;
|
||||
/* Every view of a targeted weight read by a site over the SAME logical weight is the
|
||||
/* Every view of a targeted weight read by a site over the same logical weight is the
|
||||
* site's own (codes, scale, ref). Anything else reading it is not a site. */
|
||||
if (site && b && prog.weights[b].source == prog.weights[h].source) continue;
|
||||
UnsupportedUse u;
|
||||
@@ -669,32 +671,6 @@ int build_sites(const Program& prog, const TargetSet& targets, int max_rank,
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
TargetSet all_capable_targets(const Program& prog) {
|
||||
std::set<std::string> leaves;
|
||||
for (size_t h = 1; h < prog.weights.size(); ++h) {
|
||||
const WeightInfo& w = prog.weights[h];
|
||||
if (excluded_weight(prog, w)) continue;
|
||||
std::vector<const RadNameMap*> maps;
|
||||
scan_maps(prog, w.source, &maps);
|
||||
for (const RadNameMap* m : maps)
|
||||
for (int k = 0; k < m->n_src && k < 8; ++k) {
|
||||
std::string leaf;
|
||||
const std::string src = m->src[k] ? m->src[k] : "";
|
||||
if (!module_of(src, &leaf).empty() && !excluded_source(src)) leaves.insert(leaf);
|
||||
}
|
||||
}
|
||||
TargetSet ok;
|
||||
for (const std::string& l : leaves) {
|
||||
const TargetSet one{ l };
|
||||
std::vector<Site> s;
|
||||
std::string e;
|
||||
if (find_unsupported_uses(prog, one, nullptr) == 0 && build_sites(prog, one, 1, &s, &e) == RAD_OK &&
|
||||
!s.empty())
|
||||
ok.insert(l);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
std::string explain_module(const Program& prog, const TargetSet& targets, const std::string& module) {
|
||||
const std::string want[3] = {
|
||||
module,
|
||||
@@ -817,6 +793,17 @@ int load_adapter(const std::string& dir, const std::vector<Site>& sites, int max
|
||||
if (truthy("trainable_token_indices"))
|
||||
return fail(err, RAD_E_UNSUPPORTED, cfg_path + ": trainable_token_indices -- embedding "
|
||||
"updates are not served");
|
||||
/* PEFT options that change the forward pass itself, not only the weights. */
|
||||
if (truthy("layer_replication"))
|
||||
return fail(err, RAD_E_UNSUPPORTED, cfg_path + ": layer_replication -- an adapter over "
|
||||
"repeated base layers is not served");
|
||||
if (truthy("use_qalora"))
|
||||
return fail(err, RAD_E_UNSUPPORTED, cfg_path + ": use_qalora -- QA-LoRA's pooled input is "
|
||||
"not served");
|
||||
if (truthy("alora_invocation_tokens"))
|
||||
return fail(err, RAD_E_UNSUPPORTED, cfg_path + ": alora_invocation_tokens -- an activated "
|
||||
"LoRA, which applies only after its "
|
||||
"invocation, is not served");
|
||||
if (!cfg.contains("r") || !cfg["r"].is_number_integer() || cfg["r"].get<int64_t>() <= 0)
|
||||
return fail(err, RAD_E_FORMAT, cfg_path + ": no positive integer `r`");
|
||||
if (!cfg.contains("lora_alpha") || !cfg["lora_alpha"].is_number())
|
||||
@@ -833,6 +820,9 @@ int load_adapter(const std::string& dir, const std::vector<Site>& sites, int max
|
||||
else if (t.is_array())
|
||||
for (const ojson& x : t) if (x.is_string()) info->target_modules.push_back(x.get<std::string>());
|
||||
}
|
||||
/* PEFT's get_pattern_key: a key matches a module whose name ends in it on a dot boundary,
|
||||
* `(.*\.)?(key)$` from the start of the name, and the first matching key in the file's order
|
||||
* wins. */
|
||||
struct Pat { std::string key; std::regex re; double v; };
|
||||
std::vector<Pat> rpat, apat;
|
||||
for (const char* which : { "rank_pattern", "alpha_pattern" }) {
|
||||
@@ -846,14 +836,17 @@ int load_adapter(const std::string& dir, const std::vector<Site>& sites, int max
|
||||
Pat p;
|
||||
p.key = it.key();
|
||||
try {
|
||||
p.re = std::regex(p.key, std::regex::ECMAScript);
|
||||
p.re = std::regex("(.*\\.)?(" + p.key + ")$", std::regex::ECMAScript);
|
||||
} catch (const std::regex_error& e) {
|
||||
return fail(err, RAD_E_FORMAT, cfg_path + ": " + which + " key '" + p.key +
|
||||
"' is not a regex this engine reads (" + e.what() + ")");
|
||||
}
|
||||
p.v = it.value().get<double>();
|
||||
if (!std::isfinite(p.v))
|
||||
return fail(err, RAD_E_FORMAT, cfg_path + ": " + which + "[" + p.key +
|
||||
"] is not a finite number");
|
||||
if (!std::strcmp(which, "rank_pattern")) {
|
||||
if (!it.value().is_number_integer() || p.v <= 0)
|
||||
if (!it.value().is_number_integer() || p.v <= 0 || p.v > (double)INT32_MAX)
|
||||
return fail(err, RAD_E_FORMAT, cfg_path + ": rank_pattern[" + p.key +
|
||||
"] is not a positive integer");
|
||||
info->rank_pattern.push_back({ p.key, (int64_t)p.v });
|
||||
@@ -873,19 +866,10 @@ int load_adapter(const std::string& dir, const std::vector<Site>& sites, int max
|
||||
"adapter as safetensors");
|
||||
if (st.open(st_path) != RAD_OK) return fail(err, RAD_E_IO, st_path + ": cannot open as safetensors");
|
||||
|
||||
uint64_t hash = 1469598103934665603ull;
|
||||
fnv(&hash, text.data(), text.size());
|
||||
std::vector<const StTensor*> order;
|
||||
for (const StTensor& t : st.tensors()) order.push_back(&t);
|
||||
std::sort(order.begin(), order.end(),
|
||||
[](const StTensor* a, const StTensor* b) { return a->name < b->name; });
|
||||
for (const StTensor* t : order) {
|
||||
fnv(&hash, t->name.data(), t->name.size() + 1);
|
||||
fnv(&hash, t->dtype_str.data(), t->dtype_str.size() + 1);
|
||||
for (int64_t d : t->shape) fnv(&hash, &d, sizeof d);
|
||||
if (t->data) fnv(&hash, t->data, (size_t)t->bytes);
|
||||
}
|
||||
info->content_hash = hash;
|
||||
|
||||
static const std::regex key_re(R"(^(.*)\.lora_([AB])\.(weight|default\.weight)$)");
|
||||
std::map<std::string, AdModule> mods;
|
||||
@@ -902,7 +886,7 @@ int load_adapter(const std::string& dir, const std::vector<Site>& sites, int max
|
||||
"; only linear LoRA A/B pairs are served");
|
||||
}
|
||||
std::string mod = m[1].str();
|
||||
const size_t at = mod.find("base_model.model."); /* rad-export: first occurrence */
|
||||
const size_t at = mod.find("base_model.model."); /* the first one, as rad-export strips it */
|
||||
if (at != std::string::npos) mod.erase(at, 17);
|
||||
AdModule& am = mods[mod];
|
||||
am.name = mod;
|
||||
@@ -932,8 +916,8 @@ int load_adapter(const std::string& dir, const std::vector<Site>& sites, int max
|
||||
}
|
||||
int64_t rr = info->r;
|
||||
double aa = info->alpha;
|
||||
for (const Pat& p : rpat) if (std::regex_search(am.name, p.re)) { rr = (int64_t)p.v; break; }
|
||||
for (const Pat& p : apat) if (std::regex_search(am.name, p.re)) { aa = p.v; break; }
|
||||
for (const Pat& p : rpat) if (std::regex_match(am.name, p.re)) { rr = (int64_t)p.v; break; }
|
||||
for (const Pat& p : apat) if (std::regex_match(am.name, p.re)) { aa = p.v; break; }
|
||||
if (am.A->shape[0] != rr || am.B->shape[1] != rr)
|
||||
return fail(err, RAD_E_SHAPE,
|
||||
fmt("module %s: expected rank %lld (r / rank_pattern), found lora_A %s and "
|
||||
@@ -945,6 +929,9 @@ int load_adapter(const std::string& dir, const std::vector<Site>& sites, int max
|
||||
am.rank = rr;
|
||||
am.alpha = aa;
|
||||
am.scale = (float)(info->rslora ? aa / std::sqrt((double)rr) : aa / (double)rr);
|
||||
if (!std::isfinite(am.scale))
|
||||
return fail(err, RAD_E_FORMAT, fmt("module %s: alpha %g over rank %lld is no finite "
|
||||
"f32 scale", am.name.c_str(), aa, (long long)rr));
|
||||
info->max_module_rank = std::max(info->max_module_rank, rr);
|
||||
const size_t d = am.name.rfind('.');
|
||||
info->leaves.insert(d == std::string::npos ? am.name : am.name.substr(d + 1));
|
||||
|
||||
+22
-41
@@ -1,22 +1,19 @@
|
||||
/* lora.h -- runtime LoRA: which declared GEMMs an adapter lands in, and the bytes of one adapter for
|
||||
* one rank (research/radiance/lora/DESIGN.md §2, §5).
|
||||
* one rank. Two host-only steps that read nothing but the declared Program:
|
||||
*
|
||||
* TWO PURE STEPS, both host-only and both read nothing but the declared Program:
|
||||
* build_sites() after declare, every LoRA-capable trunk GEMM whose weight a target names becomes
|
||||
* a Site: the weight's per-rank geometry (K slice, output width, where each PEFT
|
||||
* module's rows land in y), read from what the plugin recorded on the weight
|
||||
* (shard mode, row parts, shard and part spans, name-map sources), never from a
|
||||
* plugin's own split rule. One Site per GEMM op; the Sites of one declared weight
|
||||
* share one pool.
|
||||
* load_adapter() a PEFT directory -> one PackedSite per pool, the bytes of one slot.
|
||||
*
|
||||
* build_sites() after declare: every LoRA-capable trunk GEMM whose weight a target names becomes
|
||||
* a Site -- the weight's per-rank geometry (K slice, output width, which rows of
|
||||
* which PEFT module land where in the GEMM's y) read off the facts the plugin
|
||||
* recorded on the weight: its shard mode, row parts, shard / part spans and its
|
||||
* name-map sources. Never a plugin's own split rule. One Site per GEMM op; the
|
||||
* Sites of one declared weight share one POOL (one slot layout per weight).
|
||||
* load_adapter() a PEFT directory -> one PackedSite per pool: exactly the bytes of one slot.
|
||||
* Nothing here touches a device; the caller declares the pools and copies.
|
||||
*
|
||||
* Nothing here allocates device memory or touches a device; the caller declares the pools and copies.
|
||||
*
|
||||
* TARGETS ARE HF MODULE LEAF NAMES ("q_proj", "gate_proj", "in_proj_qkv", ...). A declared weight is
|
||||
* targeted when one of its name-map sources is `<prefix>.<leaf>.weight`, `leaf` in the set, and the
|
||||
* weight is not in a non-trunk scope (weight_targeted() states the rule; build_sites and
|
||||
* find_unsupported_uses use exactly that predicate, and the builder may call it mid-declare).
|
||||
* Targets are HF module leaf names ("q_proj", "gate_proj", "in_proj_qkv", ...). A declared weight is
|
||||
* targeted when one of its name-map sources is `<prefix>.<leaf>.weight` with `leaf` in the set and
|
||||
* the weight is in the trunk; weight_targeted() is the one predicate everything here uses.
|
||||
*/
|
||||
#pragma once
|
||||
#include "../rad_core.h"
|
||||
@@ -30,25 +27,23 @@
|
||||
namespace rad {
|
||||
namespace lora {
|
||||
|
||||
/* HF module LEAF names. A std::set so two callers that build it in different orders get the same
|
||||
* answer. Empty targets nothing. */
|
||||
/* HF module leaf names, ordered so every caller builds the same set. Empty targets nothing. */
|
||||
using TargetSet = std::set<std::string>;
|
||||
|
||||
/* ONE CONTIGUOUS RUN OF ONE PEFT MODULE'S ROWS INSIDE THIS SITE'S y, ON THIS RANK.
|
||||
* A module may need SEVERAL parts: in_proj_qkv covers three row parts of the delta net's fused
|
||||
* ssm_inz, and a rank holds a slice of EACH, so its B rows on that rank are three disjoint runs
|
||||
* landing at three y columns. Every part of one module carries the same t_off. */
|
||||
/* One contiguous run of one PEFT module's rows inside this site's y, on this rank. A module may
|
||||
* need several: in_proj_qkv covers three row parts of the delta net's fused ssm_inz and a rank
|
||||
* holds a slice of each, so its B rows there are three runs at three y columns. Every part of one
|
||||
* module has the same t_off. */
|
||||
struct SitePart {
|
||||
std::string module; /* HF module path without ".weight", as the name map spells it */
|
||||
int64_t src_lo = 0, src_hi = 0;/* rows [lo,hi) of the module's full output (= rows of lora_B) */
|
||||
int64_t y_col = 0; /* column of this site's per-rank y where row src_lo lands */
|
||||
int64_t t_off = 0; /* first t column of the module's block = module slot * max_rank */
|
||||
/* ADDED: the weight's row part this run lies in (0 for a weight of one part). */
|
||||
int32_t row_part = 0;
|
||||
int32_t row_part = 0; /* the weight's row part this run lies in (0: a single part) */
|
||||
};
|
||||
|
||||
/* ADDED: one name-map source of the site's weight, in concatenation order -- targeted or not,
|
||||
* because an untargeted source still occupies rows of the fused weight. */
|
||||
/* One name-map source of the site's weight, in concatenation order, targeted or not: an untargeted
|
||||
* source still occupies rows of the fused weight. */
|
||||
struct SiteSource {
|
||||
std::string module; /* source without ".weight" */
|
||||
bool targeted = false;
|
||||
@@ -59,28 +54,20 @@ struct SiteSource {
|
||||
};
|
||||
|
||||
struct Site {
|
||||
uint32_t gemm = 0; /* the gemm op handle (anchor) */
|
||||
uint32_t lora_shrink = 0, lora_expand = 0; /* appended op handles (stream G) */
|
||||
uint32_t gemm = 0; /* the gemm op handle */
|
||||
std::string weight; /* declared weight name */
|
||||
int shard = 0; /* RAD_SHARD_ROW / COL / NONE of the base weight */
|
||||
int64_t K_r = 0, k_lo = 0, K_full = 0; /* x columns on this rank; COL: A columns
|
||||
* [k_lo, k_lo + K_r). K_full 0 = not recorded on
|
||||
* the weight (a COL span): taken from lora_A. */
|
||||
int64_t N_r = 0; /* per-rank y width the gemm writes */
|
||||
int64_t R = 0; /* capacity: n_modules * max_rank (see report:
|
||||
* DESIGN said parts.size() * max_rank) */
|
||||
int64_t R = 0; /* capacity: n_modules * max_rank */
|
||||
std::vector<SitePart> parts; /* empty while !parts_resolved */
|
||||
uint32_t pool_a = 0, pool_b = 0, pool_scale = 0; /* PERSIST buffers; 0 here, the caller's */
|
||||
|
||||
/* ---- ADDED */
|
||||
int32_t pool = -1; /* index of the weight in build_sites' per-weight pool list */
|
||||
uint32_t w = 0; /* the declared weight's handle */
|
||||
std::string op; /* the gemm's op name */
|
||||
int32_t n_modules = 0; /* targeted sources */
|
||||
int32_t max_rank = 0;
|
||||
bool parts_resolved = true; /* false: the fused layout is decided by B rows at load */
|
||||
bool res_unchecked = false; /* gemm_nt_bias: its optional `res` operand is bound at
|
||||
* issue, not at declare -- stream G checks it there */
|
||||
std::vector<SiteSource> sources;
|
||||
/* Per row part g of the weight (P = max(1, n_row_parts) entries): the part's rows in the
|
||||
* container (0 = not recorded: an equal-part single span), this rank's rows [lo, hi) of it in
|
||||
@@ -93,7 +80,6 @@ struct PackedSite {
|
||||
std::vector<uint16_t> a; /* [R, K_r] bf16, zero rows past the module's rank in each block */
|
||||
std::vector<uint16_t> b; /* [N_r, R] bf16, zero outside the module's rows / rank block */
|
||||
std::vector<float> scale; /* [N_r]: the module's alpha/r (alpha/sqrt(r) rsLoRA) on its rows */
|
||||
/* ADDED */
|
||||
int32_t pool = -1;
|
||||
bool empty = true; /* no module of this adapter lands in this pool */
|
||||
};
|
||||
@@ -121,7 +107,6 @@ struct AdapterInfo {
|
||||
std::set<std::string> leaves; /* leaf names present in the tensors (targets present) */
|
||||
std::vector<std::string> absent; /* modules no site on THIS rank took (see check_placed) */
|
||||
int64_t max_module_rank = 0;
|
||||
uint64_t content_hash = 0; /* FNV-1a 64 over the config bytes and the tensors */
|
||||
};
|
||||
|
||||
/* A targeted weight read by an op that is not a LoRA site. The caller refuses at startup. */
|
||||
@@ -165,10 +150,6 @@ int build_sites(const Program& prog, const TargetSet& targets, int max_rank,
|
||||
std::vector<Site>* out, std::string* err,
|
||||
std::vector<std::string>* pool_weights = nullptr);
|
||||
|
||||
/* Every leaf whose every targeted weight in `prog` would be a site: what "all LoRA-capable trunk
|
||||
* linears" means when no adapter names the targets. */
|
||||
TargetSet all_capable_targets(const Program& prog);
|
||||
|
||||
/* One adapter for one rank: `packed` gets one PackedSite per pool (index = Site::pool), in the
|
||||
* slot layout of the pool's sites. Refusals return a negative code and name module, expected and
|
||||
* found. A module with no site on THIS rank is not refused here -- an attention-zero rank holds no
|
||||
|
||||
+2
-3
@@ -29,9 +29,8 @@ int SlotTable::reserve(const std::string& name, int* slot, bool* cooling, std::s
|
||||
*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). */
|
||||
/* The old adapter's requests still run on its slot; the name loads again once they
|
||||
* are 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(),
|
||||
|
||||
+11
-11
@@ -2,22 +2,22 @@
|
||||
* 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:
|
||||
* 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.
|
||||
* A request holds the adapter it names from admission until its reply is done or it is cancelled
|
||||
* (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: no new request can name it (acquire answers
|
||||
* as for a name never loaded), the requests already holding it finish, and the last hold to go
|
||||
* frees the slot. A load of the same name is refused until then.
|
||||
*
|
||||
* 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.
|
||||
* Cooling: a freed slot is written again only after the step loop has gone round
|
||||
* kSlotCoolRounds times (`epoch` counts its rounds). A step that read the slot may still be on
|
||||
* the card when the last hold goes, issued ahead of the readback; an idle round has no step in
|
||||
* flight and a busy round collects one, so with one step issued ahead two rounds suffice.
|
||||
*
|
||||
* 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
|
||||
@@ -85,7 +85,7 @@ private:
|
||||
const std::atomic<uint64_t>* epoch_ = nullptr;
|
||||
};
|
||||
|
||||
/* Step-loop rounds a freed slot waits before it is written again (see COOLING above). */
|
||||
/* Step-loop rounds a freed slot waits before it is written again (see Cooling above). */
|
||||
inline constexpr uint64_t kSlotCoolRounds = 3;
|
||||
|
||||
} /* namespace lora */
|
||||
|
||||
Reference in new issue
Block a user