core: runtime LoRA issue chain and the program tables it reads

Off by default: no program has a LoRA site until declare builds one, so issue
and the batch derivations do exactly what they did before.
This commit is contained in:
baKon committed 2026-10-16 20:52:09 -05:00
1 parent 51293b269c
commit 50543d931b
6 files changed
+176 -3

No files matched your search

+74 -3
View File
@@ -395,7 +395,11 @@ static long g_iss_n = 0;
* after_op runs on the op's stream, where "every reader of this weight is behind us" can be
* recorded. */
int Ctx::issue(rad_op h, const RadOperand* opd, int n_opd, int64_t n) {
if (!staging_) return issue_body(h, opd, n_opd, n);
if (!staging_) {
const int rc = issue_body(h, opd, n_opd, n);
if (rc < 0 || (size_t)h >= plans_.size() || plans_[h].lora_site < 0) return rc;
return issue_lora(h, opd, n_opd, n);
}
if (step_status_ < 0) return step_status_;
bool resync = false;
const int sb = stager_->before_op(h, cur_stream(), &resync);
@@ -407,9 +411,74 @@ int Ctx::issue(rad_op h, const RadOperand* opd, int n_opd, int64_t n) {
const int sa = stager_->after_op(h, cur_stream());
if (sa < 0)
return abort_step_msg(h, sa, "the weight stager could not release this op's weights");
if ((size_t)h < plans_.size() && plans_[h].lora_site >= 0) return issue_lora(h, opd, n_opd, n);
return rc;
}
/* RUNTIME LoRA: the two ops the core appended for a targeted gemm, issued right behind it on the
* same stream, over the same rows, through the ordinary issue path (bands, tape, hazards).
*
* t = A[slot] . x lora_shrink, x = the gemm's own activation operand(s)
* y += scale . B[slot] . t lora_expand, y = the gemm's own output operand
*
* Here and only here: a row-parallel gemm's y is this rank's PARTIAL sum and the plugin reduces it
* after this returns, so the delta rides the same reduction (B.A.x is linear in x's shards). Only
* the trunk pass: a draft pass runs the drafter's weights, an encoder pass the tower's. A base row
* (slot -1) is not touched by either kernel, so a step with no adapter rows computes exactly what
* the same step without LoRA does. */
int Ctx::issue_lora(rad_op h, const RadOperand* opd, int n_opd, int64_t n) {
if (!batch_ || batch_->draft_pass != 0 || batch_->enc != 0 || batch_->gen != 0) return RAD_OK;
const OpPlan& p = plans_[h];
const LoraIssueSite& s = program_->lora_sites[(size_t)p.lora_site];
if (n == RAD_N_BATCH) n = batch_->n_tok;
if (n <= 0) return RAD_OK;
if (op_domain_[h].load(std::memory_order_relaxed) != RAD_DOMAIN_DEVICE)
return abort_step_msg(h, RAD_E_UNSUPPORTED,
"a LoRA-targeted gemm was placed on the host; its adapter delta has "
"no host path, so the step would compute the base model's output");
if (s.opd_a < 0 || s.opd_a >= n_opd || s.opd_y < 0 || s.opd_y >= n_opd)
return abort_step_msg(h, RAD_E_STATE, "LoRA site: the gemm was issued with fewer operands "
"than its schema names");
const RadOperand& ya = opd[s.opd_y];
const RadOperand& xa = opd[s.opd_a];
if (ya.kind != RAD_OPK_BUF || xa.kind != RAD_OPK_BUF ||
(size_t)ya.handle >= program_->buffers.size() || (size_t)xa.handle >= program_->buffers.size())
return abort_step_msg(h, RAD_E_UNSUPPORTED, "LoRA site: the gemm's activation or output is "
"not a declared buffer");
/* THE ROWS THE GEMM WROTE ARE ROWS OF THE STEP'S TOKENS starting at r0, so the slot array is
* read from r0 too. A row slice offsets by whole rows of the buffer's declared width; a column
* slice offsets by less than one row. Anything else is a view this code cannot place. */
const RadBufDecl& yd = program_->buffers[ya.handle].decl;
const RadBufDecl& xd = program_->buffers[xa.handle].decl;
if (yd.dtype != s.y_dtype || xd.dtype != s.a_dtype)
return abort_step_msg(h, RAD_E_STATE,
fmt("LoRA site: declared for activation %s / output %s, issued with "
"%s / %s", rad_dtype_name(s.a_dtype), rad_dtype_name(s.y_dtype),
rad_dtype_name(xd.dtype), rad_dtype_name(yd.dtype)));
const int64_t yw = yd.rank >= 2 ? yd.shape[yd.rank - 1] : 0;
if (yw <= 0) return abort_step_msg(h, RAD_E_STATE, "LoRA site: the gemm's output has no width");
if (ya.cols == 0 && ya.offset % yw != 0)
return abort_step_msg(h, RAD_E_STATE, "LoRA site: the gemm's output view starts inside a "
"row, so its token rows cannot be told");
const int64_t r0 = ya.offset / yw;
if (r0 < 0 || r0 + n > batch_->n_tok)
return abort_step_msg(h, RAD_E_STATE,
fmt("LoRA site: the gemm wrote rows [%lld, %lld) of a %lld-token step",
(long long)r0, (long long)(r0 + n), (long long)batch_->n_tok));
const LoraPool& pool = program_->lora_pools[(size_t)s.pool];
const rad_buf t = program_->lora_t[(size_t)s.t].lane[lane_ ? 1 : 0];
const RadOperand slot = RAD_B_N(program_->lora_slot_buf, r0, n);
const RadOperand tv = RAD_B_N(t, 0, n);
const RadOperand sh[5] = { xa,
s.opd_a_scale >= 0 && s.opd_a_scale < n_opd ? opd[s.opd_a_scale]
: RAD_NONE,
slot, RAD_B(pool.a), tv };
int rc = issue_body((rad_op)s.shrink, sh, 5, n);
if (rc < 0) return rc;
const RadOperand ex[5] = { tv, slot, RAD_B(pool.b), RAD_B(pool.scale), ya };
return issue_body((rad_op)s.expand, ex, 5, n);
}
int Ctx::issue_body(rad_op h, const RadOperand* opd, int n_opd, int64_t n) {
const auto t_iss0 = g_dbg_issue ? std::chrono::steady_clock::now()
: std::chrono::steady_clock::time_point{};
@@ -512,8 +581,10 @@ int Ctx::issue_body(rad_op h, const RadOperand* opd, int n_opd, int64_t n) {
* depends on the plan's packing. */
{
const BufferInfo& bi = program_->buffers[bh];
if (bi.first_def >= 0 &&
((int32_t)h < bi.first_def || (int32_t)h > bi.last_use))
/* A core-appended op (runtime LoRA) is judged at the op it rides on: it touches
* that op's activation and output inside that op's issue, never after it. */
const int32_t at = p.info && p.info->anchor >= 0 ? p.info->anchor : (int32_t)h;
if (bi.first_def >= 0 && (at < bi.first_def || at > bi.last_use))
return abort_step_msg(h, RAD_E_STATE,
fmt("operand %d names buffer '%s', which this op never "
"declared: the plan gives it ops %d..%d and this is "