metal.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Evaluate a statically exported ggml computation graph with Metal
  2. //
  3. // - First, export a LLaMA graph:
  4. //
  5. // $ ./bin/main -m ../models/7B/ggml-model-q4_0.bin --export
  6. //
  7. // - Run this tool to evaluate the exported graph:
  8. //
  9. // $ ./bin/metal llama.ggml
  10. //
  11. // The purpose of this tool is mostly for debugging and demonstration purposes.
  12. // The main limitation of exporting computation graphs is that their sizes are static which often
  13. // can be a problem for real-world applications.
  14. //
  15. #include "ggml.h"
  16. #include "ggml-metal.h"
  17. #include <cstdio>
  18. #include <cstring>
  19. #include <cstdlib>
  20. int main(int argc, char ** argv) {
  21. ggml_time_init();
  22. if (argc != 2) {
  23. fprintf(stderr, "Usage: %s llama.ggml\n", argv[0]);
  24. return -1;
  25. }
  26. const char * fname_cgraph = argv[1];
  27. // load the compute graph
  28. struct ggml_context * ctx_data = NULL;
  29. struct ggml_context * ctx_eval = NULL;
  30. struct ggml_cgraph gf = ggml_graph_import(fname_cgraph, &ctx_data, &ctx_eval);
  31. // this allocates all Metal resources and memory buffers
  32. auto * ctx_metal = ggml_metal_init(1);
  33. const size_t max_size_data = ggml_get_max_tensor_size(ctx_data);
  34. const size_t max_size_eval = ggml_get_max_tensor_size(ctx_eval);
  35. ggml_metal_add_buffer(ctx_metal, "data", ggml_get_mem_buffer(ctx_data), ggml_get_mem_size(ctx_data), max_size_data);
  36. ggml_metal_add_buffer(ctx_metal, "eval", ggml_get_mem_buffer(ctx_eval), ggml_get_mem_size(ctx_eval), max_size_eval);
  37. // main
  38. {
  39. struct ggml_tensor * input = ggml_graph_get_tensor(&gf, "embd");
  40. *(int32_t *) input->data = 1; // BOS
  41. ggml_metal_set_tensor(ctx_metal, input);
  42. // warmup
  43. ggml_metal_graph_compute(ctx_metal, &gf);
  44. const int n_iter = 16;
  45. const int64_t t0 = ggml_time_us();
  46. // the actual inference happens here
  47. for (int i = 0; i < n_iter; ++i) {
  48. ggml_metal_graph_compute(ctx_metal, &gf);
  49. }
  50. const int64_t t1 = ggml_time_us();
  51. printf("time: %.2f ms, %.2f ms/tok\n", (t1 - t0) / 1000.0, (t1 - t0) / 1000.0 / n_iter);
  52. }
  53. // debug output
  54. {
  55. struct ggml_tensor * logits = gf.nodes[gf.n_nodes - 1];
  56. ggml_metal_get_tensor(ctx_metal, logits);
  57. float * ptr = (float *) ggml_get_data(logits);
  58. printf("logits: ");
  59. for (int i = 0; i < 10; i++) {
  60. printf("%8.4f ", ptr[i]);
  61. }
  62. printf("\n");
  63. int imax = 0;
  64. double sum = 0.0;
  65. double vmax = -1e9;
  66. for (int i = 0; i < 32000; i++) {
  67. sum += (double) ptr[i];
  68. if (ptr[i] > vmax) {
  69. vmax = ptr[i];
  70. imax = i;
  71. }
  72. }
  73. printf("sum: %f, imax = %d, vmax = %f\n", sum, imax, vmax);
  74. }
  75. ggml_metal_free(ctx_metal);
  76. ggml_free(ctx_data);
  77. ggml_free(ctx_eval);
  78. return 0;
  79. }