ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / examples / perf-metal / perf-metal.cpp
| 1 | // basic tool to experiment with the Metal backend |
| 2 | // |
| 3 | // 1. Get GPU trace of a dummy graph: |
| 4 | // |
| 5 | // rm -rf /tmp/perf-metal.gputrace |
| 6 | // make -j perf-metal && METAL_CAPTURE_ENABLED=1 ./bin/perf-metal |
| 7 | // open /tmp/perf-metal.gputrace |
| 8 | // |
| 9 | // https://github.com/ggerganov/llama.cpp/issues/9507 |
| 10 | // |
| 11 | |
| 12 | #include "ggml.h" |
| 13 | #include "ggml-alloc.h" |
| 14 | #include "ggml-backend.h" |
| 15 | #include "ggml-metal.h" |
| 16 | |
| 17 | #include <cstdio> |
| 18 | #include <vector> |
| 19 | #include <thread> |
| 20 | |
| 21 | int main(int argc, char ** argv) { |
| 22 | int n_op = 1024; |
| 23 | int n_iter = 128; |
| 24 | |
| 25 | if (argc > 1) { |
| 26 | n_op = std::atoi(argv[1]); |
| 27 | } |
| 28 | |
| 29 | if (argc > 2) { |
| 30 | n_iter = std::atoi(argv[2]); |
| 31 | } |
| 32 | |
| 33 | printf("%s: n_op = %d, n_iter = %d\n", __func__, n_op, n_iter); |
| 34 | |
| 35 | const int ne00 = 8; |
| 36 | const int ne01 = 8; |
| 37 | const int ne11 = 8; |
| 38 | |
| 39 | std::vector<float> data0(ne00*ne01, 1.0f); |
| 40 | std::vector<float> data1(ne00*ne01, 1.0f/ne00); |
| 41 | |
| 42 | ggml_backend_t backend = ggml_backend_metal_init(); |
| 43 | if (!backend) { |
| 44 | fprintf(stderr, "%s: ggml_backend_metal_init() failed\n", __func__); |
| 45 | return 1; |
| 46 | } |
| 47 | |
| 48 | const size_t ctx_size = 2 * ggml_tensor_overhead(); |
| 49 | |
| 50 | struct ggml_init_params params = { |
| 51 | /*.mem_size =*/ ctx_size, |
| 52 | /*.mem_buffer =*/ NULL, |
| 53 | /*.no_alloc =*/ true, |
| 54 | }; |
| 55 | struct ggml_context * ctx = ggml_init(params); |
| 56 | |
| 57 | struct ggml_tensor * t0 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, ne00, ne01); |
| 58 | struct ggml_tensor * t1 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, ne00, ne11); |
| 59 | |
| 60 | ggml_backend_buffer_t buffer = ggml_backend_alloc_ctx_tensors(ctx, backend); |
| 61 | |
| 62 | ggml_backend_tensor_set(t0, data0.data(), 0, ggml_nbytes(t0)); |
| 63 | ggml_backend_tensor_set(t1, data1.data(), 0, ggml_nbytes(t1)); |
| 64 | |
| 65 | struct ggml_cgraph * gf = NULL; |
| 66 | |
| 67 | struct ggml_context * ctx_cgraph = NULL; |
| 68 | |
| 69 | // create a dummy compute graph: |
| 70 | // |
| 71 | // x = mul_mat(t0, t1) |
| 72 | // x = x * 1.0f |
| 73 | // x = mul_mat(x, t1) |
| 74 | // x = x * 1.0f |
| 75 | // ... repeat n_op times ... |
| 76 | // |
| 77 | { |
| 78 | struct ggml_init_params params0 = { |
| 79 | /*.mem_size =*/ 4*n_op*ggml_tensor_overhead() + ggml_graph_overhead(), |
| 80 | /*.mem_buffer =*/ NULL, |
| 81 | /*.no_alloc =*/ true, |
| 82 | }; |
| 83 | ctx_cgraph = ggml_init(params0); |
| 84 | |
| 85 | gf = ggml_new_graph_custom(ctx_cgraph, 4*n_op, false); |
| 86 | |
| 87 | struct ggml_tensor * cur = ggml_mul_mat(ctx_cgraph, t0, t1); |
| 88 | cur = ggml_scale(ctx_cgraph, cur, 1.0f); |
| 89 | |
| 90 | for (int i = 0; i < n_op - 1; i++) { |
| 91 | cur = ggml_mul_mat(ctx_cgraph, cur, t1); |
| 92 | cur = ggml_scale(ctx_cgraph, cur, 1.0f); |
| 93 | } |
| 94 | |
| 95 | cur = ggml_scale(ctx_cgraph, cur, 42.0f); |
| 96 | |
| 97 | ggml_build_forward_expand(gf, cur); |
| 98 | } |
| 99 | |
| 100 | printf("%s: graph nodes = %d\n", __func__, ggml_graph_n_nodes(gf)); |
| 101 | |
| 102 | ggml_gallocr_t allocr = ggml_gallocr_new(ggml_backend_get_default_buffer_type(backend)); |
| 103 | ggml_gallocr_alloc_graph(allocr, gf); |
| 104 | |
| 105 | { |
| 106 | // warm-up |
| 107 | ggml_backend_graph_compute(backend, gf); |
| 108 | |
| 109 | const int64_t t_start = ggml_time_us(); |
| 110 | |
| 111 | for (int iter = 0; iter < n_iter; iter++) { |
| 112 | ggml_backend_graph_compute(backend, gf); |
| 113 | } |
| 114 | |
| 115 | const int64_t t_end = ggml_time_us(); |
| 116 | |
| 117 | // actual trace |
| 118 | ggml_backend_metal_capture_next_compute(backend); |
| 119 | ggml_backend_graph_compute(backend, gf); |
| 120 | //std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // NOTE: these intervals do not appear in the XCode trace! |
| 121 | ggml_backend_metal_capture_next_compute(backend); |
| 122 | ggml_backend_graph_compute(backend, gf); |
| 123 | //std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // NOTE: these intervals do not appear in the XCode trace! |
| 124 | ggml_backend_metal_capture_next_compute(backend); |
| 125 | ggml_backend_graph_compute(backend, gf); |
| 126 | |
| 127 | printf("%s: time = %f ms\n", __func__, (t_end - t_start) / 1000.0 / n_iter); |
| 128 | } |
| 129 | |
| 130 | { |
| 131 | struct ggml_tensor * res = ggml_graph_node(gf, -1); |
| 132 | |
| 133 | std::vector<float> data(res->ne[0] * res->ne[1], 0.0f); |
| 134 | |
| 135 | ggml_backend_tensor_get(res, data.data(), 0, ggml_nbytes(res)); |
| 136 | |
| 137 | for (int i1 = 0; i1 < res->ne[1]; i1++) { |
| 138 | for (int i0 = 0; i0 < res->ne[0]; i0++) { |
| 139 | printf("%f ", data[i1*res->ne[0] + i0]); |
| 140 | } |
| 141 | printf("\n"); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | ggml_free(ctx_cgraph); |
| 146 | ggml_gallocr_free(allocr); |
| 147 | ggml_free(ctx); |
| 148 | ggml_backend_buffer_free(buffer); |
| 149 | ggml_backend_free(backend); |
| 150 | |
| 151 | return 0; |
| 152 | } |