llama.cpp icon indicating copy to clipboard operation
llama.cpp copied to clipboard

[Feature Request] Add API for manually setting logits.

Open shaunabanana opened this issue 1 year ago • 2 comments

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • [X] I am running the latest code. Development is very rapid so there are no tagged versions as of now.
  • [X] I carefully followed the README.md.
  • [x] I searched using keywords relevant to my issue to make sure that I am creating a new issue that is not already open (or closed).
  • [X] I reviewed the Discussions, and have a new bug or useful enhancement to share.

Expected Behavior

Add a public API (e.g., llama_set_logits(ctx, logits)) that allows to manually set the logits. This would open the door to bridging llama.cpp with use cases like FUDGE (code) and Grounded Decoding, which guides the generation process by manipulating the probabilities without the need for fine-tuning.

Current Behavior

I understand that there is currently an API function for getting the current logits (llama_get_logits(ctx)), but no way to modify the logits through a public API.

shaunabanana avatar Apr 17 '23 07:04 shaunabanana

The function llama_get_logits() returns the pointer to the logits array in llama.cpp's state. To modify the logits, you just have to write to the array. You can an example in main.cpp itself:

auto logits = llama_get_logits(ctx);
if (params.ignore_eos) {
    logits[llama_token_eos()] = 0;
}

Still, maybe not the best API, better would be something like this which makes a copy:

// length from llama_n_vocab()
LLAMA_API int llama_get_logits(struct llama_context * ctx, float * logits);
LLAMA_API int llama_set_logits(struct llama_context * ctx, const float * logits);
// or maybe
LLAMA_API float llama_get_logit(struct llama_context * ctx, llama_token token);
LLAMA_API void llama_set_logit(struct llama_context * ctx, llama_token, float logit);

SlyEcho avatar Apr 17 '23 08:04 SlyEcho

Thank you for pointing this out! I'll see if I can work out something :)

shaunabanana avatar Apr 17 '23 08:04 shaunabanana