gguf.cpp 7.5 KB

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