quantize.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 (GPT-2 117M)
  14. struct starcoder_hparams {
  15. int32_t n_vocab = 49280;
  16. int32_t n_ctx = 2048;
  17. int32_t n_embd = 2048;
  18. int32_t n_head = 16;
  19. int32_t n_layer = 24;
  20. int32_t ftype = 1;
  21. };
  22. // quantize a model
  23. bool starcoder_model_quantize(const std::string & fname_inp, const std::string & fname_out, ggml_ftype ftype) {
  24. gpt_vocab vocab;
  25. printf("%s: loading model from '%s'\n", __func__, fname_inp.c_str());
  26. auto finp = std::ifstream(fname_inp, std::ios::binary);
  27. if (!finp) {
  28. fprintf(stderr, "%s: failed to open '%s' for reading\n", __func__, 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__, fname_out.c_str());
  34. return false;
  35. }
  36. // verify magic
  37. {
  38. uint32_t magic;
  39. finp.read((char *) &magic, sizeof(magic));
  40. if (magic != GGML_FILE_MAGIC) {
  41. fprintf(stderr, "%s: invalid model file '%s' (bad magic)\n", __func__, fname_inp.c_str());
  42. return false;
  43. }
  44. fout.write((char *) &magic, sizeof(magic));
  45. }
  46. starcoder_hparams hparams;
  47. // load hparams
  48. {
  49. finp.read((char *) &hparams.n_vocab, sizeof(hparams.n_vocab));
  50. finp.read((char *) &hparams.n_ctx, sizeof(hparams.n_ctx));
  51. finp.read((char *) &hparams.n_embd, sizeof(hparams.n_embd));
  52. finp.read((char *) &hparams.n_head, sizeof(hparams.n_head));
  53. finp.read((char *) &hparams.n_layer, sizeof(hparams.n_layer));
  54. finp.read((char *) &hparams.ftype, sizeof(hparams.ftype));
  55. const int32_t qntvr_src = hparams.ftype / GGML_QNT_VERSION_FACTOR;
  56. const int32_t ftype_dst = GGML_QNT_VERSION * GGML_QNT_VERSION_FACTOR + ftype;
  57. printf("%s: n_vocab = %d\n", __func__, hparams.n_vocab);
  58. printf("%s: n_ctx = %d\n", __func__, hparams.n_ctx);
  59. printf("%s: n_embd = %d\n", __func__, hparams.n_embd);
  60. printf("%s: n_head = %d\n", __func__, hparams.n_head);
  61. printf("%s: n_layer = %d\n", __func__, hparams.n_layer);
  62. printf("%s: ftype (src) = %d\n", __func__, hparams.ftype);
  63. printf("%s: qntvr (src) = %d\n", __func__, qntvr_src);
  64. printf("%s: ftype (dst) = %d\n", __func__, ftype_dst);
  65. printf("%s: qntvr (dst) = %d\n", __func__, GGML_QNT_VERSION);
  66. fout.write((char *) &hparams.n_vocab, sizeof(hparams.n_vocab));
  67. fout.write((char *) &hparams.n_ctx, sizeof(hparams.n_ctx));
  68. fout.write((char *) &hparams.n_embd, sizeof(hparams.n_embd));
  69. fout.write((char *) &hparams.n_head, sizeof(hparams.n_head));
  70. fout.write((char *) &hparams.n_layer, sizeof(hparams.n_layer));
  71. fout.write((char *) &ftype_dst, sizeof(ftype_dst));
  72. }
  73. // load vocab
  74. {
  75. int32_t n_vocab = 0;
  76. finp.read ((char *) &n_vocab, sizeof(n_vocab));
  77. fout.write((char *) &n_vocab, sizeof(n_vocab));
  78. if (n_vocab != hparams.n_vocab) {
  79. fprintf(stderr, "%s: invalid model file '%s' (bad vocab size %d != %d)\n",
  80. __func__, fname_inp.c_str(), n_vocab, hparams.n_vocab);
  81. return false;
  82. }
  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. "model/wte",
  98. "model/lm_head",
  99. "model/h.*/attn/c_attn/w",
  100. "model/h.*/attn/c_proj/w",
  101. "model/h.*/mlp/c_fc/w",
  102. "model/h.*/mlp/c_proj/w",
  103. };
  104. if (!ggml_common_quantize_0(finp, fout, ftype, to_quant, {})) {
  105. fprintf(stderr, "%s: failed to quantize model '%s'\n", __func__, fname_inp.c_str());
  106. return false;
  107. }
  108. finp.close();
  109. fout.close();
  110. return true;
  111. }
  112. // usage:
  113. // ./gpt-2-quantize models/gpt-2-117M/ggml-model.bin models/gpt-2-117M/ggml-model-quant.bin type
  114. //
  115. int main(int argc, char ** argv) {
  116. if (argc != 4) {
  117. fprintf(stderr, "usage: %s model-f32.bin model-quant.bin type\n", 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 (!starcoder_model_quantize(fname_inp, fname_out, ggml_ftype(ftype))) {
  136. fprintf(stderr, "%s: failed to quantize model from '%s'\n", __func__, fname_inp.c_str());
  137. return 1;
  138. }
  139. t_quantize_us = ggml_time_us() - t_start_us;
  140. }
  141. // report timing
  142. {
  143. const int64_t t_main_end_us = ggml_time_us();
  144. printf("\n");
  145. printf("%s: quantize time = %8.2f ms\n", __func__, t_quantize_us/1000.0f);
  146. printf("%s: total time = %8.2f ms\n", __func__, (t_main_end_us - t_main_start_us)/1000.0f);
  147. }
  148. return 0;
  149. }