Skip to content

Commit 635b65a

Browse files
spec: add spec metrics mean acceptance length and acceptance rate per position (ggml-org#24536)
* spec: add spec metrics mean acceptance length and acceptance per pos * fix as suggestion Co-authored-by: Georgi Gerganov <[email protected]> * fix as suggestion Co-authored-by: Georgi Gerganov <[email protected]> * fix as suggestion Co-authored-by: Georgi Gerganov <[email protected]> * fix as suggestions --------- Co-authored-by: Georgi Gerganov <[email protected]>
1 parent e3a74b2 commit 635b65a

2 files changed

Lines changed: 57 additions & 4 deletions

File tree

common/speculative.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ struct common_speculative_impl {
140140
size_t n_gen_tokens = 0; // number of tokens generated by this implementation.
141141
size_t n_acc_tokens = 0; // number of tokens accepted by the target model.
142142

143+
std::vector<size_t> n_acc_tokens_per_pos; // number of tokens accepted per draft position.
144+
143145
// TODO: track performance of most recent calls
144146
const bool gen_perf = true; // whether to generate performance stats.
145147

@@ -2059,6 +2061,15 @@ void common_speculative_accept(common_speculative * spec, llama_seq_id seq_id, u
20592061

20602062
{
20612063
common_time_meas tm(impl->t_accept_us, !impl->gen_perf);
2064+
2065+
if (impl->n_acc_tokens_per_pos.size() < n_accepted) {
2066+
impl->n_acc_tokens_per_pos.resize(n_accepted, 0);
2067+
}
2068+
2069+
for (size_t i = 0; i < n_accepted; ++i) {
2070+
impl->n_acc_tokens_per_pos[i]++;
2071+
}
2072+
20622073
if (n_accepted > 0) {
20632074
impl->n_acc_drafts++;
20642075
impl->n_acc_tokens += n_accepted;
@@ -2093,13 +2104,31 @@ void common_speculative_print_stats(const common_speculative * spec) {
20932104
str_perf = "";
20942105
}
20952106

2096-
LOG_INF("statistics %16s: #calls(b,g,a) = %4zu %6zu %6zu, #gen drafts = %6zu, #acc drafts = %5zu, #gen tokens = %6zu, #acc tokens = %5zu%s\n",
2107+
std::string str_stats;
2108+
if (impl->n_call_accept > 0) {
2109+
const double mean =
2110+
1.0 + (double) impl->n_acc_tokens / (double) impl->n_call_accept;
2111+
std::ostringstream tmp;
2112+
tmp << std::fixed << std::setprecision(3);
2113+
for (size_t i = 0; i < impl->n_acc_tokens_per_pos.size(); ++i) {
2114+
if (i > 0) {
2115+
tmp << ", ";
2116+
}
2117+
tmp << (double) impl->n_acc_tokens_per_pos[i] / (double) impl->n_call_accept;
2118+
}
2119+
std::ostringstream oss;
2120+
oss << std::fixed << std::setprecision(2) << mean;
2121+
str_stats = ", #mean acc len = " + oss.str() + ", #acc rate/pos = (" + tmp.str() + ")";
2122+
}
2123+
2124+
LOG_INF("statistics %16s: #calls(b,g,a) = %4zu %6zu %6zu, #gen drafts = %6zu, #acc drafts = %5zu, #gen tokens = %6zu, #acc tokens = %5zu%s%s\n",
20972125
common_speculative_type_to_str(impl->type).c_str(),
20982126
impl->n_call_begin, impl->n_call_draft, impl->n_call_accept,
20992127
impl->n_gen_drafts,
21002128
impl->n_acc_drafts,
21012129
impl->n_gen_tokens,
21022130
impl->n_acc_tokens,
2131+
str_stats.c_str(),
21032132
str_perf.c_str());
21042133
}
21052134
}

tools/server/server-context.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ struct server_slot {
201201
// Speculative decoding stats
202202
int32_t n_draft_total = 0; // Total draft tokens generated
203203
int32_t n_draft_accepted = 0; // Draft tokens actually accepted
204+
int32_t n_draft_verif_steps = 0; // Total draft token verification steps by the target model
205+
std::vector<int32_t> n_accepted_per_pos; // Accepted tokens per draft position
204206

205207
void reset() {
206208
SLT_DBG(*this, "%s", "\n");
@@ -227,6 +229,8 @@ struct server_slot {
227229
// clear speculative decoding stats
228230
n_draft_total = 0;
229231
n_draft_accepted = 0;
232+
n_draft_verif_steps = 0;
233+
n_accepted_per_pos.clear();
230234

231235
task_prev = std::move(task);
232236
task.reset();
@@ -509,10 +513,22 @@ struct server_slot {
509513
llama_perf_context(ctx_tgt).n_reused);
510514

511515
if (n_draft_total > 0) {
512-
const float draft_ratio = (float) n_draft_accepted / n_draft_total;
516+
const float draft_ratio = (float) n_draft_accepted / n_draft_total;
517+
const double mean_acc_len = n_draft_verif_steps > 0 ? 1.0 + (double) n_draft_accepted / (double) n_draft_verif_steps : 1.0;
518+
519+
std::string acceptance_rates_per_pos;
520+
if (n_draft_verif_steps > 0) {
521+
for (size_t i = 0; i < n_accepted_per_pos.size(); ++i) {
522+
if (i > 0) {
523+
acceptance_rates_per_pos += ", ";
524+
}
525+
acceptance_rates_per_pos += string_format("%.3f", (double) n_accepted_per_pos[i] / (double) n_draft_verif_steps);
526+
}
527+
}
528+
513529
SLT_INF(*this,
514-
"draft acceptance = %0.5f (%5d accepted / %5d generated)\n",
515-
draft_ratio, n_draft_accepted, n_draft_total);
530+
"draft acceptance = %0.5f (%5d accepted / %5d generated), mean acceptance length = %5.2f, acceptance rate per position = (%s)\n",
531+
draft_ratio, n_draft_accepted, n_draft_total, mean_acc_len, acceptance_rates_per_pos.c_str());
516532
}
517533

518534
common_speculative_print_stats(spec);
@@ -3543,6 +3559,14 @@ struct server_context_impl {
35433559

35443560
// update how many tokens out of those tested were accepted
35453561
slot.n_draft_accepted += ids.size() - 1;
3562+
slot.n_draft_verif_steps += 1;
3563+
3564+
if (slot.n_accepted_per_pos.empty()) {
3565+
slot.n_accepted_per_pos.resize(common_speculative_n_max(&params_base.speculative), 0);
3566+
}
3567+
for (size_t i = 0; i < ids.size() - 1 && i < slot.n_accepted_per_pos.size(); ++i) {
3568+
slot.n_accepted_per_pos[i]++;
3569+
}
35463570

35473571
// add accepted tokens to the prompt
35483572
slot.prompt.tokens.keep_first(slot.prompt.n_tokens() - n_draft);

0 commit comments

Comments
 (0)