quantize.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. int32_t ftype = 0;
  20. };
  21. // quantize a model
  22. bool mpt_model_quantize(const std::string & fname_inp,
  23. const std::string & fname_out, ggml_ftype ftype) {
  24. printf("%s: loading model from '%s'\n", __func__, fname_inp.c_str());
  25. auto finp = std::ifstream(fname_inp, std::ios::binary);
  26. if (!finp) {
  27. fprintf(stderr, "%s: failed to open '%s' for reading\n", __func__,
  28. fname_inp.c_str());
  29. return false;
  30. }
  31. auto fout = std::ofstream(fname_out, std::ios::binary);
  32. if (!fout) {
  33. fprintf(stderr, "%s: failed to open '%s' for writing\n", __func__,
  34. fname_out.c_str());
  35. return false;
  36. }
  37. // verify magic
  38. {
  39. uint32_t magic;
  40. finp.read((char *)&magic, sizeof(magic));
  41. if (magic != GGML_FILE_MAGIC) {
  42. fprintf(stderr, "%s: invalid model file '%s' (bad magic)\n",
  43. __func__, fname_inp.c_str());
  44. return false;
  45. }
  46. fout.write((char *)&magic, sizeof(magic));
  47. }
  48. mpt_hparams hparams;
  49. // load hparams
  50. {
  51. finp.read((char *) &hparams.d_model, sizeof(hparams.d_model));
  52. finp.read((char *) &hparams.max_seq_len, sizeof(hparams.max_seq_len));
  53. finp.read((char *) &hparams.n_heads, sizeof(hparams.n_heads));
  54. finp.read((char *) &hparams.n_layers, sizeof(hparams.n_layers));
  55. finp.read((char *) &hparams.n_vocab, sizeof(hparams.n_vocab));
  56. finp.read((char *) &hparams.ftype, sizeof(hparams.ftype));
  57. const int32_t qntvr_src = hparams.ftype / GGML_QNT_VERSION_FACTOR;
  58. const int32_t ftype_dst = GGML_QNT_VERSION * GGML_QNT_VERSION_FACTOR + ftype;
  59. printf("%s: d_model = %d\n", __func__, hparams.d_model);
  60. printf("%s: max_seq_len = %d\n", __func__, hparams.max_seq_len);
  61. printf("%s: n_heads = %d\n", __func__, hparams.n_heads);
  62. printf("%s: n_layers = %d\n", __func__, hparams.n_layers);
  63. printf("%s: n_vocab = %d\n", __func__, hparams.n_vocab);
  64. printf("%s: ftype (src) = %d\n", __func__, hparams.ftype);
  65. printf("%s: qntvr (src) = %d\n", __func__, qntvr_src);
  66. printf("%s: ftype (dst) = %d\n", __func__, ftype_dst);
  67. printf("%s: qntvr (dst) = %d\n", __func__, GGML_QNT_VERSION);
  68. fout.write((char *) &hparams.d_model, sizeof(hparams.d_model));
  69. fout.write((char *) &hparams.max_seq_len, sizeof(hparams.max_seq_len));
  70. fout.write((char *) &hparams.n_heads, sizeof(hparams.n_heads));
  71. fout.write((char *) &hparams.n_layers, sizeof(hparams.n_layers));
  72. fout.write((char *) &hparams.n_vocab, sizeof(hparams.n_vocab));
  73. fout.write((char *) &ftype_dst, sizeof(ftype_dst));
  74. }
  75. // load vocab
  76. {
  77. const int32_t n_vocab = hparams.n_vocab;
  78. std::string word;
  79. for (int i = 0; i < n_vocab; i++) {
  80. uint32_t len;
  81. finp.read((char *)&len, sizeof(len));
  82. fout.write((char *)&len, sizeof(len));
  83. word.resize(len);
  84. finp.read((char *)word.data(), len);
  85. fout.write((char *)word.data(), len);
  86. float prob;
  87. finp.read((char *)&prob, sizeof(prob));
  88. fout.write((char *)&prob, sizeof(prob));
  89. }
  90. }
  91. printf("%s: quantizing tensors\n", __func__);
  92. // regexes of tensor names to be quantized
  93. const std::vector<std::string> to_quant = {
  94. ".*weight",
  95. };
  96. if (!ggml_common_quantize_0(finp, fout, ftype, to_quant, {})) {
  97. fprintf(stderr, "%s: failed to quantize model '%s'\n", __func__,
  98. fname_inp.c_str());
  99. return false;
  100. }
  101. finp.close();
  102. fout.close();
  103. return true;
  104. }
  105. // usage:
  106. // ./replit-quantize models/replit/ggml-model.bin
  107. // models/replit/ggml-model-quant.bin type
  108. //
  109. int main(int argc, char ** argv) {
  110. if (argc != 4) {
  111. fprintf(stderr, "usage: %s model-f32.bin model-quant.bin type\n",
  112. 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 (!mpt_model_quantize(fname_inp, fname_out, ggml_ftype(ftype))) {
  131. fprintf(stderr, "%s: failed to quantize model from '%s'\n",
  132. __func__, fname_inp.c_str());
  133. return 1;
  134. }
  135. t_quantize_us = ggml_time_us() - t_start_us;
  136. }
  137. // report timing
  138. {
  139. const int64_t t_main_end_us = ggml_time_us();
  140. printf("\n");
  141. printf("%s: quantize time = %8.2f ms\n", __func__,
  142. t_quantize_us / 1000.0f);
  143. printf("%s: total time = %8.2f ms\n", __func__,
  144. (t_main_end_us - t_main_start_us) / 1000.0f);
  145. }
  146. return 0;
  147. }