ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / examples / simple / simple-ctx.cpp
| 1 | #include "ggml.h" |
| 2 | #include "ggml-cpu.h" |
| 3 | |
| 4 | #include <cassert> |
| 5 | #include <cmath> |
| 6 | #include <cstdio> |
| 7 | #include <cstring> |
| 8 | #include <fstream> |
| 9 | #include <map> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | // This is a simple model with two tensors a and b |
| 14 | struct simple_model { |
| 15 | struct ggml_tensor * a; |
| 16 | struct ggml_tensor * b; |
| 17 | |
| 18 | // the context to define the tensor information (dimensions, size, memory data) |
| 19 | struct ggml_context * ctx; |
| 20 | }; |
| 21 | |
| 22 | // initialize the tensors of the model in this case two matrices 2x2 |
| 23 | void load_model(simple_model & model, float * a, float * b, int rows_A, int cols_A, int rows_B, int cols_B) { |
| 24 | size_t ctx_size = 0; |
| 25 | { |
| 26 | ctx_size += rows_A * cols_A * ggml_type_size(GGML_TYPE_F32); // tensor a |
| 27 | ctx_size += rows_B * cols_B * ggml_type_size(GGML_TYPE_F32); // tensor b |
| 28 | ctx_size += 2 * ggml_tensor_overhead(), // tensors |
| 29 | ctx_size += ggml_graph_overhead(); // compute graph |
| 30 | ctx_size += 1024; // some overhead |
| 31 | } |
| 32 | |
| 33 | struct ggml_init_params params { |
| 34 | /*.mem_size =*/ ctx_size, |
| 35 | /*.mem_buffer =*/ NULL, |
| 36 | /*.no_alloc =*/ false, // NOTE: this should be false when using the legacy API |
| 37 | }; |
| 38 | |
| 39 | // create context |
| 40 | model.ctx = ggml_init(params); |
| 41 | |
| 42 | // create tensors |
| 43 | model.a = ggml_new_tensor_2d(model.ctx, GGML_TYPE_F32, cols_A, rows_A); |
| 44 | model.b = ggml_new_tensor_2d(model.ctx, GGML_TYPE_F32, cols_B, rows_B); |
| 45 | |
| 46 | memcpy(model.a->data, a, ggml_nbytes(model.a)); |
| 47 | memcpy(model.b->data, b, ggml_nbytes(model.b)); |
| 48 | } |
| 49 | |
| 50 | // build the compute graph to perform a matrix multiplication |
| 51 | struct ggml_cgraph * build_graph(const simple_model& model) { |
| 52 | struct ggml_cgraph * gf = ggml_new_graph(model.ctx); |
| 53 | |
| 54 | // result = a*b^T |
| 55 | struct ggml_tensor * result = ggml_mul_mat(model.ctx, model.a, model.b); |
| 56 | |
| 57 | ggml_build_forward_expand(gf, result); |
| 58 | return gf; |
| 59 | } |
| 60 | |
| 61 | // compute with backend |
| 62 | struct ggml_tensor * compute(const simple_model & model) { |
| 63 | struct ggml_cgraph * gf = build_graph(model); |
| 64 | |
| 65 | int n_threads = 1; // number of threads to perform some operations with multi-threading |
| 66 | |
| 67 | ggml_graph_compute_with_ctx(model.ctx, gf, n_threads); |
| 68 | |
| 69 | // in this case, the output tensor is the last one in the graph |
| 70 | return ggml_graph_node(gf, -1); |
| 71 | } |
| 72 | |
| 73 | int main(void) { |
| 74 | ggml_time_init(); |
| 75 | |
| 76 | // initialize data of matrices to perform matrix multiplication |
| 77 | const int rows_A = 4, cols_A = 2; |
| 78 | |
| 79 | float matrix_A[rows_A * cols_A] = { |
| 80 | 2, 8, |
| 81 | 5, 1, |
| 82 | 4, 2, |
| 83 | 8, 6 |
| 84 | }; |
| 85 | |
| 86 | const int rows_B = 3, cols_B = 2; |
| 87 | /* Transpose([ |
| 88 | 10, 9, 5, |
| 89 | 5, 9, 4 |
| 90 | ]) 2 rows, 3 cols */ |
| 91 | float matrix_B[rows_B * cols_B] = { |
| 92 | 10, 5, |
| 93 | 9, 9, |
| 94 | 5, 4 |
| 95 | }; |
| 96 | |
| 97 | simple_model model; |
| 98 | load_model(model, matrix_A, matrix_B, rows_A, cols_A, rows_B, cols_B); |
| 99 | |
| 100 | // perform computation in cpu |
| 101 | struct ggml_tensor * result = compute(model); |
| 102 | |
| 103 | // get the result data pointer as a float array to print |
| 104 | std::vector<float> out_data(ggml_nelements(result)); |
| 105 | memcpy(out_data.data(), result->data, ggml_nbytes(result)); |
| 106 | |
| 107 | // expected result: |
| 108 | // [ 60.00 55.00 50.00 110.00 |
| 109 | // 90.00 54.00 54.00 126.00 |
| 110 | // 42.00 29.00 28.00 64.00 ] |
| 111 | |
| 112 | printf("mul mat (%d x %d) (transposed result):\n[", (int) result->ne[0], (int) result->ne[1]); |
| 113 | for (int j = 0; j < result->ne[1] /* rows */; j++) { |
| 114 | if (j > 0) { |
| 115 | printf("\n"); |
| 116 | } |
| 117 | |
| 118 | for (int i = 0; i < result->ne[0] /* cols */; i++) { |
| 119 | printf(" %.2f", out_data[j * result->ne[0] + i]); |
| 120 | } |
| 121 | } |
| 122 | printf(" ]\n"); |
| 123 | |
| 124 | // free memory |
| 125 | ggml_free(model.ctx); |
| 126 | return 0; |
| 127 | } |