0003-embeddings.patch 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. From a9a7820ae111d70e24d4f7004378b5321e8a29c7 Mon Sep 17 00:00:00 2001
  2. From: Michael Yang <mxyng@pm.me>
  3. Date: Mon, 16 Sep 2024 15:53:14 -0700
  4. Subject: [PATCH 03/11] embeddings
  5. ---
  6. src/llama.cpp | 9 ++++++---
  7. 1 file changed, 6 insertions(+), 3 deletions(-)
  8. diff --git a/src/llama.cpp b/src/llama.cpp
  9. index fa09f3b3..d1791af0 100644
  10. --- a/src/llama.cpp
  11. +++ b/src/llama.cpp
  12. @@ -17398,7 +17398,7 @@ static size_t llama_output_reserve(llama_context & lctx, size_t n_outputs) {
  13. const auto n_embd = hparams.n_embd;
  14. // TODO: use a per-batch flag for logits presence instead
  15. - const bool has_logits = !cparams.embeddings;
  16. + const bool has_logits = cparams.causal_attn;
  17. const bool has_embd = cparams.embeddings && (cparams.pooling_type == LLAMA_POOLING_TYPE_NONE);
  18. const size_t logits_size = has_logits ? n_vocab*n_outputs_max : 0;
  19. @@ -17693,7 +17693,6 @@ static int llama_decode_internal(
  20. res = nullptr;
  21. embd = nullptr;
  22. } else if (cparams.embeddings) {
  23. - res = nullptr; // do not extract logits for embedding case
  24. embd = nullptr;
  25. for (int i = ggml_graph_n_nodes(gf) - 1; i >= 0; --i) {
  26. if (strcmp(ggml_graph_node(gf, i)->name, "result_embd_pooled") == 0) {
  27. @@ -17701,11 +17700,15 @@ static int llama_decode_internal(
  28. break;
  29. }
  30. }
  31. - GGML_ASSERT(embd != nullptr && "missing embeddings tensor");
  32. } else {
  33. embd = nullptr; // do not extract embeddings when not needed
  34. GGML_ASSERT(strcmp(res->name, "result_output") == 0 && "missing result_output tensor");
  35. }
  36. +
  37. + if (!cparams.causal_attn) {
  38. + res = nullptr; // do not extract logits when not needed
  39. + }
  40. +
  41. // LLAMA_LOG_INFO("graph build time: %.3f ms (%d nodes, %d leafs)\n", (ggml_time_us() - t_start_us)/1000.0, gf->n_nodes, gf->n_leafs);
  42. ggml_backend_sched_alloc_graph(lctx.sched.get(), gf);
  43. --
  44. 2.46.0