echodict/llama.cpp
version https://git-lfs.github.com/spec/v1 oid sha256:cfc44b7ba25614df70e6b65e3341cae0310163bd32fd31a6b928a542df433faf size 30786
0479
1#include "arg.h"2#include "common.h"3#include "debug.h"4#include "log.h"5#include "llama.h"6#include "llama-cpp.h"7 8#include <clocale>9#include <string>10#include <vector>11 12static bool run(llama_context * ctx, const common_params & params) {13 const llama_model * model = llama_get_model(ctx);14 const llama_vocab * vocab = llama_model_get_vocab(model);15 16 const bool add_bos = llama_vocab_get_add_bos(vocab);17 18 std::vector<llama_token> tokens = common_tokenize(ctx, params.prompt, add_bos, true);19 20 if (tokens.empty()) {21 LOG_ERR("%s : there are not input tokens to process - (try to provide a prompt with '-p')\n", __func__);22 return false;23 }24 25 LOG_INF("number of input tokens = %zu\n", tokens.size());26 for (size_t i = 0; i < tokens.size(); ++i) {27 LOG_INF(" %d\n", tokens[i]);28 }29 30 if (llama_decode(ctx, llama_batch_get_one(tokens.data(), tokens.size()))) {31 LOG_ERR("%s : failed to eval\n", __func__);32 return false;33 }34 35 return true;36}37 38int main(int argc, char ** argv) {39 std::setlocale(LC_NUMERIC, "C");40 41 base_callback_data cb_data;42 43 common_params params;44 45 common_init();46 47 if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON)) {48 return 1;49 }50 51 llama_backend_init();52 llama_numa_init(params.numa);53 54 // pass the callback to the backend scheduler55 // it will be executed for each node during the graph computation56 params.cb_eval = common_debug_cb_eval<false>;57 params.cb_eval_user_data = &cb_data;58 params.warmup = false;59 60 // init61 auto llama_init = common_init_from_params(params);62 63 auto * model = llama_init->model();64 auto * ctx = llama_init->context();65 66 if (model == nullptr || ctx == nullptr) {67 LOG_ERR("%s : failed to init\n", __func__);68 return 1;69 }70 71 // print system information72 {73 LOG_INF("\n");74 LOG_INF("%s\n", common_params_get_system_info(params).c_str());75 LOG_INF("\n");76 }77 78 bool OK = run(ctx, params);79 if (!OK) {80 return 1;81 }82 83 LOG("\n");84 llama_perf_context_print(ctx);85 86 llama_backend_free();87 88 return 0;89}90 