using json = nlohmann::ordered_json;
-llama_arg & llama_arg::set_examples(std::initializer_list<enum llama_example> examples) {
+common_arg & common_arg::set_examples(std::initializer_list<enum llama_example> examples) {
this->examples = std::move(examples);
return *this;
}
-llama_arg & llama_arg::set_env(const char * env) {
+common_arg & common_arg::set_env(const char * env) {
help = help + "\n(env: " + env + ")";
this->env = env;
return *this;
}
-llama_arg & llama_arg::set_sparam() {
+common_arg & common_arg::set_sparam() {
is_sparam = true;
return *this;
}
-bool llama_arg::in_example(enum llama_example ex) {
+bool common_arg::in_example(enum llama_example ex) {
return examples.find(ex) != examples.end();
}
-bool llama_arg::get_value_from_env(std::string & output) {
+bool common_arg::get_value_from_env(std::string & output) {
if (env == nullptr) return false;
char * value = std::getenv(env);
if (value) {
return false;
}
-bool llama_arg::has_value_from_env() {
+bool common_arg::has_value_from_env() {
return env != nullptr && std::getenv(env);
}
return result;
}
-std::string llama_arg::to_string() {
+std::string common_arg::to_string() {
// params for printing to console
const static int n_leading_spaces = 40;
const static int n_char_per_line_help = 70; // TODO: detect this based on current console
return std::string(buf.data(), size);
}
-static void gpt_params_handle_model_default(gpt_params & params) {
+static void common_params_handle_model_default(common_params & params) {
if (!params.hf_repo.empty()) {
// short-hand to avoid specifying --hf-file -> default it to --model
if (params.hf_file.empty()) {
// CLI argument parsing functions
//
-static bool gpt_params_parse_ex(int argc, char ** argv, gpt_params_context & ctx_arg) {
+static bool common_params_parse_ex(int argc, char ** argv, common_params_context & ctx_arg) {
std::string arg;
const std::string arg_prefix = "--";
- gpt_params & params = ctx_arg.params;
+ common_params & params = ctx_arg.params;
- std::unordered_map<std::string, llama_arg *> arg_to_options;
+ std::unordered_map<std::string, common_arg *> arg_to_options;
for (auto & opt : ctx_arg.options) {
for (const auto & arg : opt.args) {
arg_to_options[arg] = &opt;
throw std::invalid_argument("error: --prompt-cache-all not supported in interactive mode yet\n");
}
- gpt_params_handle_model_default(params);
+ common_params_handle_model_default(params);
if (params.escape) {
string_process_escapes(params.prompt);
return true;
}
-static void gpt_params_print_usage(gpt_params_context & ctx_arg) {
- auto print_options = [](std::vector<llama_arg *> & options) {
- for (llama_arg * opt : options) {
+static void common_params_print_usage(common_params_context & ctx_arg) {
+ auto print_options = [](std::vector<common_arg *> & options) {
+ for (common_arg * opt : options) {
printf("%s", opt->to_string().c_str());
}
};
- std::vector<llama_arg *> common_options;
- std::vector<llama_arg *> sparam_options;
- std::vector<llama_arg *> specific_options;
+ std::vector<common_arg *> common_options;
+ std::vector<common_arg *> sparam_options;
+ std::vector<common_arg *> specific_options;
for (auto & opt : ctx_arg.options) {
// in case multiple LLAMA_EXAMPLE_* are set, we prioritize the LLAMA_EXAMPLE_* matching current example
if (opt.is_sparam) {
print_options(specific_options);
}
-bool gpt_params_parse(int argc, char ** argv, gpt_params & params, llama_example ex, void(*print_usage)(int, char **)) {
- auto ctx_arg = gpt_params_parser_init(params, ex, print_usage);
- const gpt_params params_org = ctx_arg.params; // the example can modify the default params
+bool common_params_parse(int argc, char ** argv, common_params & params, llama_example ex, void(*print_usage)(int, char **)) {
+ auto ctx_arg = common_params_parser_init(params, ex, print_usage);
+ const common_params params_org = ctx_arg.params; // the example can modify the default params
try {
- if (!gpt_params_parse_ex(argc, argv, ctx_arg)) {
+ if (!common_params_parse_ex(argc, argv, ctx_arg)) {
ctx_arg.params = params_org;
return false;
}
if (ctx_arg.params.usage) {
- gpt_params_print_usage(ctx_arg);
+ common_params_print_usage(ctx_arg);
if (ctx_arg.print_usage) {
ctx_arg.print_usage(argc, argv);
}
return true;
}
-gpt_params_context gpt_params_parser_init(gpt_params & params, llama_example ex, void(*print_usage)(int, char **)) {
- gpt_params_context ctx_arg(params);
+common_params_context common_params_parser_init(common_params & params, llama_example ex, void(*print_usage)(int, char **)) {
+ common_params_context ctx_arg(params);
ctx_arg.print_usage = print_usage;
ctx_arg.ex = ex;
std::string sampler_type_chars;
std::string sampler_type_names;
for (const auto & sampler : params.sparams.samplers) {
- sampler_type_chars += gpt_sampler_type_to_chr(sampler);
- sampler_type_names += gpt_sampler_type_to_str(sampler) + ";";
+ sampler_type_chars += common_sampler_type_to_chr(sampler);
+ sampler_type_names += common_sampler_type_to_str(sampler) + ";";
}
sampler_type_names.pop_back();
* - if LLAMA_EXAMPLE_* is set (other than COMMON), we only show the option in the corresponding example
* - if both {LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_*,} are set, we will prioritize the LLAMA_EXAMPLE_* matching current example
*/
- auto add_opt = [&](llama_arg arg) {
+ auto add_opt = [&](common_arg arg) {
if (arg.in_example(ex) || arg.in_example(LLAMA_EXAMPLE_COMMON)) {
ctx_arg.options.push_back(std::move(arg));
}
};
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-h", "--help", "--usage"},
"print usage and exit",
- [](gpt_params & params) {
+ [](common_params & params) {
params.usage = true;
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--version"},
"show version and build info",
- [](gpt_params &) {
+ [](common_params &) {
fprintf(stderr, "version: %d (%s)\n", LLAMA_BUILD_NUMBER, LLAMA_COMMIT);
fprintf(stderr, "built with %s for %s\n", LLAMA_COMPILER, LLAMA_BUILD_TARGET);
exit(0);
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--verbose-prompt"},
format("print a verbose prompt before generation (default: %s)", params.verbose_prompt ? "true" : "false"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.verbose_prompt = true;
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--no-display-prompt"},
format("don't print prompt at generation (default: %s)", !params.display_prompt ? "true" : "false"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.display_prompt = false;
}
).set_examples({LLAMA_EXAMPLE_MAIN}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-co", "--color"},
format("colorise output to distinguish prompt and user input from generations (default: %s)", params.use_color ? "true" : "false"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.use_color = true;
}
).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_INFILL, LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_LOOKUP}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-t", "--threads"}, "N",
format("number of threads to use during generation (default: %d)", params.cpuparams.n_threads),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.cpuparams.n_threads = value;
if (params.cpuparams.n_threads <= 0) {
params.cpuparams.n_threads = std::thread::hardware_concurrency();
}
}
).set_env("LLAMA_ARG_THREADS"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-tb", "--threads-batch"}, "N",
"number of threads to use during batch and prompt processing (default: same as --threads)",
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.cpuparams_batch.n_threads = value;
if (params.cpuparams_batch.n_threads <= 0) {
params.cpuparams_batch.n_threads = std::thread::hardware_concurrency();
}
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-td", "--threads-draft"}, "N",
"number of threads to use during generation (default: same as --threads)",
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.draft_cpuparams.n_threads = value;
if (params.draft_cpuparams.n_threads <= 0) {
params.draft_cpuparams.n_threads = std::thread::hardware_concurrency();
}
}
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-tbd", "--threads-batch-draft"}, "N",
"number of threads to use during batch and prompt processing (default: same as --threads-draft)",
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.draft_cpuparams_batch.n_threads = value;
if (params.draft_cpuparams_batch.n_threads <= 0) {
params.draft_cpuparams_batch.n_threads = std::thread::hardware_concurrency();
}
}
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-C", "--cpu-mask"}, "M",
"CPU affinity mask: arbitrarily long hex. Complements cpu-range (default: \"\")",
- [](gpt_params & params, const std::string & mask) {
+ [](common_params & params, const std::string & mask) {
params.cpuparams.mask_valid = true;
if (!parse_cpu_mask(mask, params.cpuparams.cpumask)) {
throw std::invalid_argument("invalid cpumask");
}
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-Cr", "--cpu-range"}, "lo-hi",
"range of CPUs for affinity. Complements --cpu-mask",
- [](gpt_params & params, const std::string & range) {
+ [](common_params & params, const std::string & range) {
params.cpuparams.mask_valid = true;
if (!parse_cpu_range(range, params.cpuparams.cpumask)) {
throw std::invalid_argument("invalid range");
}
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--cpu-strict"}, "<0|1>",
format("use strict CPU placement (default: %u)\n", (unsigned) params.cpuparams.strict_cpu),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.cpuparams.strict_cpu = std::stoul(value);
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--prio"}, "N",
format("set process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.cpuparams.priority),
- [](gpt_params & params, int prio) {
+ [](common_params & params, int prio) {
if (prio < 0 || prio > 3) {
throw std::invalid_argument("invalid value");
}
params.cpuparams.priority = (enum ggml_sched_priority) prio;
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--poll"}, "<0...100>",
format("use polling level to wait for work (0 - no polling, default: %u)\n", (unsigned) params.cpuparams.poll),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.cpuparams.poll = std::stoul(value);
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-Cb", "--cpu-mask-batch"}, "M",
"CPU affinity mask: arbitrarily long hex. Complements cpu-range-batch (default: same as --cpu-mask)",
- [](gpt_params & params, const std::string & mask) {
+ [](common_params & params, const std::string & mask) {
params.cpuparams_batch.mask_valid = true;
if (!parse_cpu_mask(mask, params.cpuparams_batch.cpumask)) {
throw std::invalid_argument("invalid cpumask");
}
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-Crb", "--cpu-range-batch"}, "lo-hi",
"ranges of CPUs for affinity. Complements --cpu-mask-batch",
- [](gpt_params & params, const std::string & range) {
+ [](common_params & params, const std::string & range) {
params.cpuparams_batch.mask_valid = true;
if (!parse_cpu_range(range, params.cpuparams_batch.cpumask)) {
throw std::invalid_argument("invalid range");
}
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--cpu-strict-batch"}, "<0|1>",
"use strict CPU placement (default: same as --cpu-strict)",
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.cpuparams_batch.strict_cpu = value;
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--prio-batch"}, "N",
format("set process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.cpuparams_batch.priority),
- [](gpt_params & params, int prio) {
+ [](common_params & params, int prio) {
if (prio < 0 || prio > 3) {
throw std::invalid_argument("invalid value");
}
params.cpuparams_batch.priority = (enum ggml_sched_priority) prio;
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--poll-batch"}, "<0|1>",
"use polling to wait for work (default: same as --poll)",
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.cpuparams_batch.poll = value;
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-Cd", "--cpu-mask-draft"}, "M",
"Draft model CPU affinity mask. Complements cpu-range-draft (default: same as --cpu-mask)",
- [](gpt_params & params, const std::string & mask) {
+ [](common_params & params, const std::string & mask) {
params.draft_cpuparams.mask_valid = true;
if (!parse_cpu_mask(mask, params.draft_cpuparams.cpumask)) {
throw std::invalid_argument("invalid cpumask");
}
}
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-Crd", "--cpu-range-draft"}, "lo-hi",
"Ranges of CPUs for affinity. Complements --cpu-mask-draft",
- [](gpt_params & params, const std::string & range) {
+ [](common_params & params, const std::string & range) {
params.draft_cpuparams.mask_valid = true;
if (!parse_cpu_range(range, params.draft_cpuparams.cpumask)) {
throw std::invalid_argument("invalid range");
}
}
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--cpu-strict-draft"}, "<0|1>",
"Use strict CPU placement for draft model (default: same as --cpu-strict)",
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.draft_cpuparams.strict_cpu = value;
}
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--prio-draft"}, "N",
format("set draft process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.draft_cpuparams.priority),
- [](gpt_params & params, int prio) {
+ [](common_params & params, int prio) {
if (prio < 0 || prio > 3) {
throw std::invalid_argument("invalid value");
}
params.draft_cpuparams.priority = (enum ggml_sched_priority) prio;
}
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--poll-draft"}, "<0|1>",
"Use polling to wait for draft model work (default: same as --poll])",
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.draft_cpuparams.poll = value;
}
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-Cbd", "--cpu-mask-batch-draft"}, "M",
"Draft model CPU affinity mask. Complements cpu-range-draft (default: same as --cpu-mask)",
- [](gpt_params & params, const std::string & mask) {
+ [](common_params & params, const std::string & mask) {
params.draft_cpuparams_batch.mask_valid = true;
if (!parse_cpu_mask(mask, params.draft_cpuparams_batch.cpumask)) {
throw std::invalid_argument("invalid cpumask");
}
}
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-Crbd", "--cpu-range-batch-draft"}, "lo-hi",
"Ranges of CPUs for affinity. Complements --cpu-mask-draft-batch)",
- [](gpt_params & params, const std::string & range) {
+ [](common_params & params, const std::string & range) {
params.draft_cpuparams_batch.mask_valid = true;
if (!parse_cpu_range(range, params.draft_cpuparams_batch.cpumask)) {
throw std::invalid_argument("invalid cpumask");
}
}
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--cpu-strict-batch-draft"}, "<0|1>",
"Use strict CPU placement for draft model (default: --cpu-strict-draft)",
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.draft_cpuparams_batch.strict_cpu = value;
}
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--prio-batch-draft"}, "N",
format("set draft process/thread priority : 0-normal, 1-medium, 2-high, 3-realtime (default: %d)\n", params.draft_cpuparams_batch.priority),
- [](gpt_params & params, int prio) {
+ [](common_params & params, int prio) {
if (prio < 0 || prio > 3) {
throw std::invalid_argument("invalid value");
}
params.draft_cpuparams_batch.priority = (enum ggml_sched_priority) prio;
}
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--poll-batch-draft"}, "<0|1>",
"Use polling to wait for draft model work (default: --poll-draft)",
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.draft_cpuparams_batch.poll = value;
}
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--draft"}, "N",
format("number of tokens to draft for speculative decoding (default: %d)", params.n_draft),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_draft = value;
}
).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_LOOKUP}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-ps", "--p-split"}, "N",
format("speculative decoding split probability (default: %.1f)", (double)params.p_split),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.p_split = std::stof(value);
}
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-lcs", "--lookup-cache-static"}, "FNAME",
"path to static lookup cache to use for lookup decoding (not updated by generation)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.lookup_cache_static = value;
}
).set_examples({LLAMA_EXAMPLE_LOOKUP}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-lcd", "--lookup-cache-dynamic"}, "FNAME",
"path to dynamic lookup cache to use for lookup decoding (updated by generation)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.lookup_cache_dynamic = value;
}
).set_examples({LLAMA_EXAMPLE_LOOKUP}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-c", "--ctx-size"}, "N",
format("size of the prompt context (default: %d, 0 = loaded from model)", params.n_ctx),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_ctx = value;
}
).set_env("LLAMA_ARG_CTX_SIZE"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-n", "--predict", "--n-predict"}, "N",
format("number of tokens to predict (default: %d, -1 = infinity, -2 = until context filled)", params.n_predict),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_predict = value;
}
).set_env("LLAMA_ARG_N_PREDICT"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-b", "--batch-size"}, "N",
format("logical maximum batch size (default: %d)", params.n_batch),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_batch = value;
}
).set_env("LLAMA_ARG_BATCH"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-ub", "--ubatch-size"}, "N",
format("physical maximum batch size (default: %d)", params.n_ubatch),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_ubatch = value;
}
).set_env("LLAMA_ARG_UBATCH"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--keep"}, "N",
format("number of tokens to keep from the initial prompt (default: %d, -1 = all)", params.n_keep),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_keep = value;
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--no-context-shift"},
format("disables context shift on inifinite text generation (default: %s)", params.ctx_shift ? "disabled" : "enabled"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.ctx_shift = false;
}
).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_NO_CONTEXT_SHIFT"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--chunks"}, "N",
format("max number of chunks to process (default: %d, -1 = all)", params.n_chunks),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_chunks = value;
}
).set_examples({LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_PERPLEXITY, LLAMA_EXAMPLE_RETRIEVAL}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-fa", "--flash-attn"},
format("enable Flash Attention (default: %s)", params.flash_attn ? "enabled" : "disabled"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.flash_attn = true;
}
).set_env("LLAMA_ARG_FLASH_ATTN"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-p", "--prompt"}, "PROMPT",
ex == LLAMA_EXAMPLE_MAIN
? "prompt to start generation with\nif -cnv is set, this will be used as system prompt"
: "prompt to start generation with",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.prompt = value;
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--no-perf"},
format("disable internal libllama performance timings (default: %s)", params.no_perf ? "true" : "false"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.no_perf = true;
params.sparams.no_perf = true;
}
).set_env("LLAMA_ARG_NO_PERF"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-f", "--file"}, "FNAME",
"a file containing the prompt (default: none)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
std::ifstream file(value);
if (!file) {
throw std::runtime_error(format("error: failed to open file '%s'\n", value.c_str()));
}
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--in-file"}, "FNAME",
"an input file (repeat to specify multiple files)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
std::ifstream file(value);
if (!file) {
throw std::runtime_error(format("error: failed to open file '%s'\n", value.c_str()));
params.in_files.push_back(value);
}
).set_examples({LLAMA_EXAMPLE_IMATRIX}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-bf", "--binary-file"}, "FNAME",
"binary file containing the prompt (default: none)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
std::ifstream file(value, std::ios::binary);
if (!file) {
throw std::runtime_error(format("error: failed to open file '%s'\n", value.c_str()));
fprintf(stderr, "Read %zu bytes from binary file %s\n", params.prompt.size(), value.c_str());
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-e", "--escape"},
format("process escapes sequences (\\n, \\r, \\t, \\', \\\", \\\\) (default: %s)", params.escape ? "true" : "false"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.escape = true;
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--no-escape"},
"do not process escape sequences",
- [](gpt_params & params) {
+ [](common_params & params) {
params.escape = false;
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-ptc", "--print-token-count"}, "N",
format("print token count every N tokens (default: %d)", params.n_print),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_print = value;
}
).set_examples({LLAMA_EXAMPLE_MAIN}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--prompt-cache"}, "FNAME",
"file to cache prompt state for faster startup (default: none)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.path_prompt_cache = value;
}
).set_examples({LLAMA_EXAMPLE_MAIN}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--prompt-cache-all"},
"if specified, saves user input and generations to cache as well\n",
- [](gpt_params & params) {
+ [](common_params & params) {
params.prompt_cache_all = true;
}
).set_examples({LLAMA_EXAMPLE_MAIN}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--prompt-cache-ro"},
"if specified, uses the prompt cache but does not update it",
- [](gpt_params & params) {
+ [](common_params & params) {
params.prompt_cache_ro = true;
}
).set_examples({LLAMA_EXAMPLE_MAIN}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-r", "--reverse-prompt"}, "PROMPT",
"halt generation at PROMPT, return control in interactive mode\n",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.antiprompt.emplace_back(value);
}
).set_examples({LLAMA_EXAMPLE_MAIN}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-sp", "--special"},
format("special tokens output enabled (default: %s)", params.special ? "true" : "false"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.special = true;
}
).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-cnv", "--conversation"},
format(
"run in conversation mode:\n"
"(default: %s)",
params.conversation ? "true" : "false"
),
- [](gpt_params & params) {
+ [](common_params & params) {
params.conversation = true;
}
).set_examples({LLAMA_EXAMPLE_MAIN}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-i", "--interactive"},
format("run in interactive mode (default: %s)", params.interactive ? "true" : "false"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.interactive = true;
}
).set_examples({LLAMA_EXAMPLE_MAIN}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-if", "--interactive-first"},
format("run in interactive mode and wait for input right away (default: %s)", params.interactive_first ? "true" : "false"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.interactive_first = true;
}
).set_examples({LLAMA_EXAMPLE_MAIN}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-mli", "--multiline-input"},
"allows you to write or paste multiple lines without ending each in '\\'",
- [](gpt_params & params) {
+ [](common_params & params) {
params.multiline_input = true;
}
).set_examples({LLAMA_EXAMPLE_MAIN}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--in-prefix-bos"},
"prefix BOS to user inputs, preceding the `--in-prefix` string",
- [](gpt_params & params) {
+ [](common_params & params) {
params.input_prefix_bos = true;
params.enable_chat_template = false;
}
).set_examples({LLAMA_EXAMPLE_MAIN}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--in-prefix"}, "STRING",
"string to prefix user inputs with (default: empty)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.input_prefix = value;
params.enable_chat_template = false;
}
).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_INFILL}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--in-suffix"}, "STRING",
"string to suffix after user inputs with (default: empty)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.input_suffix = value;
params.enable_chat_template = false;
}
).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_INFILL}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--no-warmup"},
"skip warming up the model with an empty run",
- [](gpt_params & params) {
+ [](common_params & params) {
params.warmup = false;
}
).set_examples({LLAMA_EXAMPLE_MAIN}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--spm-infill"},
format(
"use Suffix/Prefix/Middle pattern for infill (instead of Prefix/Suffix/Middle) as some models prefer this. (default: %s)",
params.spm_infill ? "enabled" : "disabled"
),
- [](gpt_params & params) {
+ [](common_params & params) {
params.spm_infill = true;
}
).set_examples({LLAMA_EXAMPLE_SERVER, LLAMA_EXAMPLE_INFILL}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--samplers"}, "SAMPLERS",
format("samplers that will be used for generation in the order, separated by \';\'\n(default: %s)", sampler_type_names.c_str()),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
const auto sampler_names = string_split(value, ';');
- params.sparams.samplers = gpt_sampler_types_from_names(sampler_names, true);
+ params.sparams.samplers = common_sampler_types_from_names(sampler_names, true);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-s", "--seed"}, "SEED",
format("RNG seed (default: %d, use random seed for %d)", params.sparams.seed, LLAMA_DEFAULT_SEED),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.sparams.seed = std::stoul(value);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--sampling-seq"}, "SEQUENCE",
format("simplified sequence for samplers that will be used (default: %s)", sampler_type_chars.c_str()),
- [](gpt_params & params, const std::string & value) {
- params.sparams.samplers = gpt_sampler_types_from_chars(value);
+ [](common_params & params, const std::string & value) {
+ params.sparams.samplers = common_sampler_types_from_chars(value);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--ignore-eos"},
"ignore end of stream token and continue generating (implies --logit-bias EOS-inf)",
- [](gpt_params & params) {
+ [](common_params & params) {
params.sparams.ignore_eos = true;
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--penalize-nl"},
format("penalize newline tokens (default: %s)", params.sparams.penalize_nl ? "true" : "false"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.sparams.penalize_nl = true;
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--temp"}, "N",
format("temperature (default: %.1f)", (double)params.sparams.temp),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.sparams.temp = std::stof(value);
params.sparams.temp = std::max(params.sparams.temp, 0.0f);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--top-k"}, "N",
format("top-k sampling (default: %d, 0 = disabled)", params.sparams.top_k),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.sparams.top_k = value;
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--top-p"}, "N",
format("top-p sampling (default: %.1f, 1.0 = disabled)", (double)params.sparams.top_p),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.sparams.top_p = std::stof(value);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--min-p"}, "N",
format("min-p sampling (default: %.1f, 0.0 = disabled)", (double)params.sparams.min_p),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.sparams.min_p = std::stof(value);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--tfs"}, "N",
format("tail free sampling, parameter z (default: %.1f, 1.0 = disabled)", (double)params.sparams.tfs_z),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.sparams.tfs_z = std::stof(value);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--typical"}, "N",
format("locally typical sampling, parameter p (default: %.1f, 1.0 = disabled)", (double)params.sparams.typ_p),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.sparams.typ_p = std::stof(value);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--repeat-last-n"}, "N",
format("last n tokens to consider for penalize (default: %d, 0 = disabled, -1 = ctx_size)", params.sparams.penalty_last_n),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.sparams.penalty_last_n = value;
params.sparams.n_prev = std::max(params.sparams.n_prev, params.sparams.penalty_last_n);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--repeat-penalty"}, "N",
format("penalize repeat sequence of tokens (default: %.1f, 1.0 = disabled)", (double)params.sparams.penalty_repeat),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.sparams.penalty_repeat = std::stof(value);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--presence-penalty"}, "N",
format("repeat alpha presence penalty (default: %.1f, 0.0 = disabled)", (double)params.sparams.penalty_present),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.sparams.penalty_present = std::stof(value);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--frequency-penalty"}, "N",
format("repeat alpha frequency penalty (default: %.1f, 0.0 = disabled)", (double)params.sparams.penalty_freq),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.sparams.penalty_freq = std::stof(value);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--dynatemp-range"}, "N",
format("dynamic temperature range (default: %.1f, 0.0 = disabled)", (double)params.sparams.dynatemp_range),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.sparams.dynatemp_range = std::stof(value);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--dynatemp-exp"}, "N",
format("dynamic temperature exponent (default: %.1f)", (double)params.sparams.dynatemp_exponent),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.sparams.dynatemp_exponent = std::stof(value);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--mirostat"}, "N",
format("use Mirostat sampling.\nTop K, Nucleus, Tail Free and Locally Typical samplers are ignored if used.\n"
"(default: %d, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)", params.sparams.mirostat),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.sparams.mirostat = value;
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--mirostat-lr"}, "N",
format("Mirostat learning rate, parameter eta (default: %.1f)", (double)params.sparams.mirostat_eta),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.sparams.mirostat_eta = std::stof(value);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--mirostat-ent"}, "N",
format("Mirostat target entropy, parameter tau (default: %.1f)", (double)params.sparams.mirostat_tau),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.sparams.mirostat_tau = std::stof(value);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-l", "--logit-bias"}, "TOKEN_ID(+/-)BIAS",
"modifies the likelihood of token appearing in the completion,\n"
"i.e. `--logit-bias 15043+1` to increase likelihood of token ' Hello',\n"
"or `--logit-bias 15043-1` to decrease likelihood of token ' Hello'",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
std::stringstream ss(value);
llama_token key;
char sign;
}
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--grammar"}, "GRAMMAR",
format("BNF-like grammar to constrain generations (see samples in grammars/ dir) (default: '%s')", params.sparams.grammar.c_str()),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.sparams.grammar = value;
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--grammar-file"}, "FNAME",
"file to read grammar from",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
std::ifstream file(value);
if (!file) {
throw std::runtime_error(format("error: failed to open file '%s'\n", value.c_str()));
);
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-j", "--json-schema"}, "SCHEMA",
"JSON schema to constrain generations (https://json-schema.org/), e.g. `{}` for any JSON object\nFor schemas w/ external $refs, use --grammar + example/json_schema_to_grammar.py instead",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.sparams.grammar = json_schema_to_grammar(json::parse(value));
}
).set_sparam());
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--pooling"}, "{none,mean,cls,last,rank}",
"pooling type for embeddings, use model default if unspecified",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
/**/ if (value == "none") { params.pooling_type = LLAMA_POOLING_TYPE_NONE; }
else if (value == "mean") { params.pooling_type = LLAMA_POOLING_TYPE_MEAN; }
else if (value == "cls") { params.pooling_type = LLAMA_POOLING_TYPE_CLS; }
else { throw std::invalid_argument("invalid value"); }
}
).set_examples({LLAMA_EXAMPLE_EMBEDDING, LLAMA_EXAMPLE_RETRIEVAL, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_POOLING"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--attention"}, "{causal,non,causal}",
"attention type for embeddings, use model default if unspecified",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
/**/ if (value == "causal") { params.attention_type = LLAMA_ATTENTION_TYPE_CAUSAL; }
else if (value == "non-causal") { params.attention_type = LLAMA_ATTENTION_TYPE_NON_CAUSAL; }
else { throw std::invalid_argument("invalid value"); }
}
).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--rope-scaling"}, "{none,linear,yarn}",
"RoPE frequency scaling method, defaults to linear unless specified by the model",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
/**/ if (value == "none") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_NONE; }
else if (value == "linear") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_LINEAR; }
else if (value == "yarn") { params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_YARN; }
else { throw std::invalid_argument("invalid value"); }
}
).set_env("LLAMA_ARG_ROPE_SCALING_TYPE"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--rope-scale"}, "N",
"RoPE context scaling factor, expands context by a factor of N",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.rope_freq_scale = 1.0f / std::stof(value);
}
).set_env("LLAMA_ARG_ROPE_SCALE"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--rope-freq-base"}, "N",
"RoPE base frequency, used by NTK-aware scaling (default: loaded from model)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.rope_freq_base = std::stof(value);
}
).set_env("LLAMA_ARG_ROPE_FREQ_BASE"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--rope-freq-scale"}, "N",
"RoPE frequency scaling factor, expands context by a factor of 1/N",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.rope_freq_scale = std::stof(value);
}
).set_env("LLAMA_ARG_ROPE_FREQ_SCALE"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--yarn-orig-ctx"}, "N",
format("YaRN: original context size of model (default: %d = model training context size)", params.yarn_orig_ctx),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.yarn_orig_ctx = value;
}
).set_env("LLAMA_ARG_YARN_ORIG_CTX"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--yarn-ext-factor"}, "N",
format("YaRN: extrapolation mix factor (default: %.1f, 0.0 = full interpolation)", (double)params.yarn_ext_factor),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.yarn_ext_factor = std::stof(value);
}
).set_env("LLAMA_ARG_YARN_EXT_FACTOR"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--yarn-attn-factor"}, "N",
format("YaRN: scale sqrt(t) or attention magnitude (default: %.1f)", (double)params.yarn_attn_factor),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.yarn_attn_factor = std::stof(value);
}
).set_env("LLAMA_ARG_YARN_ATTN_FACTOR"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--yarn-beta-slow"}, "N",
format("YaRN: high correction dim or alpha (default: %.1f)", (double)params.yarn_beta_slow),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.yarn_beta_slow = std::stof(value);
}
).set_env("LLAMA_ARG_YARN_BETA_SLOW"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--yarn-beta-fast"}, "N",
format("YaRN: low correction dim or beta (default: %.1f)", (double)params.yarn_beta_fast),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.yarn_beta_fast = std::stof(value);
}
).set_env("LLAMA_ARG_YARN_BETA_FAST"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-gan", "--grp-attn-n"}, "N",
format("group-attention factor (default: %d)", params.grp_attn_n),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.grp_attn_n = value;
}
).set_env("LLAMA_ARG_GRP_ATTN_N"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-gaw", "--grp-attn-w"}, "N",
format("group-attention width (default: %.1f)", (double)params.grp_attn_w),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.grp_attn_w = value;
}
).set_env("LLAMA_ARG_GRP_ATTN_W"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-dkvc", "--dump-kv-cache"},
"verbose print of the KV cache",
- [](gpt_params & params) {
+ [](common_params & params) {
params.dump_kv_cache = true;
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-nkvo", "--no-kv-offload"},
"disable KV offload",
- [](gpt_params & params) {
+ [](common_params & params) {
params.no_kv_offload = true;
}
).set_env("LLAMA_ARG_NO_KV_OFFLOAD"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-ctk", "--cache-type-k"}, "TYPE",
format("KV cache data type for K (default: %s)", params.cache_type_k.c_str()),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
// TODO: get the type right here
params.cache_type_k = value;
}
).set_env("LLAMA_ARG_CACHE_TYPE_K"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-ctv", "--cache-type-v"}, "TYPE",
format("KV cache data type for V (default: %s)", params.cache_type_v.c_str()),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
// TODO: get the type right here
params.cache_type_v = value;
}
).set_env("LLAMA_ARG_CACHE_TYPE_V"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--perplexity", "--all-logits"},
format("return logits for all tokens in the batch (default: %s)", params.logits_all ? "true" : "false"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.logits_all = true;
}
).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--hellaswag"},
"compute HellaSwag score over random tasks from datafile supplied with -f",
- [](gpt_params & params) {
+ [](common_params & params) {
params.hellaswag = true;
}
).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--hellaswag-tasks"}, "N",
format("number of tasks to use when computing the HellaSwag score (default: %zu)", params.hellaswag_tasks),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.hellaswag_tasks = value;
}
).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--winogrande"},
"compute Winogrande score over random tasks from datafile supplied with -f",
- [](gpt_params & params) {
+ [](common_params & params) {
params.winogrande = true;
}
).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--winogrande-tasks"}, "N",
format("number of tasks to use when computing the Winogrande score (default: %zu)", params.winogrande_tasks),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.winogrande_tasks = value;
}
).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--multiple-choice"},
"compute multiple choice score over random tasks from datafile supplied with -f",
- [](gpt_params & params) {
+ [](common_params & params) {
params.multiple_choice = true;
}
).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--multiple-choice-tasks"}, "N",
format("number of tasks to use when computing the multiple choice score (default: %zu)", params.multiple_choice_tasks),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.multiple_choice_tasks = value;
}
).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--kl-divergence"},
"computes KL-divergence to logits provided via --kl-divergence-base",
- [](gpt_params & params) {
+ [](common_params & params) {
params.kl_divergence = true;
}
).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--save-all-logits", "--kl-divergence-base"}, "FNAME",
"set logits file",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.logits_file = value;
}
).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--ppl-stride"}, "N",
format("stride for perplexity calculation (default: %d)", params.ppl_stride),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.ppl_stride = value;
}
).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--ppl-output-type"}, "<0|1>",
format("output type for perplexity calculation (default: %d)", params.ppl_output_type),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.ppl_output_type = value;
}
).set_examples({LLAMA_EXAMPLE_PERPLEXITY}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-dt", "--defrag-thold"}, "N",
format("KV cache defragmentation threshold (default: %.1f, < 0 - disabled)", (double)params.defrag_thold),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.defrag_thold = std::stof(value);
}
).set_env("LLAMA_ARG_DEFRAG_THOLD"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-np", "--parallel"}, "N",
format("number of parallel sequences to decode (default: %d)", params.n_parallel),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_parallel = value;
}
).set_env("LLAMA_ARG_N_PARALLEL"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-ns", "--sequences"}, "N",
format("number of sequences to decode (default: %d)", params.n_sequences),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_sequences = value;
}
).set_examples({LLAMA_EXAMPLE_PARALLEL}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-cb", "--cont-batching"},
format("enable continuous batching (a.k.a dynamic batching) (default: %s)", params.cont_batching ? "enabled" : "disabled"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.cont_batching = true;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CONT_BATCHING"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-nocb", "--no-cont-batching"},
"disable continuous batching",
- [](gpt_params & params) {
+ [](common_params & params) {
params.cont_batching = false;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_NO_CONT_BATCHING"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--mmproj"}, "FILE",
"path to a multimodal projector file for LLaVA. see examples/llava/README.md",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.mmproj = value;
}
).set_examples({LLAMA_EXAMPLE_LLAVA}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--image"}, "FILE",
"path to an image file. use with multimodal models. Specify multiple times for batching",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.image.emplace_back(value);
}
).set_examples({LLAMA_EXAMPLE_LLAVA}));
if (llama_supports_rpc()) {
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--rpc"}, "SERVERS",
"comma separated list of RPC servers",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.rpc_servers = value;
}
).set_env("LLAMA_ARG_RPC"));
}
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--mlock"},
"force system to keep model in RAM rather than swapping or compressing",
- [](gpt_params & params) {
+ [](common_params & params) {
params.use_mlock = true;
}
).set_env("LLAMA_ARG_MLOCK"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--no-mmap"},
"do not memory-map model (slower load but may reduce pageouts if not using mlock)",
- [](gpt_params & params) {
+ [](common_params & params) {
params.use_mmap = false;
}
).set_env("LLAMA_ARG_NO_MMAP"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--numa"}, "TYPE",
"attempt optimizations that help on some NUMA systems\n"
"- distribute: spread execution evenly over all nodes\n"
"- numactl: use the CPU map provided by numactl\n"
"if run without this previously, it is recommended to drop the system page cache before using this\n"
"see https://github.com/ggerganov/llama.cpp/issues/1437",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
/**/ if (value == "distribute" || value == "") { params.numa = GGML_NUMA_STRATEGY_DISTRIBUTE; }
else if (value == "isolate") { params.numa = GGML_NUMA_STRATEGY_ISOLATE; }
else if (value == "numactl") { params.numa = GGML_NUMA_STRATEGY_NUMACTL; }
else { throw std::invalid_argument("invalid value"); }
}
).set_env("LLAMA_ARG_NUMA"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-ngl", "--gpu-layers", "--n-gpu-layers"}, "N",
"number of layers to store in VRAM",
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_gpu_layers = value;
if (!llama_supports_gpu_offload()) {
fprintf(stderr, "warning: not compiled with GPU offload support, --gpu-layers option will be ignored\n");
}
}
).set_env("LLAMA_ARG_N_GPU_LAYERS"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-ngld", "--gpu-layers-draft", "--n-gpu-layers-draft"}, "N",
"number of layers to store in VRAM for the draft model",
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_gpu_layers_draft = value;
if (!llama_supports_gpu_offload()) {
fprintf(stderr, "warning: not compiled with GPU offload support, --gpu-layers-draft option will be ignored\n");
}
}
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-sm", "--split-mode"}, "{none,layer,row}",
"how to split the model across multiple GPUs, one of:\n"
"- none: use one GPU only\n"
"- layer (default): split layers and KV across GPUs\n"
"- row: split rows across GPUs",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
std::string arg_next = value;
if (arg_next == "none") {
params.split_mode = LLAMA_SPLIT_MODE_NONE;
}
}
).set_env("LLAMA_ARG_SPLIT_MODE"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-ts", "--tensor-split"}, "N0,N1,N2,...",
"fraction of the model to offload to each GPU, comma-separated list of proportions, e.g. 3,1",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
std::string arg_next = value;
// split string by , and /
}
}
).set_env("LLAMA_ARG_TENSOR_SPLIT"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-mg", "--main-gpu"}, "INDEX",
format("the GPU to use for the model (with split-mode = none), or for intermediate results and KV (with split-mode = row) (default: %d)", params.main_gpu),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.main_gpu = value;
if (!llama_supports_gpu_offload()) {
fprintf(stderr, "warning: llama.cpp was compiled without support for GPU offload. Setting the main GPU has no effect.\n");
}
}
).set_env("LLAMA_ARG_MAIN_GPU"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--check-tensors"},
format("check model tensor data for invalid values (default: %s)", params.check_tensors ? "true" : "false"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.check_tensors = true;
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--override-kv"}, "KEY=TYPE:VALUE",
"advanced option to override model metadata by key. may be specified multiple times.\n"
"types: int, float, bool, str. example: --override-kv tokenizer.ggml.add_bos_token=bool:false",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
if (!string_parse_kv_override(value.c_str(), params.kv_overrides)) {
throw std::runtime_error(format("error: Invalid type for KV override: %s\n", value.c_str()));
}
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--lora"}, "FNAME",
"path to LoRA adapter (can be repeated to use multiple adapters)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.lora_adapters.push_back({ std::string(value), 1.0 });
}
// we define this arg on both COMMON and EXPORT_LORA, so when showing help message of export-lora, it will be categorized as "example-specific" arg
).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--lora-scaled"}, "FNAME", "SCALE",
"path to LoRA adapter with user defined scaling (can be repeated to use multiple adapters)",
- [](gpt_params & params, const std::string & fname, const std::string & scale) {
+ [](common_params & params, const std::string & fname, const std::string & scale) {
params.lora_adapters.push_back({ fname, std::stof(scale) });
}
// we define this arg on both COMMON and EXPORT_LORA, so when showing help message of export-lora, it will be categorized as "example-specific" arg
).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--control-vector"}, "FNAME",
"add a control vector\nnote: this argument can be repeated to add multiple control vectors",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.control_vectors.push_back({ 1.0f, value, });
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--control-vector-scaled"}, "FNAME", "SCALE",
"add a control vector with user defined scaling SCALE\n"
"note: this argument can be repeated to add multiple scaled control vectors",
- [](gpt_params & params, const std::string & fname, const std::string & scale) {
+ [](common_params & params, const std::string & fname, const std::string & scale) {
params.control_vectors.push_back({ std::stof(scale), fname });
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--control-vector-layer-range"}, "START", "END",
"layer range to apply the control vector(s) to, start and end inclusive",
- [](gpt_params & params, const std::string & start, const std::string & end) {
+ [](common_params & params, const std::string & start, const std::string & end) {
params.control_vector_layer_start = std::stoi(start);
params.control_vector_layer_end = std::stoi(end);
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-a", "--alias"}, "STRING",
"set alias for model name (to be used by REST API)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.model_alias = value;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ALIAS"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-m", "--model"}, "FNAME",
ex == LLAMA_EXAMPLE_EXPORT_LORA
? std::string("model path from which to load base model")
"model path (default: `models/$filename` with filename from `--hf-file` "
"or `--model-url` if set, otherwise %s)", DEFAULT_MODEL_PATH
),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.model = value;
}
).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_EXPORT_LORA}).set_env("LLAMA_ARG_MODEL"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-md", "--model-draft"}, "FNAME",
"draft model for speculative decoding (default: unused)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.model_draft = value;
}
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-mu", "--model-url"}, "MODEL_URL",
"model download url (default: unused)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.model_url = value;
}
).set_env("LLAMA_ARG_MODEL_URL"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-hfr", "--hf-repo"}, "REPO",
"Hugging Face model repository (default: unused)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.hf_repo = value;
}
).set_env("LLAMA_ARG_HF_REPO"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-hff", "--hf-file"}, "FILE",
"Hugging Face model file (default: unused)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.hf_file = value;
}
).set_env("LLAMA_ARG_HF_FILE"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-hft", "--hf-token"}, "TOKEN",
"Hugging Face access token (default: value from HF_TOKEN environment variable)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.hf_token = value;
}
).set_env("HF_TOKEN"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--context-file"}, "FNAME",
"file to load context from (repeat to specify multiple files)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
std::ifstream file(value, std::ios::binary);
if (!file) {
throw std::runtime_error(format("error: failed to open file '%s'\n", value.c_str()));
params.context_files.push_back(value);
}
).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--chunk-size"}, "N",
format("minimum length of embedded text chunks (default: %d)", params.chunk_size),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.chunk_size = value;
}
).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--chunk-separator"}, "STRING",
format("separator between chunks (default: '%s')", params.chunk_separator.c_str()),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.chunk_separator = value;
}
).set_examples({LLAMA_EXAMPLE_RETRIEVAL}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--junk"}, "N",
format("number of times to repeat the junk text (default: %d)", params.n_junk),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_junk = value;
}
).set_examples({LLAMA_EXAMPLE_PASSKEY}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--pos"}, "N",
format("position of the passkey in the junk text (default: %d)", params.i_pos),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.i_pos = value;
}
).set_examples({LLAMA_EXAMPLE_PASSKEY}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-o", "--output", "--output-file"}, "FNAME",
format("output file (default: '%s')",
ex == LLAMA_EXAMPLE_EXPORT_LORA
: ex == LLAMA_EXAMPLE_CVECTOR_GENERATOR
? params.cvector_outfile.c_str()
: params.out_file.c_str()),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.out_file = value;
params.cvector_outfile = value;
params.lora_outfile = value;
}
).set_examples({LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_CVECTOR_GENERATOR, LLAMA_EXAMPLE_EXPORT_LORA}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-ofreq", "--output-frequency"}, "N",
format("output the imatrix every N iterations (default: %d)", params.n_out_freq),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_out_freq = value;
}
).set_examples({LLAMA_EXAMPLE_IMATRIX}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--save-frequency"}, "N",
format("save an imatrix copy every N iterations (default: %d)", params.n_save_freq),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_save_freq = value;
}
).set_examples({LLAMA_EXAMPLE_IMATRIX}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--process-output"},
format("collect data for the output tensor (default: %s)", params.process_output ? "true" : "false"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.process_output = true;
}
).set_examples({LLAMA_EXAMPLE_IMATRIX}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--no-ppl"},
format("do not compute perplexity (default: %s)", params.compute_ppl ? "true" : "false"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.compute_ppl = false;
}
).set_examples({LLAMA_EXAMPLE_IMATRIX}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--chunk", "--from-chunk"}, "N",
format("start processing the input from chunk N (default: %d)", params.i_chunk),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.i_chunk = value;
}
).set_examples({LLAMA_EXAMPLE_IMATRIX}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-pps"},
format("is the prompt shared across parallel sequences (default: %s)", params.is_pp_shared ? "true" : "false"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.is_pp_shared = true;
}
).set_examples({LLAMA_EXAMPLE_BENCH}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-npp"}, "n0,n1,...",
"number of prompt tokens",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
auto p = string_split<int>(value, ',');
params.n_pp.insert(params.n_pp.end(), p.begin(), p.end());
}
).set_examples({LLAMA_EXAMPLE_BENCH}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-ntg"}, "n0,n1,...",
"number of text generation tokens",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
auto p = string_split<int>(value, ',');
params.n_tg.insert(params.n_tg.end(), p.begin(), p.end());
}
).set_examples({LLAMA_EXAMPLE_BENCH}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-npl"}, "n0,n1,...",
"number of parallel prompts",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
auto p = string_split<int>(value, ',');
params.n_pl.insert(params.n_pl.end(), p.begin(), p.end());
}
).set_examples({LLAMA_EXAMPLE_BENCH}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--embd-normalize"}, "N",
format("normalisation for embendings (default: %d) (-1=none, 0=max absolute int16, 1=taxicab, 2=euclidean, >2=p-norm)", params.embd_normalize),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.embd_normalize = value;
}
).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--embd-output-format"}, "FORMAT",
"empty = default, \"array\" = [[],[]...], \"json\" = openai style, \"json+\" = same \"json\" + cosine similarity matrix",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.embd_out = value;
}
).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--embd-separator"}, "STRING",
"separator of embendings (default \\n) for example \"<#sep#>\"",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.embd_sep = value;
}
).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--host"}, "HOST",
format("ip address to listen (default: %s)", params.hostname.c_str()),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.hostname = value;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_HOST"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--port"}, "PORT",
format("port to listen (default: %d)", params.port),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.port = value;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_PORT"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--path"}, "PATH",
format("path to serve static files from (default: %s)", params.public_path.c_str()),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.public_path = value;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_STATIC_PATH"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--embedding", "--embeddings"},
format("restrict to only support embedding use case; use only with dedicated embedding models (default: %s)", params.embedding ? "enabled" : "disabled"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.embedding = true;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_EMBEDDINGS"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--reranking", "--rerank"},
format("enable reranking endpoint on server (default: %s)", params.reranking ? "enabled" : "disabled"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.reranking = true;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_RERANKING"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--api-key"}, "KEY",
"API key to use for authentication (default: none)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.api_keys.push_back(value);
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_API_KEY"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--api-key-file"}, "FNAME",
"path to file containing API keys (default: none)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
std::ifstream key_file(value);
if (!key_file) {
throw std::runtime_error(format("error: failed to open file '%s'\n", value.c_str()));
key_file.close();
}
).set_examples({LLAMA_EXAMPLE_SERVER}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--ssl-key-file"}, "FNAME",
"path to file a PEM-encoded SSL private key",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.ssl_file_key = value;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_SSL_KEY_FILE"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--ssl-cert-file"}, "FNAME",
"path to file a PEM-encoded SSL certificate",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.ssl_file_cert = value;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_SSL_CERT_FILE"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-to", "--timeout"}, "N",
format("server read/write timeout in seconds (default: %d)", params.timeout_read),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.timeout_read = value;
params.timeout_write = value;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_TIMEOUT"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--threads-http"}, "N",
format("number of threads used to process HTTP requests (default: %d)", params.n_threads_http),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_threads_http = value;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_THREADS_HTTP"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-spf", "--system-prompt-file"}, "FNAME",
"set a file to load a system prompt (initial prompt of all slots), this is useful for chat applications",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
std::ifstream file(value);
if (!file) {
throw std::runtime_error(format("error: failed to open file '%s'\n", value.c_str()));
params.system_prompt = system_prompt;
}
).set_examples({LLAMA_EXAMPLE_SERVER}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--metrics"},
format("enable prometheus compatible metrics endpoint (default: %s)", params.endpoint_metrics ? "enabled" : "disabled"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.endpoint_metrics = true;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_METRICS"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--slots"},
format("enable slots monitoring endpoint (default: %s)", params.endpoint_slots ? "enabled" : "disabled"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.endpoint_slots = true;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_SLOTS"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--props"},
format("enable changing global properties via POST /props (default: %s)", params.endpoint_props ? "enabled" : "disabled"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.endpoint_props = true;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ENDPOINT_PROPS"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--no-slots"},
"disables slots monitoring endpoint",
- [](gpt_params & params) {
+ [](common_params & params) {
params.endpoint_slots = false;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_NO_ENDPOINT_SLOTS"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--slot-save-path"}, "PATH",
"path to save slot kv cache (default: disabled)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.slot_save_path = value;
// if doesn't end with DIRECTORY_SEPARATOR, add it
if (!params.slot_save_path.empty() && params.slot_save_path[params.slot_save_path.size() - 1] != DIRECTORY_SEPARATOR) {
}
}
).set_examples({LLAMA_EXAMPLE_SERVER}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--chat-template"}, "JINJA_TEMPLATE",
"set custom jinja chat template (default: template taken from model's metadata)\n"
"if suffix/prefix are specified, template will be disabled\n"
"only commonly used templates are accepted:\nhttps://github.com/ggerganov/llama.cpp/wiki/Templates-supported-by-llama_chat_apply_template",
- [](gpt_params & params, const std::string & value) {
- if (!llama_chat_verify_template(value)) {
+ [](common_params & params, const std::string & value) {
+ if (!common_chat_verify_template(value)) {
throw std::runtime_error(format(
"error: the supplied chat template is not supported: %s\n"
"note: llama.cpp does not use jinja parser, we only support commonly used templates\n",
params.chat_template = value;
}
).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CHAT_TEMPLATE"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-sps", "--slot-prompt-similarity"}, "SIMILARITY",
format("how much the prompt of a request must match the prompt of a slot in order to use that slot (default: %.2f, 0.0 = disabled)\n", params.slot_prompt_similarity),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.slot_prompt_similarity = std::stof(value);
}
).set_examples({LLAMA_EXAMPLE_SERVER}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--lora-init-without-apply"},
format("load LoRA adapters without applying them (apply later via POST /lora-adapters) (default: %s)", params.lora_init_without_apply ? "enabled" : "disabled"),
- [](gpt_params & params) {
+ [](common_params & params) {
params.lora_init_without_apply = true;
}
).set_examples({LLAMA_EXAMPLE_SERVER}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--simple-io"},
"use basic IO for better compatibility in subprocesses and limited consoles",
- [](gpt_params & params) {
+ [](common_params & params) {
params.simple_io = true;
}
).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_INFILL}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-ld", "--logdir"}, "LOGDIR",
"path under which to save YAML logs (no logging if unset)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.logdir = value;
if (params.logdir.back() != DIRECTORY_SEPARATOR) {
}
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--positive-file"}, "FNAME",
format("positive prompts file, one prompt per line (default: '%s')", params.cvector_positive_file.c_str()),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.cvector_positive_file = value;
}
).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--negative-file"}, "FNAME",
format("negative prompts file, one prompt per line (default: '%s')", params.cvector_negative_file.c_str()),
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
params.cvector_negative_file = value;
}
).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--pca-batch"}, "N",
format("batch size used for PCA. Larger batch runs faster, but uses more memory (default: %d)", params.n_pca_batch),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_pca_batch = value;
}
).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--pca-iter"}, "N",
format("number of iterations used for PCA (default: %d)", params.n_pca_iterations),
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.n_pca_iterations = value;
}
).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--method"}, "{pca, mean}",
"dimensionality reduction method to be used (default: pca)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
/**/ if (value == "pca") { params.cvector_dimre_method = DIMRE_METHOD_PCA; }
else if (value == "mean") { params.cvector_dimre_method = DIMRE_METHOD_MEAN; }
else { throw std::invalid_argument("invalid value"); }
}
).set_examples({LLAMA_EXAMPLE_CVECTOR_GENERATOR}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--output-format"}, "{md,jsonl}",
"output format for batched-bench results (default: md)",
- [](gpt_params & params, const std::string & value) {
+ [](common_params & params, const std::string & value) {
/**/ if (value == "jsonl") { params.batched_bench_output_jsonl = true; }
else if (value == "md") { params.batched_bench_output_jsonl = false; }
else { std::invalid_argument("invalid value"); }
}
).set_examples({LLAMA_EXAMPLE_BENCH}));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--log-disable"},
"Log disable",
- [](gpt_params &) {
- gpt_log_pause(gpt_log_main());
+ [](common_params &) {
+ common_log_pause(common_log_main());
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--log-file"}, "FNAME",
"Log to file",
- [](gpt_params &, const std::string & value) {
- gpt_log_set_file(gpt_log_main(), value.c_str());
+ [](common_params &, const std::string & value) {
+ common_log_set_file(common_log_main(), value.c_str());
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--log-colors"},
"Enable colored logging",
- [](gpt_params &) {
- gpt_log_set_colors(gpt_log_main(), true);
+ [](common_params &) {
+ common_log_set_colors(common_log_main(), true);
}
).set_env("LLAMA_LOG_COLORS"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-v", "--verbose", "--log-verbose"},
"Set verbosity level to infinity (i.e. log all messages, useful for debugging)",
- [](gpt_params & params) {
+ [](common_params & params) {
params.verbosity = INT_MAX;
- gpt_log_set_verbosity_thold(INT_MAX);
+ common_log_set_verbosity_thold(INT_MAX);
}
));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"-lv", "--verbosity", "--log-verbosity"}, "N",
"Set the verbosity threshold. Messages with a higher verbosity will be ignored.",
- [](gpt_params & params, int value) {
+ [](common_params & params, int value) {
params.verbosity = value;
- gpt_log_set_verbosity_thold(value);
+ common_log_set_verbosity_thold(value);
}
).set_env("LLAMA_LOG_VERBOSITY"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--log-prefix"},
"Enable prefx in log messages",
- [](gpt_params &) {
- gpt_log_set_prefix(gpt_log_main(), true);
+ [](common_params &) {
+ common_log_set_prefix(common_log_main(), true);
}
).set_env("LLAMA_LOG_PREFIX"));
- add_opt(llama_arg(
+ add_opt(common_arg(
{"--log-timestamps"},
"Enable timestamps in log messages",
- [](gpt_params &) {
- gpt_log_set_timestamps(gpt_log_main(), true);
+ [](common_params &) {
+ common_log_set_timestamps(common_log_main(), true);
}
).set_env("LLAMA_LOG_TIMESTAMPS"));
// CLI argument parsing
//
-struct llama_arg {
+struct common_arg {
std::set<enum llama_example> examples = {LLAMA_EXAMPLE_COMMON};
std::vector<const char *> args;
const char * value_hint = nullptr; // help text or example for arg value
const char * env = nullptr;
std::string help;
bool is_sparam = false; // is current arg a sampling param?
- void (*handler_void) (gpt_params & params) = nullptr;
- void (*handler_string) (gpt_params & params, const std::string &) = nullptr;
- void (*handler_str_str)(gpt_params & params, const std::string &, const std::string &) = nullptr;
- void (*handler_int) (gpt_params & params, int) = nullptr;
+ void (*handler_void) (common_params & params) = nullptr;
+ void (*handler_string) (common_params & params, const std::string &) = nullptr;
+ void (*handler_str_str)(common_params & params, const std::string &, const std::string &) = nullptr;
+ void (*handler_int) (common_params & params, int) = nullptr;
- llama_arg(
+ common_arg(
const std::initializer_list<const char *> & args,
const char * value_hint,
const std::string & help,
- void (*handler)(gpt_params & params, const std::string &)
+ void (*handler)(common_params & params, const std::string &)
) : args(args), value_hint(value_hint), help(help), handler_string(handler) {}
- llama_arg(
+ common_arg(
const std::initializer_list<const char *> & args,
const char * value_hint,
const std::string & help,
- void (*handler)(gpt_params & params, int)
+ void (*handler)(common_params & params, int)
) : args(args), value_hint(value_hint), help(help), handler_int(handler) {}
- llama_arg(
+ common_arg(
const std::initializer_list<const char *> & args,
const std::string & help,
- void (*handler)(gpt_params & params)
+ void (*handler)(common_params & params)
) : args(args), help(help), handler_void(handler) {}
// support 2 values for arg
- llama_arg(
+ common_arg(
const std::initializer_list<const char *> & args,
const char * value_hint,
const char * value_hint_2,
const std::string & help,
- void (*handler)(gpt_params & params, const std::string &, const std::string &)
+ void (*handler)(common_params & params, const std::string &, const std::string &)
) : args(args), value_hint(value_hint), value_hint_2(value_hint_2), help(help), handler_str_str(handler) {}
- llama_arg & set_examples(std::initializer_list<enum llama_example> examples);
- llama_arg & set_env(const char * env);
- llama_arg & set_sparam();
+ common_arg & set_examples(std::initializer_list<enum llama_example> examples);
+ common_arg & set_env(const char * env);
+ common_arg & set_sparam();
bool in_example(enum llama_example ex);
bool get_value_from_env(std::string & output);
bool has_value_from_env();
std::string to_string();
};
-struct gpt_params_context {
+struct common_params_context {
enum llama_example ex = LLAMA_EXAMPLE_COMMON;
- gpt_params & params;
- std::vector<llama_arg> options;
+ common_params & params;
+ std::vector<common_arg> options;
void(*print_usage)(int, char **) = nullptr;
- gpt_params_context(gpt_params & params) : params(params) {}
+ common_params_context(common_params & params) : params(params) {}
};
// parse input arguments from CLI
// if one argument has invalid value, it will automatically display usage of the specific argument (and not the full usage message)
-bool gpt_params_parse(int argc, char ** argv, gpt_params & params, llama_example ex, void(*print_usage)(int, char **) = nullptr);
+bool common_params_parse(int argc, char ** argv, common_params & params, llama_example ex, void(*print_usage)(int, char **) = nullptr);
// function to be used by test-arg-parser
-gpt_params_context gpt_params_parser_init(gpt_params & params, llama_example ex, void(*print_usage)(int, char **) = nullptr);
+common_params_context common_params_parser_init(common_params & params, llama_example ex, void(*print_usage)(int, char **) = nullptr);
return true;
}
-void gpt_init() {
+void common_init() {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
- if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_thold) {
- gpt_log_add(gpt_log_main(), level, "%s", text);
+ if (LOG_DEFAULT_LLAMA <= common_log_verbosity_thold) {
+ common_log_add(common_log_main(), level, "%s", text);
}
}, NULL);
LOG_INF("build: %d (%s) with %s for %s%s\n", LLAMA_BUILD_NUMBER, LLAMA_COMMIT, LLAMA_COMPILER, LLAMA_BUILD_TARGET, build_type);
}
-std::string gpt_params_get_system_info(const gpt_params & params) {
+std::string common_params_get_system_info(const common_params & params) {
std::ostringstream os;
os << "system_info: n_threads = " << params.cpuparams.n_threads;
first = false;
}
- auto detokenized = llama_token_to_piece(ctx, token);
+ auto detokenized = common_token_to_piece(ctx, token);
detokenized.erase(
std::remove_if(
first = false;
}
- auto detokenized = llama_token_to_piece(ctx, batch.token[i]);
+ auto detokenized = common_token_to_piece(ctx, batch.token[i]);
detokenized.erase(
std::remove_if(
//
// Model utils
//
-struct llama_init_result llama_init_from_gpt_params(gpt_params & params) {
- llama_init_result iparams;
- auto mparams = llama_model_params_from_gpt_params(params);
+struct common_init_result common_init_from_params(common_params & params) {
+ common_init_result iparams;
+ auto mparams = common_model_params_to_llama(params);
llama_model * model = nullptr;
if (!params.hf_repo.empty() && !params.hf_file.empty()) {
- model = llama_load_model_from_hf(params.hf_repo.c_str(), params.hf_file.c_str(), params.model.c_str(), params.hf_token.c_str(), mparams);
+ model = common_load_model_from_hf(params.hf_repo.c_str(), params.hf_file.c_str(), params.model.c_str(), params.hf_token.c_str(), mparams);
} else if (!params.model_url.empty()) {
- model = llama_load_model_from_url(params.model_url.c_str(), params.model.c_str(), params.hf_token.c_str(), mparams);
+ model = common_load_model_from_url(params.model_url.c_str(), params.model.c_str(), params.hf_token.c_str(), mparams);
} else {
model = llama_load_model_from_file(params.model.c_str(), mparams);
}
}
}
- auto cparams = llama_context_params_from_gpt_params(params);
+ auto cparams = common_context_params_to_llama(params);
llama_context * lctx = llama_new_context_with_model(model, cparams);
if (lctx == NULL) {
if (params.control_vector_layer_start <= 0) params.control_vector_layer_start = 1;
if (params.control_vector_layer_end <= 0) params.control_vector_layer_end = llama_n_layer(model);
- const auto cvec = llama_control_vector_load(params.control_vectors);
+ const auto cvec = common_control_vector_load(params.control_vectors);
if (cvec.n_embd == -1) {
llama_free(lctx);
llama_free_model(model);
// load and optionally apply lora adapters
for (auto & la : params.lora_adapters) {
- llama_lora_adapter_container loaded_la;
+ common_lora_adapter_container loaded_la;
loaded_la.path = la.path;
loaded_la.scale = la.scale;
loaded_la.adapter = llama_lora_adapter_init(model, la.path.c_str());
iparams.lora_adapters.push_back(loaded_la); // copy to list of loaded adapters
}
if (!params.lora_init_without_apply) {
- llama_lora_adapters_apply(lctx, iparams.lora_adapters);
+ common_lora_adapters_apply(lctx, iparams.lora_adapters);
}
if (params.sparams.ignore_eos && llama_token_eos(model) == LLAMA_TOKEN_NULL) {
return iparams;
}
-void llama_lora_adapters_apply(struct llama_context * ctx, std::vector<llama_lora_adapter_container> & lora_adapters) {
+void common_lora_adapters_apply(struct llama_context * ctx, std::vector<common_lora_adapter_container> & lora_adapters) {
llama_lora_adapter_clear(ctx);
for (auto & la : lora_adapters) {
if (la.scale != 0.0f) {
}
}
-struct llama_model_params llama_model_params_from_gpt_params(const gpt_params & params) {
+struct llama_model_params common_model_params_to_llama(const common_params & params) {
auto mparams = llama_model_default_params();
if (params.n_gpu_layers != -1) {
throw std::runtime_error("Invalid cache type: " + s);
}
-struct llama_context_params llama_context_params_from_gpt_params(const gpt_params & params) {
+struct llama_context_params common_context_params_to_llama(const common_params & params) {
auto cparams = llama_context_default_params();
cparams.n_ctx = params.n_ctx;
return false;
}
-static bool llama_download_file(const std::string & url, const std::string & path, const std::string & hf_token) {
+static bool common_download_file(const std::string & url, const std::string & path, const std::string & hf_token) {
// Initialize libcurl
std::unique_ptr<CURL, decltype(&curl_easy_cleanup)> curl(curl_easy_init(), &curl_easy_cleanup);
}
// Send a HEAD request to retrieve the etag and last-modified headers
- struct llama_load_model_from_url_headers {
+ struct common_load_model_from_url_headers {
std::string etag;
std::string last_modified;
};
- llama_load_model_from_url_headers headers;
+ common_load_model_from_url_headers headers;
{
typedef size_t(*CURLOPT_HEADERFUNCTION_PTR)(char *, size_t, size_t, void *);
auto header_callback = [](char * buffer, size_t /*size*/, size_t n_items, void * userdata) -> size_t {
- llama_load_model_from_url_headers *headers = (llama_load_model_from_url_headers *) userdata;
+ common_load_model_from_url_headers *headers = (common_load_model_from_url_headers *) userdata;
static std::regex header_regex("([^:]+): (.*)\r\n");
static std::regex etag_regex("ETag", std::regex_constants::icase);
return true;
}
-struct llama_model * llama_load_model_from_url(
+struct llama_model * common_load_model_from_url(
const char * model_url,
const char * path_model,
const char * hf_token,
return NULL;
}
- if (!llama_download_file(model_url, path_model, hf_token)) {
+ if (!common_download_file(model_url, path_model, hf_token)) {
return NULL;
}
char split_url[LLAMA_CURL_MAX_URL_LENGTH] = {0};
llama_split_path(split_url, sizeof(split_url), split_url_prefix, download_idx, n_split);
- return llama_download_file(split_url, split_path, hf_token);
+ return common_download_file(split_url, split_path, hf_token);
}, idx));
}
return llama_load_model_from_file(path_model, params);
}
-struct llama_model * llama_load_model_from_hf(
+struct llama_model * common_load_model_from_hf(
const char * repo,
const char * model,
const char * path_model,
model_url += "/resolve/main/";
model_url += model;
- return llama_load_model_from_url(model_url.c_str(), path_model, hf_token, params);
+ return common_load_model_from_url(model_url.c_str(), path_model, hf_token, params);
}
#else
-struct llama_model * llama_load_model_from_url(
+struct llama_model * common_load_model_from_url(
const char * /*model_url*/,
const char * /*path_model*/,
const char * /*hf_token*/,
return nullptr;
}
-struct llama_model * llama_load_model_from_hf(
+struct llama_model * common_load_model_from_hf(
const char * /*repo*/,
const char * /*model*/,
const char * /*path_model*/,
// Batch utils
//
-void llama_batch_clear(struct llama_batch & batch) {
+void common_batch_clear(struct llama_batch & batch) {
batch.n_tokens = 0;
}
-void llama_batch_add(
+void common_batch_add(
struct llama_batch & batch,
llama_token id,
llama_pos pos,
// Vocab utils
//
-std::vector<llama_token> llama_tokenize(
+std::vector<llama_token> common_tokenize(
const struct llama_context * ctx,
const std::string & text,
bool add_special,
bool parse_special) {
- return llama_tokenize(llama_get_model(ctx), text, add_special, parse_special);
+ return common_tokenize(llama_get_model(ctx), text, add_special, parse_special);
}
-std::vector<llama_token> llama_tokenize(
+std::vector<llama_token> common_tokenize(
const struct llama_model * model,
const std::string & text,
bool add_special,
return result;
}
-std::string llama_token_to_piece(const struct llama_context * ctx, llama_token token, bool special) {
+std::string common_token_to_piece(const struct llama_context * ctx, llama_token token, bool special) {
std::string piece;
piece.resize(piece.capacity()); // using string internal cache, 15 bytes + '\n'
const int n_chars = llama_token_to_piece(llama_get_model(ctx), token, &piece[0], piece.size(), 0, special);
return piece;
}
-std::string llama_detokenize(llama_context * ctx, const std::vector<llama_token> & tokens, bool special) {
+std::string common_detokenize(llama_context * ctx, const std::vector<llama_token> & tokens, bool special) {
std::string text;
text.resize(std::max(text.capacity(), tokens.size()));
int32_t n_chars = llama_detokenize(llama_get_model(ctx), tokens.data(), (int32_t)tokens.size(), &text[0], (int32_t)text.size(), false, special);
// Chat template utils
//
-bool llama_chat_verify_template(const std::string & tmpl) {
+bool common_chat_verify_template(const std::string & tmpl) {
llama_chat_message chat[] = {{"user", "test"}};
int res = llama_chat_apply_template(nullptr, tmpl.c_str(), chat, 1, true, nullptr, 0);
return res >= 0;
}
-std::string llama_chat_apply_template(const struct llama_model * model,
+std::string common_chat_apply_template(const struct llama_model * model,
const std::string & tmpl,
- const std::vector<llama_chat_msg> & msgs,
+ const std::vector<common_chat_msg> & msgs,
bool add_ass) {
int alloc_size = 0;
bool fallback = false; // indicate if we must fallback to default chatml
return formatted_chat;
}
-std::string llama_chat_format_single(const struct llama_model * model,
+std::string common_chat_format_single(const struct llama_model * model,
const std::string & tmpl,
- const std::vector<llama_chat_msg> & past_msg,
- const llama_chat_msg & new_msg,
+ const std::vector<common_chat_msg> & past_msg,
+ const common_chat_msg & new_msg,
bool add_ass) {
std::ostringstream ss;
- auto fmt_past_msg = past_msg.empty() ? "" : llama_chat_apply_template(model, tmpl, past_msg, false);
- std::vector<llama_chat_msg> chat_new(past_msg);
+ auto fmt_past_msg = past_msg.empty() ? "" : common_chat_apply_template(model, tmpl, past_msg, false);
+ std::vector<common_chat_msg> chat_new(past_msg);
// if the past_msg ends with a newline, we must preserve it in the formatted version
if (add_ass && !fmt_past_msg.empty() && fmt_past_msg.back() == '\n') {
ss << "\n";
};
// format chat with new_msg
chat_new.push_back(new_msg);
- auto fmt_new_msg = llama_chat_apply_template(model, tmpl, chat_new, add_ass);
+ auto fmt_new_msg = common_chat_apply_template(model, tmpl, chat_new, add_ass);
// get the diff part
ss << fmt_new_msg.substr(fmt_past_msg.size(), fmt_new_msg.size() - fmt_past_msg.size());
return ss.str();
}
-std::string llama_chat_format_example(const struct llama_model * model,
+std::string common_chat_format_example(const struct llama_model * model,
const std::string & tmpl) {
- std::vector<llama_chat_msg> msgs = {
+ std::vector<common_chat_msg> msgs = {
{"system", "You are a helpful assistant"},
{"user", "Hello"},
{"assistant", "Hi there"},
{"user", "How are you?"},
};
- return llama_chat_apply_template(model, tmpl, msgs, true);
+ return common_chat_apply_template(model, tmpl, msgs, true);
}
//
// KV cache utils
//
-void llama_kv_cache_dump_view(const llama_kv_cache_view & view, int row_size) {
+void common_kv_cache_dump_view(const llama_kv_cache_view & view, int row_size) {
static const char slot_chars[] = ".123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+";
printf("=== Dumping KV cache. total cells %d, max sequences per cell %d, populated cells %d, total tokens in cache %d, largest empty slot=%d @ %d",
printf("\n=== Done dumping\n");
}
-void llama_kv_cache_dump_view_seqs(const llama_kv_cache_view & view, int row_size) {
+void common_kv_cache_dump_view_seqs(const llama_kv_cache_view & view, int row_size) {
static const char slot_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
printf("=== Dumping KV cache. total cells %d, max sequences per cell %d, populated cells %d, total tokens in cache %d, largest empty slot=%d @ %d\n",
// Embedding utils
//
-void llama_embd_normalize(const float * inp, float * out, int n, int embd_norm) {
+void common_embd_normalize(const float * inp, float * out, int n, int embd_norm) {
double sum = 0.0;
switch (embd_norm) {
}
}
-float llama_embd_similarity_cos(const float * embd1, const float * embd2, int n){
+float common_embd_similarity_cos(const float * embd1, const float * embd2, int n){
double sum = 0.0;
double sum1 = 0.0;
double sum2 = 0.0;
// Control vector utils
//
-static llama_control_vector_data llama_control_vector_load_one(const llama_control_vector_load_info & load_info) {
- llama_control_vector_data result = { -1, {} };
+static common_control_vector_data common_control_vector_load_one(const common_control_vector_load_info & load_info) {
+ common_control_vector_data result = { -1, {} };
ggml_context * ctx = nullptr;
struct gguf_init_params meta_gguf_params = {
return result;
}
-llama_control_vector_data llama_control_vector_load(const std::vector<llama_control_vector_load_info> & load_infos) {
- llama_control_vector_data result = { -1, {} };
+common_control_vector_data common_control_vector_load(const std::vector<common_control_vector_load_info> & load_infos) {
+ common_control_vector_data result = { -1, {} };
for (const auto & info : load_infos) {
- auto cur = llama_control_vector_load_one(info);
+ auto cur = common_control_vector_load_one(info);
if (cur.n_embd == -1) {
result.n_embd = -1;
}
}
-void yaml_dump_non_result_info(FILE * stream, const gpt_params & params, const llama_context * lctx,
+void yaml_dump_non_result_info(FILE * stream, const common_params & params, const llama_context * lctx,
const std::string & timestamp, const std::vector<int> & prompt_tokens, const char * model_desc) {
const auto & sparams = params.sparams;
#define DEFAULT_MODEL_PATH "models/7B/ggml-model-f16.gguf"
-struct llama_lora_adapter_info {
+struct common_lora_adapter_info {
std::string path;
float scale;
};
-struct llama_lora_adapter_container : llama_lora_adapter_info {
+struct common_lora_adapter_container : common_lora_adapter_info {
struct llama_lora_adapter * adapter;
};
extern char const * LLAMA_COMPILER;
extern char const * LLAMA_BUILD_TARGET;
-struct llama_control_vector_load_info;
+struct common_control_vector_load_info;
//
// CPU utils
LLAMA_EXAMPLE_COUNT,
};
-enum gpt_sampler_type {
- GPT_SAMPLER_TYPE_NONE = 0,
- GPT_SAMPLER_TYPE_TOP_K = 1,
- GPT_SAMPLER_TYPE_TOP_P = 2,
- GPT_SAMPLER_TYPE_MIN_P = 3,
- GPT_SAMPLER_TYPE_TFS_Z = 4,
- GPT_SAMPLER_TYPE_TYPICAL_P = 5,
- GPT_SAMPLER_TYPE_TEMPERATURE = 6,
+enum common_sampler_type {
+ COMMON_SAMPLER_TYPE_NONE = 0,
+ COMMON_SAMPLER_TYPE_TOP_K = 1,
+ COMMON_SAMPLER_TYPE_TOP_P = 2,
+ COMMON_SAMPLER_TYPE_MIN_P = 3,
+ COMMON_SAMPLER_TYPE_TFS_Z = 4,
+ COMMON_SAMPLER_TYPE_TYPICAL_P = 5,
+ COMMON_SAMPLER_TYPE_TEMPERATURE = 6,
};
// dimensionality reduction methods, used by cvector-generator
};
// sampler parameters
-struct gpt_sampler_params {
+struct common_sampler_params {
uint32_t seed = LLAMA_DEFAULT_SEED; // the seed used to initialize llama_sampler
int32_t n_prev = 64; // number of previous tokens to remember
bool ignore_eos = false;
bool no_perf = false; // disable performance metrics
- std::vector<enum gpt_sampler_type> samplers = {
- GPT_SAMPLER_TYPE_TOP_K,
- GPT_SAMPLER_TYPE_TFS_Z,
- GPT_SAMPLER_TYPE_TYPICAL_P,
- GPT_SAMPLER_TYPE_TOP_P,
- GPT_SAMPLER_TYPE_MIN_P,
- GPT_SAMPLER_TYPE_TEMPERATURE
+ std::vector<enum common_sampler_type> samplers = {
+ COMMON_SAMPLER_TYPE_TOP_K,
+ COMMON_SAMPLER_TYPE_TFS_Z,
+ COMMON_SAMPLER_TYPE_TYPICAL_P,
+ COMMON_SAMPLER_TYPE_TOP_P,
+ COMMON_SAMPLER_TYPE_MIN_P,
+ COMMON_SAMPLER_TYPE_TEMPERATURE
};
std::string grammar; // optional BNF-like grammar to constrain sampling
std::string print() const;
};
-struct gpt_params {
+struct common_params {
int32_t n_predict = -1; // new tokens to predict
int32_t n_ctx = 0; // context size
int32_t n_batch = 2048; // logical batch size for prompt processing (must be >=32 to use BLAS)
enum llama_pooling_type pooling_type = LLAMA_POOLING_TYPE_UNSPECIFIED; // pooling type for embeddings
enum llama_attention_type attention_type = LLAMA_ATTENTION_TYPE_UNSPECIFIED; // attention type for embeddings
- struct gpt_sampler_params sparams;
+ struct common_sampler_params sparams;
std::string model = ""; // model path // NOLINT
std::string model_draft = ""; // draft model for speculative decoding // NOLINT
std::vector<llama_model_kv_override> kv_overrides;
bool lora_init_without_apply = false; // only load lora to memory, but do not apply it to ctx (user can manually apply lora later using llama_lora_adapter_apply)
- std::vector<llama_lora_adapter_info> lora_adapters; // lora adapter path with user defined scale
+ std::vector<common_lora_adapter_info> lora_adapters; // lora adapter path with user defined scale
- std::vector<llama_control_vector_load_info> control_vectors; // control vector with user defined scale
+ std::vector<common_control_vector_load_info> control_vectors; // control vector with user defined scale
int32_t verbosity = 0;
int32_t control_vector_layer_start = -1; // layer range for control vector
// call once at the start of a program if it uses libcommon
// initializes the logging system and prints info about the build
-void gpt_init();
+void common_init();
-std::string gpt_params_get_system_info(const gpt_params & params);
+std::string common_params_get_system_info(const common_params & params);
bool parse_cpu_range(const std::string& range, bool(&boolmask)[GGML_MAX_N_THREADS]);
bool parse_cpu_mask(const std::string& mask, bool(&boolmask)[GGML_MAX_N_THREADS]);
// Model utils
//
-struct llama_init_result {
+struct common_init_result {
struct llama_model * model = nullptr;
struct llama_context * context = nullptr;
- std::vector<llama_lora_adapter_container> lora_adapters;
+ std::vector<common_lora_adapter_container> lora_adapters;
};
-struct llama_init_result llama_init_from_gpt_params(gpt_params & params);
+struct common_init_result common_init_from_params(common_params & params);
-struct llama_model_params llama_model_params_from_gpt_params (const gpt_params & params);
-struct llama_context_params llama_context_params_from_gpt_params (const gpt_params & params);
+struct llama_model_params common_model_params_to_llama (const common_params & params);
+struct llama_context_params common_context_params_to_llama(const common_params & params);
struct ggml_threadpool_params ggml_threadpool_params_from_cpu_params(const cpu_params & params);
-struct llama_model * llama_load_model_from_url(const char * model_url, const char * path_model, const char * hf_token, const struct llama_model_params & params);
-struct llama_model * llama_load_model_from_hf(const char * repo, const char * file, const char * path_model, const char * hf_token, const struct llama_model_params & params);
+struct llama_model * common_load_model_from_url(const char * model_url, const char * path_model, const char * hf_token, const struct llama_model_params & params);
+struct llama_model * common_load_model_from_hf(const char * repo, const char * file, const char * path_model, const char * hf_token, const struct llama_model_params & params);
// clear LoRA adapters from context, then apply new list of adapters
-void llama_lora_adapters_apply(struct llama_context * ctx, std::vector<llama_lora_adapter_container> & lora_adapters);
+void common_lora_adapters_apply(struct llama_context * ctx, std::vector<common_lora_adapter_container> & lora_adapters);
// Batch utils
-void llama_batch_clear(struct llama_batch & batch);
+void common_batch_clear(struct llama_batch & batch);
-void llama_batch_add(
+void common_batch_add(
struct llama_batch & batch,
llama_token id,
llama_pos pos,
// tokenizes a string into a vector of tokens
// should work similar to Python's `tokenizer.encode`
-std::vector<llama_token> llama_tokenize(
+std::vector<llama_token> common_tokenize(
const struct llama_context * ctx,
const std::string & text,
bool add_special,
bool parse_special = false);
-std::vector<llama_token> llama_tokenize(
+std::vector<llama_token> common_tokenize(
const struct llama_model * model,
const std::string & text,
bool add_special,
// tokenizes a token into a piece, optionally renders special/control tokens
// should work similar to Python's `tokenizer.id_to_piece`
-std::string llama_token_to_piece(
+std::string common_token_to_piece(
const struct llama_context * ctx,
llama_token token,
bool special = true);
// detokenizes a vector of tokens into a string
// should work similar to Python's `tokenizer.decode`
// optionally renders special/control tokens
-std::string llama_detokenize(
+std::string common_detokenize(
llama_context * ctx,
const std::vector<llama_token> & tokens,
bool special = true);
//
// same with llama_chat_message, but uses std::string
-struct llama_chat_msg {
+struct common_chat_msg {
std::string role;
std::string content;
};
// Check if the template supplied via "--chat-template" is supported or not. Returns true if it's valid
-bool llama_chat_verify_template(const std::string & tmpl);
+bool common_chat_verify_template(const std::string & tmpl);
// CPP wrapper for llama_chat_apply_template
// If the built-in template is not supported, we default to chatml
// If the custom "tmpl" is not supported, we throw an error
-std::string llama_chat_apply_template(const struct llama_model * model,
+std::string common_chat_apply_template(const struct llama_model * model,
const std::string & tmpl,
- const std::vector<llama_chat_msg> & chat,
+ const std::vector<common_chat_msg> & chat,
bool add_ass);
// Format single message, while taking into account the position of that message in chat history
-std::string llama_chat_format_single(const struct llama_model * model,
+std::string common_chat_format_single(const struct llama_model * model,
const std::string & tmpl,
- const std::vector<llama_chat_msg> & past_msg,
- const llama_chat_msg & new_msg,
+ const std::vector<common_chat_msg> & past_msg,
+ const common_chat_msg & new_msg,
bool add_ass);
// Returns an example of formatted chat
-std::string llama_chat_format_example(const struct llama_model * model,
+std::string common_chat_format_example(const struct llama_model * model,
const std::string & tmpl);
//
//
// Dump the KV cache view with the number of sequences per cell.
-void llama_kv_cache_dump_view(const llama_kv_cache_view & view, int row_size = 80);
+void common_kv_cache_dump_view(const llama_kv_cache_view & view, int row_size = 80);
// Dump the KV cache view showing individual sequences in each cell (long output).
-void llama_kv_cache_dump_view_seqs(const llama_kv_cache_view & view, int row_size = 40);
+void common_kv_cache_dump_view_seqs(const llama_kv_cache_view & view, int row_size = 40);
//
// Embedding utils
//
-void llama_embd_normalize(const float * inp, float * out, int n, int embd_norm = 2);
+void common_embd_normalize(const float * inp, float * out, int n, int embd_norm = 2);
-float llama_embd_similarity_cos(const float * embd1, const float * embd2, int n);
+float common_embd_similarity_cos(const float * embd1, const float * embd2, int n);
//
// Control vector utils
//
-struct llama_control_vector_data {
+struct common_control_vector_data {
int n_embd;
// stores data for layers [1, n_layer] where n_layer = data.size() / n_embd
std::vector<float> data;
};
-struct llama_control_vector_load_info {
+struct common_control_vector_load_info {
float strength;
std::string fname;
// Load control vectors, scale each by strength, and add them together.
// On error, returns {-1, empty}
-llama_control_vector_data llama_control_vector_load(const std::vector<llama_control_vector_load_info> & load_infos);
+common_control_vector_data common_control_vector_load(const std::vector<common_control_vector_load_info> & load_infos);
//
// Split utils
void yaml_dump_string_multiline(FILE * stream, const char * prop_name, const char * data);
void yaml_dump_non_result_info(
- FILE * stream, const gpt_params & params, const llama_context * lctx,
+ FILE * stream, const common_params & params, const llama_context * lctx,
const std::string & timestamp, const std::vector<int> & prompt_tokens, const char * model_desc);
#include <thread>
#include <vector>
-int gpt_log_verbosity_thold = LOG_DEFAULT_LLAMA;
+int common_log_verbosity_thold = LOG_DEFAULT_LLAMA;
-void gpt_log_set_verbosity_thold(int verbosity) {
- gpt_log_verbosity_thold = verbosity;
+void common_log_set_verbosity_thold(int verbosity) {
+ common_log_verbosity_thold = verbosity;
}
#define LOG_COL_DEFAULT "\033[0m"
}
// colors
-enum gpt_log_col : int {
- GPT_LOG_COL_DEFAULT = 0,
- GPT_LOG_COL_BOLD,
- GPT_LOG_COL_RED,
- GPT_LOG_COL_GREEN,
- GPT_LOG_COL_YELLOW,
- GPT_LOG_COL_BLUE,
- GPT_LOG_COL_MAGENTA,
- GPT_LOG_COL_CYAN,
- GPT_LOG_COL_WHITE,
+enum common_log_col : int {
+ COMMON_LOG_COL_DEFAULT = 0,
+ COMMON_LOG_COL_BOLD,
+ COMMON_LOG_COL_RED,
+ COMMON_LOG_COL_GREEN,
+ COMMON_LOG_COL_YELLOW,
+ COMMON_LOG_COL_BLUE,
+ COMMON_LOG_COL_MAGENTA,
+ COMMON_LOG_COL_CYAN,
+ COMMON_LOG_COL_WHITE,
};
// disable colors by default
"",
};
-struct gpt_log_entry {
+struct common_log_entry {
enum ggml_log_level level;
bool prefix;
if (!fcur) {
// stderr displays DBG messages only when their verbosity level is not higher than the threshold
// these messages will still be logged to a file
- if (level == GGML_LOG_LEVEL_DEBUG && gpt_log_verbosity_thold < LOG_DEFAULT_DEBUG) {
+ if (level == GGML_LOG_LEVEL_DEBUG && common_log_verbosity_thold < LOG_DEFAULT_DEBUG) {
return;
}
if (timestamp) {
// [M.s.ms.us]
fprintf(fcur, "%s%d.%02d.%03d.%03d%s ",
- g_col[GPT_LOG_COL_BLUE],
+ g_col[COMMON_LOG_COL_BLUE],
(int) (timestamp / 1000000 / 60),
(int) (timestamp / 1000000 % 60),
(int) (timestamp / 1000 % 1000),
(int) (timestamp % 1000),
- g_col[GPT_LOG_COL_DEFAULT]);
+ g_col[COMMON_LOG_COL_DEFAULT]);
}
switch (level) {
- case GGML_LOG_LEVEL_INFO: fprintf(fcur, "%sI %s", g_col[GPT_LOG_COL_GREEN], g_col[GPT_LOG_COL_DEFAULT]); break;
- case GGML_LOG_LEVEL_WARN: fprintf(fcur, "%sW %s", g_col[GPT_LOG_COL_MAGENTA], "" ); break;
- case GGML_LOG_LEVEL_ERROR: fprintf(fcur, "%sE %s", g_col[GPT_LOG_COL_RED], "" ); break;
- case GGML_LOG_LEVEL_DEBUG: fprintf(fcur, "%sD %s", g_col[GPT_LOG_COL_YELLOW], "" ); break;
+ case GGML_LOG_LEVEL_INFO: fprintf(fcur, "%sI %s", g_col[COMMON_LOG_COL_GREEN], g_col[COMMON_LOG_COL_DEFAULT]); break;
+ case GGML_LOG_LEVEL_WARN: fprintf(fcur, "%sW %s", g_col[COMMON_LOG_COL_MAGENTA], "" ); break;
+ case GGML_LOG_LEVEL_ERROR: fprintf(fcur, "%sE %s", g_col[COMMON_LOG_COL_RED], "" ); break;
+ case GGML_LOG_LEVEL_DEBUG: fprintf(fcur, "%sD %s", g_col[COMMON_LOG_COL_YELLOW], "" ); break;
default:
break;
}
fprintf(fcur, "%s", msg.data());
if (level == GGML_LOG_LEVEL_WARN || level == GGML_LOG_LEVEL_ERROR || level == GGML_LOG_LEVEL_DEBUG) {
- fprintf(fcur, "%s", g_col[GPT_LOG_COL_DEFAULT]);
+ fprintf(fcur, "%s", g_col[COMMON_LOG_COL_DEFAULT]);
}
fflush(fcur);
}
};
-struct gpt_log {
+struct common_log {
// default capacity - will be expanded if needed
- gpt_log() : gpt_log(256) {}
+ common_log() : common_log(256) {}
- gpt_log(size_t capacity) {
+ common_log(size_t capacity) {
file = nullptr;
prefix = false;
timestamps = false;
resume();
}
- ~gpt_log() {
+ ~common_log() {
pause();
if (file) {
fclose(file);
int64_t t_start;
// ring buffer of entries
- std::vector<gpt_log_entry> entries;
+ std::vector<common_log_entry> entries;
size_t head;
size_t tail;
// worker thread copies into this
- gpt_log_entry cur;
+ common_log_entry cur;
public:
void add(enum ggml_log_level level, const char * fmt, va_list args) {
tail = (tail + 1) % entries.size();
if (tail == head) {
// expand the buffer
- std::vector<gpt_log_entry> new_entries(2*entries.size());
+ std::vector<common_log_entry> new_entries(2*entries.size());
size_t new_tail = 0;
pause();
if (colors) {
- g_col[GPT_LOG_COL_DEFAULT] = LOG_COL_DEFAULT;
- g_col[GPT_LOG_COL_BOLD] = LOG_COL_BOLD;
- g_col[GPT_LOG_COL_RED] = LOG_COL_RED;
- g_col[GPT_LOG_COL_GREEN] = LOG_COL_GREEN;
- g_col[GPT_LOG_COL_YELLOW] = LOG_COL_YELLOW;
- g_col[GPT_LOG_COL_BLUE] = LOG_COL_BLUE;
- g_col[GPT_LOG_COL_MAGENTA] = LOG_COL_MAGENTA;
- g_col[GPT_LOG_COL_CYAN] = LOG_COL_CYAN;
- g_col[GPT_LOG_COL_WHITE] = LOG_COL_WHITE;
+ g_col[COMMON_LOG_COL_DEFAULT] = LOG_COL_DEFAULT;
+ g_col[COMMON_LOG_COL_BOLD] = LOG_COL_BOLD;
+ g_col[COMMON_LOG_COL_RED] = LOG_COL_RED;
+ g_col[COMMON_LOG_COL_GREEN] = LOG_COL_GREEN;
+ g_col[COMMON_LOG_COL_YELLOW] = LOG_COL_YELLOW;
+ g_col[COMMON_LOG_COL_BLUE] = LOG_COL_BLUE;
+ g_col[COMMON_LOG_COL_MAGENTA] = LOG_COL_MAGENTA;
+ g_col[COMMON_LOG_COL_CYAN] = LOG_COL_CYAN;
+ g_col[COMMON_LOG_COL_WHITE] = LOG_COL_WHITE;
} else {
for (size_t i = 0; i < g_col.size(); i++) {
g_col[i] = "";
// public API
//
-struct gpt_log * gpt_log_init() {
- return new gpt_log;
+struct common_log * common_log_init() {
+ return new common_log;
}
-struct gpt_log * gpt_log_main() {
- static struct gpt_log log;
+struct common_log * common_log_main() {
+ static struct common_log log;
return &log;
}
-void gpt_log_pause(struct gpt_log * log) {
+void common_log_pause(struct common_log * log) {
log->pause();
}
-void gpt_log_resume(struct gpt_log * log) {
+void common_log_resume(struct common_log * log) {
log->resume();
}
-void gpt_log_free(struct gpt_log * log) {
+void common_log_free(struct common_log * log) {
delete log;
}
-void gpt_log_add(struct gpt_log * log, enum ggml_log_level level, const char * fmt, ...) {
+void common_log_add(struct common_log * log, enum ggml_log_level level, const char * fmt, ...) {
va_list args;
va_start(args, fmt);
log->add(level, fmt, args);
va_end(args);
}
-void gpt_log_set_file(struct gpt_log * log, const char * file) {
+void common_log_set_file(struct common_log * log, const char * file) {
log->set_file(file);
}
-void gpt_log_set_colors(struct gpt_log * log, bool colors) {
+void common_log_set_colors(struct common_log * log, bool colors) {
log->set_colors(colors);
}
-void gpt_log_set_prefix(struct gpt_log * log, bool prefix) {
+void common_log_set_prefix(struct common_log * log, bool prefix) {
log->set_prefix(prefix);
}
-void gpt_log_set_timestamps(struct gpt_log * log, bool timestamps) {
+void common_log_set_timestamps(struct common_log * log, bool timestamps) {
log->set_timestamps(timestamps);
}
#define LOG_DEFAULT_LLAMA 0
// needed by the LOG_TMPL macro to avoid computing log arguments if the verbosity lower
-// set via gpt_log_set_verbosity()
-extern int gpt_log_verbosity_thold;
+// set via common_log_set_verbosity()
+extern int common_log_verbosity_thold;
-void gpt_log_set_verbosity_thold(int verbosity); // not thread-safe
+void common_log_set_verbosity_thold(int verbosity); // not thread-safe
-// the gpt_log uses an internal worker thread to print/write log messages
+// the common_log uses an internal worker thread to print/write log messages
// when the worker thread is paused, incoming log messages are discarded
-struct gpt_log;
+struct common_log;
-struct gpt_log * gpt_log_init();
-struct gpt_log * gpt_log_main(); // singleton, automatically destroys itself on exit
-void gpt_log_pause (struct gpt_log * log); // pause the worker thread, not thread-safe
-void gpt_log_resume(struct gpt_log * log); // resume the worker thread, not thread-safe
-void gpt_log_free (struct gpt_log * log);
+struct common_log * common_log_init();
+struct common_log * common_log_main(); // singleton, automatically destroys itself on exit
+void common_log_pause (struct common_log * log); // pause the worker thread, not thread-safe
+void common_log_resume(struct common_log * log); // resume the worker thread, not thread-safe
+void common_log_free (struct common_log * log);
LOG_ATTRIBUTE_FORMAT(3, 4)
-void gpt_log_add(struct gpt_log * log, enum ggml_log_level level, const char * fmt, ...);
+void common_log_add(struct common_log * log, enum ggml_log_level level, const char * fmt, ...);
// defaults: file = NULL, colors = false, prefix = false, timestamps = false
//
// D - debug (stderr, V = LOG_DEFAULT_DEBUG)
//
-void gpt_log_set_file (struct gpt_log * log, const char * file); // not thread-safe
-void gpt_log_set_colors (struct gpt_log * log, bool colors); // not thread-safe
-void gpt_log_set_prefix (struct gpt_log * log, bool prefix); // whether to output prefix to each log
-void gpt_log_set_timestamps(struct gpt_log * log, bool timestamps); // whether to output timestamps in the prefix
+void common_log_set_file (struct common_log * log, const char * file); // not thread-safe
+void common_log_set_colors (struct common_log * log, bool colors); // not thread-safe
+void common_log_set_prefix (struct common_log * log, bool prefix); // whether to output prefix to each log
+void common_log_set_timestamps(struct common_log * log, bool timestamps); // whether to output timestamps in the prefix
// helper macros for logging
// use these to avoid computing log arguments if the verbosity of the log is higher than the threshold
//
// LOG_DBG("this is a debug message: %d\n", expensive_function());
//
-// this will avoid calling expensive_function() if LOG_DEFAULT_DEBUG > gpt_log_verbosity_thold
+// this will avoid calling expensive_function() if LOG_DEFAULT_DEBUG > common_log_verbosity_thold
//
#define LOG_TMPL(level, verbosity, ...) \
do { \
- if ((verbosity) <= gpt_log_verbosity_thold) { \
- gpt_log_add(gpt_log_main(), (level), __VA_ARGS__); \
+ if ((verbosity) <= common_log_verbosity_thold) { \
+ common_log_add(common_log_main(), (level), __VA_ARGS__); \
} \
} while (0)
#include <fstream>
#include <thread>
-void llama_ngram_cache_update(llama_ngram_cache & ngram_cache, int ngram_min, int ngram_max,
+void common_ngram_cache_update(common_ngram_cache & ngram_cache, int ngram_min, int ngram_max,
std::vector<llama_token> & inp, int nnew, bool print_progress) {
const int64_t t_start_ms = ggml_time_ms();
const int64_t inp_size = inp.size();
const int64_t i_start = std::max(inp_size - nnew, ngram_size);
for (int64_t i = i_start; i < inp_size; ++i) {
const int64_t ngram_start = i - ngram_size;
- llama_ngram ngram(&inp[ngram_start], ngram_size);
+ common_ngram ngram(&inp[ngram_start], ngram_size);
const llama_token token = inp[i];
- llama_ngram_cache::iterator part_it = ngram_cache.find(ngram);
+ common_ngram_cache::iterator part_it = ngram_cache.find(ngram);
if (part_it == ngram_cache.end()) {
- llama_ngram_cache_part part;
+ common_ngram_cache_part part;
part.emplace(token, 1);
ngram_cache.emplace(ngram, part);
} else {
- llama_ngram_cache_part::iterator token_count_it = part_it->second.find(token);
+ common_ngram_cache_part::iterator token_count_it = part_it->second.find(token);
if (token_count_it == part_it->second.end()) {
part_it->second.emplace(token, 1);
} else {
constexpr int draft_min_percent_strict[LLAMA_NGRAM_MAX] = {75, 66, 66, 66};
// Helper function that tries to draft a token from only the static ngram cache:
-static llama_token try_draft(llama_ngram_cache & nc_static, const llama_ngram ngram_static) {
- llama_ngram_cache::iterator part_static_it = nc_static.find(ngram_static);
+static llama_token try_draft(common_ngram_cache & nc_static, const common_ngram ngram_static) {
+ common_ngram_cache::iterator part_static_it = nc_static.find(ngram_static);
if (part_static_it == nc_static.end()) {
return -1;
}
- const llama_ngram_cache_part part_static = part_static_it->second;
+ const common_ngram_cache_part part_static = part_static_it->second;
int max_count_static = 0;
int sum_count_static = 0;
// Try to draft a token from primary cache (context/dynamic), validate with static cache:
static llama_token try_draft(
- llama_ngram_cache & nc_primary, const std::vector<llama_ngram> & ngrams_primary, llama_ngram_cache_part & part_static,
+ common_ngram_cache & nc_primary, const std::vector<common_ngram> & ngrams_primary, common_ngram_cache_part & part_static,
const int * min_sample_size, const int * min_percent) {
llama_token drafted_token = -1;
for (int i = ngrams_primary.size()-1; i >= 0 && drafted_token == -1; --i) {
- const llama_ngram ngram_primary = ngrams_primary[i];
+ const common_ngram ngram_primary = ngrams_primary[i];
- llama_ngram_cache::iterator part_primary_it = nc_primary.find(ngram_primary);
+ common_ngram_cache::iterator part_primary_it = nc_primary.find(ngram_primary);
if (part_primary_it == nc_primary.end()) {
continue;
}
- const llama_ngram_cache_part part_primary = part_primary_it->second;
+ const common_ngram_cache_part part_primary = part_primary_it->second;
int max_count_primary = 0;
int max_count_static = 0;
for (std::pair<llama_token, int> token_count_primary : part_primary) {
const llama_token token = token_count_primary.first;
- llama_ngram_cache_part::iterator token_count_static_it = part_static.find(token);
+ common_ngram_cache_part::iterator token_count_static_it = part_static.find(token);
const int32_t count_primary = token_count_primary.second;
const int32_t count_static = token_count_static_it != part_static.end() ? 100*token_count_static_it->second : 1;
return drafted_token;
}
-void llama_ngram_cache_draft(
+void common_ngram_cache_draft(
std::vector<llama_token> & inp, std::vector<llama_token> & draft, int n_draft, int ngram_min, int ngram_max,
- llama_ngram_cache & nc_context, llama_ngram_cache & nc_dynamic, llama_ngram_cache & nc_static
+ common_ngram_cache & nc_context, common_ngram_cache & nc_dynamic, common_ngram_cache & nc_static
) {
GGML_ASSERT(draft.size() == 1);
const int inp_size = inp.size();
llama_token drafted_token = -1;
const int ngram_start_static = inp_size-LLAMA_NGRAM_STATIC + draft.size()-1;
- llama_ngram ngram_static;
+ common_ngram ngram_static;
for (int j = ngram_start_static; j < ngram_start_static + LLAMA_NGRAM_STATIC; ++j) {
ngram_static.tokens[j-ngram_start_static] = get_token(inp, draft, j);
}
- llama_ngram_cache::iterator part_static_it = nc_static.find(ngram_static);
- llama_ngram_cache_part part_static;
+ common_ngram_cache::iterator part_static_it = nc_static.find(ngram_static);
+ common_ngram_cache_part part_static;
if (part_static_it != nc_static.end()) {
part_static = part_static_it->second;
}
// cd = context + dynamic
- std::vector<llama_ngram> ngrams_cd;
+ std::vector<common_ngram> ngrams_cd;
for (int ngram_size_cd = ngram_min; ngram_size_cd <= ngram_max; ++ngram_size_cd) {
const int ngram_start_cd = inp_size-ngram_size_cd + draft.size()-1;
- llama_ngram ngram_cd;
+ common_ngram ngram_cd;
for (int j = ngram_start_cd; j < ngram_start_cd + ngram_size_cd; ++j) {
ngram_cd.tokens[j-ngram_start_cd] = get_token(inp, draft, j);
}
}
}
-void llama_ngram_cache_save(llama_ngram_cache & ngram_cache, std::string & filename) {
+void common_ngram_cache_save(common_ngram_cache & ngram_cache, std::string & filename) {
std::ofstream file_out(filename, std::ios::binary);
- for (std::pair<llama_ngram, llama_ngram_cache_part> item : ngram_cache) {
- const llama_ngram ngram = item.first;
- llama_ngram_cache_part token_counts = item.second;
+ for (std::pair<common_ngram, common_ngram_cache_part> item : ngram_cache) {
+ const common_ngram ngram = item.first;
+ common_ngram_cache_part token_counts = item.second;
GGML_ASSERT(!token_counts.empty());
const int32_t ntokens = token_counts.size();
GGML_ASSERT(ntokens > 0);
- file_out.write(reinterpret_cast<const char *>(&ngram), sizeof(llama_ngram));
+ file_out.write(reinterpret_cast<const char *>(&ngram), sizeof(common_ngram));
file_out.write(reinterpret_cast<const char *>(&ntokens), sizeof(int32_t));
for (std::pair<llama_token, int32_t> item2 : token_counts) {
const llama_token token = item2.first;
}
-llama_ngram_cache llama_ngram_cache_load(std::string & filename) {
+common_ngram_cache common_ngram_cache_load(std::string & filename) {
std::ifstream hashmap_file(filename, std::ios::binary);
if (!hashmap_file) {
throw std::ifstream::failure("Unable to open file " + filename);
}
- llama_ngram_cache ngram_cache;
+ common_ngram_cache ngram_cache;
- llama_ngram ngram;
+ common_ngram ngram;
int32_t ntokens;
llama_token token;
int32_t count;
char * ntokensc = reinterpret_cast<char*>(&ntokens);
char * tokenc = reinterpret_cast<char*>(&token);
char * countc = reinterpret_cast<char*>(&count);
- while(hashmap_file.read(ngramc, sizeof(llama_ngram))) {
+ while(hashmap_file.read(ngramc, sizeof(common_ngram))) {
GGML_ASSERT(!hashmap_file.eof());
GGML_ASSERT(hashmap_file.read(ntokensc, sizeof(int32_t)));
GGML_ASSERT(ntokens > 0);
- llama_ngram_cache_part token_counts;
+ common_ngram_cache_part token_counts;
for (int i = 0; i < ntokens; ++i) {
GGML_ASSERT(!hashmap_file.eof());
return ngram_cache;
}
-void llama_ngram_cache_merge(llama_ngram_cache & ngram_cache_target, llama_ngram_cache & ngram_cache_add) {
- for (std::pair<llama_ngram, llama_ngram_cache_part> ngram_part : ngram_cache_add) {
- const llama_ngram ngram = ngram_part.first;
- llama_ngram_cache_part part = ngram_part.second;
+void common_ngram_cache_merge(common_ngram_cache & ngram_cache_target, common_ngram_cache & ngram_cache_add) {
+ for (std::pair<common_ngram, common_ngram_cache_part> ngram_part : ngram_cache_add) {
+ const common_ngram ngram = ngram_part.first;
+ common_ngram_cache_part part = ngram_part.second;
- llama_ngram_cache::iterator part_merged_it = ngram_cache_target.find(ngram);
+ common_ngram_cache::iterator part_merged_it = ngram_cache_target.find(ngram);
if (part_merged_it == ngram_cache_target.end()) {
ngram_cache_target.emplace(ngram, part);
continue;
const int32_t count = token_count.second;
GGML_ASSERT(count > 0);
- llama_ngram_cache_part::iterator token_count_merged_it = part_merged_it->second.find(token);
+ common_ngram_cache_part::iterator token_count_merged_it = part_merged_it->second.find(token);
if (token_count_merged_it == part_merged_it->second.end()) {
part_merged_it->second.emplace(token, count);
continue;
// Data structures to map n-grams to empirical token probabilities:
-struct llama_ngram {
+struct common_ngram {
llama_token tokens[LLAMA_NGRAM_MAX];
- llama_ngram() {
+ common_ngram() {
for (int i = 0; i < LLAMA_NGRAM_MAX; ++i) {
tokens[i] = -1;
}
}
- llama_ngram(const llama_token * input, const int ngram_size) {
+ common_ngram(const llama_token * input, const int ngram_size) {
for (int i = 0; i < LLAMA_NGRAM_MAX; ++i) {
tokens[i] = i < ngram_size ? input[i] : -1;
}
}
- bool operator==(const llama_ngram & other) const {
+ bool operator==(const common_ngram & other) const {
for (int i = 0; i < LLAMA_NGRAM_MAX; ++i) {
if (tokens[i] != other.tokens[i]) {
return false;
}
};
-struct llama_token_hash_function {
+struct common_token_hash_function {
size_t operator()(const llama_token token) const {
// see https://probablydance.com/2018/06/16/fibonacci-hashing-the-optimization-that-the-world-forgot-or-a-better-alternative-to-integer-modulo/
return token * 11400714819323198485llu;
}
};
-struct llama_ngram_hash_function {
- size_t operator()(const llama_ngram & ngram) const {
- size_t hash = llama_token_hash_function{}(ngram.tokens[0]);
+struct common_ngram_hash_function {
+ size_t operator()(const common_ngram & ngram) const {
+ size_t hash = common_token_hash_function{}(ngram.tokens[0]);
for (int i = 1; i < LLAMA_NGRAM_MAX; ++i) {
- hash ^= llama_token_hash_function{}(ngram.tokens[i]);
+ hash ^= common_token_hash_function{}(ngram.tokens[i]);
}
return hash;
}
};
// token -> number of times token has been seen
-typedef std::unordered_map<llama_token, int32_t> llama_ngram_cache_part;
+typedef std::unordered_map<llama_token, int32_t> common_ngram_cache_part;
// n-gram -> empirical distribution of following tokens
-typedef std::unordered_map<llama_ngram, llama_ngram_cache_part, llama_ngram_hash_function> llama_ngram_cache;
+typedef std::unordered_map<common_ngram, common_ngram_cache_part, common_ngram_hash_function> common_ngram_cache;
// Update an ngram cache with tokens.
//
// In order to get correct results inp_data can ONLY BE APPENDED TO.
// Changes in the middle need a complete rebuild.
-void llama_ngram_cache_update(
- llama_ngram_cache & ngram_cache, int ngram_min, int ngram_max, std::vector<llama_token> & inp_data, int nnew, bool print_progress);
+void common_ngram_cache_update(
+ common_ngram_cache & ngram_cache, int ngram_min, int ngram_max, std::vector<llama_token> & inp_data, int nnew, bool print_progress);
// Try to draft tokens from ngram caches.
// inp: the tokens generated so far.
// nc_context: ngram cache based on current context.
// nc_dynamic: ngram cache based on previous user generations.
// nc_static: ngram cache generated from a large text corpus, used for validation.
-void llama_ngram_cache_draft(
+void common_ngram_cache_draft(
std::vector<llama_token> & inp, std::vector<llama_token> & draft, int n_draft, int ngram_min, int ngram_max,
- llama_ngram_cache & nc_context, llama_ngram_cache & nc_dynamic, llama_ngram_cache & nc_static);
+ common_ngram_cache & nc_context, common_ngram_cache & nc_dynamic, common_ngram_cache & nc_static);
// Save an ngram cache to a file.
// ngram_cache: the ngram cache to save.
// filename: the path under which to save the ngram cache.
-void llama_ngram_cache_save(llama_ngram_cache & ngram_cache, std::string & filename);
+void common_ngram_cache_save(common_ngram_cache & ngram_cache, std::string & filename);
-// Load an ngram cache saved with llama_ngram_cache_save.
+// Load an ngram cache saved with common_ngram_cache_save.
// filename: the path from which to load the ngram cache.
// returns: an ngram cache containing the information saved to filename.
-llama_ngram_cache llama_ngram_cache_load(std::string & filename);
+common_ngram_cache common_ngram_cache_load(std::string & filename);
// Merge two ngram caches.
// ngram_cache_target: the ngram cache to which to add the information from ngram_cache_add.
// ngram_cache_add: the ngram cache to add to ngram_cache_target.
-void llama_ngram_cache_merge(llama_ngram_cache & ngram_cache_target, llama_ngram_cache & ngram_cache_add);
+void common_ngram_cache_merge(common_ngram_cache & ngram_cache_target, common_ngram_cache & ngram_cache_add);
std::vector<T> data;
};
-struct gpt_sampler {
- gpt_sampler_params params;
+struct common_sampler {
+ common_sampler_params params;
struct llama_sampler * grmr;
struct llama_sampler * chain;
}
};
-std::string gpt_sampler_params::print() const {
+std::string common_sampler_params::print() const {
char result[1024];
snprintf(result, sizeof(result),
return std::string(result);
}
-struct gpt_sampler * gpt_sampler_init(const struct llama_model * model, const struct gpt_sampler_params & params) {
+struct common_sampler * common_sampler_init(const struct llama_model * model, const struct common_sampler_params & params) {
llama_sampler_chain_params lparams = llama_sampler_chain_default_params();
lparams.no_perf = params.no_perf;
- auto * result = new gpt_sampler {
+ auto * result = new common_sampler {
/* .params = */ params,
/* .grmr = */ llama_sampler_init_grammar(model, params.grammar.c_str(), "root"),
/* .chain = */ llama_sampler_chain_init(lparams),
if (params.mirostat == 0) {
for (const auto & cnstr : params.samplers) {
switch (cnstr) {
- case GPT_SAMPLER_TYPE_TOP_K:
+ case COMMON_SAMPLER_TYPE_TOP_K:
llama_sampler_chain_add(result->chain, llama_sampler_init_top_k (params.top_k));
break;
- case GPT_SAMPLER_TYPE_TOP_P:
+ case COMMON_SAMPLER_TYPE_TOP_P:
llama_sampler_chain_add(result->chain, llama_sampler_init_top_p (params.top_p, params.min_keep));
break;
- case GPT_SAMPLER_TYPE_MIN_P:
+ case COMMON_SAMPLER_TYPE_MIN_P:
llama_sampler_chain_add(result->chain, llama_sampler_init_min_p (params.min_p, params.min_keep));
break;
- case GPT_SAMPLER_TYPE_TFS_Z:
+ case COMMON_SAMPLER_TYPE_TFS_Z:
llama_sampler_chain_add(result->chain, llama_sampler_init_tail_free(params.tfs_z, params.min_keep));
break;
- case GPT_SAMPLER_TYPE_TYPICAL_P:
+ case COMMON_SAMPLER_TYPE_TYPICAL_P:
llama_sampler_chain_add(result->chain, llama_sampler_init_typical (params.typ_p, params.min_keep));
break;
- case GPT_SAMPLER_TYPE_TEMPERATURE:
+ case COMMON_SAMPLER_TYPE_TEMPERATURE:
llama_sampler_chain_add(result->chain, llama_sampler_init_temp_ext (params.temp, params.dynatemp_range, params.dynatemp_exponent));
break;
default:
return result;
}
-void gpt_sampler_free(struct gpt_sampler * gsmpl) {
+void common_sampler_free(struct common_sampler * gsmpl) {
if (gsmpl) {
llama_sampler_free(gsmpl->grmr);
}
}
-void gpt_sampler_accept(struct gpt_sampler * gsmpl, llama_token token, bool accept_grammar) {
+void common_sampler_accept(struct common_sampler * gsmpl, llama_token token, bool accept_grammar) {
if (accept_grammar) {
llama_sampler_accept(gsmpl->grmr, token);
}
gsmpl->prev.push_back(token);
}
-void gpt_sampler_reset(struct gpt_sampler * gsmpl) {
+void common_sampler_reset(struct common_sampler * gsmpl) {
llama_sampler_reset(gsmpl->grmr);
llama_sampler_reset(gsmpl->chain);
}
-struct gpt_sampler * gpt_sampler_clone(gpt_sampler * gsmpl) {
- return new gpt_sampler {
+struct common_sampler * common_sampler_clone(common_sampler * gsmpl) {
+ return new common_sampler {
/* .params = */ gsmpl->params,
/* .grmr = */ llama_sampler_clone(gsmpl->grmr),
/* .chain = */ llama_sampler_clone(gsmpl->chain),
};
}
-void gpt_perf_print(const struct llama_context * ctx, const struct gpt_sampler * gsmpl) {
+void common_perf_print(const struct llama_context * ctx, const struct common_sampler * gsmpl) {
// TODO: measure grammar performance
if (gsmpl) {
}
}
-llama_token gpt_sampler_sample(struct gpt_sampler * gsmpl, struct llama_context * ctx, int idx, bool grammar_first) {
+llama_token common_sampler_sample(struct common_sampler * gsmpl, struct llama_context * ctx, int idx, bool grammar_first) {
gsmpl->set_logits(ctx, idx);
auto & grmr = gsmpl->grmr;
return cur_p.data[cur_p.selected].id;
}
-uint32_t gpt_sampler_get_seed(const struct gpt_sampler * gsmpl) {
+uint32_t common_sampler_get_seed(const struct common_sampler * gsmpl) {
return llama_sampler_get_seed(gsmpl->chain);
}
// helpers
-llama_token_data_array * gpt_sampler_get_candidates(struct gpt_sampler * gsmpl) {
+llama_token_data_array * common_sampler_get_candidates(struct common_sampler * gsmpl) {
return &gsmpl->cur_p;
}
-llama_token gpt_sampler_last(const struct gpt_sampler * gsmpl) {
+llama_token common_sampler_last(const struct common_sampler * gsmpl) {
return gsmpl->prev.rat(0);
}
-std::string gpt_sampler_print(const struct gpt_sampler * gsmpl) {
+std::string common_sampler_print(const struct common_sampler * gsmpl) {
std::string result = "logits ";
for (int i = 0; i < llama_sampler_chain_n(gsmpl->chain); i++) {
return result;
}
-std::string gpt_sampler_prev_str(gpt_sampler * gsmpl, llama_context * ctx_main, int n) {
+std::string common_sampler_prev_str(common_sampler * gsmpl, llama_context * ctx_main, int n) {
n = std::min(n, (int) gsmpl->prev.size());
if (n <= 0) {
GGML_ASSERT(id != LLAMA_TOKEN_NULL && "null token in the sampling history - should not happen");
- result += llama_token_to_piece(ctx_main, id);
+ result += common_token_to_piece(ctx_main, id);
}
return result;
}
-char gpt_sampler_type_to_chr(enum gpt_sampler_type cnstr) {
+char common_sampler_type_to_chr(enum common_sampler_type cnstr) {
switch (cnstr) {
- case GPT_SAMPLER_TYPE_TOP_K: return 'k';
- case GPT_SAMPLER_TYPE_TFS_Z: return 'f';
- case GPT_SAMPLER_TYPE_TYPICAL_P: return 'y';
- case GPT_SAMPLER_TYPE_TOP_P: return 'p';
- case GPT_SAMPLER_TYPE_MIN_P: return 'm';
- case GPT_SAMPLER_TYPE_TEMPERATURE: return 't';
+ case COMMON_SAMPLER_TYPE_TOP_K: return 'k';
+ case COMMON_SAMPLER_TYPE_TFS_Z: return 'f';
+ case COMMON_SAMPLER_TYPE_TYPICAL_P: return 'y';
+ case COMMON_SAMPLER_TYPE_TOP_P: return 'p';
+ case COMMON_SAMPLER_TYPE_MIN_P: return 'm';
+ case COMMON_SAMPLER_TYPE_TEMPERATURE: return 't';
default : return '?';
}
}
-std::string gpt_sampler_type_to_str(enum gpt_sampler_type cnstr) {
+std::string common_sampler_type_to_str(enum common_sampler_type cnstr) {
switch (cnstr) {
- case GPT_SAMPLER_TYPE_TOP_K: return "top_k";
- case GPT_SAMPLER_TYPE_TFS_Z: return "tfs_z";
- case GPT_SAMPLER_TYPE_TYPICAL_P: return "typ_p";
- case GPT_SAMPLER_TYPE_TOP_P: return "top_p";
- case GPT_SAMPLER_TYPE_MIN_P: return "min_p";
- case GPT_SAMPLER_TYPE_TEMPERATURE: return "temperature";
+ case COMMON_SAMPLER_TYPE_TOP_K: return "top_k";
+ case COMMON_SAMPLER_TYPE_TFS_Z: return "tfs_z";
+ case COMMON_SAMPLER_TYPE_TYPICAL_P: return "typ_p";
+ case COMMON_SAMPLER_TYPE_TOP_P: return "top_p";
+ case COMMON_SAMPLER_TYPE_MIN_P: return "min_p";
+ case COMMON_SAMPLER_TYPE_TEMPERATURE: return "temperature";
default : return "";
}
}
-std::vector<gpt_sampler_type> gpt_sampler_types_from_names(const std::vector<std::string> & names, bool allow_alt_names) {
- std::unordered_map<std::string, gpt_sampler_type> sampler_canonical_name_map {
- { "top_k", GPT_SAMPLER_TYPE_TOP_K },
- { "top_p", GPT_SAMPLER_TYPE_TOP_P },
- { "typ_p", GPT_SAMPLER_TYPE_TYPICAL_P },
- { "min_p", GPT_SAMPLER_TYPE_MIN_P },
- { "tfs_z", GPT_SAMPLER_TYPE_TFS_Z },
- { "temperature", GPT_SAMPLER_TYPE_TEMPERATURE },
+std::vector<common_sampler_type> common_sampler_types_from_names(const std::vector<std::string> & names, bool allow_alt_names) {
+ std::unordered_map<std::string, common_sampler_type> sampler_canonical_name_map {
+ { "top_k", COMMON_SAMPLER_TYPE_TOP_K },
+ { "top_p", COMMON_SAMPLER_TYPE_TOP_P },
+ { "typ_p", COMMON_SAMPLER_TYPE_TYPICAL_P },
+ { "min_p", COMMON_SAMPLER_TYPE_MIN_P },
+ { "tfs_z", COMMON_SAMPLER_TYPE_TFS_Z },
+ { "temperature", COMMON_SAMPLER_TYPE_TEMPERATURE },
};
// since samplers names are written multiple ways
// make it ready for both system names and input names
- std::unordered_map<std::string, gpt_sampler_type> sampler_alt_name_map {
- { "top-k", GPT_SAMPLER_TYPE_TOP_K },
- { "top-p", GPT_SAMPLER_TYPE_TOP_P },
- { "nucleus", GPT_SAMPLER_TYPE_TOP_P },
- { "typical-p", GPT_SAMPLER_TYPE_TYPICAL_P },
- { "typical", GPT_SAMPLER_TYPE_TYPICAL_P },
- { "typ-p", GPT_SAMPLER_TYPE_TYPICAL_P },
- { "typ", GPT_SAMPLER_TYPE_TYPICAL_P },
- { "min-p", GPT_SAMPLER_TYPE_MIN_P },
- { "tfs-z", GPT_SAMPLER_TYPE_TFS_Z },
- { "tfs", GPT_SAMPLER_TYPE_TFS_Z },
- { "temp", GPT_SAMPLER_TYPE_TEMPERATURE },
+ std::unordered_map<std::string, common_sampler_type> sampler_alt_name_map {
+ { "top-k", COMMON_SAMPLER_TYPE_TOP_K },
+ { "top-p", COMMON_SAMPLER_TYPE_TOP_P },
+ { "nucleus", COMMON_SAMPLER_TYPE_TOP_P },
+ { "typical-p", COMMON_SAMPLER_TYPE_TYPICAL_P },
+ { "typical", COMMON_SAMPLER_TYPE_TYPICAL_P },
+ { "typ-p", COMMON_SAMPLER_TYPE_TYPICAL_P },
+ { "typ", COMMON_SAMPLER_TYPE_TYPICAL_P },
+ { "min-p", COMMON_SAMPLER_TYPE_MIN_P },
+ { "tfs-z", COMMON_SAMPLER_TYPE_TFS_Z },
+ { "tfs", COMMON_SAMPLER_TYPE_TFS_Z },
+ { "temp", COMMON_SAMPLER_TYPE_TEMPERATURE },
};
- std::vector<gpt_sampler_type> samplers;
+ std::vector<common_sampler_type> samplers;
samplers.reserve(names.size());
for (const auto & name : names) {
return samplers;
}
-std::vector<gpt_sampler_type> gpt_sampler_types_from_chars(const std::string & chars) {
- std::unordered_map<char, gpt_sampler_type> sampler_name_map = {
- { gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_TOP_K), GPT_SAMPLER_TYPE_TOP_K },
- { gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_TFS_Z), GPT_SAMPLER_TYPE_TFS_Z },
- { gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_TYPICAL_P), GPT_SAMPLER_TYPE_TYPICAL_P },
- { gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_TOP_P), GPT_SAMPLER_TYPE_TOP_P },
- { gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_MIN_P), GPT_SAMPLER_TYPE_MIN_P },
- { gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_TEMPERATURE), GPT_SAMPLER_TYPE_TEMPERATURE }
+std::vector<common_sampler_type> common_sampler_types_from_chars(const std::string & chars) {
+ std::unordered_map<char, common_sampler_type> sampler_name_map = {
+ { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TOP_K), COMMON_SAMPLER_TYPE_TOP_K },
+ { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TFS_Z), COMMON_SAMPLER_TYPE_TFS_Z },
+ { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TYPICAL_P), COMMON_SAMPLER_TYPE_TYPICAL_P },
+ { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TOP_P), COMMON_SAMPLER_TYPE_TOP_P },
+ { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_MIN_P), COMMON_SAMPLER_TYPE_MIN_P },
+ { common_sampler_type_to_chr(COMMON_SAMPLER_TYPE_TEMPERATURE), COMMON_SAMPLER_TYPE_TEMPERATURE }
};
- std::vector<gpt_sampler_type> samplers;
+ std::vector<common_sampler_type> samplers;
samplers.reserve(chars.size());
for (const auto & c : chars) {
#include <string>
#include <vector>
-// gpt_sampler extends llama_sampler with additional functionality:
+// common_sampler extends llama_sampler with additional functionality:
//
// - grammar support
// - custom sampler logic based on the parameters
// token in order to verify if it fits the grammar. And only if the token doesn't fit the grammar, the
// grammar constraints are applied to the full vocabulary and the token is resampled.
//
-// The gpt_sampler also maintains a container with the last accepted tokens. In the future, this can
+// The common_sampler also maintains a container with the last accepted tokens. In the future, this can
// be moved into the core llama library.
//
-// For convenience, the gpt_sampler also maintains a container with the current candidate tokens.
+// For convenience, the common_sampler also maintains a container with the current candidate tokens.
// This can be used to access the probabilities of the rest of the non-sampled tokens.
//
// TODO: measure grammar performance
//
-struct gpt_sampler;
+struct common_sampler;
// llama_sampler API overloads
-struct gpt_sampler * gpt_sampler_init(const struct llama_model * model, const struct gpt_sampler_params & params);
+struct common_sampler * common_sampler_init(const struct llama_model * model, const struct common_sampler_params & params);
-void gpt_sampler_free(struct gpt_sampler * gsmpl);
+void common_sampler_free(struct common_sampler * gsmpl);
// if accept_grammar is true, the token is accepted both by the sampling chain and the grammar
-void gpt_sampler_accept(struct gpt_sampler * gsmpl, llama_token token, bool accept_grammar);
-void gpt_sampler_reset (struct gpt_sampler * gsmpl);
-struct gpt_sampler * gpt_sampler_clone (struct gpt_sampler * gsmpl);
+void common_sampler_accept(struct common_sampler * gsmpl, llama_token token, bool accept_grammar);
+void common_sampler_reset (struct common_sampler * gsmpl);
+struct common_sampler * common_sampler_clone (struct common_sampler * gsmpl);
// arguments can be nullptr to skip printing
-void gpt_perf_print(const struct llama_context * ctx, const struct gpt_sampler * gsmpl);
+void common_perf_print(const struct llama_context * ctx, const struct common_sampler * gsmpl);
// extended sampling implementation:
//
// if grammar_first is true, the grammar is applied before the samplers (slower)
// useful in cases where all the resulting candidates (not just the sampled one) must fit the grammar
//
-llama_token gpt_sampler_sample(struct gpt_sampler * gsmpl, struct llama_context * ctx, int idx, bool grammar_first = false);
+llama_token common_sampler_sample(struct common_sampler * gsmpl, struct llama_context * ctx, int idx, bool grammar_first = false);
-uint32_t gpt_sampler_get_seed(const struct gpt_sampler * gsmpl);
+uint32_t common_sampler_get_seed(const struct common_sampler * gsmpl);
// helpers
// access the internal list of current candidate tokens
-llama_token_data_array * gpt_sampler_get_candidates(struct gpt_sampler * gsmpl);
+llama_token_data_array * common_sampler_get_candidates(struct common_sampler * gsmpl);
// get the last accepted token
-llama_token gpt_sampler_last(const struct gpt_sampler * gsmpl);
+llama_token common_sampler_last(const struct common_sampler * gsmpl);
// print the sampler chain into a string
-std::string gpt_sampler_print(const struct gpt_sampler * gsmpl);
+std::string common_sampler_print(const struct common_sampler * gsmpl);
// get a string representation of the last accepted tokens
-std::string gpt_sampler_prev_str(gpt_sampler * gsmpl, llama_context * ctx, int n);
+std::string common_sampler_prev_str(common_sampler * gsmpl, llama_context * ctx, int n);
-char gpt_sampler_type_to_chr(enum gpt_sampler_type cnstr);
-std::string gpt_sampler_type_to_str(enum gpt_sampler_type cnstr);
+char common_sampler_type_to_chr(enum common_sampler_type cnstr);
+std::string common_sampler_type_to_str(enum common_sampler_type cnstr);
-std::vector<enum gpt_sampler_type> gpt_sampler_types_from_names(const std::vector<std::string> & names, bool allow_alt_names);
-std::vector<enum gpt_sampler_type> gpt_sampler_types_from_chars(const std::string & chars);
+std::vector<enum common_sampler_type> common_sampler_types_from_names(const std::vector<std::string> & names, bool allow_alt_names);
+std::vector<enum common_sampler_type> common_sampler_types_from_chars(const std::string & chars);
}
int main(int argc, char ** argv) {
- gpt_params params;
+ common_params params;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_BENCH, print_usage)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_BENCH, print_usage)) {
return 1;
}
- gpt_init();
+ common_init();
int is_pp_shared = params.is_pp_shared;
// initialize the model
- llama_model_params model_params = llama_model_params_from_gpt_params(params);
+ llama_model_params model_params = common_model_params_to_llama(params);
llama_model * model = llama_load_model_from_file(params.model.c_str(), model_params);
return 1;
}
- llama_context_params ctx_params = llama_context_params_from_gpt_params(params);
+ llama_context_params ctx_params = common_context_params_to_llama(params);
// ensure enough sequences are available
ctx_params.n_seq_max = n_pl.empty() ? 1 : *std::max_element(n_pl.begin(), n_pl.end());
// warm up
{
for (int i = 0; i < 16; ++i) {
- llama_batch_add(batch, 0, i, { 0 }, false);
+ common_batch_add(batch, 0, i, { 0 }, false);
}
if (!decode_helper(ctx, batch, ctx_params.n_batch)) {
continue;
}
- llama_batch_clear(batch);
+ common_batch_clear(batch);
for (int i = 0; i < pp; ++i) {
for (int j = 0; j < (is_pp_shared ? 1 : pl); ++j) {
- llama_batch_add(batch, 0, i, { j }, false);
+ common_batch_add(batch, 0, i, { j }, false);
}
}
batch.logits[batch.n_tokens - 1] = true;
const auto t_tg_start = ggml_time_us();
for (int i = 0; i < tg; ++i) {
- llama_batch_clear(batch);
+ common_batch_clear(batch);
for (int j = 0; j < pl; ++j) {
- llama_batch_add(batch, 0, pp + i, { j }, true);
+ common_batch_add(batch, 0, pp + i, { j }, true);
}
if (!decode_helper(ctx, batch, ctx_params.n_batch)) {
}
int main(int argc, char ** argv) {
- gpt_params params;
+ common_params params;
params.prompt = "Hello my name is";
params.n_predict = 32;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON, print_usage)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON, print_usage)) {
return 1;
}
- gpt_init();
+ common_init();
// number of parallel batches
int n_parallel = params.n_parallel;
// initialize the model
- llama_model_params model_params = llama_model_params_from_gpt_params(params);
+ llama_model_params model_params = common_model_params_to_llama(params);
llama_model * model = llama_load_model_from_file(params.model.c_str(), model_params);
// tokenize the prompt
std::vector<llama_token> tokens_list;
- tokens_list = ::llama_tokenize(model, params.prompt, true);
+ tokens_list = common_tokenize(model, params.prompt, true);
const int n_kv_req = tokens_list.size() + (n_predict - tokens_list.size())*n_parallel;
// initialize the context
- llama_context_params ctx_params = llama_context_params_from_gpt_params(params);
+ llama_context_params ctx_params = common_context_params_to_llama(params);
ctx_params.n_ctx = n_kv_req;
ctx_params.n_batch = std::max(n_predict, n_parallel);
LOG("\n");
for (auto id : tokens_list) {
- LOG("%s", llama_token_to_piece(ctx, id).c_str());
+ LOG("%s", common_token_to_piece(ctx, id).c_str());
}
// create a llama_batch
// evaluate the initial prompt
for (size_t i = 0; i < tokens_list.size(); ++i) {
- llama_batch_add(batch, tokens_list[i], i, seq_ids, false);
+ common_batch_add(batch, tokens_list[i], i, seq_ids, false);
}
GGML_ASSERT(batch.n_tokens == (int) tokens_list.size());
decoder_start_token_id = llama_token_bos(model);
}
- llama_batch_clear(batch);
- llama_batch_add(batch, decoder_start_token_id, 0, seq_ids, false);
+ common_batch_clear(batch);
+ common_batch_add(batch, decoder_start_token_id, 0, seq_ids, false);
}
// llama_decode will output logits only for the last token of the prompt
while (n_cur <= n_predict) {
// prepare the next batch
- llama_batch_clear(batch);
+ common_batch_clear(batch);
// sample the next token for each parallel sequence / stream
for (int32_t i = 0; i < n_parallel; ++i) {
// if there is only one stream, we print immediately to stdout
if (n_parallel == 1) {
- LOG("%s", llama_token_to_piece(ctx, new_token_id).c_str());
+ LOG("%s", common_token_to_piece(ctx, new_token_id).c_str());
}
- streams[i] += llama_token_to_piece(ctx, new_token_id);
+ streams[i] += common_token_to_piece(ctx, new_token_id);
i_batch[i] = batch.n_tokens;
// push this new token for next evaluation
- llama_batch_add(batch, new_token_id, n_cur, { i }, true);
+ common_batch_add(batch, new_token_id, n_cur, { i }, true);
n_decode += 1;
}
}
int main(int argc, char ** argv) {
- gpt_init();
+ common_init();
struct train_params params = get_default_train_params();
if (!params_parse(argc, argv, ¶ms)) {
static std::string tokens_to_str(llama_context * ctx, Iter begin, Iter end) {
std::string ret;
for (; begin != end; ++begin) {
- ret += llama_token_to_piece(ctx, *begin);
+ ret += common_token_to_piece(ctx, *begin);
}
return ret;
tokenized_prompt(llama_context * ctx, std::string pos, std::string neg) {
const bool add_bos = llama_add_bos_token(llama_get_model(ctx));
- tokens_pos = ::llama_tokenize(ctx, pos, add_bos, true);
- tokens_neg = ::llama_tokenize(ctx, neg, add_bos, true);
+ tokens_pos = common_tokenize(ctx, pos, add_bos, true);
+ tokens_neg = common_tokenize(ctx, neg, add_bos, true);
max_seq_len = std::max(tokens_pos.size(), tokens_neg.size());
padding_seq(ctx, tokens_pos, max_seq_len);
padding_seq(ctx, tokens_neg, max_seq_len);
void padding_seq(llama_context * ctx, std::vector<llama_token> & tokens, size_t len) {
// TODO: customize padding token
- std::vector<llama_token> pad_tokens = ::llama_tokenize(ctx, " ", false);
+ std::vector<llama_token> pad_tokens = common_tokenize(ctx, " ", false);
llama_token pad_tok = pad_tokens.back();
while (tokens.size() < len) {
tokens.push_back(pad_tok);
* Load prompt files and completion file.
* Then format each pair of prompt + completion to make an entry.
*/
-static int prepare_entries(gpt_params & params, train_context & ctx_train) {
+static int prepare_entries(common_params & params, train_context & ctx_train) {
// load prompts
std::vector<std::string> positive_prompts = ctrlvec_load_prompt_file(params.cvector_positive_file, true);
std::vector<std::string> negative_prompts = ctrlvec_load_prompt_file(params.cvector_negative_file, true);
}
int main(int argc, char ** argv) {
- gpt_params params;
+ common_params params;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_CVECTOR_GENERATOR, print_usage)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_CVECTOR_GENERATOR, print_usage)) {
return 1;
}
llama_numa_init(params.numa);
// load the model to get hparams
- llama_init_result llama_init = llama_init_from_gpt_params(params);
+ common_init_result llama_init = common_init_from_params(params);
llama_model * model = llama_init.model;
llama_context * ctx = llama_init.context;
static void batch_add_seq(llama_batch & batch, const std::vector<int32_t> & tokens, llama_seq_id seq_id) {
size_t n_tokens = tokens.size();
for (size_t i = 0; i < n_tokens; i++) {
- llama_batch_add(batch, tokens[i], i, { seq_id }, true);
+ common_batch_add(batch, tokens[i], i, { seq_id }, true);
}
}
}
float * out = output + embd_pos * n_embd;
- llama_embd_normalize(embd, out, n_embd, embd_norm);
+ common_embd_normalize(embd, out, n_embd, embd_norm);
}
}
int main(int argc, char ** argv) {
- gpt_params params;
+ common_params params;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_EMBEDDING)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_EMBEDDING)) {
return 1;
}
- gpt_init();
+ common_init();
params.embedding = true;
// For non-causal models, batch size must be equal to ubatch size
llama_numa_init(params.numa);
// load the model
- llama_init_result llama_init = llama_init_from_gpt_params(params);
+ common_init_result llama_init = common_init_from_params(params);
llama_model * model = llama_init.model;
llama_context * ctx = llama_init.context;
// print system information
{
LOG_INF("\n");
- LOG_INF("%s\n", gpt_params_get_system_info(params).c_str());
+ LOG_INF("%s\n", common_params_get_system_info(params).c_str());
}
// split the prompt into lines
// tokenize the prompts and trim
std::vector<std::vector<int32_t>> inputs;
for (const auto & prompt : prompts) {
- auto inp = ::llama_tokenize(ctx, prompt, true, true);
+ auto inp = common_tokenize(ctx, prompt, true, true);
if (inp.size() > n_batch) {
LOG_ERR("%s: number of tokens in input line (%lld) exceeds batch size (%lld), increase batch size and re-run\n",
__func__, (long long int) inp.size(), (long long int) n_batch);
LOG_INF("%s: prompt %d: '%s'\n", __func__, i, prompts[i].c_str());
LOG_INF("%s: number of tokens in prompt = %zu\n", __func__, inputs[i].size());
for (int j = 0; j < (int) inputs[i].size(); j++) {
- LOG("%6d -> '%s'\n", inputs[i][j], llama_token_to_piece(ctx, inputs[i][j]).c_str());
+ LOG("%6d -> '%s'\n", inputs[i][j], common_token_to_piece(ctx, inputs[i][j]).c_str());
}
LOG("\n\n");
}
batch_decode(ctx, batch, out, s, n_embd, params.embd_normalize);
e += pooling_type == LLAMA_POOLING_TYPE_NONE ? batch.n_tokens : s;
s = 0;
- llama_batch_clear(batch);
+ common_batch_clear(batch);
}
// add to batch
LOG("\n");
for (int i = 0; i < n_prompts; i++) {
for (int j = 0; j < n_prompts; j++) {
- float sim = llama_embd_similarity_cos(emb + i * n_embd, emb + j * n_embd, n_embd);
+ float sim = common_embd_similarity_cos(emb + i * n_embd, emb + j * n_embd, n_embd);
LOG("%6.2f ", sim);
}
LOG("%1.10s", prompts[i].c_str());
for (int i = 0;;) { // at least two iteration (n_embd_count > 1)
LOG(" [");
for (int j = 0;;) { // at least two iteration (n_embd_count > 1)
- float sim = llama_embd_similarity_cos(emb + i * n_embd, emb + j * n_embd, n_embd);
+ float sim = common_embd_similarity_cos(emb + i * n_embd, emb + j * n_embd, n_embd);
LOG("%6.2f", sim);
j++;
if (j < n_embd_count) LOG(", "); else break;
return true;
}
-static bool run(llama_context * ctx, const gpt_params & params) {
+static bool run(llama_context * ctx, const common_params & params) {
const bool add_bos = llama_add_bos_token(llama_get_model(ctx));
- std::vector<llama_token> tokens = ::llama_tokenize(ctx, params.prompt, add_bos);
+ std::vector<llama_token> tokens = common_tokenize(ctx, params.prompt, add_bos);
if (llama_decode(ctx, llama_batch_get_one(tokens.data(), tokens.size(), 0, 0))) {
LOG_ERR("%s : failed to eval\n", __func__);
int main(int argc, char ** argv) {
callback_data cb_data;
- gpt_params params;
+ common_params params;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON)) {
return 1;
}
- gpt_init();
+ common_init();
llama_backend_init();
llama_numa_init(params.numa);
params.warmup = false;
// init
- llama_init_result llama_init = llama_init_from_gpt_params(params);
+ common_init_result llama_init = common_init_from_params(params);
llama_model * model = llama_init.model;
llama_context * ctx = llama_init.context;
// print system information
{
LOG_INF("\n");
- LOG_INF("%s\n", gpt_params_get_system_info(params).c_str());
+ LOG_INF("%s\n", common_params_get_system_info(params).c_str());
LOG_INF("\n");
}
lora_merge_ctx(
std::string & base_fname,
- std::vector<llama_lora_adapter_info> & lora_files,
+ std::vector<common_lora_adapter_info> & lora_files,
std::string & outfile,
int n_threads) : base_model(base_fname, 0), n_threads(n_threads), fout(outfile, std::ios::binary) {
fout.exceptions(std::ofstream::failbit); // fail fast on write errors
}
int main(int argc, char ** argv) {
- gpt_params params;
+ common_params params;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_EXPORT_LORA, print_usage)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_EXPORT_LORA, print_usage)) {
return 1;
}
file << "| -------- | ----------- |\n";
}
-static void write_table_entry(std::ofstream & file, const llama_arg & opt) {
+static void write_table_entry(std::ofstream & file, const common_arg & opt) {
file << "| `";
// args
for (const auto & arg : opt.args) {
file << "` | " << md_help << " |\n";
}
-static void write_table(std::ofstream & file, std::vector<llama_arg *> & opts) {
+static void write_table(std::ofstream & file, std::vector<common_arg *> & opts) {
write_table_header(file);
for (const auto & opt : opts) {
write_table_entry(file, *opt);
static void export_md(std::string fname, llama_example ex) {
std::ofstream file(fname, std::ofstream::out | std::ofstream::trunc);
- gpt_params params;
- auto ctx_arg = gpt_params_parser_init(params, ex);
+ common_params params;
+ auto ctx_arg = common_params_parser_init(params, ex);
- std::vector<llama_arg *> common_options;
- std::vector<llama_arg *> sparam_options;
- std::vector<llama_arg *> specific_options;
+ std::vector<common_arg *> common_options;
+ std::vector<common_arg *> sparam_options;
+ std::vector<common_arg *> specific_options;
for (auto & opt : ctx_arg.options) {
// in case multiple LLAMA_EXAMPLE_* are set, we prioritize the LLAMA_EXAMPLE_* matching current example
if (opt.is_sparam) {
llama_batch batch = llama_batch_init(llama_n_batch(ctx), 0, 1);
for (uint64_t i = 0; i < sentences.size(); i++) {
- llama_batch_clear(batch);
+ common_batch_clear(batch);
const std::string input_string = instruction + sentences[i];
- std::vector<llama_token> inputs = llama_tokenize(model, input_string, true, false);
+ std::vector<llama_token> inputs = common_tokenize(model, input_string, true, false);
const int32_t n_toks = inputs.size();
// inputs.push_back(llama_token_eos(model));
// we want to ignore instruction tokens for mean pooling
- const int32_t n_inst = llama_tokenize(model, instruction, true, false).size();
+ const int32_t n_inst = common_tokenize(model, instruction, true, false).size();
#ifdef GRIT_DEBUG
// debug tokens - should be matching as referenced in the GritLM sample
// add input to batch (this increments n_tokens)
for (int32_t j = 0; j < n_toks; j++) {
- llama_batch_add(batch, inputs[j], j, { 0 }, j >= n_inst);
+ common_batch_add(batch, inputs[j], j, { 0 }, j >= n_inst);
}
// clear previous kv_cache values (irrelevant for embeddings)
}
std::vector<float> emb_norm(emb_unorm.size());
- llama_embd_normalize(emb_unorm.data(), emb_norm.data(), n_embd);
+ common_embd_normalize(emb_unorm.data(), emb_norm.data(), n_embd);
result.push_back(emb_norm);
#ifdef GRIT_DEBUG
llama_batch bat = llama_batch_init(llama_n_batch(ctx), 0, 1);
- std::vector<llama_token> inputs = llama_tokenize(model, prompt, false, true);
+ std::vector<llama_token> inputs = common_tokenize(model, prompt, false, true);
int32_t i_current_token = 0;
while (true) {
- llama_batch_clear(bat);
+ common_batch_clear(bat);
{
const int32_t n_inputs = inputs.size();
for (int32_t i = 0; i < n_inputs; i++) {
- llama_batch_add(bat, inputs[i], i_current_token++, { 0 }, i == n_inputs - 1);
+ common_batch_add(bat, inputs[i], i_current_token++, { 0 }, i == n_inputs - 1);
}
}
inputs.clear();
break;
}
- std::string piece = llama_token_to_piece(ctx, token);
+ std::string piece = common_token_to_piece(ctx, token);
if (stream) {
std::printf("%s", piece.c_str());
std::fflush(stdout);
}
int main(int argc, char * argv[]) {
- gpt_params params;
+ common_params params;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON)) {
return 1;
}
- gpt_init();
+ common_init();
- llama_model_params mparams = llama_model_params_from_gpt_params(params);
- llama_context_params cparams = llama_context_params_from_gpt_params(params);
+ llama_model_params mparams = common_model_params_to_llama(params);
+ llama_context_params cparams = common_context_params_to_llama(params);
llama_backend_init();
const int n_embd = llama_n_embd(model);
- const float cosine_sim_q0_d0 = llama_embd_similarity_cos(q_rep[0].data(), d_rep[0].data(), n_embd);
- const float cosine_sim_q0_d1 = llama_embd_similarity_cos(q_rep[0].data(), d_rep[1].data(), n_embd);
- const float cosine_sim_q1_d0 = llama_embd_similarity_cos(q_rep[1].data(), d_rep[0].data(), n_embd);
- const float cosine_sim_q1_d1 = llama_embd_similarity_cos(q_rep[1].data(), d_rep[1].data(), n_embd);
+ const float cosine_sim_q0_d0 = common_embd_similarity_cos(q_rep[0].data(), d_rep[0].data(), n_embd);
+ const float cosine_sim_q0_d1 = common_embd_similarity_cos(q_rep[0].data(), d_rep[1].data(), n_embd);
+ const float cosine_sim_q1_d0 = common_embd_similarity_cos(q_rep[1].data(), d_rep[0].data(), n_embd);
+ const float cosine_sim_q1_d1 = common_embd_similarity_cos(q_rep[1].data(), d_rep[1].data(), n_embd);
std::printf("Cosine similarity between \"%.50s\" and \"%.50s\" is: %.3f\n", queries[0].c_str(), documents[0].c_str(), cosine_sim_q0_d0);
std::printf("Cosine similarity between \"%.50s\" and \"%.50s\" is: %.3f\n", queries[0].c_str(), documents[1].c_str(), cosine_sim_q0_d1);
class IMatrixCollector {
public:
IMatrixCollector() = default;
- void set_params(gpt_params params) { m_params = std::move(params); }
+ void set_params(common_params params) { m_params = std::move(params); }
bool collect_imatrix(struct ggml_tensor * t, bool ask, void * user_data);
void save_imatrix(int ncall = -1) const;
bool load_imatrix(const char * file_name);
private:
std::unordered_map<std::string, Stats> m_stats;
- gpt_params m_params;
+ common_params m_params;
std::mutex m_mutex;
int m_last_call = 0;
std::vector<float> m_src1_data;
}
}
-static bool compute_imatrix(llama_context * ctx, const gpt_params & params) {
+static bool compute_imatrix(llama_context * ctx, const common_params & params) {
const bool add_bos = llama_add_bos_token(llama_get_model(ctx));
GGML_ASSERT(!llama_add_eos_token(llama_get_model(ctx)));
const int n_ctx = llama_n_ctx(ctx);
auto tim1 = std::chrono::high_resolution_clock::now();
LOG_INF("%s: tokenizing the input ..\n", __func__);
- std::vector<llama_token> tokens = ::llama_tokenize(ctx, params.prompt, true);
+ std::vector<llama_token> tokens = common_tokenize(ctx, params.prompt, true);
auto tim2 = std::chrono::high_resolution_clock::now();
LOG_INF("%s: tokenization took %g ms\n",__func__,1e-3*std::chrono::duration_cast<std::chrono::microseconds>(tim2-tim1).count());
}
int main(int argc, char ** argv) {
- gpt_params params;
+ common_params params;
params.n_ctx = 512;
params.logits_all = true;
params.escape = false;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_IMATRIX, print_usage)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_IMATRIX, print_usage)) {
return 1;
}
- gpt_init();
+ common_init();
params.n_batch = std::min(params.n_batch, params.n_ctx);
params.warmup = false;
// init
- llama_init_result llama_init = llama_init_from_gpt_params(params);
+ common_init_result llama_init = common_init_from_params(params);
llama_model * model = llama_init.model;
llama_context * ctx = llama_init.context;
// print system information
{
LOG_INF("\n");
- LOG_INF("%s\n", gpt_params_get_system_info(params).c_str());
+ LOG_INF("%s\n", common_params_get_system_info(params).c_str());
}
if (!compute_imatrix(ctx, params)) {
static llama_context ** g_ctx;
static llama_model ** g_model;
-static gpt_sampler ** g_smpl;
-static gpt_params * g_params;
+static common_sampler ** g_smpl;
+static common_params * g_params;
static std::vector<llama_token> * g_input_tokens;
static std::ostringstream * g_output_ss;
static std::vector<llama_token> * g_output_tokens;
static bool is_interacting = false;
static void write_logfile(
- const llama_context * ctx, const gpt_params & params, const llama_model * model,
+ const llama_context * ctx, const common_params & params, const llama_model * model,
const std::vector<llama_token> & input_tokens, const std::string & output,
const std::vector<llama_token> & output_tokens
) {
} else {
console::cleanup();
LOG("\n");
- gpt_perf_print(*g_ctx, *g_smpl);
+ common_perf_print(*g_ctx, *g_smpl);
write_logfile(*g_ctx, *g_params, *g_model, *g_input_tokens, g_output_ss->str(), *g_output_tokens);
// make sure all logs are flushed
LOG("Interrupted by user\n");
- gpt_log_pause(gpt_log_main());
+ common_log_pause(common_log_main());
_exit(130);
}
#endif
int main(int argc, char ** argv) {
- gpt_params params;
+ common_params params;
g_params = ¶ms;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_INFILL)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_INFILL)) {
return 1;
}
- gpt_init();
+ common_init();
auto & sparams = params.sparams;
llama_model * model = nullptr;
llama_context * ctx = nullptr;
- gpt_sampler * smpl = nullptr;
+ common_sampler * smpl = nullptr;
g_model = &model;
g_ctx = &ctx;
// load the model and apply lora adapter, if any
LOG_INF("%s: load the model and apply lora adapter, if any\n", __func__);
- llama_init_result llama_init = llama_init_from_gpt_params(params);
+ common_init_result llama_init = common_init_from_params(params);
model = llama_init.model;
ctx = llama_init.context;
// print system information
{
LOG_INF("\n");
- LOG_INF("%s\n", gpt_params_get_system_info(params).c_str());
+ LOG_INF("%s\n", common_params_get_system_info(params).c_str());
}
const bool add_bos = llama_add_bos_token(model);
GGML_ASSERT(!llama_add_eos_token(model));
std::vector<llama_token> embd_inp;
std::vector<llama_token> embd_end;
- std::vector<llama_token> inp_pfx = ::llama_tokenize(ctx, params.input_prefix, false);
- std::vector<llama_token> inp_sfx = ::llama_tokenize(ctx, params.input_suffix, false);
+ std::vector<llama_token> inp_pfx = common_tokenize(ctx, params.input_prefix, false);
+ std::vector<llama_token> inp_sfx = common_tokenize(ctx, params.input_suffix, false);
GGML_ASSERT(llama_token_prefix(model) >= 0);
GGML_ASSERT(llama_token_suffix(model) >= 0);
LOG_INF("%s: prompt: '%s'\n", __func__, params.prompt.c_str());
LOG_INF("%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size());
for (int i = 0; i < (int) embd_inp.size(); i++) {
- LOG_INF("%6d -> '%s'\n", embd_inp[i], llama_token_to_piece(ctx, embd_inp[i]).c_str());
+ LOG_INF("%6d -> '%s'\n", embd_inp[i], common_token_to_piece(ctx, embd_inp[i]).c_str());
}
if (params.n_keep > 0) {
LOG_INF("%s: static prompt based on n_keep: '", __func__);
for (int i = 0; i < params.n_keep; i++) {
- LOG_CNT("%s", llama_token_to_piece(ctx, embd_inp[i]).c_str());
+ LOG_CNT("%s", common_token_to_piece(ctx, embd_inp[i]).c_str());
}
LOG_CNT("'\n");
}
LOG_INF("Input suffix: '%s'\n", params.input_suffix.c_str());
}
}
- smpl = gpt_sampler_init(model, sparams);
+ smpl = common_sampler_init(model, sparams);
- LOG_INF("sampler seed: %u\n", gpt_sampler_get_seed(smpl));
+ LOG_INF("sampler seed: %u\n", common_sampler_get_seed(smpl));
LOG_INF("sampler params: \n%s\n", sparams.print().c_str());
- LOG_INF("sampler chain: %s\n", gpt_sampler_print(smpl).c_str());
+ LOG_INF("sampler chain: %s\n", common_sampler_print(smpl).c_str());
LOG_INF("generate: n_ctx = %d, n_batch = %d, n_predict = %d, n_keep = %d\n", n_ctx, params.n_batch, params.n_predict, params.n_keep);
embd.clear();
if ((int) embd_inp.size() <= n_consumed && !is_interacting) {
- const llama_token id = gpt_sampler_sample(smpl, ctx, -1);
+ const llama_token id = common_sampler_sample(smpl, ctx, -1);
- gpt_sampler_accept(smpl, id, true);
+ common_sampler_accept(smpl, id, true);
// LOG_DBG("last: %s\n", string_from(ctx, smpl->prev.to_vector()).c_str());
// push the prompt in the sampling context in order to apply repetition penalties later
// for the prompt, we don't apply grammar rules
- gpt_sampler_accept(smpl, embd_inp[n_consumed], false);
+ common_sampler_accept(smpl, embd_inp[n_consumed], false);
++n_consumed;
if ((int) embd.size() >= params.n_batch) {
// display text
if (input_echo) {
for (auto id : embd) {
- const std::string token_str = llama_token_to_piece(ctx, id);
+ const std::string token_str = common_token_to_piece(ctx, id);
LOG("%s", token_str.c_str());
if (embd.size() > 1) {
// if not currently processing queued inputs;
if ((int) embd_inp.size() <= n_consumed) {
// deal with eot token in infill mode
- if ((gpt_sampler_last(smpl) == llama_token_eot(model) || is_interacting) && params.interactive){
+ if ((common_sampler_last(smpl) == llama_token_eot(model) || is_interacting) && params.interactive){
if (is_interacting && !params.interactive_first) {
// print an eot token
- LOG("%s", llama_token_to_piece(ctx, llama_token_eot(model)).c_str());
+ LOG("%s", common_token_to_piece(ctx, llama_token_eot(model)).c_str());
}
LOG("\n");
console::set_display(console::user_input);
}
// tokenize new prefix and suffix
- std::vector<llama_token> inp_pfx = ::llama_tokenize(ctx, params.input_prefix, false);
- std::vector<llama_token> inp_sfx = ::llama_tokenize(ctx, params.input_suffix, false);
+ std::vector<llama_token> inp_pfx = common_tokenize(ctx, params.input_prefix, false);
+ std::vector<llama_token> inp_sfx = common_tokenize(ctx, params.input_suffix, false);
inp_pfx.insert(inp_pfx.begin(), llama_token_prefix(model));
inp_sfx.insert(inp_sfx.begin(), llama_token_suffix(model));
is_interacting = false;
}
// deal with end of generation tokens in interactive mode
- else if (llama_token_is_eog(model, gpt_sampler_last(smpl))) {
+ else if (llama_token_is_eog(model, common_sampler_last(smpl))) {
LOG_DBG("found EOS token\n");
if (params.interactive) {
const size_t original_size = embd_inp.size();
- const auto line_inp = ::llama_tokenize(ctx, buffer, false);
+ const auto line_inp = common_tokenize(ctx, buffer, false);
LOG_DBG("input tokens: %s\n", string_from(ctx, line_inp).c_str());
embd_inp.insert(embd_inp.end(), line_inp.begin(), line_inp.end());
for (size_t i = original_size; i < embd_inp.size(); ++i) {
const llama_token token = embd_inp[i];
output_tokens.push_back(token);
- output_ss << llama_token_to_piece(ctx, token);
+ output_ss << common_token_to_piece(ctx, token);
}
n_remain -= line_inp.size();
if (n_past > 0) {
if (is_interacting) {
- gpt_sampler_reset(smpl);
+ common_sampler_reset(smpl);
}
is_interacting = false;
}
}
}
if (!params.interactive && n_remain <= 0) {
- LOG("%s", llama_token_to_piece(ctx, llama_token_eot(model)).c_str());
+ LOG("%s", common_token_to_piece(ctx, llama_token_eot(model)).c_str());
}
LOG("\n");
- gpt_perf_print(ctx, smpl);
+ common_perf_print(ctx, smpl);
write_logfile(ctx, params, model, input_tokens, output_ss.str(), output_tokens);
llama_free(ctx);
llama_free_model(model);
- gpt_sampler_free(smpl);
+ common_sampler_free(smpl);
llama_backend_free();
return 0;
for (nri = 0; nri < nr; nri++) {
LOGi("Benchmark prompt processing (pp)");
- llama_batch_clear(*batch);
+ common_batch_clear(*batch);
const int n_tokens = pp;
for (i = 0; i < n_tokens; i++) {
- llama_batch_add(*batch, 0, i, { 0 }, false);
+ common_batch_add(*batch, 0, i, { 0 }, false);
}
batch->logits[batch->n_tokens - 1] = true;
const auto t_tg_start = ggml_time_us();
for (i = 0; i < tg; i++) {
- llama_batch_clear(*batch);
+ common_batch_clear(*batch);
for (j = 0; j < pl; j++) {
- llama_batch_add(*batch, 0, i, { j }, true);
+ common_batch_add(*batch, 0, i, { j }, true);
}
LOGi("llama_decode() text generation: %d", i);
const auto context = reinterpret_cast<llama_context *>(context_pointer);
const auto batch = reinterpret_cast<llama_batch *>(batch_pointer);
- const auto tokens_list = llama_tokenize(context, text, 1);
+ const auto tokens_list = common_tokenize(context, text, 1);
auto n_ctx = llama_n_ctx(context);
auto n_kv_req = tokens_list.size() + (n_len - tokens_list.size());
}
for (auto id : tokens_list) {
- LOGi("%s", llama_token_to_piece(context, id).c_str());
+ LOGi("%s", common_token_to_piece(context, id).c_str());
}
- llama_batch_clear(*batch);
+ common_batch_clear(*batch);
// evaluate the initial prompt
for (auto i = 0; i < tokens_list.size(); i++) {
- llama_batch_add(*batch, tokens_list[i], i, { 0 }, false);
+ common_batch_add(*batch, tokens_list[i], i, { 0 }, false);
}
// llama_decode will output logits only for the last token of the prompt
return nullptr;
}
- auto new_token_chars = llama_token_to_piece(context, new_token_id);
+ auto new_token_chars = common_token_to_piece(context, new_token_id);
cached_token_chars += new_token_chars;
jstring new_token = nullptr;
new_token = env->NewStringUTF("");
}
- llama_batch_clear(*batch);
- llama_batch_add(*batch, new_token_id, n_cur, { 0 }, true);
+ common_batch_clear(*batch);
+ common_batch_add(*batch, new_token_id, n_cur, { 0 }, true);
env->CallVoidMethod(intvar_ncur, la_int_var_inc);
static bool eval_string(struct llama_context * ctx_llama, const char* str, int n_batch, int * n_past, bool add_bos){
std::string str2 = str;
- std::vector<llama_token> embd_inp = ::llama_tokenize(ctx_llama, str2, add_bos, true);
+ std::vector<llama_token> embd_inp = common_tokenize(ctx_llama, str2, add_bos, true);
eval_tokens(ctx_llama, embd_inp, n_batch, n_past);
return true;
}
-static const char * sample(struct gpt_sampler * smpl,
+static const char * sample(struct common_sampler * smpl,
struct llama_context * ctx_llama,
int * n_past) {
- const llama_token id = gpt_sampler_sample(smpl, ctx_llama, -1);
- gpt_sampler_accept(smpl, id, true);
+ const llama_token id = common_sampler_sample(smpl, ctx_llama, -1);
+ common_sampler_accept(smpl, id, true);
static std::string ret;
if (llama_token_is_eog(llama_get_model(ctx_llama), id)) {
ret = "</s>";
} else {
- ret = llama_token_to_piece(ctx_llama, id);
+ ret = common_token_to_piece(ctx_llama, id);
}
eval_id(ctx_llama, id, n_past);
return ret.c_str();
LOG("\n note: a lower temperature value like 0.1 is recommended for better quality.\n");
}
-static struct llava_image_embed * load_image(llava_context * ctx_llava, gpt_params * params, const std::string & fname) {
+static struct llava_image_embed * load_image(llava_context * ctx_llava, common_params * params, const std::string & fname) {
// load and preprocess the image
llava_image_embed * embed = NULL;
return embed;
}
-static void process_prompt(struct llava_context * ctx_llava, struct llava_image_embed * image_embed, gpt_params * params, const std::string & prompt) {
+static void process_prompt(struct llava_context * ctx_llava, struct llava_image_embed * image_embed, common_params * params, const std::string & prompt) {
int n_past = 0;
const int max_tgt_len = params->n_predict < 0 ? 256 : params->n_predict;
user_prompt = prompt.substr(image_pos + std::string("<image>").length());
LOG_INF("system_prompt: %s\n", system_prompt.c_str());
if (params->verbose_prompt) {
- auto tmp = ::llama_tokenize(ctx_llava->ctx_llama, system_prompt, true, true);
+ auto tmp = common_tokenize(ctx_llava->ctx_llama, system_prompt, true, true);
for (int i = 0; i < (int) tmp.size(); i++) {
- LOG_INF("%6d -> '%s'\n", tmp[i], llama_token_to_piece(ctx_llava->ctx_llama, tmp[i]).c_str());
+ LOG_INF("%6d -> '%s'\n", tmp[i], common_token_to_piece(ctx_llava->ctx_llama, tmp[i]).c_str());
}
}
LOG_INF("user_prompt: %s\n", user_prompt.c_str());
if (params->verbose_prompt) {
- auto tmp = ::llama_tokenize(ctx_llava->ctx_llama, user_prompt, true, true);
+ auto tmp = common_tokenize(ctx_llava->ctx_llama, user_prompt, true, true);
for (int i = 0; i < (int) tmp.size(); i++) {
- LOG_INF("%6d -> '%s'\n", tmp[i], llama_token_to_piece(ctx_llava->ctx_llama, tmp[i]).c_str());
+ LOG_INF("%6d -> '%s'\n", tmp[i], common_token_to_piece(ctx_llava->ctx_llama, tmp[i]).c_str());
}
}
} else {
system_prompt = "A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.\nUSER:";
user_prompt = prompt + "\nASSISTANT:";
if (params->verbose_prompt) {
- auto tmp = ::llama_tokenize(ctx_llava->ctx_llama, user_prompt, true, true);
+ auto tmp = common_tokenize(ctx_llava->ctx_llama, user_prompt, true, true);
for (int i = 0; i < (int) tmp.size(); i++) {
- LOG_INF("%6d -> '%s'\n", tmp[i], llama_token_to_piece(ctx_llava->ctx_llama, tmp[i]).c_str());
+ LOG_INF("%6d -> '%s'\n", tmp[i], common_token_to_piece(ctx_llava->ctx_llama, tmp[i]).c_str());
}
}
}
LOG("\n");
- struct gpt_sampler * smpl = gpt_sampler_init(ctx_llava->model, params->sparams);
+ struct common_sampler * smpl = common_sampler_init(ctx_llava->model, params->sparams);
if (!smpl) {
LOG_ERR("%s: failed to initialize sampling subsystem\n", __func__);
exit(1);
fflush(stdout);
}
- gpt_sampler_free(smpl);
+ common_sampler_free(smpl);
LOG("\n");
}
-static struct llama_model * llava_init(gpt_params * params) {
+static struct llama_model * llava_init(common_params * params) {
llama_backend_init();
llama_numa_init(params->numa);
- llama_model_params model_params = llama_model_params_from_gpt_params(*params);
+ llama_model_params model_params = common_model_params_to_llama(*params);
llama_model * model = llama_load_model_from_file(params->model.c_str(), model_params);
if (model == NULL) {
return model;
}
-static struct llava_context * llava_init_context(gpt_params * params, llama_model * model) {
+static struct llava_context * llava_init_context(common_params * params, llama_model * model) {
const char * clip_path = params->mmproj.c_str();
auto prompt = params->prompt;
auto ctx_clip = clip_model_load(clip_path, /*verbosity=*/ 1);
- llama_context_params ctx_params = llama_context_params_from_gpt_params(*params);
+ llama_context_params ctx_params = common_context_params_to_llama(*params);
ctx_params.n_ctx = params->n_ctx < 2048 ? 2048 : params->n_ctx; // we need a longer context size to process image embeddings
llama_context * ctx_llama = llama_new_context_with_model(model, ctx_params);
int main(int argc, char ** argv) {
ggml_time_init();
- gpt_params params;
+ common_params params;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_LLAVA, print_usage)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_LLAVA, print_usage)) {
return 1;
}
- gpt_init();
+ common_init();
if (params.mmproj.empty() || (params.image.empty() && !prompt_contains_image(params.prompt))) {
print_usage(argc, argv);
LOG("\nnote: a lower temperature value like 0.1 is recommended for better quality.\n");
}
-static struct llama_model * llava_init(gpt_params * params) {
+static struct llama_model * llava_init(common_params * params) {
llama_backend_init();
llama_numa_init(params->numa);
- llama_model_params model_params = llama_model_params_from_gpt_params(*params);
+ llama_model_params model_params = common_model_params_to_llama(*params);
llama_model * model = llama_load_model_from_file(params->model.c_str(), model_params);
if (model == NULL) {
return model;
}
-static struct llava_context * llava_init_context(gpt_params * params, llama_model * model) {
+static struct llava_context * llava_init_context(common_params * params, llama_model * model) {
auto prompt = params->prompt;
if (prompt.empty()) {
prompt = "describe the image in detail.";
}
- llama_context_params ctx_params = llama_context_params_from_gpt_params(*params);
+ llama_context_params ctx_params = common_context_params_to_llama(*params);
if (params->n_ctx < 2048) {
// warn user here, "Image processing requires at least 2048 context, setting context to 2048"
LOG_WRN("%s: Image processing requires at least 2048 context, setting context to 2048\n" , __func__);
llama_backend_free();
}
-static struct clip_ctx * clip_init_context(gpt_params * params) {
+static struct clip_ctx * clip_init_context(common_params * params) {
const char * clip_path = params->mmproj.c_str();
auto prompt = params->prompt;
static bool eval_string(struct llama_context * ctx_llama, const char* str, int n_batch, int * n_past, bool add_bos){
std::string str2 = str;
- std::vector<llama_token> embd_inp = ::llama_tokenize(ctx_llama, str2, add_bos, true);
+ std::vector<llama_token> embd_inp = common_tokenize(ctx_llama, str2, add_bos, true);
return eval_tokens(ctx_llama, embd_inp, n_batch, n_past);
}
llava_image_embed_free(slice_embed);
}
-static void process_image(struct llava_context * ctx_llava, struct llava_image_embed * embeds, gpt_params * params, int &n_past) {
+static void process_image(struct llava_context * ctx_llava, struct llava_image_embed * embeds, common_params * params, int &n_past) {
std::string system_prompt;
int idx = 0;
int num_image_embeds = embeds->n_image_pos / clip_n_patches(ctx_llava->ctx_clip);
LOG_INF("%s: image token past: %d\n", __func__, n_past);
}
-static const char * sample(struct gpt_sampler * smpl,
+static const char * sample(struct common_sampler * smpl,
struct llama_context * ctx_llama,
int * n_past) {
- const llama_token id = gpt_sampler_sample(smpl, ctx_llama, -1);
- gpt_sampler_accept(smpl, id, true);
+ const llama_token id = common_sampler_sample(smpl, ctx_llama, -1);
+ common_sampler_accept(smpl, id, true);
static std::string ret;
if (llama_token_is_eog(llama_get_model(ctx_llama), id)) {
ret = "</s>";
} else {
- ret = llama_token_to_piece(ctx_llama, id);
+ ret = common_token_to_piece(ctx_llama, id);
}
eval_id(ctx_llama, id, n_past);
return ret.c_str();
}
-static struct llava_context * minicpmv_init(gpt_params * params, const std::string & fname, int &n_past){
+static struct llava_context * minicpmv_init(common_params * params, const std::string & fname, int &n_past){
auto * ctx_clip = clip_init_context(params);
auto * embeds = llava_image_embed_make_with_filename(ctx_clip, params->cpuparams.n_threads, fname.c_str());
if (!embeds) {
return ctx_llava;
}
-static struct gpt_sampler * llama_init(struct llava_context * ctx_llava, gpt_params * params, const std::string & prompt, int & n_past, bool is_first = false){
+static struct common_sampler * llama_init(struct llava_context * ctx_llava, common_params * params, const std::string & prompt, int & n_past, bool is_first = false){
std::string user_prompt = prompt;
int has_minicpmv_projector = clip_is_minicpmv(ctx_llava->ctx_clip);
if (!is_first) {
LOG_INF("\n");
- struct gpt_sampler * smpl = gpt_sampler_init(ctx_llava->model, params->sparams);
+ struct common_sampler * smpl = common_sampler_init(ctx_llava->model, params->sparams);
return smpl;
}
-static const char * llama_loop(struct llava_context * ctx_llava,struct gpt_sampler * smpl, int &n_past){
+static const char * llama_loop(struct llava_context * ctx_llava,struct common_sampler * smpl, int &n_past){
const char * tmp = sample(smpl, ctx_llava->ctx_llama, &n_past);
return tmp;
int main(int argc, char ** argv) {
ggml_time_init();
- gpt_params params;
+ common_params params;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_LLAVA, show_additional_info)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_LLAVA, show_additional_info)) {
return 1;
}
- gpt_init();
+ common_init();
if (params.mmproj.empty() || (params.image.empty())) {
show_additional_info(argc, argv);
fflush(stdout);
}
- gpt_sampler_free(smpl);
+ common_sampler_free(smpl);
}else {
while (true) {
LOG("<user>");
if (strstr(response.c_str(), "<user>")) break; // minicpm-v
fflush(stdout);
}
- gpt_sampler_free(smpl);
+ common_sampler_free(smpl);
}
}
printf("\n");
};
int main(int argc, char ** argv) {
- gpt_params params;
+ common_params params;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON)) {
return 1;
}
- gpt_init();
+ common_init();
const int W = 15; // lookahead window
const int N = 5; // n-gram size
llama_numa_init(params.numa);
// load the target model
- llama_init_result llama_init = llama_init_from_gpt_params(params);
+ common_init_result llama_init = common_init_from_params(params);
llama_model * model = llama_init.model;
llama_context * ctx = llama_init.context;
std::vector<llama_token> inp;
std::vector<llama_token> all;
- inp = ::llama_tokenize(ctx, params.prompt, true, true);
+ inp = common_tokenize(ctx, params.prompt, true, true);
all = inp;
const int max_context_size = llama_n_ctx(ctx);
LOG("\n\n");
for (auto id : inp) {
- LOG("%s", llama_token_to_piece(ctx, id).c_str());
+ LOG("%s", common_token_to_piece(ctx, id).c_str());
}
fflush(stderr);
llama_batch batch = llama_batch_init(params.n_ctx, 0, W + G + 1);
// target model sampling context
- struct gpt_sampler * smpl = gpt_sampler_init(model, params.sparams);
+ struct common_sampler * smpl = common_sampler_init(model, params.sparams);
// verification n-grams
std::vector<ngram_data> ngrams_cur(G);
// sample first token
{
- id = gpt_sampler_sample(smpl, ctx, 0);
+ id = common_sampler_sample(smpl, ctx, 0);
- gpt_sampler_accept(smpl, id, true);
+ common_sampler_accept(smpl, id, true);
{
- const std::string token_str = llama_token_to_piece(ctx, id);
+ const std::string token_str = common_token_to_piece(ctx, id);
LOG("%s", token_str.c_str());
fflush(stdout);
// debug
if (dump_kv_cache) {
llama_kv_cache_view_update(ctx, &kvc_view);
- llama_kv_cache_dump_view_seqs(kvc_view, 40);
+ common_kv_cache_dump_view_seqs(kvc_view, 40);
}
// build the mask from https://lmsys.org/blog/2023-11-21-lookahead-decoding/
// V V V V V V
// id
{
- llama_batch_clear(batch);
+ common_batch_clear(batch);
// current token - first token of the first level
- llama_batch_add(batch, id, n_past, seq_id_all, true);
+ common_batch_add(batch, id, n_past, seq_id_all, true);
// verification n-grams - queue this before the lookahead tokens for less KV cache fragmentation
{
ngrams_cur[g].tokens [j + 1] = t;
ngrams_cur[g].i_batch[j + 1] = batch.n_tokens;
- llama_batch_add(batch, t, n_past + j + 1, { W + 1 + g }, true);
+ common_batch_add(batch, t, n_past + j + 1, { W + 1 + g }, true);
}
}
}
seq_id_look[j] = i + j + 1;
}
- llama_batch_add(batch, tokens_j[0][i], n_past + i, seq_id_look, false);
+ common_batch_add(batch, tokens_j[0][i], n_past + i, seq_id_look, false);
}
// fill the rest of the levels
for (int j = 1; j < N - 1; j++) {
for (int i = 0; i < W; i++) {
- llama_batch_add(batch, tokens_j[j][i], n_past + j + i, { i + 1 }, j == N - 2);
+ common_batch_add(batch, tokens_j[j][i], n_past + j + i, { i + 1 }, j == N - 2);
}
}
}
}
// sample the next token
- id = gpt_sampler_sample(smpl, ctx, i_batch);
+ id = common_sampler_sample(smpl, ctx, i_batch);
- gpt_sampler_accept(smpl, id, true);
+ common_sampler_accept(smpl, id, true);
// print
{
- const std::string token_str = llama_token_to_piece(ctx, id);
+ const std::string token_str = common_token_to_piece(ctx, id);
if (v == 0) {
LOG("%s", token_str.c_str());
// print known n-grams starting with token id (debug)
if (0 && v == 0) {
if (ngrams_observed.cnt[id] > 0) {
- LOG("\n - %d n-grams starting with '%s'\n", ngrams_observed.cnt[id], llama_token_to_piece(ctx, id).c_str());
+ LOG("\n - %d n-grams starting with '%s'\n", ngrams_observed.cnt[id], common_token_to_piece(ctx, id).c_str());
}
for (int i = 0; i < ngrams_observed.cnt[id]; i++) {
const int idx = id*(N - 1)*G + i*(N - 1);
for (int j = 0; j < N - 1; j++) {
- const std::string token_str = llama_token_to_piece(ctx, ngrams_observed.tokens[idx + j]);
+ const std::string token_str = common_token_to_piece(ctx, ngrams_observed.tokens[idx + j]);
LOG("%s", token_str.c_str());
}
if (v == 0) {
// sample from the last level
for (int i = 0; i < W; i++) {
- tokens_j[N - 2][i] = gpt_sampler_sample(smpl, ctx, ngrams_cur.size()*(N-1) + W*(N - 2) + i);
+ tokens_j[N - 2][i] = common_sampler_sample(smpl, ctx, ngrams_cur.size()*(N-1) + W*(N - 2) + i);
}
} else {
for (int i = 0; i < W; i++) {
LOG_INF("n_accept = %d\n", n_accept);
LOG_INF("\n");
- gpt_perf_print(ctx, smpl);
+ common_perf_print(ctx, smpl);
- gpt_sampler_free(smpl);
+ common_sampler_free(smpl);
llama_kv_cache_view_free(&kvc_view);
#include <vector>
int main(int argc, char ** argv){
- gpt_params params;
+ common_params params;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_LOOKUP)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_LOOKUP)) {
return 1;
}
llama_numa_init(params.numa);
// load the model
- llama_init_result llama_init = llama_init_from_gpt_params(params);
+ common_init_result llama_init = common_init_from_params(params);
llama_model * model = llama_init.model;
llama_context * ctx = llama_init.context;
// tokenize the prompt
std::vector<llama_token> inp;
- inp = ::llama_tokenize(ctx, params.prompt, true, true);
+ inp = common_tokenize(ctx, params.prompt, true, true);
fprintf(stderr, "%s: tokenization done\n", __func__);
- llama_ngram_cache ngram_cache;
- llama_ngram_cache_update(ngram_cache, LLAMA_NGRAM_STATIC, LLAMA_NGRAM_STATIC, inp, inp.size(), true);
+ common_ngram_cache ngram_cache;
+ common_ngram_cache_update(ngram_cache, LLAMA_NGRAM_STATIC, LLAMA_NGRAM_STATIC, inp, inp.size(), true);
fprintf(stderr, "%s: hashing done, writing file to %s\n", __func__, params.lookup_cache_static.c_str());
- llama_ngram_cache_save(ngram_cache, params.lookup_cache_static);
+ common_ngram_cache_save(ngram_cache, params.lookup_cache_static);
return 0;
}
}
fprintf(stderr, "lookup-merge: loading file %s\n", args[0].c_str());
- llama_ngram_cache ngram_cache_merged = llama_ngram_cache_load(args[0]);
+ common_ngram_cache ngram_cache_merged = common_ngram_cache_load(args[0]);
for (size_t i = 1; i < args.size()-1; ++i) {
fprintf(stderr, "lookup-merge: loading file %s\n", args[i].c_str());
- llama_ngram_cache ngram_cache = llama_ngram_cache_load(args[i]);
+ common_ngram_cache ngram_cache = common_ngram_cache_load(args[i]);
- llama_ngram_cache_merge(ngram_cache_merged, ngram_cache);
+ common_ngram_cache_merge(ngram_cache_merged, ngram_cache);
}
fprintf(stderr, "lookup-merge: saving file %s\n", args.back().c_str());
- llama_ngram_cache_save(ngram_cache_merged, args.back());
+ common_ngram_cache_save(ngram_cache_merged, args.back());
}
#include <vector>
int main(int argc, char ** argv){
- gpt_params params;
+ common_params params;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_LOOKUP)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_LOOKUP)) {
return 1;
}
- gpt_init();
+ common_init();
const int n_draft = params.n_draft;
llama_numa_init(params.numa);
// load the model
- llama_init_result llama_init = llama_init_from_gpt_params(params);
+ common_init_result llama_init = common_init_from_params(params);
llama_model * model = llama_init.model;
llama_context * ctx = llama_init.context;
// tokenize the prompt
std::vector<llama_token> inp;
- inp = ::llama_tokenize(ctx, params.prompt, true, true);
+ inp = common_tokenize(ctx, params.prompt, true, true);
- llama_ngram_cache ngram_cache_context;
- llama_ngram_cache ngram_cache_dynamic;
- llama_ngram_cache ngram_cache_static;
+ common_ngram_cache ngram_cache_context;
+ common_ngram_cache ngram_cache_dynamic;
+ common_ngram_cache ngram_cache_static;
int64_t t_draft_flat_us = 0;
int64_t t_draft_us = 0;
if (!params.lookup_cache_static.empty()) {
try {
- ngram_cache_static = llama_ngram_cache_load(params.lookup_cache_static);
+ ngram_cache_static = common_ngram_cache_load(params.lookup_cache_static);
} catch (std::ifstream::failure const &) {
LOG_ERR("failed to open static lookup cache: %s", params.lookup_cache_static.c_str());
exit(1);
if (!params.lookup_cache_dynamic.empty()) {
try {
- ngram_cache_dynamic = llama_ngram_cache_load(params.lookup_cache_dynamic);
+ ngram_cache_dynamic = common_ngram_cache_load(params.lookup_cache_dynamic);
} catch (std::ifstream::failure const &) {} // if the file does not exist it will simply be created at the end of the program
}
{
const int64_t t_start_draft_us = ggml_time_us();
- llama_ngram_cache_draft(pseudo_output, draft, n_draft, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, ngram_cache_context, ngram_cache_dynamic, ngram_cache_static);
+ common_ngram_cache_draft(pseudo_output, draft, n_draft, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, ngram_cache_context, ngram_cache_dynamic, ngram_cache_static);
t_draft_us += ggml_time_us() - t_start_draft_us;
}
{
const int64_t t_start_draft_us = ggml_time_us();
- llama_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, pseudo_output, 1, false);
+ common_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, pseudo_output, 1, false);
t_draft_us += ggml_time_us() - t_start_draft_us;
}
}
pseudo_output.push_back(inp_slice[pseudo_output.size()]);
{
const int64_t t_start_draft_us = ggml_time_us();
- llama_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, pseudo_output, 1, false);
+ common_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, pseudo_output, 1, false);
t_draft_us += ggml_time_us() - t_start_draft_us;
}
}
}
// After each chunk, update the dynamic ngram cache with the context ngram cache:
- llama_ngram_cache_merge(ngram_cache_dynamic, ngram_cache_context);
+ common_ngram_cache_merge(ngram_cache_dynamic, ngram_cache_context);
ngram_cache_context.clear();
}
#include <vector>
int main(int argc, char ** argv){
- gpt_params params;
+ common_params params;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_LOOKUP)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_LOOKUP)) {
return 1;
}
- gpt_init();
+ common_init();
// max. number of additional tokens to draft if match is found
const int n_draft = params.n_draft;
llama_numa_init(params.numa);
// load the model
- llama_init_result llama_init = llama_init_from_gpt_params(params);
+ common_init_result llama_init = common_init_from_params(params);
llama_model * model = llama_init.model;
llama_context * ctx = llama_init.context;
// tokenize the prompt
std::vector<llama_token> inp;
- inp = ::llama_tokenize(ctx, params.prompt, true, true);
+ inp = common_tokenize(ctx, params.prompt, true, true);
- llama_ngram_cache ngram_cache_context;
- llama_ngram_cache ngram_cache_dynamic;
- llama_ngram_cache ngram_cache_static;
+ common_ngram_cache ngram_cache_context;
+ common_ngram_cache ngram_cache_dynamic;
+ common_ngram_cache ngram_cache_static;
int64_t t_draft_flat_us = 0;
int64_t t_draft_us = 0;
{
// Fill up context ngram cache with tokens from user input:
const int64_t t_start_draft_us = ggml_time_us();
- llama_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, inp.size(), false);
+ common_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, inp.size(), false);
if (!params.lookup_cache_static.empty()) {
try {
- ngram_cache_static = llama_ngram_cache_load(params.lookup_cache_static);
+ ngram_cache_static = common_ngram_cache_load(params.lookup_cache_static);
} catch (std::ifstream::failure const &) {
LOG_ERR("failed to open static lookup cache: %s", params.lookup_cache_static.c_str());
exit(1);
if (!params.lookup_cache_dynamic.empty()) {
try {
- ngram_cache_dynamic = llama_ngram_cache_load(params.lookup_cache_dynamic);
+ ngram_cache_dynamic = common_ngram_cache_load(params.lookup_cache_dynamic);
} catch (std::ifstream::failure const &) {} // if the file does not exist it will simply be created at the end of the program
}
LOG("\n\n");
for (auto id : inp) {
- LOG("%s", llama_token_to_piece(ctx, id).c_str());
+ LOG("%s", common_token_to_piece(ctx, id).c_str());
}
fflush(stderr);
bool has_eos = false;
- struct gpt_sampler * smpl = gpt_sampler_init(model, params.sparams);
+ struct common_sampler * smpl = common_sampler_init(model, params.sparams);
std::vector<llama_token> draft;
// debug
if (dump_kv_cache) {
llama_kv_cache_view_update(ctx, &kvc_view);
- llama_kv_cache_dump_view_seqs(kvc_view, 40);
+ common_kv_cache_dump_view_seqs(kvc_view, 40);
}
// print current draft sequence
int i_dft = 0;
while (true) {
// sample from the target model
- llama_token id = gpt_sampler_sample(smpl, ctx, i_dft);
+ llama_token id = common_sampler_sample(smpl, ctx, i_dft);
- gpt_sampler_accept(smpl, id, true);
+ common_sampler_accept(smpl, id, true);
- const std::string token_str = llama_token_to_piece(ctx, id);
+ const std::string token_str = common_token_to_piece(ctx, id);
if (!params.use_color) {
LOG("%s", token_str.c_str());
{
// Update context ngram cache with the newly accepted token:
const int64_t t_start_draft_us = ggml_time_us();
- llama_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, 1, false);
+ common_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, 1, false);
t_draft_us += ggml_time_us() - t_start_draft_us;
}
{
// Update context ngram cache with the newly accepted token:
const int64_t t_start_draft_us = ggml_time_us();
- llama_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, 1, false);
+ common_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, 1, false);
t_draft_us += ggml_time_us() - t_start_draft_us;
}
break;
// clean the cache of draft tokens that weren't accepted
llama_kv_cache_seq_rm(ctx, 0, n_past, -1);
- llama_batch_clear(batch_tgt);
- llama_batch_add(batch_tgt, draft[0], n_past, { 0 }, true);
+ common_batch_clear(batch_tgt);
+ common_batch_add(batch_tgt, draft[0], n_past, { 0 }, true);
// Draft already contains a single token sampled from the model:
GGML_ASSERT(draft.size() == 1);
GGML_ASSERT(draft[0] == inp.back());
const int64_t t_start_draft_us = ggml_time_us();
- llama_ngram_cache_draft(inp, draft, n_draft, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, ngram_cache_context, ngram_cache_dynamic, ngram_cache_static);
+ common_ngram_cache_draft(inp, draft, n_draft, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, ngram_cache_context, ngram_cache_dynamic, ngram_cache_static);
for (size_t i = 1; i < draft.size(); ++i) {
- llama_batch_add(batch_tgt, draft[i], n_past + i, { 0 }, true);
+ common_batch_add(batch_tgt, draft[i], n_past + i, { 0 }, true);
}
t_draft_us += ggml_time_us() - t_start_draft_us;
auto t_dec_end = ggml_time_us();
// Update dynamic ngram cache with context ngram cache and save it to disk:
- llama_ngram_cache_merge(ngram_cache_dynamic, ngram_cache_context);
- llama_ngram_cache_save(ngram_cache_dynamic, params.lookup_cache_dynamic);
+ common_ngram_cache_merge(ngram_cache_dynamic, ngram_cache_context);
+ common_ngram_cache_save(ngram_cache_dynamic, params.lookup_cache_dynamic);
LOG("\n\n");
LOG_INF("accept = %.3f%%\n", 100.0f * n_accept / n_drafted);
LOG_INF("\ntarget:\n\n");
- gpt_perf_print(ctx, smpl);
+ common_perf_print(ctx, smpl);
- gpt_sampler_free(smpl);
+ common_sampler_free(smpl);
llama_batch_free(batch_tgt);
static llama_context ** g_ctx;
static llama_model ** g_model;
-static gpt_sampler ** g_smpl;
-static gpt_params * g_params;
+static common_sampler ** g_smpl;
+static common_params * g_params;
static std::vector<llama_token> * g_input_tokens;
static std::ostringstream * g_output_ss;
static std::vector<llama_token> * g_output_tokens;
}
static void write_logfile(
- const llama_context * ctx, const gpt_params & params, const llama_model * model,
+ const llama_context * ctx, const common_params & params, const llama_model * model,
const std::vector<llama_token> & input_tokens, const std::string & output,
const std::vector<llama_token> & output_tokens
) {
} else {
console::cleanup();
LOG("\n");
- gpt_perf_print(*g_ctx, *g_smpl);
+ common_perf_print(*g_ctx, *g_smpl);
write_logfile(*g_ctx, *g_params, *g_model, *g_input_tokens, g_output_ss->str(), *g_output_tokens);
// make sure all logs are flushed
LOG("Interrupted by user\n");
- gpt_log_pause(gpt_log_main());
+ common_log_pause(common_log_main());
_exit(130);
}
}
#endif
-static std::string chat_add_and_format(struct llama_model * model, std::vector<llama_chat_msg> & chat_msgs, const std::string & role, const std::string & content) {
- llama_chat_msg new_msg{role, content};
- auto formatted = llama_chat_format_single(model, g_params->chat_template, chat_msgs, new_msg, role == "user");
+static std::string chat_add_and_format(struct llama_model * model, std::vector<common_chat_msg> & chat_msgs, const std::string & role, const std::string & content) {
+ common_chat_msg new_msg{role, content};
+ auto formatted = common_chat_format_single(model, g_params->chat_template, chat_msgs, new_msg, role == "user");
chat_msgs.push_back({role, content});
LOG_DBG("formatted: '%s'\n", formatted.c_str());
return formatted;
}
int main(int argc, char ** argv) {
- gpt_params params;
+ common_params params;
g_params = ¶ms;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_MAIN, print_usage)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_MAIN, print_usage)) {
return 1;
}
- gpt_init();
+ common_init();
auto & sparams = params.sparams;
llama_model * model = nullptr;
llama_context * ctx = nullptr;
- gpt_sampler * smpl = nullptr;
+ common_sampler * smpl = nullptr;
- std::vector<llama_chat_msg> chat_msgs;
+ std::vector<common_chat_msg> chat_msgs;
g_model = &model;
g_ctx = &ctx;
// load the model and apply lora adapter, if any
LOG_INF("%s: load the model and apply lora adapter, if any\n", __func__);
- llama_init_result llama_init = llama_init_from_gpt_params(params);
+ common_init_result llama_init = common_init_from_params(params);
model = llama_init.model;
ctx = llama_init.context;
// print chat template example in conversation mode
if (params.conversation) {
if (params.enable_chat_template) {
- LOG_INF("%s: chat template example:\n%s\n", __func__, llama_chat_format_example(model, params.chat_template).c_str());
+ LOG_INF("%s: chat template example:\n%s\n", __func__, common_chat_format_example(model, params.chat_template).c_str());
} else {
LOG_INF("%s: in-suffix/prefix is specified, chat template will be disabled\n", __func__);
}
// print system information
{
LOG_INF("\n");
- LOG_INF("%s\n", gpt_params_get_system_info(params).c_str());
+ LOG_INF("%s\n", common_params_get_system_info(params).c_str());
LOG_INF("\n");
}
: params.prompt;
if (params.interactive_first || !params.prompt.empty() || session_tokens.empty()) {
LOG_DBG("tokenize the prompt\n");
- embd_inp = ::llama_tokenize(ctx, prompt, true, true);
+ embd_inp = common_tokenize(ctx, prompt, true, true);
} else {
LOG_DBG("use session tokens\n");
embd_inp = session_tokens;
LOG_INF("%s: prompt: '%s'\n", __func__, params.prompt.c_str());
LOG_INF("%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size());
for (int i = 0; i < (int) embd_inp.size(); i++) {
- LOG_INF("%6d -> '%s'\n", embd_inp[i], llama_token_to_piece(ctx, embd_inp[i]).c_str());
+ LOG_INF("%6d -> '%s'\n", embd_inp[i], common_token_to_piece(ctx, embd_inp[i]).c_str());
}
if (params.n_keep > add_bos) {
LOG_INF("%s: static prompt based on n_keep: '", __func__);
for (int i = 0; i < params.n_keep; i++) {
- LOG_CNT("%s", llama_token_to_piece(ctx, embd_inp[i]).c_str());
+ LOG_CNT("%s", common_token_to_piece(ctx, embd_inp[i]).c_str());
}
LOG_CNT("'\n");
}
for (const auto & antiprompt : params.antiprompt) {
LOG_INF("Reverse prompt: '%s'\n", antiprompt.c_str());
if (params.verbose_prompt) {
- auto tmp = ::llama_tokenize(ctx, antiprompt, false, true);
+ auto tmp = common_tokenize(ctx, antiprompt, false, true);
for (int i = 0; i < (int) tmp.size(); i++) {
- LOG_INF("%6d -> '%s'\n", tmp[i], llama_token_to_piece(ctx, tmp[i]).c_str());
+ LOG_INF("%6d -> '%s'\n", tmp[i], common_token_to_piece(ctx, tmp[i]).c_str());
}
}
}
if (!params.input_prefix.empty()) {
LOG_INF("Input prefix: '%s'\n", params.input_prefix.c_str());
if (params.verbose_prompt) {
- auto tmp = ::llama_tokenize(ctx, params.input_prefix, true, true);
+ auto tmp = common_tokenize(ctx, params.input_prefix, true, true);
for (int i = 0; i < (int) tmp.size(); i++) {
- LOG_INF("%6d -> '%s'\n", tmp[i], llama_token_to_piece(ctx, tmp[i]).c_str());
+ LOG_INF("%6d -> '%s'\n", tmp[i], common_token_to_piece(ctx, tmp[i]).c_str());
}
}
}
if (!params.input_suffix.empty()) {
LOG_INF("Input suffix: '%s'\n", params.input_suffix.c_str());
if (params.verbose_prompt) {
- auto tmp = ::llama_tokenize(ctx, params.input_suffix, false, true);
+ auto tmp = common_tokenize(ctx, params.input_suffix, false, true);
for (int i = 0; i < (int) tmp.size(); i++) {
- LOG_INF("%6d -> '%s'\n", tmp[i], llama_token_to_piece(ctx, tmp[i]).c_str());
+ LOG_INF("%6d -> '%s'\n", tmp[i], common_token_to_piece(ctx, tmp[i]).c_str());
}
}
}
}
- smpl = gpt_sampler_init(model, sparams);
+ smpl = common_sampler_init(model, sparams);
if (!smpl) {
LOG_ERR("%s: failed to initialize sampling subsystem\n", __func__);
return 1;
}
- LOG_INF("sampler seed: %u\n", gpt_sampler_get_seed(smpl));
+ LOG_INF("sampler seed: %u\n", common_sampler_get_seed(smpl));
LOG_INF("sampler params: \n%s\n", sparams.print().c_str());
- LOG_INF("sampler chain: %s\n", gpt_sampler_print(smpl).c_str());
+ LOG_INF("sampler chain: %s\n", common_sampler_print(smpl).c_str());
LOG_INF("generate: n_ctx = %d, n_batch = %d, n_predict = %d, n_keep = %d\n", n_ctx, params.n_batch, params.n_predict, params.n_keep);
antiprompt_ids.reserve(params.antiprompt.size());
for (const std::string & antiprompt : params.antiprompt) {
- antiprompt_ids.emplace_back(::llama_tokenize(ctx, antiprompt, false, true));
+ antiprompt_ids.emplace_back(::common_tokenize(ctx, antiprompt, false, true));
}
if (llama_model_has_encoder(model)) {
LOG_DBG("saved session to %s\n", path_session.c_str());
}
- const llama_token id = gpt_sampler_sample(smpl, ctx, -1);
+ const llama_token id = common_sampler_sample(smpl, ctx, -1);
- gpt_sampler_accept(smpl, id, /* accept_grammar= */ true);
+ common_sampler_accept(smpl, id, /* accept_grammar= */ true);
// LOG_DBG("last: %s\n", string_from(ctx, smpl->prev.to_vector()).c_str());
// push the prompt in the sampling context in order to apply repetition penalties later
// for the prompt, we don't apply grammar rules
- gpt_sampler_accept(smpl, embd_inp[n_consumed], /* accept_grammar= */ false);
+ common_sampler_accept(smpl, embd_inp[n_consumed], /* accept_grammar= */ false);
++n_consumed;
if ((int) embd.size() >= params.n_batch) {
// display text
if (input_echo && display) {
for (auto id : embd) {
- const std::string token_str = llama_token_to_piece(ctx, id, params.special);
+ const std::string token_str = common_token_to_piece(ctx, id, params.special);
// Console/Stream Output
LOG("%s", token_str.c_str());
// check for reverse prompt in the last n_prev tokens
if (!params.antiprompt.empty()) {
const int n_prev = 32;
- const std::string last_output = gpt_sampler_prev_str(smpl, ctx, n_prev);
+ const std::string last_output = common_sampler_prev_str(smpl, ctx, n_prev);
is_antiprompt = false;
// Check if each of the reverse prompts appears at the end of the output.
}
// check for reverse prompt using special tokens
- llama_token last_token = gpt_sampler_last(smpl);
+ llama_token last_token = common_sampler_last(smpl);
for (std::vector<llama_token> ids : antiprompt_ids) {
if (ids.size() == 1 && last_token == ids[0]) {
if (params.interactive) {
}
// deal with end of generation tokens in interactive mode
- if (llama_token_is_eog(model, gpt_sampler_last(smpl))) {
+ if (llama_token_is_eog(model, common_sampler_last(smpl))) {
LOG_DBG("found an EOG token\n");
if (params.interactive) {
if (!params.antiprompt.empty()) {
// tokenize and inject first reverse prompt
- const auto first_antiprompt = ::llama_tokenize(ctx, params.antiprompt.front(), false, true);
+ const auto first_antiprompt = common_tokenize(ctx, params.antiprompt.front(), false, true);
embd_inp.insert(embd_inp.end(), first_antiprompt.begin(), first_antiprompt.end());
is_antiprompt = true;
}
// if current token is not EOG, we add it to current assistant message
if (params.conversation) {
- const auto id = gpt_sampler_last(smpl);
- assistant_ss << llama_token_to_piece(ctx, id, false);
+ const auto id = common_sampler_last(smpl);
+ assistant_ss << common_token_to_piece(ctx, id, false);
}
if (n_past > 0 && is_interacting) {
? chat_add_and_format(model, chat_msgs, "user", std::move(buffer))
: std::move(buffer);
// TODO: one inconvenient of current chat template implementation is that we can't distinguish between user input and special tokens (prefix/postfix)
- const auto line_pfx = ::llama_tokenize(ctx, params.input_prefix, false, true);
- const auto line_inp = ::llama_tokenize(ctx, user_inp, false, format_chat);
- const auto line_sfx = ::llama_tokenize(ctx, params.input_suffix, false, true);
+ const auto line_pfx = common_tokenize(ctx, params.input_prefix, false, true);
+ const auto line_inp = common_tokenize(ctx, user_inp, false, format_chat);
+ const auto line_sfx = common_tokenize(ctx, params.input_suffix, false, true);
LOG_DBG("input tokens: %s\n", string_from(ctx, line_inp).c_str());
for (size_t i = original_size; i < embd_inp.size(); ++i) {
const llama_token token = embd_inp[i];
output_tokens.push_back(token);
- output_ss << llama_token_to_piece(ctx, token);
+ output_ss << common_token_to_piece(ctx, token);
}
// reset assistant message
if (n_past > 0) {
if (is_interacting) {
- gpt_sampler_reset(smpl);
+ common_sampler_reset(smpl);
}
is_interacting = false;
}
}
LOG("\n\n");
- gpt_perf_print(ctx, smpl);
+ common_perf_print(ctx, smpl);
write_logfile(ctx, params, model, input_tokens, output_ss.str(), output_tokens);
- gpt_sampler_free(smpl);
+ common_sampler_free(smpl);
llama_free(ctx);
llama_free_model(model);
struct client {
~client() {
if (smpl) {
- gpt_sampler_free(smpl);
+ common_sampler_free(smpl);
}
}
std::string prompt;
std::string response;
- struct gpt_sampler * smpl = nullptr;
+ struct common_sampler * smpl = nullptr;
};
static void print_date_time() {
int main(int argc, char ** argv) {
srand(1234);
- gpt_params params;
+ common_params params;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_PARALLEL)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_PARALLEL)) {
return 1;
}
- gpt_init();
+ common_init();
// number of simultaneous "clients" to simulate
const int32_t n_clients = params.n_parallel;
llama_numa_init(params.numa);
// load the target model
- llama_init_result llama_init = llama_init_from_gpt_params(params);
+ common_init_result llama_init = common_init_from_params(params);
llama_model * model = llama_init.model;
llama_context * ctx = llama_init.context;
for (size_t i = 0; i < clients.size(); ++i) {
auto & client = clients[i];
client.id = i;
- client.smpl = gpt_sampler_init(model, params.sparams);
+ client.smpl = common_sampler_init(model, params.sparams);
}
std::vector<llama_token> tokens_system;
- tokens_system = ::llama_tokenize(ctx, k_system, true);
+ tokens_system = common_tokenize(ctx, k_system, true);
const int32_t n_tokens_system = tokens_system.size();
llama_seq_id g_seq_id = 0;
LOG_INF("%s: Evaluating the system prompt ...\n", __func__);
for (int32_t i = 0; i < n_tokens_system; ++i) {
- llama_batch_add(batch, tokens_system[i], i, { 0 }, false);
+ common_batch_add(batch, tokens_system[i], i, { 0 }, false);
}
if (llama_decode(ctx, batch) != 0) {
while (true) {
if (dump_kv_cache) {
llama_kv_cache_view_update(ctx, &kvc_view);
- llama_kv_cache_dump_view_seqs(kvc_view, 40);
+ common_kv_cache_dump_view_seqs(kvc_view, 40);
}
- llama_batch_clear(batch);
+ common_batch_clear(batch);
// decode any currently ongoing sequences
for (auto & client : clients) {
client.i_batch = batch.n_tokens;
- llama_batch_add(batch, client.sampled, n_tokens_system + client.n_prompt + client.n_decoded, { client.id + 1 }, true);
+ common_batch_add(batch, client.sampled, n_tokens_system + client.n_prompt + client.n_decoded, { client.id + 1 }, true);
client.n_decoded += 1;
}
client.prompt = client.input + "\nAssistant:";
client.response = "";
- gpt_sampler_reset(client.smpl);
+ common_sampler_reset(client.smpl);
// do not prepend BOS because we have a system prompt!
std::vector<llama_token> tokens_prompt;
- tokens_prompt = ::llama_tokenize(ctx, client.prompt, false);
+ tokens_prompt = common_tokenize(ctx, client.prompt, false);
for (size_t i = 0; i < tokens_prompt.size(); ++i) {
- llama_batch_add(batch, tokens_prompt[i], i + n_tokens_system, { client.id + 1 }, false);
+ common_batch_add(batch, tokens_prompt[i], i + n_tokens_system, { client.id + 1 }, false);
}
// extract the logits only for the last token
//printf("client %d, seq %d, token %d, pos %d, batch %d\n",
// client.id, client.seq_id, client.sampled, client.n_decoded, client.i_batch);
- const llama_token id = gpt_sampler_sample(client.smpl, ctx, client.i_batch - i);
+ const llama_token id = common_sampler_sample(client.smpl, ctx, client.i_batch - i);
- gpt_sampler_accept(client.smpl, id, true);
+ common_sampler_accept(client.smpl, id, true);
if (client.n_decoded == 1) {
// start measuring generation time after the first token to make sure all concurrent clients
client.t_start_gen = ggml_time_us();
}
- const std::string token_str = llama_token_to_piece(ctx, id);
+ const std::string token_str = common_token_to_piece(ctx, id);
client.response += token_str;
client.sampled = id;
}
int main(int argc, char ** argv) {
- gpt_params params;
+ common_params params;
params.n_junk = 250;
params.n_keep = 32;
params.i_pos = -1;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_PASSKEY, print_usage)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_PASSKEY, print_usage)) {
return 1;
}
- gpt_init();
+ common_init();
int n_junk = params.n_junk;
int n_keep = params.n_keep;
// initialize the model
- llama_model_params model_params = llama_model_params_from_gpt_params(params);
+ llama_model_params model_params = common_model_params_to_llama(params);
llama_model * model = llama_load_model_from_file(params.model.c_str(), model_params);
// initialize the context
- llama_context_params ctx_params = llama_context_params_from_gpt_params(params);
+ llama_context_params ctx_params = common_context_params_to_llama(params);
ctx_params.n_ctx = llama_n_ctx_train(model)*n_grp + n_keep;
// tokenize the prompt
std::vector<llama_token> tokens_list;
- tokens_list = ::llama_tokenize(ctx, params.prompt, true);
+ tokens_list = common_tokenize(ctx, params.prompt, true);
// tokenize the prefix and use it as a sink
- const int n_tokens_prefix = ::llama_tokenize(ctx, prompt_prefix, true).size();
+ const int n_tokens_prefix = common_tokenize(ctx, prompt_prefix, true).size();
const int n_tokens_all = tokens_list.size();
n_past = llama_kv_cache_seq_pos_max(ctx, 0) + 1;
}
- llama_batch_clear(batch);
+ common_batch_clear(batch);
for (int j = 0; j < n_batch && i + j < n_tokens_all; j++) {
- llama_batch_add(batch, tokens_list[i + j], n_past++, { 0 }, false);
+ common_batch_add(batch, tokens_list[i + j], n_past++, { 0 }, false);
}
if (i + n_batch >= n_tokens_all) {
n_past = llama_kv_cache_seq_pos_max(ctx, 0) + 1;
- llama_batch_clear(batch);
+ common_batch_clear(batch);
for (int j = 0; j < n_batch && i + j < n_tokens_all; j++) {
- llama_batch_add(batch, tokens_list[i + j], n_past++, { 0 }, false);
+ common_batch_add(batch, tokens_list[i + j], n_past++, { 0 }, false);
}
if (i + n_batch >= n_tokens_all) {
break;
}
- LOG("%s", llama_token_to_piece(ctx, new_token_id).c_str());
+ LOG("%s", common_token_to_piece(ctx, new_token_id).c_str());
n_decode += 1;
// prepare the next batch
- llama_batch_clear(batch);
+ common_batch_clear(batch);
// push this new token for next evaluation
- llama_batch_add(batch, new_token_id, n_past++, { 0 }, true);
+ common_batch_add(batch, new_token_id, n_past++, { 0 }, true);
}
n_cur += 1;
};
static void write_logfile(
- const llama_context * ctx, const gpt_params & params, const llama_model * model,
+ const llama_context * ctx, const common_params & params, const llama_model * model,
const struct results_perplexity & results
) {
if (params.logdir.empty()) {
}
}
-static results_perplexity perplexity_v2(llama_context * ctx, const gpt_params & params) {
+static results_perplexity perplexity_v2(llama_context * ctx, const common_params & params) {
// Download: https://huggingface.co/datasets/ggml-org/ci/resolve/main/wikitext-2-raw-v1.zip
// Run `./perplexity -m models/7B/ggml-model-q4_0.bin -f wiki.test.raw`
// Output: `perplexity: 13.5106 [114/114]`
LOG_INF("%s: tokenizing the input ..\n", __func__);
- std::vector<llama_token> tokens = ::llama_tokenize(ctx, params.prompt, true);
+ std::vector<llama_token> tokens = common_tokenize(ctx, params.prompt, true);
const int n_ctx = llama_n_ctx(ctx);
return {tokens, std::exp(nll / count), logit_history, prob_history};
}
-static results_perplexity perplexity(llama_context * ctx, const gpt_params & params, const int32_t n_ctx) {
+static results_perplexity perplexity(llama_context * ctx, const common_params & params, const int32_t n_ctx) {
if (params.ppl_stride > 0) {
return perplexity_v2(ctx, params);
}
auto tim1 = std::chrono::high_resolution_clock::now();
LOG_INF("%s: tokenizing the input ..\n", __func__);
- std::vector<llama_token> tokens = ::llama_tokenize(ctx, params.prompt, true);
+ std::vector<llama_token> tokens = common_tokenize(ctx, params.prompt, true);
auto tim2 = std::chrono::high_resolution_clock::now();
LOG_INF("%s: tokenization took %g ms\n",__func__,1e-3*std::chrono::duration_cast<std::chrono::microseconds>(tim2-tim1).count());
}
}
-static void hellaswag_score(llama_context * ctx, const gpt_params & params) {
+static void hellaswag_score(llama_context * ctx, const common_params & params) {
// Calculates hellaswag score (acc_norm) from prompt
//
// Data extracted from the HellaSwag validation dataset (MIT license) https://github.com/rowanz/hellaswag/blob/master/data/hellaswag_val.jsonl
hs_cur.gold_ending_idx = std::stoi( prompt_lines[idx*6+1] );
for (size_t j = 0; j < 4; j++) {
hs_cur.ending[j] = prompt_lines[idx*6+2+j];
- hs_cur.seq_tokens[j] = ::llama_tokenize(ctx, hs_cur.context + " " + hs_cur.ending[j], true);
+ hs_cur.seq_tokens[j] = common_tokenize(ctx, hs_cur.context + " " + hs_cur.ending[j], true);
}
// determine the common prefix of the endings
size_t i1 = i0;
size_t i_logits = 0; // this tells us how many logits were needed before this point in the batch
- llama_batch_clear(batch);
+ common_batch_clear(batch);
// batch as much tasks as possible into the available context
// each task has 4 unique sequence ids - one for each ending
}
for (size_t i = 0; i < hs_cur.common_prefix; ++i) {
- llama_batch_add(batch, hs_cur.seq_tokens[0][i], i, { s0 + 0, s0 + 1, s0 + 2, s0 + 3 }, false);
+ common_batch_add(batch, hs_cur.seq_tokens[0][i], i, { s0 + 0, s0 + 1, s0 + 2, s0 + 3 }, false);
}
batch.logits[batch.n_tokens - 1] = true; // we need logits for the last token of the common prefix
n_logits += 1;
// TODO: don't evaluate the last token of each sequence
for (size_t i = hs_cur.common_prefix; i < seq_tokens_size; ++i) {
const bool needs_logits = i < seq_tokens_size - 1;
- llama_batch_add(batch, hs_cur.seq_tokens[s][i], i, { s0 + s }, needs_logits);
+ common_batch_add(batch, hs_cur.seq_tokens[s][i], i, { s0 + s }, needs_logits);
n_logits += needs_logits;
}
}
* 0,Sarah was a much better surgeon than Maria so _ always got the easier cases.,Sarah,Maria,2
*
*/
-static void winogrande_score(llama_context * ctx, const gpt_params & params) {
+static void winogrande_score(llama_context * ctx, const common_params & params) {
constexpr int k_min_trailing_ctx = 3;
LOG_INF("%s : tokenizing selected tasks\n", __func__);
for (auto & task : data) {
- task.seq_tokens[0] = ::llama_tokenize(ctx, task.first + task.choices[0] + task.second, true);
- task.seq_tokens[1] = ::llama_tokenize(ctx, task.first + task.choices[1] + task.second, true);
+ task.seq_tokens[0] = common_tokenize(ctx, task.first + task.choices[0] + task.second, true);
+ task.seq_tokens[1] = common_tokenize(ctx, task.first + task.choices[1] + task.second, true);
task.common_prefix = 0;
for (size_t k = 0; k < task.seq_tokens[0].size(); k++) {
task.seq_tokens[0].size() - task.common_prefix +
task.seq_tokens[1].size() - task.common_prefix;
- task.n_base1 = ::llama_tokenize(ctx, task.first + task.choices[0], true).size();
- task.n_base2 = ::llama_tokenize(ctx, task.first + task.choices[1], true).size();
+ task.n_base1 = common_tokenize(ctx, task.first + task.choices[0], true).size();
+ task.n_base2 = common_tokenize(ctx, task.first + task.choices[1], true).size();
}
LOG_INF("%s : calculating winogrande score over selected tasks.\n", __func__);
size_t i1 = i0;
size_t i_logits = 0;
- llama_batch_clear(batch);
+ common_batch_clear(batch);
while (n_cur + (int) data[i1].required_tokens <= n_ctx) {
int n_logits = 0;
}
for (size_t i = 0; i < data[i1].common_prefix; ++i) {
- llama_batch_add(batch, data[i1].seq_tokens[0][i], i, { s0 + 0, s0 + 1 }, false);
+ common_batch_add(batch, data[i1].seq_tokens[0][i], i, { s0 + 0, s0 + 1 }, false);
}
batch.logits[batch.n_tokens - 1] = true;
n_logits += 1;
for (int s = 0; s < 2; ++s) {
// TODO: end before the last token, no need to predict past the end of the sequences
for (size_t i = data[i1].common_prefix; i < data[i1].seq_tokens[s].size(); ++i) {
- llama_batch_add(batch, data[i1].seq_tokens[s][i], i, { s0 + s }, true);
+ common_batch_add(batch, data[i1].seq_tokens[s][i], i, { s0 + s }, true);
n_logits += 1;
}
}
}
return false;
}
- task.seq_tokens.emplace_back(::llama_tokenize(ctx, task.question + " " + answer, true));
+ task.seq_tokens.emplace_back(::common_tokenize(ctx, task.question + " " + answer, true));
}
auto min_len = task.seq_tokens.front().size();
for (auto& seq : task.seq_tokens) {
// git@hf.co:datasets/Stevross/mmlu
// https://huggingface.co/datasets/truthful_qa
//
-static void multiple_choice_score(llama_context * ctx, const gpt_params & params) {
+static void multiple_choice_score(llama_context * ctx, const common_params & params) {
std::istringstream strstream(params.prompt);
uint32_t n_task;
size_t i1 = i0;
size_t i_logits = 0; // this tells us how many logits were needed before this point in the batch
- llama_batch_clear(batch);
+ common_batch_clear(batch);
// batch as much tasks as possible into the available context
// each task has 4 unique sequence ids - one for each ending
for (size_t i = 0; i < cur_task.common_prefix; ++i) {
//llama_batch_add(batch, cur_task.seq_tokens[0][i], i, { s0 + 0, s0 + 1, s0 + 2, s0 + 3}, false);
- llama_batch_add(batch, cur_task.seq_tokens[0][i], i, batch_indeces, false);
+ common_batch_add(batch, cur_task.seq_tokens[0][i], i, batch_indeces, false);
}
batch.logits[batch.n_tokens - 1] = true; // we need logits for the last token of the common prefix
n_logits += 1;
// TODO: don't evaluate the last token of each sequence
for (size_t i = cur_task.common_prefix; i < seq_tokens_size; ++i) {
const bool needs_logits = i < seq_tokens_size - 1;
- llama_batch_add(batch, cur_task.seq_tokens[s][i], i, { s0 + s }, needs_logits);
+ common_batch_add(batch, cur_task.seq_tokens[s][i], i, { s0 + s }, needs_logits);
n_logits += needs_logits;
}
}
LOG_INF("\n");
}
-static void kl_divergence(llama_context * ctx, const gpt_params & params) {
+static void kl_divergence(llama_context * ctx, const common_params & params) {
if (params.logits_file.empty()) {
LOG_ERR("%s: you must provide a name of a file containing the log probabilities of the base model\n", __func__);
return;
}
int main(int argc, char ** argv) {
- gpt_params params;
+ common_params params;
params.n_ctx = 512;
params.logits_all = true;
params.escape = false;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_PERPLEXITY)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_PERPLEXITY)) {
return 1;
}
- gpt_init();
+ common_init();
const int32_t n_ctx = params.n_ctx;
llama_numa_init(params.numa);
// load the model and apply lora adapter, if any
- llama_init_result llama_init = llama_init_from_gpt_params(params);
+ common_init_result llama_init = common_init_from_params(params);
llama_model * model = llama_init.model;
llama_context * ctx = llama_init.context;
// print system information
{
LOG_INF("\n");
- LOG_INF("%s\n", gpt_params_get_system_info(params).c_str());
+ LOG_INF("%s\n", common_params_get_system_info(params).c_str());
}
struct results_perplexity results;
static void batch_add_seq(llama_batch & batch, const std::vector<int32_t> & tokens, llama_seq_id seq_id) {
size_t n_tokens = tokens.size();
for (size_t i = 0; i < n_tokens; i++) {
- llama_batch_add(batch, tokens[i], i, { seq_id }, true);
+ common_batch_add(batch, tokens[i], i, { seq_id }, true);
}
}
}
float * out = output + batch.seq_id[i][0] * n_embd;
- llama_embd_normalize(embd, out, n_embd);
+ common_embd_normalize(embd, out, n_embd);
}
}
int main(int argc, char ** argv) {
- gpt_params params;
+ common_params params;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_RETRIEVAL, print_usage)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_RETRIEVAL, print_usage)) {
return 1;
}
- gpt_init();
+ common_init();
// For BERT models, batch size must be equal to ubatch size
params.n_ubatch = params.n_batch;
llama_numa_init(params.numa);
// load the model
- llama_init_result llama_init = llama_init_from_gpt_params(params);
+ common_init_result llama_init = common_init_from_params(params);
llama_model * model = llama_init.model;
llama_context * ctx = llama_init.context;
// print system information
{
LOG_INF("\n");
- LOG_INF("%s\n", gpt_params_get_system_info(params).c_str());
+ LOG_INF("%s\n", common_params_get_system_info(params).c_str());
}
// max batch size
// tokenize the prompts and trim
for (auto & chunk : chunks) {
- auto inp = ::llama_tokenize(ctx, chunk.textdata, true, false);
+ auto inp = common_tokenize(ctx, chunk.textdata, true, false);
if (inp.size() > n_batch) {
LOG_ERR("%s: chunk size (%lld) exceeds batch size (%lld), increase batch size and re-run\n",
__func__, (long long int) inp.size(), (long long int) n_batch);
LOG_INF("%s: prompt %d: '%s'\n", __func__, i, chunks[i].textdata.c_str());
LOG_INF("%s: number of tokens in prompt = %zu\n", __func__, chunks[i].tokens.size());
for (int j = 0; j < (int) chunks[i].tokens.size(); j++) {
- LOG_INF("%6d -> '%s'\n", chunks[i].tokens[j], llama_token_to_piece(ctx, chunks[i].tokens[j]).c_str());
+ LOG_INF("%6d -> '%s'\n", chunks[i].tokens[j], common_token_to_piece(ctx, chunks[i].tokens[j]).c_str());
}
LOG_INF("\n\n");
}
if (batch.n_tokens + n_toks > n_batch) {
float * out = emb + p * n_embd;
batch_decode(ctx, batch, out, s, n_embd);
- llama_batch_clear(batch);
+ common_batch_clear(batch);
p += s;
s = 0;
}
while (true) {
LOG("Enter query: ");
std::getline(std::cin, query);
- std::vector<int32_t> query_tokens = llama_tokenize(ctx, query, true);
+ std::vector<int32_t> query_tokens = common_tokenize(ctx, query, true);
batch_add_seq(query_batch, query_tokens, 0);
std::vector<float> query_emb(n_embd, 0);
batch_decode(ctx, query_batch, query_emb.data(), 1, n_embd);
- llama_batch_clear(query_batch);
+ common_batch_clear(query_batch);
// compute cosine similarities
{
std::vector<std::pair<int, float>> similarities;
for (int i = 0; i < n_chunks; i++) {
- float sim = llama_embd_similarity_cos(chunks[i].embedding.data(), query_emb.data(), n_embd);
+ float sim = common_embd_similarity_cos(chunks[i].embedding.data(), query_emb.data(), n_embd);
similarities.push_back(std::make_pair(i, sim));
}
#include <cstdio>
int main(int argc, char ** argv) {
- gpt_params params;
+ common_params params;
params.prompt = "The quick brown fox";
params.sparams.seed = 1234;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON)) {
return 1;
}
std::string result2;
// init
- llama_init_result llama_init = llama_init_from_gpt_params(params);
+ common_init_result llama_init = common_init_from_params(params);
llama_model * model = llama_init.model;
llama_context * ctx = llama_init.context;
llama_sampler_chain_add(smpl, llama_sampler_init_dist(params.sparams.seed));
// tokenize prompt
- auto tokens = llama_tokenize(ctx, params.prompt, true);
+ auto tokens = common_tokenize(ctx, params.prompt, true);
// evaluate prompt
llama_decode(ctx, llama_batch_get_one(tokens.data(), tokens.size(), n_past, 0));
for (auto i = 0; i < params.n_predict; i++) {
auto next_token = llama_sampler_sample(smpl, ctx, -1);
- auto next_token_str = llama_token_to_piece(ctx, next_token);
+ auto next_token_str = common_token_to_piece(ctx, next_token);
printf("%s", next_token_str.c_str());
result0 += next_token_str;
llama_free(ctx);
// make new context
- auto * ctx2 = llama_new_context_with_model(model, llama_context_params_from_gpt_params(params));
+ auto * ctx2 = llama_new_context_with_model(model, common_context_params_to_llama(params));
llama_sampler * smpl2 = llama_sampler_chain_init(sparams);
// second run
for (auto i = 0; i < params.n_predict; i++) {
auto next_token = llama_sampler_sample(smpl2, ctx2, -1);
- auto next_token_str = llama_token_to_piece(ctx2, next_token);
+ auto next_token_str = common_token_to_piece(ctx2, next_token);
printf("%s", next_token_str.c_str());
result1 += next_token_str;
}
// make new context
- auto * ctx3 = llama_new_context_with_model(model, llama_context_params_from_gpt_params(params));
+ auto * ctx3 = llama_new_context_with_model(model, common_context_params_to_llama(params));
llama_sampler * smpl3 = llama_sampler_chain_init(sparams);
// third run with seq 1 instead of 0
for (auto i = 0; i < params.n_predict; i++) {
auto next_token = llama_sampler_sample(smpl3, ctx3, -1);
- auto next_token_str = llama_token_to_piece(ctx3, next_token);
+ auto next_token_str = common_token_to_piece(ctx3, next_token);
printf("%s", next_token_str.c_str());
result2 += next_token_str;
// sampling
json json_schema;
- struct gpt_sampler_params sparams;
- struct gpt_sampler * smpl = nullptr;
+ struct common_sampler_params sparams;
+ struct common_sampler * smpl = nullptr;
llama_token sampled;
generated_token_probs.clear();
}
- bool has_budget(gpt_params &global_params) {
+ bool has_budget(common_params &global_params) {
if (params.n_predict == -1 && global_params.n_predict == -1) {
return true; // limitless
}
struct server_context {
llama_model * model = nullptr;
llama_context * ctx = nullptr;
- std::vector<llama_lora_adapter_container> loras;
+ std::vector<common_lora_adapter_container> loras;
- gpt_params params;
+ common_params params;
llama_batch batch = {};
// Clear any sampling context
for (server_slot & slot : slots) {
if (slot.smpl != nullptr) {
- gpt_sampler_free(slot.smpl);
+ common_sampler_free(slot.smpl);
}
}
llama_batch_free(batch);
}
- bool load_model(const gpt_params & params_) {
+ bool load_model(const common_params & params_) {
params = params_;
// dedicate one sequence to the system prompt
params.n_parallel += 1;
- llama_init_result llama_init = llama_init_from_gpt_params(params);
+ common_init_result llama_init = common_init_from_params(params);
model = llama_init.model;
ctx = llama_init.context;
std::vector<llama_token> p;
if (first) {
- p = ::llama_tokenize(ctx, s, add_special, TMP_FORCE_SPECIAL);
+ p = common_tokenize(ctx, s, add_special, TMP_FORCE_SPECIAL);
first = false;
} else {
- p = ::llama_tokenize(ctx, s, false, TMP_FORCE_SPECIAL);
+ p = common_tokenize(ctx, s, false, TMP_FORCE_SPECIAL);
}
prompt_tokens.insert(prompt_tokens.end(), p.begin(), p.end());
}
} else {
auto s = json_prompt.template get<std::string>();
- prompt_tokens = ::llama_tokenize(ctx, s, add_special, TMP_FORCE_SPECIAL);
+ prompt_tokens = common_tokenize(ctx, s, add_special, TMP_FORCE_SPECIAL);
}
return prompt_tokens;
slot.sparams.logit_bias.push_back({tok, bias});
}
} else if (el[0].is_string()) {
- auto toks = llama_tokenize(model, el[0].get<std::string>(), false);
+ auto toks = common_tokenize(model, el[0].get<std::string>(), false);
for (auto tok : toks) {
slot.sparams.logit_bias.push_back({tok, bias});
}
sampler_names.emplace_back(name);
}
}
- slot.sparams.samplers = gpt_sampler_types_from_names(sampler_names, false);
+ slot.sparams.samplers = common_sampler_types_from_names(sampler_names, false);
} else {
slot.sparams.samplers = default_sparams.samplers;
}
{
if (slot.smpl != nullptr) {
- gpt_sampler_free(slot.smpl);
+ common_sampler_free(slot.smpl);
}
- slot.smpl = gpt_sampler_init(model, slot.sparams);
+ slot.smpl = common_sampler_init(model, slot.sparams);
if (slot.smpl == nullptr) {
// for now, the only error that may happen here is invalid grammar
send_error(task, "Failed to parse grammar", ERROR_TYPE_INVALID_REQUEST);
system_tokens.clear();
if (!system_prompt.empty()) {
- system_tokens = ::llama_tokenize(ctx, system_prompt, true);
+ system_tokens = common_tokenize(ctx, system_prompt, true);
const int32_t n_batch = llama_n_batch(ctx);
const int32_t n_tokens_prompt = system_tokens.size();
for (int32_t i = 0; i < n_tokens_prompt; i += n_batch) {
const int32_t n_tokens = std::min(n_batch, n_tokens_prompt - i);
- llama_batch_clear(batch);
+ common_batch_clear(batch);
for (int32_t j = 0; j < n_tokens; ++j) {
- llama_batch_add(batch, system_tokens[i + j], i + j, { 0 }, false);
+ common_batch_add(batch, system_tokens[i + j], i + j, { 0 }, false);
}
if (llama_decode(ctx, batch) != 0) {
bool process_token(completion_token_output & result, server_slot & slot) {
// remember which tokens were sampled - used for repetition penalties during sampling
- const std::string token_str = llama_token_to_piece(ctx, result.tok, params.special);
+ const std::string token_str = common_token_to_piece(ctx, result.tok, params.special);
slot.sampled = result.tok;
// search stop word and delete it
std::vector<std::string> samplers;
samplers.reserve(slot.sparams.samplers.size());
for (const auto & sampler : slot.sparams.samplers) {
- samplers.emplace_back(gpt_sampler_type_to_str(sampler));
+ samplers.emplace_back(common_sampler_type_to_str(sampler));
}
return json {
{"n_predict", slot.n_predict}, // Server configured n_predict
{"model", params.model_alias},
{"seed", slot.sparams.seed},
- {"seed_cur", slot.smpl ? gpt_sampler_get_seed(slot.smpl) : 0},
+ {"seed_cur", slot.smpl ? common_sampler_get_seed(slot.smpl) : 0},
{"temperature", slot.sparams.temp},
{"dynatemp_range", slot.sparams.dynatemp_range},
{"dynatemp_exponent", slot.sparams.dynatemp_exponent},
};
if (slot.sparams.n_probs > 0) {
- const std::vector<llama_token> to_send_toks = llama_tokenize(ctx, tkn.text_to_send, false);
+ const std::vector<llama_token> to_send_toks = common_tokenize(ctx, tkn.text_to_send, false);
const size_t probs_pos = std::min(slot.n_sent_token_probs, slot.generated_token_probs.size());
const size_t probs_stop_pos = std::min(slot.n_sent_token_probs + to_send_toks.size(), slot.generated_token_probs.size());
if (slot.sparams.n_probs > 0) {
std::vector<completion_token_output> probs;
if (!slot.params.stream && slot.stopped_word) {
- const std::vector<llama_token> stop_word_toks = llama_tokenize(ctx, slot.stopping_word, false);
+ const std::vector<llama_token> stop_word_toks = common_tokenize(ctx, slot.stopping_word, false);
size_t safe_offset = std::min(slot.generated_token_probs.size(), stop_word_toks.size());
probs = std::vector<completion_token_output>(
continue;
}
- llama_embd_normalize(embd, embd_res.data(), n_embd);
+ common_embd_normalize(embd, embd_res.data(), n_embd);
res.data = json {
{"embedding", embd_res},
} break;
case SERVER_TASK_TYPE_SET_LORA:
{
- llama_lora_adapters_apply(ctx, loras);
+ common_lora_adapters_apply(ctx, loras);
server_task_result result;
result.id = task.id;
result.stop = true;
}
// start populating the batch for this iteration
- llama_batch_clear(batch);
+ common_batch_clear(batch);
// frist, add sampled tokens from any ongoing sequences
for (auto & slot : slots) {
// TODO: we always have to take into account the "system_tokens"
// this is not great and needs to be improved somehow
- llama_batch_add(batch, slot.sampled, system_tokens.size() + slot_npast, { slot.id + 1 }, true);
+ common_batch_add(batch, slot.sampled, system_tokens.size() + slot_npast, { slot.id + 1 }, true);
slot.n_past += 1;
GGML_ASSERT(slot.n_prompt_tokens < slot.n_ctx);
}
- gpt_sampler_reset(slot.smpl);
+ common_sampler_reset(slot.smpl);
if (!slot.params.cache_prompt) {
slot.n_past_se = 0;
// push the prompt into the sampling context (do not apply grammar)
for (int i = 0; i < slot.n_past; ++i) {
- gpt_sampler_accept(slot.smpl, slot.cache_tokens[i], false);
+ common_sampler_accept(slot.smpl, slot.cache_tokens[i], false);
}
}
}
slot.n_past_se = 0;
slot.ga_i = 0;
// TODO: is the system prompt ever in the sampling context?
- gpt_sampler_reset(slot.smpl);
+ common_sampler_reset(slot.smpl);
}
// remove the non-common part from the cache
}
}
- llama_batch_add(batch, prompt_tokens[slot.n_past], system_tokens.size() + slot_npast, { slot.id + 1 }, false);
+ common_batch_add(batch, prompt_tokens[slot.n_past], system_tokens.size() + slot_npast, { slot.id + 1 }, false);
if (slot.params.cache_prompt) {
slot.cache_tokens.push_back(prompt_tokens[slot.n_past]);
}
completion_token_output result;
- const llama_token id = gpt_sampler_sample(slot.smpl, ctx, slot.i_batch - i);
+ const llama_token id = common_sampler_sample(slot.smpl, ctx, slot.i_batch - i);
- gpt_sampler_accept(slot.smpl, id, true);
+ common_sampler_accept(slot.smpl, id, true);
slot.n_decoded += 1;
if (slot.n_decoded == 1) {
result.tok = id;
- const auto * cur_p = gpt_sampler_get_candidates(slot.smpl);
+ const auto * cur_p = common_sampler_get_candidates(slot.smpl);
for (size_t i = 0; i < (size_t) slot.sparams.n_probs; ++i) {
result.probs.push_back({
int main(int argc, char ** argv) {
// own arguments required by this example
- gpt_params params;
+ common_params params;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_SERVER)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_SERVER)) {
return 1;
}
- gpt_init();
+ common_init();
// enabling this will output extra debug information in the HTTP responses from the server
// see format_final_response_oaicompat()
LOG_INF("system info: n_threads = %d, n_threads_batch = %d, total_threads = %d\n", params.cpuparams.n_threads, params.cpuparams_batch.n_threads, std::thread::hardware_concurrency());
LOG_INF("\n");
- LOG_INF("%s\n", gpt_params_get_system_info(params).c_str());
+ LOG_INF("%s\n", common_params_get_system_info(params).c_str());
LOG_INF("\n");
std::unique_ptr<httplib::Server> svr;
if (with_pieces) {
for (const auto& token : tokens) {
- std::string piece = llama_token_to_piece(ctx_server.ctx, token);
+ std::string piece = common_token_to_piece(ctx_server.ctx, token);
json piece_json;
// Check if the piece is valid UTF-8
}
// print sample chat example to make it clear which template is used
- LOG_INF("%s: chat template, built_in: %d, chat_example: '%s'\n", __func__, params.chat_template.empty(), llama_chat_format_example(ctx_server.model, params.chat_template).c_str());
+ LOG_INF("%s: chat template, built_in: %d, chat_example: '%s'\n", __func__, params.chat_template.empty(), common_chat_format_example(ctx_server.model, params.chat_template).c_str());
ctx_server.queue_tasks.on_new_task(std::bind(
&server_context::process_single_task, &ctx_server, std::placeholders::_1));
// Format given chat. If tmpl is empty, we take the template from model metadata
inline std::string format_chat(const struct llama_model * model, const std::string & tmpl, const std::vector<json> & messages) {
- std::vector<llama_chat_msg> chat;
+ std::vector<common_chat_msg> chat;
for (size_t i = 0; i < messages.size(); ++i) {
const auto & curr_msg = messages[i];
chat.push_back({role, content});
}
- const auto formatted_chat = llama_chat_apply_template(model, tmpl, chat, true);
+ const auto formatted_chat = common_chat_apply_template(model, tmpl, chat, true);
LOG_DBG("formatted_chat: '%s'\n", formatted_chat.c_str());
return formatted_chat;
static std::string tokens_to_str(llama_context * ctx, Iter begin, Iter end) {
std::string ret;
for (; begin != end; ++begin) {
- ret += llama_token_to_piece(ctx, *begin);
+ ret += common_token_to_piece(ctx, *begin);
}
return ret;
// format incomplete utf-8 multibyte character for output
static std::string tokens_to_output_formatted_string(const llama_context * ctx, const llama_token token) {
- std::string out = token == -1 ? "" : llama_token_to_piece(ctx, token);
+ std::string out = token == -1 ? "" : common_token_to_piece(ctx, token);
// if the size is 1 and first bit is 1, meaning it's a partial character
// (size > 1 meaning it's already a known token)
std::vector<llama_token> tokens;
std::vector<std::vector<llama_token_data>> dists;
- struct gpt_sampler * smpl = nullptr;
+ struct common_sampler * smpl = nullptr;
};
int main(int argc, char ** argv) {
- gpt_params params;
+ common_params params;
// needed to get candidate probs even for temp <= 0.0
params.sparams.n_probs = 128;
- if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_SPECULATIVE)) {
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_SPECULATIVE)) {
return 1;
}
- gpt_init();
+ common_init();
if (params.model_draft.empty()) {
LOG_ERR("%s: --model-draft is required\n", __func__);
llama_context * ctx_dft = NULL;
// load the target model
- llama_init_result llama_init_tgt = llama_init_from_gpt_params(params);
+ common_init_result llama_init_tgt = common_init_from_params(params);
model_tgt = llama_init_tgt.model;
ctx_tgt = llama_init_tgt.context;
}
params.cpuparams_batch.n_threads = params.draft_cpuparams_batch.n_threads;
- llama_init_result llama_init_dft = llama_init_from_gpt_params(params);
+ common_init_result llama_init_dft = common_init_from_params(params);
model_dft = llama_init_dft.model;
ctx_dft = llama_init_dft.context;
if (std::strcmp(token_text_tgt, token_text_dft) != 0) {
LOG_ERR("%s: draft model vocab must match target model to use speculation but ", __func__);
LOG_ERR("token %d content differs - target '%s', draft '%s'\n", i,
- llama_token_to_piece(ctx_tgt, i).c_str(),
- llama_token_to_piece(ctx_dft, i).c_str());
+ common_token_to_piece(ctx_tgt, i).c_str(),
+ common_token_to_piece(ctx_dft, i).c_str());
return 1;
}
}
// Tokenize the prompt
std::vector<llama_token> inp;
- inp = ::llama_tokenize(ctx_tgt, params.prompt, true, true);
+ inp = common_tokenize(ctx_tgt, params.prompt, true, true);
const int max_context_size = llama_n_ctx(ctx_tgt);
const int max_tokens_list_size = max_context_size - 4;
LOG("\n\n");
for (auto id : inp) {
- LOG("%s", llama_token_to_piece(ctx_tgt, id).c_str());
+ LOG("%s", common_token_to_piece(ctx_tgt, id).c_str());
}
const int n_input = inp.size();
bool has_eos = false;
// target model sampling context (reuse the llama_context's sampling instance)
- struct gpt_sampler * smpl = gpt_sampler_init(model_tgt, params.sparams);
+ struct common_sampler * smpl = common_sampler_init(model_tgt, params.sparams);
struct llama_sampler * softmax = llama_sampler_init_softmax();
std::vector<seq_draft> drafts(n_seq_dft);
for (int s = 0; s < n_seq_dft; ++s) {
- // allocate gpt_sampler for each draft sequence
- drafts[s].smpl = gpt_sampler_init(model_dft, params.sparams);
+ // allocate llama_sampler for each draft sequence
+ drafts[s].smpl = common_sampler_init(model_dft, params.sparams);
}
llama_batch batch_dft = llama_batch_init(params.n_ctx, 0, 1);
bool accept = false;
if (params.sparams.temp > 0) {
// stochastic verification
- gpt_sampler_sample(smpl, ctx_tgt, drafts[s_keep].i_batch_tgt[i_dft], true);
+ common_sampler_sample(smpl, ctx_tgt, drafts[s_keep].i_batch_tgt[i_dft], true);
- auto & dist_tgt = *gpt_sampler_get_candidates(smpl);
+ auto & dist_tgt = *common_sampler_get_candidates(smpl);
float p_tgt = 0.0f;
float p_dft = 0.0f;
s_keep = s;
accept = true;
token_id = drafts[s].tokens[i_dft];
- token_str = llama_token_to_piece(ctx_tgt, token_id);
- gpt_sampler_accept(smpl, token_id, true);
+ token_str = common_token_to_piece(ctx_tgt, token_id);
+ common_sampler_accept(smpl, token_id, true);
LOG_DBG("draft token %d of sequence %d (%d, '%s') accepted\n", i_dft, s, token_id, token_str.c_str());
break;
} else {
- LOG_DBG("draft token %d of sequence %d (%d, '%s') rejected\n", i_dft, s, drafts[s].tokens[i_dft], llama_token_to_piece(ctx_tgt, drafts[s].tokens[i_dft]).c_str());
+ LOG_DBG("draft token %d of sequence %d (%d, '%s') rejected\n", i_dft, s, drafts[s].tokens[i_dft], common_token_to_piece(ctx_tgt, drafts[s].tokens[i_dft]).c_str());
drafts[s].active = false;
// calculate residual probability
const int idx = dist(rng);
token_id = dist_tgt.data[idx].id;
- gpt_sampler_accept(smpl, token_id, true);
- token_str = llama_token_to_piece(ctx_tgt, token_id);
+ common_sampler_accept(smpl, token_id, true);
+ token_str = common_token_to_piece(ctx_tgt, token_id);
}
} else {
// greedy verification
// sample from the target model
LOG_DBG("sampling target: s_keep = %3d, i_dft = %3d, i_batch_tgt = %3d\n", s_keep, i_dft, drafts[s_keep].i_batch_tgt[i_dft]);
- token_id = gpt_sampler_sample(smpl, ctx_tgt, drafts[s_keep].i_batch_tgt[i_dft]);
+ token_id = common_sampler_sample(smpl, ctx_tgt, drafts[s_keep].i_batch_tgt[i_dft]);
- gpt_sampler_accept(smpl, token_id, true);
+ common_sampler_accept(smpl, token_id, true);
- token_str = llama_token_to_piece(ctx_tgt, token_id);
+ token_str = common_token_to_piece(ctx_tgt, token_id);
for (int s = 0; s < n_seq_dft; ++s) {
if (!drafts[s].active) {
drafts[0].dists.push_back(std::vector<llama_token_data>());
drafts[0].i_batch_tgt.push_back(0);
- llama_batch_clear(batch_dft);
- llama_batch_add (batch_dft, token_id, n_past_dft, { 0 }, true);
+ common_batch_clear(batch_dft);
+ common_batch_add (batch_dft, token_id, n_past_dft, { 0 }, true);
llama_kv_cache_seq_rm(ctx_dft, 0, n_past_dft, -1);
// LOG_DBG("dft batch: %s\n", LOG_BATCH_TOSTR_PRETTY(ctx_dft, batch_dft).c_str());
}
if (drafts[0].smpl) {
- gpt_sampler_free(drafts[0].smpl);
+ common_sampler_free(drafts[0].smpl);
}
- drafts[0].smpl = gpt_sampler_clone(smpl);
+ drafts[0].smpl = common_sampler_clone(smpl);
int n_seq_cur = 1;
int n_past_cur = n_past_dft;
drafts[0].drafting = true;
drafts[0].i_batch_dft = 0;
- llama_batch_clear(batch_tgt);
- llama_batch_add (batch_tgt, drafts[0].tokens[0], n_past_tgt, { 0 }, true);
+ common_batch_clear(batch_tgt);
+ common_batch_add (batch_tgt, drafts[0].tokens[0], n_past_tgt, { 0 }, true);
// sample n_draft tokens from the draft model using tree-based sampling
for (int i = 0; i < n_draft; ++i) {
continue;
}
- gpt_sampler_sample(drafts[s].smpl, ctx_dft, drafts[s].i_batch_dft, true);
+ common_sampler_sample(drafts[s].smpl, ctx_dft, drafts[s].i_batch_dft, true);
- const auto * cur_p = gpt_sampler_get_candidates(drafts[s].smpl);
+ const auto * cur_p = common_sampler_get_candidates(drafts[s].smpl);
for (int k = 0; k < std::min(n_seq_dft + 3, (int) cur_p->size); ++k) {
LOG_DBG(" - draft candidate %3d for seq %3d, pos %3d: %6d (%8.3f) '%s'\n",
- k, s, i, cur_p->data[k].id, cur_p->data[k].p, llama_token_to_piece(ctx_dft, cur_p->data[k].id).c_str());
+ k, s, i, cur_p->data[k].id, cur_p->data[k].p, common_token_to_piece(ctx_dft, cur_p->data[k].id).c_str());
}
std::vector<int> sa(1, s);
drafts[n_seq_cur].i_batch_tgt = drafts[s].i_batch_tgt;
if (drafts[n_seq_cur].smpl) {
- gpt_sampler_free(drafts[n_seq_cur].smpl);
+ common_sampler_free(drafts[n_seq_cur].smpl);
}
- drafts[n_seq_cur].smpl = gpt_sampler_clone(drafts[s].smpl);
+ drafts[n_seq_cur].smpl = common_sampler_clone(drafts[s].smpl);
sa.push_back(n_seq_cur);
const int s = sa[is];
- gpt_sampler_accept(drafts[s].smpl, id, true);
+ common_sampler_accept(drafts[s].smpl, id, true);
drafts[s].tokens.push_back(id);
// save cur_p.data into drafts[s].dists
// add unique drafted tokens to the target batch
drafts[s].i_batch_tgt.push_back(batch_tgt.n_tokens);
- llama_batch_add(batch_tgt, id, n_past_tgt + i + 1, { s }, true);
+ common_batch_add(batch_tgt, id, n_past_tgt + i + 1, { s }, true);
// add the token to the batch for batched decoding with the draft model
drafts[s].i_batch_dft = batch_dft.n_tokens;
- llama_batch_add(batch_dft, id, n_past_cur, { s }, true);
+ common_batch_add(batch_dft, id, n_past_cur, { s }, true);
if (batch_tgt.n_tokens > n_draft) {
drafts[s].drafting = false;
LOG_INF("\n");
LOG_INF("target:\n\n");
- gpt_perf_print(ctx_tgt, smpl);
+ common_perf_print(ctx_tgt, smpl);
- gpt_sampler_free(smpl);
+ common_sampler_free(smpl);
for (int s = 0; s < n_seq_dft; ++s) {
- gpt_sampler_free(drafts[s].smpl);
+ common_sampler_free(drafts[s].smpl);
}
llama_sampler_free(softmax);
const bool parse_special = !no_parse_special;
std::vector<llama_token> tokens;
- tokens = ::llama_tokenize(model, prompt, add_bos, parse_special);
+ tokens = common_tokenize(model, prompt, add_bos, parse_special);
if (printing_ids) {
printf("[");
} else {
bool invalid_utf8 = false;
printf("%6d -> '", tokens[i]);
- write_utf8_cstr_to_stdout(llama_token_to_piece(ctx, tokens[i]).c_str(), invalid_utf8);
+ write_utf8_cstr_to_stdout(common_token_to_piece(ctx, tokens[i]).c_str(), invalid_utf8);
if (invalid_utf8) {
printf("' (utf-8 decode failure)\n");
} else {
#include <cassert>
int main(void) {
- gpt_params params;
+ common_params params;
printf("test-arg-parser: make sure there is no duplicated arguments in any examples\n\n");
for (int ex = 0; ex < LLAMA_EXAMPLE_COUNT; ex++) {
try {
- auto ctx_arg = gpt_params_parser_init(params, (enum llama_example)ex);
+ auto ctx_arg = common_params_parser_init(params, (enum llama_example)ex);
std::unordered_set<std::string> seen_args;
std::unordered_set<std::string> seen_env_vars;
for (const auto & opt : ctx_arg.options) {
// missing value
argv = {"binary_name", "-m"};
- assert(false == gpt_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
+ assert(false == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
// wrong value (int)
argv = {"binary_name", "-ngl", "hello"};
- assert(false == gpt_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
+ assert(false == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
// wrong value (enum)
argv = {"binary_name", "-sm", "hello"};
- assert(false == gpt_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
+ assert(false == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
// non-existence arg in specific example (--draft cannot be used outside llama-speculative)
argv = {"binary_name", "--draft", "123"};
- assert(false == gpt_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_SERVER));
+ assert(false == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_SERVER));
printf("test-arg-parser: test valid usage\n\n");
argv = {"binary_name", "-m", "model_file.gguf"};
- assert(true == gpt_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
+ assert(true == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
assert(params.model == "model_file.gguf");
argv = {"binary_name", "-t", "1234"};
- assert(true == gpt_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
+ assert(true == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
assert(params.cpuparams.n_threads == 1234);
argv = {"binary_name", "--verbose"};
- assert(true == gpt_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
+ assert(true == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
assert(params.verbosity > 1);
argv = {"binary_name", "-m", "abc.gguf", "--predict", "6789", "--batch-size", "9090"};
- assert(true == gpt_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
+ assert(true == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
assert(params.model == "abc.gguf");
assert(params.n_predict == 6789);
assert(params.n_batch == 9090);
// --draft cannot be used outside llama-speculative
argv = {"binary_name", "--draft", "123"};
- assert(true == gpt_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_SPECULATIVE));
+ assert(true == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_SPECULATIVE));
assert(params.n_draft == 123);
// skip this part on windows, because setenv is not supported
setenv("LLAMA_ARG_THREADS", "blah", true);
argv = {"binary_name"};
- assert(false == gpt_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
+ assert(false == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
setenv("LLAMA_ARG_MODEL", "blah.gguf", true);
setenv("LLAMA_ARG_THREADS", "1010", true);
argv = {"binary_name"};
- assert(true == gpt_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
+ assert(true == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
assert(params.model == "blah.gguf");
assert(params.cpuparams.n_threads == 1010);
setenv("LLAMA_ARG_MODEL", "blah.gguf", true);
setenv("LLAMA_ARG_THREADS", "1010", true);
argv = {"binary_name", "-m", "overwritten.gguf"};
- assert(true == gpt_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
+ assert(true == common_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
assert(params.model == "overwritten.gguf");
assert(params.cpuparams.n_threads == 1010);
#endif // _WIN32
// test llama_chat_format_single for system message
printf("\n\n=== llama_chat_format_single (system message) ===\n\n");
- std::vector<llama_chat_msg> chat2;
- llama_chat_msg sys_msg{"system", "You are a helpful assistant"};
+ std::vector<common_chat_msg> chat2;
+ common_chat_msg sys_msg{"system", "You are a helpful assistant"};
auto fmt_sys = [&](std::string tmpl) {
- auto output = llama_chat_format_single(nullptr, tmpl, chat2, sys_msg, false);
+ auto output = common_chat_format_single(nullptr, tmpl, chat2, sys_msg, false);
printf("fmt_sys(%s) : %s\n", tmpl.c_str(), output.c_str());
printf("-------------------------\n");
return output;
chat2.push_back({"system", "You are a helpful assistant"});
chat2.push_back({"user", "Hello"});
chat2.push_back({"assistant", "I am assistant"});
- llama_chat_msg new_msg{"user", "How are you"};
+ common_chat_msg new_msg{"user", "How are you"};
auto fmt_single = [&](std::string tmpl) {
- auto output = llama_chat_format_single(nullptr, tmpl, chat2, new_msg, true);
+ auto output = common_chat_format_single(nullptr, tmpl, chat2, new_msg, true);
printf("fmt_single(%s) : %s\n", tmpl.c_str(), output.c_str());
printf("-------------------------\n");
return output;
}
if (rand () % 10 < 5) {
- gpt_log_set_timestamps(gpt_log_main(), rand() % 2);
- gpt_log_set_prefix (gpt_log_main(), rand() % 2);
+ common_log_set_timestamps(common_log_main(), rand() % 2);
+ common_log_set_prefix (common_log_main(), rand() % 2);
}
}
});
for (int i = 0; i < nthread; i++) {
threads[i] = std::thread([&, i]() {
for (const auto & test_kv : k_tests) {
- const std::vector<llama_token> res = llama_tokenize(ctx, test_kv.first, add_special, false);
+ const std::vector<llama_token> res = common_tokenize(ctx, test_kv.first, add_special, false);
// here only print the result of the first thread
// because the other threads are running the same tests
printf("\n");
printf("src: '%s'\n", test_kv.first.c_str());
- printf("res: '%s'\n", llama_detokenize(ctx, res).c_str());
+ printf("res: '%s'\n", common_detokenize(ctx, res).c_str());
printf("tok: ");
for (const auto & tok : res) {
printf("%d ", tok);
if (!correct) {
fprintf(stderr, "%s : failed test: '%s'\n", __func__, test_kv.first.c_str());
fprintf(stderr, "%s : detokenized to: '%s' instead of '%s'\n", __func__,
- llama_detokenize(ctx, res).c_str(),
- llama_detokenize(ctx, test_kv.second).c_str());
+ common_detokenize(ctx, res).c_str(),
+ common_detokenize(ctx, test_kv.second).c_str());
fprintf(stderr, "%s : expected tokens: ", __func__);
for (const auto & t : test_kv.second) {
- fprintf(stderr, "%6d '%s', ", t, llama_token_to_piece(ctx, t).c_str());
+ fprintf(stderr, "%6d '%s', ", t, common_token_to_piece(ctx, t).c_str());
}
fprintf(stderr, "\n");
fprintf(stderr, "%s : got tokens: ", __func__);
for (const auto & t : res) {
- fprintf(stderr, "%6d '%s', ", t, llama_token_to_piece(ctx, t).c_str());
+ fprintf(stderr, "%6d '%s', ", t, common_token_to_piece(ctx, t).c_str());
}
fprintf(stderr, "\n");
{
const auto t_start = ggml_time_us();
- res = llama_tokenize(ctx, text, add_special, false);
+ res = common_tokenize(ctx, text, add_special, false);
const auto t_end = ggml_time_us();
const int n_vocab = llama_n_vocab(model);
for (int i = 0; i < n_vocab; ++i) {
- std::string str = llama_detokenize(ctx, std::vector<int>(1, i));
+ std::string str = common_detokenize(ctx, std::vector<int>(1, i));
try {
auto cps = unicode_cpts_from_utf8(str);
- std::vector<llama_token> tokens = llama_tokenize(ctx, str, false, true);
+ std::vector<llama_token> tokens = common_tokenize(ctx, str, false, true);
if (ignore_merges && tokens.size() > 1) {
fprintf(stderr,
"%s : error: token %d detokenizes to '%s'(%zu) but "
fprintf(stderr, "]\n");
return 2;
}
- std::string check = llama_detokenize(ctx, tokens);
+ std::string check = common_detokenize(ctx, tokens);
if (check != str) {
fprintf(stderr, "%s : error: token %d detokenizes to '%s'(%zu) but tokenization of this detokenizes to '%s'(%zu)\n",
__func__, i, str.c_str(), str.length(), check.c_str(), check.length());
}
std::string str = unicode_cpt_to_utf8(cp);
- std::vector<llama_token> tokens = llama_tokenize(ctx, str, false);
- std::string check = llama_detokenize(ctx, tokens);
+ std::vector<llama_token> tokens = common_tokenize(ctx, str, false);
+ std::string check = common_detokenize(ctx, tokens);
if (cp != 9601 && str != check) {
fprintf(stderr, "error: codepoint 0x%x detokenizes to '%s'(%zu) instead of '%s'(%zu)\n",
cp, check.c_str(), check.length(), str.c_str(), str.length());
const int n_vocab = llama_n_vocab(model);
for (int i = 0; i < n_vocab; ++i) {
- std::string str = llama_detokenize(ctx, std::vector<int>(1, i), true);
- std::vector<llama_token> tokens = llama_tokenize(ctx, str, false, true);
- std::string check = llama_detokenize(ctx, tokens);
+ std::string str = common_detokenize(ctx, std::vector<int>(1, i), true);
+ std::vector<llama_token> tokens = common_tokenize(ctx, str, false, true);
+ std::string check = common_detokenize(ctx, tokens);
if (check != str) {
fprintf(stderr, "%s : error: token %d detokenizes to '%s'(%zu) but tokenization of this detokenizes to '%s'(%zu)\n",
__func__, i, str.c_str(), str.length(), check.c_str(), check.length());
}
std::string str = unicode_cpt_to_utf8(cp);
- std::vector<llama_token> tokens = llama_tokenize(ctx, str, false, true);
- std::string check = llama_detokenize(ctx, tokens);
+ std::vector<llama_token> tokens = common_tokenize(ctx, str, false, true);
+ std::string check = common_detokenize(ctx, tokens);
if (cp != 9601 && str != check) {
fprintf(stderr, "error: codepoint 0x%x detokenizes to '%s'(%zu) instead of '%s'(%zu)\n",
cp, check.c_str(), check.length(), str.c_str(), str.length());