Skip to content

Commit 8241bc7

Browse files
committed
sampling : avoid expensive softmax during greedy sampling
ggml-ci
1 parent 37f8c7b commit 8241bc7

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

common/sampling.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ struct gpt_sampler * gpt_sampler_init(const struct llama_model * model, const st
209209
GGML_ASSERT(false && "unknown mirostat version");
210210
}
211211
} else {
212-
llama_sampler_chain_add(result->chain, llama_sampler_init_softmax());
212+
if (params.n_probs > 0) {
213+
llama_sampler_chain_add(result->chain, llama_sampler_init_top_k(params.n_probs));
214+
llama_sampler_chain_add(result->chain, llama_sampler_init_softmax());
215+
}
213216
llama_sampler_chain_add(result->chain, llama_sampler_init_greedy());
214217
}
215218

include/llama.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,7 @@ extern "C" {
10661066
LLAMA_API struct llama_sampler * llama_sampler_init_dist (uint32_t seed);
10671067

10681068
/// @details Sorts candidate tokens by their logits in descending order and calculate probabilities based on logits.
1069+
/// NOTE: Avoid using on the full vocabulary as the sorting can become slow. For example, apply top-k or top-p sampling first.
10691070
LLAMA_API struct llama_sampler * llama_sampler_init_softmax (void);
10701071

10711072
/// @details Top-K sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751

src/llama-sampling.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
#include "llama-vocab.h"
44
#include "llama-grammar.h"
55

6-
#include <cassert>
76
#include <algorithm>
8-
#include <cstring>
9-
#include <ctime>
7+
#include <cassert>
108
#include <cfloat>
119
#include <chrono>
1210
#include <cmath>
11+
#include <cstdlib>
12+
#include <cstring>
13+
#include <ctime>
1314
#include <numeric>
1415
#include <random>
1516
#include <unordered_map>

tests/test-sampling.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "ggml.h"
22
#include "llama.h"
3-
#include "llama-sampling.h"
43

54
#ifdef NDEBUG
65
#undef NDEBUG
@@ -249,6 +248,45 @@ static void test_sampler_queue(const size_t n_vocab, const std::string & sampler
249248
samplers_sequence.c_str(), n_vocab, top_k, top_p, min_p);
250249
}
251250

251+
#define BENCH(__cnstr, __data, __n_iter) do { \
252+
auto * cnstr = (__cnstr); \
253+
std::vector<llama_token_data> cur((__data).size()); \
254+
std::copy((__data).begin(), (__data).end(), cur.begin()); \
255+
llama_token_data_array cur_p = { cur.data(), cur.size(), -1, false }; \
256+
llama_sampler_apply(cnstr, &cur_p); \
257+
llama_sampler_reset(cnstr); \
258+
const int64_t t_start = ggml_time_us(); \
259+
const int n_iter = (__n_iter); \
260+
for (int i = 0; i < n_iter; i++) { \
261+
std::copy((__data).begin(), (__data).end(), cur.begin()); \
262+
llama_token_data_array cur_p = { cur.data(), cur.size(), -1, false }; \
263+
llama_sampler_apply(cnstr, &cur_p); \
264+
llama_sampler_reset(cnstr); \
265+
} \
266+
const int64_t t_end = ggml_time_us(); \
267+
llama_sampler_free(cnstr); \
268+
printf("%-42s: %8.3f us/iter\n", #__cnstr, (t_end - t_start) / (float)n_iter); \
269+
} while(0)
270+
271+
static void test_perf() {
272+
const int n_vocab = 1 << 17;
273+
274+
std::vector<llama_token_data> data;
275+
276+
data.reserve(n_vocab);
277+
for (int i = 0; i < n_vocab; i++) {
278+
const float logit = 2.0f*((float)(rand())/RAND_MAX - 0.5f);
279+
data.emplace_back(llama_token_data{i, logit, 0.0f});
280+
}
281+
282+
BENCH(llama_sampler_init_top_k (40), data, 32);
283+
BENCH(llama_sampler_init_top_p (0.8f, 1), data, 32);
284+
BENCH(llama_sampler_init_min_p (0.2f, 1), data, 32);
285+
BENCH(llama_sampler_init_tail_free(0.5f, 1), data, 32);
286+
BENCH(llama_sampler_init_typical (0.5f, 1), data, 32);
287+
BENCH(llama_sampler_init_softmax (), data, 32);
288+
}
289+
252290
int main(void) {
253291
ggml_time_init();
254292

@@ -316,5 +354,7 @@ int main(void) {
316354

317355
printf("OK\n");
318356

357+
test_perf();
358+
319359
return 0;
320360
}

0 commit comments

Comments
 (0)