quantize.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "ggml/ggml.h"
  2. #include "common.h"
  3. #include "common-ggml.h"
  4. #include <cassert>
  5. #include <cmath>
  6. #include <cstdio>
  7. #include <cstring>
  8. #include <fstream>
  9. #include <map>
  10. #include <string>
  11. #include <vector>
  12. #include <regex>
  13. // default hparams (StableLM 3B)
  14. struct gpt_neox_hparams {
  15. int32_t n_vocab = 50257;
  16. int32_t n_ctx = 4096;
  17. int32_t n_embd = 4096;
  18. int32_t n_head = 32;
  19. int32_t n_layer = 16;
  20. int32_t n_rot = 32; // 0.25 * (n_embd / n_head)
  21. int32_t par_res = 1; // 1 = true, 0 = false
  22. int32_t ftype = 1;
  23. };
  24. // quantize a model
  25. bool gpt_neox_model_quantize(const std::string & fname_inp, const std::string & fname_out, ggml_ftype ftype) {
  26. gpt_vocab vocab;
  27. printf("%s: loading model from '%s'\n", __func__, fname_inp.c_str());
  28. auto finp = std::ifstream(fname_inp, std::ios::binary);
  29. if (!finp) {
  30. fprintf(stderr, "%s: failed to open '%s' for reading\n", __func__, fname_inp.c_str());
  31. return false;
  32. }
  33. auto fout = std::ofstream(fname_out, std::ios::binary);
  34. if (!fout) {
  35. fprintf(stderr, "%s: failed to open '%s' for writing\n", __func__, fname_out.c_str());
  36. return false;
  37. }
  38. // verify magic
  39. {
  40. uint32_t magic;
  41. finp.read((char *) &magic, sizeof(magic));
  42. if (magic != GGML_FILE_MAGIC) {
  43. fprintf(stderr, "%s: invalid model file '%s' (bad magic)\n", __func__, fname_inp.c_str());
  44. return false;
  45. }
  46. fout.write((char *) &magic, sizeof(magic));
  47. }
  48. gpt_neox_hparams hparams;
  49. // load hparams
  50. {
  51. finp.read((char *) &hparams.n_vocab, sizeof(hparams.n_vocab));
  52. finp.read((char *) &hparams.n_ctx, sizeof(hparams.n_ctx));
  53. finp.read((char *) &hparams.n_embd, sizeof(hparams.n_embd));
  54. finp.read((char *) &hparams.n_head, sizeof(hparams.n_head));
  55. finp.read((char *) &hparams.n_layer, sizeof(hparams.n_layer));
  56. finp.read((char *) &hparams.n_rot, sizeof(hparams.n_rot));
  57. finp.read((char *) &hparams.par_res, sizeof(hparams.par_res));
  58. finp.read((char *) &hparams.ftype, sizeof(hparams.ftype));
  59. const int32_t qntvr_src = hparams.ftype / GGML_QNT_VERSION_FACTOR;
  60. const int32_t ftype_dst = GGML_QNT_VERSION * GGML_QNT_VERSION_FACTOR + ftype;
  61. printf("%s: n_vocab = %d\n", __func__, hparams.n_vocab);
  62. printf("%s: n_ctx = %d\n", __func__, hparams.n_ctx);
  63. printf("%s: n_embd = %d\n", __func__, hparams.n_embd);
  64. printf("%s: n_head = %d\n", __func__, hparams.n_head);
  65. printf("%s: n_layer = %d\n", __func__, hparams.n_layer);
  66. printf("%s: par_res = %d\n", __func__, hparams.par_res);
  67. printf("%s: ftype (src) = %d\n", __func__, hparams.ftype);
  68. printf("%s: qntvr (src) = %d\n", __func__, qntvr_src);
  69. printf("%s: ftype (dst) = %d\n", __func__, ftype_dst);
  70. printf("%s: qntvr (dst) = %d\n", __func__, GGML_QNT_VERSION);
  71. fout.write((char *) &hparams.n_vocab, sizeof(hparams.n_vocab));
  72. fout.write((char *) &hparams.n_ctx, sizeof(hparams.n_ctx));
  73. fout.write((char *) &hparams.n_embd, sizeof(hparams.n_embd));
  74. fout.write((char *) &hparams.n_head, sizeof(hparams.n_head));
  75. fout.write((char *) &hparams.n_layer, sizeof(hparams.n_layer));
  76. fout.write((char *) &hparams.n_rot, sizeof(hparams.n_rot));
  77. fout.write((char *) &hparams.par_res, sizeof(hparams.par_res));
  78. fout.write((char *) &ftype_dst, sizeof(ftype_dst));
  79. }
  80. // load vocab
  81. {
  82. const int32_t n_vocab = hparams.n_vocab;
  83. std::string word;
  84. for (int i = 0; i < n_vocab; i++) {
  85. uint32_t len;
  86. finp.read ((char *) &len, sizeof(len));
  87. fout.write((char *) &len, sizeof(len));
  88. word.resize(len);
  89. finp.read ((char *) word.data(), len);
  90. fout.write((char *) word.data(), len);
  91. vocab.token_to_id[word] = i;
  92. vocab.id_to_token[i] = word;
  93. }
  94. }
  95. // regexes of tensor names to be quantized
  96. const std::vector<std::string> to_quant = {
  97. ".*weight",
  98. };
  99. if (!ggml_common_quantize_0(finp, fout, ftype, to_quant, {})) {
  100. fprintf(stderr, "%s: failed to quantize model '%s'\n", __func__, fname_inp.c_str());
  101. return false;
  102. }
  103. finp.close();
  104. fout.close();
  105. return true;
  106. }
  107. // usage:
  108. // ./gpt-neox-quantize models/stalellm2-117M/ggml-model.bin models/stablelm2-117M/ggml-model-quant.bin type
  109. //
  110. int main(int argc, char ** argv) {
  111. if (argc != 4) {
  112. fprintf(stderr, "usage: %s model-f32.bin model-quant.bin type\n", argv[0]);
  113. ggml_print_ftypes(stderr);
  114. return 1;
  115. }
  116. // needed to initialize f16 tables
  117. {
  118. struct ggml_init_params params = { 0, NULL, false };
  119. struct ggml_context * ctx = ggml_init(params);
  120. ggml_free(ctx);
  121. }
  122. const std::string fname_inp = argv[1];
  123. const std::string fname_out = argv[2];
  124. const ggml_ftype ftype = ggml_parse_ftype(argv[3]);
  125. const int64_t t_main_start_us = ggml_time_us();
  126. int64_t t_quantize_us = 0;
  127. // load the model
  128. {
  129. const int64_t t_start_us = ggml_time_us();
  130. if (!gpt_neox_model_quantize(fname_inp, fname_out, ggml_ftype(ftype))) {
  131. fprintf(stderr, "%s: failed to quantize model from '%s'\n", __func__, fname_inp.c_str());
  132. return 1;
  133. }
  134. t_quantize_us = ggml_time_us() - t_start_us;
  135. }
  136. // report timing
  137. {
  138. const int64_t t_main_end_us = ggml_time_us();
  139. printf("\n");
  140. printf("%s: quantize time = %8.2f ms\n", __func__, t_quantize_us/1000.0f);
  141. printf("%s: total time = %8.2f ms\n", __func__, (t_main_end_us - t_main_start_us)/1000.0f);
  142. }
  143. return 0;
  144. }