44
55#include < cstdio>
66#include < cstring>
7- #include < map >
7+ #include < vector >
88#include < string>
99
10- static const std::map<std::string, llama_ftype> LLAMA_FTYPE_MAP = {
11- {" q4_0" , LLAMA_FTYPE_MOSTLY_Q4_0},
12- {" q4_1" , LLAMA_FTYPE_MOSTLY_Q4_1},
13- {" q5_0" , LLAMA_FTYPE_MOSTLY_Q5_0},
14- {" q5_1" , LLAMA_FTYPE_MOSTLY_Q5_1},
15- {" q8_0" , LLAMA_FTYPE_MOSTLY_Q8_0},
16- {" q2_K" , LLAMA_FTYPE_MOSTLY_Q2_K},
17- {" q3_K" , LLAMA_FTYPE_MOSTLY_Q3_K_M},
18- {" q3_K_S" , LLAMA_FTYPE_MOSTLY_Q3_K_S},
19- {" q3_K_M" , LLAMA_FTYPE_MOSTLY_Q3_K_M},
20- {" q3_K_L" , LLAMA_FTYPE_MOSTLY_Q3_K_L},
21- {" q4_K" , LLAMA_FTYPE_MOSTLY_Q4_K_M},
22- {" q4_K_S" , LLAMA_FTYPE_MOSTLY_Q4_K_S},
23- {" q4_K_M" , LLAMA_FTYPE_MOSTLY_Q4_K_M},
24- {" q5_K" , LLAMA_FTYPE_MOSTLY_Q5_K_M},
25- {" q5_K_S" , LLAMA_FTYPE_MOSTLY_Q5_K_S},
26- {" q5_K_M" , LLAMA_FTYPE_MOSTLY_Q5_K_M},
27- {" q6_K" , LLAMA_FTYPE_MOSTLY_Q6_K},
10+ struct quant_option {
11+ std::string name;
12+ llama_ftype ftype;
13+ std::string desc;
2814};
2915
30- bool try_parse_ftype (const std::string & ftype_str, llama_ftype & ftype, std::string & ftype_str_out) {
31- auto it = LLAMA_FTYPE_MAP.find (ftype_str);
32- if (it != LLAMA_FTYPE_MAP.end ()) {
33- ftype = it->second ;
34- ftype_str_out = it->first ;
35- return true ;
16+ static const std::vector<struct quant_option > QUANT_OPTIONS = {
17+ {
18+ " Q4_0" ,
19+ LLAMA_FTYPE_MOSTLY_Q4_0,
20+ " 3.50G, +0.2499 ppl @ 7B - small, very high quality loss - legacy, prefer using Q3_K_M" ,
21+ },
22+ {
23+ " Q4_1" ,
24+ LLAMA_FTYPE_MOSTLY_Q4_1,
25+ " 3.90G, +0.1846 ppl @ 7B - small, substantial quality loss - legacy, prefer using Q3_K_L" ,
26+ },
27+ {
28+ " Q5_0" ,
29+ LLAMA_FTYPE_MOSTLY_Q5_0,
30+ " 4.30G, +0.0796 ppl @ 7B - medium, balanced quality - legacy, prefer using Q4_K_M" ,
31+ },
32+ {
33+ " Q5_1" ,
34+ LLAMA_FTYPE_MOSTLY_Q5_1,
35+ " 4.70G, +0.0415 ppl @ 7B - medium, low quality loss - legacy, prefer using Q5_K_M" ,
36+ },
37+ #ifdef GGML_USE_K_QUANTS
38+ {
39+ " Q2_K" ,
40+ LLAMA_FTYPE_MOSTLY_Q2_K,
41+ " 2.67G, +0.8698 ppl @ 7B - smallest, extreme quality loss - not recommended" ,
42+ },
43+ {
44+ " Q3_K" ,
45+ LLAMA_FTYPE_MOSTLY_Q3_K_M,
46+ " alias for Q3_K_M"
47+ },
48+ {
49+ " Q3_K_S" ,
50+ LLAMA_FTYPE_MOSTLY_Q3_K_S,
51+ " 2.75G, +0.5505 ppl @ 7B - very small, very high quality loss" ,
52+ },
53+ {
54+ " Q3_K_M" ,
55+ LLAMA_FTYPE_MOSTLY_Q3_K_M,
56+ " 3.06G, +0.2437 ppl @ 7B - very small, very high quality loss" ,
57+ },
58+ {
59+ " Q3_K_L" ,
60+ LLAMA_FTYPE_MOSTLY_Q3_K_L,
61+ " 3.35G, +0.1803 ppl @ 7B - small, substantial quality loss" ,
62+ },
63+ {
64+ " Q4_K" ,
65+ LLAMA_FTYPE_MOSTLY_Q4_K_M,
66+ " alias for Q4_K_M" ,
67+ },
68+ {
69+ " Q4_K_S" ,
70+ LLAMA_FTYPE_MOSTLY_Q4_K_S,
71+ " 3.56G, +0.1149 ppl @ 7B - small, significant quality loss" ,
72+ },
73+ {
74+ " Q4_K_M" ,
75+ LLAMA_FTYPE_MOSTLY_Q4_K_M,
76+ " 3.80G, +0.0535 ppl @ 7B - medium, balanced quality - *recommended*" ,
77+ },
78+ {
79+ " Q5_K" ,
80+ LLAMA_FTYPE_MOSTLY_Q5_K_M,
81+ " alias for Q5_K_M" ,
82+ },
83+ {
84+ " Q5_K_S" ,
85+ LLAMA_FTYPE_MOSTLY_Q5_K_S,
86+ " 4.33G, +0.0353 ppl @ 7B - large, low quality loss - *recommended*" ,
87+ },
88+ {
89+ " Q5_K_M" ,
90+ LLAMA_FTYPE_MOSTLY_Q5_K_M,
91+ " 4.45G, +0.0142 ppl @ 7B - large, very low quality loss - *recommended*" ,
92+ },
93+ {
94+ " Q6_K" ,
95+ LLAMA_FTYPE_MOSTLY_Q6_K,
96+ " 5.15G, +0.0044 ppl @ 7B - very large, extremely low quality loss" ,
97+ },
98+ #endif
99+ {
100+ " Q8_0" ,
101+ LLAMA_FTYPE_MOSTLY_Q8_0,
102+ " 6.70G, +0.0004 ppl @ 7B - very large, extremely low quality loss - not recommended" ,
103+ },
104+ {
105+ " F16" ,
106+ LLAMA_FTYPE_MOSTLY_F16,
107+ " 13.00G @ 7B - extremely large, virtually no quality loss - not recommended" ,
108+ },
109+ {
110+ " F32" ,
111+ LLAMA_FTYPE_ALL_F32,
112+ " 26.00G @ 7B - absolutely huge, lossless - not recommended" ,
113+ },
114+ };
115+
116+
117+ bool try_parse_ftype (const std::string & ftype_str_in, llama_ftype & ftype, std::string & ftype_str_out) {
118+ std::string ftype_str;
119+
120+ for (auto ch : ftype_str_in) {
121+ ftype_str.push_back (std::toupper (ch));
122+ }
123+ for (auto & it : QUANT_OPTIONS) {
124+ if (it.name == ftype_str) {
125+ ftype = it.ftype ;
126+ ftype_str_out = it.name ;
127+ return true ;
128+ }
36129 }
37- // try to parse as an integer
38130 try {
39131 int ftype_int = std::stoi (ftype_str);
40- for (auto it = LLAMA_FTYPE_MAP. begin (); it != LLAMA_FTYPE_MAP. end (); it++ ) {
41- if (it-> second == ftype_int) {
42- ftype = it-> second ;
43- ftype_str_out = it-> first ;
132+ for (auto & it : QUANT_OPTIONS ) {
133+ if (it. ftype == ftype_int) {
134+ ftype = it. ftype ;
135+ ftype_str_out = it. name ;
44136 return true ;
45137 }
46138 }
@@ -52,15 +144,15 @@ bool try_parse_ftype(const std::string & ftype_str, llama_ftype & ftype, std::st
52144}
53145
54146// usage:
55- // ./quantize models/llama/ggml-model.bin [models/llama/ggml-model-quant.bin] type [nthreads]
147+ // ./quantize [--allow-requantize] [--leave-output-tensor] models/llama/ggml-model.bin [models/llama/ggml-model-quant.bin] type [nthreads]
56148//
57149void usage (const char * executable) {
58- fprintf (stderr, " usage: %s [--help] [--allow-requantize] [--leave-output-tensor] model-f32.bin [model-quant.bin] type [nthreads]\n " , executable);
150+ fprintf (stderr, " usage: %s [--help] [--allow-requantize] [--leave-output-tensor] model-f32.bin [model-quant.bin] type [nthreads]\n\n " , executable);
59151 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 " );
60152 fprintf (stderr, " --leave-output-tensor: Will leave output.weight un(re)quantized. Increases model size but may also increase quality, especially when requantizing\n " );
61- fprintf (stderr, " Allowed quantization types:\n " );
62- for (auto it = LLAMA_FTYPE_MAP. begin (); it != LLAMA_FTYPE_MAP. end (); it++ ) {
63- fprintf (stderr, " type = \" %s \" or %d \n " , it-> first . c_str (), it-> second );
153+ fprintf (stderr, " \n Allowed quantization types:\n " );
154+ for (auto & it : QUANT_OPTIONS ) {
155+ printf ( " %2d or %-6s : %s \n " , it. ftype , it. name . c_str (), it. desc . c_str () );
64156 }
65157 exit (1 );
66158}
0 commit comments