1
0

eval-callback.cpp 6.1 KB

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