Skip to content

Commit 292f690

Browse files
authored
spec : remove check rate (ggml-org#19377)
* spec: remove parameter spec-ngram-check-rate * spec : renamed statistics vars * spec : add n_call_begin, n_call_accept * spec : don't enable key-map-stats
1 parent 81ddc60 commit 292f690

7 files changed

Lines changed: 36 additions & 62 deletions

File tree

common/arg.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3437,16 +3437,6 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
34373437
params.speculative.ngram_size_m = value;
34383438
}
34393439
).set_examples({LLAMA_EXAMPLE_SERVER}));
3440-
add_opt(common_arg(
3441-
{"--spec-ngram-check-rate"}, "N",
3442-
string_format("ngram check rate for ngram-simple/ngram-map speculative decoding (default: %d)", params.speculative.ngram_check_rate),
3443-
[](common_params & params, int value) {
3444-
if (value < 1) {
3445-
throw std::invalid_argument("ngram check rate must be at least 1");
3446-
}
3447-
params.speculative.ngram_check_rate = value;
3448-
}
3449-
).set_examples({LLAMA_EXAMPLE_SERVER}));
34503440
add_opt(common_arg(
34513441
{"--spec-ngram-min-hits"}, "N",
34523442
string_format("minimum hits for ngram-map speculative decoding (default: %d)", params.speculative.ngram_min_hits),

common/common.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ struct common_params_speculative {
269269

270270
uint16_t ngram_size_n = 12; // ngram size for lookup
271271
uint16_t ngram_size_m = 48; // mgram size for speculative tokens
272-
uint16_t ngram_check_rate = 1; // check rate for ngram lookup
273272
uint16_t ngram_min_hits = 1; // minimum hits at ngram/mgram lookup for mgram to be proposed
274273

275274
std::shared_ptr<common_ngram_mod> ngram_mod;

common/ngram-map.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,9 @@ void common_ngram_map_draft(common_ngram_map & map,
231231
GGML_ABORT("%s: cur_len exceeds UINT32_MAX: %zu", __func__, cur_len);
232232
}
233233

234-
// Only check every check_rate tokens to save compute
235-
// i.e., perform check if (cur_len - idx_last_check) >= check_rate
236-
if (map.idx_last_check + map.check_rate > cur_len) {
237-
return;
234+
if (map.idx_last_check > cur_len) {
235+
// Should not happen because of common_ngram_map_begin().
236+
GGML_ABORT("%s: map.idx_last_check > cur_len: %zu > %zu", __func__, map.idx_last_check, cur_len);
238237
}
239238
map.idx_last_check = cur_len;
240239

common/ngram-map.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
struct common_ngram_simple_config {
2525
uint16_t size_ngram; // size of n-grams to lookup in self-mode
2626
uint16_t size_mgram; // size of m-grams to draft in self-mode
27-
uint16_t check_rate; // check for speculative decoding without draft model for each check_rate token
2827
};
2928

3029
// Searches for a n-gram in the history and checks whether a draft sequence should be generated.
@@ -66,15 +65,14 @@ struct common_ngram_map {
6665
bool key_only; // true if only key n-grams are used, no values.
6766

6867
std::vector<common_ngram_map_key> keys; // key n-grams which occur several times in token-history
69-
uint16_t check_rate; // check for speculative decoding without draft model for each check_rate token
7068
uint16_t min_hits; // minimum number of key hits to consider a draft
7169

72-
bool show_key_map_stats = false; // true, if statitics of the key_map should be printed.
70+
bool show_key_map_stats = false; // true, if statistics of the key_map should be printed.
7371

7472
common_ngram_map(uint16_t sz_key, uint16_t sz_value, bool only_keys,
75-
uint16_t check_rate, uint16_t min_hits)
73+
uint16_t min_hits)
7674
: size_key(sz_key), size_value(sz_value), key_only(only_keys),
77-
check_rate(check_rate), min_hits(min_hits) {
75+
min_hits(min_hits) {
7876
key_map.resize(COMMON_NGRAM_HASH_MAP_SIZE); // 2^18 hash entries, 0 entries if key_map shouldn't be used
7977
}
8078

common/speculative.cpp

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,14 @@ static bool common_speculative_are_compatible(
113113
struct common_speculative_state {
114114
const enum common_speculative_type type;
115115

116-
// TODO: rename to n_call_draft, n_gen_drafts, n_acc_drafts, n_gen_tokens, n_acc_tokens
117-
// TODO: add n_call_begin, n_call_accept
118-
size_t drafts_call_count = 0; // number of times this implementation was called.
119-
size_t drafts_generated_count = 0; // number of times a draft or part was generated by this implementation.
120-
size_t drafts_accepted_count = 0; // number of times a draft or part was accepted by the target model.
121-
size_t drafts_generated_tokens = 0; // number of tokens generated by this implementation.
122-
size_t drafts_accepted_tokens = 0; // number of tokens accepted by the target model.
116+
size_t n_call_begin = 0; // number of times this implementation was called for refresh.
117+
size_t n_call_draft = 0; // number of times this implementation was called for generation.
118+
size_t n_call_accept = 0; // number of times this implementation was called for accumulation.
119+
120+
size_t n_gen_drafts = 0; // number of times a draft or part was generated by this implementation.
121+
size_t n_acc_drafts = 0; // number of times a draft or part was accepted by the target model.
122+
size_t n_gen_tokens = 0; // number of tokens generated by this implementation.
123+
size_t n_acc_tokens = 0; // number of tokens accepted by the target model.
123124

124125
// TODO: track performance of most recent calls
125126
const bool gen_perf = true; // whether to generate performance stats.
@@ -465,8 +466,6 @@ struct common_speculative_state_eagle3 : public common_speculative_state {
465466
struct common_speculative_state_ngram_simple : public common_speculative_state {
466467
common_ngram_simple_config config;
467468

468-
uint16_t check_id = 0; // used to control the frequency of generating drafts
469-
470469
common_speculative_state_ngram_simple(
471470
enum common_speculative_type type,
472471
common_ngram_simple_config config)
@@ -481,11 +480,6 @@ struct common_speculative_state_ngram_simple : public common_speculative_state {
481480
const llama_tokens & prompt_tgt,
482481
llama_token id_last,
483482
llama_tokens & result) override {
484-
++check_id;
485-
if (check_id < config.check_rate) {
486-
return;
487-
}
488-
check_id = 0;
489483

490484
result = common_ngram_simple_draft(config, prompt_tgt, id_last);
491485
GGML_UNUSED(params);
@@ -752,10 +746,9 @@ static common_ngram_map get_common_ngram_map(const common_speculative_config & c
752746
uint16_t size_key = config.params.ngram_size_n;
753747
uint16_t size_value = config.params.ngram_size_m;
754748
bool key_only = (config.type == COMMON_SPECULATIVE_TYPE_NGRAM_MAP_K);
755-
uint16_t check_rate = config.params.ngram_check_rate;
756749
uint16_t min_hits = config.params.ngram_min_hits;
757750

758-
return common_ngram_map(size_key, size_value, key_only, check_rate, min_hits);
751+
return common_ngram_map(size_key, size_value, key_only, min_hits);
759752
}
760753

761754
static common_speculative_state_ngram_cache create_state_ngram_cache(
@@ -931,12 +924,10 @@ common_speculative * common_speculative_init(
931924

932925
uint16_t ngram_size_key = ngram_map.size_key;
933926
uint16_t mgram_size_value = ngram_map.size_value;
934-
uint16_t check_rate = ngram_map.check_rate;
935927

936928
auto config_simple = common_ngram_simple_config {
937929
/* .size_ngram = */ ngram_size_key,
938-
/* .size_mgram = */ mgram_size_value,
939-
/* .check_rate = */ check_rate
930+
/* .size_mgram = */ mgram_size_value
940931
};
941932
auto state = std::make_unique<common_speculative_state_ngram_simple>(
942933
/* .type = */ config.type,
@@ -997,6 +988,7 @@ void common_speculative_begin(common_speculative * spec, const llama_tokens & pr
997988
for (auto & impl : spec->impls) {
998989
common_time_meas tm(impl->t_begin_us, !impl->gen_perf);
999990
impl->begin(prompt);
991+
impl->n_call_begin++;
1000992
}
1001993
}
1002994

@@ -1013,17 +1005,17 @@ llama_tokens common_speculative_draft(
10131005
{
10141006
common_time_meas tm(impl->t_draft_us, !impl->gen_perf);
10151007
impl->draft(params, prompt_tgt, id_last, result);
1016-
impl->drafts_call_count++;
1008+
impl->n_call_draft++;
10171009
}
10181010

10191011
if (!result.empty()) {
10201012
LOG_DBG("%s: called impl %s, hist size = %zu, call_count = %zu, gen = %zu\n", __func__,
10211013
common_speculative_type_to_str(impl.get()->type).c_str(), prompt_tgt.size(),
1022-
impl.get()->drafts_call_count, result.size());
1014+
impl.get()->n_call_draft, result.size());
10231015

10241016
spec->curr_impl = impl.get(); // set current implementation for stats
1025-
impl->drafts_generated_count++;
1026-
impl->drafts_generated_tokens += result.size();
1017+
impl->n_gen_drafts++;
1018+
impl->n_gen_tokens += result.size();
10271019

10281020
break; // We have a draft, so break out of the loop and return it.
10291021
}
@@ -1044,11 +1036,12 @@ void common_speculative_accept(common_speculative * spec, uint16_t n_accepted) {
10441036
{
10451037
common_time_meas tm(impl->t_accept_us, !impl->gen_perf);
10461038
if (n_accepted > 0) {
1047-
impl->drafts_accepted_count++;
1048-
impl->drafts_accepted_tokens += n_accepted;
1039+
impl->n_acc_drafts++;
1040+
impl->n_acc_tokens += n_accepted;
10491041
}
10501042

10511043
impl->accept(n_accepted);
1044+
impl->n_call_accept++;
10521045
}
10531046
}
10541047

@@ -1069,13 +1062,13 @@ void common_speculative_print_stats(const common_speculative * spec) {
10691062
str_perf = "";
10701063
}
10711064

1072-
LOG_INF("statistics %s: #calls = %zu, #gen drafts = %zu, #acc drafts = %zu, #gen tokens = %zu, #acc tokens = %zu%s\n",
1065+
LOG_INF("statistics %s: #calls(b,g,a) = %zu %zu %zu, #gen drafts = %zu, #acc drafts = %zu, #gen tokens = %zu, #acc tokens = %zu%s\n",
10731066
common_speculative_type_to_str(impl->type).c_str(),
1074-
impl->drafts_call_count,
1075-
impl->drafts_generated_count,
1076-
impl->drafts_accepted_count,
1077-
impl->drafts_generated_tokens,
1078-
impl->drafts_accepted_tokens,
1067+
impl->n_call_begin, impl->n_call_draft, impl->n_call_accept,
1068+
impl->n_gen_drafts,
1069+
impl->n_acc_drafts,
1070+
impl->n_gen_tokens,
1071+
impl->n_acc_tokens,
10791072
str_perf.c_str());
10801073
}
10811074
}

docs/speculative.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ If a draft model is combined with a draftless decoding the draftless decoding ha
119119
of lookup n-gram (default: 12)
120120
--spec-ngram-size-m N ngram size M for ngram-simple/ngram-map speculative decoding, length
121121
of draft m-gram (default: 48)
122-
--spec-ngram-check-rate N ngram check rate for ngram-simple/ngram-map speculative decoding
123-
(default: 1)
124122
--spec-ngram-min-hits N minimum hits for ngram-map speculative decoding (default: 1)
125123
```
126124

@@ -153,10 +151,6 @@ Sets the size M of the draft m-gram for n-gram map based speculative decoding.
153151
The m-gram size determines how many tokens to draft when a match is found.
154152
Larger values can provide more speedup but may reduce acceptance rate.
155153

156-
### `--spec-ngram-check-rate R`
157-
158-
This option aims at performance if the n-gram lookup in history is to costly. A lookup will be executed at every R tokens (default is 1, every token).
159-
160154
### `--spec-ngram-min-hits H`
161155

162156
This option defines how often a key has to appear in the token history to be used as a draft (default is 1).
@@ -175,7 +169,12 @@ draft acceptance rate = 0.70312 ( 90 accepted / 128 generated)
175169
statistics ngram_mod: #calls = 810, #gen drafts = 15, #acc drafts = 15, #gen tokens = 960, #acc tokens = 730, dur(b,g,a) = 0.149, 0.347, 0.005 ms
176170
```
177171

178-
- `#calls`: number of calls of this implementations
172+
```
173+
statistics ngram_map_k: #calls(b,g,a) = 6 1690 26, #gen drafts = 26, #acc drafts = 26, #gen tokens = 1248, #acc tokens = 968, dur(b,g,a) = 2.234, 1.427, 0.016 ms
174+
```
175+
176+
177+
- `#calls(b,g,a)`: number of calls of begin (new prompt), generation and accumulation of this implementations
179178
- `#gen drafts`: number of drafts generated by this implementation
180179
- `#acc drafts`: number of drafts accepted (partially) by the main model
181180
- `#gen tokens`: number of tokens generated by this implementation (including rejected tokens)

tools/server/server-task.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ json task_params::to_json(bool only_metrics) const {
8080
{"speculative.type", common_speculative_type_to_str(speculative.type)},
8181
{"speculative.ngram_size_n", speculative.ngram_size_n},
8282
{"speculative.ngram_size_m", speculative.ngram_size_m},
83-
{"speculative.ngram_c_rate", speculative.ngram_check_rate},
8483
{"speculative.ngram_m_hits", speculative.ngram_min_hits},
8584
{"timings_per_token", timings_per_token},
8685
{"post_sampling_probs", post_sampling_probs},
@@ -144,7 +143,6 @@ json task_params::to_json(bool only_metrics) const {
144143
{"speculative.type", common_speculative_type_to_str(speculative.type)},
145144
{"speculative.ngram_size_n", speculative.ngram_size_n},
146145
{"speculative.ngram_size_m", speculative.ngram_size_m},
147-
{"speculative.ngram_c_rate", speculative.ngram_check_rate},
148146
{"speculative.ngram_m_hits", speculative.ngram_min_hits},
149147
{"timings_per_token", timings_per_token},
150148
{"post_sampling_probs", post_sampling_probs},
@@ -257,12 +255,10 @@ task_params server_task::params_from_json_cmpl(
257255

258256
params.speculative.ngram_size_n = json_value(data, "speculative.ngram_size_n", defaults.speculative.ngram_size_n);
259257
params.speculative.ngram_size_m = json_value(data, "speculative.ngram_size_m", defaults.speculative.ngram_size_m);
260-
params.speculative.ngram_check_rate = json_value(data, "speculative.ngram_c_rate", defaults.speculative.ngram_check_rate);
261258
params.speculative.ngram_min_hits = json_value(data, "speculative.ngram_m_hits", defaults.speculative.ngram_min_hits);
262259

263260
params.speculative.ngram_size_n = std::max(std::min(1, (int) params.speculative.ngram_size_n), 1024);
264261
params.speculative.ngram_size_m = std::max(std::min(1, (int) params.speculative.ngram_size_m), 1024);
265-
params.speculative.ngram_check_rate = std::max(std::min(1, (int) params.speculative.ngram_check_rate), 1024);
266262
params.speculative.ngram_min_hits = std::max(std::min(1, (int) params.speculative.ngram_min_hits), 1024);
267263

268264
// Use OpenAI API logprobs only if n_probs wasn't provided

0 commit comments

Comments
 (0)