mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-04 07:18:50 +00:00
ling32: the logits buffer holds 32 rows in --genjobs / --serve, not a whole chunk (0.58 GiB more pool on a 1024-token chunk)
The engine allocated chunk x vocab floats of logits (644 MB at chunk 1024) although a chat or serve run asks for the last token of a prompt chunk and at most block + 1 rows of a speculative verify batch. EngineOptions::logits_rows (default 0 = a whole chunk, so the library, the fixture tests and the teacher-forced modes are unchanged) sizes it; strata-ling32 sets 32 for --serve and --genjobs (--logits-rows N overrides, 0 = a whole chunk), and the tier planner's activation estimate follows (TierSizes::logits_rows), so the freed memory becomes expert slots. forward() / forward_span() refuse a request for more rows than the buffer holds, and spec_init refuses a buffer smaller than a verify batch. RTX 5080 16 GB (tier B, ctx 131072, plain, 5 x 256 greedy): pool 1671 -> 1807 slots (fixed 7.72 -> 7.14 GiB), decode 37.9 / 38.1 -> 39.0 tok/s, greedy ids identical on all 5 prompts.
This commit is contained in:
1 parent
62d3d48c04
commit
cced9a40a1
4 files changed
+15
-2
No files matched your search
@@ -112,6 +112,7 @@ struct TierSizes {
|
||||
uint64_t lookahead_bytes = 0; // one Section-2 lookahead router set (51.25 MiB)
|
||||
uint64_t latent_bytes = 0; // MLA latent cache (ctx * 7 * 576 * 4)
|
||||
int chunk = 256; // tokens per forward()
|
||||
int logits_rows = 0; // rows of the engine's logits buffer (V floats each); 0 = chunk (the old estimate)
|
||||
int vocab = 157184;
|
||||
int hidden = 2560;
|
||||
};
|
||||
@@ -166,7 +167,8 @@ inline TierPlan plan_tier(const TierHardware& hw, const TierRequestOpts& rq, con
|
||||
if (rq.vram_budget > vram_total) p.vram_budget = vram_total;
|
||||
// what the engine needs on the card: CUDA context + scratch, trunk, latent cache, KDA state + checkpoint, per-token activations, the lookahead routers
|
||||
const uint64_t kda_state = 2ull * 35 * 2 * MiB;
|
||||
const uint64_t act = static_cast<uint64_t>(sz.chunk) * (static_cast<uint64_t>(sz.vocab) * 4 + 1200ull * 1024) + 64 * MiB;
|
||||
const uint64_t lrows = static_cast<uint64_t>(sz.logits_rows > 0 ? std::min(sz.logits_rows, sz.chunk) : sz.chunk); // the logits buffer: lrows x vocab floats (a chat / serve run keeps 32 rows, not a whole chunk)
|
||||
const uint64_t act = static_cast<uint64_t>(sz.chunk) * 1200ull * 1024 + lrows * static_cast<uint64_t>(sz.vocab) * 4 + 64 * MiB;
|
||||
p.vram_fixed = 450 * MiB + 100 * MiB + sz.trunk_bytes + sz.latent_bytes + kda_state + act + (rq.prefetch ? sz.lookahead_bytes : 0);
|
||||
// pool mode (see below) uses the card more tightly: measured at the end of the start-up on a 4090, an A4000, a 3060 and a 3060 Ti, 1.7 - 2.1 GiB of the card were still free with the
|
||||
// plan above (the 600 MiB reserve, the 2.5 % margin, and an activation estimate that is 0.55 - 0.8 GiB high). So: 256 MiB reserve (auto budget only), 1 % margin, fixed - 512 MiB.
|
||||
|
||||
@@ -92,6 +92,7 @@ struct Ling32Engine::StageCtx {
|
||||
float *q = nullptr, *ckv = nullptr, *qabs = nullptr, *ctx = nullptr, *av = nullptr, *gate = nullptr;
|
||||
float *g = nullptr, *u = nullptr, *a = nullptr;
|
||||
float *rlog = nullptr, *rw = nullptr, *gu = nullptr, *ye = nullptr, *logits = nullptr;
|
||||
int lrows = 0; // rows of `logits` (EngineOptions::logits_rows)
|
||||
int* ridx = nullptr;
|
||||
bool h_ready = false; // the previous MoE layer's combine kernel already wrote this layer's input rmsnorm into h
|
||||
float* kscr = nullptr; // chunk-parallel KDA: the state-independent per-chunk tiles (kda_chunk_scratch_floats(Nmax))
|
||||
@@ -415,12 +416,13 @@ struct Ling32Engine::StageCtx {
|
||||
C = c.kda_channels();
|
||||
Kmax = std::max({D, C, c.I_dense, c.I_shared, c.H * c.vhead});
|
||||
const size_t N = Nmax;
|
||||
lrows = o.logits_rows > 0 ? std::min<int>(o.logits_rows, static_cast<int>(N)) : static_cast<int>(N);
|
||||
bool ok = alloc(x, N * D, err) && alloc(h, N * D, err) && alloc(cur, N * D, err) && alloc(proj, N * 5 * C, err) && alloc(qn, N * C, err) && alloc(kn, N * C, err) &&
|
||||
alloc(vc, N * C, err) && alloc(ko, N * C, err) && alloc(beta, N * c.H, err) && alloc(q, N * c.mla_q_dim(), err) && alloc(ckv, N * c.mla_latent_vals(), err) &&
|
||||
alloc(qabs, N * c.H * c.kv_lora, err) && alloc(ctx, N * c.H * c.kv_lora, err) && alloc(av, N * c.H * c.vhead, err) && alloc(gate, N * c.H, err) &&
|
||||
alloc(g, N * std::max(c.I_dense, c.I_shared), err) && alloc(u, N * std::max(c.I_dense, c.I_shared), err) && alloc(a, N * std::max(c.I_dense, c.I_shared), err) &&
|
||||
alloc(rlog, N * c.n_exp, err) && alloc(ridx, N * c.top_k, err) && alloc(rw, N * c.top_k, err) && alloc(gu, N * c.top_k * 2 * c.I_moe, err) &&
|
||||
alloc(ye, N * c.top_k * D, err) && alloc(logits, N * c.V, err) && alloc(ids, N, err);
|
||||
alloc(ye, N * c.top_k * D, err) && alloc(logits, static_cast<size_t>(lrows) * c.V, err) && alloc(ids, N, err);
|
||||
if (!ok) return false;
|
||||
if (!cu(cudaMalloc(reinterpret_cast<void**>(&ecnt), 32 * sizeof(int)), "cudaMalloc", err) || !cu(cudaMemset(ecnt, 0, 32 * sizeof(int)), "cudaMemset", err)) return false;
|
||||
if (E.opt.kda_par && E.opt.kda_chunked && Nmax > E.opt.kda_seq_max_n) {
|
||||
@@ -1125,6 +1127,7 @@ bool Ling32Engine::forward_mm(const int* ids, int N, int n_logits, std::vector<f
|
||||
if (N < 1 || N > opt.max_tokens) { err = "forward: N must be 1.." + std::to_string(opt.max_tokens); return false; }
|
||||
if (pos_ + N > opt.max_ctx) { err = "context full: position " + std::to_string(pos_ + N) + " > max_ctx " + std::to_string(opt.max_ctx); return false; }
|
||||
if (n_logits > N) n_logits = N;
|
||||
if (n_logits > I.lrows) { err = "forward: " + std::to_string(n_logits) + " logits rows asked for, the buffer holds " + std::to_string(I.lrows) + " (EngineOptions::logits_rows)"; return false; }
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
if (provider_) provider_->begin_forward(N);
|
||||
I.la_enabled = true;
|
||||
@@ -1193,6 +1196,7 @@ bool Ling32Engine::forward_mm(const int* ids, int N, int n_logits, std::vector<f
|
||||
bool Ling32Engine::forward_span(const int* ids, int N, int n_logits, std::vector<float>* logits, std::string& err) {
|
||||
if (N < 1) { err = "forward_span: N must be >= 1"; return false; }
|
||||
if (n_logits > N) n_logits = N;
|
||||
if (n_logits > p_->stages.front()->lrows) { err = "forward_span: " + std::to_string(n_logits) + " logits rows asked for, the buffer holds " + std::to_string(p_->stages.front()->lrows) + " (EngineOptions::logits_rows)"; return false; }
|
||||
const int sub = opt.max_tokens;
|
||||
const bool carry = (!spec_on_ || span_spec_) && !opt.capture && n_logits <= sub && (N > sub || (provider_ && N >= 128)); // one chunk with a tier store also runs layer-major: the next-layer hint then applies
|
||||
if (!carry) { // the chunk loop of the caller
|
||||
@@ -1364,6 +1368,7 @@ bool Ling32Engine::spec_init(const SpecParams& p, std::string& err) {
|
||||
I.sp = p;
|
||||
I.sB = std::max(1, std::min(p.block, 8));
|
||||
if (I.Nmax < I.sB + 1) { err = "--chunk must be at least block + 1 for speculative decoding"; return false; }
|
||||
if (I.lrows < I.sB + 1) { err = "the logits buffer (" + std::to_string(I.lrows) + " rows) is smaller than a verify batch (block + 1)"; return false; }
|
||||
if (opt.capture) { err = "speculative decoding is not combined with layer capture"; return false; }
|
||||
I.snap_slots = I.sB;
|
||||
I.ext_len = 3 + I.sB + 1;
|
||||
|
||||
@@ -33,6 +33,7 @@ struct EngineOptions {
|
||||
bool rope_fp32 = false; // RoPE angles in float32 like a float32 framework (ablation; double by default)
|
||||
int max_ctx = 8192; // MLA latent cache rows per MLA layer (fp16, 576 values each)
|
||||
int max_tokens = 256; // tokens per forward() call
|
||||
int logits_rows = 0; // rows of the logits buffer (V floats each): 0 = max_tokens (every row of a chunk can be asked for); a chat / serve run asks for at most the verify rows of a speculative cycle, so the engine sets 32 there and the 0.6 GiB of a 1024-token chunk stay free for expert slots
|
||||
int tc_min_n = 0; // trunk projections with float activations and at least this many tokens: tensor-core GEMM (fp16 tiles, fp32 accumulate); 0 = always the float GEMV
|
||||
int tc_moe_min_n = 0; // routed experts of batches with at least this many tokens (float activations): grouped by expert, tensor-core tiles; 0 = per-pair kernels
|
||||
bool cuda_graph = false; // batches of 1..graph_max_n tokens run as one captured CUDA graph per (N, n_logits) (not with lookahead, capture, profile or an ExpertProvider)
|
||||
|
||||
@@ -204,6 +204,8 @@ struct RunOpts {
|
||||
bool hidden_decode = false; // --hidden-decode (with --dump-hidden-jobs): the continuation rows go through the one-token decode path
|
||||
std::string hidden_jobs; // --dump-hidden-jobs FILE --out DIR: for every {"id","prompt_ids"[,"cont_ids"]} line the sequence prompt ++ cont: DIR/<id>.hpre.f32 [n][D] (residual after the last layer), .hfin.f32 [n][D] (after the final norm), .ids.i32; no drafter needed
|
||||
std::string genjobs; // --genjobs FILE: {"id","prompt_ids","gen"} per line: greedy / sampled continuation only (no teacher-forced pass), spec on or off
|
||||
int logits_rows_opt = -1; // --logits-rows N: rows of the engine's logits buffer (0 = a whole chunk; -1 = 32 for --serve / --genjobs, which ask for few rows, else a whole chunk: the teacher-forced modes dump every row)
|
||||
int logits_rows() const { return logits_rows_opt >= 0 ? logits_rows_opt : ((serve || !genjobs.empty()) ? 32 : 0); }
|
||||
int dist_n = 0, dist_len = 4; // --dist-test N [--dist-len L]: N sampled continuations of the first prompt, histogram of the first L tokens
|
||||
};
|
||||
|
||||
@@ -349,6 +351,7 @@ TierSizes make_sizes(const Ling32File& f, const RunOpts& o) {
|
||||
sz.latent_bytes = static_cast<uint64_t>(n_mla) * latent_layer_bytes(o.latent, o.ctx, f.cfg().kv_lora, f.cfg().rope);
|
||||
}
|
||||
sz.chunk = o.chunk;
|
||||
sz.logits_rows = o.logits_rows() > 0 ? std::min(o.logits_rows(), o.chunk) : o.chunk;
|
||||
sz.vocab = f.cfg().V;
|
||||
sz.hidden = f.cfg().D;
|
||||
return sz;
|
||||
@@ -754,6 +757,7 @@ int cmd_run(const RunOpts& o) {
|
||||
eo.clamp_after_silu = o.clamp_after;
|
||||
eo.max_ctx = o.ctx;
|
||||
eo.max_tokens = o.chunk;
|
||||
eo.logits_rows = o.logits_rows();
|
||||
eo.lookahead = o.lookahead || o.coverage || (tiered && plan.prefetch);
|
||||
if (tiered) { eo.lookahead_ks = (o.lookahead || o.coverage) ? 3 : (plan.lookahead_k == 4 ? 2 : 1); eo.lookahead_stats = o.lookahead; }
|
||||
eo.lookahead_group_mask = !o.la_nomask;
|
||||
@@ -1524,6 +1528,7 @@ int main(int argc, char** argv) {
|
||||
else if (a == "--gen") o.gen = std::atoi(nx().c_str());
|
||||
else if (a == "--ctx") o.ctx = std::atoi(nx().c_str());
|
||||
else if (a == "--chunk") o.chunk = std::atoi(nx().c_str());
|
||||
else if (a == "--logits-rows") o.logits_rows_opt = std::atoi(nx().c_str());
|
||||
else if (a == "--device") o.device = std::atoi(nx().c_str());
|
||||
else if (a == "--topk") o.topk = std::atoi(nx().c_str());
|
||||
else if (a == "--act") o.act_q8 = nx() == "q8";
|
||||
|
||||
Reference in new issue
Block a user