quantize.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "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 (Whisper tiny)
  14. struct whisper_hparams {
  15. int32_t n_vocab = 51864;
  16. int32_t n_audio_ctx = 1500;
  17. int32_t n_audio_state = 384;
  18. int32_t n_audio_head = 6;
  19. int32_t n_audio_layer = 4;
  20. int32_t n_text_ctx = 448;
  21. int32_t n_text_state = 384;
  22. int32_t n_text_head = 6;
  23. int32_t n_text_layer = 4;
  24. int32_t n_mels = 80;
  25. int32_t ftype = 1;
  26. };
  27. struct whisper_filters {
  28. int32_t n_mel;
  29. int32_t n_fft;
  30. std::vector<float> data;
  31. };
  32. // quantize a model
  33. bool whisper_model_quantize(const std::string & fname_inp, const std::string & fname_out, ggml_ftype ftype) {
  34. gpt_vocab vocab;
  35. printf("%s: loading model from '%s'\n", __func__, fname_inp.c_str());
  36. auto finp = std::ifstream(fname_inp, std::ios::binary);
  37. if (!finp) {
  38. fprintf(stderr, "%s: failed to open '%s' for reading\n", __func__, fname_inp.c_str());
  39. return false;
  40. }
  41. auto fout = std::ofstream(fname_out, std::ios::binary);
  42. if (!fout) {
  43. fprintf(stderr, "%s: failed to open '%s' for writing\n", __func__, fname_out.c_str());
  44. return false;
  45. }
  46. // verify magic
  47. {
  48. uint32_t magic;
  49. finp.read((char *) &magic, sizeof(magic));
  50. if (magic != GGML_FILE_MAGIC) {
  51. fprintf(stderr, "%s: invalid model file '%s' (bad magic)\n", __func__, fname_inp.c_str());
  52. return false;
  53. }
  54. fout.write((char *) &magic, sizeof(magic));
  55. }
  56. whisper_hparams hparams;
  57. // load hparams
  58. {
  59. finp.read((char *) &hparams.n_vocab, sizeof(hparams.n_vocab));
  60. finp.read((char *) &hparams.n_audio_ctx, sizeof(hparams.n_audio_ctx));
  61. finp.read((char *) &hparams.n_audio_state, sizeof(hparams.n_audio_state));
  62. finp.read((char *) &hparams.n_audio_head, sizeof(hparams.n_audio_head));
  63. finp.read((char *) &hparams.n_audio_layer, sizeof(hparams.n_audio_layer));
  64. finp.read((char *) &hparams.n_text_ctx, sizeof(hparams.n_text_ctx));
  65. finp.read((char *) &hparams.n_text_state, sizeof(hparams.n_text_state));
  66. finp.read((char *) &hparams.n_text_head, sizeof(hparams.n_text_head));
  67. finp.read((char *) &hparams.n_text_layer, sizeof(hparams.n_text_layer));
  68. finp.read((char *) &hparams.n_mels, sizeof(hparams.n_mels));
  69. finp.read((char *) &hparams.ftype, sizeof(hparams.ftype));
  70. const int32_t qntvr_src = hparams.ftype / GGML_QNT_VERSION_FACTOR;
  71. const int32_t ftype_dst = GGML_QNT_VERSION * GGML_QNT_VERSION_FACTOR + ftype;
  72. fprintf(stderr, "%s: n_vocab = %d\n", __func__, hparams.n_vocab);
  73. fprintf(stderr, "%s: n_audio_ctx = %d\n", __func__, hparams.n_audio_ctx);
  74. fprintf(stderr, "%s: n_audio_state = %d\n", __func__, hparams.n_audio_state);
  75. fprintf(stderr, "%s: n_audio_head = %d\n", __func__, hparams.n_audio_head);
  76. fprintf(stderr, "%s: n_audio_layer = %d\n", __func__, hparams.n_audio_layer);
  77. fprintf(stderr, "%s: n_text_ctx = %d\n", __func__, hparams.n_text_ctx);
  78. fprintf(stderr, "%s: n_text_state = %d\n", __func__, hparams.n_text_state);
  79. fprintf(stderr, "%s: n_text_head = %d\n", __func__, hparams.n_text_head);
  80. fprintf(stderr, "%s: n_text_layer = %d\n", __func__, hparams.n_text_layer);
  81. fprintf(stderr, "%s: n_mels = %d\n", __func__, hparams.n_mels);
  82. fprintf(stderr, "%s: ftype (src) = %d\n", __func__, hparams.ftype);
  83. fprintf(stderr, "%s: qntvr (src) = %d\n", __func__, qntvr_src);
  84. fprintf(stderr, "%s: ftype (dst) = %d\n", __func__, ftype_dst);
  85. fprintf(stderr, "%s: qntvr (dst) = %d\n", __func__, GGML_QNT_VERSION);
  86. fout.write((const char *) &hparams.n_vocab, sizeof(hparams.n_vocab));
  87. fout.write((const char *) &hparams.n_audio_ctx, sizeof(hparams.n_audio_ctx));
  88. fout.write((const char *) &hparams.n_audio_state, sizeof(hparams.n_audio_state));
  89. fout.write((const char *) &hparams.n_audio_head, sizeof(hparams.n_audio_head));
  90. fout.write((const char *) &hparams.n_audio_layer, sizeof(hparams.n_audio_layer));
  91. fout.write((const char *) &hparams.n_text_ctx, sizeof(hparams.n_text_ctx));
  92. fout.write((const char *) &hparams.n_text_state, sizeof(hparams.n_text_state));
  93. fout.write((const char *) &hparams.n_text_head, sizeof(hparams.n_text_head));
  94. fout.write((const char *) &hparams.n_text_layer, sizeof(hparams.n_text_layer));
  95. fout.write((const char *) &hparams.n_mels, sizeof(hparams.n_mels));
  96. fout.write((const char *) &ftype_dst, sizeof(hparams.ftype));
  97. }
  98. // load mel filters
  99. {
  100. whisper_filters filters;
  101. finp.read ((char *) &filters.n_mel, sizeof(filters.n_mel));
  102. fout.write((char *) &filters.n_mel, sizeof(filters.n_mel));
  103. finp.read ((char *) &filters.n_fft, sizeof(filters.n_fft));
  104. fout.write((char *) &filters.n_fft, sizeof(filters.n_fft));
  105. filters.data.resize(filters.n_mel * filters.n_fft);
  106. finp.read ((char *) filters.data.data(), filters.data.size() * sizeof(float));
  107. fout.write((char *) filters.data.data(), filters.data.size() * sizeof(float));
  108. }
  109. // load vocab
  110. {
  111. int32_t n_vocab = 0;
  112. finp.read ((char *) &n_vocab, sizeof(n_vocab));
  113. fout.write((char *) &n_vocab, sizeof(n_vocab));
  114. //if (n_vocab != hparams.n_vocab) {
  115. // fprintf(stderr, "%s: invalid model file '%s' (bad vocab size %d != %d)\n",
  116. // __func__, fname_inp.c_str(), n_vocab, hparams.n_vocab);
  117. // return false;
  118. //}
  119. char word[129];
  120. for (int i = 0; i < n_vocab; i++) {
  121. uint32_t len;
  122. finp.read ((char *) &len, sizeof(len));
  123. fout.write((char *) &len, sizeof(len));
  124. word[len] = '\0';
  125. finp.read ((char *) word, len);
  126. fout.write((char *) word, len);
  127. vocab.token_to_id[word] = i;
  128. vocab.id_to_token[i] = word;
  129. }
  130. }
  131. // regexes of tensor names to not be quantized
  132. const std::vector<std::string> to_skip = {
  133. //"encoder.*",
  134. "encoder.conv1.bias",
  135. "encoder.conv2.bias",
  136. "encoder.positional_embedding",
  137. "decoder.positional_embedding",
  138. };
  139. if (!ggml_common_quantize_0(finp, fout, ftype, { ".*" }, to_skip)) {
  140. fprintf(stderr, "%s: failed to quantize model '%s'\n", __func__, fname_inp.c_str());
  141. return false;
  142. }
  143. finp.close();
  144. fout.close();
  145. return true;
  146. }
  147. int main(int argc, char ** argv) {
  148. if (argc != 4) {
  149. fprintf(stderr, "usage: %s model-f32.bin model-quant.bin type\n", argv[0]);
  150. ggml_print_ftypes(stderr);
  151. return 1;
  152. }
  153. // needed to initialize f16 tables
  154. {
  155. struct ggml_init_params params = { 0, NULL, false };
  156. struct ggml_context * ctx = ggml_init(params);
  157. ggml_free(ctx);
  158. }
  159. const std::string fname_inp = argv[1];
  160. const std::string fname_out = argv[2];
  161. const ggml_ftype ftype = ggml_parse_ftype(argv[3]);
  162. const int64_t t_main_start_us = ggml_time_us();
  163. int64_t t_quantize_us = 0;
  164. // load the model
  165. {
  166. const int64_t t_start_us = ggml_time_us();
  167. if (!whisper_model_quantize(fname_inp, fname_out, ggml_ftype(ftype))) {
  168. fprintf(stderr, "%s: failed to quantize model from '%s'\n", __func__, fname_inp.c_str());
  169. return 1;
  170. }
  171. t_quantize_us = ggml_time_us() - t_start_us;
  172. }
  173. // report timing
  174. {
  175. const int64_t t_main_end_us = ggml_time_us();
  176. printf("\n");
  177. printf("%s: quantize time = %8.2f ms\n", __func__, t_quantize_us/1000.0f);
  178. printf("%s: total time = %8.2f ms\n", __func__, (t_main_end_us - t_main_start_us)/1000.0f);
  179. }
  180. return 0;
  181. }