Archived
Add Muse Glimmer IQ4R-V4 with quantized DFlash2
This commit is contained in:
1 parent
742191a244
commit
79ac2bd50d
9 files changed
+711
-130
No files matched your search
@@ -0,0 +1,198 @@
|
||||
/* dflash2.h -- wire rad_block_dflash2.h onto a Muse Glimmer trunk.
|
||||
*
|
||||
* Meta's assistant is a plain DFlash (rad_block_muse_dflash.h). incoai's Muse-Glimmer-30B-DFlash2
|
||||
* is the DFlash2 block: candidate selector, grouped convolutions, 8 KV heads. The two are
|
||||
* selected by draft.architectures. Muse's DFlash2 ships output_multiplier and
|
||||
* final_logit_softcapping; the block applies those (Config::omul / Config::scap).
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <arch/rad_block_dflash2.h>
|
||||
#include <arch/rad_block_muse_dflash.h>
|
||||
#include <arch/rad_fp8.h>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace rad {
|
||||
namespace arch {
|
||||
|
||||
inline bool muse_draft_is_dflash2(const RadModelMeta* meta) {
|
||||
const char* da = rad_meta_gets(meta, "draft.architectures", "");
|
||||
return da && std::strstr(da, "DFlash2");
|
||||
}
|
||||
|
||||
/* Same 12 MiB prefetch qwen35 measured on two R9700s. */
|
||||
constexpr int64_t kMuseDf2Prefetch = (int64_t)12 << 20;
|
||||
|
||||
inline int declare_muse_dflash2(Dflash2* ds, Dflash2::Wire* w, rad_kvgroup* kv,
|
||||
RadBuilder* b, Names& nm, const RadModelMeta* meta,
|
||||
const RadBuildCtx* ctx, const Geom& target,
|
||||
const MuseTaps& taps, int base_layer, rad_weight tok_embd,
|
||||
rad_weight head_w) {
|
||||
if (!ds || !w || !kv || !b) return RAD_E_INVAL;
|
||||
Geom dg{};
|
||||
RAD_ARCH_TRY(drafter_geom(dg, target, meta, "DFlash2", /*kv_repl_ok=*/true));
|
||||
const int64_t want_embd = rad_meta_geti(meta, "draft.hidden_size",
|
||||
rad_meta_geti(meta, "draft.dflash_config.hidden_size",
|
||||
dg.n_embd));
|
||||
if (want_embd != dg.n_embd) {
|
||||
fprintf(stderr, "radiance: museglimmer: the DFlash2 drafter reads a %lld-wide trunk and "
|
||||
"this target is %lld wide; they are not the same model\n",
|
||||
(long long)want_embd, (long long)dg.n_embd);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
Dflash2::Config dc{};
|
||||
dc.short_block = 1;
|
||||
dc.prefetch = kMuseDf2Prefetch;
|
||||
dc.n_taps = taps.n;
|
||||
dc.block = ctx->max_spec + 1;
|
||||
dc.taps = rad_meta_geti(meta, "draft.dflash_config.conv_kernel_size", 2);
|
||||
dc.group = rad_meta_geti(meta, "draft.dflash_config.conv_group_size", 16);
|
||||
dc.sel_rank = rad_meta_geti(meta, "draft.dflash_config.selector_rank", 0);
|
||||
dc.sel_topk = rad_meta_geti(meta, "draft.dflash_config.selector_top_k", 0);
|
||||
dc.causal = rad_meta_geti(meta, "draft.is_causal", 0) ? 1 : 0;
|
||||
if (rad_meta_geti(meta, "draft.use_sliding_window", 0))
|
||||
dc.window = rad_meta_geti(meta, "draft.dflash_config.swa_window_size",
|
||||
rad_meta_geti(meta, "draft.sliding_window", 0));
|
||||
if (dc.sel_rank <= 0 || dc.sel_topk <= 0) {
|
||||
fprintf(stderr, "radiance: museglimmer: the DFlash2 drafter states no selector geometry "
|
||||
"(rank %lld, top_k %lld)\n",
|
||||
(long long)dc.sel_rank, (long long)dc.sel_topk);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
|
||||
const long long trained = rad_meta_geti(meta, "draft.dflash_config.block_size", 0);
|
||||
if (dc.block < 2 || (dc.block & (dc.block - 1))) {
|
||||
fprintf(stderr, "radiance: museglimmer: DFlash2's convolution masks the block position "
|
||||
"with block_size - 1, so the query block has to be a power of two. This "
|
||||
"serve asks for %d draft tokens, which makes it %lld; the checkpoint was "
|
||||
"trained at %lld.\n",
|
||||
ctx->max_spec, (long long)dc.block, trained);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
if (trained > 0 && trained != dc.block)
|
||||
rad_note(b, "DFlash2 was trained at block_size %lld (%lld draft tokens) and runs at "
|
||||
"%lld; it accepts less away from its trained block length",
|
||||
trained, trained - 1, (long long)dc.block);
|
||||
|
||||
dc.escale = rad_meta_getf(meta, "draft.dflash_config.input_embedding_scale", 1.0);
|
||||
dc.omul = rad_meta_getf(meta, "draft.dflash_config.output_multiplier", 1.0);
|
||||
dc.scap = rad_meta_getf(meta, "draft.dflash_config.final_logit_softcapping", 0.0);
|
||||
RAD_ARCH_TRY(Dflash2::probe_i8(b, nm, "draft", &dc.act_i8, &dc.act_g8));
|
||||
dc.fc_mapped = true;
|
||||
|
||||
RAD_ARCH_TRY(drafter_kv_group(b, nm, ctx, dg, dc.window, base_layer, kv));
|
||||
|
||||
const int64_t qd = dg.n_head * dg.head_dim;
|
||||
const int64_t kvd = dg.n_head_kv * dg.head_dim;
|
||||
const uint32_t act_dtype = dc.act_i8 ? RAD_I8 : RAD_F8E4M3;
|
||||
const int64_t act_group = dc.act_i8 ? dc.act_g8 : RAD_FP8_BLOCK;
|
||||
const auto act_groups = [act_group](int64_t n) { return (n + act_group - 1) / act_group; };
|
||||
int64_t qr = dg.max_seqs * dc.block;
|
||||
if (qr > dg.max_tok) qr = dg.max_tok;
|
||||
const int64_t sr = dg.max_seqs * dc.steps();
|
||||
const int64_t spad = ((sr + 15) / 16) * 16;
|
||||
|
||||
Dflash2::Wire& dw = *w;
|
||||
dw.src = taps.src;
|
||||
dw.c = decl_b(b, nm.f("df_c"), dg.act_dtype, {dg.max_tok, dg.n_embd});
|
||||
dw.ck = decl_b(b, nm.f("df_ck"), dg.act_dtype, {dg.max_tok, kvd});
|
||||
dw.cv = decl_b(b, nm.f("df_cv"), dg.act_dtype, {dg.max_tok, kvd});
|
||||
dw.x = decl_b(b, nm.f("df_x"), dg.act_dtype, {qr, dg.n_embd});
|
||||
dw.h = decl_b(b, nm.f("df_h"), dg.act_dtype, {qr, dg.n_embd});
|
||||
dw.h2 = decl_b(b, nm.f("df_h2"), dg.act_dtype, {qr, dg.n_embd});
|
||||
dw.coef = decl_b(b, nm.f("df_coef"), dg.act_dtype, {qr, dc.coef_dim(dg.n_embd)});
|
||||
dw.q = decl_b(b, nm.f("df_q"), dg.act_dtype, {qr, qd});
|
||||
dw.k = decl_b(b, nm.f("df_k"), dg.act_dtype, {qr, kvd});
|
||||
dw.v = decl_b(b, nm.f("df_v"), dg.act_dtype, {qr, kvd});
|
||||
dw.attn = decl_b(b, nm.f("df_attn"), dg.act_dtype, {qr, qd});
|
||||
dw.gate_up = decl_b(b, nm.f("df_gate_up"), dg.act_dtype, {qr, 2 * dg.n_ff});
|
||||
dw.ffn = decl_b(b, nm.f("df_ffn"), dg.act_dtype, {qr, dg.n_ff});
|
||||
dw.srcq = decl_b(b, nm.f("df_srcq"), act_dtype,
|
||||
{dg.max_tok, dc.n_taps * dg.n_embd});
|
||||
dw.srcs = decl_b(b, nm.f("df_srcs"), RAD_F32,
|
||||
{dg.max_tok, act_groups(dc.n_taps * dg.n_embd)});
|
||||
dw.cq = decl_b(b, nm.f("df_cq"), act_dtype, {dg.max_tok, dg.n_embd});
|
||||
dw.cs = decl_b(b, nm.f("df_cs"), RAD_F32, {dg.max_tok, act_groups(dg.n_embd)});
|
||||
dw.h2q = decl_b(b, nm.f("df_h2q"), act_dtype, {qr, dg.n_embd});
|
||||
dw.h2s = decl_b(b, nm.f("df_h2s"), RAD_F32, {qr, act_groups(dg.n_embd)});
|
||||
dw.atq = decl_b(b, nm.f("df_atq"), act_dtype, {qr, qd});
|
||||
dw.ats = decl_b(b, nm.f("df_ats"), RAD_F32, {qr, act_groups(qd)});
|
||||
dw.ffq = decl_b(b, nm.f("df_ffq"), act_dtype, {qr, dg.n_ff});
|
||||
dw.ffs = decl_b(b, nm.f("df_ffs"), RAD_F32, {qr, act_groups(dg.n_ff)});
|
||||
dw.sel_h = decl_b(b, nm.f("df_selh"), dg.act_dtype, {sr, dg.n_embd});
|
||||
dw.hp = decl_b(b, nm.f("df_hp"), dg.act_dtype, {sr, dc.sel_rank});
|
||||
dw.dq = decl_b(b, nm.f("df_dq"), RAD_I8, {sr, dg.n_embd});
|
||||
dw.ds = decl_b(b, nm.f("df_ds"), RAD_F32, {sr, 1});
|
||||
dw.dsum = decl_b(b, nm.f("df_dsum"), RAD_F32, {dg.n_embd / 128, spad});
|
||||
dw.dlog = decl_b(b, nm.f("df_dlog"), dg.act_dtype, {sr, Dflash2::head_rows(dg)});
|
||||
{
|
||||
const uint32_t hd = weight_codes_dtype(b, nm.f("output.weight"), RAD_BF16);
|
||||
if (head_w && (hd == RAD_F8E4M3 || hd == RAD_I8) &&
|
||||
(Dflash2::head_sharded(dg) || dg.world <= 1)) {
|
||||
ds->w_lm = head_w;
|
||||
ds->top_w = Dflash2::kRerankR;
|
||||
}
|
||||
}
|
||||
const int64_t tw = ds->w_lm ? ds->top_w : dc.sel_topk;
|
||||
dw.cand = decl_b(b, nm.f("df_cand"), RAD_I32, {sr, tw});
|
||||
dw.unary = decl_b(b, nm.f("df_unary"), RAD_F32, {sr, tw});
|
||||
if (Dflash2::head_sharded(dg)) {
|
||||
dw.dpair = decl_b(b, nm.f("df_dpair"), RAD_F32, {sr, tw, 2});
|
||||
dw.dgath = decl_b(b, nm.f("df_dgath"), RAD_F32, {sr, (int64_t)dg.world * tw, 2});
|
||||
}
|
||||
dw.tokens = decl_b(b, nm.f("dflash_tokens"), RAD_I32, {dg.max_seqs, dc.steps()});
|
||||
if (ctx->draft_coupling) {
|
||||
if (dc.steps() > RAD_MTP_COUPLE_MAX_ROUNDS || dc.sel_topk > RAD_DFLASH_COUPLE_MAX_K ||
|
||||
dc.sel_rank % RAD_DFLASH_EDGE_PARTS || dc.sel_rank > RAD_DFLASH_COUPLE_MAX_R) {
|
||||
fprintf(stderr, "radiance: museglimmer: --draft-coupling on DFlash2 needs at most "
|
||||
"%d drafts, %d candidates and a selector rank that is a multiple of "
|
||||
"%d up to %d\n",
|
||||
RAD_MTP_COUPLE_MAX_ROUNDS, RAD_DFLASH_COUPLE_MAX_K, RAD_DFLASH_EDGE_PARTS,
|
||||
RAD_DFLASH_COUPLE_MAX_R);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
dc.q_temp = 0.6;
|
||||
dw.couple_draws = decl_b(b, nm.f("mtp_couple_draws"), RAD_U32,
|
||||
{dg.max_seqs, RAD_MTP_COUPLE_DRAW_WORDS}, RAD_BUF_PERSIST);
|
||||
dw.couple_state = decl_b(b, nm.f("mtp_couple_state"), RAD_U32,
|
||||
{dc.steps(), dg.max_seqs, RAD_MTP_COUPLE_STATE_WORDS},
|
||||
RAD_BUF_PERSIST);
|
||||
if (!dw.couple_draws || !dw.couple_state) return RAD_E_INVAL;
|
||||
}
|
||||
|
||||
RAD_ARCH_TRY(ds->declare(b, nm, dg, dc, base_layer, *kv, tok_embd, "draft",
|
||||
nm.ckpt("lm_head.weight"), dw));
|
||||
|
||||
RadDrafterDecl drf{};
|
||||
drf.name = "dflash2";
|
||||
drf.kind = RAD_DRAFT_BLOCK;
|
||||
drf.depth = ctx->max_spec;
|
||||
drf.block = dc.block;
|
||||
drf.proposal = dw.tokens;
|
||||
drf.proposal_pitch = dc.steps();
|
||||
drf.window = dc.window;
|
||||
drf.mask_token = (int32_t)rad_meta_geti(meta, "draft.dflash_config.mask_token_id", -1);
|
||||
if (drf.mask_token < 0) {
|
||||
fprintf(stderr, "radiance: museglimmer: this DFlash2 checkpoint declares no "
|
||||
"dflash_config.mask_token_id\n");
|
||||
return RAD_E_FORMAT;
|
||||
}
|
||||
RAD_ARCH_TRY(rad_declare_drafter(b, &drf));
|
||||
if (ds->block_lo > 0) RAD_ARCH_TRY(rad_drafter_short_block(b, ds->block_lo));
|
||||
|
||||
rad_note(b, "DFlash2 drafter: %lld layers at absolute %d..%d, %lld heads / %lld kv x %lld, "
|
||||
"ffn %lld, %s attention with a %lld-token window, %d trunk tap(s); one forward "
|
||||
"proposes %lld tokens through the candidate selector",
|
||||
(long long)dg.n_layer, base_layer, base_layer + (int)dg.n_layer - 1,
|
||||
(long long)dg.n_head, (long long)dg.n_head_kv, (long long)dg.head_dim,
|
||||
(long long)dg.n_ff, dc.causal ? "causal" : "non-causal", (long long)dc.window,
|
||||
taps.n, (long long)dc.steps());
|
||||
if (dc.omul != 1.0 || dc.scap > 0.0)
|
||||
rad_note(b, "DFlash2 logits: output_multiplier %g, softcap %g", dc.omul, dc.scap);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
} /* namespace arch */
|
||||
} /* namespace rad */
|
||||
@@ -57,6 +57,7 @@
|
||||
#include <arch/rad_block_muse_dflash.h>
|
||||
#include <arch/rad_calib_full.h>
|
||||
#include <arch/rad_museglimmer_chat.h>
|
||||
#include "dflash2.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
@@ -120,13 +121,12 @@ struct Model {
|
||||
rad_op op_media_rows = 0;
|
||||
MuseTower vit{};
|
||||
|
||||
/* ---- the DFlash drafter (arch/common/rad_block_muse_dflash.h): declared last, only when the
|
||||
* container carries one and the serve asks for drafts. It reads the trunk's residual stream at
|
||||
* `draft.target_layer_ids`, keeps its own KV group, and proposes `block_size - 1` tokens a
|
||||
* pass for the trunk to verify. */
|
||||
bool have_df = false;
|
||||
/* ---- Meta's DFlash or incoai's DFlash2, selected by draft.architectures. */
|
||||
bool have_df = false, have_df2 = false;
|
||||
MuseDflash df{};
|
||||
MuseDflash::Wire dfw{};
|
||||
Dflash2 df2{};
|
||||
Dflash2::Wire df2w{};
|
||||
MuseTaps taps{};
|
||||
rad_kvgroup kv_draft = 0;
|
||||
|
||||
@@ -653,12 +653,17 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
if (m.media)
|
||||
RAD_ARCH_TRY(m.vit.declare(b, meta, ctx, nm, g.n_embd, (int)g.n_layer));
|
||||
|
||||
/* ---- the DFlash drafter, declared last: its passes run after the whole trunk's, and the
|
||||
* planner lays lifetimes out in declaration order. */
|
||||
/* ---- the drafter is declared last so its buffers cannot overlap trunk temporaries. */
|
||||
if (m.taps.n > 0) {
|
||||
RAD_ARCH_TRY(declare_muse_dflash(&m.df, &m.dfw, &m.kv_draft, b, nm, meta, ctx, g, m.taps,
|
||||
(int)g.n_layer, m.w_tok, m.w_lm_head));
|
||||
m.have_df = true;
|
||||
if (muse_draft_is_dflash2(meta)) {
|
||||
RAD_ARCH_TRY(declare_muse_dflash2(&m.df2, &m.df2w, &m.kv_draft, b, nm, meta, ctx, g,
|
||||
m.taps, (int)g.n_layer, m.w_tok, m.w_lm_head));
|
||||
m.have_df2 = true;
|
||||
} else {
|
||||
RAD_ARCH_TRY(declare_muse_dflash(&m.df, &m.dfw, &m.kv_draft, b, nm, meta, ctx, g,
|
||||
m.taps, (int)g.n_layer, m.w_tok, m.w_lm_head));
|
||||
m.have_df = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- notes for --debug-graph. */
|
||||
@@ -703,6 +708,11 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
/* A draft pass runs only the drafter: >0 the query pass on the staged block, <0 the context
|
||||
* pass that projects and stores K/V for the tokens the trunk just committed. */
|
||||
if (batch->draft_pass != 0) {
|
||||
if (m.have_df2) {
|
||||
if (batch->draft_pass > 0) m.df2.step(c, batch);
|
||||
else m.df2.context(c, batch);
|
||||
return;
|
||||
}
|
||||
if (!m.have_df) {
|
||||
rad_step_fail(c, "this serve stages a draft pass but the container carries no drafter");
|
||||
return;
|
||||
@@ -738,8 +748,9 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
const int32_t* table = kvb ? kvb->block_table : nullptr;
|
||||
const int32_t* used = kvb ? kvb->seqused : nullptr;
|
||||
|
||||
/* -- attention. */
|
||||
/* -- attention. The first Hessian sees the normalized input consumed by q/gate/k/v. */
|
||||
RAD_ISSUE(c, l.op_norm, brows(m.b_x, T), RAD_W(l.w_norm), brows(m.b_h, T));
|
||||
if (l.cs_in >= 0) m.calib.tap(c, (size_t)l.cs_in, T);
|
||||
RAD_ISSUE(c, l.op_q, brows(m.b_h, T), RAD_W(l.w_q), brows(m.b_q, T));
|
||||
RAD_ISSUE(c, l.op_gate, brows(m.b_h, T), RAD_W(l.w_gate), brows(m.b_gate, T));
|
||||
RAD_ISSUE(c, l.op_k, brows(m.b_h, T), RAD_W(l.w_k), brows(m.b_k, T));
|
||||
@@ -763,6 +774,7 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
brows(m.b_attn, T));
|
||||
RAD_ISSUE_N(c, l.op_sig, T * m.g.n_head, brows(m.b_gate, T), brows(m.b_gate, T));
|
||||
RAD_ISSUE(c, l.op_mul, brows(m.b_attn, T), brows(m.b_gate, T), brows(m.b_attn, T));
|
||||
if (l.cs_o >= 0) m.calib.tap(c, (size_t)l.cs_o, T);
|
||||
RAD_ISSUE(c, l.op_o, brows(m.b_attn, T), RAD_W(l.w_o), brows(m.b_h, T));
|
||||
if (l.op_ar) RAD_ISSUE_N(c, l.op_ar, T * m.g.n_embd, brows(m.b_h, T), RAD_NONE);
|
||||
RAD_ISSUE(c, l.op_post, brows(m.b_h, T), RAD_W(l.w_post), brows(m.b_h, T));
|
||||
@@ -770,8 +782,10 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
|
||||
/* -- feed-forward. */
|
||||
RAD_ISSUE(c, l.op_fnorm, brows(m.b_x, T), RAD_W(l.w_fnorm), brows(m.b_h, T));
|
||||
if (l.cs_mlp >= 0) m.calib.tap(c, (size_t)l.cs_mlp, T);
|
||||
RAD_ISSUE(c, l.op_gu, brows(m.b_h, T), RAD_W(l.w_gate_up), brows(m.b_gu, T));
|
||||
RAD_ISSUE(c, l.op_act, brows(m.b_gu, T), brows(m.b_ffn, T));
|
||||
if (l.cs_down >= 0) m.calib.tap(c, (size_t)l.cs_down, T);
|
||||
RAD_ISSUE(c, l.op_down, brows(m.b_ffn, T), RAD_W(l.w_down), brows(m.b_h, T));
|
||||
if (l.op_far) RAD_ISSUE_N(c, l.op_far, T * m.g.n_embd, brows(m.b_h, T), RAD_NONE);
|
||||
RAD_ISSUE(c, l.op_fpost, brows(m.b_h, T), RAD_W(l.w_fpost), brows(m.b_h, T));
|
||||
@@ -783,6 +797,7 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
|
||||
RAD_ISSUE(c, m.op_final_norm,
|
||||
brows(m.b_x, batch->n_tok), RAD_W(m.w_out_norm), brows(m.b_h, batch->n_tok));
|
||||
if (m.cs_head >= 0) m.calib.tap(c, (size_t)m.cs_head, T);
|
||||
m.calib.after_step(c, T);
|
||||
|
||||
/* The lm_head runs only on the rows the sampler asked for; a mid-prompt prefill chunk has
|
||||
@@ -806,8 +821,14 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
static int probe(const RadModelMeta* meta, RadArchProbe* out) {
|
||||
if (!meta || !out) return RAD_E_INVAL;
|
||||
if (MuseTower::present(meta)) out->enc_merge = 4;
|
||||
/* The drafter denoises `block_size` positions and the anchor row proposes nothing, so the
|
||||
* draft width is block_size - 1. It is trained for that one block, hence fixed. */
|
||||
/* DFlash2 reads block_size from dflash_config; its anchor row proposes nothing. */
|
||||
if (muse_draft_is_dflash2(meta)) {
|
||||
const long long bs = rad_meta_geti(meta, "draft.dflash_config.block_size", 16);
|
||||
out->draft_depth = bs > 1 ? (int)(bs - 1) : 15;
|
||||
out->draft_depth_fixed = 1;
|
||||
return RAD_OK;
|
||||
}
|
||||
/* Meta DFlash denoises `block_size` positions; the anchor proposes nothing. */
|
||||
const long long bs = rad_meta_geti(meta, "draft.block_size", 0);
|
||||
if (bs > 1) {
|
||||
out->draft_depth = (int)(bs - 1);
|
||||
|
||||
Reference in new issue
Block a user