diff --git a/engine/src/inkling2/cuda/ink_cuda_backend.cu b/engine/src/inkling2/cuda/ink_cuda_backend.cu index 9672c48756..504d654d00 100644 --- a/engine/src/inkling2/cuda/ink_cuda_backend.cu +++ b/engine/src/inkling2/cuda/ink_cuda_backend.cu @@ -1602,11 +1602,17 @@ public: emit_selected_logits(loc_idx, dst_rows); } + static const bool admit_prof_be = std::getenv("STRATA_INK_ADMIT_PROF") != nullptr; + const auto admit_t0 = std::chrono::steady_clock::now(); for (int s : multi_slots) { run_single_slot_seq(s, by_slot[s]); } INK_CK(cudaStreamSynchronize(runner_->st)); + if (admit_prof_be && !multi_slots.empty()) { + std::fprintf(stderr, "[ADMITBE] tokens %d slots %zu decode_items %zu multi_slots %zu (one forward_seq each): multi part %.1f ms\n", N, slot_order.size(), dec_orig.size(), multi_slots.size(), + std::chrono::duration(std::chrono::steady_clock::now() - admit_t0).count()); + } return true; } @@ -2254,6 +2260,10 @@ public: bool supports_batched_dspark() const noexcept override { return bspec_ready() || c64_k8v4_ready(); } bool prefers_candidate_rows() const noexcept override { return initialized_ && strata::ink::sm120_cfg(gpu_id_).fast; } bool batched_dspark_prefill_first() const noexcept override { return c64_k8v4_ready(); } + // A burst of prompts: forward_batch_impl runs one forward_seq per slot, and each forward reads every layer's weights (about 32 ms on an A40 for + // 8 tokens, SQ-20), so the serve loop's even split of its 512-token budget over 64 slots (8 tokens each) costs 64 forwards per step. + // 128 tokens per slot per step reads the same budget in 4 forwards. + int min_prefill_tokens_per_slot() const noexcept override { return initialized_ ? 128 : 0; } int dspark_spec_step_batched_greedy( int B, diff --git a/engine/src/inkling2/ink_backend.h b/engine/src/inkling2/ink_backend.h index aad9d9f84e..6c60510e99 100644 --- a/engine/src/inkling2/ink_backend.h +++ b/engine/src/inkling2/ink_backend.h @@ -271,6 +271,12 @@ public: // 3. Default prefill token cap while other concurrent slots are actively decoding (0 = uncapped). virtual int preferred_prefill_cap_with_decoders() const noexcept { return 0; } + // 3b. Many prompts queued at once (a burst of simultaneous users): the serve loop splits its per-step token budget over every prefilling + // slot, which leaves a few tokens per slot, and a backend that runs one forward per slot pays its whole fixed cost (every layer's weights + // read once) for each of those few tokens. A backend returns N > 0 here to have the loop serve fewer slots per step, oldest first, with at + // least N prompt tokens each (0 = the budget is split over all of them, as before). + virtual int min_prefill_tokens_per_slot() const noexcept { return 0; } + // 4. Optional BCO pre-warm hook for streaming prefix KV warming into an idle slot. virtual bool supports_bco_prewarm() const noexcept { return false; } virtual bool bco_prewarm(int slot, const int32_t* ids, int count, std::string& err) { diff --git a/engine/src/inkling2/ink_serve.h b/engine/src/inkling2/ink_serve.h index fd69e5c387..ef1087e087 100644 --- a/engine/src/inkling2/ink_serve.h +++ b/engine/src/inkling2/ink_serve.h @@ -546,6 +546,10 @@ inline int run_serve_loop(const ServeConfig& cfg, InkModelFile& mf, InkBackend& bool cand_ok = !(std::getenv("STRATA_INK_NO_CAND") && std::getenv("STRATA_INK_NO_CAND")[0] == '1'); if (const char* e = std::getenv("STRATA_INK_PREFILL_CAP"); e && *e) prefill_cap = std::max(0, std::atoi(e)); else prefill_cap = backend.preferred_prefill_cap_with_decoders(); // 0 on every backend except one that opts in (HIP R9700 / Strix fast mode) + // STRATA_INK_PREFILL_MIN_SLOT=N: with many prompts queued at once, read at least N prompt tokens of a slot per step (oldest first) instead of + // splitting the step's budget over all of them (default: the backend's own number, 0 on every backend except CUDA; 0 = the old split). + int min_slot_chunk = backend.min_prefill_tokens_per_slot(); + if (const char* e = std::getenv("STRATA_INK_PREFILL_MIN_SLOT"); e && *e) min_slot_chunk = std::max(0, std::atoi(e)); const bool use_dspark = (cfg.draft_mode != "off") && backend.dspark_ready(); // the backend says whether a drafter runs: DSpark from the model file, or the DFlash 2 sidecar beside it // STRATA_INK_SPEC=block: measurement switch, greedy requests use the linear block-8 drafter + batch verify instead of the beam tree const char* spec_env = std::getenv("STRATA_INK_SPEC"); @@ -902,7 +906,13 @@ inline int run_serve_loop(const ServeConfig& cfg, InkModelFile& mf, InkBackend& std::vector logit_slots; std::vector logits_buf; + const bool admit_prof = std::getenv("STRATA_INK_ADMIT_PROF") != nullptr; + long admit_step = 0; + double admit_t_end_prev = now_s(); + double admit_ckpt_ms = 0.0; while (true) { + const double admit_t_loop0 = now_s(); + admit_ckpt_ms = 0.0; // 1. Drain all queued stdin commands without blocking while any slot is active bool any_active = false; for (const auto& rq : reqs) if (rq.active) { any_active = true; break; } @@ -1267,7 +1277,17 @@ inline int run_serve_loop(const ServeConfig& cfg, InkModelFile& mf, InkBackend& } prefill_fed[s] = take; } else { - const int per_slot = std::max(1, token_budget / static_cast(pf_slots.size())); + int per_slot = std::max(1, token_budget / static_cast(pf_slots.size())); + if (min_slot_chunk > 0 && per_slot < min_slot_chunk) { + // A burst of prompts: serve only as many slots as the budget fills with at least min_slot_chunk tokens each, oldest request + // first (the others wait for a later step), instead of a few tokens for every slot. + const int n_take = std::max(1, token_budget / min_slot_chunk); + if (static_cast(pf_slots.size()) > n_take) { + std::stable_sort(pf_slots.begin(), pf_slots.end(), [&](int a, int b) { return reqs[a].t_start < reqs[b].t_start; }); + pf_slots.resize(n_take); + } + per_slot = std::max(1, token_budget / static_cast(pf_slots.size())); + } for (int s : pf_slots) { auto& rq = reqs[s]; const int rem = rq.prompt_len - rq.prefill_pos; @@ -1356,9 +1376,11 @@ inline int run_serve_loop(const ServeConfig& cfg, InkModelFile& mf, InkBackend& if (rq.ckpt_pending && (rq.prefill_pos == rq.ckpt_at || (backend.prefers_async_prompt_ckpt() && rq.ckpt_at == rq.prompt_len - 1 && rq.prefill_pos == rq.prompt_len))) { rq.ckpt_pending = false; const int ckpt_pos = rq.ckpt_at; // the prompt end - 1, or the tail-role checkpoint (#7241) + const double admit_c0 = now_s(); if (backend.save_prompt_checkpoint(rq.slot, ckpt_pos)) { rq.prompt_ckpt_prefix.assign(rq.prompt_ids.begin(), rq.prompt_ids.begin() + ckpt_pos); } + admit_ckpt_ms += (now_s() - admit_c0) * 1000.0; } if (rq.is_solo || !rq.admitted) { const double ppts = step_ms > 0.0 ? (prefill_fed[s] * 1000.0 / step_ms) : 0.0; @@ -1453,6 +1475,15 @@ inline int run_serve_loop(const ServeConfig& cfg, InkModelFile& mf, InkBackend& } } } + if (admit_prof) { + int n_pf_now = 0, n_pf_fed = 0, tok_fed = 0; + for (int s = 0; s < P; ++s) { if (prefill_fed[s] > 0) { ++n_pf_fed; tok_fed += prefill_fed[s]; } if (reqs[s].active && reqs[s].prefill_pos < reqs[s].prompt_len) ++n_pf_now; } + const double t_now = now_s(); + std::fprintf(stderr, "[ADMIT] step %ld active %d prefilling %d fed_slots %d fed_tokens %d batch %d want %d fwd %.1f ms ckpt %.1f ms host_before %.1f ms host_after %.1f ms gap %.1f ms\n", + ++admit_step, n_active, n_pf_now, n_pf_fed, tok_fed, batch.n_tokens, n_want, step_ms, admit_ckpt_ms, + (t_step0 - admit_t_loop0) * 1000.0, (t_now - t_step0) * 1000.0 - step_ms - admit_ckpt_ms, (admit_t_loop0 - admit_t_end_prev) * 1000.0); + admit_t_end_prev = t_now; + } if (spec_adapt && use_dspark && n_active == 1 && n_want == 1 && prefill_fed[sole_slot] == 0) { // one plain decode step: its whole cost, forward + host sampling + output const double ms = (now_s() - t_step0) * 1000.0;