quantize.cpp 6.0 KB

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