1
0

eval-callback.cpp 6.3 KB

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