quantize.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "build-info.h"
  2. #include "llama.h"
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <vector>
  6. #include <string>
  7. struct quant_option {
  8. std::string name;
  9. llama_ftype ftype;
  10. std::string desc;
  11. };
  12. static const std::vector<struct quant_option> QUANT_OPTIONS = {
  13. { "Q4_0", LLAMA_FTYPE_MOSTLY_Q4_0, " 3.50G, +0.2499 ppl @ 7B", },
  14. { "Q4_1", LLAMA_FTYPE_MOSTLY_Q4_1, " 3.90G, +0.1846 ppl @ 7B", },
  15. { "Q5_0", LLAMA_FTYPE_MOSTLY_Q5_0, " 4.30G, +0.0796 ppl @ 7B", },
  16. { "Q5_1", LLAMA_FTYPE_MOSTLY_Q5_1, " 4.70G, +0.0415 ppl @ 7B", },
  17. #ifdef GGML_USE_K_QUANTS
  18. { "Q2_K", LLAMA_FTYPE_MOSTLY_Q2_K, " 2.67G, +0.8698 ppl @ 7B", },
  19. { "Q3_K", LLAMA_FTYPE_MOSTLY_Q3_K_M, "alias for Q3_K_M" },
  20. { "Q3_K_S", LLAMA_FTYPE_MOSTLY_Q3_K_S, " 2.75G, +0.5505 ppl @ 7B", },
  21. { "Q3_K_M", LLAMA_FTYPE_MOSTLY_Q3_K_M, " 3.06G, +0.2437 ppl @ 7B", },
  22. { "Q3_K_L", LLAMA_FTYPE_MOSTLY_Q3_K_L, " 3.35G, +0.1803 ppl @ 7B", },
  23. { "Q4_K", LLAMA_FTYPE_MOSTLY_Q4_K_M, "alias for Q4_K_M", },
  24. { "Q4_K_S", LLAMA_FTYPE_MOSTLY_Q4_K_S, " 3.56G, +0.1149 ppl @ 7B", },
  25. { "Q4_K_M", LLAMA_FTYPE_MOSTLY_Q4_K_M, " 3.80G, +0.0535 ppl @ 7B", },
  26. { "Q5_K", LLAMA_FTYPE_MOSTLY_Q5_K_M, "alias for Q5_K_M", },
  27. { "Q5_K_S", LLAMA_FTYPE_MOSTLY_Q5_K_S, " 4.33G, +0.0353 ppl @ 7B", },
  28. { "Q5_K_M", LLAMA_FTYPE_MOSTLY_Q5_K_M, " 4.45G, +0.0142 ppl @ 7B", },
  29. { "Q6_K", LLAMA_FTYPE_MOSTLY_Q6_K, " 5.15G, +0.0044 ppl @ 7B", },
  30. #endif
  31. { "Q8_0", LLAMA_FTYPE_MOSTLY_Q8_0, " 6.70G, +0.0004 ppl @ 7B", },
  32. { "F16", LLAMA_FTYPE_MOSTLY_F16, "13.00G @ 7B", },
  33. { "F32", LLAMA_FTYPE_ALL_F32, "26.00G @ 7B", },
  34. };
  35. bool try_parse_ftype(const std::string & ftype_str_in, llama_ftype & ftype, std::string & ftype_str_out) {
  36. std::string ftype_str;
  37. for (auto ch : ftype_str_in) {
  38. ftype_str.push_back(std::toupper(ch));
  39. }
  40. for (auto & it : QUANT_OPTIONS) {
  41. if (it.name == ftype_str) {
  42. ftype = it.ftype;
  43. ftype_str_out = it.name;
  44. return true;
  45. }
  46. }
  47. try {
  48. int ftype_int = std::stoi(ftype_str);
  49. for (auto & it : QUANT_OPTIONS) {
  50. if (it.ftype == ftype_int) {
  51. ftype = it.ftype;
  52. ftype_str_out = it.name;
  53. return true;
  54. }
  55. }
  56. }
  57. catch (...) {
  58. // stoi failed
  59. }
  60. return false;
  61. }
  62. // usage:
  63. // ./quantize [--allow-requantize] [--leave-output-tensor] models/llama/ggml-model.gguf [models/llama/ggml-model-quant.gguf] type [nthreads]
  64. //
  65. void usage(const char * executable) {
  66. fprintf(stderr, "usage: %s [--help] [--allow-requantize] [--leave-output-tensor] model-f32.gguf [model-quant.gguf] type [nthreads]\n\n", executable);
  67. 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");
  68. fprintf(stderr, " --leave-output-tensor: Will leave output.weight un(re)quantized. Increases model size but may also increase quality, especially when requantizing\n");
  69. fprintf(stderr, "\nAllowed quantization types:\n");
  70. for (auto & it : QUANT_OPTIONS) {
  71. printf(" %2d or %-6s : %s\n", it.ftype, it.name.c_str(), it.desc.c_str());
  72. }
  73. exit(1);
  74. }
  75. int main(int argc, char ** argv) {
  76. if (argc < 3) {
  77. usage(argv[0]);
  78. }
  79. llama_model_quantize_params params = llama_model_quantize_default_params();
  80. int arg_idx = 1;
  81. for (; arg_idx < argc && strncmp(argv[arg_idx], "--", 2) == 0; arg_idx++) {
  82. if (strcmp(argv[arg_idx], "--leave-output-tensor") == 0) {
  83. params.quantize_output_tensor = false;
  84. } else if (strcmp(argv[arg_idx], "--allow-requantize") == 0) {
  85. params.allow_requantize = true;
  86. } else {
  87. usage(argv[0]);
  88. }
  89. }
  90. if (argc - arg_idx < 3) {
  91. usage(argv[0]);
  92. }
  93. llama_backend_init(false);
  94. // parse command line arguments
  95. const std::string fname_inp = argv[arg_idx];
  96. arg_idx++;
  97. std::string fname_out;
  98. std::string ftype_str;
  99. if (try_parse_ftype(argv[arg_idx], params.ftype, ftype_str)) {
  100. std::string fpath;
  101. const size_t pos = fname_inp.find_last_of('/');
  102. if (pos != std::string::npos) {
  103. fpath = fname_inp.substr(0, pos + 1);
  104. }
  105. // export as [inp path]/ggml-model-[ftype].gguf
  106. fname_out = fpath + "ggml-model-" + ftype_str + ".gguf";
  107. arg_idx++;
  108. }
  109. else {
  110. fname_out = argv[arg_idx];
  111. arg_idx++;
  112. if (argc <= arg_idx) {
  113. fprintf(stderr, "%s: missing ftype\n", __func__);
  114. return 1;
  115. }
  116. if (!try_parse_ftype(argv[arg_idx], params.ftype, ftype_str)) {
  117. fprintf(stderr, "%s: invalid ftype '%s'\n", __func__, argv[3]);
  118. return 1;
  119. }
  120. arg_idx++;
  121. }
  122. // parse nthreads
  123. if (argc > arg_idx) {
  124. try {
  125. params.nthread = std::stoi(argv[arg_idx]);
  126. }
  127. catch (const std::exception & e) {
  128. fprintf(stderr, "%s: invalid nthread '%s' (%s)\n", __func__, argv[arg_idx], e.what());
  129. return 1;
  130. }
  131. }
  132. fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
  133. fprintf(stderr, "%s: quantizing '%s' to '%s' as %s", __func__, fname_inp.c_str(), fname_out.c_str(), ftype_str.c_str());
  134. if (params.nthread > 0) {
  135. fprintf(stderr, " using %d threads", params.nthread);
  136. }
  137. fprintf(stderr, "\n");
  138. const int64_t t_main_start_us = llama_time_us();
  139. int64_t t_quantize_us = 0;
  140. // load the model
  141. {
  142. const int64_t t_start_us = llama_time_us();
  143. if (llama_model_quantize(fname_inp.c_str(), fname_out.c_str(), &params)) {
  144. fprintf(stderr, "%s: failed to quantize model from '%s'\n", __func__, fname_inp.c_str());
  145. return 1;
  146. }
  147. t_quantize_us = llama_time_us() - t_start_us;
  148. }
  149. // report timing
  150. {
  151. const int64_t t_main_end_us = llama_time_us();
  152. printf("\n");
  153. printf("%s: quantize time = %8.2f ms\n", __func__, t_quantize_us/1000.0);
  154. printf("%s: total time = %8.2f ms\n", __func__, (t_main_end_us - t_main_start_us)/1000.0);
  155. }
  156. llama_backend_free();
  157. return 0;
  158. }