summaryrefslogtreecommitdiff
path: root/examples/quantize/quantize.cpp
diff options
context:
space:
mode:
authorKerfuffle <44031344+KerfuffleV2@users.noreply.github.com>2023-06-13 04:23:23 -0600
committerGitHub <noreply@github.com>2023-06-13 04:23:23 -0600
commit74d4cfa3438cb58bd177eed30014e6588694aaa8 (patch)
tree8252d459812376411bcf8508ad7d1f059670601a /examples/quantize/quantize.cpp
parent74a6d922f12ccfe16b0c265f43be8978c6f25e98 (diff)
Allow "quantizing" to f16 and f32 (#1787)
* Allow "quantizing" to f16 and f32 Fix an issue where quantizing didn't respect LLAMA_NO_K_QUANTS Add brief help to the list of quantization types in the quantize tool Ignore case for quantization type arguments in the quantize tool
Diffstat (limited to 'examples/quantize/quantize.cpp')
-rw-r--r--examples/quantize/quantize.cpp162
1 files changed, 127 insertions, 35 deletions
diff --git a/examples/quantize/quantize.cpp b/examples/quantize/quantize.cpp
index c6bf1b72..4e8e6f52 100644
--- a/examples/quantize/quantize.cpp
+++ b/examples/quantize/quantize.cpp
@@ -4,43 +4,135 @@
#include <cstdio>
#include <cstring>
-#include <map>
+#include <vector>
#include <string>
-static const std::map<std::string, llama_ftype> LLAMA_FTYPE_MAP = {
- {"q4_0", LLAMA_FTYPE_MOSTLY_Q4_0},
- {"q4_1", LLAMA_FTYPE_MOSTLY_Q4_1},
- {"q5_0", LLAMA_FTYPE_MOSTLY_Q5_0},
- {"q5_1", LLAMA_FTYPE_MOSTLY_Q5_1},
- {"q8_0", LLAMA_FTYPE_MOSTLY_Q8_0},
- {"q2_K", LLAMA_FTYPE_MOSTLY_Q2_K},
- {"q3_K", LLAMA_FTYPE_MOSTLY_Q3_K_M},
- {"q3_K_S", LLAMA_FTYPE_MOSTLY_Q3_K_S},
- {"q3_K_M", LLAMA_FTYPE_MOSTLY_Q3_K_M},
- {"q3_K_L", LLAMA_FTYPE_MOSTLY_Q3_K_L},
- {"q4_K", LLAMA_FTYPE_MOSTLY_Q4_K_M},
- {"q4_K_S", LLAMA_FTYPE_MOSTLY_Q4_K_S},
- {"q4_K_M", LLAMA_FTYPE_MOSTLY_Q4_K_M},
- {"q5_K", LLAMA_FTYPE_MOSTLY_Q5_K_M},
- {"q5_K_S", LLAMA_FTYPE_MOSTLY_Q5_K_S},
- {"q5_K_M", LLAMA_FTYPE_MOSTLY_Q5_K_M},
- {"q6_K", LLAMA_FTYPE_MOSTLY_Q6_K},
+struct quant_option {
+ std::string name;
+ llama_ftype ftype;
+ std::string desc;
};
-bool try_parse_ftype(const std::string & ftype_str, llama_ftype & ftype, std::string & ftype_str_out) {
- auto it = LLAMA_FTYPE_MAP.find(ftype_str);
- if (it != LLAMA_FTYPE_MAP.end()) {
- ftype = it->second;
- ftype_str_out = it->first;
- return true;
+static const std::vector<struct quant_option> QUANT_OPTIONS = {
+ {
+ "Q4_0",
+ LLAMA_FTYPE_MOSTLY_Q4_0,
+ " 3.50G, +0.2499 ppl @ 7B - small, very high quality loss - legacy, prefer using Q3_K_M",
+ },
+ {
+ "Q4_1",
+ LLAMA_FTYPE_MOSTLY_Q4_1,
+ " 3.90G, +0.1846 ppl @ 7B - small, substantial quality loss - legacy, prefer using Q3_K_L",
+ },
+ {
+ "Q5_0",
+ LLAMA_FTYPE_MOSTLY_Q5_0,
+ " 4.30G, +0.0796 ppl @ 7B - medium, balanced quality - legacy, prefer using Q4_K_M",
+ },
+ {
+ "Q5_1",
+ LLAMA_FTYPE_MOSTLY_Q5_1,
+ " 4.70G, +0.0415 ppl @ 7B - medium, low quality loss - legacy, prefer using Q5_K_M",
+ },
+#ifdef GGML_USE_K_QUANTS
+ {
+ "Q2_K",
+ LLAMA_FTYPE_MOSTLY_Q2_K,
+ " 2.67G, +0.8698 ppl @ 7B - smallest, extreme quality loss - not recommended",
+ },
+ {
+ "Q3_K",
+ LLAMA_FTYPE_MOSTLY_Q3_K_M,
+ "alias for Q3_K_M"
+ },
+ {
+ "Q3_K_S",
+ LLAMA_FTYPE_MOSTLY_Q3_K_S,
+ " 2.75G, +0.5505 ppl @ 7B - very small, very high quality loss",
+ },
+ {
+ "Q3_K_M",
+ LLAMA_FTYPE_MOSTLY_Q3_K_M,
+ " 3.06G, +0.2437 ppl @ 7B - very small, very high quality loss",
+ },
+ {
+ "Q3_K_L",
+ LLAMA_FTYPE_MOSTLY_Q3_K_L,
+ " 3.35G, +0.1803 ppl @ 7B - small, substantial quality loss",
+ },
+ {
+ "Q4_K",
+ LLAMA_FTYPE_MOSTLY_Q4_K_M,
+ "alias for Q4_K_M",
+ },
+ {
+ "Q4_K_S",
+ LLAMA_FTYPE_MOSTLY_Q4_K_S,
+ " 3.56G, +0.1149 ppl @ 7B - small, significant quality loss",
+ },
+ {
+ "Q4_K_M",
+ LLAMA_FTYPE_MOSTLY_Q4_K_M,
+ " 3.80G, +0.0535 ppl @ 7B - medium, balanced quality - *recommended*",
+ },
+ {
+ "Q5_K",
+ LLAMA_FTYPE_MOSTLY_Q5_K_M,
+ "alias for Q5_K_M",
+ },
+ {
+ "Q5_K_S",
+ LLAMA_FTYPE_MOSTLY_Q5_K_S,
+ " 4.33G, +0.0353 ppl @ 7B - large, low quality loss - *recommended*",
+ },
+ {
+ "Q5_K_M",
+ LLAMA_FTYPE_MOSTLY_Q5_K_M,
+ " 4.45G, +0.0142 ppl @ 7B - large, very low quality loss - *recommended*",
+ },
+ {
+ "Q6_K",
+ LLAMA_FTYPE_MOSTLY_Q6_K,
+ " 5.15G, +0.0044 ppl @ 7B - very large, extremely low quality loss",
+ },
+#endif
+ {
+ "Q8_0",
+ LLAMA_FTYPE_MOSTLY_Q8_0,
+ " 6.70G, +0.0004 ppl @ 7B - very large, extremely low quality loss - not recommended",
+ },
+ {
+ "F16",
+ LLAMA_FTYPE_MOSTLY_F16,
+ "13.00G @ 7B - extremely large, virtually no quality loss - not recommended",
+ },
+ {
+ "F32",
+ LLAMA_FTYPE_ALL_F32,
+ "26.00G @ 7B - absolutely huge, lossless - not recommended",
+ },
+};
+
+
+bool try_parse_ftype(const std::string & ftype_str_in, llama_ftype & ftype, std::string & ftype_str_out) {
+ std::string ftype_str;
+
+ for (auto ch : ftype_str_in) {
+ ftype_str.push_back(std::toupper(ch));
+ }
+ for (auto & it : QUANT_OPTIONS) {
+ if (it.name == ftype_str) {
+ ftype = it.ftype;
+ ftype_str_out = it.name;
+ return true;
+ }
}
- // try to parse as an integer
try {
int ftype_int = std::stoi(ftype_str);
- for (auto it = LLAMA_FTYPE_MAP.begin(); it != LLAMA_FTYPE_MAP.end(); it++) {
- if (it->second == ftype_int) {
- ftype = it->second;
- ftype_str_out = it->first;
+ for (auto & it : QUANT_OPTIONS) {
+ if (it.ftype == ftype_int) {
+ ftype = it.ftype;
+ ftype_str_out = it.name;
return true;
}
}
@@ -52,15 +144,15 @@ bool try_parse_ftype(const std::string & ftype_str, llama_ftype & ftype, std::st
}
// usage:
-// ./quantize models/llama/ggml-model.bin [models/llama/ggml-model-quant.bin] type [nthreads]
+// ./quantize [--allow-requantize] [--leave-output-tensor] models/llama/ggml-model.bin [models/llama/ggml-model-quant.bin] type [nthreads]
//
void usage(const char * executable) {
- fprintf(stderr, "usage: %s [--help] [--allow-requantize] [--leave-output-tensor] model-f32.bin [model-quant.bin] type [nthreads]\n", executable);
+ fprintf(stderr, "usage: %s [--help] [--allow-requantize] [--leave-output-tensor] model-f32.bin [model-quant.bin] type [nthreads]\n\n", executable);
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");
fprintf(stderr, " --leave-output-tensor: Will leave output.weight un(re)quantized. Increases model size but may also increase quality, especially when requantizing\n");
- fprintf(stderr, "Allowed quantization types:\n");
- for (auto it = LLAMA_FTYPE_MAP.begin(); it != LLAMA_FTYPE_MAP.end(); it++) {
- fprintf(stderr, " type = \"%s\" or %d\n", it->first.c_str(), it->second);
+ fprintf(stderr, "\nAllowed quantization types:\n");
+ for (auto & it : QUANT_OPTIONS) {
+ printf(" %2d or %-6s : %s\n", it.ftype, it.name.c_str(), it.desc.c_str());
}
exit(1);
}