-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic.cpp
More file actions
70 lines (60 loc) · 2.08 KB
/
basic.cpp
File metadata and controls
70 lines (60 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "ggml.h"
#include "ggml-easy.h"
#include <iostream>
/**
* This example demonstrates how to perform matrix multiplication using ggml-easy.h
*
* Given 2 matrices A and B, the result matrix C is calculated as follows:
* C = (A x B) * 2
*
* We will use utils.debug_print() to debug the intermediate result of (A x B)
* Then, we will use utils.mark_output() to get the final result of C
*
* The final result can be printed using ggml_easy::debug::print_tensor_data()
* Or, can be used to perform further computations
*/
int main() {
ggml_easy::ctx_params params;
ggml_easy::ctx ctx(params);
// initialize data of matrices to perform matrix multiplication
const int rows_A = 4, cols_A = 2;
float matrix_A[rows_A * cols_A] = {
2, 8,
5, 1,
4, 2,
8, 6
};
const int rows_B = 3, cols_B = 2;
float matrix_B[rows_B * cols_B] = {
10, 5,
9, 9,
5, 4
};
// create cgraph
ctx.build_graph([&](ggml_context * ctx_gf, ggml_cgraph * gf, auto & utils) {
ggml_tensor * a = utils.new_input("a", GGML_TYPE_F32, cols_A, rows_A);
ggml_tensor * b = utils.new_input("b", GGML_TYPE_F32, cols_B, rows_B);
ggml_tensor * a_mul_b = ggml_mul_mat(ctx_gf, a, b);
utils.debug_print(a_mul_b, "a_mul_b");
ggml_tensor * result = ggml_scale(ctx_gf, a_mul_b, 2);
utils.mark_output(result, "result");
});
// set data
ctx.set_tensor_data("a", matrix_A);
ctx.set_tensor_data("b", matrix_B);
// optional: print backend buffer info
ggml_easy::debug::print_backend_buffer_info(ctx);
// compute
ggml_status status = ctx.compute();
if (status != GGML_STATUS_SUCCESS) {
std::cerr << "error: ggml compute return status: " << status << std::endl;
return 1;
}
// get result
auto result = ctx.get_tensor_data("result");
ggml_tensor * result_tensor = result.first;
std::vector<uint8_t> & result_data = result.second;
// print result
ggml_easy::debug::print_tensor_data(result_tensor, result_data.data());
return 0;
}