n-gram table: --ngram-table ram reads it from a locked copy in RAM

embed_lookup_q reads the n-gram table through a row cache and direct reads,
and about half of a step's rows are first-seen, so steps wait on the drive.
--ngram-table ram (default disk, unchanged) copies the table into locked
huge pages in the background, checks the copy against the mapping and then
serves the gather from memory. MemAvailable, RLIMIT_MEMLOCK and the cgroup
limit are checked first; if any is short the table stays on disk. The flag
reaches the plugin as an optional `resident` parameter of embed_lookup_q.
This commit is contained in:
keptn42 authored and StillDeadcode committed 2026-10-05 12:52:50 +02:00
1 parent 4252353bc2
commit e6c8a3d7fa
18 files changed
+363 -56

No files matched your search

+4
View File
@@ -595,6 +595,9 @@ struct Geom {
* message anyway. Straight through to `all_reduce`'s `min_bytes`; 0 leaves the choice to
* the kernel plugin, which has the measured default. */
int64_t wire_min_bytes = 0;
/* 1 = keep a file-backed table a host gather reads in RAM (--ngram-table ram). Straight
* through to `embed_lookup_q`'s `resident`; RadBuildCtx says what it costs and buys. */
int ngram_ram = 0;
float eps = 1e-6f;
/* GemmaRMSNorm's `1 +`. Qwen3.5, Qwen3-Next and Gemma normalise with `x_hat * (1 + w)` and
* store `w` around 0.1, not around 1.0 -- so reading the gain as a plain RMSNorm gain shrinks
@@ -732,6 +735,7 @@ inline int geom_from(Geom& g, const RadModelMeta* m, const RadBuildCtx* c,
g.world = c->world_size ? c->world_size : 1;
g.wire_exact= c->tp_wire_lossy ? 0 : 1;
g.wire_min_bytes = c->tp_wire_min_bytes;
g.ngram_ram = c->ngram_table_ram ? 1 : 0;
g.eps = m->rms_eps;
g.theta = m->rope_theta;
g.rope_scale= m->rope_scale ? m->rope_scale : 1.0f;
+10 -11
View File
@@ -367,19 +367,18 @@ inline int PleLayer::declare(RadBuilder* b, Names& nm, const Geom& geom, const C
{}, {b_ids});
/* M IS T * heads HERE, not T: the gather fetches one table row per HEAD per token, and the
* band this op resolves on is that count. */
* band this op resolves on is that count. `resident` only when the deployment asked for the
* table in RAM (--ngram-table ram), so the default declares exactly the op it always has. */
const RadParam gp[] = { RAD_RANGE("M", 1, g.max_tok * c.heads), RAD_INT("n_embd", HD),
RAD_INT("n_vocab", c.rows), RAD_STR("dtype", g.dtype),
RAD_INT("resident", 1) };
const int n_gp = g.ngram_ram ? 5 : 4;
if (table_bf16)
op_gather = rw(b, RAD_OP(b, "embed_lookup_q",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok * c.heads), RAD_INT("n_embd", HD),
RAD_INT("n_vocab", c.rows), RAD_STR("dtype", g.dtype)),
RAD_WEIGHTS(w_table)),
{b_ids}, {b_emb});
op_gather = rw(b, RAD_OP(b, "embed_lookup_q", gp, n_gp, RAD_WEIGHTS(w_table)),
{b_ids}, {b_emb});
else
op_gather = rw(b, RAD_OP(b, "embed_lookup_q",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok * c.heads), RAD_INT("n_embd", HD),
RAD_INT("n_vocab", c.rows), RAD_STR("dtype", g.dtype)),
RAD_WEIGHTS(w_table, w_scale)),
{b_ids}, {b_emb});
op_gather = rw(b, RAD_OP(b, "embed_lookup_q", gp, n_gp, RAD_WEIGHTS(w_table, w_scale)),
{b_ids}, {b_emb});
op_embd = rw(b, RAD_OP(b, "cast",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", c.embed_dim),