1
0

quantize.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "build-info.h"
  2. #include "llama.h"
  3. #include <cstdio>
  4. #include <cstring>
  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. {"q5_0", LLAMA_FTYPE_MOSTLY_Q5_0},
  11. {"q5_1", LLAMA_FTYPE_MOSTLY_Q5_1},
  12. {"q8_0", LLAMA_FTYPE_MOSTLY_Q8_0},
  13. {"q2_K", LLAMA_FTYPE_MOSTLY_Q2_K},
  14. {"q3_K", LLAMA_FTYPE_MOSTLY_Q3_K_M},
  15. {"q3_K_S", LLAMA_FTYPE_MOSTLY_Q3_K_S},
  16. {"q3_K_M", LLAMA_FTYPE_MOSTLY_Q3_K_M},
  17. {"q3_K_L", LLAMA_FTYPE_MOSTLY_Q3_K_L},
  18. {"q4_K", LLAMA_FTYPE_MOSTLY_Q4_K_M},
  19. {"q4_K_S", LLAMA_FTYPE_MOSTLY_Q4_K_S},
  20. {"q4_K_M", LLAMA_FTYPE_MOSTLY_Q4_K_M},
  21. {"q5_K", LLAMA_FTYPE_MOSTLY_Q5_K_M},
  22. {"q5_K_S", LLAMA_FTYPE_MOSTLY_Q5_K_S},
  23. {"q5_K_M", LLAMA_FTYPE_MOSTLY_Q5_K_M},
  24. {"q6_K", LLAMA_FTYPE_MOSTLY_Q6_K},
  25. };
  26. bool try_parse_ftype(const std::string & ftype_str, llama_ftype & ftype, std::string & ftype_str_out) {
  27. auto it = LLAMA_FTYPE_MAP.find(ftype_str);
  28. if (it != LLAMA_FTYPE_MAP.end()) {
  29. ftype = it->second;
  30. ftype_str_out = it->first;
  31. return true;
  32. }
  33. // try to parse as an integer
  34. try {
  35. int ftype_int = std::stoi(ftype_str);
  36. for (auto it = LLAMA_FTYPE_MAP.begin(); it != LLAMA_FTYPE_MAP.end(); it++) {
  37. if (it->second == ftype_int) {
  38. ftype = it->second;
  39. ftype_str_out = it->first;
  40. return true;
  41. }
  42. }
  43. }
  44. catch (...) {
  45. // stoi failed
  46. }
  47. return false;
  48. }
  49. // usage:
  50. // ./quantize models/llama/ggml-model.bin [models/llama/ggml-model-quant.bin] type [nthreads]
  51. //
  52. void usage(const char * executable) {
  53. fprintf(stderr, "usage: %s [--help] [--allow-requantize] [--leave-output-tensor] model-f32.bin [model-quant.bin] type [nthreads]\n", executable);
  54. fprintf(stderr, " --allow-requantize: Allows requantizing tensors that have already been quantized. Warning: This can severely reduce quality compared to quantizing from 16bit or 32bit\n");
  55. fprintf(stderr, " --leave-output-tensor: Will leave output.weight un(re)quantized. Increases model size but may also increase quality, especially when requantizing\n");
  56. fprintf(stderr, "Allowed quantization types:\n");
  57. for (auto it = LLAMA_FTYPE_MAP.begin(); it != LLAMA_FTYPE_MAP.end(); it++) {
  58. fprintf(stderr, " type = \"%s\" or %d\n", it->first.c_str(), it->second);
  59. }
  60. exit(1);
  61. }
  62. int main(int argc, char ** argv) {
  63. if (argc < 3) {
  64. usage(argv[0]);
  65. }
  66. llama_model_quantize_params params = llama_model_quantize_default_params();
  67. int arg_idx = 1;
  68. for (; arg_idx < argc && strncmp(argv[arg_idx], "--", 2) == 0; arg_idx++) {
  69. if (strcmp(argv[arg_idx], "--leave-output-tensor") == 0) {
  70. params.quantize_output_tensor = false;
  71. } else if (strcmp(argv[arg_idx], "--allow-requantize") == 0) {
  72. params.allow_requantize = true;
  73. } else {
  74. usage(argv[0]);
  75. }
  76. }
  77. if (argc - arg_idx < 3) {
  78. usage(argv[0]);
  79. }
  80. llama_init_backend();
  81. // parse command line arguments
  82. const std::string fname_inp = argv[arg_idx];
  83. arg_idx++;
  84. std::string fname_out;
  85. std::string ftype_str;
  86. if (try_parse_ftype(argv[arg_idx], params.ftype, ftype_str)) {
  87. std::string fpath;
  88. const size_t pos = fname_inp.find_last_of('/');
  89. if (pos != std::string::npos) {
  90. fpath = fname_inp.substr(0, pos + 1);
  91. }
  92. // export as [inp path]/ggml-model-[ftype].bin
  93. fname_out = fpath + "ggml-model-" + ftype_str + ".bin";
  94. arg_idx++;
  95. }
  96. else {
  97. fname_out = argv[arg_idx];
  98. arg_idx++;
  99. if (argc <= arg_idx) {
  100. fprintf(stderr, "%s: missing ftype\n", __func__);
  101. return 1;
  102. }
  103. if (!try_parse_ftype(argv[arg_idx], params.ftype, ftype_str)) {
  104. fprintf(stderr, "%s: invalid ftype '%s'\n", __func__, argv[3]);
  105. return 1;
  106. }
  107. arg_idx++;
  108. }
  109. // parse nthreads
  110. if (argc > arg_idx) {
  111. try {
  112. params.nthread = std::stoi(argv[arg_idx]);
  113. }
  114. catch (const std::exception & e) {
  115. fprintf(stderr, "%s: invalid nthread '%s' (%s)\n", __func__, argv[arg_idx], e.what());
  116. return 1;
  117. }
  118. }
  119. fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
  120. fprintf(stderr, "%s: quantizing '%s' to '%s' as %s", __func__, fname_inp.c_str(), fname_out.c_str(), ftype_str.c_str());
  121. if (params.nthread > 0) {
  122. fprintf(stderr, " using %d threads", params.nthread);
  123. }
  124. fprintf(stderr, "\n");
  125. const int64_t t_main_start_us = llama_time_us();
  126. int64_t t_quantize_us = 0;
  127. // load the model
  128. {
  129. const int64_t t_start_us = llama_time_us();
  130. if (llama_model_quantize(fname_inp.c_str(), fname_out.c_str(), &params)) {
  131. fprintf(stderr, "%s: failed to quantize model from '%s'\n", __func__, fname_inp.c_str());
  132. return 1;
  133. }
  134. t_quantize_us = llama_time_us() - t_start_us;
  135. }
  136. // report timing
  137. {
  138. const int64_t t_main_end_us = llama_time_us();
  139. printf("\n");
  140. printf("%s: quantize time = %8.2f ms\n", __func__, t_quantize_us/1000.0);
  141. printf("%s: total time = %8.2f ms\n", __func__, (t_main_end_us - t_main_start_us)/1000.0);
  142. }
  143. return 0;
  144. }