2023-03-10 19:40:58 +01:00
|
|
|
#include "utils.h"
|
2023-03-22 06:32:36 +01:00
|
|
|
#include "ggml.h"
|
|
|
|
#include "llama.h"
|
2023-03-10 19:40:58 +01:00
|
|
|
|
|
|
|
#include <cassert>
|
2023-03-20 11:17:23 +01:00
|
|
|
#include <cinttypes>
|
2023-03-10 19:40:58 +01:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
2023-03-19 20:44:30 +01:00
|
|
|
#include <iostream>
|
2023-03-10 19:40:58 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-03-13 04:08:01 +01:00
|
|
|
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
|
2023-03-12 22:13:28 +01:00
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
2023-03-15 20:56:24 +01:00
|
|
|
#elif defined (_WIN32)
|
|
|
|
#include <signal.h>
|
2023-03-13 04:08:01 +01:00
|
|
|
#endif
|
2023-03-12 22:13:28 +01:00
|
|
|
|
2023-03-21 17:14:46 +01:00
|
|
|
#if defined (_WIN32)
|
|
|
|
#pragma comment(lib,"kernel32.lib")
|
|
|
|
extern "C" __declspec(dllimport) void* __stdcall GetStdHandle(unsigned long nStdHandle);
|
|
|
|
extern "C" __declspec(dllimport) int __stdcall GetConsoleMode(void* hConsoleHandle, unsigned long* lpMode);
|
|
|
|
extern "C" __declspec(dllimport) int __stdcall SetConsoleMode(void* hConsoleHandle, unsigned long dwMode);
|
|
|
|
#endif
|
|
|
|
|
2023-03-12 22:13:28 +01:00
|
|
|
#define ANSI_COLOR_RED "\x1b[31m"
|
|
|
|
#define ANSI_COLOR_GREEN "\x1b[32m"
|
|
|
|
#define ANSI_COLOR_YELLOW "\x1b[33m"
|
|
|
|
#define ANSI_COLOR_BLUE "\x1b[34m"
|
|
|
|
#define ANSI_COLOR_MAGENTA "\x1b[35m"
|
|
|
|
#define ANSI_COLOR_CYAN "\x1b[36m"
|
|
|
|
#define ANSI_COLOR_RESET "\x1b[0m"
|
|
|
|
#define ANSI_BOLD "\x1b[1m"
|
|
|
|
|
2023-03-21 18:11:01 +01:00
|
|
|
/* Keep track of current color of output, and emit ANSI code if it changes. */
|
|
|
|
enum console_state {
|
|
|
|
CONSOLE_STATE_DEFAULT=0,
|
|
|
|
CONSOLE_STATE_PROMPT,
|
|
|
|
CONSOLE_STATE_USER_INPUT
|
2023-03-22 06:32:36 +01:00
|
|
|
};
|
2023-03-21 18:11:01 +01:00
|
|
|
|
|
|
|
static console_state con_st = CONSOLE_STATE_DEFAULT;
|
|
|
|
static bool con_use_color = false;
|
|
|
|
|
|
|
|
void set_console_state(console_state new_st)
|
|
|
|
{
|
|
|
|
if (!con_use_color) return;
|
|
|
|
// only emit color code if state changed
|
|
|
|
if (new_st != con_st) {
|
|
|
|
con_st = new_st;
|
|
|
|
switch(con_st) {
|
|
|
|
case CONSOLE_STATE_DEFAULT:
|
|
|
|
printf(ANSI_COLOR_RESET);
|
|
|
|
return;
|
|
|
|
case CONSOLE_STATE_PROMPT:
|
|
|
|
printf(ANSI_COLOR_YELLOW);
|
|
|
|
return;
|
|
|
|
case CONSOLE_STATE_USER_INPUT:
|
|
|
|
printf(ANSI_BOLD ANSI_COLOR_GREEN);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 17:27:42 +01:00
|
|
|
std::vector<double> softmax(const std::vector<float>& logits) {
|
|
|
|
std::vector<double> probs(logits.size());
|
|
|
|
float max_logit = logits[0];
|
|
|
|
for (float v : logits) max_logit = std::max(max_logit, v);
|
|
|
|
double sum_exp = 0.0;
|
|
|
|
for (size_t i = 0; i < logits.size(); i++) {
|
|
|
|
// Subtract the maximum logit value from the current logit value for numerical stability
|
|
|
|
float logit = logits[i] - max_logit;
|
|
|
|
double exp_logit = std::exp(logit);
|
|
|
|
sum_exp += exp_logit;
|
|
|
|
probs[i] = exp_logit;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < probs.size(); i++) probs[i] /= sum_exp;
|
|
|
|
return probs;
|
|
|
|
}
|
|
|
|
|
2023-03-22 06:32:36 +01:00
|
|
|
void perplexity(llama_context * ctx, const gpt_params & params) {
|
2023-03-21 17:27:42 +01:00
|
|
|
// Download: https://s3.amazonaws.com/research.metamind.io/wikitext/wikitext-2-raw-v1.zip?ref=salesforce-research
|
|
|
|
// Run `./main --perplexity -m models/7B/ggml-model-q4_0.bin -f wiki.test.raw`
|
|
|
|
// Output: `perplexity: 13.5106 [114/114]`
|
2023-03-22 17:09:38 +01:00
|
|
|
auto tokens = ::llama_tokenize(ctx, params.prompt, true);
|
2023-03-21 17:27:42 +01:00
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
double nll = 0.0;
|
|
|
|
int seq_count = tokens.size() / params.n_ctx;
|
2023-03-22 06:32:36 +01:00
|
|
|
|
|
|
|
fprintf(stderr, "%s : calculating perplexity over %d chunks\n", __func__, seq_count);
|
|
|
|
|
2023-03-21 17:27:42 +01:00
|
|
|
for (int i = 0; i < seq_count; ++i) {
|
|
|
|
int start = i * params.n_ctx;
|
|
|
|
int end = start + params.n_ctx - 1;
|
2023-03-22 06:32:36 +01:00
|
|
|
std::vector<llama_token> embd(tokens.begin() + start, tokens.begin() + end);
|
2023-03-21 17:27:42 +01:00
|
|
|
auto start_t = std::chrono::high_resolution_clock::now();
|
2023-03-22 06:32:36 +01:00
|
|
|
if (llama_eval(ctx, embd.data(), embd.size(), 0, params.n_threads)) {
|
|
|
|
fprintf(stderr, "%s : failed to eval\n", __func__);
|
2023-03-21 17:27:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto end_t = std::chrono::high_resolution_clock::now();
|
|
|
|
if (i == 0) {
|
|
|
|
double seconds = std::chrono::duration<double>(end_t - start_t).count();
|
|
|
|
printf("%.2f seconds per pass - ETA %.2f hours\n", seconds, (seconds * seq_count) / (60.0*60.0));
|
|
|
|
}
|
|
|
|
// We get the logits for all the tokens in the context window (params.n_ctx)
|
|
|
|
// from llama_eval above. Now, based on https://huggingface.co/docs/transformers/perplexity,
|
|
|
|
// calculate the perplexity over the last half the window (so the model always has
|
|
|
|
// some context to predict the token).
|
|
|
|
//
|
|
|
|
// We rely on the fact that attention in the forward pass only looks at previous
|
|
|
|
// tokens here, so the logits returned for each token are an accurate representation
|
|
|
|
// of what the model would have predicted at that point.
|
|
|
|
//
|
|
|
|
// Example, we have a context window of 512, we will compute perplexity for each of the
|
|
|
|
// last 256 tokens. Then, we split the input up into context window size chunks to
|
|
|
|
// process the entire prompt.
|
2023-03-22 06:32:36 +01:00
|
|
|
|
|
|
|
auto logits = llama_get_logits(ctx);
|
2023-03-21 17:27:42 +01:00
|
|
|
for (int j = params.n_ctx / 2; j < params.n_ctx - 1; ++j) {
|
|
|
|
// Calculate probability of next token, given the previous ones.
|
2023-03-22 06:32:36 +01:00
|
|
|
int n_vocab = llama_n_vocab(ctx);
|
2023-03-21 17:27:42 +01:00
|
|
|
std::vector<float> tok_logits(
|
2023-03-22 06:32:36 +01:00
|
|
|
logits + j * n_vocab,
|
|
|
|
logits + (j + 1) * n_vocab);
|
2023-03-21 17:27:42 +01:00
|
|
|
double prob = softmax(tok_logits)[tokens[start + j + 1]];
|
|
|
|
nll += -std::log(prob);
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
// perplexity is e^(average negative log-likelihood)
|
|
|
|
printf("[%d]%.4lf,", i + 1, std::exp(nll / count));
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2023-03-12 22:13:28 +01:00
|
|
|
static bool is_interacting = false;
|
|
|
|
|
2023-03-15 20:56:24 +01:00
|
|
|
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) || defined (_WIN32)
|
2023-03-12 22:13:28 +01:00
|
|
|
void sigint_handler(int signo) {
|
2023-03-21 18:11:01 +01:00
|
|
|
set_console_state(CONSOLE_STATE_DEFAULT);
|
2023-03-19 19:10:00 +01:00
|
|
|
printf("\n"); // this also force flush stdout.
|
2023-03-12 22:13:28 +01:00
|
|
|
if (signo == SIGINT) {
|
|
|
|
if (!is_interacting) {
|
|
|
|
is_interacting=true;
|
|
|
|
} else {
|
|
|
|
_exit(130);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-13 04:08:01 +01:00
|
|
|
#endif
|
2023-03-12 22:13:28 +01:00
|
|
|
|
2023-03-10 19:40:58 +01:00
|
|
|
int main(int argc, char ** argv) {
|
2023-03-22 06:32:36 +01:00
|
|
|
// has to be called once at the start of the program to init ggml stuff
|
2023-03-12 21:15:00 +01:00
|
|
|
ggml_time_init();
|
2023-03-10 19:40:58 +01:00
|
|
|
|
|
|
|
gpt_params params;
|
|
|
|
params.model = "models/llama-7B/ggml-model.bin";
|
|
|
|
|
|
|
|
if (gpt_params_parse(argc, argv, params) == false) {
|
|
|
|
return 1;
|
|
|
|
}
|
2023-03-19 17:37:02 +01:00
|
|
|
|
2023-03-19 01:10:47 +01:00
|
|
|
if (params.n_ctx > 2048) {
|
|
|
|
fprintf(stderr, "%s: warning: model does not support context sizes greater than 2048 tokens (%d specified);"
|
|
|
|
"expect poor results\n", __func__, params.n_ctx);
|
|
|
|
}
|
2023-03-10 19:40:58 +01:00
|
|
|
|
2023-03-22 06:47:15 +01:00
|
|
|
if (params.seed <= 0) {
|
2023-03-10 19:40:58 +01:00
|
|
|
params.seed = time(NULL);
|
|
|
|
}
|
|
|
|
|
2023-03-13 17:39:56 +01:00
|
|
|
fprintf(stderr, "%s: seed = %d\n", __func__, params.seed);
|
2023-03-10 19:40:58 +01:00
|
|
|
|
|
|
|
std::mt19937 rng(params.seed);
|
2023-03-19 19:36:19 +01:00
|
|
|
if (params.random_prompt) {
|
2023-03-10 19:40:58 +01:00
|
|
|
params.prompt = gpt_random_prompt(rng);
|
|
|
|
}
|
|
|
|
|
2023-03-21 18:11:01 +01:00
|
|
|
// save choice to use color for later
|
|
|
|
// (note for later: this is a slightly awkward choice)
|
|
|
|
con_use_color = params.use_color;
|
|
|
|
|
2023-03-10 22:46:39 +01:00
|
|
|
// params.prompt = R"(// this function checks if the number n is prime
|
|
|
|
//bool is_prime(int n) {)";
|
|
|
|
|
2023-03-22 06:32:36 +01:00
|
|
|
llama_context * ctx;
|
2023-03-10 19:40:58 +01:00
|
|
|
|
|
|
|
// load the model
|
|
|
|
{
|
2023-03-22 06:32:36 +01:00
|
|
|
auto lparams = llama_context_default_params();
|
|
|
|
|
2023-03-22 06:45:00 +01:00
|
|
|
lparams.n_ctx = params.n_ctx;
|
|
|
|
lparams.n_parts = params.n_parts;
|
|
|
|
lparams.seed = params.seed;
|
|
|
|
lparams.f16_kv = params.memory_f16;
|
2023-03-22 06:32:36 +01:00
|
|
|
lparams.logits_all = params.perplexity;
|
|
|
|
|
|
|
|
ctx = llama_init_from_file(params.model.c_str(), lparams);
|
|
|
|
|
|
|
|
if (ctx == NULL) {
|
|
|
|
fprintf(stderr, "%s: error: failed to load model '%s'\n", __func__, params.model.c_str());
|
2023-03-10 19:40:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 18:15:08 +01:00
|
|
|
// print system information
|
|
|
|
{
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fprintf(stderr, "system_info: n_threads = %d / %d | %s\n",
|
|
|
|
params.n_threads, std::thread::hardware_concurrency(), llama_print_system_info());
|
|
|
|
}
|
|
|
|
|
2023-03-21 17:27:42 +01:00
|
|
|
// determine the required inference memory per token:
|
2023-03-22 06:32:36 +01:00
|
|
|
// TODO: better way to do that
|
|
|
|
{
|
|
|
|
const std::vector<llama_token> tmp = { 0, 1, 2, 3 };
|
|
|
|
llama_eval(ctx, tmp.data(), tmp.size(), 0, params.n_threads);
|
|
|
|
}
|
2023-03-21 17:27:42 +01:00
|
|
|
|
|
|
|
if (params.perplexity) {
|
2023-03-22 06:32:36 +01:00
|
|
|
perplexity(ctx, params);
|
2023-03-21 17:27:42 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2023-03-10 19:40:58 +01:00
|
|
|
int n_past = 0;
|
|
|
|
|
2023-03-17 21:05:58 +01:00
|
|
|
// Add a space in front of the first character to match OG llama tokenizer behavior
|
|
|
|
params.prompt.insert(0, 1, ' ');
|
2023-03-22 06:32:36 +01:00
|
|
|
|
2023-03-10 19:40:58 +01:00
|
|
|
// tokenize the prompt
|
2023-03-22 06:32:36 +01:00
|
|
|
auto embd_inp = ::llama_tokenize(ctx, params.prompt, true);
|
2023-03-10 19:40:58 +01:00
|
|
|
|
2023-03-22 06:32:36 +01:00
|
|
|
const int n_ctx = llama_n_ctx(ctx);
|
|
|
|
|
|
|
|
params.n_predict = std::min(params.n_predict, n_ctx - (int) embd_inp.size());
|
2023-03-10 19:40:58 +01:00
|
|
|
|
2023-03-19 17:37:02 +01:00
|
|
|
// prefix & suffix for instruct mode
|
2023-03-22 06:32:36 +01:00
|
|
|
const auto inp_pfx = ::llama_tokenize(ctx, "\n\n### Instruction:\n\n", true);
|
|
|
|
const auto inp_sfx = ::llama_tokenize(ctx, "\n\n### Response:\n\n", false);
|
2023-03-19 17:37:02 +01:00
|
|
|
|
|
|
|
// in instruct mode, we inject a prefix and a suffix to each input by the user
|
|
|
|
if (params.instruct) {
|
|
|
|
params.interactive = true;
|
2023-03-19 20:33:06 +01:00
|
|
|
params.antiprompt.push_back("### Instruction:\n\n");
|
2023-03-19 17:37:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// enable interactive mode if reverse prompt is specified
|
2023-03-21 17:04:43 +01:00
|
|
|
if (params.antiprompt.size() != 0) {
|
2023-03-19 17:37:02 +01:00
|
|
|
params.interactive = true;
|
|
|
|
}
|
|
|
|
|
2023-03-13 17:39:56 +01:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fprintf(stderr, "%s: prompt: '%s'\n", __func__, params.prompt.c_str());
|
|
|
|
fprintf(stderr, "%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size());
|
2023-03-10 19:40:58 +01:00
|
|
|
for (int i = 0; i < (int) embd_inp.size(); i++) {
|
2023-03-22 06:32:36 +01:00
|
|
|
fprintf(stderr, "%6d -> '%s'\n", embd_inp[i], llama_token_to_str(ctx, embd_inp[i]));
|
2023-03-10 19:40:58 +01:00
|
|
|
}
|
2023-03-13 17:39:56 +01:00
|
|
|
fprintf(stderr, "\n");
|
2023-03-12 22:13:28 +01:00
|
|
|
if (params.interactive) {
|
2023-03-13 04:08:01 +01:00
|
|
|
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
|
2023-03-12 22:13:28 +01:00
|
|
|
struct sigaction sigint_action;
|
|
|
|
sigint_action.sa_handler = sigint_handler;
|
|
|
|
sigemptyset (&sigint_action.sa_mask);
|
2023-03-13 18:15:08 +01:00
|
|
|
sigint_action.sa_flags = 0;
|
2023-03-12 22:13:28 +01:00
|
|
|
sigaction(SIGINT, &sigint_action, NULL);
|
2023-03-15 20:56:24 +01:00
|
|
|
#elif defined (_WIN32)
|
|
|
|
signal(SIGINT, sigint_handler);
|
2023-03-13 04:08:01 +01:00
|
|
|
#endif
|
2023-03-12 22:13:28 +01:00
|
|
|
|
2023-03-13 17:39:56 +01:00
|
|
|
fprintf(stderr, "%s: interactive mode on.\n", __func__);
|
2023-03-12 22:13:28 +01:00
|
|
|
|
2023-03-21 17:04:43 +01:00
|
|
|
if(params.antiprompt.size()) {
|
|
|
|
for (auto antiprompt : params.antiprompt) {
|
|
|
|
fprintf(stderr, "Reverse prompt: '%s'\n", antiprompt.c_str());
|
2023-03-12 22:13:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-13 17:39:56 +01:00
|
|
|
fprintf(stderr, "sampling parameters: temp = %f, top_k = %d, top_p = %f, repeat_last_n = %i, repeat_penalty = %f\n", params.temp, params.top_k, params.top_p, params.repeat_last_n, params.repeat_penalty);
|
|
|
|
fprintf(stderr, "\n\n");
|
2023-03-10 19:40:58 +01:00
|
|
|
|
2023-03-22 06:32:36 +01:00
|
|
|
std::vector<llama_token> embd;
|
2023-03-10 19:40:58 +01:00
|
|
|
|
2023-03-12 10:27:42 +01:00
|
|
|
int last_n_size = params.repeat_last_n;
|
2023-03-22 06:32:36 +01:00
|
|
|
std::vector<llama_token> last_n_tokens(last_n_size);
|
2023-03-12 10:27:42 +01:00
|
|
|
std::fill(last_n_tokens.begin(), last_n_tokens.end(), 0);
|
|
|
|
|
2023-03-12 22:13:28 +01:00
|
|
|
if (params.interactive) {
|
2023-03-13 17:39:56 +01:00
|
|
|
fprintf(stderr, "== Running in interactive mode. ==\n"
|
2023-03-15 20:56:24 +01:00
|
|
|
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) || defined (_WIN32)
|
2023-03-12 22:13:28 +01:00
|
|
|
" - Press Ctrl+C to interject at any time.\n"
|
2023-03-13 04:08:01 +01:00
|
|
|
#endif
|
2023-03-12 22:13:28 +01:00
|
|
|
" - Press Return to return control to LLaMa.\n"
|
2023-03-19 17:37:02 +01:00
|
|
|
" - If you want to submit another line, end your input in '\\'.\n\n");
|
|
|
|
is_interacting = true;
|
2023-03-12 22:13:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int input_consumed = 0;
|
|
|
|
bool input_noecho = false;
|
|
|
|
|
2023-03-19 17:37:02 +01:00
|
|
|
int remaining_tokens = params.n_predict;
|
2023-03-12 22:13:28 +01:00
|
|
|
|
2023-03-21 17:14:46 +01:00
|
|
|
#if defined (_WIN32)
|
2023-03-21 18:11:01 +01:00
|
|
|
if (params.use_color) {
|
2023-03-21 17:14:46 +01:00
|
|
|
// Enable ANSI colors on Windows 10+
|
|
|
|
unsigned long dwMode = 0;
|
|
|
|
void* hConOut = GetStdHandle((unsigned long)-11); // STD_OUTPUT_HANDLE (-11)
|
|
|
|
if (hConOut && hConOut != (void*)-1 && GetConsoleMode(hConOut, &dwMode) && !(dwMode & 0x4)) {
|
|
|
|
SetConsoleMode(hConOut, dwMode | 0x4); // ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4)
|
|
|
|
}
|
2023-03-12 22:13:28 +01:00
|
|
|
}
|
2023-03-21 18:11:01 +01:00
|
|
|
#endif
|
|
|
|
// the first thing we will do is to output the prompt, so set color accordingly
|
|
|
|
set_console_state(CONSOLE_STATE_PROMPT);
|
2023-03-12 22:13:28 +01:00
|
|
|
|
2023-03-19 17:37:02 +01:00
|
|
|
while (remaining_tokens > 0 || params.interactive) {
|
2023-03-10 19:40:58 +01:00
|
|
|
// predict
|
|
|
|
if (embd.size() > 0) {
|
2023-03-22 06:32:36 +01:00
|
|
|
if (llama_eval(ctx, embd.data(), embd.size(), n_past, params.n_threads)) {
|
|
|
|
fprintf(stderr, "%s : failed to eval\n", __func__);
|
2023-03-10 19:40:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n_past += embd.size();
|
|
|
|
embd.clear();
|
|
|
|
|
2023-03-21 16:59:16 +01:00
|
|
|
if ((int) embd_inp.size() <= input_consumed) {
|
2023-03-12 23:07:34 +01:00
|
|
|
// out of user input, sample next token
|
2023-03-22 06:32:36 +01:00
|
|
|
const float top_k = params.top_k;
|
|
|
|
const float top_p = params.top_p;
|
|
|
|
const float temp = params.temp;
|
2023-03-12 10:27:42 +01:00
|
|
|
const float repeat_penalty = params.repeat_penalty;
|
2023-03-10 19:40:58 +01:00
|
|
|
|
2023-03-22 06:32:36 +01:00
|
|
|
llama_token id = 0;
|
2023-03-10 19:40:58 +01:00
|
|
|
|
|
|
|
{
|
2023-03-22 06:32:36 +01:00
|
|
|
auto logits = llama_get_logits(ctx);
|
2023-03-10 19:40:58 +01:00
|
|
|
|
2023-03-19 19:22:48 +01:00
|
|
|
if (params.ignore_eos) {
|
|
|
|
// set the logit of the eos token to zero to avoid sampling it
|
2023-03-22 06:32:36 +01:00
|
|
|
//logits[logits.size() - n_vocab + EOS_TOKEN_ID] = 0;
|
|
|
|
// TODO: this does not work of params.logits_all == true
|
|
|
|
assert(params.perplexity == false);
|
|
|
|
logits[llama_token_eos()] = 0;
|
2023-03-19 19:22:48 +01:00
|
|
|
}
|
|
|
|
|
2023-03-22 06:32:36 +01:00
|
|
|
id = llama_sample_top_p_top_k(ctx, last_n_tokens.data(), last_n_tokens.size(), top_k, top_p, temp, repeat_penalty);
|
2023-03-12 10:27:42 +01:00
|
|
|
|
|
|
|
last_n_tokens.erase(last_n_tokens.begin());
|
|
|
|
last_n_tokens.push_back(id);
|
2023-03-10 19:40:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// add it to the context
|
|
|
|
embd.push_back(id);
|
2023-03-12 22:13:28 +01:00
|
|
|
|
|
|
|
// echo this to console
|
|
|
|
input_noecho = false;
|
|
|
|
|
|
|
|
// decrement remaining sampling budget
|
|
|
|
--remaining_tokens;
|
2023-03-10 19:40:58 +01:00
|
|
|
} else {
|
2023-03-12 23:07:34 +01:00
|
|
|
// some user input remains from prompt or interaction, forward it to processing
|
2023-03-21 16:59:16 +01:00
|
|
|
while ((int) embd_inp.size() > input_consumed) {
|
2023-03-12 22:13:28 +01:00
|
|
|
embd.push_back(embd_inp[input_consumed]);
|
2023-03-12 10:27:42 +01:00
|
|
|
last_n_tokens.erase(last_n_tokens.begin());
|
2023-03-12 22:13:28 +01:00
|
|
|
last_n_tokens.push_back(embd_inp[input_consumed]);
|
|
|
|
++input_consumed;
|
2023-03-19 18:46:32 +01:00
|
|
|
if ((int) embd.size() >= params.n_batch) {
|
2023-03-10 19:40:58 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// display text
|
2023-03-12 22:13:28 +01:00
|
|
|
if (!input_noecho) {
|
|
|
|
for (auto id : embd) {
|
2023-03-22 06:32:36 +01:00
|
|
|
printf("%s", llama_token_to_str(ctx, id));
|
2023-03-12 22:13:28 +01:00
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2023-03-19 20:44:30 +01:00
|
|
|
// reset color to default if we there is no pending user input
|
2023-03-21 18:11:01 +01:00
|
|
|
if (!input_noecho && (int)embd_inp.size() == input_consumed) {
|
|
|
|
set_console_state(CONSOLE_STATE_DEFAULT);
|
2023-03-19 20:44:30 +01:00
|
|
|
}
|
2023-03-12 22:13:28 +01:00
|
|
|
|
|
|
|
// in interactive mode, and not currently processing queued inputs;
|
|
|
|
// check if we should prompt the user for more
|
2023-03-21 16:59:16 +01:00
|
|
|
if (params.interactive && (int) embd_inp.size() <= input_consumed) {
|
2023-03-12 22:13:28 +01:00
|
|
|
// check for reverse prompt
|
2023-03-21 17:04:43 +01:00
|
|
|
std::string last_output;
|
|
|
|
for (auto id : last_n_tokens) {
|
2023-03-22 06:32:36 +01:00
|
|
|
last_output += llama_token_to_str(ctx, id);
|
2023-03-21 17:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if each of the reverse prompts appears at the end of the output.
|
|
|
|
for (std::string antiprompt : params.antiprompt) {
|
|
|
|
if (last_output.find(antiprompt.c_str(), last_output.length() - antiprompt.length(), antiprompt.length()) != std::string::npos) {
|
2023-03-19 20:33:06 +01:00
|
|
|
is_interacting = true;
|
|
|
|
break;
|
|
|
|
}
|
2023-03-12 22:13:28 +01:00
|
|
|
}
|
|
|
|
if (is_interacting) {
|
2023-03-21 18:11:01 +01:00
|
|
|
// potentially set color to indicate we are taking user input
|
|
|
|
set_console_state(CONSOLE_STATE_USER_INPUT);
|
|
|
|
|
2023-03-19 17:37:02 +01:00
|
|
|
if (params.instruct) {
|
|
|
|
input_consumed = embd_inp.size();
|
|
|
|
embd_inp.insert(embd_inp.end(), inp_pfx.begin(), inp_pfx.end());
|
|
|
|
|
|
|
|
printf("\n> ");
|
|
|
|
}
|
|
|
|
|
2023-03-19 20:44:30 +01:00
|
|
|
std::string buffer;
|
|
|
|
std::string line;
|
2023-03-19 17:37:02 +01:00
|
|
|
bool another_line = true;
|
2023-03-19 20:44:30 +01:00
|
|
|
do {
|
|
|
|
std::getline(std::cin, line);
|
|
|
|
if (line.empty() || line.back() != '\\') {
|
2023-03-12 22:13:28 +01:00
|
|
|
another_line = false;
|
2023-03-19 20:44:30 +01:00
|
|
|
} else {
|
|
|
|
line.pop_back(); // Remove the continue character
|
2023-03-19 17:37:02 +01:00
|
|
|
}
|
2023-03-19 20:44:30 +01:00
|
|
|
buffer += line + '\n'; // Append the line to the result
|
|
|
|
} while (another_line);
|
2023-03-21 18:11:01 +01:00
|
|
|
|
|
|
|
// done taking input, reset color
|
|
|
|
set_console_state(CONSOLE_STATE_DEFAULT);
|
2023-03-19 17:37:02 +01:00
|
|
|
|
2023-03-22 06:32:36 +01:00
|
|
|
auto line_inp = ::llama_tokenize(ctx, buffer, false);
|
2023-03-19 20:44:30 +01:00
|
|
|
embd_inp.insert(embd_inp.end(), line_inp.begin(), line_inp.end());
|
2023-03-13 00:35:51 +01:00
|
|
|
|
2023-03-19 20:44:30 +01:00
|
|
|
if (params.instruct) {
|
|
|
|
embd_inp.insert(embd_inp.end(), inp_sfx.begin(), inp_sfx.end());
|
2023-03-12 22:13:28 +01:00
|
|
|
}
|
|
|
|
|
2023-03-19 20:44:30 +01:00
|
|
|
remaining_tokens -= line_inp.size();
|
|
|
|
|
|
|
|
input_noecho = true; // do not echo this again
|
2023-03-12 22:13:28 +01:00
|
|
|
}
|
2023-03-19 20:44:30 +01:00
|
|
|
is_interacting = false;
|
2023-03-10 19:40:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// end of text token
|
2023-03-22 06:32:36 +01:00
|
|
|
if (embd.back() == llama_token_eos()) {
|
2023-03-19 17:37:02 +01:00
|
|
|
if (params.interactive) {
|
|
|
|
is_interacting = true;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, " [end of text]\n");
|
|
|
|
break;
|
|
|
|
}
|
2023-03-10 19:40:58 +01:00
|
|
|
}
|
2023-03-19 19:31:17 +01:00
|
|
|
|
|
|
|
// In interactive mode, respect the maximum number of tokens and drop back to user input when reached.
|
|
|
|
if (params.interactive && remaining_tokens <= 0) {
|
|
|
|
remaining_tokens = params.n_predict;
|
|
|
|
is_interacting = true;
|
|
|
|
}
|
2023-03-10 19:40:58 +01:00
|
|
|
}
|
|
|
|
|
2023-03-15 20:56:24 +01:00
|
|
|
#if defined (_WIN32)
|
|
|
|
signal(SIGINT, SIG_DFL);
|
|
|
|
#endif
|
2023-03-12 22:13:28 +01:00
|
|
|
|
2023-03-22 06:32:36 +01:00
|
|
|
llama_print_timings(ctx);
|
2023-03-10 19:40:58 +01:00
|
|
|
|
2023-03-22 06:32:36 +01:00
|
|
|
llama_free(ctx);
|
2023-03-10 19:40:58 +01:00
|
|
|
|
2023-03-21 18:11:01 +01:00
|
|
|
set_console_state(CONSOLE_STATE_DEFAULT);
|
2023-03-15 20:39:38 +01:00
|
|
|
|
2023-03-10 19:40:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|