Archived
radiance 1.7.0
- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves every shipped container - Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime - /v1/messages works with Claude Code as it ships (gearwave00001) - a long agent conversation keeps its cached history when the client rewrites its last turn (manueloverride) - a stream's first event goes out with the first token, so llama-benchy measures prefill right - opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42) - four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b) - NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA adapters (baKon) - many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool arguments and the CPU's vision and audio encoders - plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
This commit is contained in:
1 parent
b068190326
commit
1e968f6e1d
739 files changed
+128447
-84632
No files matched your search
@@ -3,27 +3,18 @@
|
||||
* rmsnorm -> quant_act_fp8 -> gate_up gemm_nt_q -> silu_mul -> quant_act_fp8 -> down gemm_nt_q
|
||||
* -> [all_reduce] -> residual add
|
||||
*
|
||||
* THE ARCHITECTURE IS rad_block_mlp.h's AND IS DOCUMENTED THERE -- the gate/up fusion, why the
|
||||
* all-reduce is emitted by the plugin, what the residual add is for. This file is that block with
|
||||
* two ops added and two changed, and it repeats none of it: a second copy of the prose is a second
|
||||
* thing to keep true.
|
||||
* rad_block_mlp.h documents the architecture. Differences:
|
||||
*
|
||||
* What differs, and only this:
|
||||
* - Both projections are LinearFP8 (rad_fp8.h): an E4M3 weight with a bf16 [N/128][K/128]
|
||||
* scale plane, through `gemm_nt_q` at dtype fp8a8.
|
||||
* - Each takes an E4M3 activation quantised every step (the checkpoint's `activation_scheme`
|
||||
* is `dynamic`, so no activation scale is stored).
|
||||
* - Activations between ops stay bf16, so `Geom::dtype` stays "bf16" and every other op
|
||||
* resolves as in the bf16 block.
|
||||
*
|
||||
* - the two projections are LinearFP8 (rad_fp8.h): an E4M3 weight, a bf16 [N/128][K/128] scale
|
||||
* plane declared as a weight of its own, and `gemm_nt_q` at dtype fp8a8 instead of `gemm_nt`.
|
||||
* - each of them is preceded by a `quant_act_fp8` over its input, because an fp8a8 GEMM takes an
|
||||
* E4M3 activation and the checkpoint's `activation_scheme` is `dynamic` -- there is no
|
||||
* activation scale on disk and one is computed every step.
|
||||
* - the norm, the activation and the collective are UNCHANGED. Activations between ops are still
|
||||
* bf16; only the two GEMM operands are narrow. So `Geom::dtype` stays "bf16" for this block and
|
||||
* every op in it but the two linears resolves exactly as it does in the bf16 plugin.
|
||||
*
|
||||
* The gate/up fusion survives the format, which is not obvious and is the reason it is kept: the
|
||||
* two sources are [17408,5120] each and their scale planes [136,40] each, and concatenating both
|
||||
* along dim 0 gives [34816,5120] against [272,40] -- which is the grid, because 34816/128 is 272.
|
||||
* A fusion whose parts were not whole multiples of 128 rows would silently misalign every scale
|
||||
* after the seam.
|
||||
* The gate/up fusion survives the format because each part is a whole number of 128-row blocks:
|
||||
* two [17408, 5120] sources with [136, 40] scale planes concatenate to [34816, 5120] and
|
||||
* [272, 40]. Parts not on a 128-row boundary would misalign every scale after the seam.
|
||||
*/
|
||||
#ifndef RAD_BLOCK_MLP_FP8_H
|
||||
#define RAD_BLOCK_MLP_FP8_H
|
||||
@@ -41,9 +32,8 @@ struct MlpFP8 {
|
||||
ActFP8 ffn{}; /* [max_tok, n_ff] */
|
||||
};
|
||||
|
||||
/* The checkpoint side. `gate`, `up` and `down` name the `.weight` tensors; a block-fp8
|
||||
* checkpoint's scale planes sit beside them and the checkpoint reader pairs them, so a caller
|
||||
* cannot spell one of the pair and forget the other. */
|
||||
/* Checkpoint names. `gate`, `up` and `down` name the `.weight` tensors; the checkpoint reader
|
||||
* pairs each with its scale plane. */
|
||||
struct Src {
|
||||
const char* norm = nullptr;
|
||||
const char* gate = nullptr;
|
||||
@@ -60,37 +50,25 @@ struct MlpFP8 {
|
||||
NormQuantFP8 nq_h{};
|
||||
GateQuantFP8 gq_ffn{};
|
||||
rad_op op_ar = 0, op_add = 0;
|
||||
/* ...AND THE gate_up PROJECTION WITH THE GATE AND THE QUANTISER IN ITS EPILOGUE.
|
||||
*
|
||||
* `gq_ffn`'s only input is `gate_up`'s only output. Unfused it is one of the larger remaining
|
||||
* items in a decode step, and roughly half of that is the launch and half the second pass over
|
||||
* a [M, 2*n_ff] bf16 tensor the GEMM's transposing store had just scattered. Folded, that
|
||||
* tensor is never written at all.
|
||||
*
|
||||
* DECLARED BESIDE THE PAIR AND NOT INSTEAD OF IT, exactly as LinearFP8's matvec is: the fold
|
||||
* is the narrow tile's epilogue and serves M <= 64, so a prefill chunk takes the pair. The two
|
||||
* are byte-identical -- r4d_selftest holds them to it, and ref_test holds the reference to
|
||||
* the same claim -- so this is two spellings of one arithmetic and not two answers. */
|
||||
/* gate_up with the gate and quantiser in its epilogue, so the [M, 2*n_ff] bf16 product is
|
||||
* never written (unfused, the extra launch and second pass are a sizeable decode cost).
|
||||
* Declared beside the pair, not instead of it: the fold is the narrow tile's epilogue and
|
||||
* serves M <= 64, so prefill chunks take the pair. Byte-identical, as r4d_selftest and
|
||||
* ref_test check. */
|
||||
rad_op op_gated = 0;
|
||||
int64_t gated_rows = 0;
|
||||
/* ...AND WHETHER THE COLLECTIVE AT THE END OF THIS BLOCK IS THE NEXT BLOCK'S FIRST PASS.
|
||||
* `ar_in` says this block's own norm absorbs its PREDECESSOR's all-reduce; `ar_out` says the
|
||||
* block after it absorbs THIS one's, so this one must not issue it. They are the residual
|
||||
* fold's two flags again, one boundary later, and they are separate for the same reason: a
|
||||
* block is declared without knowing what follows it. On EITHER wire -- the fused kernel
|
||||
* carries both payloads -- and both sides ask ar_fused_ok(g, T), so the two cannot disagree
|
||||
* about whether the collective has already been issued. */
|
||||
/* The next block absorbs this block's closing all-reduce, so this one must not issue it.
|
||||
* Counterpart of `ar_in`; both sides ask ar_fused_ok(g, T), so they agree. */
|
||||
bool ar_out = false;
|
||||
|
||||
|
||||
/* `fold` says this block has a PREDECESSOR whose delta is still in `w.h.x`, so its norm does
|
||||
* the residual add that block would otherwise have issued. False for the first block of a
|
||||
* chain, whose residual stream comes straight from the embedding. See rad_fp8.h. */
|
||||
/* `fold`: a predecessor's delta is still in `w.h.x` and this norm does its residual add
|
||||
* (false for the first block of a chain). `add_out`: issue the trailing add here. See
|
||||
* rad_fp8.h. */
|
||||
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Wire& wire,
|
||||
const Src& src, bool fold = false, bool add_out = true,
|
||||
int ar_in = kArNone, bool ar_out_ = false);
|
||||
void step(RadCtx* c, const RadBatch* batch) const;
|
||||
/* The block over the first `T` rows of its buffers (MoeFP8::step_rows says who asks). */
|
||||
/* The block over the first `T` rows of its buffers (see MoeFP8::step_rows). */
|
||||
void step_rows(RadCtx* c, int64_t T) const { slice(c, T, 0, T); }
|
||||
/* One row slice of the whole block; `rows <= 0` means the rest of the step. */
|
||||
void slice(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const;
|
||||
@@ -106,41 +84,29 @@ inline int MlpFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, co
|
||||
w_norm = decl_w(b, nm.f("blk.%d.ffn_norm.weight", l), RAD_F32, {g.n_embd},
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
|
||||
|
||||
/* THE NORM, THE QUANTISER AND THE RESIDUAL ADD ARE ONE OP, and rad_fp8.h's NormQuantFP8 says
|
||||
* why: both were a launch and a second pass over a row this kernel already had in registers.
|
||||
* Folded, the norm reads the PREVIOUS block's delta out of the shared `h` buffer and adds it
|
||||
* into the stream; unfolded it reads the stream directly and the block ends with its own add. */
|
||||
/* Norm, quantise and residual add in one op (rad_fp8.h: NormQuantFP8). Folded, it adds the
|
||||
* previous block's delta from `h` into the stream; unfolded it reads the stream directly. */
|
||||
RAD_ARCH_TRY(nq_h.declare(b, g, w_norm, fold ? w.h.x : w.x, w.h, g.n_embd, fold ? w.x : 0,
|
||||
0, ar_in));
|
||||
RAD_ARCH_TRY(gate_up.declare(b, nm, g, nm.f("blk.%d.ffn_gate_up", l),
|
||||
2 * g.n_ff, g.n_embd, RAD_SHARD_ROW, grp_layer(l),
|
||||
{ src.gate, src.up }, w.h, w.gate_up, 0, { g.n_ff, g.n_ff }));
|
||||
|
||||
/* THE bf16 SILU PRODUCT IS WRITTEN ONLY FOR A READER: a down projection that reads the bf16
|
||||
* plane (LinearFP8::reads_x). A block-fp8 down_proj takes the codes and their scale alone. */
|
||||
/* The bf16 silu product is written only if the down projection reads the bf16 plane
|
||||
* (LinearFP8::reads_x); a block-fp8 down_proj reads only codes and scales. */
|
||||
const char* down_base = nm.f("blk.%d.ffn_down", l);
|
||||
RAD_ARCH_TRY(down.probe(b, nm, g, down_base, { src.down }));
|
||||
RAD_ARCH_TRY(gq_ffn.declare(b, g, w.gate_up, w.ffn, g.n_ff, "silu", 0, down.reads_x));
|
||||
|
||||
/* The fold, over the same weights and into the same activation the pair writes. Not
|
||||
* RAD_ARCH_TRY: a plugin without the row is the ordinary case and the pair still runs. It
|
||||
* multiplies fp8 codes by a block-fp8 gate_up, so a plain gate_up has no fold -- and neither
|
||||
* has an MXFP4 one: libr4d's gated fold is an fp8a8 tile, and the pair serves it (a rotated 5-bit one neither). */
|
||||
{
|
||||
const int64_t hi = g.max_tok < 64 ? g.max_tok : 64;
|
||||
if (hi > 0 && !gate_up.bf16 && !gate_up.mx && !gate_up.paro5) {
|
||||
op_gated = rw(b, RAD_OP(b, "gemm_nt_q_gated",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", gate_up.N),
|
||||
RAD_INT("K", gate_up.K),
|
||||
RAD_INT("group", RAD_FP8_BLOCK),
|
||||
RAD_STR("dtype", "fp8a8"), RAD_STR("act", "silu")),
|
||||
RAD_WEIGHTS(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 });
|
||||
if (op_gated) gated_rows = hi;
|
||||
}
|
||||
}
|
||||
/* The fold (decl_gated_fold), an fp8a8 tile, so bf16, MXFP4 and rotated 5-bit gate_up weights
|
||||
* take the pair. */
|
||||
if (!gate_up.bf16 && !gate_up.mx && !gate_up.paro5)
|
||||
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);
|
||||
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));
|
||||
@@ -154,7 +120,7 @@ inline int MlpFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, co
|
||||
RAD_NOWEIGHTS),
|
||||
{w.h.x}, {w.h.x});
|
||||
|
||||
/* Only where the NEXT block's norm will not do it; see rad_fp8.h's NormQuantFP8. */
|
||||
/* Only when the next block's norm will not add (rad_fp8.h: NormQuantFP8). */
|
||||
if (add_out)
|
||||
op_add = rw(b, RAD_OP(b, "add",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
|
||||
@@ -165,14 +131,11 @@ inline int MlpFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, co
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ONE ROW SLICE OF THE BLOCK. `rows` defaults to the whole step, and at r0 = 0 with rows = T every
|
||||
* operand is byte-for-byte the one the unsliced form built -- `brow_slice` at offset 0 is `brows`.
|
||||
*
|
||||
* Every op here is per row: the norm and the quantiser group along K within a row, the GEMMs give
|
||||
* each output row its own accumulation, and the fused all-reduce closes every reduction over one
|
||||
* row (r4d_ar.cpp says so where it caps the block count). So a slice computes exactly the rows it
|
||||
* names and nothing about the arithmetic depends on how the chunk was cut -- PROVIDED the cut
|
||||
* obeys the band rule in core/runtime/ctx.h. */
|
||||
/* One row slice of the block; r0 = 0, rows = T is the whole step (`brow_slice` at offset 0 is
|
||||
* `brows`). Every op is per row: the norm and quantiser group along K within a row, the GEMMs
|
||||
* accumulate each output row independently, and the fused all-reduce reduces within a row
|
||||
* (r4d_ar.cpp). So results do not depend on the cut, provided it obeys the band rule in
|
||||
* core/runtime/ctx.h. */
|
||||
inline void MlpFP8::slice(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const {
|
||||
if (rows <= 0) rows = T - r0;
|
||||
nq_h.step(c, T, r0, rows);
|
||||
|
||||
Reference in new issue
Block a user