test-gguf.cpp 45 KB

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