Archived
core: runtime LoRA declare, startup adapters, model routing and a per-adapter prefix key
--lora NAME=PATH, --max-loras, --max-lora-rank, --lora-target-modules. Off unless named: no site, no pool, no op, and every prefix hash is what it was.
This commit is contained in:
1 parent
86cbc59063
commit
ba406ace1b
18 files changed
+684
-4
No files matched your search
@@ -159,6 +159,16 @@ public:
|
||||
* an io_uring ring, which must not stop a sandboxed conversion. */
|
||||
void set_instances(bool on) { instances_ = on; }
|
||||
|
||||
/* RUNTIME LoRA (core/lora/): the HF module leaves an adapter may target. Set before the
|
||||
* plugin declares, so a FUSED GEMM over a targeted weight -- a GEMM no adapter delta can ride
|
||||
* on, the gated gate_up fold -- is declined as it is declared and the plugin emits the unfused
|
||||
* chain it already has for a library without the fused kernel. Null: nothing is declined. */
|
||||
void set_lora_targets(const std::set<std::string>* t) { lora_targets_ = t; }
|
||||
/* The read and write sets an op declared (rad_op_reads / rad_op_writes), for the LoRA declare,
|
||||
* which needs the dtypes of the activation and the output a targeted GEMM was declared over. */
|
||||
const std::vector<rad_buf>& op_read_set (rad_op h) const { return op_bufs_[h].reads; }
|
||||
const std::vector<rad_buf>& op_write_set(rad_op h) const { return op_bufs_[h].writes; }
|
||||
|
||||
Program& program() { return prog_; }
|
||||
const Program& program() const { return prog_; }
|
||||
|
||||
@@ -199,6 +209,7 @@ private:
|
||||
/* Parallel to prog_.ops: the buffers each op declared it reads and writes. */
|
||||
struct OpBufUse { std::vector<rad_buf> reads, writes; };
|
||||
std::vector<OpBufUse> op_bufs_;
|
||||
const std::set<std::string>* lora_targets_ = nullptr;
|
||||
|
||||
/* Buffers touched from the second lane (rad_buf_concurrent). Their declared op range does not
|
||||
* bound when their bytes are in use, so they are live for the whole program. */
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
* which makes fusion a selection rather than a compiler pass (spec §2.3).
|
||||
*/
|
||||
#include "rad_build.h"
|
||||
#include "lora/lora.h"
|
||||
#include "../device/device.h"
|
||||
|
||||
#include <algorithm>
|
||||
@@ -702,6 +703,33 @@ rad_op Builder::decl_op(const char* opname, const RadParam* p, int n_p,
|
||||
}
|
||||
}
|
||||
|
||||
/* RUNTIME LoRA: A FUSED GEMM OVER A TARGETED WEIGHT IS DECLINED. Every `gemm_*` op other than
|
||||
* the four an adapter delta can follow (lora::op_is_site) fuses something into its epilogue --
|
||||
* the gated fold quantises the SiLU product in place -- so the delta has no y to land in. The
|
||||
* op is recorded resolved to nothing, exactly as a library without the kernel would leave it,
|
||||
* and the plugin falls back to the unfused chain it emits for that library. The decision reads
|
||||
* only the weight's own declaration and name map, so the sizing declare makes the same one. */
|
||||
bool lora_declined = false;
|
||||
if (lora_targets_ && n_w > 0 && oi.op.rfind("gemm_", 0) == 0 && oi.op != "gemm_nt" &&
|
||||
oi.op != "gemm_nt_bias" && oi.op != "gemm_nt_q" && oi.op != "gemm_nt_q_bias") {
|
||||
for (int i = 0; i < n_w && !lora_declined; ++i)
|
||||
if (w[i] > 0 && w[i] < prog_.weights.size() &&
|
||||
lora::weight_targeted(prog_, w[i], *lora_targets_))
|
||||
lora_declined = true;
|
||||
}
|
||||
|
||||
if (lora_declined) {
|
||||
Band none;
|
||||
none.hi = oi.ranged_key.empty() ? INT64_MAX : oi.range_hi;
|
||||
for (int d = 0; d < RAD_N_DOMAINS; ++d)
|
||||
none.miss[d] = "declined: its weight is a runtime-LoRA target, and a fused GEMM has no "
|
||||
"output for the adapter's delta (the plugin runs the unfused chain)";
|
||||
oi.bands.assign(1, none);
|
||||
prog_.ops.push_back(std::move(oi));
|
||||
op_bufs_.emplace_back();
|
||||
return RAD_NULL_HANDLE;
|
||||
}
|
||||
|
||||
if (ref_) {
|
||||
const size_t at = prog_.ops.size();
|
||||
if (at >= ref_->ops.size() || ref_->ops[at].op != oi.op)
|
||||
|
||||
+16
-1
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
#include "build/startup.h"
|
||||
#include "build/rad_build.h"
|
||||
#include "lora/declare.h"
|
||||
#include "format/tunecache.h"
|
||||
#include "plugin/registry.h"
|
||||
|
||||
@@ -340,7 +341,8 @@ std::vector<std::string> CheckpointSources::refused() const {
|
||||
|
||||
int rad_tools_declare(const LoadedPlugins& lp, const RadModelMeta& meta, const RadBuildCtx& ctx,
|
||||
Program* out, const std::function<int(RadBuilder*, int64_t)>& also,
|
||||
const Program* ref, const WeightSourceFn& sources, bool instances) {
|
||||
const Program* ref, const WeightSourceFn& sources, bool instances,
|
||||
const LoraDeclare* lora) {
|
||||
if (!out) return RAD_E_INVAL;
|
||||
|
||||
Registry& r = rad_tools_registry();
|
||||
@@ -355,6 +357,8 @@ int rad_tools_declare(const LoadedPlugins& lp, const RadModelMeta& meta, const R
|
||||
if (ref) b.set_reference(ref);
|
||||
if (sources) b.set_sources(sources);
|
||||
b.set_instances(instances);
|
||||
const bool with_lora = lora && lora->cfg && lora->cfg->enabled();
|
||||
if (with_lora) b.set_lora_targets(&lora->targets);
|
||||
|
||||
int s = ap->arch_declare(&b, &meta, &ctx);
|
||||
if (s < 0) {
|
||||
@@ -363,6 +367,17 @@ int rad_tools_declare(const LoadedPlugins& lp, const RadModelMeta& meta, const R
|
||||
return s;
|
||||
}
|
||||
|
||||
/* RUNTIME LoRA, after the plugin and before the sampler: the sites read the plugin's finished
|
||||
* graph, and the appended ops go through the same resolution as everything else. */
|
||||
if (with_lora) {
|
||||
b.set_lora_targets(nullptr);
|
||||
s = lora_declare(b, *lora);
|
||||
if (s < 0) {
|
||||
for (const auto& e : b.errors()) RAD_ERR(" %s", e.c_str());
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
/* The sampler declares into the same builder before finish(). An unresolved sampler op is not
|
||||
* a declare failure (a deployment that never sends `typical_p` needs no typical kernel), so a
|
||||
* negative return here is structural. Skipped for embedding programs and image generators,
|
||||
|
||||
@@ -108,7 +108,7 @@ int rad_tools_declare(const LoadedPlugins& plugins, const RadModelMeta& meta,
|
||||
const RadBuildCtx& ctx, Program* out,
|
||||
const std::function<int(RadBuilder*, int64_t logits_width)>& also = {},
|
||||
const Program* ref = nullptr, const WeightSourceFn& sources = {},
|
||||
bool instances = true);
|
||||
bool instances = true, const struct LoraDeclare* lora = nullptr);
|
||||
|
||||
/* A container's entries as the source of every declaration's encoding. `f` must outlive the
|
||||
* declare. */
|
||||
|
||||
Reference in new issue
Block a user