1#include "ggml.h"
2#include "ggml-backend.h"
3#include "../ggml/src/ggml-impl.h"
4#include "gguf.h"
5
6#include <algorithm>
7#include <array>
8#include <cmath>
9#include <cstdint>
10#include <cstdio>
11#include <random>
12#include <string>
13#include <vector>
14
15constexpr int offset_has_kv = 1000;
16constexpr int offset_has_tensors = 2000;
17constexpr int offset_has_data = 3000;
18
19enum handcrafted_file_type {
20 HANDCRAFTED_HEADER_BAD_MAGIC = 10,
21 HANDCRAFTED_HEADER_BAD_VERSION_0 = 15,
22 HANDCRAFTED_HEADER_BAD_VERSION_1 = 20,
23 HANDCRAFTED_HEADER_BAD_VERSION_FUTURE = 30,
24 HANDCRAFTED_HEADER_BAD_N_TENSORS = 40,
25 HANDCRAFTED_HEADER_BAD_N_KV = 50,
26 HANDCRAFTED_HEADER_EMPTY = 800,
27
28 HANDCRAFTED_KV_BAD_KEY_SIZE = 10 + offset_has_kv,
29 HANDCRAFTED_KV_BAD_TYPE = 20 + offset_has_kv,
30 // HANDCRAFTED_KV_BAD_VALUE_SIZE = 30 + offset_has_kv, // removed because it can result in allocations > 1 TB (default sanitizer limit)
31 HANDCRAFTED_KV_DUPLICATE_KEY = 40 + offset_has_kv,
32 HANDCRAFTED_KV_BAD_ALIGN = 50 + offset_has_kv,
33 HANDCRAFTED_KV_SUCCESS = 800 + offset_has_kv,
34
35 HANDCRAFTED_TENSORS_BAD_NAME_SIZE = 10 + offset_has_tensors,
36 HANDCRAFTED_TENSORS_BAD_N_DIMS = 20 + offset_has_tensors,
37 HANDCRAFTED_TENSORS_BAD_SHAPE = 30 + offset_has_tensors,
38 HANDCRAFTED_TENSORS_NE_TOO_BIG = 40 + offset_has_tensors,
39 HANDCRAFTED_TENSORS_NBYTES_TOO_BIG = 45 + offset_has_tensors,
40 HANDCRAFTED_TENSORS_BAD_TYPE = 50 + offset_has_tensors,
41 HANDCRAFTED_TENSORS_BAD_OFFSET = 60 + offset_has_tensors,
42 HANDCRAFTED_TENSORS_DUPLICATE_NAME = 70 + offset_has_tensors,
43 HANDCRAFTED_TENSORS_BAD_ALIGN = 75 + offset_has_tensors,
44 HANDCRAFTED_TENSORS_INCONSISTENT_ALIGN = 80 + offset_has_tensors,
45 HANDCRAFTED_TENSORS_SUCCESS = 800 + offset_has_tensors,
46 HANDCRAFTED_TENSORS_CUSTOM_ALIGN = 810 + offset_has_tensors,
47
48 HANDCRAFTED_DATA_NOT_ENOUGH_DATA = 10 + offset_has_data,
49 HANDCRAFTED_DATA_BAD_ALIGN = 15 + offset_has_data,
50 HANDCRAFTED_DATA_INCONSISTENT_ALIGN = 20 + offset_has_data,
51 HANDCRAFTED_DATA_SUCCESS = 800 + offset_has_data,
52 HANDCRAFTED_DATA_CUSTOM_ALIGN = 810 + offset_has_data,
53};
54
55static std::string handcrafted_file_type_name(const enum handcrafted_file_type hft) {
56 switch (hft) {
57 case HANDCRAFTED_HEADER_BAD_MAGIC: return "HEADER_BAD_MAGIC";
58 case HANDCRAFTED_HEADER_BAD_VERSION_0: return "HEADER_BAD_VERSION_0";
59 case HANDCRAFTED_HEADER_BAD_VERSION_1: return "HEADER_BAD_VERSION_1";
60 case HANDCRAFTED_HEADER_BAD_VERSION_FUTURE: return "HEADER_BAD_VERSION_FUTURE";
61 case HANDCRAFTED_HEADER_BAD_N_KV: return "HEADER_BAD_N_KV";
62 case HANDCRAFTED_HEADER_BAD_N_TENSORS: return "HEADER_BAD_N_TENSORS";
63 case HANDCRAFTED_HEADER_EMPTY: return "HEADER_EMPTY";
64
65 case HANDCRAFTED_KV_BAD_KEY_SIZE: return "KV_BAD_KEY_SIZE";
66 case HANDCRAFTED_KV_BAD_TYPE: return "KV_BAD_TYPE";
67 case HANDCRAFTED_KV_DUPLICATE_KEY: return "KV_DUPLICATE_KEY";
68 case HANDCRAFTED_KV_BAD_ALIGN: return "KV_BAD_ALIGN";
69 case HANDCRAFTED_KV_SUCCESS: return "KV_RANDOM_KV";
70
71 case HANDCRAFTED_TENSORS_BAD_NAME_SIZE: return "TENSORS_BAD_NAME_SIZE";
72 case HANDCRAFTED_TENSORS_BAD_N_DIMS: return "TENSORS_BAD_N_DIMS";
73 case HANDCRAFTED_TENSORS_BAD_SHAPE: return "TENSORS_BAD_SHAPE";
74 case HANDCRAFTED_TENSORS_NE_TOO_BIG: return "TENSORS_NE_TOO_BIG";
75 case HANDCRAFTED_TENSORS_NBYTES_TOO_BIG: return "TENSORS_NBYTES_TOO_BIG";
76 case HANDCRAFTED_TENSORS_BAD_TYPE: return "TENSORS_BAD_TYPE";
77 case HANDCRAFTED_TENSORS_BAD_OFFSET: return "TENSORS_BAD_OFFSET";
78 case HANDCRAFTED_TENSORS_DUPLICATE_NAME: return "TENSORS_DUPLICATE_NAME";
79 case HANDCRAFTED_TENSORS_BAD_ALIGN: return "TENSORS_BAD_ALIGN";
80 case HANDCRAFTED_TENSORS_INCONSISTENT_ALIGN: return "TENSORS_INCONSISTENT_ALIGN";
81 case HANDCRAFTED_TENSORS_SUCCESS: return "TENSORS_SUCCESS";
82 case HANDCRAFTED_TENSORS_CUSTOM_ALIGN: return "TENSORS_CUSTOM_ALIGN";
83
84 case HANDCRAFTED_DATA_NOT_ENOUGH_DATA: return "DATA_NOT_ENOUGH_DATA";
85 case HANDCRAFTED_DATA_BAD_ALIGN: return "DATA_BAD_ALIGN";
86 case HANDCRAFTED_DATA_INCONSISTENT_ALIGN: return "DATA_INCONSISTENT_ALIGN";
87 case HANDCRAFTED_DATA_SUCCESS: return "DATA_SUCCESS";
88 case HANDCRAFTED_DATA_CUSTOM_ALIGN: return "DATA_CUSTOM_ALIGN";
89 }
90 GGML_ABORT("fatal error");
91}
92
93static bool expect_context_not_null(const enum handcrafted_file_type hft) {
94 if (hft < offset_has_kv) {
95 return hft >= HANDCRAFTED_HEADER_EMPTY;
96 }
97 if (hft < offset_has_tensors) {
98 return hft >= HANDCRAFTED_KV_SUCCESS;
99 }
100 if (hft < offset_has_data) {
101 return hft >= HANDCRAFTED_TENSORS_SUCCESS;
102 }
103 return hft >= HANDCRAFTED_DATA_SUCCESS;
104}
105
106typedef std::pair<enum ggml_type, std::array<int64_t, GGML_MAX_DIMS>> tensor_config_t;
107
108static std::vector<tensor_config_t> get_tensor_configs(std::mt19937 & rng) {
109 std::vector<tensor_config_t> tensor_configs;
110 tensor_configs.reserve(100);
111
112 for (int i = 0; i < 100; ++i) {
113 const enum ggml_type type = ggml_type(rng() % GGML_TYPE_COUNT);
114 if (ggml_type_size(type) == 0) {
115 continue;
116 }
117
118 std::array<int64_t, GGML_MAX_DIMS> shape = {1, 1, 1, 1};
119 shape[0] = (1 + rng() % 10) * ggml_blck_size(type);
120 const int n_dims = 1 + rng() % GGML_MAX_DIMS;
121 for (int i = 1; i < n_dims; ++i) {
122 shape[i] = 1 + rng() % 10;
123 }
124
125 tensor_configs.push_back(std::make_pair(type, shape));
126 }
127
128 return tensor_configs;
129}
130
131static std::vector<std::pair<enum gguf_type, enum gguf_type>> get_kv_types(std::mt19937 rng) {
132 std::vector<std::pair<enum gguf_type, enum gguf_type>> kv_types;
133 kv_types.reserve(100);
134
135 for (int i = 0; i < 100; ++i) {
136 const gguf_type type = gguf_type(rng() % GGUF_TYPE_COUNT);
137
138 if (type == GGUF_TYPE_ARRAY) {
139 const gguf_type type_arr = gguf_type(rng() % GGUF_TYPE_COUNT);
140 if (type_arr == GGUF_TYPE_ARRAY) {
141 continue;
142 }
143 kv_types.push_back(std::make_pair(type, type_arr));
144 continue;
145 }
146
147 kv_types.push_back(std::make_pair(type, gguf_type(-1)));
148 }
149 std::shuffle(kv_types.begin(), kv_types.end(), rng);
150
151 return kv_types;
152}
153
154template <typename T>
155static void helper_write(FILE * file, const T & val) {
156 GGML_ASSERT(fwrite(&val, 1, sizeof(val), file) == sizeof(val));
157}
158
159static void helper_write(FILE * file, const void * data, const size_t nbytes) {
160 GGML_ASSERT(fwrite(data, 1, nbytes, file) == nbytes);
161}
162
163static FILE * get_handcrafted_file(const unsigned int seed, const enum handcrafted_file_type hft, const int extra_bytes = 0) {
164 FILE * file = tmpfile();
165
166 if (!file) {
167 return file;
168 }
169
170 std::mt19937 rng(seed);
171 uint32_t alignment = GGUF_DEFAULT_ALIGNMENT;
172
173 if (hft == HANDCRAFTED_HEADER_BAD_MAGIC) {
174 const char bad_magic[4] = {'F', 'U', 'G', 'G'};
175 helper_write(file, bad_magic, sizeof(bad_magic));
176 } else {
177 helper_write(file, GGUF_MAGIC, 4);
178 }
179
180 if (hft == HANDCRAFTED_HEADER_BAD_VERSION_0) {
181 const uint32_t version = 0;
182 helper_write(file, version);
183 } else if (hft == HANDCRAFTED_HEADER_BAD_VERSION_1) {
184 const uint32_t version = 1;
185 helper_write(file, version);
186 } else if (hft == HANDCRAFTED_HEADER_BAD_VERSION_FUTURE) {
187 const uint32_t version = GGUF_VERSION + 1;
188 helper_write(file, version);
189 } else {
190 const uint32_t version = GGUF_VERSION;
191 helper_write(file, version);
192 }
193
194 std::vector<tensor_config_t> tensor_configs;
195 if (hft >= offset_has_tensors) {
196 tensor_configs = get_tensor_configs(rng);
197 }
198
199 if (hft == HANDCRAFTED_HEADER_BAD_N_TENSORS) {
200 const uint64_t n_tensors = -1;
201 helper_write(file, n_tensors);
202 } else {
203 const uint64_t n_tensors = tensor_configs.size();
204 helper_write(file, n_tensors);
205 }
206
207 std::vector<std::pair<enum gguf_type, enum gguf_type>> kv_types;
208 if (hft >= offset_has_kv) {
209 kv_types = get_kv_types(rng);
210 }
211 {
212 uint64_t n_kv = kv_types.size();
213 if (hft == HANDCRAFTED_KV_BAD_ALIGN ||
214 hft == HANDCRAFTED_TENSORS_BAD_ALIGN || hft == HANDCRAFTED_TENSORS_CUSTOM_ALIGN ||
215 hft == HANDCRAFTED_DATA_BAD_ALIGN || hft == HANDCRAFTED_DATA_CUSTOM_ALIGN) {
216
217 n_kv += 1;
218 } else if (hft == HANDCRAFTED_HEADER_BAD_N_KV) {
219 n_kv = -1;
220 }
221 helper_write(file, n_kv);
222 }
223
224 if (hft < offset_has_kv) {
225 while (ftell(file) % alignment != 0) {
226 const char pad = 0;
227 helper_write(file, pad);
228 }
229
230 for (int i = 0; i < extra_bytes; ++i) {
231 const char tmp = 0;
232 helper_write(file, tmp);
233 }
234 rewind(file);
235 return file;
236 }
237
238 for (int i = 0; i < int(kv_types.size()); ++i) {
239 const enum gguf_type type = gguf_type(hft == HANDCRAFTED_KV_BAD_TYPE ? GGUF_TYPE_COUNT : kv_types[i].first);
240 const enum gguf_type type_arr = gguf_type(hft == HANDCRAFTED_KV_BAD_TYPE ? GGUF_TYPE_COUNT : kv_types[i].second);
241
242 const std::string key = "my_key_" + std::to_string((hft == HANDCRAFTED_KV_DUPLICATE_KEY ? i/2 : i));
243
244 if (hft == HANDCRAFTED_KV_BAD_KEY_SIZE) {
245 const uint64_t n = -1;
246 helper_write(file, n);
247 } else {
248 const uint64_t n = key.length();
249 helper_write(file, n);
250 }
251 helper_write(file, key.data(), key.length());
252
253 {
254 const int32_t type32 = int32_t(type);
255 helper_write(file, type32);
256 }
257
258 uint32_t data[16];
259 for (int j = 0; j < 16; ++j) {
260 data[j] = rng();
261 if (type == GGUF_TYPE_STRING || type_arr == GGUF_TYPE_STRING) {
262 data[j] |= 0x01010101; // avoid random null-termination of string
263 }
264 }
265
266 if (type == GGUF_TYPE_STRING) {
267 const uint64_t n = rng() % sizeof(data);
268 helper_write(file, n);
269 helper_write(file, data, n);
270 continue;
271 }
272
273 if (type == GGUF_TYPE_ARRAY) {
274 {
275 const int32_t type32 = int32_t(type_arr);
276 helper_write(file, type32);
277 }
278 if (type_arr == GGUF_TYPE_STRING) {
279 const uint64_t nstr = rng() % (16 + 1);
280 helper_write(file, nstr);
281 for (uint64_t istr = 0; istr < nstr; ++istr) {
282 const uint64_t n = rng() % (sizeof(uint32_t) + 1);
283 helper_write(file, n);
284 helper_write(file, &data[istr], n);
285 }
286 continue;
287 }
288 const size_t type_size = gguf_type_size(type_arr);
289 const uint64_t n = (rng() % sizeof(data)) / type_size;
290 helper_write(file, n);
291 helper_write(file, &data, n*type_size);
292 continue;
293 }
294
295 helper_write(file, data, hft == HANDCRAFTED_KV_BAD_TYPE ? 1 : gguf_type_size(type));
296 }
297
298 if (hft == HANDCRAFTED_KV_BAD_ALIGN ||
299 hft == HANDCRAFTED_TENSORS_BAD_ALIGN || hft == HANDCRAFTED_TENSORS_CUSTOM_ALIGN ||
300 hft == HANDCRAFTED_DATA_BAD_ALIGN || hft == HANDCRAFTED_DATA_CUSTOM_ALIGN) {
301
302 const uint64_t n = strlen(GGUF_KEY_GENERAL_ALIGNMENT);
303 helper_write(file, n);
304 helper_write(file, GGUF_KEY_GENERAL_ALIGNMENT, n);
305
306 const int32_t type = gguf_type(GGUF_TYPE_UINT32);
307 helper_write(file, type);
308
309 alignment = expect_context_not_null(hft) ? 1 : 13;
310 helper_write(file, alignment);
311 }
312
313 if (hft < offset_has_tensors) {
314 while (ftell(file) % alignment != 0) {
315 const char pad = 0;
316 helper_write(file, pad);
317 }
318
319 for (int i = 0; i < extra_bytes; ++i) {
320 const char tmp = 0;
321 helper_write(file, tmp);
322 }
323 rewind(file);
324 return file;
325 }
326
327 if (hft == HANDCRAFTED_TENSORS_INCONSISTENT_ALIGN || hft == HANDCRAFTED_DATA_INCONSISTENT_ALIGN) {
328 alignment = 1;
329 }
330
331 uint64_t offset = 0;
332 for (int i = 0; i < int(tensor_configs.size()); ++i) {
333 const ggml_type type = hft == HANDCRAFTED_TENSORS_NBYTES_TOO_BIG ? GGML_TYPE_I64 : tensor_configs[i].first;
334 const std::array<int64_t, GGML_MAX_DIMS> shape = tensor_configs[i].second;
335
336 std::string name = "my_tensor";
337 if (hft != HANDCRAFTED_TENSORS_DUPLICATE_NAME) {
338 name += "_" + std::to_string(i);
339 }
340 if (hft == HANDCRAFTED_TENSORS_BAD_NAME_SIZE) {
341 name += "_with_a_very_long_name_which_is_longer_than_what_is_allowed_for_ggml_tensors";
342 GGML_ASSERT(name.length() >= GGML_MAX_NAME);
343 }
344 {
345 const uint64_t n = name.length();
346 helper_write(file, n);
347 }
348 helper_write(file, name.data(), name.length());
349
350 uint32_t n_dims = (hft == HANDCRAFTED_TENSORS_NE_TOO_BIG || hft == HANDCRAFTED_TENSORS_NBYTES_TOO_BIG) ? 2 : 1;
351 for (int i = GGML_MAX_DIMS-1; i >= 1; --i) {
352 if (shape[i] != 1) {
353 n_dims = i + 1;
354 break;
355 }
356 }
357 if (hft == HANDCRAFTED_TENSORS_BAD_N_DIMS) {
358 const uint32_t n_dims_bad = GGML_MAX_DIMS + 1;
359 helper_write(file, n_dims_bad);
360 } else {
361 helper_write(file, n_dims);
362 }
363
364 if (hft == HANDCRAFTED_TENSORS_BAD_SHAPE) {
365 const int64_t bad_dim = -1;
366 for (uint32_t j = 0; j < n_dims; ++j) {
367 helper_write(file, bad_dim);
368 }
369 } else if (hft == HANDCRAFTED_TENSORS_NE_TOO_BIG){
370 const int64_t big_dim = 4*int64_t(INT32_MAX);
371 for (uint32_t j = 0; j < n_dims; ++j) {
372 helper_write(file, big_dim);
373 }
374 } else if (hft == HANDCRAFTED_TENSORS_NBYTES_TOO_BIG){
375 const size_t big_ne = SIZE_MAX/ggml_type_size(type);
376 const int64_t big_dim = GGML_PAD(int64_t(1.01f*std::pow(big_ne, 1.0f/n_dims)) + 1, ggml_blck_size(type));
377 for (uint32_t j = 0; j < n_dims; ++j) {
378 helper_write(file, big_dim);
379 }
380 } else {
381 helper_write(file, shape.data(), n_dims*sizeof(int64_t));
382 }
383
384 {
385 const int32_t type32 = hft == HANDCRAFTED_TENSORS_BAD_TYPE ? GGML_TYPE_COUNT : int32_t(type);
386 helper_write(file, type32);
387 }
388
389 if (hft == HANDCRAFTED_TENSORS_BAD_OFFSET) {
390 const uint64_t bad_offset = -1;
391 helper_write(file, bad_offset);
392 } else {
393 helper_write(file, offset);
394 }
395
396 int64_t ne = shape[0];
397 for (uint32_t i = 1; i < n_dims; ++i) {
398 ne *= shape[i];
399 }
400 offset += GGML_PAD(ggml_row_size(type, ne), alignment);
401 }
402
403 while (ftell(file) % alignment != 0) {
404 const char pad = 0;
405 helper_write(file, pad);
406 }
407
408 if (hft >= offset_has_data) {
409 rng.seed(seed + 1);
410 uint64_t nbytes = offset;
411 if (hft == HANDCRAFTED_DATA_NOT_ENOUGH_DATA) {
412 nbytes -= 1;
413 }
414 for (uint64_t i = 0; i < nbytes; ++i) {
415 const uint8_t random_byte = i % 256;
416 helper_write(file, random_byte);
417 }
418 }
419
420 for (int i = 0; i < extra_bytes; ++i) {
421 const char tmp = 0;
422 helper_write(file, tmp);
423 }
424 rewind(file);
425 return file;
426}
427
428static bool handcrafted_check_header(const gguf_context * gguf_ctx, const unsigned int seed, const bool has_kv, const bool has_tensors, const bool alignment_defined) {
429 if (!gguf_ctx) {
430 return false;
431 }
432
433 std::mt19937 rng(seed);
434
435 std::vector<tensor_config_t> tensor_configs;
436 if (has_tensors) {
437 tensor_configs = get_tensor_configs(rng);
438 }
439 std::vector<std::pair<enum gguf_type, enum gguf_type>> kv_types;
440 if (has_kv) {
441 kv_types = get_kv_types(rng);
442 }
443
444 bool ok = true;
445
446 if (gguf_get_version(gguf_ctx) != GGUF_VERSION) {
447 ok = false;
448 }
449 if (gguf_get_n_tensors(gguf_ctx) != int(tensor_configs.size())) {
450 ok = false;
451 }
452 if (gguf_get_n_kv(gguf_ctx) != int(alignment_defined ? kv_types.size() + 1 : kv_types.size())) {
453 ok = false;
454 }
455
456 return ok;
457}
458
459static bool handcrafted_check_kv(const gguf_context * gguf_ctx, const unsigned int seed, const bool has_tensors, const bool alignment_defined) {
460 if (!gguf_ctx) {
461 return false;
462 }
463
464 std::mt19937 rng(seed);
465
466 std::vector<tensor_config_t> tensor_configs;
467 if (has_tensors) {
468 tensor_configs = get_tensor_configs(rng);
469 }
470
471 std::vector<std::pair<enum gguf_type, enum gguf_type>> kv_types = get_kv_types(rng);
472
473 bool ok = true;
474
475 for (int i = 0; i < int(kv_types.size()); ++i) {
476 const enum gguf_type type = gguf_type(kv_types[i].first);
477 const enum gguf_type type_arr = gguf_type(kv_types[i].second);
478
479 const std::string key = "my_key_" + std::to_string(i);
480
481 uint32_t data[16];
482 for (int j = 0; j < 16; ++j) {
483 data[j] = rng();
484 if (type == GGUF_TYPE_STRING || type_arr == GGUF_TYPE_STRING) {
485 data[j] |= 0x01010101; // avoid random null-termination of string
486 }
487 }
488
489 const char * data8 = reinterpret_cast<const char *>(data);
490 const int id = gguf_find_key(gguf_ctx, key.c_str());
491
492 if (type == GGUF_TYPE_STRING) {
493 const char * str = gguf_get_val_str(gguf_ctx, id);
494 const uint64_t n = strlen(str);
495 const uint64_t n_expected = rng() % sizeof(data);
496 if (n != n_expected) {
497 ok = false;
498 continue;
499 }
500 if (!std::equal(str, str + n, data8)) {
501 ok = false;
502 }
503 continue;
504 }
505
506 if (type == GGUF_TYPE_ARRAY) {
507 const size_t type_size = gguf_type_size(type_arr);
508 const uint64_t arr_n = gguf_get_arr_n(gguf_ctx, id);
509
510 if (type_arr == GGUF_TYPE_STRING) {
511 const uint64_t nstr_expected = rng() % (16 + 1);
512 if (arr_n != nstr_expected) {
513 ok = false;
514 continue;
515 }
516 for (uint64_t istr = 0; istr < nstr_expected; ++istr) {
517 const char * str = gguf_get_arr_str(gguf_ctx, id, istr);
518 const uint64_t n = strlen(str);
519 const uint64_t n_expected = rng() % (sizeof(uint32_t) + 1);
520
521 if (n != n_expected) {
522 ok = false;
523 continue;
524 }
525 const char * str_expected = reinterpret_cast<const char *>(&data[istr]);
526 if (strncmp(str, str_expected, n) != 0) {
527 ok = false;
528 continue;
529 }
530 }
531 continue;
532 }
533
534 const uint64_t arr_n_expected = (rng() % sizeof(data)) / type_size;
535 if (arr_n != arr_n_expected) {
536 ok = false;
537 continue;
538 }
539
540 const char * data_gguf = reinterpret_cast<const char *>(gguf_get_arr_data(gguf_ctx, id));
541
542 if (type_arr == GGUF_TYPE_BOOL) {
543 for (size_t arr_i = 0; arr_i < arr_n; ++arr_i) {
544 if (bool(data8[arr_i]) != bool(data_gguf[arr_i])) {
545 ok = false;
546 }
547 }
548 continue;
549 }
550
551 if (!std::equal(data8, data8 + arr_n*type_size, data_gguf)) {
552 ok = false;
553 }
554 continue;
555 }
556
557 const char * data_gguf = reinterpret_cast<const char *>(gguf_get_val_data(gguf_ctx, id));
558
559 if (type == GGUF_TYPE_BOOL) {
560 if (bool(*data8) != bool(*data_gguf)) {
561 ok = false;
562 }
563 continue;
564 }
565
566 if (!std::equal(data8, data8 + gguf_type_size(type), data_gguf)) {
567 ok = false;
568 }
569 }
570
571 const uint32_t expected_alignment = alignment_defined ? 1 : GGUF_DEFAULT_ALIGNMENT;
572 if (gguf_get_alignment(gguf_ctx) != expected_alignment) {
573 ok = false;
574 }
575
576 return ok;
577}
578
579static bool handcrafted_check_tensors(const gguf_context * gguf_ctx, const unsigned int seed) {
580 if (!gguf_ctx) {
581 return false;
582 }
583
584 std::mt19937 rng(seed);
585
586 std::vector<tensor_config_t> tensor_configs = get_tensor_configs(rng);
587
588 // Call get_kv_types to get the same RNG state:
589 get_kv_types(rng);
590
591 bool ok = true;
592
593 const int id_alignment = gguf_find_key(gguf_ctx, GGUF_KEY_GENERAL_ALIGNMENT);
594 const uint32_t alignment = id_alignment >= 0 ? gguf_get_val_u32(gguf_ctx, id_alignment) : GGUF_DEFAULT_ALIGNMENT;
595
596 uint64_t expected_offset = 0;
597 for (int i = 0; i < int(tensor_configs.size()); ++i) {
598 const ggml_type type = tensor_configs[i].first;
599 const std::array<int64_t, GGML_MAX_DIMS> shape = tensor_configs[i].second;
600
601 const std::string name = "my_tensor_" + std::to_string(i);
602 const int id = gguf_find_tensor(gguf_ctx, name.c_str());
603
604 if (id >= 0) {
605 if (std::string(gguf_get_tensor_name(gguf_ctx, id)) != name) {
606 ok = false;
607 }
608
609 if (gguf_get_tensor_type(gguf_ctx, id) != type) {
610 ok = false;
611 }
612 } else {
613 ok = false;
614 continue;
615 }
616
617 const size_t offset = gguf_get_tensor_offset(gguf_ctx, id);
618
619 if (offset != expected_offset) {
620 ok = false;
621 }
622
623 int64_t ne = shape[0];
624 for (size_t j = 1; j < GGML_MAX_DIMS; ++j) {
625 ne *= shape[j];
626 }
627 expected_offset += GGML_PAD(ggml_row_size(type, ne), alignment);
628 }
629
630 return ok;
631}
632
633static bool handcrafted_check_tensor_data(const gguf_context * gguf_ctx, const unsigned int seed, FILE * file) {
634 if (!gguf_ctx) {
635 return false;
636 }
637
638 std::mt19937 rng(seed);
639
640 std::vector<tensor_config_t> tensor_configs = get_tensor_configs(rng);
641
642 bool ok = true;
643
644 for (int i = 0; i < int(tensor_configs.size()); ++i) {
645 const ggml_type type = tensor_configs[i].first;
646 const std::array<int64_t, GGML_MAX_DIMS> shape = tensor_configs[i].second;
647
648 int64_t ne = shape[0];
649 for (size_t j = 1; j < GGML_MAX_DIMS; ++j) {
650 ne *= shape[j];
651 }
652 const size_t size = ggml_row_size(type, ne);
653
654 const std::string name = "my_tensor_" + std::to_string(i);
655 const size_t offset = gguf_get_tensor_offset(gguf_ctx, gguf_find_tensor(gguf_ctx, name.c_str()));
656
657 std::vector<uint8_t> data(size);
658 GGML_ASSERT(fseek(file, gguf_get_data_offset(gguf_ctx) + offset, SEEK_SET) == 0);
659 GGML_ASSERT(fread(data.data(), 1, data.size(), file) == data.size());
660
661 for (size_t j = 0; j < size; ++j) {
662 const uint8_t expected_byte = (j + offset) % 256;
663 if (data[j] != expected_byte) {
664 ok = false;
665 }
666 }
667 }
668
669 return ok;
670}
671
672static std::pair<int, int> test_handcrafted_file(const unsigned int seed) {
673 int npass = 0;
674 int ntest = 0;
675
676 const std::vector<handcrafted_file_type> hfts = {
677 HANDCRAFTED_HEADER_BAD_MAGIC,
678 HANDCRAFTED_HEADER_BAD_VERSION_0,
679 HANDCRAFTED_HEADER_BAD_VERSION_1,
680 HANDCRAFTED_HEADER_BAD_VERSION_FUTURE,
681 HANDCRAFTED_HEADER_BAD_N_KV,
682 HANDCRAFTED_HEADER_BAD_N_TENSORS,
683 HANDCRAFTED_HEADER_EMPTY,
684
685 HANDCRAFTED_KV_BAD_KEY_SIZE,
686 HANDCRAFTED_KV_BAD_TYPE,
687 HANDCRAFTED_KV_DUPLICATE_KEY,
688 HANDCRAFTED_KV_BAD_ALIGN,
689 HANDCRAFTED_KV_SUCCESS,
690
691 HANDCRAFTED_TENSORS_BAD_NAME_SIZE,
692 HANDCRAFTED_TENSORS_BAD_N_DIMS,
693 HANDCRAFTED_TENSORS_BAD_SHAPE,
694 HANDCRAFTED_TENSORS_NE_TOO_BIG,
695 HANDCRAFTED_TENSORS_NBYTES_TOO_BIG,
696 HANDCRAFTED_TENSORS_BAD_TYPE,
697 HANDCRAFTED_TENSORS_BAD_OFFSET,
698 HANDCRAFTED_TENSORS_DUPLICATE_NAME,
699 HANDCRAFTED_TENSORS_BAD_ALIGN,
700 HANDCRAFTED_TENSORS_INCONSISTENT_ALIGN,
701 HANDCRAFTED_TENSORS_SUCCESS,
702 HANDCRAFTED_TENSORS_CUSTOM_ALIGN,
703
704 HANDCRAFTED_DATA_NOT_ENOUGH_DATA,
705 HANDCRAFTED_DATA_BAD_ALIGN,
706 HANDCRAFTED_DATA_INCONSISTENT_ALIGN,
707 HANDCRAFTED_DATA_SUCCESS,
708 HANDCRAFTED_DATA_CUSTOM_ALIGN,
709 };
710
711 for (enum handcrafted_file_type hft : hfts) {
712 printf("%s: handcrafted_file_type=%s\n", __func__, handcrafted_file_type_name(hft).c_str());
713 FILE * file = get_handcrafted_file(seed, hft);
714
715#ifdef _WIN32
716 if (!file) {
717 printf("failed to create tmpfile(), needs elevated privileges on Windows");
718 printf("skipping tests");
719 continue;
720 }
721#else
722 GGML_ASSERT(file);
723#endif // _WIN32
724
725 struct ggml_context * ctx = nullptr;
726 struct gguf_init_params gguf_params = {
727 /*no_alloc =*/ false,
728 /*ctx =*/ hft >= offset_has_data ? &ctx : nullptr,
729 };
730
731 struct gguf_context * gguf_ctx = gguf_init_from_file_impl(file, gguf_params);
732
733 if (expect_context_not_null(hft)) {
734 printf("%s: - context_not_null: ", __func__);
735 } else {
736 printf("%s: - context_null: ", __func__);
737 }
738 if (bool(gguf_ctx) == expect_context_not_null(hft)) {
739 printf("\033[1;32mOK\033[0m\n");
740 npass++;
741 } else {
742 printf("\033[1;31mFAIL\033[0m\n");
743 }
744 ntest++;
745
746 if (hft >= offset_has_data && !expect_context_not_null(hft)) {
747 printf("%s: - no_dangling_ggml_context_pointer: ", __func__);
748 if (ctx) {
749 printf("\033[1;31mFAIL\033[0m\n");
750 } else {
751 printf("\033[1;32mOK\033[0m\n");
752 npass++;
753 }
754 ntest++;
755 }
756
757 const bool alignment_defined = hft == HANDCRAFTED_TENSORS_CUSTOM_ALIGN || hft == HANDCRAFTED_DATA_CUSTOM_ALIGN;
758
759 if (expect_context_not_null(hft)) {
760 printf("%s: - check_header: ", __func__);
761 if (handcrafted_check_header(gguf_ctx, seed, hft >= offset_has_kv, hft >= offset_has_tensors, alignment_defined)) {
762 printf("\033[1;32mOK\033[0m\n");
763 npass++;
764 } else {
765 printf("\033[1;31mFAIL\033[0m\n");
766 }
767 ntest++;
768 }
769
770 if (expect_context_not_null(hft) && hft >= offset_has_kv) {
771 printf("%s: - check_kv: ", __func__);
772 if (handcrafted_check_kv(gguf_ctx, seed, hft >= offset_has_tensors, alignment_defined)) {
773 printf("\033[1;32mOK\033[0m\n");
774 npass++;
775 } else {
776 printf("\033[1;31mFAIL\033[0m\n");
777 }
778 ntest++;
779 }
780
781 if (expect_context_not_null(hft) && hft >= offset_has_tensors) {
782 printf("%s: - check_tensors: ", __func__);
783 if (handcrafted_check_tensors(gguf_ctx, seed)) {
784 printf("\033[1;32mOK\033[0m\n");
785 npass++;
786 } else {
787 printf("\033[1;31mFAIL\033[0m\n");
788 }
789 ntest++;
790 }
791
792 if (expect_context_not_null(hft) && hft >= offset_has_data) {
793 printf("%s: - check_tensor_data: ", __func__);
794 if (handcrafted_check_tensor_data(gguf_ctx, seed, file)) {
795 printf("\033[1;32mOK\033[0m\n");
796 npass++;
797 } else {
798 printf("\033[1;31mFAIL\033[0m\n");
799 }
800 ntest++;
801 }
802
803 fclose(file);
804 if (gguf_ctx) {
805 ggml_free(ctx);
806 gguf_free(gguf_ctx);
807 }
808 printf("\n");
809 }
810
811
812 return std::make_pair(npass, ntest);
813}
814
815struct random_gguf_context_result {
816 struct gguf_context * gguf_ctx;
817 struct ggml_context * ctx;
818 ggml_backend_buffer_t buffer;
819};
820
821static struct random_gguf_context_result get_random_gguf_context(ggml_backend_t backend, const unsigned int seed) {
822 std::mt19937 rng(seed);
823
824 struct gguf_context * gguf_ctx = gguf_init_empty();
825
826 for (int i = 0; i < 256; ++i) {
827 const std::string key = "my_key_" + std::to_string(rng() % 1024);
828 const enum gguf_type type = gguf_type(rng() % GGUF_TYPE_COUNT);
829
830 switch (type) {
831 case GGUF_TYPE_UINT8: gguf_set_val_u8 (gguf_ctx, key.c_str(), rng() % (1 << 7)); break;
832 case GGUF_TYPE_INT8: gguf_set_val_i8 (gguf_ctx, key.c_str(), rng() % (1 << 7) - (1 << 6)); break;
833 case GGUF_TYPE_UINT16: gguf_set_val_u16 (gguf_ctx, key.c_str(), rng() % (1 << 15)); break;
834 case GGUF_TYPE_INT16: gguf_set_val_i16 (gguf_ctx, key.c_str(), rng() % (1 << 15) - (1 << 14)); break;
835 case GGUF_TYPE_UINT32: gguf_set_val_u32 (gguf_ctx, key.c_str(), rng()); break;
836 case GGUF_TYPE_INT32: gguf_set_val_i32 (gguf_ctx, key.c_str(), rng() - (1 << 30)); break;
837 case GGUF_TYPE_FLOAT32: gguf_set_val_f32 (gguf_ctx, key.c_str(), rng() % 1024 - 512); break;
838 case GGUF_TYPE_BOOL: gguf_set_val_bool(gguf_ctx, key.c_str(), rng() % 2 == 0); break;
839 case GGUF_TYPE_STRING: gguf_set_val_str (gguf_ctx, key.c_str(), std::to_string(rng()).c_str()); break;
840 case GGUF_TYPE_UINT64: gguf_set_val_u64 (gguf_ctx, key.c_str(), rng()); break;
841 case GGUF_TYPE_INT64: gguf_set_val_i64 (gguf_ctx, key.c_str(), rng() - (1 << 30)); break;
842 case GGUF_TYPE_FLOAT64: gguf_set_val_f32 (gguf_ctx, key.c_str(), rng() % 1024 - 512); break;
843 case GGUF_TYPE_ARRAY: {
844 const enum gguf_type type_arr = gguf_type(rng() % GGUF_TYPE_COUNT);
845 const uint64_t ne = rng() % 1024;
846
847 switch (type_arr) {
848 case GGUF_TYPE_UINT8:
849 case GGUF_TYPE_INT8:
850 case GGUF_TYPE_UINT16:
851 case GGUF_TYPE_INT16:
852 case GGUF_TYPE_UINT32:
853 case GGUF_TYPE_INT32:
854 case GGUF_TYPE_FLOAT32:
855 case GGUF_TYPE_BOOL:
856 case GGUF_TYPE_UINT64:
857 case GGUF_TYPE_INT64:
858 case GGUF_TYPE_FLOAT64: {
859 const size_t nbytes = ne*gguf_type_size(type_arr);
860 std::vector<uint32_t> random_data((nbytes + sizeof(uint32_t) - 1) / sizeof(uint32_t));
861 for (size_t j = 0; j < random_data.size(); ++j) {
862 random_data[j] = rng();
863 if (type_arr == GGUF_TYPE_BOOL) {
864 random_data[j] &= 0x01010101; // the sanitizer complains if booleans are not 0 or 1
865 }
866 }
867 gguf_set_arr_data(gguf_ctx, key.c_str(), type_arr, random_data.data(), ne);
868 } break;
869 case GGUF_TYPE_STRING: {
870 std::vector<std::string> data_cpp(ne);
871 std::vector<const char *> data_c(ne);
872 for (size_t j = 0; j < data_cpp.size(); ++j) {
873 data_cpp[j] = std::to_string(rng());
874 data_c[j] = data_cpp[j].c_str();
875 }
876 gguf_set_arr_str(gguf_ctx, key.c_str(), data_c.data(), ne);
877 } break;
878 case GGUF_TYPE_ARRAY: {
879 break; // not supported
880 }
881 case GGUF_TYPE_COUNT:
882 default: {
883 GGML_ABORT("fatal error");
884 }
885 }
886 } break;
887 case GGUF_TYPE_COUNT:
888 default: {
889 GGML_ABORT("fatal error");
890 }
891 }
892 }
893
894 struct ggml_init_params ggml_params = {
895 /*.mem_size =*/ 256*ggml_tensor_overhead(),
896 /*.mem_buffer =*/ nullptr,
897 /*.no_alloc =*/ true,
898 };
899 struct ggml_context * ctx = ggml_init(ggml_params);
900
901 for (int i = 0; i < 256; ++i) {
902 const std::string name = "my_tensor_" + std::to_string(i);
903 const enum ggml_type type = ggml_type(rng() % GGML_TYPE_COUNT);
904 const size_t type_size = ggml_type_size(type);
905
906 if (type_size == 0) {
907 continue;
908 }
909
910 const int n_dims = 1 + rng() % GGML_MAX_DIMS;
911 int64_t ne[GGML_MAX_DIMS];
912 ne[0] = (1 + rng() % 10) * ggml_blck_size(type);
913 for (int j = 1; j < n_dims; ++j) {
914 ne[j] = 1 + rng() % 10;
915 }
916
917 struct ggml_tensor * tensor = ggml_new_tensor(ctx, type, n_dims, ne);
918 ggml_set_name(tensor, name.c_str());
919 }
920
921 ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx, backend);
922 for (struct ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
923 const size_t nbytes = ggml_nbytes(t);
924 std::vector<uint32_t> random_data((nbytes + sizeof(uint32_t) - 1) / sizeof(uint32_t));
925 for (size_t j = 0; j < random_data.size(); ++j) {
926 random_data[j] = rng();
927 }
928 ggml_backend_tensor_set(t, random_data.data(), 0, nbytes);
929
930 gguf_add_tensor(gguf_ctx, t);
931 }
932
933 return {gguf_ctx, ctx, buf};
934}
935
936static bool all_kv_in_other(const gguf_context * ctx, const gguf_context * other) {
937 bool ok = true;
938
939 const int n_kv = gguf_get_n_kv(ctx);
940 for (int id = 0; id < n_kv; ++id) {
941 const char * name = gguf_get_key(ctx, id);
942
943 const int idx_other = gguf_find_key(other, name);
944 if (idx_other < 0) {
945 ok = false;
946 continue;
947 }
948
949 const gguf_type type = gguf_get_kv_type(ctx, id);
950 if (type != gguf_get_kv_type(other, idx_other)) {
951 ok = false;
952 continue;
953 }
954
955 if (type == GGUF_TYPE_ARRAY) {
956 const size_t arr_n = gguf_get_arr_n(ctx, id);
957 if (arr_n != gguf_get_arr_n(other, idx_other)) {
958 ok = false;
959 continue;
960 }
961
962 const gguf_type type_arr = gguf_get_arr_type(ctx, id);
963 if (type_arr != gguf_get_arr_type(other, idx_other)) {
964 ok = false;
965 continue;
966 }
967
968 if (type_arr == GGUF_TYPE_BOOL) {
969 const int8_t * data = reinterpret_cast<const int8_t *>(gguf_get_arr_data(ctx, id));
970 const int8_t * data_other = reinterpret_cast<const int8_t *>(gguf_get_arr_data(other, idx_other));
971 for (size_t arr_i = 0; arr_i < arr_n; ++arr_i) {
972 if (bool(data[arr_i]) != bool(data_other[arr_i])) {
973 ok = false;
974 }
975 }
976 continue;
977 }
978
979 if (type_arr == GGUF_TYPE_STRING) {
980 for (size_t arr_i = 0; arr_i < arr_n; ++arr_i) {
981 const std::string str = gguf_get_arr_str(ctx, id, arr_i);
982 const std::string str_other = gguf_get_arr_str(other, idx_other, arr_i);
983 if (str != str_other) {
984 ok = false;
985 }
986 }
987 continue;
988 }
989
990 const int8_t * data = reinterpret_cast<const int8_t *>(gguf_get_arr_data(ctx, id));
991 const int8_t * data_other = reinterpret_cast<const int8_t *>(gguf_get_arr_data(other, idx_other));
992 if (!std::equal(data, data + arr_n*gguf_type_size(type_arr), data_other)) {
993 ok = false;
994 }
995 continue;
996 }
997
998 if (type == GGUF_TYPE_STRING) {
999 const std::string str = gguf_get_val_str(ctx, id);
1000 const std::string str_other = gguf_get_val_str(other, idx_other);
1001 if (str != str_other) {
1002 ok = false;
1003 }
1004 continue;
1005 }
1006
1007 const char * data = reinterpret_cast<const char *>(gguf_get_val_data(ctx, id));
1008 const char * data_other = reinterpret_cast<const char *>(gguf_get_val_data(other, idx_other));
1009 if (!std::equal(data, data + gguf_type_size(type), data_other)) {
1010 ok = false;
1011 }
1012 }
1013
1014 return ok;
1015}
1016
1017static bool all_tensors_in_other(const gguf_context * ctx, const gguf_context * other) {
1018 bool ok = true;
1019
1020 const int n_tensors = gguf_get_n_tensors(ctx);
1021 for (int id = 0; id < n_tensors; ++id) {
1022 const std::string name = gguf_get_tensor_name(ctx, id);
1023
1024 const int idx_other = gguf_find_tensor(other, name.c_str());
1025 if (id != idx_other) {
1026 ok = false;
1027 if (idx_other < 0) {
1028 continue;
1029 }
1030 }
1031
1032 const ggml_type type = gguf_get_tensor_type(ctx, id);
1033 if (type != gguf_get_tensor_type(other, id)) {
1034 ok = false;
1035 }
1036
1037 const size_t offset = gguf_get_tensor_offset(ctx, id);
1038 if (offset != gguf_get_tensor_offset(other, id)) {
1039 ok = false;
1040 }
1041 }
1042
1043 return ok;
1044}
1045
1046static bool same_tensor_data(const struct ggml_context * orig, const struct ggml_context * read) {
1047 bool ok = true;
1048
1049 struct ggml_tensor * t_orig = ggml_get_first_tensor(orig);
1050 struct ggml_tensor * t_read = ggml_get_first_tensor(read);
1051
1052 if (std::string(t_read->name) != "GGUF tensor data binary blob") {
1053 return false;
1054 }
1055 t_read = ggml_get_next_tensor(read, t_read);
1056
1057 while (t_orig) {
1058 if (!t_read) {
1059 ok = false;
1060 break;
1061 }
1062
1063 const size_t nbytes = ggml_nbytes(t_orig);
1064 if (ggml_nbytes(t_read) != nbytes) {
1065 ok = false;
1066 break;
1067 }
1068 std::vector<char> data_orig(nbytes);
1069 ggml_backend_tensor_get(t_orig, data_orig.data(), 0, nbytes);
1070 if (!std::equal(data_orig.data(), data_orig.data() + nbytes, reinterpret_cast<const char *>(t_read->data))) {
1071 ok = false;
1072 }
1073
1074 t_orig = ggml_get_next_tensor(orig, t_orig);
1075 t_read = ggml_get_next_tensor(read, t_read);
1076 }
1077 if (t_read) {
1078 ok = false;
1079 }
1080
1081 return ok;
1082}
1083
1084static std::pair<int, int> test_roundtrip(ggml_backend_dev_t dev, const unsigned int seed, const bool only_meta) {
1085 ggml_backend_t backend = ggml_backend_dev_init(dev, nullptr);
1086 printf("%s: device=%s, backend=%s, only_meta=%s\n",
1087 __func__, ggml_backend_dev_description(dev), ggml_backend_name(backend), only_meta ? "yes" : "no");
1088
1089 int npass = 0;
1090 int ntest = 0;
1091
1092 struct gguf_context * gguf_ctx_0;
1093 struct ggml_context * ctx_0;
1094 ggml_backend_buffer_t bbuf;
1095 {
1096 struct random_gguf_context_result result = get_random_gguf_context(backend, seed);
1097 gguf_ctx_0 = result.gguf_ctx;
1098 ctx_0 = result.ctx;
1099 bbuf = result.buffer;
1100 }
1101
1102 FILE * file = tmpfile();
1103
1104#ifdef _WIN32
1105 if (!file) {
1106 printf("failed to create tmpfile(), needs elevated privileges on Windows");
1107 printf("skipping tests");
1108 return std::make_pair(0, 0);
1109 }
1110#else
1111 GGML_ASSERT(file);
1112#endif // _WIN32
1113
1114 {
1115 std::vector<int8_t> buf;
1116 gguf_write_to_buf(gguf_ctx_0, buf, only_meta);
1117 GGML_ASSERT(fwrite(buf.data(), 1, buf.size(), file) == buf.size());
1118 rewind(file);
1119 }
1120
1121 struct ggml_context * ctx_1 = nullptr;
1122 struct gguf_init_params gguf_params = {
1123 /*no_alloc =*/ false,
1124 /*ctx =*/ only_meta ? nullptr : &ctx_1,
1125 };
1126 struct gguf_context * gguf_ctx_1 = gguf_init_from_file_impl(file, gguf_params);
1127
1128 printf("%s: same_version: ", __func__);
1129 if (gguf_get_version(gguf_ctx_0) == gguf_get_version(gguf_ctx_1)) {
1130 printf("\033[1;32mOK\033[0m\n");
1131 npass++;
1132 } else {
1133 printf("\033[1;31mFAIL\033[0m\n");
1134 }
1135 ntest++;
1136
1137 printf("%s: same_n_kv: ", __func__);
1138 if (gguf_get_n_kv(gguf_ctx_0) == gguf_get_n_kv(gguf_ctx_1)) {
1139 printf("\033[1;32mOK\033[0m\n");
1140 npass++;
1141 } else {
1142 printf("\033[1;31mFAIL\033[0m\n");
1143 }
1144 ntest++;
1145
1146 printf("%s: same_n_tensors: ", __func__);
1147 if (gguf_get_n_tensors(gguf_ctx_0) == gguf_get_n_tensors(gguf_ctx_1)) {
1148 printf("\033[1;32mOK\033[0m\n");
1149 npass++;
1150 } else {
1151 printf("\033[1;31mFAIL\033[0m\n");
1152 }
1153 ntest++;
1154
1155 printf("%s: all_orig_kv_in_read: ", __func__);
1156 if (all_kv_in_other(gguf_ctx_0, gguf_ctx_1)) {
1157 printf("\033[1;32mOK\033[0m\n");
1158 npass++;
1159 } else {
1160 printf("\033[1;31mFAIL\033[0m\n");
1161 }
1162 ntest++;
1163
1164 printf("%s: all_read_kv_in_orig: ", __func__);
1165 if (all_kv_in_other(gguf_ctx_1, gguf_ctx_0)) {
1166 printf("\033[1;32mOK\033[0m\n");
1167 npass++;
1168 } else {
1169 printf("\033[1;31mFAIL\033[0m\n");
1170 }
1171 ntest++;
1172
1173 printf("%s: all_orig_tensors_in_read: ", __func__);
1174 if (all_tensors_in_other(gguf_ctx_0, gguf_ctx_1)) {
1175 printf("\033[1;32mOK\033[0m\n");
1176 npass++;
1177 } else {
1178 printf("\033[1;31mFAIL\033[0m\n");
1179 }
1180 ntest++;
1181
1182 printf("%s: all_read_tensors_in_orig: ", __func__);
1183 if (all_tensors_in_other(gguf_ctx_1, gguf_ctx_0)) {
1184 printf("\033[1;32mOK\033[0m\n");
1185 npass++;
1186 } else {
1187 printf("\033[1;31mFAIL\033[0m\n");
1188 }
1189 ntest++;
1190
1191 if (!only_meta) {
1192 printf("%s: same_tensor_data: ", __func__);
1193 if (same_tensor_data(ctx_0, ctx_1)) {
1194 printf("\033[1;32mOK\033[0m\n");
1195 npass++;
1196 } else {
1197 printf("\033[1;31mFAIL\033[0m\n");
1198 }
1199 ntest++;
1200 }
1201
1202 ggml_backend_buffer_free(bbuf);
1203 ggml_free(ctx_0);
1204 ggml_free(ctx_1);
1205 gguf_free(gguf_ctx_0);
1206 gguf_free(gguf_ctx_1);
1207 ggml_backend_free(backend);
1208 fclose(file);
1209
1210 printf("\n");
1211 return std::make_pair(npass, ntest);
1212}
1213
1214static std::pair<int, int> test_gguf_set_kv(ggml_backend_dev_t dev, const unsigned int seed) {
1215 ggml_backend_t backend = ggml_backend_dev_init(dev, nullptr);
1216 printf("%s: device=%s, backend=%s\n", __func__, ggml_backend_dev_description(dev), ggml_backend_name(backend));
1217
1218 int npass = 0;
1219 int ntest = 0;
1220
1221 struct gguf_context * gguf_ctx_0;
1222 struct ggml_context * ctx_0;
1223 ggml_backend_buffer_t bbuf_0;
1224 {
1225 struct random_gguf_context_result result = get_random_gguf_context(backend, seed);
1226 gguf_ctx_0 = result.gguf_ctx;
1227 ctx_0 = result.ctx;
1228 bbuf_0 = result.buffer;
1229 }
1230
1231 struct gguf_context * gguf_ctx_1;
1232 struct ggml_context * ctx_1;
1233 ggml_backend_buffer_t bbuf_1;
1234 {
1235 struct random_gguf_context_result result = get_random_gguf_context(backend, seed + 1);
1236 gguf_ctx_1 = result.gguf_ctx;
1237 ctx_1 = result.ctx;
1238 bbuf_1 = result.buffer;
1239 }
1240
1241 struct gguf_context * gguf_ctx_2 = gguf_init_empty();
1242
1243 gguf_set_kv(gguf_ctx_1, gguf_ctx_0);
1244 gguf_set_kv(gguf_ctx_2, gguf_ctx_0);
1245
1246 printf("%s: same_n_kv: ", __func__);
1247 if (gguf_get_n_kv(gguf_ctx_0) == gguf_get_n_kv(gguf_ctx_2)) {
1248 printf("\033[1;32mOK\033[0m\n");
1249 npass++;
1250 } else {
1251 printf("\033[1;31mFAIL\033[0m\n");
1252 }
1253 ntest++;
1254
1255 printf("%s: all_kv_0_in_1: ", __func__);
1256 if (all_kv_in_other(gguf_ctx_0, gguf_ctx_1)) {
1257 printf("\033[1;32mOK\033[0m\n");
1258 npass++;
1259 } else {
1260 printf("\033[1;31mFAIL\033[0m\n");
1261 }
1262 ntest++;
1263
1264 printf("%s: all_kv_0_in_2: ", __func__);
1265 if (all_kv_in_other(gguf_ctx_0, gguf_ctx_2)) {
1266 printf("\033[1;32mOK\033[0m\n");
1267 npass++;
1268 } else {
1269 printf("\033[1;31mFAIL\033[0m\n");
1270 }
1271 ntest++;
1272
1273 gguf_set_kv(gguf_ctx_0, gguf_ctx_1);
1274
1275 printf("%s: same_n_kv_after_double_copy: ", __func__);
1276 if (gguf_get_n_kv(gguf_ctx_0) == gguf_get_n_kv(gguf_ctx_1)) {
1277 printf("\033[1;32mOK\033[0m\n");
1278 npass++;
1279 } else {
1280 printf("\033[1;31mFAIL\033[0m\n");
1281 }
1282 ntest++;
1283
1284 printf("%s: all_kv_1_in_0_after_double_copy: ", __func__);
1285 if (all_kv_in_other(gguf_ctx_1, gguf_ctx_0)) {
1286 printf("\033[1;32mOK\033[0m\n");
1287 npass++;
1288 } else {
1289 printf("\033[1;31mFAIL\033[0m\n");
1290 }
1291 ntest++;
1292
1293 ggml_backend_buffer_free(bbuf_0);
1294 ggml_backend_buffer_free(bbuf_1);
1295 ggml_free(ctx_0);
1296 ggml_free(ctx_1);
1297 gguf_free(gguf_ctx_0);
1298 gguf_free(gguf_ctx_1);
1299 gguf_free(gguf_ctx_2);
1300 ggml_backend_free(backend);
1301
1302 printf("\n");
1303 return std::make_pair(npass, ntest);
1304}
1305
1306static void print_usage() {
1307 printf("usage: test-gguf [seed]\n");
1308 printf(" if no seed is unspecified then a random seed is used\n");
1309}
1310
1311int main(int argc, char ** argv) {
1312 if (argc > 2) {
1313 print_usage();
1314 return 1;
1315 }
1316
1317 std::random_device rd;
1318 const unsigned int seed = argc < 2 ? rd() : std::stoi(argv[1]);
1319
1320 // Initialize ggml backends early so the prints aren't interleaved with the test results:
1321 ggml_backend_dev_count();
1322 fprintf(stderr, "\n");
1323
1324 int npass = 0;
1325 int ntest = 0;
1326 {
1327 std::pair<int, int> result = test_handcrafted_file(seed);
1328 npass += result.first;
1329 ntest += result.second;
1330 }
1331
1332 for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
1333 ggml_backend_dev_t dev = ggml_backend_dev_get(i);
1334
1335 for (bool only_meta : {true, false}) {
1336 std::pair<int, int> result = test_roundtrip(dev, seed, only_meta);
1337 npass += result.first;
1338 ntest += result.second;
1339 }
1340
1341 {
1342 std::pair<int, int> result = test_gguf_set_kv(dev, seed);
1343 npass += result.first;
1344 ntest += result.second;
1345 }
1346 }
1347
1348 printf("%d/%d tests passed\n", npass, ntest);
1349 if (npass != ntest) {
1350 printf("\033[1;31mFAIL\033[0m\n");
1351 return 1;
1352 }
1353 printf("\033[1;32mOK\033[0m\n");
1354 return 0;
1355}