Merge main into laya-arch-plugin

This commit is contained in:
Brandon Bertelsen committed 2026-10-29 13:45:18 -04:00
commit ec243c2a7d
189 files changed
+11212 -7060

No files matched your search

+83 -5
View File
@@ -1134,6 +1134,30 @@ int Builder::decl_name_map(const RadNameMap* m) {
/* The buffer the core samples from. A second call is refused rather than winning: a target and
* its drafter declaring into one builder (spec §10) both have logits, and keeping the second would
* sample the draft distribution. The drafter's logits are reached through its scope. */
int Builder::declare_scores(rad_buf hidden, rad_weight table) {
if (finished_) { err(RAD_E_STATE, "declare_scores after declare finished"); return RAD_E_STATE; }
if (hidden == RAD_NULL_HANDLE || (size_t)hidden >= prog_.buffers.size()) {
err(RAD_E_INVAL, "declare_scores: %u is not a declared buffer", (unsigned)hidden);
return RAD_E_INVAL;
}
if (table == RAD_NULL_HANDLE || (size_t)table >= prog_.weights.size()) {
err(RAD_E_INVAL, "declare_scores: %u is not a declared weight", (unsigned)table);
return RAD_E_INVAL;
}
const RadBufDecl& h = prog_.buffers[hidden].decl;
const auto& w = prog_.weights[table];
if (h.dtype != RAD_BF16 || h.rank != 2 || w.decl.dtype != RAD_BF16 || w.decl.rank != 2 ||
w.decl.shape[1] != h.shape[1]) {
err(RAD_E_INVAL, "declare_scores: the hidden rows ('%s') and the table ('%s') must be bf16 "
"[rows, n_embd] and [n_vocab, n_embd]", prog_.buffers[hidden].name.c_str(),
w.name.c_str());
return RAD_E_INVAL;
}
prog_.score_hidden = hidden;
prog_.score_table = table;
return RAD_OK;
}
int Builder::declare_logits(rad_buf b) {
if (finished_) { err(RAD_E_STATE, "declare_logits after declare finished"); return RAD_E_STATE; }
if (b == RAD_NULL_HANDLE || (size_t)b >= prog_.buffers.size()) {
@@ -1943,7 +1967,10 @@ int Builder::declare_drafter(const RadDrafterDecl* d) {
int64_t block = d->block;
if (d->kind == RAD_DRAFT_BLOCK) {
if (block == 0) block = (int64_t)d->depth + 1;
if (block < 2 || block < (int64_t)d->depth || block > (int64_t)d->depth + 1) {
/* A tree's (rad_drafter_tree) node count is not its block's draft count: more nodes
* than draft positions (a wider tree) or fewer (a block longer than the tree is deep on
* most steps). Checked at finish, once the tree is or is not declared. */
if (block < 2) {
err(RAD_E_INVAL, "declare_drafter '%s': block %lld rows at depth %d. The head runs "
"over the last %d rows of the block, so the block is either %d rows "
"(the anchor predicts nothing) or %d (it does).",
@@ -2077,17 +2104,48 @@ int Builder::drafter_short_block(int64_t block) {
}
const int64_t full = prog_.drafter.block > 0 ? prog_.drafter.block : prog_.drafter.depth + 1;
const int64_t lead = full - prog_.drafter.depth; /* rows ahead of the proposing ones */
if (block >= full || block - lead < 1) {
/* A tree (rad_drafter_tree, declared first) grows a tree of its own over the shorter pass, up
* to block - 1 nodes whatever its own node count, so it needs only two rows. */
if (prog_.drafter_tree ? (block < 2 || block >= full) : (block >= full || block - lead < 1)) {
err(RAD_E_INVAL, "drafter_short_block '%s': %lld rows. The short block must be shorter "
"than the drafter's own (%lld) and still propose a token (more than %lld "
"rows).", prog_.drafter_name.c_str(), (long long)block, (long long)full,
(long long)lead);
"than the drafter's own (%lld) and still propose a token (%s %lld rows).",
prog_.drafter_name.c_str(), (long long)block, (long long)full,
prog_.drafter_tree ? "at least" : "more than",
(long long)(prog_.drafter_tree ? 2 : lead));
return RAD_E_INVAL;
}
prog_.drafter_short_block = block;
return RAD_OK;
}
/* A block drafter whose proposal is a tree (rad_builder.h). */
int Builder::drafter_tree(rad_buf parents) {
if (finished_) {
err(RAD_E_STATE, "drafter_tree after declare finished");
return RAD_E_STATE;
}
if (prog_.drafter.kind != RAD_DRAFT_BLOCK) {
err(RAD_E_STATE, "drafter_tree: %s. Only a BLOCK drafter's one pass proposes every node at "
"once; declare it with rad_declare_drafter first.",
prog_.drafter.kind == RAD_DRAFT_NONE ? "no drafter is declared"
: "the declared drafter is a SERIAL head");
return RAD_E_STATE;
}
if (parents == RAD_NULL_HANDLE || (size_t)parents >= prog_.buffers.size()) {
err(RAD_E_INVAL, "drafter_tree '%s': parents buffer %u is not a declared buffer",
prog_.drafter_name.c_str(), (unsigned)parents);
return RAD_E_INVAL;
}
if (prog_.buffers[parents].decl.dtype != RAD_I32) {
err(RAD_E_DTYPE, "drafter_tree '%s': parents buffer '%s' is not i32",
prog_.drafter_name.c_str(), prog_.buffers[parents].name.c_str());
return RAD_E_DTYPE;
}
prog_.drafter_tree = true;
prog_.drafter_parents = parents;
return RAD_OK;
}
void Builder::note_v(const char* f, va_list ap) {
char msg[1024];
vsnprintf(msg, sizeof msg, f ? f : "", ap);
@@ -2442,6 +2500,16 @@ int Builder::finish() {
w.source.c_str(), rad_dtype_name(w.decl.dtype));
}
if (prog_.drafter.kind == RAD_DRAFT_BLOCK && !prog_.drafter_tree &&
(prog_.drafter.block < prog_.drafter.depth ||
prog_.drafter.block > (int64_t)prog_.drafter.depth + 1))
err(RAD_E_INVAL, "declare_drafter '%s': block %lld rows at depth %d. The head runs over the "
"last %d rows of the block, so the block is either %d rows (the anchor "
"predicts nothing) or %d (it does); only a tree (rad_drafter_tree) "
"proposes a node count other than its block's drafts.",
prog_.drafter_name.c_str(), (long long)prog_.drafter.block, prog_.drafter.depth,
prog_.drafter.depth, prog_.drafter.depth + 1, prog_.drafter.depth);
if (instances_) finish_instances();
fill_tune_instances();
finish_weight_uses();
@@ -2599,6 +2667,11 @@ int rad_declare_logits(RadBuilder* b, rad_buf logits) {
return B(b)->declare_logits(logits);
}
int rad_declare_scores(RadBuilder* b, rad_buf hidden, rad_weight table) {
if (!b) return RAD_E_INVAL;
return B(b)->declare_scores(hidden, table);
}
int rad_declare_pooled(RadBuilder* b, rad_buf pooled) {
if (!b) return RAD_E_INVAL;
return B(b)->declare_pooled(pooled);
@@ -2630,6 +2703,11 @@ int rad_lora_alias(RadBuilder* b, const char* native, const char* from, const ch
return B(b)->lora_alias(native, from, to, from_lo, from_hi, to_lo);
}
int rad_drafter_tree(RadBuilder* b, rad_buf parents) {
if (!b) return RAD_E_INVAL;
return B(b)->drafter_tree(parents);
}
int rad_chain_on(void) { return rad_dev_chain_enabled(); }
int rad_drafter_short_block(RadBuilder* b, int64_t block) {