arch: take the LoRA plain-bf16 view only when an adapter targets the module
9bb3c3e6 set plain_bf16_view on attn_output, ssm_out and the shared-expert
down for every container. A rotated int8 weight then declares gemm_nt over a
relaid bf16 view, which no default kernel joins, so Flash-Next containers fail
at startup with no adapter configured (#587, the serving side of #581).
The builder already knows the runtime targets before arch_declare. Expose that
as rad_lora_source_targeted (the same predicate as lora::source_targeted) and
set the view only when the checkpoint source is one of them. With no adapter
those modules keep gemm_nt_q i8a8h.
This commit is contained in:
1 parent
97bd578a7a
commit
5a80ee2f8f
13 files changed
+168
-16
No files matched your search
@@ -1148,6 +1148,14 @@ int rad_lora_root(RadBuilder* b, const char* native, const char* spellings);
|
||||
int rad_lora_alias(RadBuilder* b, const char* native, const char* from, const char* to,
|
||||
int64_t from_lo, int64_t from_hi, int64_t to_lo);
|
||||
|
||||
/* Is checkpoint tensor `src` a runtime-LoRA target of this build? 1 when an adapter configured
|
||||
* for this startup targets it, 0 otherwise (no adapter, a different module, a tower, the drafter,
|
||||
* or a generator source outside a declared root). The same predicate as lora::source_targeted:
|
||||
* the leaf of a `.weight` name. A plugin asks before the weight exists, and sets a LoRA-only
|
||||
* declaration (the plain bf16 view of a rotated int8 trunk weight) only on 1. Null or an empty
|
||||
* name is 0. Additive export; RAD_ABI_VERSION does not move. */
|
||||
int rad_lora_source_targeted(RadBuilder* b, const char* src);
|
||||
|
||||
/* ================================================================== the audio generator */
|
||||
/* A program that makes audio from text: an autoregressive language model writes the piece frame by
|
||||
* frame, with classifier-free guidance (each request runs a conditional and an unconditional
|
||||
|
||||
@@ -553,11 +553,13 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
|
||||
}
|
||||
}
|
||||
|
||||
/* qwen4exp_lora: self_attn.o_proj is a LoRA-targeted module kind. Over a rotated int8
|
||||
* container it runs as a plain bf16 GEMM over the kernel-relaid weight view (rad_fp8.h's
|
||||
* LinearFP8::plain_bf16_view); every other container and the drafter's same-leaf siblings
|
||||
/* qwen4exp_lora: self_attn.o_proj is a LoRA module kind. Over a rotated int8 container
|
||||
* it runs as a plain bf16 GEMM over the kernel-relaid weight view (LinearFP8::plain_bf16_view)
|
||||
* only when a runtime adapter targets this source. With none it keeps gemm_nt_q i8a8h: the
|
||||
* view's plain gemm_nt has no layout in the default libraries, so declaring it for every
|
||||
* container stops startup (#587). Every other encoding, and the drafter's same-leaf siblings,
|
||||
* declare exactly what the tree declares. */
|
||||
o.plain_bf16_view = true;
|
||||
o.plain_bf16_view = src.o && rad_lora_source_targeted(b, src.o);
|
||||
RAD_ARCH_TRY(o.declare(b, nm, g, nm.f("blk.%d.attn_output", l), g.n_embd, q_dim,
|
||||
RAD_SHARD_COL, grp_layer(l), { src.o }, w.attn, w.h.x,
|
||||
0, {}, o_span_lo, o_span_hi));
|
||||
|
||||
@@ -372,9 +372,9 @@ inline int GdnFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, const Con
|
||||
}
|
||||
|
||||
RAD_ARCH_TRY(q_o.declare(b, g, w.o, v_dim));
|
||||
/* qwen4exp_lora: linear_attn.out_proj is a LoRA-targeted module kind; see the gated attn
|
||||
* block's o.declare (LinearFP8::plain_bf16_view). */
|
||||
out.plain_bf16_view = true;
|
||||
/* qwen4exp_lora: linear_attn.out_proj is a LoRA module kind. The plain bf16 view is
|
||||
* taken only when a runtime adapter targets this source (see the gated attn block). */
|
||||
out.plain_bf16_view = s.out && rad_lora_source_targeted(b, s.out);
|
||||
RAD_ARCH_TRY(out.declare(b, nm, g, nm.f("blk.%d.ssm_out", l), g.n_embd, v_dim,
|
||||
RAD_SHARD_COL, grp_layer(l), { s.out }, w.o, w.h.x, 0, {},
|
||||
cfg.uneven() ? cfg.v_first * cfg.head_v : 0,
|
||||
|
||||
@@ -1571,10 +1571,11 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
|
||||
? src.shared_mlp_up
|
||||
: nm.ckpt("%s.up_proj.weight", src.shared);
|
||||
/* An architecture that sized the gate's code pair from this weight has probed it. */
|
||||
/* qwen4exp_lora: mlp.shared_expert.down_proj is a LoRA-targeted module kind; see the
|
||||
* gated attn block's o.declare (LinearFP8::plain_bf16_view). Probing it as the view
|
||||
* (reads_x, not probed_i8) makes the SwiGLU producer write the bf16 plane below. */
|
||||
sh_down.plain_bf16_view = true;
|
||||
/* qwen4exp_lora: mlp.shared_expert.down_proj is a LoRA module kind. The plain bf16
|
||||
* view is taken only when a runtime adapter targets this source (see the gated attn
|
||||
* block). Probing it as the view (reads_x, not probed_i8) makes the SwiGLU producer write
|
||||
* the bf16 plane below; with no adapter the probe stays the int8 one. */
|
||||
sh_down.plain_bf16_view = shared_down && rad_lora_source_targeted(b, shared_down);
|
||||
if (!sh_down.mapped) RAD_ARCH_TRY(sh_down.probe(b, nm, g, down_base, { shared_down }));
|
||||
/* The code pair the gate writes, E4M3 or int8 (ActFP8::q8_fed); an int8 down projection
|
||||
* that quantises for itself also writes the int8 pair on this lane. */
|
||||
|
||||
@@ -1088,6 +1088,8 @@ inline bool linear_is_w8h(const RadEncoding& e) { return linear_w8h_form(e) != W
|
||||
* encoding's `codes` and `scale` planes (read as plain bf16, which the reading kernel's
|
||||
* layout/relayout hook produces at load: FWHT_128(codes * scale), e.g. the lora_plain plugin)
|
||||
* and its product as a plain `gemm_nt` over the activation's bf16 plane, into the same output.
|
||||
* The shared blocks set the flag only when rad_lora_source_targeted says a runtime adapter
|
||||
* targets that checkpoint source. Unset, the linear keeps its stock declaration.
|
||||
*
|
||||
* Eligible encodings are exactly what such a hook accepts: affine, fwht128, i8 codes [x 1] with
|
||||
* a bf16 scale per [1 x 128], two planes and nothing else. IQ8R (u8 per 256 under a bf16 second
|
||||
|
||||
@@ -131,6 +131,8 @@ public:
|
||||
int lora_root(const char* native, const char* spellings);
|
||||
int lora_alias(const char* native, const char* from, const char* to, int64_t from_lo,
|
||||
int64_t from_hi, int64_t to_lo);
|
||||
/* 1 when `src` is a runtime-LoRA target (lora::source_targeted); 0 when none is configured. */
|
||||
int lora_source_targeted(const char* src) const;
|
||||
int drafter_short_block(int64_t block);
|
||||
int drafter_tree(rad_buf parents);
|
||||
int route_table(rad_buf table);
|
||||
|
||||
@@ -2235,6 +2235,11 @@ int Builder::lora_root(const char* native, const char* spellings) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int Builder::lora_source_targeted(const char* src) const {
|
||||
if (!lora_targets_ || !src || !*src) return 0;
|
||||
return lora::source_targeted(prog_, *lora_targets_, src) ? 1 : 0;
|
||||
}
|
||||
|
||||
int Builder::lora_alias(const char* native, const char* from, const char* to, int64_t from_lo,
|
||||
int64_t from_hi, int64_t to_lo) {
|
||||
if (finished_) {
|
||||
@@ -2918,6 +2923,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_lora_source_targeted(RadBuilder* b, const char* src) {
|
||||
if (!b || !src) return 0;
|
||||
return B(b)->lora_source_targeted(src);
|
||||
}
|
||||
|
||||
int rad_drafter_tree(RadBuilder* b, rad_buf parents) {
|
||||
if (!b) return RAD_E_INVAL;
|
||||
return B(b)->drafter_tree(parents);
|
||||
|
||||
+8
-4
@@ -144,7 +144,7 @@ struct MapIndex {
|
||||
/* Is checkpoint tensor `src` an adapter target? A token model: its leaf is in the target list and
|
||||
* it is in the trunk. A generator: it is under a declared root, and its leaf is in the list or the
|
||||
* list is "*". */
|
||||
bool source_targeted(const Program& prog, const TargetSet& targets, const std::string& src) {
|
||||
bool source_hit(const Program& prog, const TargetSet& targets, const std::string& src) {
|
||||
std::string leaf;
|
||||
if (module_of(src, &leaf).empty()) return false;
|
||||
if (!gen_scope(prog)) return targets.count(leaf) && !excluded_source(src);
|
||||
@@ -161,7 +161,7 @@ bool targeted_core(const Program& prog, const WeightInfo& w,
|
||||
if (excluded_weight(prog, w)) return false;
|
||||
for (const RadNameMap* m : maps)
|
||||
for (int k = 0; k < m->n_src && k < 8; ++k)
|
||||
if (m->src[k] && source_targeted(prog, targets, m->src[k])) return true;
|
||||
if (m->src[k] && source_hit(prog, targets, m->src[k])) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -505,7 +505,7 @@ int make_site(const Program& prog, const MapIndex& mi, const TargetSet& targets,
|
||||
* that is is not recorded. Usable only when exactly one alternative is targeted. */
|
||||
std::vector<std::string> hit;
|
||||
for (const std::string& x : srcs)
|
||||
if (source_targeted(prog, targets, x)) hit.push_back(x);
|
||||
if (source_hit(prog, targets, x)) hit.push_back(x);
|
||||
if (hit.size() != 1)
|
||||
return fail(err, RAD_E_UNSUPPORTED,
|
||||
fmt("weight '%s' is one of %zu alternative checkpoint tensors and %zu of "
|
||||
@@ -522,7 +522,7 @@ int make_site(const Program& prog, const MapIndex& mi, const TargetSet& targets,
|
||||
ss.module = module_of(x, &leaf);
|
||||
if (ss.module.empty()) ss.module = x;
|
||||
ss.module += piece[si];
|
||||
ss.targeted = source_targeted(prog, targets, x);
|
||||
ss.targeted = source_hit(prog, targets, x);
|
||||
if (ss.targeted) ss.slot = slot++;
|
||||
s.sources.push_back(ss);
|
||||
}
|
||||
@@ -567,6 +567,10 @@ int make_site(const Program& prog, const MapIndex& mi, const TargetSet& targets,
|
||||
} /* namespace */
|
||||
|
||||
/* ================================================================== predicates */
|
||||
bool source_targeted(const Program& prog, const TargetSet& targets, const std::string& src) {
|
||||
return source_hit(prog, targets, src);
|
||||
}
|
||||
|
||||
bool weight_targeted(const Program& prog, rad_weight w, const TargetSet& targets) {
|
||||
if (targets.empty() || w == 0 || w >= prog.weights.size()) return false;
|
||||
/* A generator's fused GEMMs are kept: its scope is the plain GEMMs (build_sites). */
|
||||
|
||||
@@ -138,6 +138,13 @@ struct UnsupportedUse {
|
||||
* A routed expert IS targeted when its leaf is (so its moe op is reported, never skipped). */
|
||||
bool weight_targeted(const Program& prog, rad_weight w, const TargetSet& targets);
|
||||
|
||||
/* Is checkpoint tensor `src` an adapter target? The leaf of a `.weight` name, so a caller can ask
|
||||
* before the weight exists. A token model: the leaf is in the list and the source is in the trunk
|
||||
* (not mtp/draft, a vision or audio tower, embed_tokens or lm_head). A generator: the source is
|
||||
* under a declared root, and the leaf is in the list or the list is "*". On a token model "*"
|
||||
* matches nothing. */
|
||||
bool source_targeted(const Program& prog, const TargetSet& targets, const std::string& src);
|
||||
|
||||
/* Is this op a LoRA-capable GEMM over weight handle `w` (as its `b` operand)? `why` says why not.
|
||||
* gemm_nt; gemm_nt_bias without `act`; gemm_nt_q without `pb1`/`pb2`, with K == the weight's K,
|
||||
* over a weight whose known encoding has no `transform`; gemm_nt_q_bias (same encoding rule). */
|
||||
|
||||
@@ -58,6 +58,10 @@ the architecture against patched copies of four SDK arch headers (a `radiance-ho
|
||||
engine source is touched). That step is arch-specific and is not in this change. If a plugin could
|
||||
override `declare()` for named module kinds, the whole path would live out of tree with no shadow.
|
||||
|
||||
In tree, the shared fp8 blocks (`attn_output`, `ssm_out`, `ffn_down_shexp`) set that plain view
|
||||
only when a runtime adapter targets the checkpoint source (`rad_lora_source_targeted`). With no
|
||||
adapter those weights keep `gemm_nt_q` i8a8h, which is what a Flash-Next container starts on.
|
||||
|
||||
## 5. Cost and verification
|
||||
|
||||
On a 2x R9700 gfx1201 rig against an unmodified `qwen3.8-next-flash-iqr-v4` container, 96 GEMMs
|
||||
|
||||
+37
-1
@@ -146,8 +146,10 @@ struct RadBuilder {
|
||||
std::string generator_name;
|
||||
|
||||
/* Adapter key names (rad_lora_root, rad_lora_alias): (native, spellings), and each alias as
|
||||
* (native, from, to, from_lo, from_hi, to_lo). */
|
||||
* (native, from, to, from_lo, from_hi, to_lo). Runtime targets (rad_lora_source_targeted):
|
||||
* module leaf names, empty when no adapter is configured. */
|
||||
std::vector<std::pair<std::string, std::string>> lora_roots;
|
||||
std::set<std::string> lora_targets;
|
||||
struct LoraAliasRec { std::string native, from, to; int64_t from_lo, from_hi, to_lo; };
|
||||
std::vector<LoraAliasRec> lora_aliases;
|
||||
|
||||
@@ -367,6 +369,40 @@ extern "C" int rad_lora_alias(RadBuilder* b, const char* native, const char* fro
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* Mirrors lora::source_targeted. A token model matches the leaf and excludes towers, mtp, draft,
|
||||
* embed_tokens and lm_head; "*" matches nothing there. A generator (a declared root) matches "*"
|
||||
* or the leaf, and only under a root. */
|
||||
extern "C" int rad_lora_source_targeted(RadBuilder* b, const char* src) {
|
||||
if (!b || !src || !*src || b->lora_targets.empty()) return 0;
|
||||
const std::string s(src);
|
||||
if (s.size() < 8 || s.compare(s.size() - 7, 7, ".weight") != 0) return 0;
|
||||
const std::string mod = s.substr(0, s.size() - 7);
|
||||
if (mod.empty()) return 0;
|
||||
const size_t dot = mod.rfind('.');
|
||||
const std::string leaf = dot == std::string::npos ? mod : mod.substr(dot + 1);
|
||||
auto excluded = [&]() {
|
||||
if (leaf == "embed_tokens" || leaf == "lm_head") return true;
|
||||
static const char* tower[] = { "visual", "vision_tower", "vision_model", "audio_tower",
|
||||
"audio_model", "embed_vision", "embed_audio",
|
||||
"multi_modal_projector", "mm_projector" };
|
||||
for (size_t at = 0; at < s.size();) {
|
||||
size_t d = s.find('.', at);
|
||||
if (d == std::string::npos) d = s.size();
|
||||
const std::string c = s.substr(at, d - at);
|
||||
if (at == 0 && (c == "mtp" || c == "draft")) return true;
|
||||
for (const char* t : tower) if (c == t) return true;
|
||||
if (d == s.size()) break;
|
||||
at = d + 1;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
if (b->lora_roots.empty()) return !excluded() && b->lora_targets.count(leaf) ? 1 : 0;
|
||||
if (!b->lora_targets.count("*") && !b->lora_targets.count(leaf)) return 0;
|
||||
for (const auto& r : b->lora_roots)
|
||||
if (!r.first.empty() && s.compare(0, r.first.size(), r.first) == 0) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Recorded: gemma4's prefill overlap names the buffers its second lane reduces in place. */
|
||||
extern "C" int rad_buf_concurrent(RadBuilder* b, rad_buf buf) {
|
||||
if (!b || !buf) return RAD_E_INVAL;
|
||||
|
||||
@@ -11254,6 +11254,52 @@ TEST(qwen35_w8a8_feeds_its_rotated_codes_from_the_producers_where_libr4d_serves_
|
||||
}
|
||||
}
|
||||
|
||||
/* The plain bf16 view (#552) is a runtime-LoRA declaration, not the container's. With no adapter
|
||||
* the 36 trunk linears stay gemm_nt_q i8a8h. An adapter on o_proj switches only the two attention
|
||||
* outputs; one on out_proj switches only the six delta-net outputs. q_proj is a target the blocks
|
||||
* do not detour, and a drafter source of the same leaf is not a trunk target. */
|
||||
TEST(qwen35_w8a8_plain_bf16_view_follows_the_runtime_lora_target) {
|
||||
MetaKV mk = q35w8a8_meta();
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
auto count = [&](const std::set<std::string>& targets) {
|
||||
struct C { int rc = 0, rot = 0, view_o = 0, view_ssm = 0; };
|
||||
C c;
|
||||
RadBuilder b;
|
||||
w8a8_encs(&b);
|
||||
b.lora_targets = targets;
|
||||
c.rc = qwen35_w8a8::declare(&b, mk.meta(), &ctx);
|
||||
if (c.rc != RAD_OK) return c;
|
||||
for (const RecOp& o : b.ops) {
|
||||
if (o.op == "gemm_nt_q" && pstr(&o, "dtype") == "i8a8h") ++c.rot;
|
||||
if (o.op != "gemm_nt" || o.w.empty() || !o.w[0]) continue;
|
||||
const std::string& n = b.weights[o.w[0] - 1].name;
|
||||
c.view_o += n.find("attn_output.weight") != std::string::npos;
|
||||
c.view_ssm += n.find("ssm_out.weight") != std::string::npos;
|
||||
}
|
||||
return c;
|
||||
};
|
||||
const int trunk = 2 * 4 + 6 * 2 + 8 * 2;
|
||||
const auto none = count({});
|
||||
REQUIRE_EQ(none.rc, RAD_OK);
|
||||
CHECK_EQ(none.rot, trunk);
|
||||
CHECK_EQ(none.view_o, 0);
|
||||
CHECK_EQ(none.view_ssm, 0);
|
||||
const auto q = count({ "q_proj" });
|
||||
REQUIRE_EQ(q.rc, RAD_OK);
|
||||
CHECK_EQ(q.rot, trunk);
|
||||
CHECK_EQ(q.view_o, 0);
|
||||
const auto o = count({ "o_proj" });
|
||||
REQUIRE_EQ(o.rc, RAD_OK);
|
||||
CHECK_EQ(o.view_o, 2);
|
||||
CHECK_EQ(o.view_ssm, 0);
|
||||
CHECK_EQ(o.rot, trunk - 2);
|
||||
const auto ssm = count({ "out_proj" });
|
||||
REQUIRE_EQ(ssm.rc, RAD_OK);
|
||||
CHECK_EQ(ssm.view_o, 0);
|
||||
CHECK_EQ(ssm.view_ssm, 6);
|
||||
CHECK_EQ(ssm.rot, trunk - 6);
|
||||
}
|
||||
|
||||
/* What the int8 build refuses: a trunk that is not int8 (no stated encoding, which a block-fp8
|
||||
* plugin reads as fp8), a plain int8 trunk (the descriptor names the rotation, and a plain weight
|
||||
* served behind the rotated quantiser would be wrong), and a container stated for another format
|
||||
|
||||
@@ -873,6 +873,20 @@ TEST(weight_targeted_reads_only_what_exists_at_the_moment) {
|
||||
const rad_weight w = rad_decl_weight(&b, "blk.0.attn_q.weight", &d);
|
||||
REQUIRE(w != 0);
|
||||
CHECK(!lora::weight_targeted(b.program(), w, t)); /* no name map yet */
|
||||
CHECK(lora::source_targeted(b.program(), t, "model.layers.0.self_attn.q_proj.weight"));
|
||||
b.set_lora_targets(&t);
|
||||
CHECK_EQ(rad_lora_source_targeted(&b, "model.layers.0.self_attn.q_proj.weight"), 1);
|
||||
CHECK_EQ(rad_lora_source_targeted(&b, "model.layers.0.self_attn.k_proj.weight"), 0);
|
||||
CHECK_EQ(rad_lora_source_targeted(&b, "mtp.layers.0.self_attn.q_proj.weight"), 0);
|
||||
CHECK_EQ(rad_lora_source_targeted(&b, "model.visual.q_proj.weight"), 0);
|
||||
const lora::TargetSet head{ "embed_tokens", "lm_head" };
|
||||
b.set_lora_targets(&head);
|
||||
CHECK_EQ(rad_lora_source_targeted(&b, "model.embed_tokens.weight"), 0);
|
||||
CHECK_EQ(rad_lora_source_targeted(&b, "lm_head.weight"), 0);
|
||||
b.set_lora_targets(&t);
|
||||
CHECK_EQ(rad_lora_source_targeted(&b, nullptr), 0);
|
||||
b.set_lora_targets(nullptr);
|
||||
CHECK_EQ(rad_lora_source_targeted(&b, "model.layers.0.self_attn.q_proj.weight"), 0);
|
||||
REQUIRE_EQ(AR::map_copy(&b, "blk.0.attn_q.weight", "model.layers.0.self_attn.q_proj.weight"), RAD_OK);
|
||||
CHECK(lora::weight_targeted(b.program(), w, t));
|
||||
CHECK(!lora::weight_targeted(b.program(), w, { "k_proj" }));
|
||||
@@ -881,6 +895,22 @@ TEST(weight_targeted_reads_only_what_exists_at_the_moment) {
|
||||
CHECK(!lora::weight_targeted(b.program(), 999, t));
|
||||
}
|
||||
|
||||
TEST(lora_source_targeted_on_a_generator_is_the_declared_root) {
|
||||
const RadModelMeta meta = llama_meta();
|
||||
const RadBuildCtx ctx = ctx_for(0, 1);
|
||||
Builder b(empty_registry(), meta, ctx);
|
||||
const lora::TargetSet star{ "*" };
|
||||
b.set_lora_targets(&star);
|
||||
CHECK_EQ(rad_lora_source_targeted(&b, "diffusion_model.blocks.0.attn.q.weight"), 0);
|
||||
REQUIRE_EQ(rad_lora_root(&b, "diffusion_model", "-"), RAD_OK);
|
||||
CHECK_EQ(rad_lora_source_targeted(&b, "diffusion_model.blocks.0.attn.q.weight"), 1);
|
||||
CHECK_EQ(rad_lora_source_targeted(&b, "text_encoder.blocks.0.attn.q.weight"), 0);
|
||||
const lora::TargetSet q{ "q" };
|
||||
b.set_lora_targets(&q);
|
||||
CHECK_EQ(rad_lora_source_targeted(&b, "diffusion_model.blocks.0.attn.q.weight"), 1);
|
||||
CHECK_EQ(rad_lora_source_targeted(&b, "diffusion_model.blocks.0.attn.k.weight"), 0);
|
||||
}
|
||||
|
||||
TEST(scopes_that_are_never_targets) {
|
||||
const RadModelMeta meta = llama_meta(); /* n_layers 2 */
|
||||
const RadBuildCtx ctx = ctx_for(0, 1);
|
||||
|
||||
Reference in new issue
Block a user