eval-callback.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "common.h"
  2. #include "llama.h"
  3. #include "ggml.h"
  4. #include <cstdio>
  5. #include <random>
  6. #include <string>
  7. #include <tuple>
  8. #include <vector>
  9. /**
  10. * This the arbitrary data which will be passed to each callback.
  11. * Later on we can for example add operation or tensor name filter from the CLI arg, or a file descriptor to dump the tensor.
  12. */
  13. struct callback_data {
  14. std::vector<uint8_t> data;
  15. };
  16. static std::string ggml_ne_string(const ggml_tensor * t) {
  17. std::string str;
  18. for (int i = 0; i < GGML_MAX_DIMS; ++i) {
  19. str += std::to_string(t->ne[i]);
  20. if (i + 1 < GGML_MAX_DIMS) {
  21. str += ", ";
  22. }
  23. }
  24. return str;
  25. }
  26. static void ggml_print_tensor(uint8_t * data, ggml_type type, const int64_t * ne, const size_t * nb, int64_t n) {
  27. float sum = 0;
  28. for (int64_t i3 = 0; i3 < ne[3]; i3++) {
  29. printf(" [\n");
  30. for (int64_t i2 = 0; i2 < ne[2] && i2 < n; i2++) {
  31. printf(" [\n");
  32. for (int64_t i1 = 0; i1 < ne[1] && i1 < n; i1++) {
  33. printf(" [");
  34. for (int64_t i0 = 0; i0 < ne[0] && i0 < n; i0++) {
  35. size_t i = i3 * nb[3] + i2 * nb[2] + i1 * nb[1] + i0 * nb[0];
  36. float v;
  37. if (type == GGML_TYPE_F16) {
  38. v = ggml_fp16_to_fp32(*(ggml_fp16_t *) data + i);
  39. } else if (type == GGML_TYPE_F32) {
  40. v = *(float *) data + i;
  41. } else if (type == GGML_TYPE_I32) {
  42. v = (float) *(int32_t *) data + i;
  43. } else if (type == GGML_TYPE_I16) {
  44. v = (float) *(int16_t *) data + i;
  45. } else if (type == GGML_TYPE_I8) {
  46. v = (float) *(int8_t *) data + i;
  47. } else {
  48. GGML_ASSERT(false);
  49. }
  50. printf("%8.4f", v);
  51. sum += v;
  52. if (i0 < ne[0] - 1 && i0 < n - 1) printf(", ");
  53. }
  54. if (ne[0] > n) printf(", ...");
  55. printf("],\n");
  56. }
  57. if (ne[1] > n) printf(" ...\n");
  58. printf(" ],\n");
  59. }
  60. if (ne[2] > n) printf(" ...\n");
  61. printf(" ]\n");
  62. printf(" sum = %f\n", sum);
  63. }
  64. }
  65. /**
  66. * GGML operations callback during the graph execution.
  67. *
  68. * @param t current tensor
  69. * @param ask when ask is true, the scheduler wants to know if we are interested in data from this tensor
  70. * if we return true, a follow-up call will be made with ask=false in which we can do the actual collection.
  71. * see ggml_backend_sched_eval_callback
  72. * @param user_data user data to pass at each call back
  73. * @return true to receive data or continue the graph, false otherwise
  74. */
  75. static bool ggml_debug(struct ggml_tensor * t, bool ask, void * user_data) {
  76. auto * cb_data = (callback_data *) user_data;
  77. const struct ggml_tensor * src0 = t->src[0];
  78. const struct ggml_tensor * src1 = t->src[1];
  79. if (ask) {
  80. return true; // Always retrieve data
  81. }
  82. char src1_str[128] = {0};
  83. if (src1) {
  84. sprintf(src1_str, "%s{%s}", src1->name, ggml_ne_string(src1).c_str());
  85. }
  86. printf("%s: %24s = (%s) %10s(%s{%s}, %s}) = {%s}\n", __func__,
  87. t->name, ggml_type_name(t->type), ggml_op_name(t->op),
  88. src0->name, ggml_ne_string(src0).c_str(),
  89. src1 ? src1_str : "",
  90. ggml_ne_string(t).c_str());
  91. // copy the data from the GPU memory if needed
  92. const bool is_host = ggml_backend_buffer_is_host(t->buffer);
  93. if (!is_host) {
  94. auto n_bytes = ggml_nbytes(t);
  95. cb_data->data.resize(n_bytes);
  96. ggml_backend_tensor_get(t, cb_data->data.data(), 0, n_bytes);
  97. }
  98. if (!ggml_is_quantized(t->type)) {
  99. uint8_t * data = is_host ? (uint8_t *) t->data : cb_data->data.data();
  100. ggml_print_tensor(data, t->type, t->ne, t->nb, 3);
  101. }
  102. return true;
  103. }
  104. static bool run(llama_context * ctx, const gpt_params & params) {
  105. const bool add_bos = llama_should_add_bos_token(llama_get_model(ctx));
  106. std::vector<llama_token> tokens = ::llama_tokenize(ctx, params.prompt, add_bos);
  107. if (llama_decode(ctx, llama_batch_get_one(tokens.data(), tokens.size(), 0, 0))) {
  108. fprintf(stderr, "%s : failed to eval\n", __func__);
  109. return false;
  110. }
  111. return true;
  112. }
  113. int main(int argc, char ** argv) {
  114. callback_data cb_data;
  115. gpt_params params;
  116. if (!gpt_params_parse(argc, argv, params)) {
  117. return 1;
  118. }
  119. print_build_info();
  120. std::mt19937 rng(params.seed);
  121. if (params.random_prompt) {
  122. params.prompt = gpt_random_prompt(rng);
  123. }
  124. llama_backend_init();
  125. llama_numa_init(params.numa);
  126. // pass the callback to the backend scheduler
  127. // it will be executed for each node during the graph computation
  128. params.cb_eval = ggml_debug;
  129. params.cb_eval_user_data = &cb_data;
  130. params.warmup = false;
  131. // init
  132. llama_model * model;
  133. llama_context * ctx;
  134. std::tie(model, ctx) = llama_init_from_gpt_params(params);
  135. if (model == nullptr || ctx == nullptr) {
  136. fprintf(stderr, "%s : failed to init\n", __func__);
  137. return 1;
  138. }
  139. // print system information
  140. {
  141. fprintf(stderr, "\n");
  142. fprintf(stderr, "%s\n", get_system_info(params).c_str());
  143. }
  144. bool OK = run(ctx, params);
  145. if (!OK) {
  146. return 1;
  147. }
  148. llama_print_timings(ctx);
  149. llama_free(ctx);
  150. llama_free_model(model);
  151. llama_backend_free();
  152. return 0;
  153. }