Skip to content

Commit 18d1c14

Browse files
committed
llama : minimize swaps when reordering logits
This reduces overhead when running hellaswag on thousands of sequences with very small 100k params Mamba models.
1 parent 72eea49 commit 18d1c14

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

llama.cpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19828,33 +19828,43 @@ void llama_synchronize(struct llama_context * ctx) {
1982819828
static void llama_reorder_outputs(struct llama_context * ctx) {
1982919829
std::vector<size_t> & out_ids = ctx->sbatch.out_ids;
1983019830
if (!out_ids.empty()) {
19831-
std::vector<float> logits_tmp;
19832-
std::vector<float> embd_tmp;
1983319831
uint32_t n_vocab = ctx->model.hparams.n_vocab;
1983419832
uint32_t n_embd = ctx->model.hparams.n_embd;
1983519833
int32_t n_outputs = ctx->n_outputs;
1983619834
GGML_ASSERT((size_t) n_outputs == out_ids.size());
19837-
// insertion sort (from https://en.wikipedia.org/wiki/Insertion_sort, but using memmove)
19838-
for (int32_t i = 1; i < n_outputs; ++i) {
19839-
int32_t j = i;
19840-
size_t out_id_tmp = out_ids[i];
19841-
while (j > 0 && out_ids[j - 1] > out_id_tmp) { j -= 1; }
19842-
if (i - j == 0) { continue; }
19843-
memmove(out_ids.data() + j + 1, out_ids.data() + j, (i - j)*sizeof(out_ids[0]));
19844-
out_ids[j] = out_id_tmp;
19835+
{
19836+
bool is_already_sorted = true;
19837+
for (int32_t i = 0; i < n_outputs - 1; ++i) {
19838+
if (out_ids[i] > out_ids[i + 1]) {
19839+
is_already_sorted = false;
19840+
break;
19841+
}
19842+
}
19843+
if (is_already_sorted) {
19844+
out_ids.clear();
19845+
return;
19846+
}
19847+
}
19848+
// TODO: is there something more efficient which also minimizes swaps?
19849+
// selection sort, to minimize swaps (from https://en.wikipedia.org/wiki/Selection_sort)
19850+
for (int32_t i = 0; i < n_outputs - 1; ++i) {
19851+
int32_t j_min = i;
19852+
for (int32_t j = i + 1; j < n_outputs; ++j) {
19853+
if (out_ids[j] < out_ids[j_min]) {
19854+
j_min = j;
19855+
}
19856+
}
19857+
if (j_min == i) { continue; }
19858+
std::swap(out_ids[i], out_ids[j_min]);
1984519859
if (ctx->logits_size > 0) {
19846-
// only allocate once something needs to be moved
19847-
if (logits_tmp.empty()) { logits_tmp.resize(n_vocab); }
19848-
memcpy(logits_tmp.data(), ctx->logits + i*n_vocab, n_vocab*sizeof(float));
19849-
memmove(ctx->logits + (j + 1)*n_vocab, ctx->logits + j*n_vocab, (i - j)*n_vocab*sizeof(float));
19850-
memcpy(ctx->logits + j*n_vocab, logits_tmp.data(), n_vocab*sizeof(float));
19860+
for (uint32_t k = 0; k < n_vocab; k++) {
19861+
std::swap(ctx->logits[i*n_vocab + k], ctx->logits[j_min*n_vocab + k]);
19862+
}
1985119863
}
1985219864
if (ctx->embd_size > 0) {
19853-
// only allocate once something needs to be moved
19854-
if (embd_tmp.empty()) { embd_tmp.resize(n_embd); }
19855-
memcpy(embd_tmp.data(), ctx->embd + i*n_embd, n_embd*sizeof(float));
19856-
memmove(ctx->embd + (j + 1)*n_embd, ctx->embd + j*n_embd, (i - j)*n_embd*sizeof(float));
19857-
memcpy(ctx->embd + j*n_embd, embd_tmp.data(), n_embd*sizeof(float));
19865+
for (uint32_t k = 0; k < n_embd; k++) {
19866+
std::swap(ctx->embd[i*n_embd + k], ctx->embd[j_min*n_embd + k]);
19867+
}
1985819868
}
1985919869
}
1986019870
std::fill(ctx->output_ids.begin(), ctx->output_ids.end(), -1);

0 commit comments

Comments
 (0)