From 81cdd3ce48d233af03d4d91406c6712bbfb97b47 Mon Sep 17 00:00:00 2001 From: Denis Artyomov Date: Sun, 30 Aug 2026 20:43:30 +0500 Subject: [PATCH] limit DFlash2 draft ubatch to reduce compute buffer --- common/common.cpp | 11 +++++++++++ common/speculative.cpp | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/common/common.cpp b/common/common.cpp index 8299c40be..65aa77da2 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1319,6 +1319,17 @@ common_init_result::common_init_result(common_params & params, bool model_only) } cparams_dft.n_rs_seq = 0; + // keep the fit estimate in sync with common_speculative_init_result (see there) + const bool has_block_draft = std::any_of( + params.speculative.types.begin(), params.speculative.types.end(), + [](common_speculative_type t) { + return t == COMMON_SPECULATIVE_TYPE_DRAFT_DFLASH || t == COMMON_SPECULATIVE_TYPE_DRAFT_DSPARK; + }); + if (has_block_draft) { + cparams_dft.n_ubatch = std::min(cparams_dft.n_ubatch, + params_dft.n_parallel * (uint32_t) std::max(1, params_dft.speculative.draft.n_max + 1)); + } + const common_fit_extra_model extra = { /*.path_model =*/ params_dft.model.path.c_str(), /*.mparams =*/ &mparams_dft, diff --git a/common/speculative.cpp b/common/speculative.cpp index 516f344ef..1489ce880 100644 --- a/common/speculative.cpp +++ b/common/speculative.cpp @@ -2592,6 +2592,23 @@ common_speculative_init_result::common_speculative_init_result( cparams.n_rs_seq = 0; cparams.ctx_other = ctx_tgt; + // DFlash2/DSpark rank the full target vocabulary inside the draft graph (top_k / argmax + // on the lm_head output), so the worst-case compute buffer grows with n_ubatch * n_vocab. + // Their largest decode batch is one noise block per sequence (n_max + 1 tokens), so cap the + // ubatch to that; anything larger only inflates the reserved compute buffer. + const bool has_block_draft = std::any_of( + params.speculative.types.begin(), params.speculative.types.end(), + [](common_speculative_type t) { + return t == COMMON_SPECULATIVE_TYPE_DRAFT_DFLASH || t == COMMON_SPECULATIVE_TYPE_DRAFT_DSPARK; + }); + if (has_block_draft) { + const uint32_t n_ubatch_dft = params.n_parallel * (uint32_t) std::max(1, params.speculative.draft.n_max + 1); + if (cparams.n_ubatch > n_ubatch_dft) { + LOG_INF("%s: capping draft context ubatch from %u to %u (block draft)\n", __func__, cparams.n_ubatch, n_ubatch_dft); + cparams.n_ubatch = n_ubatch_dft; + } + } + std::string model_path; if (has_draft) { model_path = params.speculative.draft.mparams.path;