ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / examples / simple / simple-backend.cpp
| 1 | #include "ggml.h" |
| 2 | #include "ggml-backend.h" |
| 3 | |
| 4 | #include <cassert> |
| 5 | #include <cmath> |
| 6 | #include <cstdio> |
| 7 | #include <cstring> |
| 8 | #include <fstream> |
| 9 | #include <vector> |
| 10 | |
| 11 | static void ggml_log_callback_default(ggml_log_level level, const char * text, void * user_data) { |
| 12 | (void) level; |
| 13 | (void) user_data; |
| 14 | fputs(text, stderr); |
| 15 | fflush(stderr); |
| 16 | } |
| 17 | |
| 18 | // This is a simple model with two tensors a and b |
| 19 | struct simple_model { |
| 20 | struct ggml_tensor * a {}; |
| 21 | struct ggml_tensor * b {}; |
| 22 | |
| 23 | // the backend to perform the computation (CPU, CUDA, METAL) |
| 24 | ggml_backend_t backend {}; |
| 25 | ggml_backend_t cpu_backend {}; |
| 26 | ggml_backend_sched_t sched {}; |
| 27 | |
| 28 | // storage for the graph and tensors |
| 29 | std::vector<uint8_t> buf; |
| 30 | }; |
| 31 | |
| 32 | // initialize data of matrices to perform matrix multiplication |
| 33 | const int rows_A = 4, cols_A = 2; |
| 34 | |
| 35 | float matrix_A[rows_A * cols_A] = { |
| 36 | 2, 8, |
| 37 | 5, 1, |
| 38 | 4, 2, |
| 39 | 8, 6 |
| 40 | }; |
| 41 | |
| 42 | const int rows_B = 3, cols_B = 2; |
| 43 | /* Transpose([ |
| 44 | 10, 9, 5, |
| 45 | 5, 9, 4 |
| 46 | ]) 2 rows, 3 cols */ |
| 47 | float matrix_B[rows_B * cols_B] = { |
| 48 | 10, 5, |
| 49 | 9, 9, |
| 50 | 5, 4 |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | // initialize the backends and scheduler |
| 55 | void init_model(simple_model & model) { |
| 56 | ggml_log_set(ggml_log_callback_default, nullptr); |
| 57 | |
| 58 | ggml_backend_load_all(); |
| 59 | |
| 60 | model.backend = ggml_backend_init_best(); |
| 61 | model.cpu_backend = ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, nullptr); |
| 62 | |
| 63 | ggml_backend_t backends[2] = { model.backend, model.cpu_backend }; |
| 64 | model.sched = ggml_backend_sched_new(backends, nullptr, 2, GGML_DEFAULT_GRAPH_SIZE, false, true); |
| 65 | } |
| 66 | |
| 67 | // build the compute graph to perform a matrix multiplication |
| 68 | struct ggml_cgraph * build_graph(simple_model& model) { |
| 69 | size_t buf_size = ggml_tensor_overhead()*GGML_DEFAULT_GRAPH_SIZE + ggml_graph_overhead(); |
| 70 | model.buf.resize(buf_size); |
| 71 | |
| 72 | struct ggml_init_params params0 = { |
| 73 | /*.mem_size =*/ buf_size, |
| 74 | /*.mem_buffer =*/ model.buf.data(), |
| 75 | /*.no_alloc =*/ true, // the tensors will be allocated later |
| 76 | }; |
| 77 | |
| 78 | // create a context to build the graph |
| 79 | struct ggml_context * ctx = ggml_init(params0); |
| 80 | |
| 81 | struct ggml_cgraph * gf = ggml_new_graph(ctx); |
| 82 | |
| 83 | // create tensors |
| 84 | model.a = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, cols_A, rows_A); |
| 85 | model.b = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, cols_B, rows_B); |
| 86 | |
| 87 | // result = a*b^T |
| 88 | struct ggml_tensor * result = ggml_mul_mat(ctx, model.a, model.b); |
| 89 | |
| 90 | // build operations nodes |
| 91 | ggml_build_forward_expand(gf, result); |
| 92 | |
| 93 | ggml_free(ctx); |
| 94 | |
| 95 | return gf; |
| 96 | } |
| 97 | |
| 98 | // compute with backend |
| 99 | struct ggml_tensor * compute(simple_model & model, struct ggml_cgraph * gf) { |
| 100 | ggml_backend_sched_reset(model.sched); |
| 101 | ggml_backend_sched_alloc_graph(model.sched, gf); |
| 102 | |
| 103 | // load data from cpu memory to backend buffer |
| 104 | ggml_backend_tensor_set(model.a, matrix_A, 0, ggml_nbytes(model.a)); |
| 105 | ggml_backend_tensor_set(model.b, matrix_B, 0, ggml_nbytes(model.b)); |
| 106 | |
| 107 | // compute the graph |
| 108 | ggml_backend_sched_graph_compute(model.sched, gf); |
| 109 | |
| 110 | // in this case, the output tensor is the last one in the graph |
| 111 | return ggml_graph_node(gf, -1); |
| 112 | } |
| 113 | |
| 114 | int main(void) { |
| 115 | ggml_time_init(); |
| 116 | |
| 117 | simple_model model; |
| 118 | init_model(model); |
| 119 | |
| 120 | struct ggml_cgraph * gf = build_graph(model); |
| 121 | |
| 122 | // perform computation |
| 123 | struct ggml_tensor * result = compute(model, gf); |
| 124 | |
| 125 | // create a array to print result |
| 126 | std::vector<float> out_data(ggml_nelements(result)); |
| 127 | |
| 128 | // bring the data from the backend memory |
| 129 | ggml_backend_tensor_get(result, out_data.data(), 0, ggml_nbytes(result)); |
| 130 | |
| 131 | // expected result: |
| 132 | // [ 60.00 55.00 50.00 110.00 |
| 133 | // 90.00 54.00 54.00 126.00 |
| 134 | // 42.00 29.00 28.00 64.00 ] |
| 135 | |
| 136 | printf("mul mat (%d x %d) (transposed result):\n[", (int) result->ne[0], (int) result->ne[1]); |
| 137 | for (int j = 0; j < result->ne[1] /* rows */; j++) { |
| 138 | if (j > 0) { |
| 139 | printf("\n"); |
| 140 | } |
| 141 | |
| 142 | for (int i = 0; i < result->ne[0] /* cols */; i++) { |
| 143 | printf(" %.2f", out_data[j * result->ne[0] + i]); |
| 144 | } |
| 145 | } |
| 146 | printf(" ]\n"); |
| 147 | |
| 148 | // release backend memory and free backend |
| 149 | ggml_backend_sched_free(model.sched); |
| 150 | ggml_backend_free(model.backend); |
| 151 | ggml_backend_free(model.cpu_backend); |
| 152 | return 0; |
| 153 | } |