quantize.cpp 6.7 KB

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