Skip to content

Commit 557410b

Browse files
compiladeggerganov
andauthored
llama : greatly reduce output buffer memory usage (ggml-org#6122)
* llama : greatly reduce logits memory usage * llama : more compact state saving and reloading * llama : fix lctx.n_outputs not being set before building graph * perplexity : adapt to the logits API changes * perplexity : fix Winogrande, use correct logits for second choice start The first logits used to evaluate the second choice were not from the end of the common prefix; instead, they were the logits from the end of the first choice. This has been corrected. The previous implementation sometimes had outliers in the scores of choices for some tasks, and the logic to skip choices words in the log-likelihood evaluation probably was an attempt to reduce those, but it was complex and didn't quite seem to be the right thing. This is simpler now, and the outlier scores aren't there anymore. * perplexity : normalize spaces and punctuation in Winogrande sentences * llama : fix embedding conditions * llama : fix llama_get_embeddings_ith when the resulting id is 0 * llama : fix wrong n_outputs in llama_set_inputs A mismatch happened when using a smaller n_ubatch than n_batch and then using llama_batch_get_one(). The decision of what n_outputs should be now almost fully depends on how lctx.n_outputs is set in llama_decode_internal. The conditions are simpler this way. * llama : when saving the state, recalculate n_outputs This ensures the correct number of outputs for the entire previous batch is stored in the session file, even when n_ubatch is smaller than n_batch. * llama : fix not-skipping outputs of non-causal models * llama : fix running a batch with n_outputs == 0 It previously worked because lctx.inp_out_ids was not initialized, so it pointed to some garbage address which was somehow still valid when I ran my tests. * llama : keep same graph topology even when n_outputs == 0 * ggml : saner ggml_can_repeat with empty tensors * ggml : future-proof ggml_is_empty by using GGML_MAX_DIMS - 1 * ggml : do not multi-thread ops returning empty tensors * ggml : make ggml_is_empty public and work with views * llama : use a vector for ctx->output_ids * llama : rework reallocation logic for llama_output_reserve Now comparing the actual size with the new total size of the output buffer to allow more efficient enabling and disabling of the embeddings and/or logits output in the future. * ggml : skip empty tensors in all backends * llama : fix llama_output_reserve nullptr deref when new_size is 0 * perplexity : make Winogrande work as it does on master The problems with the Winogrande implementation will need to be fixed in a separate PR to ease review. * llama : clearer error messages for invalid logits or embeddings ids * llama : assert all models that can have inp_out_ids Since the graph topology is now constant, this presence check can be done even when there are no outputs. * llama : assert logits and embd buffers exist before writing to them * llama : handle errors from llama_output_reserve at call sites * perplexity : make hellaswag and multiple-choice outputs identical to master Due to how the KV cache is updated, the logprobs for tokens in a batch are very slightly affected by the other tokens present in the batch, so to make hellaswag and multiple-choice return exactly the same results as on master, the last token of each sequence needs to be evaluated even though its output is not used at all. This will probably be changed back in the future to make these benchmarks a tiny bit faster. * perplexity : fix division by zero when using less than 100 multiple-choice tasks * llama : allow loading state saved with a different ctx size When loading a session file, the context size is now only required to be at least enough to load the KV cells contained in that session file, instead of requiring to use exactly the same context size as when saving. Doing this enables the use-case of extending or shrinking the context size of a saved session. This breaks existing session files because the meaning of kv_buf_size is slightly changed (previously it was the size of the whole KV cache, now it's only the size of the saved part of it). This allows for finer-grained sanity checks when loading in an effort to keep kv_buf_size useful even when the kv_size is changed. * llama : minor ggml-ci * readme : update recent API changes, and warn about Vulkan --------- Co-authored-by: Georgi Gerganov <[email protected]>
1 parent 55c1b2a commit 557410b

16 files changed

+705
-198
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Inference of Meta's [LLaMA](https://arxiv.org/abs/2302.13971) model (and others)
1010

1111
### Recent API changes
1212

13+
- [2024 Mar 26] Logits and embeddings API updated for compactness https://github.com/ggerganov/llama.cpp/pull/6122
1314
- [2024 Mar 13] Add `llama_synchronize()` + `llama_context_params.n_ubatch` https://github.com/ggerganov/llama.cpp/pull/6017
1415
- [2024 Mar 8] `llama_kv_cache_seq_rm()` returns a `bool` instead of `void`, and new `llama_n_seq_max()` returns the upper limit of acceptable `seq_id` in batches (relevant when dealing with multiple sequences) https://github.com/ggerganov/llama.cpp/pull/5328
1516
- [2024 Mar 4] Embeddings API updated https://github.com/ggerganov/llama.cpp/pull/5796
@@ -630,6 +631,15 @@ Building the program with BLAS support may lead to some performance improvements
630631
631632
- #### Vulkan
632633
634+
> [!WARNING]
635+
>
636+
> Vulkan support has been broken in https://github.com/ggerganov/llama.cpp/pull/6122
637+
> due to relying on `GGML_OP_GET_ROWS` which is not yet properly supported by the Vulkan backend,
638+
> but should be fixed relatively soon (possibly in https://github.com/ggerganov/llama.cpp/pull/6155
639+
> (ref: https://github.com/ggerganov/llama.cpp/pull/6122#issuecomment-2015327635)).
640+
>
641+
> Meanwhile, if you want to use the Vulkan backend, you should use the commit right before the breaking change, https://github.com/ggerganov/llama.cpp/commit/55c1b2a3bbd470e9e2a3a0618b92cf64a885f806
642+
633643
**With docker**:
634644
635645
You don't need to install Vulkan SDK. It will be installed inside the container.

examples/imatrix/imatrix.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ static bool compute_imatrix(llama_context * ctx, const gpt_params & params, bool
424424
tokens[batch_start] = llama_token_bos(llama_get_model(ctx));
425425
}
426426

427+
// TODO: use batch.logits to save computations instead of relying on logits_all == true
427428
if (llama_decode(ctx, llama_batch_get_one(tokens.data() + batch_start, batch_size, j * n_batch, 0))) {
428429
fprintf(stderr, "%s : failed to eval\n", __func__);
429430
return false;

examples/parallel/parallel.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ int main(int argc, char ** argv) {
132132
llama_context * ctx = NULL;
133133

134134
// load the target model
135-
params.logits_all = true;
136135
std::tie(model, ctx) = llama_init_from_gpt_params(params);
137136

138137
// load the prompts from an external file if there are any

examples/perplexity/perplexity.cpp

+82-47
Large diffs are not rendered by default.

examples/server/server.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,8 @@ struct server_context {
747747
{
748748
const int32_t n_batch = llama_n_batch(ctx);
749749

750-
batch = llama_batch_init(n_batch, 0, params.n_parallel);
750+
// only a single seq_id per token is needed
751+
batch = llama_batch_init(n_batch, 0, 1);
751752
}
752753

753754
metrics.init();

examples/speculative/speculative.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ int main(int argc, char ** argv) {
6565
llama_context * ctx_dft = NULL;
6666

6767
// load the target model
68-
params.logits_all = true;
6968
std::tie(model_tgt, ctx_tgt) = llama_init_from_gpt_params(params);
7069

7170
// load the draft model

ggml-cuda.cu

+1-1
Original file line numberDiff line numberDiff line change
@@ -2505,7 +2505,7 @@ GGML_CALL static enum ggml_status ggml_backend_cuda_graph_compute(ggml_backend_t
25052505
for (int i = 0; i < cgraph->n_nodes; i++) {
25062506
ggml_tensor * node = cgraph->nodes[i];
25072507

2508-
if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_NONE) {
2508+
if (ggml_is_empty(node) || node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_NONE) {
25092509
continue;
25102510
}
25112511

ggml-kompute.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,10 @@ static void ggml_vk_graph_compute(struct ggml_kompute_context * ctx, struct ggml
14301430
struct ggml_tensor * dst = gf->nodes[i];
14311431
GGML_ASSERT(dst->data != nullptr);
14321432

1433+
if (ggml_is_empty(dst)) {
1434+
continue;
1435+
}
1436+
14331437
switch (dst->op) {
14341438
case GGML_OP_NONE:
14351439
case GGML_OP_RESHAPE:

ggml-metal.m

+4
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,10 @@ static enum ggml_status ggml_metal_graph_compute(
847847
struct ggml_tensor * src2 = gf->nodes[i]->src[2];
848848
struct ggml_tensor * dst = gf->nodes[i];
849849

850+
if (ggml_is_empty(dst)) {
851+
continue;
852+
}
853+
850854
switch (dst->op) {
851855
case GGML_OP_NONE:
852856
case GGML_OP_RESHAPE:

ggml-opencl.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -2234,6 +2234,11 @@ static ggml_backend_buffer_type_t ggml_backend_opencl_get_default_buffer_type(gg
22342234
static ggml_status ggml_backend_opencl_graph_compute(ggml_backend_t backend, ggml_cgraph * graph) {
22352235
for (int i = 0; i < graph->n_nodes; ++i) {
22362236
ggml_tensor * node = graph->nodes[i];
2237+
2238+
if (ggml_is_empty(node)) {
2239+
continue;
2240+
}
2241+
22372242
switch (node->op) {
22382243
case GGML_OP_MUL_MAT:
22392244
ggml_cl_mul_mat(node->src[0], node->src[1], node, nullptr, 0);

ggml-sycl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16973,7 +16973,7 @@ GGML_CALL static ggml_status ggml_backend_sycl_graph_compute(ggml_backend_t back
1697316973
params.ith = 0;
1697416974
for (int i = 0; i < cgraph->n_nodes; i++) {
1697516975
ggml_tensor * node = cgraph->nodes[i];
16976-
if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_NONE) {
16976+
if (ggml_is_empty(node) || node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_NONE) {
1697716977
continue;
1697816978
}
1697916979
#ifndef NDEBUG

ggml-vulkan.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -5566,7 +5566,7 @@ GGML_CALL static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backen
55665566
for (int i = 0; i < cgraph->n_nodes; i++) {
55675567
ggml_tensor * node = cgraph->nodes[i];
55685568

5569-
if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_NONE) {
5569+
if (ggml_is_empty(node) || node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_NONE) {
55705570
continue;
55715571
}
55725572

ggml.c

+18-2
Original file line numberDiff line numberDiff line change
@@ -2607,6 +2607,16 @@ static inline bool ggml_is_padded_1d(const struct ggml_tensor * tensor) {
26072607
tensor->nb[3] == tensor->nb[2]*tensor->ne[2];
26082608
}
26092609

2610+
GGML_CALL bool ggml_is_empty(const struct ggml_tensor * tensor) {
2611+
for (int i = 0; i < GGML_MAX_DIMS; ++i) {
2612+
if (tensor->ne[i] == 0) {
2613+
// empty if any dimension has no elements
2614+
return true;
2615+
}
2616+
}
2617+
return false;
2618+
}
2619+
26102620
bool ggml_are_same_shape(const struct ggml_tensor * t0, const struct ggml_tensor * t1) {
26112621
static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
26122622

@@ -2621,7 +2631,7 @@ bool ggml_are_same_shape(const struct ggml_tensor * t0, const struct ggml_tensor
26212631
static inline bool ggml_can_repeat(const struct ggml_tensor * t0, const struct ggml_tensor * t1) {
26222632
static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
26232633

2624-
return
2634+
return ggml_is_empty(t0) ? ggml_is_empty(t1) :
26252635
(t1->ne[0]%t0->ne[0] == 0) &&
26262636
(t1->ne[1]%t0->ne[1] == 0) &&
26272637
(t1->ne[2]%t0->ne[2] == 0) &&
@@ -16114,7 +16124,7 @@ static void ggml_compute_forward_cross_entropy_loss_back(
1611416124
static void ggml_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
1611516125
GGML_ASSERT(params);
1611616126

16117-
if (tensor->op == GGML_OP_NONE) {
16127+
if (tensor->op == GGML_OP_NONE || ggml_is_empty(tensor)) {
1611816128
return;
1611916129
}
1612016130

@@ -17983,6 +17993,12 @@ static void ggml_graph_compute_perf_stats_node(struct ggml_tensor * node, const
1798317993
static int ggml_get_n_tasks(struct ggml_tensor * node, int n_threads, int n_cur_threads) {
1798417994
int n_tasks = 0;
1798517995

17996+
if (ggml_is_empty(node)) {
17997+
// no need to multi-thread a no-op
17998+
n_tasks = 1;
17999+
return n_tasks;
18000+
}
18001+
1798618002
switch (node->op) {
1798718003
case GGML_OP_CPY:
1798818004
case GGML_OP_DUP:

ggml.h

+1
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ extern "C" {
750750
GGML_API GGML_CALL bool ggml_is_transposed(const struct ggml_tensor * tensor);
751751
GGML_API GGML_CALL bool ggml_is_contiguous(const struct ggml_tensor * tensor);
752752
GGML_API GGML_CALL bool ggml_is_permuted (const struct ggml_tensor * tensor);
753+
GGML_API GGML_CALL bool ggml_is_empty (const struct ggml_tensor * tensor);
753754
GGML_API bool ggml_is_scalar (const struct ggml_tensor * tensor);
754755
GGML_API bool ggml_is_vector (const struct ggml_tensor * tensor);
755756
GGML_API bool ggml_is_matrix (const struct ggml_tensor * tensor);

0 commit comments

Comments
 (0)