quantize.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. static void usage(const char * executable) {
  69. printf("usage: %s [--help] [--allow-requantize] [--leave-output-tensor] model-f32.gguf [model-quant.gguf] type [nthreads]\n\n", executable);
  70. 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");
  71. printf(" --leave-output-tensor: Will leave output.weight un(re)quantized. Increases model size but may also increase quality, especially when requantizing\n");
  72. printf("\nAllowed quantization types:\n");
  73. for (auto & it : QUANT_OPTIONS) {
  74. if (it.name != "COPY") {
  75. printf(" %2d or ", it.ftype);
  76. } else {
  77. printf(" ");
  78. }
  79. printf("%-6s : %s\n", it.name.c_str(), it.desc.c_str());
  80. }
  81. exit(1);
  82. }
  83. int main(int argc, char ** argv) {
  84. if (argc < 3) {
  85. usage(argv[0]);
  86. }
  87. llama_model_quantize_params params = llama_model_quantize_default_params();
  88. int arg_idx = 1;
  89. for (; arg_idx < argc && strncmp(argv[arg_idx], "--", 2) == 0; arg_idx++) {
  90. if (strcmp(argv[arg_idx], "--leave-output-tensor") == 0) {
  91. params.quantize_output_tensor = false;
  92. } else if (strcmp(argv[arg_idx], "--allow-requantize") == 0) {
  93. params.allow_requantize = true;
  94. } else {
  95. usage(argv[0]);
  96. }
  97. }
  98. if (argc - arg_idx < 2) {
  99. usage(argv[0]);
  100. }
  101. llama_backend_init(false);
  102. // parse command line arguments
  103. const std::string fname_inp = argv[arg_idx];
  104. arg_idx++;
  105. std::string fname_out;
  106. std::string ftype_str;
  107. if (try_parse_ftype(argv[arg_idx], params.ftype, ftype_str)) {
  108. std::string fpath;
  109. const size_t pos = fname_inp.find_last_of("/\\");
  110. if (pos != std::string::npos) {
  111. fpath = fname_inp.substr(0, pos + 1);
  112. }
  113. // export as [inp path]/ggml-model-[ftype].gguf
  114. fname_out = fpath + "ggml-model-" + ftype_str + ".gguf";
  115. arg_idx++;
  116. if (ftype_str == "COPY") {
  117. params.only_copy = true;
  118. }
  119. }
  120. else {
  121. fname_out = argv[arg_idx];
  122. arg_idx++;
  123. if (argc <= arg_idx) {
  124. fprintf(stderr, "%s: missing ftype\n", __func__);
  125. return 1;
  126. }
  127. if (!try_parse_ftype(argv[arg_idx], params.ftype, ftype_str)) {
  128. fprintf(stderr, "%s: invalid ftype '%s'\n", __func__, argv[3]);
  129. return 1;
  130. }
  131. if (ftype_str == "COPY") {
  132. params.only_copy = true;
  133. }
  134. arg_idx++;
  135. }
  136. // parse nthreads
  137. if (argc > arg_idx) {
  138. try {
  139. params.nthread = std::stoi(argv[arg_idx]);
  140. }
  141. catch (const std::exception & e) {
  142. fprintf(stderr, "%s: invalid nthread '%s' (%s)\n", __func__, argv[arg_idx], e.what());
  143. return 1;
  144. }
  145. }
  146. print_build_info();
  147. fprintf(stderr, "%s: quantizing '%s' to '%s' as %s", __func__, fname_inp.c_str(), fname_out.c_str(), ftype_str.c_str());
  148. if (params.nthread > 0) {
  149. fprintf(stderr, " using %d threads", params.nthread);
  150. }
  151. fprintf(stderr, "\n");
  152. const int64_t t_main_start_us = llama_time_us();
  153. int64_t t_quantize_us = 0;
  154. // load the model
  155. {
  156. const int64_t t_start_us = llama_time_us();
  157. if (llama_model_quantize(fname_inp.c_str(), fname_out.c_str(), &params)) {
  158. fprintf(stderr, "%s: failed to quantize model from '%s'\n", __func__, fname_inp.c_str());
  159. return 1;
  160. }
  161. t_quantize_us = llama_time_us() - t_start_us;
  162. }
  163. // report timing
  164. {
  165. const int64_t t_main_end_us = llama_time_us();
  166. printf("\n");
  167. printf("%s: quantize time = %8.2f ms\n", __func__, t_quantize_us/1000.0);
  168. printf("%s: total time = %8.2f ms\n", __func__, (t_main_end_us - t_main_start_us)/1000.0);
  169. }
  170. llama_backend_free();
  171. return 0;
  172. }