eval-callback.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "arg.h"
  2. #include "common.h"
  3. #include "log.h"
  4. #include "llama.h"
  5. #include "ggml.h"
  6. #include <cstdio>
  7. #include <string>
  8. #include <vector>
  9. #include <numeric>
  10. /**
  11. * This the arbitrary data which will be passed to each callback.
  12. * Later on we can for example add operation or tensor name filter from the CLI arg, or a file descriptor to dump the tensor.
  13. */
  14. struct callback_data {
  15. std::vector<uint8_t> data;
  16. };
  17. static std::string ggml_ne_string(const ggml_tensor * t) {
  18. std::string str;
  19. for (int i = 0; i < GGML_MAX_DIMS; ++i) {
  20. str += std::to_string(t->ne[i]);
  21. if (i + 1 < GGML_MAX_DIMS) {
  22. str += ", ";
  23. }
  24. }
  25. return str;
  26. }
  27. static void ggml_print_tensor(uint8_t * data, ggml_type type, const int64_t * ne, const size_t * nb, int64_t n) {
  28. GGML_ASSERT(n > 0);
  29. float sum = 0;
  30. for (int64_t i3 = 0; i3 < ne[3]; i3++) {
  31. LOG(" [\n");
  32. for (int64_t i2 = 0; i2 < ne[2]; i2++) {
  33. if (i2 == n && ne[2] > 2*n) {
  34. LOG(" ..., \n");
  35. i2 = ne[2] - n;
  36. }
  37. LOG(" [\n");
  38. for (int64_t i1 = 0; i1 < ne[1]; i1++) {
  39. if (i1 == n && ne[1] > 2*n) {
  40. LOG(" ..., \n");
  41. i1 = ne[1] - n;
  42. }
  43. LOG(" [");
  44. for (int64_t i0 = 0; i0 < ne[0]; i0++) {
  45. if (i0 == n && ne[0] > 2*n) {
  46. LOG("..., ");
  47. i0 = ne[0] - n;
  48. }
  49. size_t i = i3 * nb[3] + i2 * nb[2] + i1 * nb[1] + i0 * nb[0];
  50. float v;
  51. if (type == GGML_TYPE_F16) {
  52. v = ggml_fp16_to_fp32(*(ggml_fp16_t *) &data[i]);
  53. } else if (type == GGML_TYPE_F32) {
  54. v = *(float *) &data[i];
  55. } else if (type == GGML_TYPE_I64) {
  56. v = (float) *(int64_t *) &data[i];
  57. } else if (type == GGML_TYPE_I32) {
  58. v = (float) *(int32_t *) &data[i];
  59. } else if (type == GGML_TYPE_I16) {
  60. v = (float) *(int16_t *) &data[i];
  61. } else if (type == GGML_TYPE_I8) {
  62. v = (float) *(int8_t *) &data[i];
  63. } else {
  64. GGML_ABORT("fatal error");
  65. }
  66. LOG("%12.4f", v);
  67. sum += v;
  68. if (i0 < ne[0] - 1) LOG(", ");
  69. }
  70. LOG("],\n");
  71. }
  72. LOG(" ],\n");
  73. }
  74. LOG(" ]\n");
  75. LOG(" sum = %f\n", sum);
  76. }
  77. // TODO: make this abort configurable/optional?
  78. if (std::isnan(sum)) {
  79. LOG_ERR("encountered NaN - aborting\n");
  80. exit(0);
  81. }
  82. }
  83. /**
  84. * GGML operations callback during the graph execution.
  85. *
  86. * @param t current tensor
  87. * @param ask when ask is true, the scheduler wants to know if we are interested in data from this tensor
  88. * if we return true, a follow-up call will be made with ask=false in which we can do the actual collection.
  89. * see ggml_backend_sched_eval_callback
  90. * @param user_data user data to pass at each call back
  91. * @return true to receive data or continue the graph, false otherwise
  92. */
  93. static bool ggml_debug(struct ggml_tensor * t, bool ask, void * user_data) {
  94. auto * cb_data = (callback_data *) user_data;
  95. const struct ggml_tensor * src0 = t->src[0];
  96. const struct ggml_tensor * src1 = t->src[1];
  97. if (ask) {
  98. return true; // Always retrieve data
  99. }
  100. char src1_str[128] = {0};
  101. if (src1) {
  102. snprintf(src1_str, sizeof(src1_str), "%s{%s}", src1->name, ggml_ne_string(src1).c_str());
  103. }
  104. LOG("%s: %24s = (%s) %10s(%s{%s}, %s}) = {%s}\n", __func__,
  105. t->name, ggml_type_name(t->type), ggml_op_desc(t),
  106. src0->name, ggml_ne_string(src0).c_str(),
  107. src1 ? src1_str : "",
  108. ggml_ne_string(t).c_str());
  109. // copy the data from the GPU memory if needed
  110. const bool is_host = ggml_backend_buffer_is_host(t->buffer);
  111. if (!is_host) {
  112. auto n_bytes = ggml_nbytes(t);
  113. cb_data->data.resize(n_bytes);
  114. ggml_backend_tensor_get(t, cb_data->data.data(), 0, n_bytes);
  115. }
  116. if (!ggml_is_quantized(t->type)) {
  117. uint8_t * data = is_host ? (uint8_t *) t->data : cb_data->data.data();
  118. ggml_print_tensor(data, t->type, t->ne, t->nb, 3);
  119. }
  120. return true;
  121. }
  122. static bool run(llama_context * ctx, const common_params & params) {
  123. const llama_model * model = llama_get_model(ctx);
  124. const llama_vocab * vocab = llama_model_get_vocab(model);
  125. const bool add_bos = llama_vocab_get_add_bos(vocab);
  126. std::vector<llama_token> tokens = common_tokenize(ctx, params.prompt, add_bos);
  127. if (tokens.empty()) {
  128. LOG_ERR("%s : there are not input tokens to process - (try to provide a prompt with '-p')\n", __func__);
  129. return false;
  130. }
  131. if (llama_decode(ctx, llama_batch_get_one(tokens.data(), tokens.size()))) {
  132. LOG_ERR("%s : failed to eval\n", __func__);
  133. return false;
  134. }
  135. return true;
  136. }
  137. int main(int argc, char ** argv) {
  138. callback_data cb_data;
  139. common_params params;
  140. if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON)) {
  141. return 1;
  142. }
  143. common_init();
  144. llama_backend_init();
  145. llama_numa_init(params.numa);
  146. // pass the callback to the backend scheduler
  147. // it will be executed for each node during the graph computation
  148. params.cb_eval = ggml_debug;
  149. params.cb_eval_user_data = &cb_data;
  150. params.warmup = false;
  151. // init
  152. common_init_result llama_init = common_init_from_params(params);
  153. llama_model * model = llama_init.model.get();
  154. llama_context * ctx = llama_init.context.get();
  155. if (model == nullptr || ctx == nullptr) {
  156. LOG_ERR("%s : failed to init\n", __func__);
  157. return 1;
  158. }
  159. // print system information
  160. {
  161. LOG_INF("\n");
  162. LOG_INF("%s\n", common_params_get_system_info(params).c_str());
  163. LOG_INF("\n");
  164. }
  165. bool OK = run(ctx, params);
  166. if (!OK) {
  167. return 1;
  168. }
  169. LOG("\n");
  170. llama_perf_context_print(ctx);
  171. llama_backend_free();
  172. return 0;
  173. }