1
0

eval-callback.cpp 6.0 KB

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