1
0

quantize.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "common.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.56G, +0.2166 ppl @ LLaMA-v1-7B", },
  14. { "Q4_1", LLAMA_FTYPE_MOSTLY_Q4_1, " 3.90G, +0.1585 ppl @ LLaMA-v1-7B", },
  15. { "Q5_0", LLAMA_FTYPE_MOSTLY_Q5_0, " 4.33G, +0.0683 ppl @ LLaMA-v1-7B", },
  16. { "Q5_1", LLAMA_FTYPE_MOSTLY_Q5_1, " 4.70G, +0.0349 ppl @ LLaMA-v1-7B", },
  17. #ifdef GGML_USE_K_QUANTS
  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. #endif
  31. { "Q8_0", LLAMA_FTYPE_MOSTLY_Q8_0, " 6.70G, +0.0004 ppl @ LLaMA-v1-7B", },
  32. { "F16", LLAMA_FTYPE_MOSTLY_F16, "13.00G @ 7B", },
  33. { "F32", LLAMA_FTYPE_ALL_F32, "26.00G @ 7B", },
  34. // Note: Ensure COPY comes after F32 to avoid ftype 0 from matching.
  35. { "COPY", LLAMA_FTYPE_ALL_F32, "only copy tensors, no quantizing", },
  36. };
  37. static bool try_parse_ftype(const std::string & ftype_str_in, llama_ftype & ftype, std::string & ftype_str_out) {
  38. std::string ftype_str;
  39. for (auto ch : ftype_str_in) {
  40. ftype_str.push_back(std::toupper(ch));
  41. }
  42. for (auto & it : QUANT_OPTIONS) {
  43. if (it.name == ftype_str) {
  44. ftype = it.ftype;
  45. ftype_str_out = it.name;
  46. return true;
  47. }
  48. }
  49. try {
  50. int ftype_int = std::stoi(ftype_str);
  51. for (auto & it : QUANT_OPTIONS) {
  52. if (it.ftype == ftype_int) {
  53. ftype = it.ftype;
  54. ftype_str_out = it.name;
  55. return true;
  56. }
  57. }
  58. }
  59. catch (...) {
  60. // stoi failed
  61. }
  62. return false;
  63. }
  64. // usage:
  65. // ./quantize [--allow-requantize] [--leave-output-tensor] models/llama/ggml-model.gguf [models/llama/ggml-model-quant.gguf] type [nthreads]
  66. //
  67. static void usage(const char * executable) {
  68. printf("usage: %s [--help] [--allow-requantize] [--leave-output-tensor] 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("\nAllowed quantization types:\n");
  72. for (auto & it : QUANT_OPTIONS) {
  73. if (it.name != "COPY") {
  74. printf(" %2d or ", it.ftype);
  75. } else {
  76. printf(" ");
  77. }
  78. printf("%-6s : %s\n", it.name.c_str(), it.desc.c_str());
  79. }
  80. exit(1);
  81. }
  82. int main(int argc, char ** argv) {
  83. if (argc < 3) {
  84. usage(argv[0]);
  85. }
  86. llama_model_quantize_params params = llama_model_quantize_default_params();
  87. int arg_idx = 1;
  88. for (; arg_idx < argc && strncmp(argv[arg_idx], "--", 2) == 0; arg_idx++) {
  89. if (strcmp(argv[arg_idx], "--leave-output-tensor") == 0) {
  90. params.quantize_output_tensor = false;
  91. } else if (strcmp(argv[arg_idx], "--allow-requantize") == 0) {
  92. params.allow_requantize = true;
  93. } else {
  94. usage(argv[0]);
  95. }
  96. }
  97. if (argc - arg_idx < 2) {
  98. usage(argv[0]);
  99. }
  100. llama_backend_init(false);
  101. // parse command line arguments
  102. const std::string fname_inp = argv[arg_idx];
  103. arg_idx++;
  104. std::string fname_out;
  105. std::string ftype_str;
  106. if (try_parse_ftype(argv[arg_idx], params.ftype, ftype_str)) {
  107. std::string fpath;
  108. const size_t pos = fname_inp.find_last_of("/\\");
  109. if (pos != std::string::npos) {
  110. fpath = fname_inp.substr(0, pos + 1);
  111. }
  112. // export as [inp path]/ggml-model-[ftype].gguf
  113. fname_out = fpath + "ggml-model-" + ftype_str + ".gguf";
  114. arg_idx++;
  115. if (ftype_str == "COPY") {
  116. params.only_copy = true;
  117. }
  118. }
  119. else {
  120. fname_out = argv[arg_idx];
  121. arg_idx++;
  122. if (argc <= arg_idx) {
  123. fprintf(stderr, "%s: missing ftype\n", __func__);
  124. return 1;
  125. }
  126. if (!try_parse_ftype(argv[arg_idx], params.ftype, ftype_str)) {
  127. fprintf(stderr, "%s: invalid ftype '%s'\n", __func__, argv[3]);
  128. return 1;
  129. }
  130. if (ftype_str == "COPY") {
  131. params.only_copy = true;
  132. }
  133. arg_idx++;
  134. }
  135. // parse nthreads
  136. if (argc > arg_idx) {
  137. try {
  138. params.nthread = std::stoi(argv[arg_idx]);
  139. }
  140. catch (const std::exception & e) {
  141. fprintf(stderr, "%s: invalid nthread '%s' (%s)\n", __func__, argv[arg_idx], e.what());
  142. return 1;
  143. }
  144. }
  145. print_build_info();
  146. fprintf(stderr, "%s: quantizing '%s' to '%s' as %s", __func__, fname_inp.c_str(), fname_out.c_str(), ftype_str.c_str());
  147. if (params.nthread > 0) {
  148. fprintf(stderr, " using %d threads", params.nthread);
  149. }
  150. fprintf(stderr, "\n");
  151. const int64_t t_main_start_us = llama_time_us();
  152. int64_t t_quantize_us = 0;
  153. // load the model
  154. {
  155. const int64_t t_start_us = llama_time_us();
  156. if (llama_model_quantize(fname_inp.c_str(), fname_out.c_str(), &params)) {
  157. fprintf(stderr, "%s: failed to quantize model from '%s'\n", __func__, fname_inp.c_str());
  158. return 1;
  159. }
  160. t_quantize_us = llama_time_us() - t_start_us;
  161. }
  162. // report timing
  163. {
  164. const int64_t t_main_end_us = llama_time_us();
  165. printf("\n");
  166. printf("%s: quantize time = %8.2f ms\n", __func__, t_quantize_us/1000.0);
  167. printf("%s: total time = %8.2f ms\n", __func__, (t_main_end_us - t_main_start_us)/1000.0);
  168. }
  169. llama_backend_free();
  170. return 0;
  171. }