Skip to content

Commit dc01f83

Browse files
committed
dflash: auto-apply chat template + thinking-off in speculative-simple
raw prompt acceptance ~4% -> 80% (459 t/s) with LLAMA_SPEC_NO_THINK; matches PR/blog.
1 parent 3f23d0a commit dc01f83

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

examples/speculative-simple/speculative-simple.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include "arg.h"
22
#include "common.h"
3+
#include "chat.h"
34
#include "sampling.h"
45
#include "speculative.h"
56
#include "log.h"
67
#include "llama.h"
78

89
#include <clocale>
910
#include <cstdio>
11+
#include <cstdlib>
1012
#include <cstring>
1113
#include <cinttypes>
1214
#include <string>
@@ -102,9 +104,32 @@ int main(int argc, char ** argv) {
102104
LOG_INF("speculative decoding will use checkpoints (context does not support partial sequence removal)\n");
103105
}
104106

107+
// DFlash drafters are trained on chat-formatted data, so apply the target's chat template
108+
// (and, with LLAMA_SPEC_NO_THINK set, disable thinking). Without this a raw prompt gives very
109+
// low acceptance; the blog reports thinking-off raising acceptance from a few % to ~90%.
110+
std::string prompt = params.prompt;
111+
if (is_dflash) {
112+
auto chat_templates = common_chat_templates_init(model_tgt, params.chat_template);
113+
if (common_chat_templates_was_explicit(chat_templates.get())) {
114+
common_chat_msg msg;
115+
msg.role = "user";
116+
msg.content = params.prompt;
117+
118+
common_chat_templates_inputs cinputs;
119+
cinputs.messages = { msg };
120+
cinputs.add_generation_prompt = true;
121+
if (const char * nt = std::getenv("LLAMA_SPEC_NO_THINK"); nt && std::string(nt) != "0") {
122+
cinputs.enable_thinking = false; // Qwen3 / Qwen3.5
123+
cinputs.chat_template_kwargs["reasoning_effort"] = "\"low\""; // gpt-oss
124+
}
125+
prompt = common_chat_templates_apply(chat_templates.get(), cinputs).prompt;
126+
LOG_INF("%s: DFlash chat template applied\n", __func__);
127+
}
128+
}
129+
105130
// Tokenize the prompt
106131
std::vector<llama_token> inp;
107-
inp = common_tokenize(ctx_tgt, params.prompt, true, true);
132+
inp = common_tokenize(ctx_tgt, prompt, true, true);
108133

109134
if (llama_n_ctx(ctx_tgt) < (uint32_t) inp.size()) {
110135
LOG_ERR("%s: the prompt exceeds the context size (%d tokens, ctx %d)\n", __func__, (int) inp.size(), llama_n_ctx(ctx_tgt));

0 commit comments

Comments
 (0)