1
0

quantize.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. { "Q2_K", LLAMA_FTYPE_MOSTLY_Q2_K, " 2.63G, +0.6717 ppl @ LLaMA-v1-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.5551 ppl @ LLaMA-v1-7B", },
  21. { "Q3_K_M", LLAMA_FTYPE_MOSTLY_Q3_K_M, " 3.07G, +0.2496 ppl @ LLaMA-v1-7B", },
  22. { "Q3_K_L", LLAMA_FTYPE_MOSTLY_Q3_K_L, " 3.35G, +0.1764 ppl @ LLaMA-v1-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.59G, +0.0992 ppl @ LLaMA-v1-7B", },
  25. { "Q4_K_M", LLAMA_FTYPE_MOSTLY_Q4_K_M, " 3.80G, +0.0532 ppl @ LLaMA-v1-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.0400 ppl @ LLaMA-v1-7B", },
  28. { "Q5_K_M", LLAMA_FTYPE_MOSTLY_Q5_K_M, " 4.45G, +0.0122 ppl @ LLaMA-v1-7B", },
  29. { "Q6_K", LLAMA_FTYPE_MOSTLY_Q6_K, " 5.15G, -0.0008 ppl @ LLaMA-v1-7B", },
  30. { "Q8_0", LLAMA_FTYPE_MOSTLY_Q8_0, " 6.70G, +0.0004 ppl @ LLaMA-v1-7B", },
  31. { "F16", LLAMA_FTYPE_MOSTLY_F16, "13.00G @ 7B", },
  32. { "F32", LLAMA_FTYPE_ALL_F32, "26.00G @ 7B", },
  33. // Note: Ensure COPY comes after F32 to avoid ftype 0 from matching.
  34. { "COPY", LLAMA_FTYPE_ALL_F32, "only copy tensors, no quantizing", },
  35. };
  36. static bool try_parse_ftype(const std::string & ftype_str_in, llama_ftype & ftype, std::string & ftype_str_out) {
  37. std::string ftype_str;
  38. for (auto ch : ftype_str_in) {
  39. ftype_str.push_back(std::toupper(ch));
  40. }
  41. for (auto & it : QUANT_OPTIONS) {
  42. if (it.name == ftype_str) {
  43. ftype = it.ftype;
  44. ftype_str_out = it.name;
  45. return true;
  46. }
  47. }
  48. try {
  49. int ftype_int = std::stoi(ftype_str);
  50. for (auto & it : QUANT_OPTIONS) {
  51. if (it.ftype == ftype_int) {
  52. ftype = it.ftype;
  53. ftype_str_out = it.name;
  54. return true;
  55. }
  56. }
  57. }
  58. catch (...) {
  59. // stoi failed
  60. }
  61. return false;
  62. }
  63. // usage:
  64. // ./quantize [--allow-requantize] [--leave-output-tensor] [--pure] models/llama/ggml-model.gguf [models/llama/ggml-model-quant.gguf] type [nthreads]
  65. //
  66. [[noreturn]]
  67. static void usage(const char * executable) {
  68. printf("usage: %s [--help] [--allow-requantize] [--leave-output-tensor] [--pure] model-f32.gguf [model-quant.gguf] type [nthreads]\n\n", executable);
  69. 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");
  70. printf(" --leave-output-tensor: Will leave output.weight un(re)quantized. Increases model size but may also increase quality, especially when requantizing\n");
  71. printf(" --pure: Disable k-quant mixtures and quantize all tensors to the same type\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 if (strcmp(argv[arg_idx], "--pure") == 0) {
  95. params.pure = true;
  96. } else {
  97. usage(argv[0]);
  98. }
  99. }
  100. if (argc - arg_idx < 2) {
  101. usage(argv[0]);
  102. }
  103. llama_backend_init(false);
  104. // parse command line arguments
  105. const std::string fname_inp = argv[arg_idx];
  106. arg_idx++;
  107. std::string fname_out;
  108. std::string ftype_str;
  109. if (try_parse_ftype(argv[arg_idx], params.ftype, ftype_str)) {
  110. std::string fpath;
  111. const size_t pos = fname_inp.find_last_of("/\\");
  112. if (pos != std::string::npos) {
  113. fpath = fname_inp.substr(0, pos + 1);
  114. }
  115. // export as [inp path]/ggml-model-[ftype].gguf
  116. fname_out = fpath + "ggml-model-" + ftype_str + ".gguf";
  117. arg_idx++;
  118. if (ftype_str == "COPY") {
  119. params.only_copy = true;
  120. }
  121. }
  122. else {
  123. fname_out = argv[arg_idx];
  124. arg_idx++;
  125. if (argc <= arg_idx) {
  126. fprintf(stderr, "%s: missing ftype\n", __func__);
  127. return 1;
  128. }
  129. if (!try_parse_ftype(argv[arg_idx], params.ftype, ftype_str)) {
  130. fprintf(stderr, "%s: invalid ftype '%s'\n", __func__, argv[3]);
  131. return 1;
  132. }
  133. if (ftype_str == "COPY") {
  134. params.only_copy = true;
  135. }
  136. arg_idx++;
  137. }
  138. // parse nthreads
  139. if (argc > arg_idx) {
  140. try {
  141. params.nthread = std::stoi(argv[arg_idx]);
  142. }
  143. catch (const std::exception & e) {
  144. fprintf(stderr, "%s: invalid nthread '%s' (%s)\n", __func__, argv[arg_idx], e.what());
  145. return 1;
  146. }
  147. }
  148. print_build_info();
  149. fprintf(stderr, "%s: quantizing '%s' to '%s' as %s", __func__, fname_inp.c_str(), fname_out.c_str(), ftype_str.c_str());
  150. if (params.nthread > 0) {
  151. fprintf(stderr, " using %d threads", params.nthread);
  152. }
  153. fprintf(stderr, "\n");
  154. const int64_t t_main_start_us = llama_time_us();
  155. int64_t t_quantize_us = 0;
  156. // load the model
  157. {
  158. const int64_t t_start_us = llama_time_us();
  159. if (llama_model_quantize(fname_inp.c_str(), fname_out.c_str(), &params)) {
  160. fprintf(stderr, "%s: failed to quantize model from '%s'\n", __func__, fname_inp.c_str());
  161. return 1;
  162. }
  163. t_quantize_us = llama_time_us() - t_start_us;
  164. }
  165. // report timing
  166. {
  167. const int64_t t_main_end_us = llama_time_us();
  168. printf("\n");
  169. printf("%s: quantize time = %8.2f ms\n", __func__, t_quantize_us/1000.0);
  170. printf("%s: total time = %8.2f ms\n", __func__, (t_main_end_us - t_main_start_us)/1000.0);
  171. }
  172. llama_backend_free();
  173. return 0;
  174. }