Skip to content

Commit 9bedaf4

Browse files
committed
Add space around * pointers and & references.
1 parent abe0829 commit 9bedaf4

File tree

4 files changed

+34
-35
lines changed

4 files changed

+34
-35
lines changed

examples/beam_search/beam_search.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929

3030
// Used for debugging to print out beam tokens.
3131
struct ostream_beam_view {
32-
llama_context* ctx;
32+
llama_context * ctx;
3333
llama_beam_view beam_view;
3434
};
35-
std::ostream& operator<<(std::ostream& os, ostream_beam_view const& obv) {
35+
std::ostream& operator<<(std::ostream& os, ostream_beam_view const & obv) {
3636
os << "p(" << obv.beam_view.p << ") eos(" << std::boolalpha << obv.beam_view.eos << ") tokens(";
3737
for (size_t i=0 ; i<obv.beam_view.n_tokens ; ++i) {
3838
os << llama_token_to_str(obv.ctx, obv.beam_view.tokens[i]);
@@ -42,11 +42,11 @@ std::ostream& operator<<(std::ostream& os, ostream_beam_view const& obv) {
4242

4343
// Put here anything you want back in beam_search_callback().
4444
struct beam_search_callback_data {
45-
llama_context* ctx;
45+
llama_context * ctx;
4646
std::vector<llama_token> response;
4747
};
4848

49-
bool is_at_eos(beam_search_callback_data const& callback_data, llama_token const* tokens, size_t const n_tokens) {
49+
bool is_at_eos(beam_search_callback_data const & callback_data, llama_token const * tokens, size_t const n_tokens) {
5050
return n_tokens && tokens[n_tokens-1] == llama_token_eos(callback_data.ctx);
5151
}
5252

@@ -56,7 +56,7 @@ bool is_at_eos(beam_search_callback_data const& callback_data, llama_token const
5656
// * When all beams converge to a common prefix, they are made available in beams_state.beams[0].
5757
// This is also called when the stop condition is met.
5858
// Collect tokens into std::vector<llama_token> response which is pointed to by callback_data.
59-
void beam_search_callback(void* callback_data_ptr, llama_beams_state beams_state) {
59+
void beam_search_callback(void * callback_data_ptr, llama_beams_state beams_state) {
6060
auto& callback_data = *static_cast<beam_search_callback_data*>(callback_data_ptr);
6161
// Mark beams as EOS as needed.
6262
for (size_t i=0 ; i<beams_state.n_beams ; ++i) {
@@ -69,7 +69,7 @@ void beam_search_callback(void* callback_data_ptr, llama_beams_state beams_state
6969
if (size_t const n = beams_state.common_prefix_length) {
7070
callback_data.response.resize(callback_data.response.size() + n);
7171
assert(0u < beams_state.n_beams);
72-
llama_token const* tokens = beams_state.beam_views[0].tokens;
72+
llama_token const * tokens = beams_state.beam_views[0].tokens;
7373
std::copy(tokens, tokens + n, callback_data.response.end() - n);
7474
printf("%lu", n);
7575
}

examples/server/server.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ static void log_server_request(const Request &req, const Response &res)
12091209
});
12101210
}
12111211

1212-
bool is_at_eos(llama_server_context& server_context, llama_token const* tokens, size_t const n_tokens) {
1212+
bool is_at_eos(llama_server_context & server_context, llama_token const * tokens, size_t const n_tokens) {
12131213
return n_tokens && tokens[n_tokens-1] == llama_token_eos(server_context.ctx);
12141214
}
12151215

@@ -1219,11 +1219,11 @@ bool is_at_eos(llama_server_context& server_context, llama_token const* tokens,
12191219
// * When all beams converge to a common prefix, they are made available in beams_state.beams[0].
12201220
// This is also called when the stop condition is met.
12211221
// Collect tokens into std::vector<llama_token> response which is pointed to by callback_data.
1222-
void beam_search_callback(void* callback_data, llama_beams_state beams_state) {
1223-
auto& llama = *static_cast<llama_server_context*>(callback_data);
1222+
void beam_search_callback(void * callback_data, llama_beams_state beams_state) {
1223+
auto & llama = *static_cast<llama_server_context*>(callback_data);
12241224
// Mark beams as EOS as needed.
12251225
for (size_t i=0 ; i<beams_state.n_beams ; ++i) {
1226-
llama_beam_view& beam_view = beams_state.beam_views[i];
1226+
llama_beam_view & beam_view = beams_state.beam_views[i];
12271227
if (!beam_view.eos && is_at_eos(llama, beam_view.tokens, beam_view.n_tokens)) {
12281228
beam_view.eos = true;
12291229
}
@@ -1232,8 +1232,7 @@ void beam_search_callback(void* callback_data, llama_beams_state beams_state) {
12321232
if (size_t const n = beams_state.common_prefix_length) {
12331233
llama.generated_token_probs.resize(llama.generated_token_probs.size() + n);
12341234
assert(0u < beams_state.n_beams);
1235-
llama_token const* tokens = beams_state.beam_views[0].tokens;
1236-
//std::copy(tokens, tokens + n, llama->generated_token_probs.end() - n);
1235+
llama_token const * tokens = beams_state.beam_views[0].tokens;
12371236
auto const map = [](llama_token tok) { return completion_token_output{{},tok}; };
12381237
std::transform(tokens, tokens + n, llama.generated_token_probs.end() - n, map);
12391238
printf("%lu", n);
@@ -1248,20 +1247,20 @@ void beam_search_callback(void* callback_data, llama_beams_state beams_state) {
12481247
}
12491248

12501249
struct token_translator {
1251-
llama_context* ctx;
1250+
llama_context * ctx;
12521251
std::string operator()(llama_token tok) const { return llama_token_to_str(ctx, tok); }
12531252
std::string operator()(completion_token_output cto) const { return (*this)(cto.tok); }
12541253
};
12551254

1256-
void append_to_generated_text_from_generated_token_probs(llama_server_context& llama) {
1257-
auto& gtps = llama.generated_token_probs;
1255+
void append_to_generated_text_from_generated_token_probs(llama_server_context & llama) {
1256+
auto & gtps = llama.generated_token_probs;
12581257
auto translator = token_translator{llama.ctx};
1259-
auto add_strlen = [=](size_t sum, completion_token_output const& cto) { return sum + translator(cto).size(); };
1258+
auto add_strlen = [=](size_t sum, completion_token_output const & cto) { return sum + translator(cto).size(); };
12601259
size_t const len = std::accumulate(gtps.begin(), gtps.end(), size_t(0), add_strlen);
12611260
if (llama.generated_text.capacity() < llama.generated_text.size() + len) {
12621261
llama.generated_text.reserve(llama.generated_text.size() + len);
12631262
}
1264-
for (completion_token_output const& cto : gtps) {
1263+
for (completion_token_output const & cto : gtps) {
12651264
llama.generated_text += translator(cto);
12661265
}
12671266
}

llama.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4335,7 +4335,7 @@ struct llama_beam {
43354335
float p; // Cumulative beam probability (renormalized relative to all beams)
43364336
bool eos; // Initialize end-of-sentence to false. Callback sets this to true.
43374337
// Sort beams by probability. In case of ties, prefer beams at eos.
4338-
bool operator<(llama_beam const& rhs) const {
4338+
bool operator<(llama_beam const & rhs) const {
43394339
return std::make_tuple(p, eos) < std::make_tuple(rhs.p, rhs.eos);
43404340
}
43414341
// Shift off first n tokens and discard them.
@@ -4350,15 +4350,15 @@ struct llama_beam {
43504350

43514351
// A struct for calculating logit-related info.
43524352
struct logit_info {
4353-
float const* const logits;
4353+
float const * const logits;
43544354
int const n_vocab;
43554355
float const max_l;
43564356
float const normalizer;
43574357
struct sum_exp {
43584358
float max_l;
43594359
float operator()(float sum, float l) const { return sum + std::exp(l - max_l); }
43604360
};
4361-
logit_info(llama_context* ctx)
4361+
logit_info(llama_context * ctx)
43624362
: logits(llama_get_logits(ctx))
43634363
, n_vocab(llama_n_vocab(ctx))
43644364
, max_l(*std::max_element(logits, logits + n_vocab))
@@ -4376,7 +4376,7 @@ struct logit_info {
43764376
for (llama_token token_id=0 ; token_id<k_min ; ++token_id) {
43774377
min_heap.push_back(get_token_data(token_id));
43784378
}
4379-
auto comp = [](llama_token_data const& a, llama_token_data const& b) { return a.logit > b.logit; };
4379+
auto comp = [](llama_token_data const & a, llama_token_data const & b) { return a.logit > b.logit; };
43804380
std::make_heap(min_heap.begin(), min_heap.end(), comp);
43814381
for (llama_token token_id=k_min ; token_id<n_vocab ; ++token_id) {
43824382
if (min_heap.front().logit < logits[token_id]) {
@@ -4432,9 +4432,9 @@ struct beam_search {
44324432
// * Gather elements until the vector is full, then call std::make_heap() on it.
44334433
// * If the heap is full and a new element is found that should be included, pop the
44344434
// least element to the back(), replace it with the new, then push it into the heap.
4435-
void fill_next_beams_by_top_probabilities(llama_beam& beam) {
4435+
void fill_next_beams_by_top_probabilities(llama_beam & beam) {
44364436
// Min-heaps use a greater-than comparator.
4437-
auto const comp = [](llama_beam const& a, llama_beam const& b) { return a.p > b.p; };
4437+
auto const comp = [](llama_beam const & a, llama_beam const & b) { return a.p > b.p; };
44384438
if (beam.eos) {
44394439
// beam is at end-of-sentence, so just copy it to next_beams if its probability is high enough.
44404440
if (next_beams.size() < n_beams) {
@@ -4516,9 +4516,9 @@ struct beam_search {
45164516
// * any of the beams have not yet reached end-of-sentence, AND
45174517
// * the highest probability beam(s) (plural in case of ties) are not at end-of-sentence
45184518
// (since all other beam probabilities can only decrease)
4519-
void loop(llama_beam_search_callback_fn_t const callback, void* const callback_data) {
4519+
void loop(llama_beam_search_callback_fn_t const callback, void * const callback_data) {
45204520
beams.push_back({{}, 1.0f, false}); // Start with one empty beam w/ probability = 1.0 and !eos.
4521-
auto const not_eos = [](llama_beam const& beam) { return !beam.eos; };
4521+
auto const not_eos = [](llama_beam const & beam) { return !beam.eos; };
45224522
for (int i=0 ; i<n_predict && std::any_of(beams.begin(),beams.end(),not_eos) &&
45234523
!beams[top_beam_index()].eos ; ++i) {
45244524
callback(callback_data, get_beams_state(false)); // Sets common_prefix_length
@@ -4528,8 +4528,8 @@ struct beam_search {
45284528
n_past += common_prefix_length;
45294529
}
45304530
// Zero-out next_beam probabilities to place them last in following min-heap.
4531-
std::for_each(next_beams.begin(), next_beams.end(), [](llama_beam& beam) { beam.p = 0.0f; });
4532-
for (llama_beam& beam : beams) {
4531+
std::for_each(next_beams.begin(), next_beams.end(), [](llama_beam & beam) { beam.p = 0.0f; });
4532+
for (llama_beam & beam : beams) {
45334533
beam.shift_tokens(common_prefix_length);
45344534
fill_next_beams_by_top_probabilities(beam);
45354535
}
@@ -4543,10 +4543,10 @@ struct beam_search {
45434543

45444544
// As beams grow, the cumulative probabilities decrease.
45454545
// Renormalize them to avoid floating point underflow.
4546-
static void renormalize_beam_probabilities(std::vector<llama_beam>& beams) {
4547-
auto const sum_p = [](float sum, llama_beam& beam) { return sum + beam.p; };
4546+
static void renormalize_beam_probabilities(std::vector<llama_beam> & beams) {
4547+
auto const sum_p = [](float sum, llama_beam & beam) { return sum + beam.p; };
45484548
float const inv_sum = 1.0f / std::accumulate(beams.begin(), beams.end(), 0.0f, sum_p);
4549-
std::for_each(beams.begin(), beams.end(), [=](llama_beam& beam) { beam.p *= inv_sum; });
4549+
std::for_each(beams.begin(), beams.end(), [=](llama_beam & beam) { beam.p *= inv_sum; });
45504550
}
45514551

45524552
// Assumes beams is non-empty. Uses llama_beam::operator<() for ordering.
@@ -4564,7 +4564,7 @@ struct beam_search {
45644564
};
45654565

45664566
void llama_beam_search(llama_context * ctx,
4567-
llama_beam_search_callback_fn_t callback, void* callback_data,
4567+
llama_beam_search_callback_fn_t callback, void * callback_data,
45684568
size_t n_beams, int n_past, int n_predict, int n_threads) {
45694569
assert(ctx);
45704570
const int64_t t_start_sample_us = ggml_time_us();

llama.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ extern "C" {
470470
//
471471

472472
struct llama_beam_view {
473-
llama_token const* tokens;
473+
const llama_token * tokens;
474474
size_t n_tokens;
475475
float p; // Cumulative beam probability (renormalized relative to all beams)
476476
bool eos; // Callback should set this to true when a beam is at end-of-sentence.
@@ -481,7 +481,7 @@ extern "C" {
481481
// (e.g. beams[0]) as they will be removed (shifted) from all beams in all subsequent callbacks.
482482
// These pointers are valid only during the synchronous callback, so should not be saved.
483483
struct llama_beams_state {
484-
llama_beam_view* beam_views;
484+
llama_beam_view * beam_views;
485485
size_t n_beams; // Number of elements in beam_views[].
486486
size_t common_prefix_length; // Current max length of prefix tokens shared by all beams.
487487
bool last_call; // True iff this is the last callback invocation.
@@ -490,7 +490,7 @@ extern "C" {
490490
// Type of pointer to the beam_search_callback function.
491491
// void* callback_data is any custom data passed to llama_beam_search, that is subsequently
492492
// passed back to beam_search_callback. This avoids having to use global variables in the callback.
493-
typedef void (*llama_beam_search_callback_fn_t)(void* callback_data, llama_beams_state);
493+
typedef void (*llama_beam_search_callback_fn_t)(void * callback_data, llama_beams_state);
494494

495495
/// @details Deterministically returns entire sentence constructed by a beam search.
496496
/// @param ctx Pointer to the llama_context.
@@ -501,7 +501,7 @@ extern "C" {
501501
/// @param n_past Number of tokens already evaluated.
502502
/// @param n_predict Maximum number of tokens to predict. EOS may occur earlier.
503503
/// @param n_threads Number of threads as passed to llama_eval().
504-
LLAMA_API void llama_beam_search(struct llama_context * ctx, llama_beam_search_callback_fn_t callback, void* callback_data, size_t n_beams, int n_past, int n_predict, int n_threads);
504+
LLAMA_API void llama_beam_search(struct llama_context * ctx, llama_beam_search_callback_fn_t callback, void * callback_data, size_t n_beams, int n_past, int n_predict, int n_threads);
505505

506506
// Performance information
507507
LLAMA_API struct llama_timings llama_get_timings(struct llama_context * ctx);

0 commit comments

Comments
 (0)