1
0

eval-callback.cpp 6.8 KB

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