mirror of
https://github.com/LostRuins/koboldcpp.git
synced 2025-09-10 09:04:36 +00:00
bugfixes and support for persistent states
This commit is contained in:
parent
f952b7c613
commit
356c1b87ba
3 changed files with 66 additions and 22 deletions
63
expose.cpp
63
expose.cpp
|
@ -28,7 +28,8 @@ extern "C" {
|
|||
const int top_k;
|
||||
const float top_p;
|
||||
const float rep_pen;
|
||||
const int rep_pen_range;
|
||||
const int rep_pen_range;
|
||||
const bool reset_state = true; //determines if we can continue off the previous prompt state
|
||||
};
|
||||
struct generation_outputs
|
||||
{
|
||||
|
@ -40,7 +41,10 @@ extern "C" {
|
|||
gpt_vocab api_vocab;
|
||||
llama_model api_model;
|
||||
int api_n_past = 0;
|
||||
gpt_vocab::id old_embd_id = -1;
|
||||
std::vector<float> api_logits;
|
||||
std::vector<gpt_vocab::id> last_n_tokens;
|
||||
size_t mem_per_token = 0;
|
||||
|
||||
bool load_model(const load_model_inputs inputs)
|
||||
{
|
||||
|
@ -69,6 +73,12 @@ extern "C" {
|
|||
api_params.temp = inputs.temperature;
|
||||
api_params.repeat_last_n = inputs.rep_pen_range;
|
||||
api_params.repeat_penalty = inputs.rep_pen;
|
||||
|
||||
bool reset_state = inputs.reset_state;
|
||||
if(api_n_past==0)
|
||||
{
|
||||
reset_state = true;
|
||||
}
|
||||
|
||||
if(api_params.repeat_last_n<1)
|
||||
{
|
||||
|
@ -88,42 +98,61 @@ extern "C" {
|
|||
// char * tst2 = (char*)tst.c_str();
|
||||
// gpt_print_usage(1,&tst2,api_params);
|
||||
|
||||
api_params.prompt.insert(0, 1, ' ');
|
||||
if(reset_state)
|
||||
{
|
||||
api_params.prompt.insert(0, 1, ' ');
|
||||
mem_per_token = 0;
|
||||
}
|
||||
// tokenize the prompt
|
||||
std::vector<gpt_vocab::id> embd_inp = ::llama_tokenize(api_vocab, api_params.prompt, true);
|
||||
api_params.n_predict = std::min(api_params.n_predict, api_model.hparams.n_ctx - (int)embd_inp.size());
|
||||
std::vector<gpt_vocab::id> embd;
|
||||
size_t mem_per_token = 0;
|
||||
llama_eval(api_model, api_params.n_threads, 0, {0, 1, 2, 3}, api_logits, mem_per_token);
|
||||
|
||||
|
||||
int last_n_size = api_params.repeat_last_n;
|
||||
std::vector<gpt_vocab::id> last_n_tokens(last_n_size);
|
||||
std::fill(last_n_tokens.begin(), last_n_tokens.end(), 0);
|
||||
last_n_tokens.resize(last_n_size);
|
||||
if(reset_state)
|
||||
{
|
||||
llama_eval(api_model, api_params.n_threads, 0, {0, 1, 2, 3}, api_logits, mem_per_token);
|
||||
std::fill(last_n_tokens.begin(), last_n_tokens.end(), 0);
|
||||
api_n_past = 0;
|
||||
}else{
|
||||
//strip out the reset token (1) at the start of the embedding
|
||||
if(embd_inp.size()>0)
|
||||
{
|
||||
embd_inp.erase(embd_inp.begin());
|
||||
}
|
||||
if(old_embd_id!=-1)
|
||||
{
|
||||
embd.push_back(old_embd_id);
|
||||
}
|
||||
}
|
||||
|
||||
int remaining_tokens = api_params.n_predict;
|
||||
int input_consumed = 0;
|
||||
std::mt19937 api_rng(api_params.seed);
|
||||
|
||||
std::string concat_output = "";
|
||||
std::string concat_output = "";
|
||||
|
||||
while (remaining_tokens > 0)
|
||||
{
|
||||
gpt_vocab::id id = 0;
|
||||
gpt_vocab::id id = 0;
|
||||
// predict
|
||||
if (embd.size() > 0)
|
||||
{
|
||||
|
||||
// for (auto i: embd) {
|
||||
// std::cout << i << ',';
|
||||
// }
|
||||
//printf("\nnp:%d embd:%d mem:%d",api_n_past,embd.size(),mem_per_token);
|
||||
if (!llama_eval(api_model, api_params.n_threads, api_n_past, embd, api_logits, mem_per_token))
|
||||
{
|
||||
fprintf(stderr, "Failed to predict\n");
|
||||
_snprintf_s(output.text,sizeof(output.text),_TRUNCATE,"%s","");
|
||||
snprintf(output.text, sizeof(output.text), "%s", "");
|
||||
output.status = 0;
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
api_n_past += embd.size();
|
||||
embd.clear();
|
||||
|
||||
embd.clear();
|
||||
if (embd_inp.size() <= input_consumed)
|
||||
{
|
||||
// out of user input, sample next token
|
||||
|
@ -148,11 +177,12 @@ extern "C" {
|
|||
}
|
||||
|
||||
// add it to the context
|
||||
old_embd_id = id;
|
||||
embd.push_back(id);
|
||||
|
||||
// decrement remaining sampling budget
|
||||
--remaining_tokens;
|
||||
|
||||
//printf("\nid:%d word:%s\n",id,api_vocab.id_to_token[id].c_str());
|
||||
concat_output += api_vocab.id_to_token[id].c_str();
|
||||
}
|
||||
else
|
||||
|
@ -160,6 +190,7 @@ extern "C" {
|
|||
// some user input remains from prompt or interaction, forward it to processing
|
||||
while (embd_inp.size() > input_consumed)
|
||||
{
|
||||
old_embd_id = embd_inp[input_consumed];
|
||||
embd.push_back(embd_inp[input_consumed]);
|
||||
last_n_tokens.erase(last_n_tokens.begin());
|
||||
last_n_tokens.push_back(embd_inp[input_consumed]);
|
||||
|
@ -175,7 +206,7 @@ extern "C" {
|
|||
|
||||
//printf("output: %s",concat_output.c_str());
|
||||
output.status = 1;
|
||||
_snprintf_s(output.text,sizeof(output.text),_TRUNCATE,"%s",concat_output.c_str());
|
||||
snprintf(output.text, sizeof(output.text), "%s", concat_output.c_str());
|
||||
return output;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue