build the compute graph to perform a matrix multiplication
| 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) { |
no test coverage detected