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
@@ -1,5 +1,5 @@
|
||||
# A plugin like any other: built here inside radiance's tree, or on its own against an installed
|
||||
# radiance (`cmake -S arch/lfm2_bf16 -B build -DCMAKE_PREFIX_PATH=<radiance prefix>`).
|
||||
# Built in radiance's tree, or standalone against an installed radiance
|
||||
# (`cmake -S arch/lfm2_bf16 -B build -DCMAKE_PREFIX_PATH=<radiance prefix>`); see cmake/RadAddPlugin.cmake.
|
||||
if(NOT COMMAND rad_add_plugin)
|
||||
cmake_minimum_required(VERSION 3.21)
|
||||
project(lfm2_bf16 LANGUAGES CXX)
|
||||
|
||||
@@ -2,30 +2,29 @@
|
||||
*
|
||||
* ============================== THE SHAPE ==============================
|
||||
*
|
||||
* A hybrid of two layer kinds, in the order the checkpoint's `layer_types` lists them -- d1-3B's
|
||||
* schedule is irregular (attention at 2, 5, 9, 13, ...), so it is read, never derived:
|
||||
* A hybrid of two layer kinds, in the order the checkpoint's `layer_types` lists them (read, not
|
||||
* derived: d1-3B's schedule is irregular, attention at 2, 5, 9, 13, ...):
|
||||
*
|
||||
* conv in_proj -> [B | C | x], y = out_proj(C * conv(B * x)), where conv is a
|
||||
* depthwise causal convolution of width `conv_L_cache` with no activation
|
||||
* full_attention GQA with per-head RMS norms on q and k, NeoX rotary over the whole head
|
||||
*
|
||||
* each followed by a SwiGLU feed-forward (w1 the gate, w3 the up, w2 the down). Every norm is a
|
||||
* plain RMSNorm -- `x_hat * w`, not Gemma's `1 + w` -- and the final one is `embedding_norm`. At
|
||||
* plain RMSNorm (`x_hat * w`, not Gemma's `1 + w`), and the final one is `embedding_norm`. At
|
||||
* d1-3B: 30 layers (10 attention), hidden 2048, 32 heads over 8 at head_dim 64, ffn 10752,
|
||||
* vocabulary 128000 with the lm_head TIED to the embedding, conv width 3.
|
||||
* vocabulary 128000 with the lm_head tied to the embedding, conv width 3.
|
||||
*
|
||||
* LFM2-VL nests the same text model under `model.language_model` and adds a SigLIP2 tower and a
|
||||
* projector (siglip2.h), declared when the container carries their weights and the deployment
|
||||
* asked for media; the projector's rows replace the image placeholders' embeddings, which rotate
|
||||
* at their token positions like any other token. A text-only LFM2 checkpoint keeps the text model
|
||||
* under `model`. Which one this is is read off the config (a VL checkpoint has a `text_config`),
|
||||
* not off the model's name.
|
||||
* under `model`. Which one this is comes from the config (a VL checkpoint has a `text_config`),
|
||||
* not the model's name.
|
||||
*
|
||||
* ============================== WHAT IT DOES NOT SERVE ==============================
|
||||
*
|
||||
* Tensor parallelism: one rank. A 3B model has no need of a second card, and the vocabulary
|
||||
* shard, the head split and the all-reduces would be code no deployment runs. A drafter, MoE
|
||||
* (LFM2-8B-A1B), conv bias, or a checkpoint without the rotary: each is refused by name.
|
||||
* Tensor parallelism (a 3B model needs one card). A drafter, MoE (LFM2-8B-A1B), conv bias, or a
|
||||
* checkpoint without the rotary: each is refused by name.
|
||||
*/
|
||||
#include <arch/rad_arch.h>
|
||||
#include <arch/rad_block_mlp.h>
|
||||
@@ -96,7 +95,7 @@ static bool text_bool(const RadModelMeta* m, const char* key, bool dflt) {
|
||||
return std::strcmp(v, "true") == 0 || std::strcmp(v, "1") == 0;
|
||||
}
|
||||
|
||||
/* `layer_types`, one word a layer. The container flattens the list space-separated. */
|
||||
/* `layer_types`, one word per layer, space-separated in the container. */
|
||||
static int read_schedule(const RadModelMeta* m, int64_t n_layer, std::vector<Layer>* out) {
|
||||
const char* s = text_key(m, "layer_types");
|
||||
if (!s || !*s) {
|
||||
@@ -142,8 +141,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
m.nm = Names(ctx->scope);
|
||||
Names& nm = m.nm;
|
||||
|
||||
/* The FFN width as stated, unless the config asks for LFM2's sizing rule (the 1.2B and
|
||||
* smaller releases): two thirds of it, times the multiplier, rounded up to `multiple_of`. */
|
||||
/* The FFN width as stated, unless the config asks for LFM2's sizing rule (1.2B and smaller
|
||||
* releases): two thirds of it, times the multiplier, rounded up to `multiple_of`. */
|
||||
RadModelMeta dims = *meta;
|
||||
if (text_bool(meta, "block_auto_adjust_ff_dim", false)) {
|
||||
int64_t ff = 2 * dims.n_ff / 3;
|
||||
@@ -205,9 +204,9 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
m.b_logits = decl_b(b, nm.f("logits"), RAD_F32, {g.max_logit_rows, g.n_vocab});
|
||||
RAD_ARCH_TRY(rad_declare_logits(b, m.b_logits));
|
||||
|
||||
/* ---- the vocabulary edges. The lm_head is tied in every release so far, and declared as a
|
||||
* weight of its own anyway: the embedding lives in host memory (--embedding-placement) and the
|
||||
* head on the card, and a kernel may ask the head for a layout the lookup does not read. */
|
||||
/* ---- the vocabulary edges. The lm_head is tied but declared as its own weight: the
|
||||
* embedding may live in host memory (--embedding-placement) and the head on the card, and a
|
||||
* kernel may want a head layout the lookup does not read. */
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("token_embd.weight"), nm.ckpt("%s.embed_tokens.weight", pre)));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("output_norm.weight"), nm.ckpt("%s.embedding_norm.weight", pre)));
|
||||
RAD_ARCH_TRY(map_copy_alt(b, nm.f("output.weight"), nm.ckpt("lm_head.weight"),
|
||||
@@ -224,7 +223,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
RAD_INT("n_vocab", g.n_vocab), RAD_STR("dtype", g.dtype)),
|
||||
RAD_WEIGHTS(m.w_tok)),
|
||||
{}, {m.b_x});
|
||||
/* The encoder's rows, over the lookup: one rank, so nothing is reduced after it. */
|
||||
/* The encoder's rows overwrite the lookup; with one rank there is no reduction after it. */
|
||||
m.media = Siglip2::present(meta) && ctx->max_enc_patches > 0;
|
||||
if (m.media) RAD_ARCH_TRY(m.mrows.declare(b, g.max_tok, g.n_embd, m.b_x));
|
||||
|
||||
@@ -281,14 +280,14 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
L.op_k = gemm(g.kv_dim(), g.n_embd, L.w_k, m.b_h, m.b_k);
|
||||
L.op_v = gemm(g.kv_dim(), g.n_embd, L.w_v, m.b_h, m.b_v);
|
||||
/* The head norms run over rows of head_dim: a [T, heads * head_dim] buffer is
|
||||
* T * heads such rows. */
|
||||
* T * heads rows. */
|
||||
L.op_q_norm = rmsnorm(g.max_tok * g.n_head, g.head_dim, L.w_q_norm, m.b_q, m.b_q);
|
||||
L.op_k_norm = rmsnorm(g.max_tok * g.n_head_kv, g.head_dim, L.w_k_norm, m.b_k, m.b_k);
|
||||
L.op_rope_q = rw(b, decl_rope(b, g, g.max_tok, g.head_dim, g.n_head, 0, g.head_dim,
|
||||
"neox"), {m.b_q}, {m.b_q});
|
||||
L.op_rope_k = rw(b, decl_rope(b, g, g.max_tok, g.head_dim, g.n_head_kv, 0, g.head_dim,
|
||||
"neox"), {m.b_k}, {m.b_k});
|
||||
/* attn_paged before kv_store: the cache's block size is read off the resolved
|
||||
/* attn_paged before kv_store: the cache's block size is read from the resolved
|
||||
* attention kernel. */
|
||||
L.op_attn = rw(b, RAD_OP(b, "attn_paged",
|
||||
RAD_PARAMS(RAD_RANGE("q_len", 1, g.max_tok),
|
||||
@@ -371,7 +370,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
RAD_WEIGHTS(m.w_lm_head)),
|
||||
{m.b_hout}, {m.b_logits});
|
||||
|
||||
/* THE VISION TOWER, LAST, so its activations share the arena with every buffer above. */
|
||||
/* The vision tower, last, so its activations share the arena with every buffer above. */
|
||||
if (m.media) RAD_ARCH_TRY(m.vit.declare(b, meta, ctx, nm, g.n_embd, (int)g.n_layer + 64));
|
||||
|
||||
int64_t n_attn = 0;
|
||||
@@ -384,12 +383,12 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* THE CONVOLUTION IS ISSUED ONCE PER HALF OF THE STEP, as the PLE's and the gated delta net's
|
||||
* are: the decode rows [0, D) with `num_accepted`, which leaves the window in the layout a
|
||||
* partly rejected verify reads back from, and the prefill chunks [D, S) without it, each leaving
|
||||
* its tail at offset zero. `has_init` is `ctx_lens`: a sequence with tokens behind it has a
|
||||
* history. The per-sequence arrays of the prefill half move to `+ D`; the token operands stay at
|
||||
* their base, because the kernel reads rows at the absolute offsets in `cu`. */
|
||||
/* The convolution is issued once per half of the step, like the PLE's and the gated delta net's:
|
||||
* the decode rows [0, D) with `num_accepted`, leaving the window in the layout a partly rejected
|
||||
* verify reads back from, and the prefill chunks [D, S) without it, each leaving its tail at
|
||||
* offset zero. `has_init` is `ctx_lens` (a sequence with earlier tokens has a history). The
|
||||
* prefill half's per-sequence arrays start at `+ D`; the token operands stay at their base, since
|
||||
* the kernel reads rows at the absolute offsets in `cu`. */
|
||||
static void conv_step(const Model& m, const Layer& L, RadCtx* c, const RadBatch* batch, int l) {
|
||||
const int64_t T = batch->n_tok, S = batch->n_seq;
|
||||
const RadKVGroupBatch* cv = kv_batch(batch, m.kv_conv);
|
||||
@@ -421,7 +420,7 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
"and the program declares none of them");
|
||||
return;
|
||||
}
|
||||
/* AN ENCODER PASS RUNS THE VISION TOWER AND NOTHING ELSE (RadBatch::enc). */
|
||||
/* An encoder pass runs only the vision tower (RadBatch::enc). */
|
||||
if (batch->enc) {
|
||||
m.vit.step(c, batch);
|
||||
return;
|
||||
@@ -468,8 +467,8 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
L.mlp.step(c, batch);
|
||||
}
|
||||
|
||||
/* The lm_head runs on the rows the sampler asked for and on nothing else; a prefill chunk
|
||||
* that finishes no sequence issues none of it. */
|
||||
/* The lm_head runs only on the rows the sampler asked for; a prefill chunk that finishes no
|
||||
* sequence skips it. */
|
||||
if (batch->n_out > 0) {
|
||||
RAD_ISSUE_N(c, m.op_gather, batch->n_out, brows(m.b_x, T),
|
||||
praw(batch->out_ids, RAD_I32, batch->n_out), brows(m.b_hout, batch->n_out));
|
||||
|
||||
+20
-24
@@ -1,6 +1,6 @@
|
||||
/* siglip2.h -- LFM2-VL's image encoder: the SigLIP2 NaFlex tower and the projector after it.
|
||||
*
|
||||
* WHAT IT COMPUTES, per encoder pass (RadBatch::enc):
|
||||
* Per encoder pass (RadBatch::enc):
|
||||
*
|
||||
* x = patches @ Wp^T + bp the patch embedding, a linear layer over a patch
|
||||
* row (y, x, channel)
|
||||
@@ -13,18 +13,18 @@
|
||||
* y = post_LN(x)
|
||||
* out = l2(gelu(l1([LN](y viewed [P/merge^2, merge^2 * width])))) the projector
|
||||
*
|
||||
* which is transformers' Siglip2VisionModel (no pooling head) followed by Lfm2VlMultiModalProjector.
|
||||
* The projector's pixel unshuffle is a VIEW here: the processor stores each segment's patches in
|
||||
* i.e. transformers' Siglip2VisionModel (no pooling head) then Lfm2VlMultiModalProjector. The
|
||||
* projector's pixel unshuffle is a view: the processor stores each segment's patches in
|
||||
* merge-block order (core/mm/processor.cpp, image_lfm2), so a block's merge^2 patches are
|
||||
* consecutive rows, dy then dx -- the order the unshuffle concatenates them in. The tower itself
|
||||
* does not care about the order: attention within a segment is permutation-equivariant and every
|
||||
* other layer is per patch, and the position table is resampled at each patch's own (row, column).
|
||||
* consecutive rows, dy then dx, the order the unshuffle concatenates them. The tower is
|
||||
* order-independent: attention within a segment is permutation-equivariant, every other layer is
|
||||
* per patch, and the position table is resampled at each patch's own (row, column).
|
||||
*
|
||||
* q, k and v are one fused weight, concatenated at convert (map_concat), so a layer is one QKV
|
||||
* GEMM; the attention reads the three as column slices.
|
||||
*
|
||||
* Replicated, per-request weights and pass-sized activations, as arch/common/rad_block_vit.h's
|
||||
* tower: see the notes there.
|
||||
* Replicated, per-request weights and pass-sized activations, as in arch/common/rad_block_vit.h's
|
||||
* tower.
|
||||
*/
|
||||
#ifndef LFM2_SIGLIP2_H
|
||||
#define LFM2_SIGLIP2_H
|
||||
@@ -64,11 +64,6 @@ struct Siglip2 {
|
||||
return !std::strcmp(rad_meta_gets(m, "vision_config.model_type", ""), "siglip2_vision_model") &&
|
||||
rad_meta_geti(m, "vision_config.num_hidden_layers", 0) > 0;
|
||||
}
|
||||
/* Input patches an output row: the projector's unshuffle. */
|
||||
static int64_t merge_of(const RadModelMeta* m) {
|
||||
const int64_t d = rad_meta_geti(m, "downsample_factor", 0);
|
||||
return d * d;
|
||||
}
|
||||
|
||||
int declare(RadBuilder* b, const RadModelMeta* m, const RadBuildCtx* ctx, Names& nm,
|
||||
int64_t model_embd, int first_group);
|
||||
@@ -102,8 +97,8 @@ inline int Siglip2::declare(RadBuilder* b, const RadModelMeta* m, const RadBuild
|
||||
(long long)proj_hidden);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
/* The pooling head is SigLIP2's contrastive readout; LFM2-VL reads the patch rows, and a
|
||||
* checkpoint that ships a head would load weights nothing runs. Refused by name. */
|
||||
/* The pooling head is SigLIP2's contrastive readout; LFM2-VL reads the patch rows, so a
|
||||
* checkpoint that ships a head is refused by name rather than loading unused weights. */
|
||||
if (rad_meta_geti(m, "vision_config.vision_use_head", 0) != 0 ||
|
||||
!std::strcmp(rad_meta_gets(m, "vision_config.vision_use_head", ""), "true")) {
|
||||
fprintf(stderr, "radiance: lfm2: this SigLIP2 tower has a pooling head, which LFM2-VL does "
|
||||
@@ -131,8 +126,8 @@ inline int Siglip2::declare(RadBuilder* b, const RadModelMeta* m, const RadBuild
|
||||
const int64_t mm = merge * merge, mw = mm * width;
|
||||
P = ctx->max_enc_patches;
|
||||
/* The longest segment attention sees: a tile or the largest thumbnail, which the processor
|
||||
* keeps within max_num_patches. A bound that picks the attention kernel's block height, so it
|
||||
* is the real one in a sizing declare too. */
|
||||
* keeps within max_num_patches. It selects the attention kernel's block height, so a sizing
|
||||
* declare uses the real value too. */
|
||||
const int64_t seg = rad_meta_geti(m, "processor.image_processor.max_num_patches",
|
||||
rad_meta_geti(m, "max_num_patches", P));
|
||||
const int64_t max_seg = std::min<int64_t>(P, seg);
|
||||
@@ -166,7 +161,7 @@ inline int Siglip2::declare(RadBuilder* b, const RadModelMeta* m, const RadBuild
|
||||
l.n1b = bw(nm.f("v.blk.%d.norm1.bias", li), nm.ckpt(e, tw, li, "layer_norm1.bias"), { width }, g);
|
||||
l.n2w = bw(nm.f("v.blk.%d.norm2.weight", li), nm.ckpt(e, tw, li, "layer_norm2.weight"), { width }, g);
|
||||
l.n2b = bw(nm.f("v.blk.%d.norm2.bias", li), nm.ckpt(e, tw, li, "layer_norm2.bias"), { width }, g);
|
||||
/* q, k, v fused at convert: one GEMM a layer. */
|
||||
/* q, k, v fused at convert: one GEMM per layer. */
|
||||
const char* qw = nm.f("v.blk.%d.qkv.weight", li);
|
||||
const char* qb = nm.f("v.blk.%d.qkv.bias", li);
|
||||
if (map_concat(b, qw, { nm.ckpt(e, tw, li, "self_attn.q_proj.weight"),
|
||||
@@ -203,8 +198,9 @@ inline int Siglip2::declare(RadBuilder* b, const RadModelMeta* m, const RadBuild
|
||||
if (!w_patch || !b_patch || !w_pos || !pn_w || !pn_b || !m1_w || !m1_b || !m2_w || !m2_b)
|
||||
return RAD_E_INVAL;
|
||||
|
||||
/* ---- activations. `mh` is the post-norm's output declared at the MERGED width: the norm
|
||||
* writes P rows of `width` and the projector reads P/mm rows of mm * width, the same bytes. */
|
||||
/* ---- activations. `mh` is the post-norm's output declared at the merged width: the norm
|
||||
* writes P rows of `width` and the projector reads the same bytes as P/mm rows of
|
||||
* mm * width. */
|
||||
b_x = decl_b(b, nm.f("v.x"), RAD_BF16, { P, width });
|
||||
b_h = decl_b(b, nm.f("v.h"), RAD_BF16, { P, width });
|
||||
b_qkv = decl_b(b, nm.f("v.qkv"), RAD_BF16, { P, 3 * width });
|
||||
@@ -290,8 +286,8 @@ inline void Siglip2::step(RadCtx* c, const RadBatch* batch) const {
|
||||
"merged rows");
|
||||
return;
|
||||
}
|
||||
/* The patches, their coordinates -- a dense [4, P] of row, column, segment height and width,
|
||||
* which is what the position resample reads -- and the segment boundaries. */
|
||||
/* The patches, their coordinates (a dense [4, P] of row, column, segment height and width,
|
||||
* read by the position resample) and the segment boundaries. */
|
||||
const RadOperand pix = praw2(batch->enc_pixels, RAD_BF16, Pn, patch_dim);
|
||||
const RadOperand crd = praw2(batch->enc_coord, RAD_I32, 4, Pn);
|
||||
const RadOperand cu = praw(batch->enc_cu, RAD_I32, S + 1);
|
||||
@@ -313,8 +309,8 @@ inline void Siglip2::step(RadCtx* c, const RadBatch* batch) const {
|
||||
RAD_ISSUE_N(c, l.down, Pn, brows(b_ff, Pn), RAD_W(l.down_w), RAD_W(l.down_b),
|
||||
brows(b_x, Pn), brows(b_x, Pn));
|
||||
}
|
||||
/* The post-norm writes Pn rows of `width` into a buffer declared at the merged width: the
|
||||
* same bytes the projector reads as Pn/mm rows -- the pixel unshuffle. */
|
||||
/* The post-norm writes Pn rows of `width` into the merged-width buffer, which the projector
|
||||
* reads as Pn/mm rows: the pixel unshuffle. */
|
||||
RAD_ISSUE_N(c, op_post, Pn, brows(b_x, Pn), RAD_W(pn_w), RAD_W(pn_b), brows(b_mh, Rn));
|
||||
if (proj_norm)
|
||||
RAD_ISSUE_N(c, op_mln, Rn, brows(b_mh, Rn), RAD_W(mn_w), RAD_W(mn_b), brows(b_mn, Rn));
|
||||
|
||||
Reference in new issue
Block a user