gguf.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "ggml.h"
  2. #include "gguf.h"
  3. #include <cstdio>
  4. #include <string>
  5. #include <sstream>
  6. #include <vector>
  7. #undef MIN
  8. #undef MAX
  9. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  10. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  11. template <typename T>
  12. static std::string to_string(const T & val) {
  13. std::stringstream ss;
  14. ss << val;
  15. return ss.str();
  16. }
  17. static bool gguf_ex_write(const std::string & fname) {
  18. struct gguf_context * ctx = gguf_init_empty();
  19. gguf_set_val_u8 (ctx, "some.parameter.uint8", 0x12);
  20. gguf_set_val_i8 (ctx, "some.parameter.int8", -0x13);
  21. gguf_set_val_u16 (ctx, "some.parameter.uint16", 0x1234);
  22. gguf_set_val_i16 (ctx, "some.parameter.int16", -0x1235);
  23. gguf_set_val_u32 (ctx, "some.parameter.uint32", 0x12345678);
  24. gguf_set_val_i32 (ctx, "some.parameter.int32", -0x12345679);
  25. gguf_set_val_f32 (ctx, "some.parameter.float32", 0.123456789f);
  26. gguf_set_val_u64 (ctx, "some.parameter.uint64", 0x123456789abcdef0ull);
  27. gguf_set_val_i64 (ctx, "some.parameter.int64", -0x123456789abcdef1ll);
  28. gguf_set_val_f64 (ctx, "some.parameter.float64", 0.1234567890123456789);
  29. gguf_set_val_bool(ctx, "some.parameter.bool", true);
  30. gguf_set_val_str (ctx, "some.parameter.string", "hello world");
  31. gguf_set_arr_data(ctx, "some.parameter.arr.i16", GGUF_TYPE_INT16, std::vector<int16_t>{ 1, 2, 3, 4, }.data(), 4);
  32. gguf_set_arr_data(ctx, "some.parameter.arr.f32", GGUF_TYPE_FLOAT32, std::vector<float>{ 3.145f, 2.718f, 1.414f, }.data(), 3);
  33. gguf_set_arr_str (ctx, "some.parameter.arr.str", std::vector<const char *>{ "hello", "world", "!" }.data(), 3);
  34. struct ggml_init_params params = {
  35. /*.mem_size =*/ 128ull*1024ull*1024ull,
  36. /*.mem_buffer =*/ NULL,
  37. /*.no_alloc =*/ false,
  38. };
  39. struct ggml_context * ctx_data = ggml_init(params);
  40. const int n_tensors = 10;
  41. // tensor infos
  42. for (int i = 0; i < n_tensors; ++i) {
  43. const std::string name = "tensor_" + to_string(i);
  44. int64_t ne[GGML_MAX_DIMS] = { 1 };
  45. int32_t n_dims = rand() % GGML_MAX_DIMS + 1;
  46. for (int j = 0; j < n_dims; ++j) {
  47. ne[j] = rand() % 10 + 1;
  48. }
  49. struct ggml_tensor * cur = ggml_new_tensor(ctx_data, GGML_TYPE_F32, n_dims, ne);
  50. ggml_set_name(cur, name.c_str());
  51. {
  52. float * data = (float *) cur->data;
  53. for (int j = 0; j < ggml_nelements(cur); ++j) {
  54. data[j] = 100 + i;
  55. }
  56. }
  57. gguf_add_tensor(ctx, cur);
  58. }
  59. gguf_write_to_file(ctx, fname.c_str(), false);
  60. printf("%s: wrote file '%s;\n", __func__, fname.c_str());
  61. ggml_free(ctx_data);
  62. gguf_free(ctx);
  63. return true;
  64. }
  65. // just read tensor info
  66. static bool gguf_ex_read_0(const std::string & fname) {
  67. struct gguf_init_params params = {
  68. /*.no_alloc = */ false,
  69. /*.ctx = */ NULL,
  70. };
  71. struct gguf_context * ctx = gguf_init_from_file(fname.c_str(), params);
  72. if (!ctx) {
  73. fprintf(stderr, "%s: failed to load '%s'\n", __func__, fname.c_str());
  74. return false;
  75. }
  76. printf("%s: version: %d\n", __func__, gguf_get_version(ctx));
  77. printf("%s: alignment: %zu\n", __func__, gguf_get_alignment(ctx));
  78. printf("%s: data offset: %zu\n", __func__, gguf_get_data_offset(ctx));
  79. // kv
  80. {
  81. const int n_kv = gguf_get_n_kv(ctx);
  82. printf("%s: n_kv: %d\n", __func__, n_kv);
  83. for (int i = 0; i < n_kv; ++i) {
  84. const char * key = gguf_get_key(ctx, i);
  85. printf("%s: kv[%d]: key = %s\n", __func__, i, key);
  86. }
  87. }
  88. // find kv string
  89. {
  90. const char * findkey = "some.parameter.string";
  91. const int keyidx = gguf_find_key(ctx, findkey);
  92. if (keyidx == -1) {
  93. printf("%s: find key: %s not found.\n", __func__, findkey);
  94. } else {
  95. const char * key_value = gguf_get_val_str(ctx, keyidx);
  96. printf("%s: find key: %s found, kv[%d] value = %s\n", __func__, findkey, keyidx, key_value);
  97. }
  98. }
  99. // tensor info
  100. {
  101. const int n_tensors = gguf_get_n_tensors(ctx);
  102. printf("%s: n_tensors: %d\n", __func__, n_tensors);
  103. for (int i = 0; i < n_tensors; ++i) {
  104. const char * name = gguf_get_tensor_name (ctx, i);
  105. const size_t size = gguf_get_tensor_size (ctx, i);
  106. const size_t offset = gguf_get_tensor_offset(ctx, i);
  107. printf("%s: tensor[%d]: name = %s, size = %zu, offset = %zu\n", __func__, i, name, size, offset);
  108. }
  109. }
  110. gguf_free(ctx);
  111. return true;
  112. }
  113. // read and create ggml_context containing the tensors and their data
  114. static bool gguf_ex_read_1(const std::string & fname, bool check_data) {
  115. struct ggml_context * ctx_data = NULL;
  116. struct gguf_init_params params = {
  117. /*.no_alloc = */ false,
  118. /*.ctx = */ &ctx_data,
  119. };
  120. struct gguf_context * ctx = gguf_init_from_file(fname.c_str(), params);
  121. printf("%s: version: %d\n", __func__, gguf_get_version(ctx));
  122. printf("%s: alignment: %zu\n", __func__, gguf_get_alignment(ctx));
  123. printf("%s: data offset: %zu\n", __func__, gguf_get_data_offset(ctx));
  124. // kv
  125. {
  126. const int n_kv = gguf_get_n_kv(ctx);
  127. printf("%s: n_kv: %d\n", __func__, n_kv);
  128. for (int i = 0; i < n_kv; ++i) {
  129. const char * key = gguf_get_key(ctx, i);
  130. printf("%s: kv[%d]: key = %s\n", __func__, i, key);
  131. }
  132. }
  133. // tensor info
  134. {
  135. const int n_tensors = gguf_get_n_tensors(ctx);
  136. printf("%s: n_tensors: %d\n", __func__, n_tensors);
  137. for (int i = 0; i < n_tensors; ++i) {
  138. const char * name = gguf_get_tensor_name (ctx, i);
  139. const size_t size = gguf_get_tensor_size (ctx, i);
  140. const size_t offset = gguf_get_tensor_offset(ctx, i);
  141. const auto type = gguf_get_tensor_type (ctx, i);
  142. const char * type_name = ggml_type_name(type);
  143. const size_t type_size = ggml_type_size(type);
  144. const size_t n_elements = size / type_size;
  145. printf("%s: tensor[%d]: name = %s, size = %zu, offset = %zu, type = %s, n_elts = %zu\n", __func__, i, name, size, offset, type_name, n_elements);
  146. }
  147. }
  148. // data
  149. {
  150. const int n_tensors = gguf_get_n_tensors(ctx);
  151. for (int i = 0; i < n_tensors; ++i) {
  152. printf("%s: reading tensor %d data\n", __func__, i);
  153. const char * name = gguf_get_tensor_name(ctx, i);
  154. struct ggml_tensor * cur = ggml_get_tensor(ctx_data, name);
  155. printf("%s: tensor[%d]: n_dims = %d, ne = (%d, %d, %d, %d), name = %s, data = %p\n",
  156. __func__, i, ggml_n_dims(cur), int(cur->ne[0]), int(cur->ne[1]), int(cur->ne[2]), int(cur->ne[3]), cur->name, cur->data);
  157. // print first 10 elements
  158. const float * data = (const float *) cur->data;
  159. printf("%s data[:10] : ", name);
  160. for (int j = 0; j < MIN(10, ggml_nelements(cur)); ++j) {
  161. printf("%f ", data[j]);
  162. }
  163. printf("\n\n");
  164. // check data
  165. if (check_data) {
  166. const float * data = (const float *) cur->data;
  167. for (int j = 0; j < ggml_nelements(cur); ++j) {
  168. if (data[j] != 100 + i) {
  169. fprintf(stderr, "%s: tensor[%d], data[%d]: found %f, expected %f\n", __func__, i, j, data[j], float(100 + i));
  170. gguf_free(ctx);
  171. return false;
  172. }
  173. }
  174. }
  175. }
  176. }
  177. printf("%s: ctx_data size: %zu\n", __func__, ggml_get_mem_size(ctx_data));
  178. ggml_free(ctx_data);
  179. gguf_free(ctx);
  180. return true;
  181. }
  182. int main(int argc, char ** argv) {
  183. if (argc < 3) {
  184. printf("usage: %s data.gguf r|w [n]\n", argv[0]);
  185. printf("r: read data.gguf file\n");
  186. printf("w: write data.gguf file\n");
  187. printf("n: no check of tensor data\n");
  188. return -1;
  189. }
  190. bool check_data = true;
  191. if (argc == 4) {
  192. check_data = false;
  193. }
  194. srand(123456);
  195. const std::string fname(argv[1]);
  196. const std::string mode (argv[2]);
  197. GGML_ASSERT((mode == "r" || mode == "w") && "mode must be r or w");
  198. if (mode == "w") {
  199. GGML_ASSERT(gguf_ex_write(fname) && "failed to write gguf file");
  200. } else if (mode == "r") {
  201. GGML_ASSERT(gguf_ex_read_0(fname) && "failed to read gguf file");
  202. GGML_ASSERT(gguf_ex_read_1(fname, check_data) && "failed to read gguf file");
  203. }
  204. return 0;
  205. }