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 / examples / mnist / mnist-common.cpp

496 lines · 20278 bytes Raw Blame History
1#include "ggml.h"
2#include "ggml-alloc.h"
3#include "ggml-backend.h"
4#include "ggml-opt.h"
5
6#include "mnist-common.h"
7
8#include <algorithm>
9#include <cmath>
10#include <cstdio>
11#include <cstring>
12#include <cstdint>
13#include <fstream>
14#include <random>
15#include <string>
16#include <utility>
17
18bool mnist_image_load(const std::string & fname, ggml_opt_dataset_t dataset) {
19 auto fin = std::ifstream(fname, std::ios::binary);
20 if (!fin) {
21 fprintf(stderr, "failed to open images file %s\n", fname.c_str());
22 return false;
23 }
24 fin.seekg(16);
25
26 uint8_t image[MNIST_NINPUT];
27 struct ggml_tensor * images = ggml_opt_dataset_data(dataset);
28 float * buf = ggml_get_data_f32(images);
29
30 GGML_ASSERT(images->ne[0] == MNIST_NINPUT);
31 for (int64_t iex = 0; iex < images->ne[1]; ++iex) {
32 fin.read((char *) image, sizeof(image));
33
34 for (int64_t i = 0; i < MNIST_NINPUT; ++i) {
35 buf[iex*MNIST_NINPUT + i] = image[i] / 255.0f; // Normalize to [0, 1]
36 }
37 }
38
39 return true;
40}
41
42void mnist_image_print(FILE * stream, ggml_opt_dataset_t dataset, const int iex) {
43 struct ggml_tensor * images = ggml_opt_dataset_data(dataset);
44 GGML_ASSERT(images->ne[0] == MNIST_NINPUT);
45 GGML_ASSERT(iex < images->ne[1]);
46 const float * image = ggml_get_data_f32(images) + iex*MNIST_NINPUT;
47
48 for (int64_t row = 0; row < MNIST_HW; row++) {
49 for (int64_t col = 0; col < MNIST_HW; col++) {
50 const int rgb = roundf(255.0f * image[row*MNIST_HW + col]);
51#ifdef _WIN32
52 fprintf(stream, "%s", rgb >= 220 ? "##" : "__"); // Represented via text.
53#else
54 fprintf(stream, "\033[48;2;%d;%d;%dm \033[0m", rgb, rgb, rgb); // Represented via colored blocks.
55#endif // _WIN32
56 }
57 fprintf(stream, "\n");
58 }
59}
60
61bool mnist_label_load(const std::string & fname, ggml_opt_dataset_t dataset) {
62 auto fin = std::ifstream(fname, std::ios::binary);
63 if (!fin) {
64 fprintf(stderr, "failed to open labels file %s\n", fname.c_str());
65 return 0;
66 }
67 fin.seekg(8);
68
69 uint8_t label;
70 struct ggml_tensor * labels = ggml_opt_dataset_labels(dataset);
71 float * buf = ggml_get_data_f32(labels);
72
73 GGML_ASSERT(labels->ne[0] == MNIST_NCLASSES);
74 for (int64_t iex = 0; iex < labels->ne[1]; ++iex) {
75 fin.read((char *) &label, sizeof(label));
76
77 for (int64_t i = 0; i < MNIST_NCLASSES; ++i) {
78 buf[iex*MNIST_NCLASSES + i] = i == label ? 1.0f : 0.0f;
79 }
80 }
81
82 return true;
83}
84
85// Temporary util function for loading data from GGUF to a backend != CPU until GGML itself provides this functionality:
86bool load_from_gguf(const char * fname, struct ggml_context * ctx_ggml, struct gguf_context * ctx_gguf) {
87 FILE * f = ggml_fopen(fname, "rb");
88 if (!f) {
89 return false;
90 }
91
92 const size_t buf_size = 4*1024*1024;
93 void * buf = malloc(buf_size);
94
95 const int n_tensors = gguf_get_n_tensors(ctx_gguf);
96 for (int i = 0; i < n_tensors; i++) {
97 const char * name = gguf_get_tensor_name(ctx_gguf, i);
98
99 struct ggml_tensor * tensor = ggml_get_tensor(ctx_ggml, name);
100 if (!tensor) {
101 continue;
102 }
103
104 const size_t offs = gguf_get_data_offset(ctx_gguf) + gguf_get_tensor_offset(ctx_gguf, i);
105
106 if (fseek(f, offs, SEEK_SET) != 0) {
107 fclose(f);
108 free(buf);
109 return false;
110 }
111
112 const size_t nbytes = ggml_nbytes(tensor);
113 for (size_t pos = 0; pos < nbytes; pos += buf_size) {
114 const size_t nbytes_cpy = buf_size < nbytes - pos ? buf_size : nbytes - pos;
115
116 if (fread(buf, 1, nbytes_cpy, f) != nbytes_cpy) {
117 fclose(f);
118 free(buf);
119 return false;
120 }
121
122 ggml_backend_tensor_set(tensor, buf, pos, nbytes_cpy);
123 }
124 }
125
126 fclose(f);
127 free(buf);
128 return true;
129}
130
131mnist_model mnist_model_init_from_file(const std::string & fname, const std::string & backend, const int nbatch_logical, const int nbatch_physical) {
132 mnist_model model(backend, nbatch_logical, nbatch_physical);
133 fprintf(stderr, "%s: loading model weights from '%s'\n", __func__, fname.c_str());
134
135 struct gguf_context * ctx;
136 {
137 struct gguf_init_params params = {
138 /*.no_alloc =*/ true,
139 /*.ctx =*/ &model.ctx_gguf,
140 };
141 ctx = gguf_init_from_file(fname.c_str(), params);
142 if (!ctx) {
143 fprintf(stderr, "%s: gguf_init_from_file() failed\n", __func__);
144 exit(1);
145 }
146 }
147 model.arch = gguf_get_val_str(ctx, gguf_find_key(ctx, "general.architecture"));
148 fprintf(stderr, "%s: model arch is %s\n", __func__, model.arch.c_str());
149
150 if (model.arch == "mnist-fc") {
151 model.fc1_weight = ggml_get_tensor(model.ctx_gguf, "fc1.weight");
152 GGML_ASSERT(model.fc1_weight->ne[0] == MNIST_NINPUT);
153 GGML_ASSERT(model.fc1_weight->ne[1] == MNIST_NHIDDEN);
154 GGML_ASSERT(model.fc1_weight->ne[2] == 1);
155 GGML_ASSERT(model.fc1_weight->ne[3] == 1);
156
157 model.fc1_bias = ggml_get_tensor(model.ctx_gguf, "fc1.bias");
158 GGML_ASSERT(model.fc1_bias->ne[0] == MNIST_NHIDDEN);
159 GGML_ASSERT(model.fc1_bias->ne[1] == 1);
160 GGML_ASSERT(model.fc1_bias->ne[2] == 1);
161 GGML_ASSERT(model.fc1_bias->ne[3] == 1);
162
163 model.fc2_weight = ggml_get_tensor(model.ctx_gguf, "fc2.weight");
164 GGML_ASSERT(model.fc2_weight->ne[0] == MNIST_NHIDDEN);
165 GGML_ASSERT(model.fc2_weight->ne[1] == MNIST_NCLASSES);
166 GGML_ASSERT(model.fc2_weight->ne[2] == 1);
167 GGML_ASSERT(model.fc2_weight->ne[3] == 1);
168
169 model.fc2_bias = ggml_get_tensor(model.ctx_gguf, "fc2.bias");
170 GGML_ASSERT(model.fc2_bias->ne[0] == MNIST_NCLASSES);
171 GGML_ASSERT(model.fc2_bias->ne[1] == 1);
172 GGML_ASSERT(model.fc2_bias->ne[2] == 1);
173 GGML_ASSERT(model.fc2_bias->ne[3] == 1);
174 } else if (model.arch == "mnist-cnn") {
175 model.conv1_kernel = ggml_get_tensor(model.ctx_gguf, "conv1.kernel");
176 GGML_ASSERT(model.conv1_kernel->type == GGML_TYPE_F32);
177 GGML_ASSERT(model.conv1_kernel->ne[0] == 3);
178 GGML_ASSERT(model.conv1_kernel->ne[1] == 3);
179 GGML_ASSERT(model.conv1_kernel->ne[2] == 1);
180 GGML_ASSERT(model.conv1_kernel->ne[3] == MNIST_CNN_NCB);
181
182 model.conv1_bias = ggml_get_tensor(model.ctx_gguf, "conv1.bias");
183 GGML_ASSERT(model.conv1_bias->type == GGML_TYPE_F32);
184 GGML_ASSERT(model.conv1_bias->ne[0] == 1);
185 GGML_ASSERT(model.conv1_bias->ne[1] == 1);
186 GGML_ASSERT(model.conv1_bias->ne[2] == MNIST_CNN_NCB);
187 GGML_ASSERT(model.conv1_bias->ne[3] == 1);
188
189 model.conv2_kernel = ggml_get_tensor(model.ctx_gguf, "conv2.kernel");
190 GGML_ASSERT(model.conv2_kernel->type == GGML_TYPE_F32);
191 GGML_ASSERT(model.conv2_kernel->ne[0] == 3);
192 GGML_ASSERT(model.conv2_kernel->ne[1] == 3);
193 GGML_ASSERT(model.conv2_kernel->ne[2] == MNIST_CNN_NCB);
194 GGML_ASSERT(model.conv2_kernel->ne[3] == MNIST_CNN_NCB*2);
195
196 model.conv2_bias = ggml_get_tensor(model.ctx_gguf, "conv2.bias");
197 GGML_ASSERT(model.conv2_bias->type == GGML_TYPE_F32);
198 GGML_ASSERT(model.conv2_bias->ne[0] == 1);
199 GGML_ASSERT(model.conv2_bias->ne[1] == 1);
200 GGML_ASSERT(model.conv2_bias->ne[2] == MNIST_CNN_NCB*2);
201 GGML_ASSERT(model.conv2_bias->ne[3] == 1);
202
203 model.dense_weight = ggml_get_tensor(model.ctx_gguf, "dense.weight");
204 GGML_ASSERT(model.dense_weight->type == GGML_TYPE_F32);
205 GGML_ASSERT(model.dense_weight->ne[0] == (MNIST_HW/4)*(MNIST_HW/4)*(MNIST_CNN_NCB*2));
206 GGML_ASSERT(model.dense_weight->ne[1] == MNIST_NCLASSES);
207 GGML_ASSERT(model.dense_weight->ne[2] == 1);
208 GGML_ASSERT(model.dense_weight->ne[3] == 1);
209
210 model.dense_bias = ggml_get_tensor(model.ctx_gguf, "dense.bias");
211 GGML_ASSERT(model.dense_bias->type == GGML_TYPE_F32);
212 GGML_ASSERT(model.dense_bias->ne[0] == MNIST_NCLASSES);
213 GGML_ASSERT(model.dense_bias->ne[1] == 1);
214 GGML_ASSERT(model.dense_bias->ne[2] == 1);
215 GGML_ASSERT(model.dense_bias->ne[3] == 1);
216 } else {
217 fprintf(stderr, "%s: unknown model arch: %s\n", __func__, model.arch.c_str());
218 }
219
220 model.buf_gguf = ggml_backend_alloc_ctx_tensors(model.ctx_gguf, model.backends[0]);
221
222 if(!load_from_gguf(fname.c_str(), model.ctx_gguf, ctx)) {
223 fprintf(stderr, "%s: loading weights from %s failed\n", __func__, fname.c_str());
224 exit(1);
225 }
226
227 // The space in ctx_gguf exactly fits the model weights,
228 // the images (which also need to be statically allocated) need to be put in a different context.
229
230 model.images = ggml_new_tensor_2d(model.ctx_static, GGML_TYPE_F32, MNIST_NINPUT, nbatch_physical);
231
232 ggml_set_name(model.images, "images");
233 ggml_set_input(model.images);
234
235 model.buf_static = ggml_backend_alloc_ctx_tensors(model.ctx_static, model.backends[0]);
236
237 fprintf(stderr, "%s: successfully loaded weights from %s\n", __func__, fname.c_str());
238 return model;
239}
240
241mnist_model mnist_model_init_random(const std::string & arch, const std::string & backend, const int nbatch_logical, const int nbatch_physical) {
242 mnist_model model(backend, nbatch_logical, nbatch_physical);
243 model.arch = arch;
244
245 std::random_device rd{};
246 std::mt19937 gen{rd()};
247 std::normal_distribution<float> nd{0.0f, 1e-2f};
248 std::vector<ggml_tensor *> init_tensors;
249
250 if (model.arch == "mnist-fc") {
251 fprintf(stderr, "%s: initializing random weights for a fully connected model\n", __func__);
252
253 model.fc1_weight = ggml_new_tensor_2d(model.ctx_static, GGML_TYPE_F32, MNIST_NINPUT, MNIST_NHIDDEN);
254 model.fc1_bias = ggml_new_tensor_1d(model.ctx_static, GGML_TYPE_F32, MNIST_NHIDDEN);
255 model.fc2_weight = ggml_new_tensor_2d(model.ctx_static, GGML_TYPE_F32, MNIST_NHIDDEN, MNIST_NCLASSES);
256 model.fc2_bias = ggml_new_tensor_1d(model.ctx_static, GGML_TYPE_F32, MNIST_NCLASSES);
257
258 ggml_set_name(model.fc1_weight, "fc1.weight");
259 ggml_set_name(model.fc1_bias, "fc1.bias");
260 ggml_set_name(model.fc2_weight, "fc2.weight");
261 ggml_set_name(model.fc2_bias, "fc2.bias");
262
263 init_tensors.push_back(model.fc1_weight);
264 init_tensors.push_back(model.fc1_bias);
265 init_tensors.push_back(model.fc2_weight);
266 init_tensors.push_back(model.fc2_bias);
267 } else if (model.arch == "mnist-cnn") {
268 model.conv1_kernel = ggml_new_tensor_4d(model.ctx_static, GGML_TYPE_F32, 3, 3, 1, MNIST_CNN_NCB);
269 model.conv1_bias = ggml_new_tensor_3d(model.ctx_static, GGML_TYPE_F32, 1, 1, MNIST_CNN_NCB);
270 model.conv2_kernel = ggml_new_tensor_4d(model.ctx_static, GGML_TYPE_F32, 3, 3, MNIST_CNN_NCB, MNIST_CNN_NCB*2);
271 model.conv2_bias = ggml_new_tensor_3d(model.ctx_static, GGML_TYPE_F32, 1, 1, MNIST_CNN_NCB*2);
272 model.dense_weight = ggml_new_tensor_2d(model.ctx_static, GGML_TYPE_F32, (MNIST_HW/4)*(MNIST_HW/4)*(MNIST_CNN_NCB*2), MNIST_NCLASSES);
273 model.dense_bias = ggml_new_tensor_1d(model.ctx_static, GGML_TYPE_F32, MNIST_NCLASSES);
274
275 ggml_set_name(model.conv1_kernel, "conv1.kernel");
276 ggml_set_name(model.conv1_bias, "conv1.bias");
277 ggml_set_name(model.conv2_kernel, "conv2.kernel");
278 ggml_set_name(model.conv2_bias, "conv2.bias");
279 ggml_set_name(model.dense_weight, "dense.weight");
280 ggml_set_name(model.dense_bias, "dense.bias");
281
282 init_tensors.push_back(model.conv1_kernel);
283 init_tensors.push_back(model.conv1_bias);
284 init_tensors.push_back(model.conv2_kernel);
285 init_tensors.push_back(model.conv2_bias);
286 init_tensors.push_back(model.dense_weight);
287 init_tensors.push_back(model.dense_bias);
288 } else {
289 fprintf(stderr, "%s: unknown model arch: %s\n", __func__, model.arch.c_str());
290 }
291
292 model.images = ggml_new_tensor_2d(model.ctx_static, GGML_TYPE_F32, MNIST_NINPUT, MNIST_NBATCH_PHYSICAL);
293 ggml_set_name(model.images, "images");
294 ggml_set_input(model.images);
295
296 model.buf_static = ggml_backend_alloc_ctx_tensors(model.ctx_static, model.backends[0]);
297
298 for (ggml_tensor * t : init_tensors) {
299 GGML_ASSERT(t->type == GGML_TYPE_F32);
300 const int64_t ne = ggml_nelements(t);
301 std::vector<float> tmp(ne);
302
303 for (int64_t i = 0; i < ne; ++i) {
304 tmp[i] = nd(gen);
305 }
306 ggml_backend_tensor_set(t, tmp.data(), 0, ggml_nbytes(t));
307 }
308
309 return model;
310}
311
312void mnist_model_build(mnist_model & model) {
313 if (model.arch == "mnist-fc") {
314 ggml_set_param(model.fc1_weight);
315 ggml_set_param(model.fc1_bias);
316 ggml_set_param(model.fc2_weight);
317 ggml_set_param(model.fc2_bias);
318
319 ggml_tensor * fc1 = ggml_relu(model.ctx_compute, ggml_add(model.ctx_compute,
320 ggml_mul_mat(model.ctx_compute, model.fc1_weight, model.images),
321 model.fc1_bias));
322 model.logits = ggml_add(model.ctx_compute,
323 ggml_mul_mat(model.ctx_compute, model.fc2_weight, fc1),
324 model.fc2_bias);
325 } else if (model.arch == "mnist-cnn") {
326 ggml_set_param(model.conv1_kernel);
327 ggml_set_param(model.conv1_bias);
328 ggml_set_param(model.conv2_kernel);
329 ggml_set_param(model.conv2_bias);
330 ggml_set_param(model.dense_weight);
331 ggml_set_param(model.dense_bias);
332
333 struct ggml_tensor * images_2D = ggml_reshape_4d(model.ctx_compute, model.images, MNIST_HW, MNIST_HW, 1, model.images->ne[1]);
334
335 struct ggml_tensor * conv1_out = ggml_relu(model.ctx_compute, ggml_add(model.ctx_compute,
336 ggml_conv_2d(model.ctx_compute, model.conv1_kernel, images_2D, 1, 1, 1, 1, 1, 1),
337 model.conv1_bias));
338 GGML_ASSERT(conv1_out->ne[0] == MNIST_HW);
339 GGML_ASSERT(conv1_out->ne[1] == MNIST_HW);
340 GGML_ASSERT(conv1_out->ne[2] == MNIST_CNN_NCB);
341 GGML_ASSERT(conv1_out->ne[3] == model.nbatch_physical);
342
343 struct ggml_tensor * conv2_in = ggml_pool_2d(model.ctx_compute, conv1_out, GGML_OP_POOL_MAX, 2, 2, 2, 2, 0, 0);
344 GGML_ASSERT(conv2_in->ne[0] == MNIST_HW/2);
345 GGML_ASSERT(conv2_in->ne[1] == MNIST_HW/2);
346 GGML_ASSERT(conv2_in->ne[2] == MNIST_CNN_NCB);
347 GGML_ASSERT(conv2_in->ne[3] == model.nbatch_physical);
348
349 struct ggml_tensor * conv2_out = ggml_relu(model.ctx_compute, ggml_add(model.ctx_compute,
350 ggml_conv_2d(model.ctx_compute, model.conv2_kernel, conv2_in, 1, 1, 1, 1, 1, 1),
351 model.conv2_bias));
352 GGML_ASSERT(conv2_out->ne[0] == MNIST_HW/2);
353 GGML_ASSERT(conv2_out->ne[1] == MNIST_HW/2);
354 GGML_ASSERT(conv2_out->ne[2] == MNIST_CNN_NCB*2);
355 GGML_ASSERT(conv2_out->ne[3] == model.nbatch_physical);
356
357 struct ggml_tensor * dense_in = ggml_pool_2d(model.ctx_compute, conv2_out, GGML_OP_POOL_MAX, 2, 2, 2, 2, 0, 0);
358 GGML_ASSERT(dense_in->ne[0] == MNIST_HW/4);
359 GGML_ASSERT(dense_in->ne[1] == MNIST_HW/4);
360 GGML_ASSERT(dense_in->ne[2] == MNIST_CNN_NCB*2);
361 GGML_ASSERT(dense_in->ne[3] == model.nbatch_physical);
362
363 dense_in = ggml_reshape_2d(model.ctx_compute,
364 ggml_cont(model.ctx_compute, ggml_permute(model.ctx_compute, dense_in, 1, 2, 0, 3)),
365 (MNIST_HW/4)*(MNIST_HW/4)*(MNIST_CNN_NCB*2), model.nbatch_physical);
366 GGML_ASSERT(dense_in->ne[0] == (MNIST_HW/4)*(MNIST_HW/4)*(MNIST_CNN_NCB*2));
367 GGML_ASSERT(dense_in->ne[1] == model.nbatch_physical);
368 GGML_ASSERT(dense_in->ne[2] == 1);
369 GGML_ASSERT(dense_in->ne[3] == 1);
370
371 model.logits = ggml_add(model.ctx_compute, ggml_mul_mat(model.ctx_compute, model.dense_weight, dense_in), model.dense_bias);
372 } else {
373 GGML_ASSERT(false);
374 }
375
376 ggml_set_name(model.logits, "logits");
377 ggml_set_output(model.logits);
378 GGML_ASSERT(model.logits->type == GGML_TYPE_F32);
379 GGML_ASSERT(model.logits->ne[0] == MNIST_NCLASSES);
380 GGML_ASSERT(model.logits->ne[1] == model.nbatch_physical);
381 GGML_ASSERT(model.logits->ne[2] == 1);
382 GGML_ASSERT(model.logits->ne[3] == 1);
383}
384
385ggml_opt_result_t mnist_model_eval(mnist_model & model, ggml_opt_dataset_t dataset) {
386 ggml_opt_result_t result = ggml_opt_result_init();
387
388 ggml_opt_params params = ggml_opt_default_params(model.backend_sched, GGML_OPT_LOSS_TYPE_CROSS_ENTROPY);
389 params.ctx_compute = model.ctx_compute;
390 params.inputs = model.images;
391 params.outputs = model.logits;
392 params.build_type = GGML_OPT_BUILD_TYPE_FORWARD;
393 ggml_opt_context_t opt_ctx = ggml_opt_init(params);
394
395 {
396 const int64_t t_start_us = ggml_time_us();
397
398 ggml_opt_epoch(opt_ctx, dataset, nullptr, result, /*idata_split =*/ 0, nullptr, nullptr);
399
400 const int64_t t_total_us = ggml_time_us() - t_start_us;
401 const double t_total_ms = 1e-3*t_total_us;
402 const int nex = ggml_opt_dataset_data(dataset)->ne[1];
403 fprintf(stderr, "%s: model evaluation on %d images took %.2lf ms, %.2lf us/image\n",
404 __func__, nex, t_total_ms, (double) t_total_us/nex);
405 }
406
407 ggml_opt_free(opt_ctx);
408
409 return result;
410}
411
412void mnist_model_train(mnist_model & model, ggml_opt_dataset_t dataset, const int nepoch, const float val_split) {
413 ggml_opt_fit(model.backend_sched, model.ctx_compute, model.images, model.logits, dataset,
414 GGML_OPT_LOSS_TYPE_CROSS_ENTROPY, GGML_OPT_OPTIMIZER_TYPE_ADAMW, ggml_opt_get_default_optimizer_params, nepoch, model.nbatch_logical, val_split, false);
415}
416
417void mnist_model_save(mnist_model & model, const std::string & fname) {
418 printf("%s: saving model to '%s'\n", __func__, fname.c_str());
419
420 struct ggml_context * ggml_ctx;
421 {
422 struct ggml_init_params params = {
423 /*.mem_size =*/ 100 * 1024*1024,
424 /*.mem_buffer =*/ NULL,
425 /*.no_alloc =*/ false,
426 };
427 ggml_ctx = ggml_init(params);
428 }
429
430 gguf_context * gguf_ctx = gguf_init_empty();
431 gguf_set_val_str(gguf_ctx, "general.architecture", model.arch.c_str());
432
433 std::vector<struct ggml_tensor *> weights;
434 if (model.arch == "mnist-fc") {
435 weights = {model.fc1_weight, model.fc1_bias, model.fc2_weight, model.fc2_bias};
436 } else if (model.arch == "mnist-cnn") {
437 weights = {model.conv1_kernel, model.conv1_bias, model.conv2_kernel, model.conv2_bias, model.dense_weight, model.dense_bias};
438 } else {
439 GGML_ASSERT(false);
440 }
441 for (struct ggml_tensor * t : weights) {
442 struct ggml_tensor * copy = ggml_dup_tensor(ggml_ctx, t);
443 ggml_set_name(copy, t->name);
444 ggml_backend_tensor_get(t, copy->data, 0, ggml_nbytes(t));
445 gguf_add_tensor(gguf_ctx, copy);
446 }
447 gguf_write_to_file(gguf_ctx, fname.c_str(), false);
448
449 ggml_free(ggml_ctx);
450 gguf_free(gguf_ctx);
451}
452
453#ifdef __cplusplus
454extern "C" {
455#endif
456
457int wasm_eval(uint8_t * digitPtr) {
458 std::vector<float> digit(digitPtr, digitPtr + MNIST_NINPUT);
459
460 ggml_opt_dataset_t dataset = ggml_opt_dataset_init(GGML_TYPE_F32, GGML_TYPE_F32, MNIST_NINPUT, MNIST_NCLASSES, 1, 1);
461 struct ggml_tensor * data = ggml_opt_dataset_data(dataset);
462
463 float * buf = ggml_get_data_f32(data);
464 for (int i = 0; i < MNIST_NINPUT; ++i) {
465 buf[i] = digitPtr[i] / 255.0f;
466 }
467 ggml_set_zero(ggml_opt_dataset_labels(dataset)); // The labels are not needed.
468
469 mnist_model model = mnist_model_init_from_file("mnist-f32.gguf", "CPU", /*nbatch_logical =*/ 1, /*nbatch_physical =*/ 1);
470 mnist_model_build(model);
471 ggml_opt_result_t result = mnist_model_eval(model, dataset);
472
473 int32_t pred;
474 ggml_opt_result_pred(result, &pred);
475
476 return pred;
477}
478
479int wasm_random_digit(char * digitPtr) {
480 auto fin = std::ifstream("t10k-images-idx3-ubyte", std::ios::binary);
481 if (!fin) {
482 fprintf(stderr, "failed to open digits file\n");
483 return 0;
484 }
485 srand(time(NULL));
486
487 // Seek to a random digit: 16-byte header + 28*28 * (random 0 - 10000)
488 fin.seekg(16 + MNIST_NINPUT * (rand() % MNIST_NTEST));
489 fin.read(digitPtr, MNIST_NINPUT);
490
491 return 1;
492}
493
494#ifdef __cplusplus
495}
496#endif