MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / main

Function main

tests/test-opt.cpp:88–138  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

86}
87
88int main(void) {
89 struct ggml_init_params params = {
90 /* .mem_size = */ 1024*1024*1024,
91 /* .mem_buffer = */ NULL,
92 /* .no_alloc = */ false,
93 };
94
95 struct ggml_context * ctx = ggml_init(params);
96
97 int64_t ne1[4] = {4, 128, 1, 1};
98 int64_t ne2[4] = {4, 256, 1, 1};
99 int64_t ne3[4] = {128, 256, 1, 1};
100
101 struct ggml_tensor * a = get_random_tensor(ctx, 2, ne1, -1, +1);
102 struct ggml_tensor * b = get_random_tensor(ctx, 2, ne2, -1, +1);
103 ggml_set_param(ctx, a);
104 ggml_set_param(ctx, b);
105
106 struct ggml_tensor * c = get_random_tensor(ctx, 2, ne3, -1, +1);
107
108 struct ggml_tensor * ab = ggml_mul_mat(ctx, a, b);
109 struct ggml_tensor * d = ggml_sub(ctx, c, ab);
110 struct ggml_tensor * e = ggml_sum(ctx, ggml_sqr(ctx, d));
111
112 struct ggml_cgraph * ge = ggml_new_graph_custom(ctx, GGML_DEFAULT_GRAPH_SIZE, true);
113 ggml_build_forward_expand(ge, e);
114 ggml_graph_reset(ge);
115
116 ggml_graph_compute_with_ctx(ctx, ge, /*n_threads*/ 1);
117
118 const float fe = ggml_get_f32_1d(e, 0);
119 printf("%s: e = %.4f\n", __func__, fe);
120
121 struct ggml_opt_params opt_params = ggml_opt_default_params(GGML_OPT_ADAM);
122
123 ggml_opt(ctx, opt_params, e);
124
125 ggml_graph_reset(ge);
126
127 ggml_graph_compute_with_ctx(ctx, ge, /*n_threads*/ 1);
128
129 const float fe_opt = ggml_get_f32_1d(e, 0);
130 printf("%s: original e = %.4f\n", __func__, fe);
131 printf("%s: optimized e = %.4f\n", __func__, fe_opt);
132
133 const bool success = (fe_opt <= fe);
134 assert(success);
135
136 ggml_free(ctx);
137 return success ? 0 : -1;
138}
139// int64_t ne1[4] = {4, 128, 1, 1};
140// int64_t ne2[4] = {4, 256, 1, 1};;
141// int64_t ne3[4] = {128, 256, 1, 1};

Callers

nothing calls this directly

Calls 15

get_random_tensorFunction · 0.85
printfFunction · 0.85
ggml_optFunction · 0.85
ggml_initFunction · 0.50
ggml_set_paramFunction · 0.50
ggml_mul_matFunction · 0.50
ggml_subFunction · 0.50
ggml_sumFunction · 0.50
ggml_sqrFunction · 0.50
ggml_new_graph_customFunction · 0.50
ggml_graph_resetFunction · 0.50

Tested by

no test coverage detected