1
0

eval-callback.cpp 7.1 KB

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