Archived
Merge branch 'pr1-device' into pr2-engine
This commit is contained in:
commit
06172e98de
49 files changed
+2100
-205
No files matched your search
@@ -119,18 +119,27 @@ inline int MlpFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, co
|
||||
down.reads_x && !(w.ffn.q8_fed && down.probed_i8), false,
|
||||
swiglu_limit));
|
||||
|
||||
/* The fold (decl_gated_fold), an fp8a8 tile, so bf16, f32-scaled, MXFP4, rotated 5-bit, 4-bit
|
||||
* and int8 gate_up weights take the pair, and so does a clamped (swiglu_limit) product.
|
||||
* Declared over int8 codes, only a host kernel would take it, and that would pin the layer's
|
||||
* weights to the host. */
|
||||
if (swiglu_limit <= 0 && !gate_up.bf16 && !gate_up.f32_scale && !gate_up.mx &&
|
||||
!gate_up.paro5 && !gate_up.w4 && !gate_up.i8)
|
||||
/* The fold: decl_gated_fold, an fp8a8 tile, for block-fp8 weights; decl_gated_fold_mx for a
|
||||
* plain MXFP4 one at one to eight rows. bf16, f32-scaled, rotated MXFP4, rotated 5-bit, 4-bit
|
||||
* and int8 gate_up weights take the pair, and so does a clamped (swiglu_limit) product, which
|
||||
* neither fold computes. Declared over int8 codes, only a host kernel would take it, and that
|
||||
* would pin the layer's weights to the host. */
|
||||
const bool fold_ok = swiglu_limit <= 0 && !gate_up.f32_scale;
|
||||
if (fold_ok && !gate_up.bf16 && !gate_up.mx && !gate_up.paro5 && !gate_up.w4 && !gate_up.i8)
|
||||
op_gated = decl_gated_fold(b, g.max_tok, gate_up.N, gate_up.K, gate_up.w, gate_up.ws,
|
||||
{ w.h.q, w.h.s },
|
||||
gq_ffn.want_x
|
||||
? std::initializer_list<rad_buf>{ w.ffn.q, w.ffn.s, w.ffn.x }
|
||||
: std::initializer_list<rad_buf>{ w.ffn.q, w.ffn.s },
|
||||
&gated_rows);
|
||||
else if (fold_ok && gate_up.mx && !gate_up.paro)
|
||||
op_gated = decl_gated_fold_mx(b, g.max_tok, gate_up.N, gate_up.K, gate_up.w, gate_up.ws,
|
||||
gate_up.wref, { w.h.q, w.h.s },
|
||||
gq_ffn.want_x
|
||||
? std::initializer_list<rad_buf>{ w.ffn.q, w.ffn.s,
|
||||
w.ffn.x }
|
||||
: std::initializer_list<rad_buf>{ w.ffn.q, w.ffn.s },
|
||||
&gated_rows);
|
||||
RAD_ARCH_TRY(down.declare(b, nm, g, down_base,
|
||||
g.n_embd, g.n_ff, RAD_SHARD_COL, grp_layer(l),
|
||||
{ src.down }, w.ffn, w.h.x));
|
||||
@@ -179,7 +188,13 @@ inline void MlpFP8::slice(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const
|
||||
}
|
||||
|
||||
inline void MlpFP8::body(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const {
|
||||
if (op_gated && rows <= gated_rows) {
|
||||
if (op_gated && rows <= gated_rows && gate_up.mx) {
|
||||
RAD_ISSUE_N(c, op_gated, rows, brow_slice(w.h.q, r0, rows, g.n_embd),
|
||||
brow_slice(w.h.s, r0, rows, g.n_embd / RAD_FP8_BLOCK), RAD_W(gate_up.w),
|
||||
RAD_W(gate_up.ws), RAD_W(gate_up.wref), brow_slice(w.ffn.q, r0, rows, g.n_ff),
|
||||
brow_slice(w.ffn.s, r0, rows, g.n_ff / RAD_FP8_BLOCK),
|
||||
gq_ffn.want_x ? brow_slice(w.ffn.x, r0, rows, g.n_ff) : RAD_NONE);
|
||||
} else if (op_gated && rows <= gated_rows) {
|
||||
RAD_ISSUE_N(c, op_gated, rows, brow_slice(w.h.q, r0, rows, g.n_embd),
|
||||
brow_slice(w.h.s, r0, rows, g.n_embd / RAD_FP8_BLOCK), RAD_W(gate_up.w),
|
||||
RAD_W(gate_up.ws), brow_slice(w.ffn.q, r0, rows, g.n_ff),
|
||||
|
||||
@@ -106,10 +106,18 @@ struct PleLayer {
|
||||
rad_op op_gate = 0, op_conv = 0, op_add = 0;
|
||||
|
||||
int64_t hn() const { return c.hc * g.n_embd; }
|
||||
/* The read-ahead queue's depth, in steps: the scheduler stages this many steps of the
|
||||
* following prompt per step (batch.cpp, k_ahead_chunks) and the gather hands them to the
|
||||
* reader thread, which prefetches their rows off the step path while the current chunk runs.
|
||||
* Clamped to what `b_ids` was declared to hold, so a scheduler that offers more degrades the
|
||||
* queue's depth, never an overflow. */
|
||||
static constexpr int64_t kAheadChunks = 8;
|
||||
/* The prompt that follows the step, bounded by what `b_ids` holds after the step's rows. */
|
||||
int64_t ahead_rows(const RadBatch* batch) const {
|
||||
const int64_t a = batch->n_ahead;
|
||||
return a <= 0 ? 0 : (a < g.max_tok ? a : g.max_tok);
|
||||
if (a <= 0) return 0;
|
||||
const int64_t cap = kAheadChunks * g.max_tok;
|
||||
return a < cap ? a : cap;
|
||||
}
|
||||
|
||||
int declare(RadBuilder* b, Names& nm, const Geom& geom, const Config& cfg, int layer,
|
||||
@@ -275,10 +283,12 @@ inline int PleLayer::declare(RadBuilder* b, Names& nm, const Geom& geom, const C
|
||||
* follows declared order, where the two ops are adjacent, so a transient could be overwritten
|
||||
* in between. */
|
||||
{
|
||||
/* Twice the step's rows: the step's ids, then those of the prompt that follows
|
||||
* (RadBatch::n_ahead), which the gather's reader prefetches while the step runs. */
|
||||
/* The step's rows, then the read-ahead queue's: up to kAheadChunks further steps of the
|
||||
* following prompt (RadBatch::n_ahead), which the gather's reader prefetches while the
|
||||
* step runs, so a prefill's table rows are read at disk speed off the step path. */
|
||||
RadBufDecl d{};
|
||||
d.dtype = RAD_I32; d.rank = 2; d.shape[0] = 2 * g.max_tok; d.shape[1] = c.heads;
|
||||
d.dtype = RAD_I32; d.rank = 2;
|
||||
d.shape[0] = (1 + kAheadChunks) * g.max_tok; d.shape[1] = c.heads;
|
||||
d.kind = RAD_BUF_PERSIST; d.domain = RAD_DOMAIN_HOST;
|
||||
b_ids = rad_decl_buffer(b, nm.f("ple_ids"), &d);
|
||||
|
||||
|
||||
@@ -1692,6 +1692,28 @@ inline rad_op decl_gated_fold(RadBuilder* b, int64_t rows, int64_t N, int64_t K,
|
||||
return op;
|
||||
}
|
||||
|
||||
/* The same fold over an OCP-MXFP4 [2n, K] gate_up weight (`gemm_nt_q_gated_mx`: codes, the E8M0
|
||||
* plane and that plane again as the per-row reference), declared for M up to min(rows, 8), the band
|
||||
* of libr4d's one-to-eight-row GEMM it is built on. Optional like decl_gated_fold, and asked for
|
||||
* only when a loaded library declares the op: libref does not, so a CPU-only engine takes the pair
|
||||
* without ever naming it. */
|
||||
inline rad_op decl_gated_fold_mx(RadBuilder* b, int64_t rows, int64_t N, int64_t K, rad_weight w,
|
||||
rad_weight ws, rad_weight wref,
|
||||
std::initializer_list<rad_buf> reads,
|
||||
std::initializer_list<rad_buf> writes, int64_t* gated_rows) {
|
||||
const int64_t hi = rows < 8 ? rows : 8;
|
||||
if (hi <= 0 || N % 256 || K % RAD_FP8_BLOCK) return 0;
|
||||
if (!rad_op_declared(b, "gemm_nt_q_gated_mx")) return 0;
|
||||
const rad_op op = rw(b, RAD_OP(b, "gemm_nt_q_gated_mx",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K),
|
||||
RAD_INT("group", RAD_MX_GROUP), RAD_STR("dtype", "mxfp4a8"),
|
||||
RAD_STR("act", "silu")),
|
||||
RAD_WEIGHTS(w, ws, wref)),
|
||||
reads, writes);
|
||||
if (op) *gated_rows = hi;
|
||||
return op;
|
||||
}
|
||||
|
||||
} /* namespace arch */
|
||||
} /* namespace rad */
|
||||
|
||||
|
||||
Reference in new issue
Block a user