eval-callback.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include <string>
  8. #include <vector>
  9. static bool run(llama_context * ctx, const common_params & params) {
  10. const llama_model * model = llama_get_model(ctx);
  11. const llama_vocab * vocab = llama_model_get_vocab(model);
  12. const bool add_bos = llama_vocab_get_add_bos(vocab);
  13. std::vector<llama_token> tokens = common_tokenize(ctx, params.prompt, add_bos);
  14. if (tokens.empty()) {
  15. LOG_ERR("%s : there are not input tokens to process - (try to provide a prompt with '-p')\n", __func__);
  16. return false;
  17. }
  18. if (llama_decode(ctx, llama_batch_get_one(tokens.data(), tokens.size()))) {
  19. LOG_ERR("%s : failed to eval\n", __func__);
  20. return false;
  21. }
  22. return true;
  23. }
  24. int main(int argc, char ** argv) {
  25. base_callback_data cb_data;
  26. common_params params;
  27. if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON)) {
  28. return 1;
  29. }
  30. common_init();
  31. llama_backend_init();
  32. llama_numa_init(params.numa);
  33. // pass the callback to the backend scheduler
  34. // it will be executed for each node during the graph computation
  35. params.cb_eval = common_debug_cb_eval<false>;
  36. params.cb_eval_user_data = &cb_data;
  37. params.warmup = false;
  38. // init
  39. auto llama_init = common_init_from_params(params);
  40. auto * model = llama_init->model();
  41. auto * ctx = llama_init->context();
  42. if (model == nullptr || ctx == nullptr) {
  43. LOG_ERR("%s : failed to init\n", __func__);
  44. return 1;
  45. }
  46. // print system information
  47. {
  48. LOG_INF("\n");
  49. LOG_INF("%s\n", common_params_get_system_info(params).c_str());
  50. LOG_INF("\n");
  51. }
  52. bool OK = run(ctx, params);
  53. if (!OK) {
  54. return 1;
  55. }
  56. LOG("\n");
  57. llama_perf_context_print(ctx);
  58. llama_backend_free();
  59. return 0;
  60. }