quantize.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "ggml.h"
  2. #include "llama.h"
  3. #include "build-info.h"
  4. #include <cstdio>
  5. #include <map>
  6. #include <string>
  7. static const std::map<std::string, llama_ftype> LLAMA_FTYPE_MAP = {
  8. {"q4_0", LLAMA_FTYPE_MOSTLY_Q4_0},
  9. {"q4_1", LLAMA_FTYPE_MOSTLY_Q4_1},
  10. {"q4_2", LLAMA_FTYPE_MOSTLY_Q4_2},
  11. {"q5_0", LLAMA_FTYPE_MOSTLY_Q5_0},
  12. {"q5_1", LLAMA_FTYPE_MOSTLY_Q5_1},
  13. {"q8_0", LLAMA_FTYPE_MOSTLY_Q8_0},
  14. };
  15. bool try_parse_ftype(const std::string & ftype_str, llama_ftype & ftype, std::string & ftype_str_out) {
  16. auto it = LLAMA_FTYPE_MAP.find(ftype_str);
  17. if (it != LLAMA_FTYPE_MAP.end()) {
  18. ftype = it->second;
  19. ftype_str_out = it->first;
  20. return true;
  21. }
  22. // try to parse as an integer
  23. try {
  24. int ftype_int = std::stoi(ftype_str);
  25. for (auto it = LLAMA_FTYPE_MAP.begin(); it != LLAMA_FTYPE_MAP.end(); it++) {
  26. if (it->second == ftype_int) {
  27. ftype = it->second;
  28. ftype_str_out = it->first;
  29. return true;
  30. }
  31. }
  32. }
  33. catch (...) {
  34. // stoi failed
  35. }
  36. return false;
  37. }
  38. // usage:
  39. // ./quantize models/llama/ggml-model.bin [models/llama/ggml-model-quant.bin] type [nthreads]
  40. //
  41. int main(int argc, char ** argv) {
  42. ggml_time_init();
  43. if (argc < 3) {
  44. fprintf(stderr, "usage: %s model-f32.bin [model-quant.bin] type [nthreads]\n", argv[0]);
  45. for (auto it = LLAMA_FTYPE_MAP.begin(); it != LLAMA_FTYPE_MAP.end(); it++) {
  46. fprintf(stderr, " type = \"%s\" or %d\n", it->first.c_str(), it->second);
  47. }
  48. return 1;
  49. }
  50. // needed to initialize f16 tables
  51. {
  52. struct ggml_init_params params = { 0, NULL, false };
  53. struct ggml_context * ctx = ggml_init(params);
  54. ggml_free(ctx);
  55. }
  56. // parse command line arguments
  57. const std::string fname_inp = argv[1];
  58. std::string fname_out;
  59. int nthread;
  60. llama_ftype ftype;
  61. int arg_idx = 2;
  62. std::string ftype_str;
  63. if (try_parse_ftype(argv[arg_idx], ftype, ftype_str)) {
  64. // argv[2] is the ftype
  65. std::string fpath;
  66. const size_t pos = fname_inp.find_last_of('/');
  67. if (pos != std::string::npos) {
  68. fpath = fname_inp.substr(0, pos + 1);
  69. }
  70. // export as [inp path]/ggml-model-[ftype].bin
  71. fname_out = fpath + "ggml-model-" + ftype_str + ".bin";
  72. arg_idx++;
  73. }
  74. else {
  75. // argv[2] is the output path
  76. fname_out = argv[arg_idx];
  77. arg_idx++;
  78. if (argc <= arg_idx) {
  79. fprintf(stderr, "%s: missing ftype\n", __func__);
  80. return 1;
  81. }
  82. // argv[3] is the ftype
  83. if (!try_parse_ftype(argv[arg_idx], ftype, ftype_str)) {
  84. fprintf(stderr, "%s: invalid ftype '%s'\n", __func__, argv[3]);
  85. return 1;
  86. }
  87. arg_idx++;
  88. }
  89. // parse nthreads
  90. if (argc > arg_idx) {
  91. try {
  92. nthread = std::stoi(argv[arg_idx]);
  93. }
  94. catch (const std::exception & e) {
  95. fprintf(stderr, "%s: invalid nthread '%s' (%s)\n", __func__, argv[arg_idx], e.what());
  96. return 1;
  97. }
  98. } else {
  99. nthread = 0;
  100. }
  101. fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
  102. fprintf(stderr, "%s: quantizing '%s' to '%s' as %s", __func__, fname_inp.c_str(), fname_out.c_str(), ftype_str.c_str());
  103. if (nthread > 0) {
  104. fprintf(stderr, " using %d threads", nthread);
  105. }
  106. fprintf(stderr, "\n");
  107. const int64_t t_main_start_us = ggml_time_us();
  108. int64_t t_quantize_us = 0;
  109. // load the model
  110. {
  111. const int64_t t_start_us = ggml_time_us();
  112. if (llama_model_quantize(fname_inp.c_str(), fname_out.c_str(), ftype, nthread)) {
  113. fprintf(stderr, "%s: failed to quantize model from '%s'\n", __func__, fname_inp.c_str());
  114. return 1;
  115. }
  116. t_quantize_us = ggml_time_us() - t_start_us;
  117. }
  118. // report timing
  119. {
  120. const int64_t t_main_end_us = ggml_time_us();
  121. printf("\n");
  122. printf("%s: quantize time = %8.2f ms\n", __func__, t_quantize_us/1000.0);
  123. printf("%s: total time = %8.2f ms\n", __func__, (t_main_end_us - t_main_start_us)/1000.0);
  124. }
  125. return 0;
  126. }