interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / tests / test-customop.c

300 lines · 9908 bytes Raw Blame History
1#include "ggml.h"
2#include "ggml-cpu.h"
3
4#include <string.h>
5#include <stdio.h>
6#include <stdlib.h>
7
8#if defined(_WIN32)
9#include <windows.h>
10typedef volatile LONG atomic_int;
11static LONG atomic_fetch_add(atomic_int * ptr, LONG inc) {
12 return InterlockedExchangeAdd(ptr, inc);
13}
14#else
15#include <stdatomic.h>
16#endif
17
18#define MIN(a, b) ((a) < (b) ? (a) : (b))
19#define MAX(a, b) ((a) > (b) ? (a) : (b))
20
21struct ggml_context * make_ctx(void) {
22 struct ggml_init_params params = {
23 /*.mem_size =*/ 1 * 1024 * 1024,
24 /*.mem_buffer =*/ NULL,
25 /*.no_alloc =*/ false,
26 };
27
28 return ggml_init(params);
29}
30
31char g_userdata[] = "ggml";
32atomic_int g_custom1_count = 0;
33atomic_int g_custom2_count = 0;
34atomic_int g_custom3_count = 0;
35
36void custom1(struct ggml_tensor * dst , const struct ggml_tensor * a, int ith, int nth, void * userdata) {
37 // check that the userdata is correct
38 GGML_ASSERT(userdata == NULL);
39 GGML_ASSERT(ggml_are_same_shape(dst, a));
40
41 atomic_fetch_add(&g_custom1_count, 1);
42
43 const float * a_data = ggml_get_data_f32(a);
44 float * dst_data = ggml_get_data_f32(dst);
45
46 // this assumes that the tensors are contiguous
47 GGML_ASSERT(ggml_is_contiguous(dst));
48 GGML_ASSERT(ggml_is_contiguous(a));
49
50 // parallelize by elements
51 const int ne = (int)ggml_nelements(dst);
52 const int dr = (ne + nth - 1) / nth;
53 const int ie0 = dr * ith;
54 const int ie1 = MIN(ie0 + dr, ne);
55
56 for (int i = ie0; i < ie1; ++i) {
57 dst_data[i] = a_data[i] * 2;
58 }
59}
60
61void custom2(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, int ith, int nth, void * userdata) {
62 // check that the userdata is correct
63 GGML_ASSERT(userdata == g_userdata);
64 GGML_ASSERT(strcmp(userdata, "ggml") == 0);
65 GGML_ASSERT(ggml_are_same_shape(dst, a));
66 GGML_ASSERT(ggml_are_same_shape(dst, b));
67
68 atomic_fetch_add(&g_custom2_count, 1);
69
70 const float * a_data = ggml_get_data_f32(a);
71 const float * b_data = ggml_get_data_f32(b);
72 float * dst_data = ggml_get_data_f32(dst);
73
74 // parallelize by rows
75 const int nr = (int)ggml_nrows(dst);
76 // number of rows per thread
77 const int dr = (nr + nth - 1) / nth;
78 // row range for this thread
79 const int ir0 = dr * ith;
80 const int ir1 = MIN(ir0 + dr, nr);
81
82 // number of columns
83 const int nc = (int)dst->ne[0];
84
85 // this assumes that the tensors are contiguous
86 GGML_ASSERT(ggml_is_contiguous(dst));
87 GGML_ASSERT(ggml_is_contiguous(a));
88 GGML_ASSERT(ggml_is_contiguous(b));
89
90 for (int ir = ir0; ir < ir1; ++ir) {
91 for (int ic = 0; ic < nc; ++ic) {
92 const int i = ir * nc + ic;
93 dst_data[i] = a_data[i] + b_data[i];
94 }
95 }
96}
97
98void custom3(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, const struct ggml_tensor * c, int ith, int nth, void * userdata) {
99 // check that the userdata is correct
100 GGML_ASSERT(userdata == g_userdata);
101 GGML_ASSERT(strcmp(userdata, "ggml") == 0);
102 GGML_ASSERT(ggml_are_same_shape(dst, a));
103 GGML_ASSERT(ggml_are_same_shape(dst, b));
104 GGML_ASSERT(ggml_are_same_shape(dst, c));
105
106 atomic_fetch_add(&g_custom3_count, 1);
107
108 const float * a_data = ggml_get_data_f32(a);
109 const float * b_data = ggml_get_data_f32(b);
110 const float * c_data = ggml_get_data_f32(c);
111 float * dst_data = ggml_get_data_f32(dst);
112
113 // dont parallelize
114 GGML_ASSERT(ith == 0);
115
116 // number of elements
117 const int ne = (int)ggml_nelements(dst);
118
119 // this assumes that the tensors are contiguous
120 GGML_ASSERT(ggml_is_contiguous(dst));
121 GGML_ASSERT(ggml_is_contiguous(a));
122 GGML_ASSERT(ggml_is_contiguous(b));
123 GGML_ASSERT(ggml_is_contiguous(c));
124
125 for (int i = 0; i < ne; ++i) {
126 dst_data[i] = a_data[i] + b_data[i] + c_data[i];
127 }
128}
129
130void custom(struct ggml_tensor * dst, int ith, int nth, void * userdata) {
131 struct ggml_tensor * src0 = dst->src[0];
132 struct ggml_tensor * src1 = dst->src[1];
133 struct ggml_tensor * src2 = dst->src[2];
134 struct ggml_tensor * src3 = dst->src[3];
135 struct ggml_tensor * src4 = dst->src[4];
136
137 int32_t * dst_data = (int32_t *) ggml_get_data(dst);
138 const float * src0_data = ggml_get_data_f32(src0);
139 const float * src1_data = ggml_get_data_f32(src1);
140 const float * src2_data = ggml_get_data_f32(src2);
141 const float * src3_data = ggml_get_data_f32(src3);
142 const float * src4_data = ggml_get_data_f32(src4);
143
144 // check that the userdata is correct
145 GGML_ASSERT(userdata == g_userdata);
146 GGML_ASSERT(strcmp(userdata, "ggml") == 0);
147
148 // check that the tensors are contiguous
149 GGML_ASSERT(ggml_is_contiguous(dst));
150 GGML_ASSERT(ggml_is_contiguous(src0));
151 GGML_ASSERT(ggml_is_contiguous(src1));
152 GGML_ASSERT(ggml_is_contiguous(src2));
153 GGML_ASSERT(ggml_is_contiguous(src3));
154 GGML_ASSERT(ggml_is_contiguous(src4));
155
156 // check that the shapes are the same
157 GGML_ASSERT(ggml_are_same_shape(dst, src0));
158 GGML_ASSERT(ggml_are_same_shape(dst, src1));
159 GGML_ASSERT(ggml_are_same_shape(dst, src2));
160 GGML_ASSERT(ggml_are_same_shape(dst, src3));
161 GGML_ASSERT(ggml_are_same_shape(dst, src4));
162
163
164 for (int i = ith; i < ggml_nelements(dst); i += nth) {
165 dst_data[i] = src0_data[i] + src1_data[i] * src2_data[i] - src3_data[i] * src4_data[i];
166 }
167}
168
169int main(int argc, const char** argv) {
170
171 float buf1_f32[1024];
172 for (int i = 0; i < 1024; ++i) {
173 buf1_f32[i] = (float)(i + 1);
174 }
175 float buf2_f32[1024];
176 for (int i = 0; i < 1024; ++i) {
177 buf2_f32[i] = (float)(i + 1) * 2;
178 }
179 float buf3_f32[1024];
180 for (int i = 0; i < 1024; ++i) {
181 buf3_f32[i] = (float)(i + 1) * 3;
182 }
183
184 // map_custom1
185 // 2 tasks, no userdata, parallelized by elements
186 {
187 struct ggml_context * ctx = make_ctx();
188 struct ggml_tensor * t = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
189 memcpy(t->data, buf1_f32, ggml_nbytes(t));
190
191 struct ggml_tensor * m1 = ggml_map_custom1(ctx, t, custom1, 2, NULL);
192
193 struct ggml_cgraph * graph = ggml_new_graph(ctx);
194 ggml_build_forward_expand(graph, m1);
195
196 ggml_graph_compute_with_ctx(ctx, graph, 4);
197
198 const float * output = ggml_get_data_f32(m1);
199
200 for (int i = 0; i < ggml_nelements(m1); ++i) {
201 GGML_ASSERT(output[i] == buf1_f32[i] * 2);
202 }
203 GGML_ASSERT(g_custom1_count == 2);
204
205 ggml_free(ctx);
206 }
207
208 // map_custom2
209 // max tasks (4), userdata, parallelized by rows
210 {
211 struct ggml_context * ctx = make_ctx();
212 struct ggml_tensor * t1 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
213 struct ggml_tensor * t2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
214 memcpy(t1->data, buf1_f32, ggml_nbytes(t1));
215 memcpy(t2->data, buf2_f32, ggml_nbytes(t2));
216
217 struct ggml_tensor * m2 = ggml_map_custom2(ctx, t1, t2, custom2, GGML_N_TASKS_MAX, g_userdata);
218
219 struct ggml_cgraph * graph = ggml_new_graph(ctx);
220 ggml_build_forward_expand(graph, m2);
221
222 ggml_graph_compute_with_ctx(ctx, graph, 4);
223
224 const float * output = ggml_get_data_f32(m2);
225
226 for (int i = 0; i < ggml_nelements(m2); ++i) {
227 GGML_ASSERT(output[i] == buf1_f32[i] + buf2_f32[i]);
228 }
229
230 GGML_ASSERT(g_custom2_count == 4);
231
232 ggml_free(ctx);
233 }
234
235 // map_custom3
236 // 1 task, userdata, not parallelized
237 {
238 struct ggml_context * ctx = make_ctx();
239 struct ggml_tensor * t1 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
240 struct ggml_tensor * t2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
241 struct ggml_tensor * t3 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
242
243 memcpy(t1->data, buf1_f32, ggml_nbytes(t1));
244 memcpy(t2->data, buf2_f32, ggml_nbytes(t2));
245 memcpy(t3->data, buf3_f32, ggml_nbytes(t3));
246
247 struct ggml_tensor * m3 = ggml_map_custom3(ctx, t1, t2, t3, custom3, 1, g_userdata);
248
249 struct ggml_cgraph * graph = ggml_new_graph(ctx);
250 ggml_build_forward_expand(graph, m3);
251
252 ggml_graph_compute_with_ctx(ctx, graph, 4);
253
254 const float * output = ggml_get_data_f32(m3);
255
256 for (int i = 0; i < ggml_nelements(m3); ++i) {
257 GGML_ASSERT(output[i] == buf1_f32[i] + buf2_f32[i] + buf3_f32[i]);
258 }
259
260 GGML_ASSERT(g_custom3_count == 1);
261
262 ggml_free(ctx);
263 }
264
265 // custom
266 {
267 struct ggml_context * ctx = make_ctx();
268 struct ggml_tensor * t1 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
269 struct ggml_tensor * t2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
270 struct ggml_tensor * t3 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
271 struct ggml_tensor * t4 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
272 struct ggml_tensor * t5 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
273 memcpy(t1->data, buf1_f32, ggml_nbytes(t1));
274 memcpy(t2->data, buf2_f32, ggml_nbytes(t2));
275 memcpy(t3->data, buf3_f32, ggml_nbytes(t3));
276 memcpy(t4->data, buf1_f32, ggml_nbytes(t4));
277 memcpy(t5->data, buf2_f32, ggml_nbytes(t5));
278
279 struct ggml_tensor * args[] = {
280 t1, t2, t3, t4, t5,
281 };
282
283 struct ggml_tensor * m4 = ggml_custom_4d(ctx, GGML_TYPE_I32, 10, 2, 1, 1, args, sizeof(args)/sizeof(args[0]), custom, GGML_N_TASKS_MAX, g_userdata);
284
285 struct ggml_cgraph * graph = ggml_new_graph(ctx);
286 ggml_build_forward_expand(graph, m4);
287
288 ggml_graph_compute_with_ctx(ctx, graph, 4);
289
290 const int32_t * output = (const int32_t *) ggml_get_data(m4);
291
292 for (int i = 0; i < ggml_nelements(m4); ++i) {
293 GGML_ASSERT(output[i] == buf1_f32[i] + buf2_f32[i] * buf3_f32[i] - buf1_f32[i] * buf2_f32[i]);
294 }
295
296 ggml_free(ctx);
297 }
298
299 return 0;
300}